@lobehub/editor
Version:
A powerful and extensible rich text editor built on Meta's Lexical framework, providing a modern editing experience with React integration.
92 lines (90 loc) • 5.03 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 { mergeRegister } from '@lexical/utils';
import { $getNodeByKey, $getSelection, $isBlockElementNode, $isRangeSelection } from 'lexical';
import { memo, useRef, useState } from 'react';
import { $closestNodeType } from "../../../../editor-kernel";
import { useLexicalEditor } from "../../../../editor-kernel/react";
import { $canShowPlaceholderCurry } from "../../utils";
import { styles } from "./style";
import { jsx as _jsx } from "react/jsx-runtime";
function canShowPlaceholderFromCurrentEditorState(editor) {
var currentCanShowPlaceholder = editor.getEditorState().read($canShowPlaceholderCurry(editor.isComposing()));
return currentCanShowPlaceholder;
}
// 判断 DOM 是否只有一个 br 子元素
function hasOnlyBrChild(element) {
var children = element.childNodes;
return children.length === 1 && children[0].nodeType === Node.ELEMENT_NODE && children[0].tagName.toLowerCase() === 'br';
}
// Keep memo: Complex editor state monitoring with update listeners and DOM manipulation
var Placeholder = /*#__PURE__*/memo(function (_ref) {
var children = _ref.children,
style = _ref.style,
lineEmptyPlaceholder = _ref.lineEmptyPlaceholder;
var currentPlaceHolderRef = useRef(null);
var _useState = useState(function () {
return false;
}),
_useState2 = _slicedToArray(_useState, 2),
canShowPlaceholder = _useState2[0],
setCanShowPlaceholder = _useState2[1];
useLexicalEditor(function (editor) {
setCanShowPlaceholder(function () {
return canShowPlaceholderFromCurrentEditorState(editor);
});
function resetCanShowPlaceholder() {
var currentCanShowPlaceholder = canShowPlaceholderFromCurrentEditorState(editor);
setCanShowPlaceholder(currentCanShowPlaceholder);
return currentCanShowPlaceholder;
}
resetCanShowPlaceholder();
return mergeRegister(editor.registerUpdateListener(function () {
var show = resetCanShowPlaceholder();
if (!show && lineEmptyPlaceholder) {
editor.getEditorState().read(function () {
var sel = $getSelection();
if ($isRangeSelection(sel) && sel.isCollapsed()) {
var anchor = sel.anchor;
var node = $getNodeByKey(anchor.key);
while (node && !$isBlockElementNode(node)) {
node = node.getParent();
}
var tableNode = $closestNodeType(node, ['tablecell', 'heading']);
if (node && !tableNode) {
var dom = editor.getElementByKey(node.getKey());
if (dom && hasOnlyBrChild(dom)) {
if (currentPlaceHolderRef.current && currentPlaceHolderRef.current !== dom) {
currentPlaceHolderRef.current.dataset.placeholder = '';
}
currentPlaceHolderRef.current = dom;
dom.dataset.placeholder = lineEmptyPlaceholder;
return;
}
}
if (currentPlaceHolderRef.current) {
currentPlaceHolderRef.current.dataset.placeholder = '';
currentPlaceHolderRef.current = null;
}
}
});
}
}), editor.registerEditableListener(function () {
resetCanShowPlaceholder();
}));
}, []);
if (!canShowPlaceholder) {
return null;
}
return /*#__PURE__*/_jsx("div", {
className: styles.placeholder,
style: style,
children: children
});
});
Placeholder.displayName = 'Placeholder';
export default Placeholder;