element-plus
Version:
A Component Library for Vue 3
1 lines • 2.59 kB
Source Map (JSON)
{"version":3,"file":"index.mjs","names":[],"sources":["../../../../../packages/hooks/use-cursor/index.ts"],"sourcesContent":["import type { ShallowRef } from 'vue'\n\ninterface SelectionInfo {\n selectionStart?: number\n selectionEnd?: number\n value?: string\n beforeTxt?: string\n afterTxt?: string\n}\n\n// Keep input cursor in the correct position when we use formatter.\nexport function useCursor(\n input: ShallowRef<HTMLInputElement | undefined>\n): [() => void, () => void] {\n let selectionInfo: SelectionInfo\n function recordCursor() {\n if (input.value == undefined) return\n\n const { selectionStart, selectionEnd, value } = input.value\n\n if (selectionStart == null || selectionEnd == null) return\n\n const beforeTxt = value.slice(0, Math.max(0, selectionStart))\n const afterTxt = value.slice(Math.max(0, selectionEnd))\n\n selectionInfo = {\n selectionStart,\n selectionEnd,\n value,\n beforeTxt,\n afterTxt,\n }\n }\n function setCursor() {\n if (input.value == undefined || selectionInfo == undefined) return\n\n const { value } = input.value\n const { beforeTxt, afterTxt, selectionStart } = selectionInfo\n\n if (\n beforeTxt == undefined ||\n afterTxt == undefined ||\n selectionStart == undefined\n )\n return\n\n let startPos = value.length\n\n if (value.endsWith(afterTxt)) {\n startPos = value.length - afterTxt.length\n } else if (value.startsWith(beforeTxt)) {\n startPos = beforeTxt.length\n } else {\n const beforeLastChar = beforeTxt[selectionStart - 1]\n const newIndex = value.indexOf(beforeLastChar, selectionStart - 1)\n if (newIndex !== -1) {\n startPos = newIndex + 1\n }\n }\n\n input.value.setSelectionRange(startPos, startPos)\n }\n\n return [recordCursor, setCursor]\n}\n"],"mappings":";AAWA,SAAgB,UACd,OAC0B;CAC1B,IAAI;CACJ,SAAS,eAAe;AACtB,MAAI,MAAM,SAAS,OAAW;EAE9B,MAAM,EAAE,gBAAgB,cAAc,UAAU,MAAM;AAEtD,MAAI,kBAAkB,QAAQ,gBAAgB,KAAM;AAKpD,kBAAgB;GACd;GACA;GACA;GACA,WAPgB,MAAM,MAAM,GAAG,KAAK,IAAI,GAAG,eAAe,CAAC;GAQ3D,UAPe,MAAM,MAAM,KAAK,IAAI,GAAG,aAAa,CAAC;GAQtD;;CAEH,SAAS,YAAY;AACnB,MAAI,MAAM,SAAS,UAAa,iBAAiB,OAAW;EAE5D,MAAM,EAAE,UAAU,MAAM;EACxB,MAAM,EAAE,WAAW,UAAU,mBAAmB;AAEhD,MACE,aAAa,UACb,YAAY,UACZ,kBAAkB,OAElB;EAEF,IAAI,WAAW,MAAM;AAErB,MAAI,MAAM,SAAS,SAAS,CAC1B,YAAW,MAAM,SAAS,SAAS;WAC1B,MAAM,WAAW,UAAU,CACpC,YAAW,UAAU;OAChB;GACL,MAAM,iBAAiB,UAAU,iBAAiB;GAClD,MAAM,WAAW,MAAM,QAAQ,gBAAgB,iBAAiB,EAAE;AAClE,OAAI,aAAa,GACf,YAAW,WAAW;;AAI1B,QAAM,MAAM,kBAAkB,UAAU,SAAS;;AAGnD,QAAO,CAAC,cAAc,UAAU"}