{"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;EACtB,IAAI,MAAM,SAAS,KAAA,GAAW;EAE9B,MAAM,EAAE,gBAAgB,cAAc,UAAU,MAAM;EAEtD,IAAI,kBAAkB,QAAQ,gBAAgB,MAAM;EAKpD,gBAAgB;GACd;GACA;GACA;GACA,WAPgB,MAAM,MAAM,GAAG,KAAK,IAAI,GAAG,eAAe,CAOjD;GACT,UAPe,MAAM,MAAM,KAAK,IAAI,GAAG,aAAa,CAO5C;GACT;;CAEH,SAAS,YAAY;EACnB,IAAI,MAAM,SAAS,KAAA,KAAa,iBAAiB,KAAA,GAAW;EAE5D,MAAM,EAAE,UAAU,MAAM;EACxB,MAAM,EAAE,WAAW,UAAU,mBAAmB;EAEhD,IACE,aAAa,KAAA,KACb,YAAY,KAAA,KACZ,kBAAkB,KAAA,GAElB;EAEF,IAAI,WAAW,MAAM;EAErB,IAAI,MAAM,SAAS,SAAS,EAC1B,WAAW,MAAM,SAAS,SAAS;OAC9B,IAAI,MAAM,WAAW,UAAU,EACpC,WAAW,UAAU;OAChB;GACL,MAAM,iBAAiB,UAAU,iBAAiB;GAClD,MAAM,WAAW,MAAM,QAAQ,gBAAgB,iBAAiB,EAAE;GAClE,IAAI,aAAa,IACf,WAAW,WAAW;;EAI1B,MAAM,MAAM,kBAAkB,UAAU,SAAS;;CAGnD,OAAO,CAAC,cAAc,UAAU"}