mirror of
https://github.com/element-plus/element-plus.git
synced 2024-11-27 02:01:15 +08:00
perf(hooks): replace the ref object with a plain object (#15061)
Co-authored-by: 唐铃 <ling.tang01@geely.com>
This commit is contained in:
parent
2fce7b469b
commit
ce0220612e
@ -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<HTMLInputElement | undefined>
|
||||
): [() => 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 ||
|
||||
|
Loading…
Reference in New Issue
Block a user