UNPKG

@lexical/react

Version:

This package provides Lexical components and hooks for React applications.

10 lines (8 loc) • 2.67 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{$isLinkNode as e,LinkNode as t,AutoLinkNode as n}from"@lexical/link";import{INSERT_EMBED_COMMAND as r}from"@lexical/react/LexicalAutoEmbedPluginUtils";export{AutoEmbedOption,INSERT_EMBED_COMMAND,URL_MATCHER}from"@lexical/react/LexicalAutoEmbedPluginUtils";import{useLexicalComposerContext as o}from"@lexical/react/LexicalComposerContext";import{LexicalNodeMenuPlugin as l}from"@lexical/react/LexicalNodeMenuPlugin";import{objectKlassEquals as i}from"@lexical/utils";import{$getNodeByKey as u,mergeRegister as a,PASTE_COMMAND as c,$onUpdate as m,COMMAND_PRIORITY_BEFORE_EDITOR as s,COMMAND_PRIORITY_EDITOR as d,$getSelection as f,COMMAND_PRIORITY_LOW as p,PASTE_TAG as g}from"lexical";import{useState as x,useRef as C,useCallback as b,useEffect as E,useMemo as M}from"react";import{jsx as y}from"react/jsx-runtime";function L(t,n){const r=u(t);return e(r)&&r.isAttached()&&r.getURL()===n?r:null}function A(e,t){return null!=t&&t(e)}function R({embedConfigs:R,onOpenEmbedModalForConfig:P,getMenuOptions:U,menuRenderFn:k,menuCommandPriority:v=p,$disableMenuFor:D}){const[O]=o(),[h,F]=x(null),N=null===h?null:h.key,S=null===h?null:h.embedConfig,T=C(0),_=b(()=>{F(null)},[]),I=b(async t=>{const n=++T.current,r=O.read("latest",function(){const n=u(t);if(e(n)){if(A(n,D))return;return n.getURL()}});if(void 0!==r)for(let e=R.length-1;e>=0;e--){const o=R[e],l=await Promise.resolve(o.parseUrl(r));if(n!==T.current)return;if(null!=l)return void(O.read("pending",()=>{const e=L(t,r);return null!==e&&!A(e,D)})&&F({embedConfig:o,key:t,result:l,url:r}))}},[D,O,R]);E(()=>{let e=!1;const r=(t,{updateTags:n})=>{for(const[r,o]of t)"created"===o&&n.has(g)&&e?I(r):r===N&&_()};return a(O.registerCommand(c,t=>(e=i(t,ClipboardEvent)&&null!==t.clipboardData&&/^\S+$/.test(t.clipboardData.getData("text/plain")),e&&m(()=>{e=!1}),!1),s),...[t,n].map(e=>O.registerMutationListener(e,r,{skipInitialization:!0})))},[I,O,N,_]),E(()=>{if(P)return O.registerCommand(r,e=>{const t=R.find(({type:t})=>t===e);return!!t&&(P(t),!0)},d)},[O,R,P]);const $=b(()=>{if(null==h)return;const{embedConfig:e,key:t,result:n,url:r}=h;O.update(()=>{const o=L(t,r);null!==o&&(f()||o.selectEnd(),e.insertNode(O,n),o.isAttached()&&o.remove())})},[O,h]),j=M(()=>null!=S&&null!=N?U(S,$,_):[],[S,$,U,N,_]),w=b((e,t,n)=>{O.update(()=>{e.onSelect(t),n()})},[O]);return null!=N?/*#__PURE__*/y(l,{nodeKey:N,onClose:_,onSelectOption:w,options:j,menuRenderFn:k,commandPriority:v}):null}export{R as LexicalAutoEmbedPlugin};