{"version":3,"file":"get-caret-coordinates.cjs","names":[],"sources":["../../../../src/components/Mentions/utils/get-caret-coordinates.ts"],"sourcesContent":["/*\n * Derived from textarea-caret 3.1.0 (https://github.com/component/textarea-caret-position)\n * — MIT, Copyright (c) 2015 Jonathan Ong. See ../LICENSE for the full license text.\n *\n * Local changes compared to textarea-caret 3.1.0:\n * - The mirror of an input uses white-space: nowrap (no wrapping, like the input's single\n *   horizontally scrolling line) instead of pre-wrap, whitespace is kept with non-breaking spaces\n * - height falls back to the font size when the computed line-height is not a number\n *   (line-height: normal)\n * - No Firefox overflow special case and no debug mode, returns a zero rect on the server\n * - TypeScript types adjusted for the Mantine build, formatted with oxfmt\n */\n\nexport interface CaretCoordinates {\n  top: number;\n  left: number;\n  height: number;\n}\n\nconst MIRROR_PROPERTIES = [\n  'direction',\n  'boxSizing',\n  'width',\n  'height',\n  'overflowX',\n  'overflowY',\n  'borderTopWidth',\n  'borderRightWidth',\n  'borderBottomWidth',\n  'borderLeftWidth',\n  'paddingTop',\n  'paddingRight',\n  'paddingBottom',\n  'paddingLeft',\n  'fontStyle',\n  'fontVariant',\n  'fontWeight',\n  'fontStretch',\n  'fontSize',\n  'fontSizeAdjust',\n  'lineHeight',\n  'fontFamily',\n  'textAlign',\n  'textTransform',\n  'textIndent',\n  'textDecoration',\n  'letterSpacing',\n  'wordSpacing',\n  'tabSize',\n] as const;\n\nexport function getCaretCoordinates(\n  element: HTMLTextAreaElement | HTMLInputElement,\n  position: number\n): CaretCoordinates {\n  if (typeof window === 'undefined' || typeof document === 'undefined') {\n    return { top: 0, left: 0, height: 0 };\n  }\n\n  const isInput = element.nodeName === 'INPUT';\n  const computed = window.getComputedStyle(element);\n\n  const mirror = document.createElement('div');\n  const style = mirror.style;\n\n  if (isInput) {\n    style.whiteSpace = 'nowrap';\n  } else {\n    style.whiteSpace = 'pre-wrap';\n    style.wordWrap = 'break-word';\n  }\n  style.position = 'absolute';\n  style.visibility = 'hidden';\n\n  MIRROR_PROPERTIES.forEach((property) => {\n    style[property as any] = computed[property as any];\n  });\n\n  style.overflow = 'hidden';\n\n  mirror.textContent = element.value.substring(0, position);\n  if (isInput) {\n    mirror.textContent = mirror.textContent.replace(/\\s/g, '\\u00a0');\n  }\n\n  const span = document.createElement('span');\n  span.textContent = element.value.substring(position) || '.';\n  mirror.appendChild(span);\n\n  document.body.appendChild(mirror);\n\n  const coordinates: CaretCoordinates = {\n    top: span.offsetTop + (parseInt(computed.borderTopWidth, 10) || 0),\n    left: span.offsetLeft + (parseInt(computed.borderLeftWidth, 10) || 0),\n    height: parseInt(computed.lineHeight, 10) || parseInt(computed.fontSize, 10) || 0,\n  };\n\n  document.body.removeChild(mirror);\n\n  return coordinates;\n}\n"],"mappings":";;AAmBA,MAAM,oBAAoB;CACxB;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;AACF;AAEA,SAAgB,oBACd,SACA,UACkB;CAClB,IAAI,OAAO,WAAW,eAAe,OAAO,aAAa,aACvD,OAAO;EAAE,KAAK;EAAG,MAAM;EAAG,QAAQ;CAAE;CAGtC,MAAM,UAAU,QAAQ,aAAa;CACrC,MAAM,WAAW,OAAO,iBAAiB,OAAO;CAEhD,MAAM,SAAS,SAAS,cAAc,KAAK;CAC3C,MAAM,QAAQ,OAAO;CAErB,IAAI,SACF,MAAM,aAAa;MACd;EACL,MAAM,aAAa;EACnB,MAAM,WAAW;CACnB;CACA,MAAM,WAAW;CACjB,MAAM,aAAa;CAEnB,kBAAkB,SAAS,aAAa;EACtC,MAAM,YAAmB,SAAS;CACpC,CAAC;CAED,MAAM,WAAW;CAEjB,OAAO,cAAc,QAAQ,MAAM,UAAU,GAAG,QAAQ;CACxD,IAAI,SACF,OAAO,cAAc,OAAO,YAAY,QAAQ,OAAO,MAAQ;CAGjE,MAAM,OAAO,SAAS,cAAc,MAAM;CAC1C,KAAK,cAAc,QAAQ,MAAM,UAAU,QAAQ,KAAK;CACxD,OAAO,YAAY,IAAI;CAEvB,SAAS,KAAK,YAAY,MAAM;CAEhC,MAAM,cAAgC;EACpC,KAAK,KAAK,aAAa,SAAS,SAAS,gBAAgB,EAAE,KAAK;EAChE,MAAM,KAAK,cAAc,SAAS,SAAS,iBAAiB,EAAE,KAAK;EACnE,QAAQ,SAAS,SAAS,YAAY,EAAE,KAAK,SAAS,SAAS,UAAU,EAAE,KAAK;CAClF;CAEA,SAAS,KAAK,YAAY,MAAM;CAEhC,OAAO;AACT"}