@lexical/react
Version:
This package provides Lexical components and hooks for React applications.
10 lines (8 loc) • 2.32 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{$isLinkNode as e,LinkNode as t,AutoLinkNode as n}from"@lexical/link";import{useLexicalComposerContext as o}from"@lexical/react/LexicalComposerContext";import{MenuOption as r,LexicalNodeMenuPlugin as i}from"@lexical/react/LexicalNodeMenuPlugin";import{mergeRegister as l}from"@lexical/utils";import{createCommand as s,$getNodeByKey as a,COMMAND_PRIORITY_EDITOR as c,$getSelection as u,COMMAND_PRIORITY_LOW as m,PASTE_TAG as d}from"lexical";import{useState as p,useCallback as f,useEffect as x,useMemo as g}from"react";import{jsx as w}from"react/jsx-runtime";const C=/((https?:\/\/(www\.)?)|(www\.))[-a-zA-Z0-9@:%._+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()@:%_+.~#?&//=]*)/,y=s("INSERT_EMBED_COMMAND");class E extends r{constructor(e,t){super(e),this.title=e,this.onSelect=t.onSelect.bind(this)}}function M({embedConfigs:r,onOpenEmbedModalForConfig:s,getMenuOptions:C,menuRenderFn:E,menuCommandPriority:M=m}){const[S]=o(),[h,_]=p(null),[v,z]=p(null),A=f((()=>{_(null),z(null)}),[]),L=f((async t=>{const n=S.getEditorState().read((function(){const n=a(t);if(e(n))return n.getURL()}));if(void 0!==n)for(const e of r){null!=await Promise.resolve(e.parseUrl(n))&&(z(e),_(t))}}),[S,r]);x((()=>l(...[t,n].map((e=>S.registerMutationListener(e,((...e)=>((e,{updateTags:t,dirtyLeaves:n})=>{for(const[o,r]of e)"created"===r&&t.has(d)&&n.size<=3?L(o):o===h&&A()})(...e)),{skipInitialization:!0}))))),[L,S,r,h,A]),x((()=>S.registerCommand(y,(e=>{const t=r.find((({type:t})=>t===e));return!!t&&(s(t),!0)}),c)),[S,r,s]);const P=f((async function(){if(null!=v&&null!=h){const t=S.getEditorState().read((()=>{const t=a(h);return e(t)?t:null}));if(e(t)){const e=await Promise.resolve(v.parseUrl(t.__url));null!=e&&S.update((()=>{u()||t.selectEnd(),v.insertNode(S,e),t.isAttached()&&t.remove()}))}}}),[v,S,h]),b=g((()=>null!=v&&null!=h?C(v,P,A):[]),[v,P,C,h,A]),N=f(((e,t,n)=>{S.update((()=>{e.onSelect(t),n()}))}),[S]);return null!=h?w(i,{nodeKey:h,onClose:A,onSelectOption:N,options:b,menuRenderFn:E,commandPriority:M}):null}export{E as AutoEmbedOption,y as INSERT_EMBED_COMMAND,M as LexicalAutoEmbedPlugin,C as URL_MATCHER};