@lexical/react
Version:
This package provides Lexical components and hooks for React applications.
10 lines (8 loc) • 1.44 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.
*
*/
import{createLexicalComposerContext as t,LexicalComposerContext as e}from"@lexical/react/LexicalComposerContext";import{CAN_USE_DOM as n,createEditor as o,$getRoot as r,$createParagraphNode as i,getActiveElement as a,$getSelection as l,HISTORY_MERGE_TAG as c}from"lexical";import{useLayoutEffect as s,useEffect as m,useMemo as d}from"react";import{jsx as u}from"react/jsx-runtime";const p=n?s:m,f={tag:c};function E({initialConfig:c,children:s}){const m=d(()=>{const{theme:e,namespace:s,nodes:m,onError:d,onWarn:u,editorState:p,html:E}=c,b=t(null,e),h=o({editable:c.editable,html:E,namespace:s,nodes:m,onError:t=>d(t,h),...u?{onWarn:t=>u(t,h)}:{},theme:e});return function(t,e){if(null===e)return;if(void 0===e)t.update(()=>{const e=r();if(e.isEmpty()){const o=i();e.append(o);const r=t.getRootElement(),c=n&&null!==r?a(r):null;(null!==l()||null!==c&&c===r)&&o.select()}},f);else if(null!==e)switch(typeof e){case"string":{const n=t.parseEditorState(e);t.setEditorState(n,f);break}case"object":t.setEditorState(e,f);break;case"function":t.update(()=>{r().isEmpty()&&e(t)},f)}}(h,p),[h,b]},[]);return p(()=>{const t=c.editable,[e]=m;e.setEditable(void 0===t||t)},[]),/*#__PURE__*/u(e.Provider,{value:m,children:s})}export{E as LexicalComposer};