vue-admin-core
Version:
A Component Library for Vue 3
82 lines (79 loc) • 2.5 kB
JavaScript
import { DomEditor } from '@wangeditor/editor';
import { isString } from 'lodash-es';
import emitter from './mitt.mjs';
function getNumber(str) {
const matches = str.match(/\d+(\.\d+)?/);
const number = matches ? parseFloat(matches[0]) : 0;
return number;
}
function getLeftAndTop(editor, node) {
const { width, height } = editor.getEditableContainer().getBoundingClientRect();
const pos = node ? editor.getNodePosition(node) : editor.getSelectionPosition();
const lineHeight = window.getComputedStyle(editor.getEditableContainer()).lineHeight;
let __left = 0, __top = 0;
if (isString(pos.left))
__left = getNumber(pos.left);
if (isString(pos.top))
__top = getNumber(pos.top);
if (isString(pos.bottom))
__top = height - getNumber(pos.bottom) + getNumber(lineHeight);
if (isString(pos.right))
__left = width - getNumber(pos.right);
return {
left: __left,
top: __top,
isRight: !!pos.right
};
}
function withVariable(editor) {
const { insertText, isInline, isVoid } = editor;
const newEditor = editor;
newEditor.insertText = (t) => {
const elems = DomEditor.getSelectedElems(newEditor);
const isSelectedVoidElem = elems.some((elem) => newEditor.isVoid(elem));
if (isSelectedVoidElem) {
insertText(t);
return;
}
if (t === "$" || t === "\uFFE5") {
setTimeout(() => {
emitter.emit("show", getLeftAndTop(newEditor));
setTimeout(() => {
function _hide() {
emitter.emit("hide", newEditor);
}
newEditor.once("fullScreen", _hide);
newEditor.once("unFullScreen", _hide);
newEditor.once("scroll", _hide);
newEditor.once("modalOrPanelShow", _hide);
newEditor.once("modalOrPanelHide", _hide);
function hideOnChange() {
if (newEditor.selection != null) {
_hide();
newEditor.off("change", hideOnChange);
}
}
newEditor.on("change", hideOnChange);
});
});
}
insertText(t);
};
newEditor.isInline = (elem) => {
const type = DomEditor.getNodeType(elem);
if (type === "variable") {
return true;
}
return isInline(elem);
};
newEditor.isVoid = (elem) => {
const type = DomEditor.getNodeType(elem);
if (type === "variable") {
return true;
}
return isVoid(elem);
};
return newEditor;
}
export { withVariable as default, getLeftAndTop };
//# sourceMappingURL=plugin.mjs.map