@lexical/react
Version:
This package provides Lexical components and hooks for React applications.
10 lines (8 loc) • 4.97 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{AutoLinkNode as t,$isAutoLinkNode as e,$isLinkNode as n,TOGGLE_LINK_COMMAND as r,$createAutoLinkNode as l}from"@lexical/link";import{useLexicalComposerContext as o}from"@lexical/react/LexicalComposerContext";import{mergeRegister as i}from"@lexical/utils";import{TextNode as s,$getSelection as u,$isRangeSelection as g,COMMAND_PRIORITY_LOW as c,$isTextNode as a,$isElementNode as f,$isLineBreakNode as h,$createTextNode as x,$isNodeSelection as p}from"lexical";import{useEffect as d}from"react";function m(t){return t&&t.__esModule&&Object.prototype.hasOwnProperty.call(t,"default")?t.default:t}var T=m((function(t){const e=new URLSearchParams;e.append("code",t);for(let t=1;t<arguments.length;t++)e.append("v",arguments[t]);throw Error(`Minified Lexical error #${t}; visit https://lexical.dev/docs/error?${e} for the full message or use the non-minified dev environment for full errors and additional helpful warnings.`)}));function C(t,e=(t=>t)){return n=>{const r=t.exec(n);return null===r?null:{index:r.index,length:r[0].length,text:r[0],url:e(r[0])}}}function b(t,e){for(let n=0;n<e.length;n++){const r=e[n](t);if(r)return r}return null}const v=/[.,;\s]/;function S(t){return v.test(t)}function U(t){return S(t[t.length-1])}function L(t){return S(t[0])}function R(t){let e=t.getPreviousSibling();return f(e)&&(e=e.getLastDescendant()),null===e||h(e)||a(e)&&U(e.getTextContent())}function k(t){let e=t.getNextSibling();return f(e)&&(e=e.getFirstDescendant()),null===e||h(e)||a(e)&&L(e.getTextContent())}function y(t,e,n,r){if(!(t>0?S(n[t-1]):R(r[0])))return!1;return e<n.length?S(n[e]):k(r[r.length-1])}function D(t,e,n){const r=[],l=[],o=[];let i=0,s=0;const u=[...t];for(;u.length>0;){const t=u[0],g=t.getTextContent().length,c=s;s+g<=e?(r.push(t),i+=g):c>=n?o.push(t):l.push(t),s+=g,u.shift()}return[i,r,l,o]}function N(t,e,n,r){const o=l(r.url,r.attributes);if(1===t.length){let l,i=t[0];0===e?[l,i]=i.splitText(n):[,l,i]=i.splitText(e,n);const s=x(r.text);return s.setFormat(l.getFormat()),s.setDetail(l.getDetail()),s.setStyle(l.getStyle()),o.append(s),l.replace(o),i}if(t.length>1){const r=t[0];let l,i=r.getTextContent().length;0===e?l=r:[,l]=r.splitText(e);const s=[];let c;for(let e=1;e<t.length;e++){const r=t[e],l=r.getTextContent().length,o=i;if(o<n)if(i+l<=n)s.push(r);else{const[t,e]=r.splitText(n-o);s.push(t),c=e}i+=l}const f=u(),h=f?f.getNodes().find(a):void 0,d=x(l.getTextContent());return d.setFormat(l.getFormat()),d.setDetail(l.getDetail()),d.setStyle(l.getStyle()),o.append(d,...s),h&&h===l&&(g(f)?d.select(f.anchor.offset,f.focus.offset):p(f)&&d.select(0,d.getTextContent().length)),l.replace(o),c}}function P(t,e,n){const r=t.getChildren(),l=r.length;for(let e=0;e<l;e++){const l=r[e];if(!a(l)||!l.isSimpleText())return I(t),void n(null,t.getURL())}const o=t.getTextContent(),i=b(o,e);if(null===i||i.text!==o)return I(t),void n(null,t.getURL());if(!R(t)||!k(t))return I(t),void n(null,t.getURL());const s=t.getURL();if(s!==i.url&&(t.setURL(i.url),n(i.url,s)),i.attributes){const e=t.getRel();e!==i.attributes.rel&&(t.setRel(i.attributes.rel||null),n(i.attributes.rel||null,e));const r=t.getTarget();r!==i.attributes.target&&(t.setTarget(i.attributes.target||null),n(i.attributes.target||null,r))}}function I(t){const e=t.getChildren();for(let n=e.length-1;n>=0;n--)t.insertAfter(e[n]);return t.remove(),e.map((t=>t.getLatest()))}function w(l,o,f){d((()=>{l.hasNodes([t])||T(77);const h=(t,e)=>{f&&f(t,e)};return i(l.registerNodeTransform(s,(t=>{const r=t.getParentOrThrow(),l=t.getPreviousSibling();if(e(r)&&!r.getIsUnlinked())P(r,o,h);else if(!n(r)){if(t.isSimpleText()&&(L(t.getTextContent())||!e(l))){const e=function(t){const e=[t];let n=t.getNextSibling();for(;null!==n&&a(n)&&n.isSimpleText()&&(e.push(n),!/[\s]/.test(n.getTextContent()));)n=n.getNextSibling();return e}(t);!function(t,e,n){let r=[...t];const l=r.map((t=>t.getTextContent())).join("");let o,i=l,s=0;for(;(o=b(i,e))&&null!==o;){const t=o.index,e=t+o.length;if(y(s+t,s+e,l,r)){const[l,,i,u]=D(r,s+t,s+e),g=N(i,s+t-l,s+e-l,o);r=g?[g,...u]:u,n(o.url,null),s=0}else s+=e;i=i.substring(e)}}(e,o,h)}!function(t,n,r){const l=t.getPreviousSibling(),o=t.getNextSibling(),i=t.getTextContent();var s;!e(l)||l.getIsUnlinked()||L(i)&&(s=i,!(l.isEmailURI()?/^\.[a-zA-Z]{2,}/.test(s):/^\.[a-zA-Z0-9]{1,}/.test(s)))||(l.append(t),P(l,n,r),r(null,l.getURL())),!e(o)||o.getIsUnlinked()||U(i)||(I(o),P(o,n,r),r(null,o.getURL()))}(t,o,h)}})),l.registerCommand(r,(t=>{const n=u();if(null!==t||!g(n))return!1;return n.extract().forEach((t=>{const n=t.getParent();if(e(n))return n.setIsUnlinked(!n.getIsUnlinked()),n.markDirty(),!0})),!1}),c))}),[l,o,f])}function F({matchers:t,onChange:e}){const[n]=o();return w(n,t,e),null}export{F as AutoLinkPlugin,C as createLinkMatcherWithRegExp};