/** * 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. * */ "use strict";var e=require("@lexical/link"),t=require("@lexical/react/LexicalComposerContext"),n=require("@lexical/utils"),i=require("lexical"),r=require("react");function o(e,t){for(let n=0;n0?s(n[e-1]):c(i[0])))return!1;return t0;){const e=u[0],g=e.getTextContent().length,c=s;s+g<=t?(i.push(e),l+=g):c>=n?o.push(e):r.push(e),s+=g,u.shift()}return[l,i,r,o]}function x(t,n,r,o){const l=e.$createAutoLinkNode(o.url,o.attributes);if(1===t.length){let e,s=t[0];0===n?[e,s]=s.splitText(r):[,e,s]=s.splitText(n,r);const u=i.$createTextNode(o.text);return u.setFormat(e.getFormat()),u.setDetail(e.getDetail()),u.setStyle(e.getStyle()),l.append(u),e.replace(l),s}if(t.length>1){const e=t[0];let o,s=e.getTextContent().length;0===n?o=e:[,o]=e.splitText(n);const u=[];let g;for(let e=1;e=0;n--)e.insertAfter(t[n]);return e.remove(),t.map((e=>e.getLatest()))}function T(t,l,s){r.useEffect((()=>{t.hasNodes([e.AutoLinkNode])||function(e,...t){const n=new URL("https://lexical.dev/docs/error"),i=new URLSearchParams;i.append("code",e);for(const e of t)i.append("v",e);throw n.search=i.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.`)}(77);const r=(e,t)=>{s&&s(e,t)};return n.mergeRegister(t.registerNodeTransform(i.TextNode,(t=>{const n=t.getParentOrThrow(),s=t.getPreviousSibling();if(e.$isAutoLinkNode(n)&&!n.getIsUnlinked())h(n,l,r);else if(!e.$isLinkNode(n)){if(t.isSimpleText()&&(g(t.getTextContent())||!e.$isAutoLinkNode(s))){const e=function(e){const t=[e];let n=e.getNextSibling();for(;null!==n&&i.$isTextNode(n)&&n.isSimpleText()&&(t.push(n),!/[\s]/.test(n.getTextContent()));)n=n.getNextSibling();return t}(t);!function(e,t,n){let i=[...e];const r=i.map((e=>e.getTextContent())).join("");let l,s=r,u=0;for(;(l=o(s,t))&&null!==l;){const e=l.index,t=e+l.length;if(f(u+e,u+t,r,i)){const[r,,o,s]=d(i,u+e,u+t),g=x(o,u+e-r,u+t-r,l);i=g?[g,...s]:s,n(l.url,null),u=0}else u+=t;s=s.substring(t)}}(e,l,r)}!function(t,n,i){const r=t.getPreviousSibling(),o=t.getNextSibling(),l=t.getTextContent();var s;!e.$isAutoLinkNode(r)||r.getIsUnlinked()||g(l)&&(s=l,!(r.isEmailURI()?/^\.[a-zA-Z]{2,}/.test(s):/^\.[a-zA-Z0-9]{1,}/.test(s)))||(r.append(t),h(r,n,i),i(null,r.getURL())),!e.$isAutoLinkNode(o)||o.getIsUnlinked()||u(l)||(p(o),h(o,n,i),i(null,o.getURL()))}(t,l,r)}})),t.registerCommand(e.TOGGLE_LINK_COMMAND,(t=>{const n=i.$getSelection();if(null!==t||!i.$isRangeSelection(n))return!1;return n.extract().forEach((t=>{const n=t.getParent();e.$isAutoLinkNode(n)&&(n.setIsUnlinked(!n.getIsUnlinked()),n.markDirty())})),!1}),i.COMMAND_PRIORITY_LOW))}),[t,l,s])}exports.AutoLinkPlugin=function({matchers:e,onChange:n}){const[i]=t.useLexicalComposerContext();return T(i,e,n),null},exports.createLinkMatcherWithRegExp=function(e,t=(e=>e)){return n=>{const i=e.exec(n);return null===i?null:{index:i.index,length:i[0].length,text:i[0],url:t(i[0])}}};