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.

96 lines (92 loc) 4.08 kB
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 { $getRoot, $isDecoratorNode, $isElementNode, $isParagraphNode, $isTextNode } from 'lexical'; 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); }; }