@lobehub/editor
Version:
A powerful and extensible rich text editor built on Meta's Lexical framework, providing a modern editing experience with React integration.
80 lines (77 loc) • 4.61 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 { Suspense, useEffect, useLayoutEffect, useMemo, useState } from 'react';
import { createPortal, flushSync } from 'react-dom';
import { CAN_USE_DOM } from "../../common/canUseDOM";
import { jsx as _jsx } from "react/jsx-runtime";
// This workaround is no longer necessary in React 19,
// but we currently support React >=17.x
// https://github.com/facebook/react/pull/26395
var useLayoutEffectImpl = CAN_USE_DOM ? useLayoutEffect : useEffect;
export function useDecorators(editor, ErrorBoundary) {
var _useState = useState(function () {
var _editor$getLexicalEdi;
return ((_editor$getLexicalEdi = editor.getLexicalEditor()) === null || _editor$getLexicalEdi === void 0 ? void 0 : _editor$getLexicalEdi.getDecorators()) || {};
}),
_useState2 = _slicedToArray(_useState, 2),
decorators = _useState2[0],
setDecorators = _useState2[1];
// Subscribe to changes
useLayoutEffectImpl(function () {
var clears = [];
var handleInit = function handleInit(editor) {
// Get initial decorators
var initialDecorators = editor.getDecorators();
setDecorators(initialDecorators);
clears.push(editor.registerDecoratorListener(function (nextDecorators) {
flushSync(function () {
setDecorators(nextDecorators);
});
}));
};
// Check if editor is already initialized
var lexicalEditor = editor.getLexicalEditor();
if (lexicalEditor) {
handleInit(lexicalEditor);
} else {
editor.on('initialized', handleInit);
clears.push(function () {
editor.off('initialized', handleInit);
});
}
return function () {
clears.forEach(function (clear) {
return clear();
});
};
}, [editor]);
// Return decorators defined as React Portals
return useMemo(function () {
var decoratedPortals = [];
var decoratorKeys = Object.keys(decorators);
for (var _i = 0, _decoratorKeys = decoratorKeys; _i < _decoratorKeys.length; _i++) {
var _editor$getLexicalEdi3;
var nodeKey = _decoratorKeys[_i];
var decorator = decorators[nodeKey];
var reactDecorator = /*#__PURE__*/_jsx(ErrorBoundary, {
onError: function onError(e) {
var _editor$getLexicalEdi2;
return (_editor$getLexicalEdi2 = editor.getLexicalEditor()) === null || _editor$getLexicalEdi2 === void 0 ? void 0 : _editor$getLexicalEdi2._onError(e);
},
children: /*#__PURE__*/_jsx(Suspense, {
fallback: null,
children: 'render' in decorator ? decorator.render : decorator
})
});
var element = (_editor$getLexicalEdi3 = editor.getLexicalEditor()) === null || _editor$getLexicalEdi3 === void 0 ? void 0 : _editor$getLexicalEdi3.getElementByKey(nodeKey);
if (element !== null && element !== undefined) {
decoratedPortals.push( /*#__PURE__*/createPortal(reactDecorator, 'queryDOM' in decorator ? decorator.queryDOM(element) : element, nodeKey));
}
}
return decoratedPortals;
}, [ErrorBoundary, decorators, editor]);
}