@lexical/react
Version:
This package provides Lexical components and hooks for React applications.
12 lines (10 loc) • 1.42 kB
JavaScript
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
*/
;var e=require("@lexical/react/LexicalComposerContext"),f=require("lexical"),g=require("react"),l=require("react/jsx-runtime");let m="undefined"!==typeof window&&"undefined"!==typeof window.document&&"undefined"!==typeof window.document.createElement,n=m?g.useLayoutEffect:g.useEffect,p={tag:"history-merge"};
function q(a,c){if(null!==c)if(void 0===c)a.update(()=>{var b=f.$getRoot();if(b.isEmpty()){let d=f.$createParagraphNode();b.append(d);b=m?document.activeElement:null;(null!==f.$getSelection()||null!==b&&b===a.getRootElement())&&d.select()}},p);else if(null!==c)switch(typeof c){case "string":let b=a.parseEditorState(c);a.setEditorState(b,p);break;case "object":a.setEditorState(c,p);break;case "function":a.update(()=>{f.$getRoot().isEmpty()&&c(a)},p)}}
exports.LexicalComposer=function({initialConfig:a,children:c}){let b=g.useMemo(()=>{const {theme:d,namespace:h,nodes:r,onError:t,editorState:u,html:v}=a,w=e.createLexicalComposerContext(null,d),k=f.createEditor({editable:a.editable,html:v,namespace:h,nodes:r,onError:x=>t(x,k),theme:d});q(k,u);return[k,w]},[]);n(()=>{let d=a.editable,[h]=b;h.setEditable(void 0!==d?d:!0)},[]);return l.jsx(e.LexicalComposerContext.Provider,{value:b,children:c})}