UNPKG

@fecp/mobile

Version:

42 lines (41 loc) 1.44 kB
"use strict"; Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" }); function useCursor() { let selectionInfo; function recordCursor(inputElement) { if (inputElement == void 0) return; const { selectionStart, selectionEnd, value } = inputElement; if (selectionStart == null || selectionEnd == null) return; const beforeTxt = value.slice(0, Math.max(0, selectionStart)); const afterTxt = value.slice(Math.max(0, selectionEnd)); selectionInfo = { selectionStart, selectionEnd, value, beforeTxt, afterTxt }; } function setCursor(inputElement) { if (inputElement == void 0 || selectionInfo == void 0) return; const { value } = inputElement; const { beforeTxt, afterTxt, selectionStart } = selectionInfo; 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; } } inputElement.setSelectionRange(startPos, startPos); } return [recordCursor, setCursor]; } exports.useCursor = useCursor;