UNPKG

@lexical/react

Version:

This package provides Lexical components and hooks for React applications.

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