Files
echo-nexus/node_modules/@lexical/link/LexicalLink.prod.js
T

10 lines
6.8 KiB
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.
*
*/
"use strict";var t=require("@lexical/utils"),e=require("lexical");const n=new Set(["http:","https:","mailto:","sms:","tel:"]);class r extends e.ElementNode{static getType(){return"link"}static clone(t){return new r(t.__url,{rel:t.__rel,target:t.__target,title:t.__title},t.__key)}constructor(t="",e={},n){super(n);const{target:r=null,rel:i=null,title:s=null}=e;this.__url=t,this.__target=r,this.__rel=i,this.__title=s}createDOM(e){const n=document.createElement("a");return this.updateLinkDOM(null,n,e),t.addClassNamesToElement(n,e.theme.link),n}updateLinkDOM(e,n,r){if(t.isHTMLAnchorElement(n)){e&&e.__url===this.__url||(n.href=this.sanitizeUrl(this.__url));for(const t of["target","rel","title"]){const r=`__${t}`,i=this[r];e&&e[r]===i||(i?n[t]=i:n.removeAttribute(t))}}}updateDOM(t,e,n){return this.updateLinkDOM(t,e,n),!1}static importDOM(){return{a:t=>({conversion:i,priority:1})}}static importJSON(t){return s().updateFromJSON(t)}updateFromJSON(t){return super.updateFromJSON(t).setURL(t.url).setRel(t.rel||null).setTarget(t.target||null).setTitle(t.title||null)}sanitizeUrl(t){t=p(t);try{const e=new URL(p(t));if(!n.has(e.protocol))return"about:blank"}catch(e){return t}return t}exportJSON(){return{...super.exportJSON(),rel:this.getRel(),target:this.getTarget(),title:this.getTitle(),url:this.getURL()}}getURL(){return this.getLatest().__url}setURL(t){const e=this.getWritable();return e.__url=t,e}getTarget(){return this.getLatest().__target}setTarget(t){const e=this.getWritable();return e.__target=t,e}getRel(){return this.getLatest().__rel}setRel(t){const e=this.getWritable();return e.__rel=t,e}getTitle(){return this.getLatest().__title}setTitle(t){const e=this.getWritable();return e.__title=t,e}insertNewAfter(t,e=!0){const n=s(this.__url,{rel:this.__rel,target:this.__target,title:this.__title});return this.insertAfter(n,e),n}canInsertTextBefore(){return!1}canInsertTextAfter(){return!1}canBeEmpty(){return!1}isInline(){return!0}extractWithChild(t,n,r){if(!e.$isRangeSelection(n))return!1;const i=n.anchor.getNode(),s=n.focus.getNode();return this.isParentOf(i)&&this.isParentOf(s)&&n.getTextContent().length>0}isEmailURI(){return this.__url.startsWith("mailto:")}isWebSiteURI(){return this.__url.startsWith("https://")||this.__url.startsWith("http://")}}function i(e){let n=null;if(t.isHTMLAnchorElement(e)){const t=e.textContent;(null!==t&&""!==t||e.children.length>0)&&(n=s(e.getAttribute("href")||"",{rel:e.getAttribute("rel"),target:e.getAttribute("target"),title:e.getAttribute("title")}))}return{node:n}}function s(t="",n){return e.$applyNodeReplacement(new r(t,n))}function l(t){return t instanceof r}class o extends r{constructor(t="",e={},n){super(t,e,n),this.__isUnlinked=void 0!==e.isUnlinked&&null!==e.isUnlinked&&e.isUnlinked}static getType(){return"autolink"}static clone(t){return new o(t.__url,{isUnlinked:t.__isUnlinked,rel:t.__rel,target:t.__target,title:t.__title},t.__key)}getIsUnlinked(){return this.__isUnlinked}setIsUnlinked(t){const e=this.getWritable();return e.__isUnlinked=t,e}createDOM(t){return this.__isUnlinked?document.createElement("span"):super.createDOM(t)}updateDOM(t,e,n){return super.updateDOM(t,e,n)||t.__isUnlinked!==this.__isUnlinked}static importJSON(t){return u().updateFromJSON(t)}updateFromJSON(t){return super.updateFromJSON(t).setIsUnlinked(t.isUnlinked||!1)}static importDOM(){return null}exportJSON(){return{...super.exportJSON(),isUnlinked:this.__isUnlinked}}insertNewAfter(t,n=!0){const r=this.getParentOrThrow().insertNewAfter(t,n);if(e.$isElementNode(r)){const t=u(this.__url,{isUnlinked:this.__isUnlinked,rel:this.__rel,target:this.__target,title:this.__title});return r.append(t),t}return null}}function u(t="",n){return e.$applyNodeReplacement(new o(t,n))}function a(t){return t instanceof o}const c=e.createCommand("TOGGLE_LINK_COMMAND");function _(t,n){if("element"===t.type){const r=t.getNode();e.$isElementNode(r)||function(t,...e){const n=new URL("https://lexical.dev/docs/error"),r=new URLSearchParams;r.append("code",t);for(const t of e)r.append("v",t);throw n.search=r.toString(),Error(`Minified Lexical error #${t}; visit ${n.toString()} for the full message or use the non-minified dev environment for full errors and additional helpful warnings.`)}(252);return r.getChildren()[t.offset+n]||null}return null}function d(n,r={}){const{target:i,title:o}=r,u=void 0===r.rel?"noreferrer":r.rel,c=e.$getSelection();if(null===c||!e.$isRangeSelection(c)&&!e.$isNodeSelection(c))return;if(e.$isNodeSelection(c)){const e=c.getNodes();if(0===e.length)return;return void e.forEach((e=>{if(null===n){const n=t.$findMatchingParent(e,(t=>!a(t)&&l(t)));n&&(n.insertBefore(e),0===n.getChildren().length&&n.remove())}else{const r=t.$findMatchingParent(e,(t=>!a(t)&&l(t)));if(r)r.setURL(n),void 0!==i&&r.setTarget(i),void 0!==u&&r.setRel(u);else{const t=s(n,{rel:u,target:i});e.insertBefore(t),t.append(e)}}}))}const d=c.extract();if(null===n)return void d.forEach((e=>{const n=t.$findMatchingParent(e,(t=>!a(t)&&l(t)));if(n){const t=n.getChildren();for(let e=0;e<t.length;e++)n.insertBefore(t[e]);n.remove()}}));const g=new Set,f=t=>{g.has(t.getKey())||(g.add(t.getKey()),t.setURL(n),void 0!==i&&t.setTarget(i),void 0!==u&&t.setRel(u),void 0!==o&&t.setTitle(o))};if(1===d.length){const t=h(d[0],l);if(null!==t)return f(t)}!function(t){const n=e.$getSelection();if(!e.$isRangeSelection(n))return t();const r=e.$normalizeSelection__EXPERIMENTAL(n),i=r.isBackward(),s=_(r.anchor,i?-1:0),l=_(r.focus,i?0:-1),o=t();if(s||l){const t=e.$getSelection();if(e.$isRangeSelection(t)){const n=t.clone();if(s){const t=s.getParent();t&&n.anchor.set(t.getKey(),s.getIndexWithinParent()+(i?1:0),"element")}if(l){const t=l.getParent();t&&n.focus.set(t.getKey(),l.getIndexWithinParent()+(i?0:1),"element")}e.$setSelection(e.$normalizeSelection__EXPERIMENTAL(n))}}}((()=>{let t=null;for(const r of d){if(!r.isAttached())continue;const c=h(r,l);if(c){f(c);continue}if(e.$isElementNode(r)){if(!r.isInline())continue;if(l(r)){if(!(a(r)||null!==t&&t.getParentOrThrow().isParentOf(r))){f(r),t=r;continue}for(const t of r.getChildren())r.insertBefore(t);r.remove();continue}}const _=r.getPreviousSibling();l(_)&&_.is(t)?_.append(r):(t=s(n,{rel:u,target:i,title:o}),r.insertAfter(t),t.append(r))}}))}const g=d;function h(t,e){let n=t;for(;null!==n&&null!==n.getParent()&&!e(n);)n=n.getParentOrThrow();return e(n)?n:null}const f=/^\+?[0-9\s()-]{5,}$/;function p(t){return t.match(/^[a-z][a-z0-9+.-]*:/i)||t.match(/^[/#.]/)?t:t.includes("@")?`mailto:${t}`:f.test(t)?`tel:${t}`:`https://${t}`}exports.$createAutoLinkNode=u,exports.$createLinkNode=s,exports.$isAutoLinkNode=a,exports.$isLinkNode=l,exports.$toggleLink=d,exports.AutoLinkNode=o,exports.LinkNode=r,exports.TOGGLE_LINK_COMMAND=c,exports.formatUrl=p,exports.toggleLink=g;