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.

198 lines (189 loc) 7.97 kB
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 _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); } function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread 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 _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); } function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); } 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 { $isQuoteNode } from '@lexical/rich-text'; import { $isTableCellNode, $isTableNode } from '@lexical/table'; import { $getRoot, $isDecoratorNode, $isElementNode, $isParagraphNode, $isRangeSelection, $isTextNode } from 'lexical'; import { INodeHelper } from "../../../editor-kernel/inode/helper"; export var sampleReader = function sampleReader(format, xmlElement, children) { children.forEach(function (child) { if (INodeHelper.isTextNode(child)) { child.format = (child.format || 0) | format; } }); return children; }; export var createBlockNode = function createBlockNode(createNode) { return function (parentNode, children, match, isImport) { var node = createNode(match, parentNode); node.append.apply(node, _toConsumableArray(children)); parentNode.replace(node); if (!isImport) { node.select(0, 0); } }; }; /** * Returns the root's text content. * @returns The root's text content. */ export function $rootTextContent() { var root = $getRoot(); return root.getTextContent(); } /** * Determines if the root has any text content and can trim any whitespace if it does. * @param isEditorComposing - Is the editor in composition mode due to an active Input Method Editor? * @param trim - Should the root text have its whitespaced trimmed? Defaults to true. * @returns true if text content is empty, false if there is text or isEditorComposing is true. */ export function $isRootTextContentEmpty(isEditorComposing) { var trim = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : true; if (isEditorComposing) { return false; } var text = $rootTextContent(); if (trim) { text = text.trim(); } return text === ''; } /** * Determines if the input should show the placeholder. If anything is in * in the root the placeholder should not be shown. * @param isComposing - Is the editor in composition mode due to an active Input Method Editor? * @returns true if the input should show the placeholder, false otherwise. */ export function $canShowPlaceholder(isComposing) { if (!$isRootTextContentEmpty(isComposing, false)) { return false; } var root = $getRoot(); var children = root.getChildren(); var childrenLength = children.length; if (childrenLength > 1) { return false; } for (var i = 0; i < childrenLength; i++) { var topBlock = children[i]; if ($isDecoratorNode(topBlock)) { return false; } if ($isElementNode(topBlock)) { if (!$isParagraphNode(topBlock)) { return false; } if (topBlock.__indent !== 0) { return false; } var topBlockChildren = topBlock.getChildren(); var topBlockChildrenLength = topBlockChildren.length; for (var s = 0; s < topBlockChildrenLength; s++) { var child = topBlockChildren[i]; if (!$isTextNode(child)) { return false; } } } } return true; } /** * Returns a function that executes {@link $canShowPlaceholder} * @param isEditorComposing - Is the editor in composition mode due to an active Input Method Editor? * @returns A function that executes $canShowPlaceholder with arguments. */ export function $canShowPlaceholderCurry(isEditorComposing) { return function () { return $canShowPlaceholder(isEditorComposing); }; } // Utility function to check if cursor is in a table export function $isCursorInTable(selection) { if (!$isRangeSelection(selection)) { return { inCell: false, inTable: false }; } var focusNode = selection.focus.getNode(); var currentNode = focusNode; var inTable = false; var inCell = false; // Traverse up the parent chain to find table context while (currentNode) { if ($isTableCellNode(currentNode)) { inCell = true; inTable = true; break; } if ($isTableNode(currentNode)) { inTable = true; break; } var parent = currentNode.getParent(); if (!parent) { break; } currentNode = parent; } return { inCell: inCell, inTable: inTable }; } // Utility function to check if cursor is in a quote export function $isCursorInQuote(selection) { if (!$isRangeSelection(selection)) { return false; } var focusNode = selection.focus.getNode(); var currentNode = focusNode; // Traverse up the parent chain to find quote context while (currentNode) { if ($isQuoteNode(currentNode)) { return true; } var parent = currentNode.getParent(); if (!parent) { break; } currentNode = parent; } return false; } export function exportNodeToJSON(node) { var serializedNode = node.exportJSON(); var nodeClass = node.constructor; // @ts-expect-error not error serializedNode.id = node.getKey(); 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 _iterator = _createForOfIteratorHelper(children), _step; try { for (_iterator.s(); !(_step = _iterator.n()).done;) { var child = _step.value; var serializedChildNode = exportNodeToJSON(child); serializedChildren.push(serializedChildNode); } } catch (err) { _iterator.e(err); } finally { _iterator.f(); } } // @ts-expect-error not error return serializedNode; }