UNPKG

@lexical/react

Version:

This package provides Lexical components and hooks for React applications.

10 lines (8 loc) 2.19 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{useCollaborationContext as e}from"@lexical/react/LexicalCollaborationContext";import{LexicalComposerContext as t,createLexicalComposerContext as n}from"@lexical/react/LexicalComposerContext";import{useRef as o,useContext as r,useMemo as i,useEffect as s}from"react";import{jsx as a}from"react/jsx-runtime";function l(e){const t=e.transform();return new Set(t?[t]:[])}const c=()=>{},p=()=>{};function f({initialEditor:f,children:d,initialNodes:m,initialTheme:u,skipCollabChecks:h,skipEditableListener:g}){const _=o(!1),x=r(t);null==x&&function(e,...t){const n=new URL("https://lexical.dev/docs/error"),o=new URLSearchParams;o.append("code",e);for(const e of t)o.append("v",e);throw n.search=o.toString(),Error(`Minified Lexical error #${e}; visit ${n.toString()} for the full message or use the non-minified dev environment for full errors and additional helpful warnings.`)}(9);const[v,{getTheme:E}]=x,w=i((()=>{const e=u||E()||void 0,t=n(x,e);void 0!==e&&(f._config.theme=e),f._parentEditor=f._parentEditor||v;const o=f._createEditorArgs,r=o&&o.namespace;if(m){c(),r||(p(),f._config.namespace=v._config.namespace);for(let e of m){let t=null,n=null;if("function"!=typeof e){const o=e;e=o.replace,t=o.with,n=o.withKlass||null}const o=f._nodes.get(e.getType());f._nodes.set(e.getType(),{exportDOM:o?o.exportDOM:void 0,klass:e,replace:t,replaceWithKlass:n,transforms:l(e)})}}else if(o&&o.nodes)r||(p(),f._config.namespace=v._config.namespace);else{const e=f._nodes=new Map(v._nodes);r||(f._config.namespace=v._config.namespace);for(const[t,n]of e)f._nodes.set(t,{exportDOM:n.exportDOM,klass:n.klass,replace:n.replace,replaceWithKlass:n.replaceWithKlass,transforms:l(n.klass)})}return[f,t]}),[]),{isCollabActive:b,yjsDocMap:k}=e(),C=h||_.current||k.has(f.getKey());return s((()=>{C&&(_.current=!0)}),[C]),s((()=>{if(!g){const e=e=>f.setEditable(e);return e(v.isEditable()),v.registerEditableListener(e)}}),[f,v,g]),a(t.Provider,{value:w,children:!b||C?d:null})}export{f as LexicalNestedComposer};