diff --git a/packages/hooks/use-cursor/index.ts b/packages/hooks/use-cursor/index.ts index 3cc49b4752..11f93d95ca 100644 --- a/packages/hooks/use-cursor/index.ts +++ b/packages/hooks/use-cursor/index.ts @@ -1,19 +1,18 @@ -import { ref } from 'vue' - import type { ShallowRef } from 'vue' +interface SelectionInfo { + selectionStart?: number + selectionEnd?: number + value?: string + beforeTxt?: string + afterTxt?: string +} + // Keep input cursor in the correct position when we use formatter. export function useCursor( input: ShallowRef ): [() => void, () => void] { - const selectionRef = ref<{ - selectionStart?: number - selectionEnd?: number - value?: string - beforeTxt?: string - afterTxt?: string - }>() - + let selectionInfo: SelectionInfo function recordCursor() { if (input.value == undefined) return @@ -24,7 +23,7 @@ export function useCursor( const beforeTxt = value.slice(0, Math.max(0, selectionStart)) const afterTxt = value.slice(Math.max(0, selectionEnd)) - selectionRef.value = { + selectionInfo = { selectionStart, selectionEnd, value, @@ -33,10 +32,10 @@ export function useCursor( } } function setCursor() { - if (input.value == undefined || selectionRef.value == undefined) return + if (input.value == undefined || selectionInfo == undefined) return const { value } = input.value - const { beforeTxt, afterTxt, selectionStart } = selectionRef.value + const { beforeTxt, afterTxt, selectionStart } = selectionInfo if ( beforeTxt == undefined ||