From ce0220612efd4e2cd76cb8d5a6c6cb3d82735e96 Mon Sep 17 00:00:00 2001 From: Polaris-tl <38718927+Polaris-tl@users.noreply.github.com> Date: Sun, 28 Jul 2024 18:22:44 +0800 Subject: [PATCH] perf(hooks): replace the ref object with a plain object (#15061) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: 唐铃 --- packages/hooks/use-cursor/index.ts | 25 ++++++++++++------------- 1 file changed, 12 insertions(+), 13 deletions(-) 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 ||