UNPKG

@lobehub/editor

Version:

A powerful and extensible rich text editor built on Meta's Lexical framework, providing a modern editing experience with React integration.

119 lines 4.12 kB
import { mergeRegister } from '@lexical/utils'; import { $getNodeByKey, $isElementNode, COMMAND_PRIORITY_EDITOR, createCommand } from 'lexical'; import { DiffNode } from "../node/DiffNode"; export var DiffAction = /*#__PURE__*/function (DiffAction) { DiffAction[DiffAction["Reject"] = 0] = "Reject"; DiffAction[DiffAction["Accept"] = 1] = "Accept"; return DiffAction; }({}); export var LITEXML_DIFFNODE_COMMAND = createCommand('LITEXML_DIFFNODE_COMMAND'); export var LITEXML_DIFFNODE_ALL_COMMAND = createCommand('LITEXML_DIFFNODE_ALL_COMMAND'); function doAction(node, action) { if (node.diffType === 'modify') { var children = node.getChildren(); if (action === DiffAction.Accept) { node.replace(children[1], false).selectEnd(); } else if (action === DiffAction.Reject) { node.replace(children[0], false).selectEnd(); } } if (node.diffType === 'remove') { if (action === DiffAction.Accept) { node.remove(); } else if (action === DiffAction.Reject) { var _children = node.getChildren(); node.replace(_children[0], false).selectEnd(); } } if (node.diffType === 'add') { if (action === DiffAction.Accept) { var _children2 = node.getChildren(); node.replace(_children2[0], false).selectEnd(); } else if (action === DiffAction.Reject) { node.remove(); } } if (node.diffType === 'listItemModify') { var _children3 = node.getChildren(); if (action === DiffAction.Accept) { var lastChild = _children3[1]; if (!$isElementNode(lastChild)) { throw new Error('Expected element node as child of DiffNode'); } var nodeChildrens = lastChild.getChildren(); for (var i = nodeChildrens.length - 1; i >= 0; i--) { node.insertAfter(nodeChildrens[i]); } var parent = node.getParentOrThrow(); node.remove(); parent.selectEnd(); } else if (action === DiffAction.Reject) { var firstChild = _children3[0]; if (!$isElementNode(firstChild)) { throw new Error('Expected element node as child of DiffNode'); } var _nodeChildrens = firstChild.getChildren(); for (var _i = _nodeChildrens.length - 1; _i >= 0; _i--) { node.insertAfter(_nodeChildrens[_i]); } var _parent = node.getParentOrThrow(); node.remove(); _parent.selectEnd(); } } if (node.diffType === 'listItemRemove') { if (action === DiffAction.Accept) { node.getParentOrThrow().remove(); } else if (action === DiffAction.Reject) { node.getChildren().forEach(function (child) { node.getParentOrThrow().append(child); }); node.getParentOrThrow().selectEnd(); node.remove(); } } if (node.diffType === 'listItemAdd') { if (action === DiffAction.Accept) { var _children4 = node.getChildren(); _children4.forEach(function (child) { node.getParentOrThrow().append(child); }); node.getParentOrThrow().selectEnd(); node.remove(); } else if (action === DiffAction.Reject) { node.remove(); } } } export function registerLiteXMLDiffCommand(editor) { return mergeRegister(editor.registerCommand(LITEXML_DIFFNODE_COMMAND, function (payload) { var action = payload.action, nodeKey = payload.nodeKey; var node = editor.getEditorState().read(function () { return $getNodeByKey(nodeKey); }); if (!node) { return false; } editor.update(function () { doAction(node, action); }); return false; }, COMMAND_PRIORITY_EDITOR), editor.registerCommand(LITEXML_DIFFNODE_ALL_COMMAND, function (payload) { var action = payload.action; var nodes = editor.getEditorState().read(function () { return Array.from(editor._editorState._nodeMap.values()).filter(function (n) { return n instanceof DiffNode && !!n.getParent(); }); }); if (!nodes.length) { return false; } editor.update(function () { nodes.forEach(function (node) { doAction(node, action); }); }); return false; }, COMMAND_PRIORITY_EDITOR)); }