@nexusui/components
Version:
These are custom components specially-developed for NexusUI applications. They will make your life easier by giving you out-of-the-box implementations for various high-level UI elements that you can drop directly into your application.
2 lines (1 loc) • 1.99 kB
JavaScript
import{jsx as t,Fragment as e}from"react/jsx-runtime";import{forwardRef as o,useState as n,useEffect as r}from"react";import{useLexicalComposerContext as s}from"@lexical/react/LexicalComposerContext";import{COMMAND_PRIORITY_LOW as i,CLICK_COMMAND as c,$getSelection as f,$isRangeSelection as m,$isTextNode as a,$createTextNode as l}from"lexical";import{getSelectedNode as x}from"../utils/getSelectedNode.js";import{TOGGLE_INSERT_LINK_MODAL_COMMAND as u}from"../command/index.js";import{InsertLinkModal as g}from"../components/InsertLinkModal.js";import{openLink as p}from"../../../util/functions/index.js";import{mergeRegister as d,$findMatchingParent as h}from"@lexical/utils";import{$isLinkNode as C,toggleLink as T}from"@lexical/link";const j=o((({t:o},j)=>{const[y]=s(),[K,k]=n(!1),[L,S]=n(""),[M,U]=n("");r((()=>{const t=()=>{const t=f();if(m(t)){const e=x(t),o=h(e,C);if(h(e,a)){const n=Math.min(t.anchor.offset,t.focus.offset),r=Math.max(t.anchor.offset,t.focus.offset);n<r&&S(e.getTextContent().slice(n,r)),o&&(U(o.getURL()),S(e.getTextContent()),y.update((()=>{const e=f();if(m(e)){const o=x(t),n=h(o,a);n&&(e.focus.set(n.getKey(),0,"text"),e.anchor.set(n.getKey(),n.getTextContentSize(),"text"))}})))}}};return d(y.registerCommand(u,(()=>(t(),k(!0),!1)),i),y.registerCommand(c,(e=>{const o=f();if(m(o)){const n=x(o),r=h(n,C);if(C(r)){const n=r.getTextContent(),s=o.anchor.offset;return 0===s||s===n.length||(s===o.focus.offset?(t(),k(!0),!0):((e.metaKey||e.ctrlKey)&&p(r.getURL(),"_blank"),!0))}}return k(!1),b(),!1}),i))}),[y]);const b=()=>{S(""),U("")};return K&&j?.current?t(g,{anchorEl:j?.current,onCancel:()=>{k(!1),b()},onSave:(t,e)=>{((t,e,o)=>{y.update((()=>{const n=f();if(m(n)){const{anchor:r,focus:s}=n,i=l(t);n.insertNodes([i]),r.offset-=t.length,s.offset=r.offset+t.length,T(e,{target:"_blank",title:e}),o&&r.set(i.getKey(),i.getTextContentSize(),"text")}}))})(t,e,!L),k(!1),b()},initialText:L,initialUrl:M,t:o},`${L}-${M}`):t(e,{})}));export{j as InsertLinkPlugin};