UNPKG

element-plus

Version:

A Component Library for Vue 3

1 lines 13.6 kB
{"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":";;;AAIa,MAAA,YAAA,GAAe,CAC1B,OAAA,EACA,MACY,KAAA;AACZ,EAAM,MAAA,SAAA,GAAY,QAAQ,WAAY,EAAA,CAAA;AACtC,EAAA,MAAM,KAAQ,GAAA,MAAA,CAAO,KAAS,IAAA,MAAA,CAAO,KAAS,IAAA,EAAA,CAAA;AAC9C,EAAA,OAAO,KAAM,CAAA,WAAA,EAAc,CAAA,QAAA,CAAS,SAAS,CAAA,CAAA;AAC/C,EAAA;AAEO,MAAM,aAAgB,GAAA,CAC3B,OACA,EAAA,MAAA,EACA,KACG,KAAA;AACH,EAAM,MAAA,EAAE,cAAiB,GAAA,OAAA,CAAA;AACzB,EAAA,IAAI,YAAiB,KAAA,IAAA;AAAM,IAAA,OAAA;AAC3B,EAAA,MAAM,aAAa,OAAQ,CAAA,KAAA,CAAA;AAC3B,EAAM,MAAA,WAAA,GAAcA,UAAY,MAAM,CAAA,CAAA;AACtC,EAAA,IAAI,UAAa,GAAA,CAAA,CAAA,CAAA;AACjB,EAAI,IAAA,UAAA,CAAA;AACJ,EAAA,KAAA,IAAS,IAAI,YAAe,GAAA,CAAA,EAAG,CAAK,IAAA,CAAA,EAAG,EAAE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