@difizen/ai-flow
Version:
Scalable, out-of-the-box, agent-oriented flow
212 lines (210 loc) • 8.6 kB
JavaScript
function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t.return && (u = t.return(), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } }
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
import { $createTextNode, $getSelection, $isRangeSelection, $isTextNode } from 'lexical';
import { CustomTextNode } from "./plugins/custom-text/node";
export function registerLexicalTextEntity(editor, getMatch, targetNode, createNode) {
var isTargetNode = function isTargetNode(node) {
return node instanceof targetNode;
};
var replaceWithSimpleText = function replaceWithSimpleText(node) {
var textNode = $createTextNode(node.getTextContent());
textNode.setFormat(node.getFormat());
node.replace(textNode);
};
var getMode = function getMode(node) {
return node.getLatest().__mode;
};
var textNodeTransform = function textNodeTransform(node) {
if (!node.isSimpleText()) {
return;
}
var prevSibling = node.getPreviousSibling();
var text = node.getTextContent();
var currentNode = node;
var match;
if ($isTextNode(prevSibling)) {
var previousText = prevSibling.getTextContent();
var combinedText = previousText + text;
var prevMatch = getMatch(combinedText);
if (isTargetNode(prevSibling)) {
if (prevMatch === null || getMode(prevSibling) !== 0) {
replaceWithSimpleText(prevSibling);
return;
} else {
var diff = prevMatch.end - previousText.length;
if (diff > 0) {
var concatText = text.slice(0, diff);
var newTextContent = previousText + concatText;
prevSibling.select();
prevSibling.setTextContent(newTextContent);
if (diff === text.length) {
node.remove();
} else {
var remainingText = text.slice(diff);
node.setTextContent(remainingText);
}
return;
}
}
} else if (prevMatch === null || prevMatch.start < previousText.length) {
return;
}
}
// eslint-disable-next-line no-constant-condition
while (true) {
match = getMatch(text);
var nextText = match === null ? '' : text.slice(match.end);
text = nextText;
if (nextText === '' && currentNode) {
var nextSibling = currentNode.getNextSibling();
if ($isTextNode(nextSibling)) {
nextText = currentNode.getTextContent() + nextSibling.getTextContent();
var nextMatch = getMatch(nextText);
if (nextMatch === null) {
if (isTargetNode(nextSibling)) {
replaceWithSimpleText(nextSibling);
} else {
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);
replacementNode.setFormat(nodeToReplace.getFormat());
nodeToReplace.replace(replacementNode);
if (currentNode === null) {
return;
}
}
};
var reverseNodeTransform = function reverseNodeTransform(node) {
var text = node.getTextContent();
var match = getMatch(text);
if (match === null || match.start !== 0) {
replaceWithSimpleText(node);
return;
}
if (text.length > match.end) {
// This will split out the rest of the text as simple text
node.splitText(match.end);
return;
}
var prevSibling = node.getPreviousSibling();
if ($isTextNode(prevSibling) && prevSibling.isTextEntity()) {
replaceWithSimpleText(prevSibling);
replaceWithSimpleText(node);
}
var nextSibling = node.getNextSibling();
if ($isTextNode(nextSibling) && nextSibling.isTextEntity()) {
replaceWithSimpleText(nextSibling); // This may have already been converted in the previous block
if (isTargetNode(node)) {
replaceWithSimpleText(node);
}
}
};
var removePlainTextTransform = editor.registerNodeTransform(CustomTextNode, textNodeTransform);
var removeReverseNodeTransform = editor.registerNodeTransform(targetNode, reverseNodeTransform);
return [removePlainTextTransform, removeReverseNodeTransform];
}
function getFullMatchOffset(documentText, entryText, offset) {
var triggerOffset = offset;
for (var i = triggerOffset; i <= entryText.length; i++) {
if (documentText.substr(-i) === entryText.substr(0, i)) {
triggerOffset = i;
}
}
return triggerOffset;
}
export function $splitNodeContainingQuery(match) {
var selection = $getSelection();
if (!$isRangeSelection(selection) || !selection.isCollapsed()) {
return null;
}
var anchor = selection.anchor;
if (anchor.type !== 'text') {
return null;
}
var anchorNode = anchor.getNode();
if (!anchorNode.isSimpleText()) {
return null;
}
var selectionOffset = anchor.offset;
var textContent = anchorNode.getTextContent().slice(0, selectionOffset);
var characterOffset = match.replaceableString.length;
var queryOffset = getFullMatchOffset(textContent, match.matchingString, characterOffset);
var startOffset = selectionOffset - queryOffset;
if (startOffset < 0) {
return null;
}
var newNode;
if (startOffset === 0) {
var _anchorNode$splitText = anchorNode.splitText(selectionOffset);
var _anchorNode$splitText2 = _slicedToArray(_anchorNode$splitText, 1);
newNode = _anchorNode$splitText2[0];
} else {
var _anchorNode$splitText3 = anchorNode.splitText(startOffset, selectionOffset);
var _anchorNode$splitText4 = _slicedToArray(_anchorNode$splitText3, 2);
newNode = _anchorNode$splitText4[1];
}
return newNode;
}
export function textToEditorState(text) {
var paragraph = text.split('\n');
return JSON.stringify({
root: {
children: paragraph.map(function (p) {
return {
children: [{
detail: 0,
format: 0,
mode: 'normal',
style: '',
text: p,
type: 'custom-text',
version: 1
}],
direction: 'ltr',
format: '',
indent: 0,
type: 'paragraph',
version: 1
};
}),
direction: 'ltr',
format: '',
indent: 0,
type: 'root',
version: 1
}
});
}