diff --git a/src/lib/actions/selectionStart.ts b/src/lib/actions/selectionStart.ts new file mode 100644 index 000000000..bd4299927 --- /dev/null +++ b/src/lib/actions/selectionStart.ts @@ -0,0 +1,28 @@ +import type { Action } from 'svelte/action'; + +type Props = { + onChange: (selectionStart: number) => void; +}; + +export const selectionStart: Action = (node, { onChange }) => { + const handler = () => { + onChange(node.selectionStart); + }; + node.addEventListener('click', handler); + node.addEventListener('keydown', handler); + node.addEventListener('keyup', handler); + + return { + update({ onChange }) { + const handler = () => { + onChange(node.selectionStart); + }; + node.addEventListener('click', handler); + node.addEventListener('keydown', handler); + node.addEventListener('keyup', handler); + }, + destroy() { + onChange(-1); + } + }; +}; diff --git a/src/lib/components/eventModal.svelte b/src/lib/components/eventModal.svelte index 3686b8fad..8f01d32e5 100644 --- a/src/lib/components/eventModal.svelte +++ b/src/lib/components/eventModal.svelte @@ -3,8 +3,9 @@ import { Modal } from '$lib/components'; import { Pill } from '$lib/elements'; import { createEventDispatcher } from 'svelte'; - import { at, empty, last } from '$lib/helpers/array'; + import { at, empty } from '$lib/helpers/array'; import Copy from './copy.svelte'; + import { selectionStart } from '$lib/actions/selectionStart'; // Props export let show = false; @@ -186,11 +187,28 @@ } } - function getHelperStr(input: string): string { + function getHelperStr(input: string, selectionStart?: number): string { const fields = input.split('.'); - const secondToLastField = at(fields, -3); - const prevField = at(fields, -2); - const currField = last(fields); + let fieldIndex = -1; + + if (selectionStart > -1) { + let currentIndex = 0; + let arrayIndex = 0; + + for (const item of fields) { + currentIndex += item.length + 1; + if (currentIndex > selectionStart) { + fieldIndex = arrayIndex; + break; + } + arrayIndex++; + } + } + console.log(selectionStart, fieldIndex); + + const currField = at(fields, fieldIndex); + const prevField = at(fields, fieldIndex - 1); + const secondToLastField = at(fields, fieldIndex - 2); if (fields.length === 1) return 'service'; if (isAttribute(currField) || isAction(prevField)) return 'attribute'; @@ -304,8 +322,10 @@ } $: if (showInput) { - helper = getHelperStr(customInput); + helper = getHelperStr(customInput, customInputCursor); } + + let customInputCursor = -1; @@ -379,7 +399,11 @@ {#if showInput}
- + (customInputCursor = newValue) }} />