UNPKG

prism-code-editor

Version:

Lightweight, extensible code editor component for the web using Prism

48 lines (47 loc) 1.9 kB
import { a as createTemplate, b as addTextareaListener } from "../index-Bb4AMnd0.js"; import { s as scrollToEl, c as getLineBefore, e as getLineEnd } from "../index-2teoWRgh.js"; const cursorTemplate = createTemplate( "<div style=position:absolute;top:0;opacity:0;padding:inherit> <span></span> " ); const cursorPosition = () => { let cEditor, prevBefore = " ", prevAfter = " "; const cursorContainer = cursorTemplate(), [before, cursor, after] = cursorContainer.childNodes, selectionChange = ([start, end, direction]) => { let { value, activeLine } = cEditor, position = direction == "backward" ? start : end, newBefore = getLineBefore(value, position), newAfter = value.slice(position, getLineEnd(value, position)); if (!newBefore && !newAfter) newAfter = " "; if (prevBefore != newBefore) before.data = prevBefore = newBefore; if (prevAfter != newAfter) after.data = prevAfter = newAfter; if (cursorContainer.parentNode != activeLine) activeLine.prepend(cursorContainer); }, scrollIntoView = () => scrollToEl(cEditor, cursor); const self = (editor) => { editor.addListener("selectionChange", selectionChange); cEditor = editor; editor.extensions.cursor = self; addTextareaListener(editor, "input", (e) => { if (/history/.test(e.inputType)) scrollIntoView(); }); if (editor.activeLine) selectionChange(editor.getSelection()); }; self.getPosition = () => { const rect1 = cursor.getBoundingClientRect(), rect2 = cEditor.overlays.getBoundingClientRect(); return { top: rect1.y - rect2.y, bottom: rect2.bottom - rect1.bottom, left: rect1.x - rect2.x, right: rect2.right - rect1.x, height: rect1.height }; }; self.scrollIntoView = scrollIntoView; self.element = cursor; return self; }; export { cursorPosition }; //# sourceMappingURL=cursor.js.map