UNPKG

@lexical/react

Version:

This package provides Lexical components and hooks for React applications.

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