{"version":3,"file":"helper.mjs","names":[],"sources":["../../../../../../packages/components/mention/src/helper.ts"],"sourcesContent":["import { ensureArray, isFirefox } from '@element-plus/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 (\n      splitIndex === -1 &&\n      (char === split || char === '\\n' || char === '\\r')\n    ) {\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"],"mappings":";;;;AAIA,MAAa,gBACX,SACA,WACY;CACZ,MAAM,YAAY,QAAQ,aAAa;AAEvC,SADc,OAAO,SAAS,OAAO,SAAS,IACjC,aAAa,CAAC,SAAS,UAAU;;AAGhD,MAAa,iBACX,SACA,QACA,UACG;CACH,MAAM,EAAE,iBAAiB;AACzB,KAAI,iBAAiB,KAAM;CAC3B,MAAM,aAAa,QAAQ;CAC3B,MAAM,cAAc,YAAY,OAAO;CACvC,IAAI,aAAa;CACjB,IAAI;AACJ,MAAK,IAAI,IAAI,eAAe,GAAG,KAAK,GAAG,EAAE,GAAG;EAC1C,MAAM,OAAO,WAAW;AACxB,MACE,eAAe,OACd,SAAS,SAAS,SAAS,QAAQ,SAAS,OAC7C;AACA,gBAAa;AACb;;AAEF,MAAI,YAAY,SAAS,KAAK,EAAE;GAC9B,MAAM,MAAM,eAAe,KAAK,eAAe;AAE/C,gBAAa;IACX,SAFc,WAAW,MAAM,IAAI,GAAG,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