UNPKG

@lexical/react

Version:

This package provides Lexical components and hooks for React applications.

10 lines (8 loc) 1.42 kB
/** * 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. * */ "use strict";var e=require("@lexical/react/LexicalComposerContext"),t=require("lexical"),o=require("react"),r=require("react/jsx-runtime");const n="undefined"!=typeof window&&void 0!==window.document&&void 0!==window.document.createElement,i=n?o.useLayoutEffect:o.useEffect,a={tag:"history-merge"};exports.LexicalComposer=function({initialConfig:c,children:s}){const l=o.useMemo((()=>{const{theme:o,namespace:r,nodes:i,onError:s,editorState:l,html:d}=c,u=e.createLexicalComposerContext(null,o),m=t.createEditor({editable:c.editable,html:d,namespace:r,nodes:i,onError:e=>s(e,m),theme:o});return function(e,o){if(null===o)return;if(void 0===o)e.update((()=>{const o=t.$getRoot();if(o.isEmpty()){const r=t.$createParagraphNode();o.append(r);const i=n?document.activeElement:null;(null!==t.$getSelection()||null!==i&&i===e.getRootElement())&&r.select()}}),a);else if(null!==o)switch(typeof o){case"string":{const t=e.parseEditorState(o);e.setEditorState(t,a);break}case"object":e.setEditorState(o,a);break;case"function":e.update((()=>{t.$getRoot().isEmpty()&&o(e)}),a)}}(m,l),[m,u]}),[]);return i((()=>{const e=c.editable,[t]=l;t.setEditable(void 0===e||e)}),[]),r.jsx(e.LexicalComposerContext.Provider,{value:l,children:s})};