12345678910111213141516171819202122232425262728293031323334353637383940414243444546 |
- import { ref } from 'vue';
- function useCursor(input) {
- const selectionRef = ref();
- function recordCursor() {
- if (input.value == void 0)
- return;
- const { selectionStart, selectionEnd, value } = input.value;
- if (selectionStart == null || selectionEnd == null)
- return;
- const beforeTxt = value.slice(0, Math.max(0, selectionStart));
- const afterTxt = value.slice(Math.max(0, selectionEnd));
- selectionRef.value = {
- selectionStart,
- selectionEnd,
- value,
- beforeTxt,
- afterTxt
- };
- }
- function setCursor() {
- if (input.value == void 0 || selectionRef.value == void 0)
- return;
- const { value } = input.value;
- const { beforeTxt, afterTxt, selectionStart } = selectionRef.value;
- if (beforeTxt == void 0 || afterTxt == void 0 || selectionStart == void 0)
- return;
- let startPos = value.length;
- if (value.endsWith(afterTxt)) {
- startPos = value.length - afterTxt.length;
- } else if (value.startsWith(beforeTxt)) {
- startPos = beforeTxt.length;
- } else {
- const beforeLastChar = beforeTxt[selectionStart - 1];
- const newIndex = value.indexOf(beforeLastChar, selectionStart - 1);
- if (newIndex !== -1) {
- startPos = newIndex + 1;
- }
- }
- input.value.setSelectionRange(startPos, startPos);
- }
- return [recordCursor, setCursor];
- }
- export { useCursor };
- //# sourceMappingURL=index.mjs.map
|