Some checks failed
Build & Deploy / 🔍 Prepare (push) Successful in 20s
Build & Deploy / 🧪 QA (push) Failing after 34s
Build & Deploy / 🏗️ Build (push) Has started running
Build & Deploy / 🚀 Deploy (push) Has been cancelled
Build & Deploy / 🧪 Smoke Test (push) Has been cancelled
Build & Deploy / ⚡ Lighthouse (push) Has been cancelled
Build & Deploy / 🔔 Notify (push) Has been cancelled
10 lines
6.6 KiB
Plaintext
10 lines
6.6 KiB
Plaintext
/**
|
|
* 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{addClassNamesToElement as t,isHTMLAnchorElement as e,$findMatchingParent as r}from"@lexical/utils";import{createCommand as n,ElementNode as i,$isRangeSelection as s,$applyNodeReplacement as l,$isElementNode as o,$getSelection as u,$isNodeSelection as a,$normalizeSelection__EXPERIMENTAL as c,$setSelection as _}from"lexical";const h=new Set(["http:","https:","mailto:","sms:","tel:"]);class g extends i{static getType(){return"link"}static clone(t){return new g(t.__url,{rel:t.__rel,target:t.__target,title:t.__title},t.__key)}constructor(t="",e={},r){super(r);const{target:n=null,rel:i=null,title:s=null}=e;this.__url=t,this.__target=n,this.__rel=i,this.__title=s}createDOM(e){const r=document.createElement("a");return this.updateLinkDOM(null,r,e),t(r,e.theme.link),r}updateLinkDOM(t,r,n){if(e(r)){t&&t.__url===this.__url||(r.href=this.sanitizeUrl(this.__url));for(const e of["target","rel","title"]){const n=`__${e}`,i=this[n];t&&t[n]===i||(i?r[e]=i:r.removeAttribute(e))}}}updateDOM(t,e,r){return this.updateLinkDOM(t,e,r),!1}static importDOM(){return{a:t=>({conversion:f,priority:1})}}static importJSON(t){return d().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=S(t);try{const e=new URL(S(t));if(!h.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 r=d(this.__url,{rel:this.__rel,target:this.__target,title:this.__title});return this.insertAfter(r,e),r}canInsertTextBefore(){return!1}canInsertTextAfter(){return!1}canBeEmpty(){return!1}isInline(){return!0}extractWithChild(t,e,r){if(!s(e))return!1;const n=e.anchor.getNode(),i=e.focus.getNode();return this.isParentOf(n)&&this.isParentOf(i)&&e.getTextContent().length>0}isEmailURI(){return this.__url.startsWith("mailto:")}isWebSiteURI(){return this.__url.startsWith("https://")||this.__url.startsWith("http://")}}function f(t){let r=null;if(e(t)){const e=t.textContent;(null!==e&&""!==e||t.children.length>0)&&(r=d(t.getAttribute("href")||"",{rel:t.getAttribute("rel"),target:t.getAttribute("target"),title:t.getAttribute("title")}))}return{node:r}}function d(t="",e){return l(new g(t,e))}function p(t){return t instanceof g}class m extends g{constructor(t="",e={},r){super(t,e,r),this.__isUnlinked=void 0!==e.isUnlinked&&null!==e.isUnlinked&&e.isUnlinked}static getType(){return"autolink"}static clone(t){return new m(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,r){return super.updateDOM(t,e,r)||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,e=!0){const r=this.getParentOrThrow().insertNewAfter(t,e);if(o(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="",e){return l(new m(t,e))}function O(t){return t instanceof m}const k=n("TOGGLE_LINK_COMMAND");function N(t,e){if("element"===t.type){const r=t.getNode();o(r)||function(t,...e){const r=new URL("https://lexical.dev/docs/error"),n=new URLSearchParams;n.append("code",t);for(const t of e)n.append("v",t);throw r.search=n.toString(),Error(`Minified Lexical error #${t}; visit ${r.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+e]||null}return null}function v(t,e={}){const{target:n,title:i}=e,l=void 0===e.rel?"noreferrer":e.rel,h=u();if(null===h||!s(h)&&!a(h))return;if(a(h)){const e=h.getNodes();if(0===e.length)return;return void e.forEach((e=>{if(null===t){const t=r(e,(t=>!O(t)&&p(t)));t&&(t.insertBefore(e),0===t.getChildren().length&&t.remove())}else{const i=r(e,(t=>!O(t)&&p(t)));if(i)i.setURL(t),void 0!==n&&i.setTarget(n),void 0!==l&&i.setRel(l);else{const r=d(t,{rel:l,target:n});e.insertBefore(r),r.append(e)}}}))}const g=h.extract();if(null===t)return void g.forEach((t=>{const e=r(t,(t=>!O(t)&&p(t)));if(e){const t=e.getChildren();for(let r=0;r<t.length;r++)e.insertBefore(t[r]);e.remove()}}));const f=new Set,m=e=>{f.has(e.getKey())||(f.add(e.getKey()),e.setURL(t),void 0!==n&&e.setTarget(n),void 0!==l&&e.setRel(l),void 0!==i&&e.setTitle(i))};if(1===g.length){const t=x(g[0],p);if(null!==t)return m(t)}!function(t){const e=u();if(!s(e))return t();const r=c(e),n=r.isBackward(),i=N(r.anchor,n?-1:0),l=N(r.focus,n?0:-1),o=t();if(i||l){const t=u();if(s(t)){const e=t.clone();if(i){const t=i.getParent();t&&e.anchor.set(t.getKey(),i.getIndexWithinParent()+(n?1:0),"element")}if(l){const t=l.getParent();t&&e.focus.set(t.getKey(),l.getIndexWithinParent()+(n?0:1),"element")}_(c(e))}}}((()=>{let e=null;for(const r of g){if(!r.isAttached())continue;const s=x(r,p);if(s){m(s);continue}if(o(r)){if(!r.isInline())continue;if(p(r)){if(!(O(r)||null!==e&&e.getParentOrThrow().isParentOf(r))){m(r),e=r;continue}for(const t of r.getChildren())r.insertBefore(t);r.remove();continue}}const u=r.getPreviousSibling();p(u)&&u.is(e)?u.append(r):(e=d(t,{rel:l,target:n,title:i}),r.insertAfter(e),e.append(r))}}))}const T=v;function x(t,e){let r=t;for(;null!==r&&null!==r.getParent()&&!e(r);)r=r.getParentOrThrow();return e(r)?r:null}const L=/^\+?[0-9\s()-]{5,}$/;function S(t){return t.match(/^[a-z][a-z0-9+.-]*:/i)||t.match(/^[/#.]/)?t:t.includes("@")?`mailto:${t}`:L.test(t)?`tel:${t}`:`https://${t}`}export{U as $createAutoLinkNode,d as $createLinkNode,O as $isAutoLinkNode,p as $isLinkNode,v as $toggleLink,m as AutoLinkNode,g as LinkNode,k as TOGGLE_LINK_COMMAND,S as formatUrl,T as toggleLink};
|