@lobehub/editor
Version:
A powerful and extensible rich text editor built on Meta's Lexical framework, providing a modern editing experience with React integration.
88 lines (86 loc) • 4.79 kB
JavaScript
function _createForOfIteratorHelper(o, allowArrayLike) { var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"]; if (!it) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e) { throw _e; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var normalCompletion = true, didErr = false, err; return { s: function s() { it = it.call(o); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e2) { didErr = true; err = _e2; }, f: function f() { try { if (!normalCompletion && it.return != null) it.return(); } finally { if (didErr) throw err; } } }; }
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; }
import { $isElementNode } from 'lexical';
export function $parseSerializedNodeImpl(serializedNode, editor) {
var keepId = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false;
var state = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : null;
var type = serializedNode.type;
var registeredNode = editor._nodes.get(type);
if (registeredNode === undefined) {
throw new Error("parseEditorState: type \"".concat(type, "\" not found"));
}
var nodeClass = registeredNode.klass;
if (serializedNode.type !== nodeClass.getType()) {
throw new Error("LexicalNode: Node ".concat(nodeClass.name, " does not implement .importJSON()."));
}
var node = nodeClass.importJSON(serializedNode);
if (keepId && serializedNode.id) {
node.__key = serializedNode.id;
state === null || state === void 0 || state._nodeMap.set(node.__key, node);
}
var children = serializedNode.children;
if ($isElementNode(node) && Array.isArray(children)) {
var childNodes = [];
var _iterator = _createForOfIteratorHelper(children),
_step;
try {
for (_iterator.s(); !(_step = _iterator.n()).done;) {
var serializedJSONChildNode = _step.value;
var childNode = $parseSerializedNodeImpl(serializedJSONChildNode, editor, keepId, state);
childNodes.push(childNode);
}
} catch (err) {
_iterator.e(err);
} finally {
_iterator.f();
}
node.append.apply(node, childNodes);
}
return node;
}
function exportNodeToJSON(node) {
var serializedNode = node.exportJSON();
var nodeClass = node.constructor;
if (serializedNode.type !== nodeClass.getType()) {
throw new Error("LexicalNode: Node ".concat(nodeClass.name, " does not match the serialized type. Check if .exportJSON() is implemented and it is returning the correct type."));
}
if ($isElementNode(node)) {
var serializedChildren = serializedNode.children;
if (!Array.isArray(serializedChildren)) {
throw new Error("LexicalNode: Node ".concat(nodeClass.name, " is an element but .exportJSON() does not have a children array."));
}
var children = node.getChildren();
var _iterator2 = _createForOfIteratorHelper(children),
_step2;
try {
for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) {
var child = _step2.value;
var serializedChildNode = exportNodeToJSON(child);
serializedChildren.push(serializedChildNode);
}
} catch (err) {
_iterator2.e(err);
} finally {
_iterator2.f();
}
}
// @ts-expect-error
return serializedNode;
}
export function $cloneNode(node, editor) {
var json = exportNodeToJSON(node);
return $parseSerializedNodeImpl(json, editor);
}
var maxId = 1679616; // 36^4
var startId = 1000000; // to avoid short ids
var step = 7211; // a prime number to reduce collisions
var modInverse = 1394051; // modular inverse of step mod maxId
export function idToChar(id) {
var nId = (Number(id) * step + startId) % maxId;
return nId.toString(36).padStart(4, '0');
}
export function charToId(char) {
var nId = parseInt(char, 36);
return String((nId - startId + maxId) * modInverse % maxId);
}