element-plus
Version:
A Component Library for Vue 3
1 lines • 13.3 kB
Source Map (JSON)
{"version":3,"file":"helper.mjs","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 (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":";;;AAIO,MAAM,YAAA,GAAe,CAC1B,OAAA,EACA,MAAA,KACY;AACZ,EAAA,MAAM,SAAA,GAAY,QAAQ,WAAA,EAAY;AACtC,EAAA,MAAM,KAAA,GAAQ,MAAA,CAAO,KAAA,IAAS,MAAA,CAAO,KAAA,IAAS,EAAA;AAC9C,EAAA,OAAO,KAAA,CAAM,WAAA,EAAY,CAAE,QAAA,CAAS,SAAS,CAAA;AAC/C;AAEO,MAAM,aAAA,GAAgB,CAC3B,OAAA,EACA,MAAA,EACA,KAAA,KACG;AACH,EAAA,MAAM,EAAE,cAAa,GAAI,OAAA;AACzB,EAAA,IAAI,iBAAiB,IAAA,EAAM;AAC3B,EAAA,MAAM,aAAa,OAAA,CAAQ,KAAA;AAC3B,EAAA,MAAM,WAAA,GAAcA,UAAY,MAAM,CAAA;AACtC,EAAA,IAAI,UAAA,GAAa,EAAA;AACjB,EAAA,IAAI,UAAA;AACJ,EAAA,KAAA,IAAS,IAAI,YAAA,GAAe,CAAA,EAAG,CAAA,IAAK,CAAA,EAAG,EAAE,CAAA,EAAG;AAC1C,IAAA,MAAM,IAAA,GAAO,WAAW,CAAC,CAAA;AACzB,IAA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