prism-code-editor
Version:
Lightweight, extensible code editor component for the web using Prism
43 lines (42 loc) • 1.55 kB
JavaScript
import { c as createTemplate } from "./index-q0zRzWVD.js";
import { l as getPosition, a as addTextareaListener, u as updateNode, f as getLineBefore, k as getLineEnd, m as scrollToEl } from "./index-CxiLA9IO.js";
const cursorTemplate = /* @__PURE__ */ createTemplate(
"<div style=position:absolute;top:0;opacity:0;padding-right:inherit> <span><span></span> "
);
const cursorPosition = () => {
let cEditor;
const cursorContainer = cursorTemplate();
const [before, span] = cursorContainer.childNodes;
const [cursor, after] = span.childNodes;
const update = () => {
const selection = cEditor.getSelection();
const value = cEditor.value;
const activeLine = cEditor.lines[cEditor.activeLine];
const position = selection[selection[2] < "f" ? 0 : 1];
updateNode(before, getLineBefore(value, position));
updateNode(after, value.slice(position, getLineEnd(value, position)) + "\n");
if (cursorContainer.parentNode != activeLine) activeLine.prepend(cursorContainer);
};
const scrollIntoView = () => {
update();
scrollToEl(cEditor, cursor);
};
const self = (editor) => {
cEditor = editor;
editor.extensions.cursor = self;
addTextareaListener(editor, "input", (e) => {
if (/history/.test(e.inputType)) scrollIntoView();
});
};
self.getPosition = () => {
update();
return getPosition(cEditor, cursor);
};
self.scrollIntoView = scrollIntoView;
self.element = cursor;
return self;
};
export {
cursorPosition as c
};
//# sourceMappingURL=position-VAfpj0jh.js.map