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.

305 lines 16.6 kB
function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); } 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 _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; } 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; } function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, _toPropertyKey(descriptor.key), descriptor); } } function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; } function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : String(i); } function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); } function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); Object.defineProperty(subClass, "prototype", { writable: false }); if (superClass) _setPrototypeOf(subClass, superClass); } function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); } function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; } function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } else if (call !== void 0) { throw new TypeError("Derived constructors may only return object or undefined"); } return _assertThisInitialized(self); } function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; } function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } } function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf.bind() : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); } import { mergeRegister } from '@lexical/utils'; import { $createTextNode, $getNodeByKey, $getSelection, $isDecoratorNode, $isRangeSelection, $setSelection, COMMAND_PRIORITY_HIGH, DecoratorNode, ElementNode, KEY_ARROW_LEFT_COMMAND, KEY_ARROW_RIGHT_COMMAND, KEY_BACKSPACE_COMMAND, TextNode } from 'lexical'; import { createDebugLogger } from "../../../utils/debug"; var logger = createDebugLogger('common', 'cursor'); export var CardLikeElementNode = /*#__PURE__*/function (_ElementNode) { _inherits(CardLikeElementNode, _ElementNode); var _super = _createSuper(CardLikeElementNode); function CardLikeElementNode() { _classCallCheck(this, CardLikeElementNode); return _super.apply(this, arguments); } _createClass(CardLikeElementNode, [{ key: "isCardLike", value: function isCardLike() { return true; } }]); return CardLikeElementNode; }(ElementNode); export var CursorNode = /*#__PURE__*/function (_TextNode) { _inherits(CursorNode, _TextNode); var _super2 = _createSuper(CursorNode); function CursorNode() { _classCallCheck(this, CursorNode); return _super2.apply(this, arguments); } _createClass(CursorNode, [{ key: "isUnmergeable", value: function isUnmergeable() { return true; } }], [{ key: "getType", value: function getType() { return 'cursor'; } }]); return CursorNode; }(TextNode); export function $createCursorNode() { return new CursorNode("\uFEFF"); } export function $isCardLikeElementNode(node) { return node instanceof CardLikeElementNode; } export function $isCursorNode(node) { return node instanceof CursorNode; } export function registerCursorNode(editor) { return mergeRegister(editor.registerUpdateListener(function (_ref) { var mutatedNodes = _ref.mutatedNodes; editor.read(function () { if (!mutatedNodes) return; var needAddCursor = []; var _iterator = _createForOfIteratorHelper(mutatedNodes), _step; try { for (_iterator.s(); !(_step = _iterator.n()).done;) { var _step$value = _slicedToArray(_step.value, 2), kClass = _step$value[0], nodeMaps = _step$value[1]; // eslint-disable-next-line no-prototype-builtins if (DecoratorNode.prototype.isPrototypeOf(kClass.prototype)) { var _iterator2 = _createForOfIteratorHelper(nodeMaps), _step2; try { for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) { var _step2$value = _slicedToArray(_step2.value, 2), key = _step2$value[0], mutation = _step2$value[1]; var node = $getNodeByKey(key); logger.debug('🎭 DecoratorNode mutated:', node === null || node === void 0 ? void 0 : node.getType(), mutation, node); if (mutation === 'created' && node !== null && node !== void 0 && node.isInline() && node.getNextSibling() === null) { needAddCursor.push(node); } } } catch (err) { _iterator2.e(err); } finally { _iterator2.f(); } } } } catch (err) { _iterator.e(err); } finally { _iterator.f(); } if (needAddCursor.length > 0) { editor.update(function () { needAddCursor.forEach(function (node) { node.insertAfter($createCursorNode()); }); }); } return false; }); }), editor.registerUpdateListener(function (_ref2) { var mutatedNodes = _ref2.mutatedNodes; editor.read(function () { var cursorNodes = mutatedNodes === null || mutatedNodes === void 0 ? void 0 : mutatedNodes.get(CursorNode); var needRemove = new Set(); if (cursorNodes) { var _iterator3 = _createForOfIteratorHelper(cursorNodes), _step3; try { for (_iterator3.s(); !(_step3 = _iterator3.n()).done;) { var _step3$value = _slicedToArray(_step3.value, 2), key = _step3$value[0], mutation = _step3$value[1]; if (mutation === 'updated') { var cursorNode = $getNodeByKey(key); var element = cursorNode === null || cursorNode === void 0 ? void 0 : cursorNode.getPreviousSibling(); if (!$isCardLikeElementNode(element) && !$isDecoratorNode(element) && !$isCardLikeElementNode(cursorNode === null || cursorNode === void 0 ? void 0 : cursorNode.getParent())) { needRemove.add(cursorNode); } } } } catch (err) { _iterator3.e(err); } finally { _iterator3.f(); } } if (needRemove.size > 0) { editor.update(function () { needRemove.forEach(function (node) { node.remove(); }); }); } return false; }); }), editor.registerUpdateListener(function () { editor.read(function () { var selection = $getSelection(); if (!$isRangeSelection(selection) || !selection.isCollapsed()) { return false; } var node = selection.anchor.getNode(); if (node instanceof CursorNode) { if (node.__text !== "\uFEFF") { editor.update(function () { node.setTextContent("\uFEFF"); var data = node.__text.replace("\uFEFF", ''); if (data) { var textNode = $createTextNode(data); node.insertAfter(textNode); textNode.selectEnd(); } }); } return false; } }); }), editor.registerCommand(KEY_BACKSPACE_COMMAND, function (event) { var selection = $getSelection(); if (!$isRangeSelection(selection) || !selection.isCollapsed()) { return false; } var node = selection.anchor.getNode(); if (node instanceof CursorNode) { event.preventDefault(); var prev = node.getPreviousSibling(); var parent = node.getParent(); var parentPrev = parent === null || parent === void 0 ? void 0 : parent.getPreviousSibling(); var needDispatch = false; if ($isDecoratorNode(prev)) { prev.selectPrevious(); node.remove(); prev.remove(); event.preventDefault(); return true; } else if (prev) { prev.selectEnd(); needDispatch = true; } else if (parent) { if (parent.getChildrenSize() === 1) { parent.remove(); } else if (parentPrev) { parentPrev.selectEnd(); needDispatch = true; } } if (needDispatch) { queueMicrotask(function () { editor.dispatchCommand(KEY_BACKSPACE_COMMAND, event); }); } return true; } return false; }, COMMAND_PRIORITY_HIGH), editor.registerCommand(KEY_ARROW_LEFT_COMMAND, function (event) { var selection = $getSelection(); if (!$isRangeSelection(selection)) { return false; } var focusNode = selection.focus.getNode(); if (!event.shiftKey) { if (focusNode instanceof CursorNode && !$isCardLikeElementNode(focusNode.getParent()) && !$isCardLikeElementNode(focusNode.getPreviousSibling())) { focusNode.selectStart(); } return false; } var prev = focusNode.getPreviousSibling(); if (selection.focus.offset === 0 && $isCursorNode(prev)) { try { var _selection$anchor = selection.anchor, anchorKey = _selection$anchor.key, anchorOffset = _selection$anchor.offset, anchorType = _selection$anchor.type; var sel = prev.selectPrevious(); var _sel$anchor = sel.anchor, key = _sel$anchor.key, offset = _sel$anchor.offset, type = _sel$anchor.type; sel.anchor.set(anchorKey, anchorOffset, anchorType); sel.focus.set(key, offset, type); $setSelection(sel); return true; } catch (error) { logger.error('❌ Cursor selection error:', error); } } else if ($isCursorNode(focusNode)) { try { var _selection$anchor2 = selection.anchor, _anchorKey = _selection$anchor2.key, _anchorOffset = _selection$anchor2.offset, _anchorType = _selection$anchor2.type; var _sel = focusNode.selectPrevious(); var _sel$anchor2 = _sel.anchor, _key = _sel$anchor2.key, _offset = _sel$anchor2.offset, _type = _sel$anchor2.type; _sel.anchor.set(_anchorKey, _anchorOffset, _anchorType); _sel.focus.set(_key, _offset, _type); $setSelection(_sel); return true; } catch (error) { logger.error('❌ Cursor navigation error:', error); } } return false; }, COMMAND_PRIORITY_HIGH), editor.registerCommand(KEY_ARROW_RIGHT_COMMAND, function (event) { var selection = $getSelection(); if (!$isRangeSelection(selection)) { return false; } if (!event.shiftKey) { var _focusNode = selection.focus.getNode(); if (_focusNode instanceof CursorNode && !$isCardLikeElementNode(_focusNode.getParent()) && !$isCardLikeElementNode(_focusNode.getPreviousSibling())) { _focusNode.selectEnd(); } return false; } var _selection$anchor3 = selection.anchor, anchorKey = _selection$anchor3.key, anchorOffset = _selection$anchor3.offset, anchorType = _selection$anchor3.type; var _selection$focus = selection.focus, focusKey = _selection$focus.key, focusOffset = _selection$focus.offset, focusType = _selection$focus.type; var focusNode = selection.focus.getNode(); if (focusType === 'text' && focusOffset !== focusNode.getTextContentSize() || focusType === 'element' && focusOffset !== focusNode.getChildrenSize()) { return false; } var sel = focusNode.selectNext(); if ($isCursorNode(sel.focus.getNode())) { sel.focus.getNode().selectNext(); var _sel$anchor3 = sel.anchor, key = _sel$anchor3.key, offset = _sel$anchor3.offset, type = _sel$anchor3.type; sel.anchor.set(anchorKey, anchorOffset, anchorType); sel.focus.set(key, offset, type); $setSelection(sel); return true; } else { selection.anchor.set(anchorKey, anchorOffset, anchorType); selection.focus.set(focusKey, focusOffset, focusType); } return false; }, COMMAND_PRIORITY_HIGH)); }