@yuntijs/ui
Version:
☁️ Yunti UI - an open-source UI component library for building Cloud Native web apps
91 lines (89 loc) • 3.3 kB
JavaScript
import _toConsumableArray from "@babel/runtime/helpers/esm/toConsumableArray";
import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray";
import { $createParagraphNode, $getRoot, $isTextNode, $selectAll, createCommand } from 'lexical';
import { $convertToMentionNodes } from "./plugins/mention-converter";
export var INSERT_MENTION_COMMAND = createCommand('INSERT_MENTION_COMMAND');
export var DELETE_MENTION_COMMAND = createCommand('DELETE_MENTION_COMMAND');
export var CLEAR_HIDE_MENU_TIMEOUT = createCommand('CLEAR_HIDE_MENU_TIMEOUT');
export var UPDATE_MENTIONS_OPTIONS = createCommand('UPDATE_MENTIONS_OPTIONS');
export var decoratorTransform = function decoratorTransform(node, getMatch, createNode) {
if (!node.isSimpleText()) {
return;
}
var prevSibling = node.getPreviousSibling();
var text = node.getTextContent();
var currentNode = node;
var match;
// eslint-disable-next-line no-constant-condition
while (true) {
match = getMatch(text);
var nextText = match === null ? '' : text.slice(match.end);
text = nextText;
if (nextText === '') {
var nextSibling = currentNode.getNextSibling();
if ($isTextNode(nextSibling)) {
nextText = currentNode.getTextContent() + nextSibling.getTextContent();
var nextMatch = getMatch(nextText);
if (nextMatch === null) {
nextSibling.markDirty();
return;
} else if (nextMatch.start !== 0) {
return;
}
}
} else {
var _nextMatch = getMatch(nextText);
if (_nextMatch !== null && _nextMatch.start === 0) {
return;
}
}
if (match === null) {
return;
}
if (match.start === 0 && $isTextNode(prevSibling) && prevSibling.isTextEntity()) {
continue;
}
var nodeToReplace = void 0;
if (match.start === 0) {
var _currentNode$splitTex = currentNode.splitText(match.end);
var _currentNode$splitTex2 = _slicedToArray(_currentNode$splitTex, 2);
nodeToReplace = _currentNode$splitTex2[0];
currentNode = _currentNode$splitTex2[1];
} else {
var _currentNode$splitTex3 = currentNode.splitText(match.start, match.end);
var _currentNode$splitTex4 = _slicedToArray(_currentNode$splitTex3, 3);
nodeToReplace = _currentNode$splitTex4[1];
currentNode = _currentNode$splitTex4[2];
}
var replacementNode = createNode(nodeToReplace);
nodeToReplace.replace(replacementNode);
// eslint-disable-next-line eqeqeq
if (currentNode == null) {
return;
}
}
};
export function textToEditorState(initialValue, triggers, options) {
return function () {
var _ref = options || {
cursor: 'end'
},
punctuation = _ref.punctuation,
cursor = _ref.cursor;
var root = $getRoot();
root.clear();
var paragraph = $createParagraphNode();
paragraph.append.apply(paragraph, _toConsumableArray($convertToMentionNodes(initialValue, triggers, punctuation)));
root.append(paragraph);
if (!cursor || cursor === 'end') {
// 👇 把光标移动到文本末尾
var lastNode = paragraph.getLastDescendant();
if ($isTextNode(lastNode)) {
lastNode.select(); // 选中并聚焦尾部
}
}
if (cursor === 'all') {
$selectAll();
}
};
}