{"version":3,"file":"helper.mjs","sources":["../../../../../../packages/components/mention/src/helper.ts"],"sourcesContent":["import { ensureArray, isFirefox } from '@lit-element/utils'\n\nimport type { MentionCtx, MentionOption } from './types'\n\nexport const filterOption = (\n  pattern: string,\n  option: MentionOption\n): boolean => {\n  const lowerCase = pattern.toLowerCase()\n  const label = option.label || option.value\n  return label.toLowerCase().includes(lowerCase)\n}\n\nexport const getMentionCtx = (\n  inputEl: HTMLInputElement | HTMLTextAreaElement,\n  prefix: string | string[],\n  split: string\n) => {\n  const { selectionEnd } = inputEl\n  if (selectionEnd === null) return\n  const inputValue = inputEl.value\n  const prefixArray = ensureArray(prefix)\n  let splitIndex = -1\n  let mentionCtx: MentionCtx | undefined\n  for (let i = selectionEnd - 1; i >= 0; --i) {\n    const char = inputValue[i]\n    if (char === split || char === '\\n' || char === '\\r') {\n      splitIndex = i\n      continue\n    }\n    if (prefixArray.includes(char)) {\n      const end = splitIndex === -1 ? selectionEnd : splitIndex\n      const pattern = inputValue.slice(i + 1, end)\n      mentionCtx = {\n        pattern,\n        start: i + 1,\n        end,\n        prefix: char,\n        prefixIndex: i,\n        splitIndex,\n        selectionEnd,\n      }\n      break\n    }\n  }\n  return mentionCtx\n}\n\n/**\n * fork from textarea-caret-position\n * https://github.com/component/textarea-caret-position\n * The MIT License (MIT)\n * Copyright (c) 2015 Jonathan Ong me@jongleberry.com\n * Permission is hereby granted, free of charge, to any person obtaining a copy\n * of this software and associated documentation files (the \"Software\"), to deal\n * in the Software without restriction, including without limitation the rights\n * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\n * copies of the Software, and to permit persons to whom the Software is\n * furnished to do so, subject to the following conditions:\n * The above copyright notice and this permission notice shall be included in all\n * copies or substantial portions of the Software.\n * THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\n * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\n * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\n * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\n * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\n * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\n * SOFTWARE.\n */\nexport const getCursorPosition = (\n  element: HTMLInputElement | HTMLTextAreaElement,\n  options = {\n    debug: false,\n    useSelectionEnd: false,\n  }\n) => {\n  const selectionStart =\n    element.selectionStart !== null ? element.selectionStart : 0\n  const selectionEnd = element.selectionEnd !== null ? element.selectionEnd : 0\n  const position = options.useSelectionEnd ? selectionEnd : selectionStart\n  // We'll copy the properties below into the mirror div.\n  // Note that some browsers, such as Firefox, do not concatenate properties\n  // into their shorthand (e.g. padding-top, padding-bottom etc. -> padding),\n  // so we have to list every single property explicitly.\n  const properties: string[] = [\n    'direction', // RTL support\n    'boxSizing',\n    'width', // on Chrome and IE, exclude the scrollbar, so the mirror div wraps exactly as the textarea does\n    'height',\n    'overflowX',\n    'overflowY', // copy the scrollbar for IE\n    'borderTopWidth',\n    'borderRightWidth',\n    'borderBottomWidth',\n    'borderLeftWidth',\n    'borderStyle',\n    'paddingTop',\n    'paddingRight',\n    'paddingBottom',\n    'paddingLeft',\n    // https://developer.mozilla.org/en-US/docs/Web/CSS/font\n    'fontStyle',\n    'fontVariant',\n    'fontWeight',\n    'fontStretch',\n    'fontSize',\n    'fontSizeAdjust',\n    'lineHeight',\n    'fontFamily',\n    'textAlign',\n    'textTransform',\n    'textIndent',\n    'textDecoration', // might not make a difference, but better be safe\n    'letterSpacing',\n    'wordSpacing',\n    'tabSize',\n    'MozTabSize',\n  ]\n\n  if (options.debug) {\n    const el = document.querySelector(\n      '#input-textarea-caret-position-mirror-div'\n    )\n    if (el?.parentNode) el.parentNode.removeChild(el)\n  }\n\n  // The mirror div will replicate the textareas style\n  const div = document.createElement('div')\n  div.id = 'input-textarea-caret-position-mirror-div'\n  document.body.appendChild(div)\n\n  const style = div.style\n  const computed = window.getComputedStyle(element)\n\n  const isInput = element.nodeName === 'INPUT'\n\n  // Default textarea styles\n  style.whiteSpace = isInput ? 'nowrap' : 'pre-wrap'\n  if (!isInput) style.wordWrap = 'break-word' // only for textarea-s\n\n  // Position off-screen\n  style.position = 'absolute' // required to return coordinates properly\n  if (!options.debug) style.visibility = 'hidden' // not 'display: none' because we want rendering\n\n  // Transfer the element's properties to the div\n  properties.forEach((prop) => {\n    if (isInput && prop === 'lineHeight') {\n      // Special case for <input>s because text is rendered centered and line height may be != height\n      if (computed.boxSizing === 'border-box') {\n        const height = Number.parseInt(computed.height as string)\n        const outerHeight =\n          Number.parseInt(computed.paddingTop as string) +\n          Number.parseInt(computed.paddingBottom as string) +\n          Number.parseInt(computed.borderTopWidth as string) +\n          Number.parseInt(computed.borderBottomWidth as string)\n        const targetHeight =\n          outerHeight + Number.parseInt(computed.lineHeight as string)\n        if (height > targetHeight) {\n          style.lineHeight = `${height - outerHeight}px`\n        } else if (height === targetHeight) {\n          style.lineHeight = computed.lineHeight\n        } else {\n          style.lineHeight = '0'\n        }\n      } else {\n        style.lineHeight = computed.height\n      }\n    } else {\n      style[prop as any] = computed[prop as any]\n    }\n  })\n\n  if (isFirefox()) {\n    // Firefox lies about the overflow property for textareas: https://bugzilla.mozilla.org/show_bug.cgi?id=984275\n    if (element.scrollHeight > Number.parseInt(computed.height as string)) {\n      style.overflowY = 'scroll'\n    }\n  } else {\n    style.overflow = 'hidden' // for Chrome to not render a scrollbar; IE keeps overflowY = 'scroll'\n  }\n\n  div.textContent = element.value.slice(0, Math.max(0, position))\n  // The second special handling for input type=\"text\" vs textarea:\n  // spaces need to be replaced with non-breaking spaces - http://stackoverflow.com/a/13402035/1269037\n  if (isInput && div.textContent) {\n    div.textContent = div.textContent.replace(/\\s/g, '\\u00A0')\n  }\n\n  const span = document.createElement('span')\n  // Wrapping must be replicated *exactly*, including when a long word gets\n  // onto the next line, with whitespace at the end of the line before (#7).\n  // The  *only* reliable way to do that is to copy the *entire* rest of the\n  // textareas content into the <span> created at the caret position.\n  // For inputs, just '.' would be enough, but no need to bother.\n  span.textContent = element.value.slice(Math.max(0, position)) || '.' // || because a completely empty faux span doesn't render at all\n  span.style.position = 'relative'\n  span.style.left = `${-element.scrollLeft}px`\n  span.style.top = `${-element.scrollTop}px`\n  div.appendChild(span)\n\n  const relativePosition = {\n    top: span.offsetTop + Number.parseInt(computed.borderTopWidth as string),\n    left: span.offsetLeft + Number.parseInt(computed.borderLeftWidth as string),\n    // We don't use line-height since it may be too large for position. Eg. 34px\n    // for input\n    height: Number.parseInt(computed.fontSize as string) * 1.5,\n  }\n\n  if (options.debug) {\n    span.style.backgroundColor = '#aaa'\n  } else {\n    document.body.removeChild(div)\n  }\n\n  if (relativePosition.left >= element.clientWidth) {\n    relativePosition.left = element.clientWidth\n  }\n  return relativePosition\n}\n"],"names":["ensureArray"],"mappings":";;;;AACY,MAAC,YAAY,GAAG,CAAC,OAAO,EAAE,MAAM,KAAK;AACjD,EAAE,MAAM,SAAS,GAAG,OAAO,CAAC,WAAW,EAAE,CAAC;AAC1C,EAAE,MAAM,KAAK,GAAG,MAAM,CAAC,KAAK,IAAI,MAAM,CAAC,KAAK,CAAC;AAC7C,EAAE,OAAO,KAAK,CAAC,WAAW,EAAE,CAAC,QAAQ,CAAC,SAAS,CAAC,CAAC;AACjD,EAAE;AACU,MAAC,aAAa,GAAG,CAAC,OAAO,EAAE,MAAM,EAAE,KAAK,KAAK;AACzD,EAAE,MAAM,EAAE,YAAY,EAAE,GAAG,OAAO,CAAC;AACnC,EAAE,IAAI,YAAY,KAAK,IAAI;AAC3B,IAAI,OAAO;AACX,EAAE,MAAM,UAAU,GAAG,OAAO,CAAC,KAAK,CAAC;AACnC,EAAE,MAAM,WAAW,GAAGA,SAAW,CAAC,MAAM,CAAC,CAAC;AAC1C,EAAE,IAAI,UAAU,GAAG,CAAC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