UNPKG

prism-code-editor

Version:

Lightweight, extensible code editor component for the web using Prism

100 lines (99 loc) 3.7 kB
import { d as isChrome, n as numLines, e as isWebKit } from "./index-Bb4AMnd0.js"; const scrollToEl = (editor, el, paddingTop = 0) => { const style1 = editor.scrollContainer.style, style2 = document.documentElement.style; style1.scrollPaddingBlock = style2.scrollPaddingBlock = `${paddingTop}px ${isChrome && !el.textContent ? el.offsetHeight : 0}px`; el.scrollIntoView({ block: "nearest" }); style1.scrollPaddingBlock = style2.scrollPaddingBlock = ""; }; const getLineStart = (text, position) => position ? text.lastIndexOf("\n", position - 1) + 1 : 0; const getLineEnd = (text, position) => (position = text.indexOf("\n", position)) + 1 ? position : text.length; const addListener = (editor, type, listener) => { editor.addListener(type, listener); return () => editor.removeListener(type, listener); }; let prevSelection; const regexEscape = (str) => str.replace(/[$+?|.^*()[\]{}\\]/g, "\\$&"); const getLineBefore = (text, position) => text.slice(getLineStart(text, position), position); const getLines = (text, start, end = start) => [ text.slice(start = getLineStart(text, start), end = getLineEnd(text, end)).split("\n"), start, end ]; const getClosestToken = (editor, selector, marginLeft = 0, marginRight = marginLeft, position = editor.getSelection()[0]) => { const value = editor.value; const line = editor.wrapper.children[numLines(value, 0, position)]; const walker = document.createTreeWalker(line, 5); let node = walker.lastChild(); let offset = getLineEnd(value, position) + 1 - position - node.length; while (-offset <= marginRight && (node = walker.previousNode())) { if (node.lastChild) continue; offset -= node.length || 0; if (offset <= marginLeft) { for (; node != line; node = node.parentNode) { if (node.matches?.(selector)) return node; } } } }; const getLanguage = (editor, position) => getClosestToken(editor, '[class*="language-"]', 0, 0, position)?.className.match( /language-(\S*)/ )[1] || editor.options.language; const insertText = (editor, text, start, end, newCursorStart, newCursorEnd) => { if (editor.options.readOnly) return; prevSelection = editor.getSelection(); end ?? (end = start); let textarea = editor.textarea; let value = editor.value; let avoidBug = isChrome && !value[end ?? prevSelection[1]] && /\n$/.test(text) && /^$|\n$/.test(value); let removeListener; editor.focused || textarea.focus(); if (start != null) textarea.setSelectionRange(start, end); if (newCursorStart != null) { removeListener = addListener(editor, "update", () => { textarea.setSelectionRange( newCursorStart, newCursorEnd ?? newCursorStart, prevSelection[2] ); removeListener(); }); } isWebKit || textarea.dispatchEvent(new InputEvent("beforeinput", { data: text })); if (isChrome || isWebKit) { if (avoidBug) { textarea.selectionEnd--; text = text.slice(0, -1); } if (isWebKit) text += "\n"; document.execCommand( text ? "insertHTML" : "delete", false, text.replace(/&/g, "&amp;").replace(/</g, "&lt;") ); if (avoidBug) textarea.selectionStart++; } else document.execCommand(text ? "insertText" : "delete", false, text); prevSelection = 0; }; const getModifierCode = (e) => e.altKey + e.ctrlKey * 2 + e.metaKey * 4 + e.shiftKey * 8; export { getModifierCode as a, getLines as b, getLineBefore as c, getLineStart as d, getLineEnd as e, getClosestToken as f, getLanguage as g, addListener as h, insertText as i, prevSelection as p, regexEscape as r, scrollToEl as s }; //# sourceMappingURL=index-2teoWRgh.js.map