@lexical/link 0.44.1-nightly.20260519.0 → 0.45.1-dev.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,67 @@
1
+ /**
2
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
3
+ *
4
+ * This source code is licensed under the MIT license found in the
5
+ * LICENSE file in the root directory of this source tree.
6
+ *
7
+ */
8
+
9
+ import {
10
+ $distributeInlineWrapper,
11
+ defineImportRule,
12
+ DOMImportExtension,
13
+ sel,
14
+ } from '@lexical/html';
15
+ import {configExtension, defineExtension} from 'lexical';
16
+
17
+ import {LinkExtension} from './LexicalLinkExtension';
18
+ import {$createLinkNode} from './LexicalLinkNode';
19
+
20
+ const AnchorRule = defineImportRule({
21
+ $import: (ctx, el) => {
22
+ if (!el.textContent && el.children.length === 0) {
23
+ return [];
24
+ }
25
+ // Use no schema here: when the `<a>` contains block descendants
26
+ // (e.g. `<a><h1>x</h1><div>y</div></a>`), we want them lifted so each
27
+ // block becomes a sibling at the link's level, with the link wrapping
28
+ // its inline content. $distributeInlineWrapper handles both the
29
+ // common all-inline case (single LinkNode wrapping the run) and the
30
+ // mixed-block case (per-block recursion).
31
+ const href = el.getAttribute('href') || '';
32
+ const attrs = {
33
+ rel: el.getAttribute('rel'),
34
+ target: el.getAttribute('target'),
35
+ title: el.getAttribute('title'),
36
+ };
37
+ return $distributeInlineWrapper(ctx.$importChildren(el), () =>
38
+ $createLinkNode(href, attrs),
39
+ );
40
+ },
41
+ match: sel.tag('a'),
42
+ name: '@lexical/link/a',
43
+ });
44
+
45
+ /**
46
+ * Import rules for {@link LinkNode}.
47
+ *
48
+ * @experimental
49
+ */
50
+ export const LinkImportRules = [AnchorRule];
51
+
52
+ /**
53
+ * Bundles {@link LinkImportRules} together with the runtime
54
+ * {@link LinkExtension}. The application is expected to already have
55
+ * `CoreImportExtension` (or some equivalent) in its dependency graph —
56
+ * the core/text/paragraph/inline-format rules are a shared baseline,
57
+ * not something this leaf importer should re-declare.
58
+ *
59
+ * @experimental
60
+ */
61
+ export const LinkImportExtension = defineExtension({
62
+ dependencies: [
63
+ LinkExtension,
64
+ configExtension(DOMImportExtension, {rules: LinkImportRules}),
65
+ ],
66
+ name: '@lexical/link/Import',
67
+ });
package/src/index.ts ADDED
@@ -0,0 +1,43 @@
1
+ /**
2
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
3
+ *
4
+ * This source code is licensed under the MIT license found in the
5
+ * LICENSE file in the root directory of this source tree.
6
+ *
7
+ */
8
+
9
+ import {$toggleLink} from './LexicalLinkNode';
10
+
11
+ export {
12
+ type ClickableLinkConfig,
13
+ ClickableLinkExtension,
14
+ registerClickableLink,
15
+ } from './ClickableLinkExtension';
16
+ export {
17
+ type AutoLinkConfig,
18
+ AutoLinkExtension,
19
+ type ChangeHandler,
20
+ createLinkMatcherWithRegExp,
21
+ type LinkMatcher,
22
+ registerAutoLink,
23
+ } from './LexicalAutoLinkExtension';
24
+ export {LinkExtension, registerLink} from './LexicalLinkExtension';
25
+ export {
26
+ $createAutoLinkNode,
27
+ $createLinkNode,
28
+ $isAutoLinkNode,
29
+ $isLinkNode,
30
+ $toggleLink,
31
+ type AutoLinkAttributes,
32
+ AutoLinkNode,
33
+ formatUrl,
34
+ type LinkAttributes,
35
+ LinkNode,
36
+ type SerializedAutoLinkNode,
37
+ type SerializedLinkNode,
38
+ TOGGLE_LINK_COMMAND,
39
+ } from './LexicalLinkNode';
40
+ export {LinkImportExtension, LinkImportRules} from './LinkImportExtension';
41
+
42
+ /** @deprecated renamed to {@link $toggleLink} by @lexical/eslint-plugin rules-of-lexical */
43
+ export const toggleLink = $toggleLink;
@@ -1,9 +0,0 @@
1
- /**
2
- * Copyright (c) Meta Platforms, Inc. and affiliates.
3
- *
4
- * This source code is licensed under the MIT license found in the
5
- * LICENSE file in the root directory of this source tree.
6
- *
7
- */
8
-
9
- "use strict";var e=require("@lexical/utils"),t=require("lexical"),n=require("@lexical/extension");const r=new Set(["http:","https:","mailto:","sms:","tel:"]);class i extends t.ElementNode{__url;__target;__rel;__title;static getType(){return"link"}static clone(e){return new i(e.__url,{rel:e.__rel,target:e.__target,title:e.__title},e.__key)}constructor(e="",t={},n){super(n);const{target:r=null,rel:i=null,title:s=null}=t;this.__url=e,this.__target=r,this.__rel=i,this.__title=s}afterCloneFrom(e){super.afterCloneFrom(e),this.__url=e.__url,this.__rel=e.__rel,this.__target=e.__target,this.__title=e.__title}createDOM(t){const n=document.createElement("a");return this.updateLinkDOM(null,n,t),e.addClassNamesToElement(n,t.theme.link),n}updateLinkDOM(t,n,r){if(e.isHTMLAnchorElement(n)){t&&t.__url===this.__url||(n.href=this.sanitizeUrl(this.__url));for(const e of["target","rel","title"]){const r=`__${e}`,i=this[r];t&&t[r]===i||(i?n[e]=i:n.removeAttribute(e))}}}updateDOM(e,t,n){return this.updateLinkDOM(e,t,n),!1}static importDOM(){return{a:e=>({conversion:a,priority:1})}}static importJSON(e){return u().updateFromJSON(e)}updateFromJSON(e){return super.updateFromJSON(e).setURL(e.url).setRel(e.rel||null).setTarget(e.target||null).setTitle(e.title||null)}sanitizeUrl(e){e=x(e);try{const t=new URL(x(e));if(!r.has(t.protocol))return"about:blank"}catch(t){return e}return e}exportJSON(){return{...super.exportJSON(),rel:this.getRel(),target:this.getTarget(),title:this.getTitle(),url:this.getURL()}}getURL(){return this.getLatest().__url}setURL(e){const t=this.getWritable();return t.__url=e,t}getTarget(){return this.getLatest().__target}setTarget(e){const t=this.getWritable();return t.__target=e,t}getRel(){return this.getLatest().__rel}setRel(e){const t=this.getWritable();return t.__rel=e,t}getTitle(){return this.getLatest().__title}setTitle(e){const t=this.getWritable();return t.__title=e,t}insertNewAfter(e,n=!0){const r=t.$copyNode(this);return this.insertAfter(r,n),r}canInsertTextBefore(){return!1}canInsertTextAfter(){return!1}canBeEmpty(){return!1}isInline(){return!0}extractWithChild(e,n,r){if(!t.$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://")}shouldMergeAdjacentLink(e){return this.getType()===e.getType()&&this.__url===e.__url&&this.__target===e.__target&&this.__rel===e.__rel&&this.__title===e.__title}}function s(e){const n=t.$caretFromPoint(e,"next");return[n,n.getFlipped()]}function o(e,n){for(const r of n)if(r.origin.isAttached()){const n=t.$normalizeCaret(r);return void t.$setPointFromCaret(e,n)}}function l(n){const r=t.$getSelection();let i=null,l=null;function a(){t.$isRangeSelection(r)&&(o(r.anchor,i),o(r.focus,l),t.$normalizeSelection__EXPERIMENTAL(r))}t.$isRangeSelection(r)&&(i=s(r.anchor),l=s(r.focus));let u=!1;for(const r of t.$getChildCaret(n,"next")){const i=r.origin;if(t.$isElementNode(i)&&!i.isInline()){const s=i.getChildren();if(s.length>0){const e=t.$copyNode(n);e.append(...s),i.append(e),u=!0}e.$insertNodeToNearestRootAtCaret(i,t.$rewindSiblingCaret(r),{$shouldSplit:()=>!1})}}function g(e,n,r){const[i,s]=e,o=e=>t.$isSiblingCaret(e)&&e.origin.is(n);if(!o(i)&&!o(s))return e;const l=t.$normalizeCaret(t.$getChildCaret(r,"next"));return[l,l.getFlipped()]}if(n.isAttached()){const e=n.getPreviousSibling();if(c(e)&&e.shouldMergeAdjacentLink(n))return i&&(i=g(i,e,n)),l&&(l=g(l,e,n)),e.append(...n.getChildren()),n.remove(),void a();const t=n.getNextSibling();c(t)&&n.shouldMergeAdjacentLink(t)&&(i&&(i=g(i,n,t)),l&&(l=g(l,n,t)),n.append(...t.getChildren()),t.remove(),u=!0)}if(u){if(!n.canBeEmpty()&&n.isEmpty()){const e=n.getParent();n.remove(),e&&e.isEmpty()&&e.remove()}a()}}function a(t){let n=null;if(e.isHTMLAnchorElement(t)){const e=t.textContent;(null!==e&&""!==e||t.children.length>0)&&(n=u(t.getAttribute("href")||"",{rel:t.getAttribute("rel"),target:t.getAttribute("target"),title:t.getAttribute("title")}))}return{node:n}}function u(e="",n){return t.$applyNodeReplacement(new i(e,n))}function c(e){return e instanceof i}class g extends i{__isUnlinked;constructor(e="",t={},n){super(e,t,n),this.__isUnlinked=void 0!==t.isUnlinked&&null!==t.isUnlinked&&t.isUnlinked}afterCloneFrom(e){super.afterCloneFrom(e),this.__isUnlinked=e.__isUnlinked}static getType(){return"autolink"}static clone(e){return new g(e.__url,{isUnlinked:e.__isUnlinked,rel:e.__rel,target:e.__target,title:e.__title},e.__key)}shouldMergeAdjacentLink(e){return!1}getIsUnlinked(){return this.__isUnlinked}setIsUnlinked(e){const t=this.getWritable();return t.__isUnlinked=e,t}createDOM(e){return this.__isUnlinked?document.createElement("span"):super.createDOM(e)}updateDOM(e,t,n){return super.updateDOM(e,t,n)||e.__isUnlinked!==this.__isUnlinked}static importJSON(e){return d().updateFromJSON(e)}updateFromJSON(e){return super.updateFromJSON(e).setIsUnlinked(e.isUnlinked||!1)}static importDOM(){return null}exportJSON(){return{...super.exportJSON(),isUnlinked:this.__isUnlinked}}insertNewAfter(e,t=!0){const n=d(this.__url,{isUnlinked:this.__isUnlinked,rel:this.__rel,target:this.__target,title:this.__title});return this.insertAfter(n,t),n}}function d(e="",n){return t.$applyNodeReplacement(new g(e,n))}function f(e){return e instanceof g}const h=t.createCommand("TOGGLE_LINK_COMMAND");function p(e,n){if("element"===e.type){const r=e.getNode();t.$isElementNode(r)||function(e,...t){const n=new URL("https://lexical.dev/docs/error"),r=new URLSearchParams;r.append("code",e);for(const e of t)r.append("v",e);throw n.search=r.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.`)}(252);return r.getChildren()[e.offset+n]||null}return null}function _(n,r={}){let i;if(n&&"object"==typeof n){const{url:e,...t}=n;i=e,r={...t,...r}}else i=n;const{target:s,title:o}=r,l=void 0===r.rel?"noreferrer":r.rel,a=t.$getSelection();if(null===a||!t.$isRangeSelection(a)&&!t.$isNodeSelection(a))return;if(t.$isNodeSelection(a)){const t=a.getNodes();if(0===t.length)return;return void t.forEach(t=>{if(null===i){const n=e.$findMatchingParent(t,e=>!f(e)&&c(e));n&&(n.insertBefore(t),0===n.getChildren().length&&n.remove())}else{const n=e.$findMatchingParent(t,e=>!f(e)&&c(e));if(n)n.setURL(i),void 0!==s&&n.setTarget(s),void 0!==l&&n.setRel(l);else{const e=u(i,{rel:l,target:s});t.insertBefore(e),e.append(t)}}})}if(a.isCollapsed()&&null===i)for(const t of a.getNodes()){const n=e.$findMatchingParent(t,e=>!f(e)&&c(e));return void(null!==n&&(n.getParentOrThrow().splice(n.getIndexWithinParent(),0,n.getChildren()),n.remove()))}const g=a.extract();if(null===i){const n=new Set;return void g.forEach(r=>{const i=e.$findMatchingParent(r,e=>!f(e)&&c(e));if(null!==i){const e=i.getKey();if(n.has(e))return;!function(e,n){const r=new Set(n.filter(t=>e.isParentOf(t)).map(e=>e.getKey())),i=e.getChildren(),s=i=>r.has(i.getKey())||t.$isElementNode(i)&&n.some(t=>e.isParentOf(t)&&i.isParentOf(t)),o=i.filter(s);if(o.length===i.length)return i.forEach(t=>e.insertBefore(t)),void e.remove();const l=i.findIndex(s),a=i.findLastIndex(s),u=0===l,c=a===i.length-1;if(u)o.forEach(t=>e.insertBefore(t));else if(c)for(let t=o.length-1;t>=0;t--)e.insertAfter(o[t]);else{for(let t=o.length-1;t>=0;t--)e.insertAfter(o[t]);const n=i.slice(a+1);if(n.length>0){const r=t.$copyNode(e);o[o.length-1].insertAfter(r),n.forEach(e=>r.append(e))}}}(i,g),n.add(e)}})}const d=new Set,h=e=>{d.has(e.getKey())||(d.add(e.getKey()),e.setURL(i),void 0!==s&&e.setTarget(s),void 0!==l&&e.setRel(l),void 0!==o&&e.setTitle(o))};if(1===g.length){const t=g[0],n=e.$findMatchingParent(t,c);if(null!==n)return h(n)}!function(e){const n=t.$getSelection();if(!t.$isRangeSelection(n))return e();const r=t.$normalizeSelection__EXPERIMENTAL(n),i=r.isBackward(),s=p(r.anchor,i?-1:0),o=p(r.focus,i?0:-1),l=e();if(s||o){const e=t.$getSelection();if(t.$isRangeSelection(e)){const n=e.clone();if(s){const e=s.getParent();e&&n.anchor.set(e.getKey(),s.getIndexWithinParent()+(i?1:0),"element")}if(o){const e=o.getParent();e&&n.focus.set(e.getKey(),o.getIndexWithinParent()+(i?0:1),"element")}t.$setSelection(t.$normalizeSelection__EXPERIMENTAL(n))}}}(()=>{let n=null;for(const r of g){if(!r.isAttached())continue;const a=e.$findMatchingParent(r,c);if(a){h(a);continue}if(t.$isElementNode(r)){if(!r.isInline())continue;if(c(r)){if(!(f(r)||null!==n&&n.getParentOrThrow().isParentOf(r))){h(r),n=r;continue}for(const e of r.getChildren())r.insertBefore(e);r.remove();continue}}const g=r.getPreviousSibling();c(g)&&g.is(n)?g.append(r):(n=u(i,{rel:l,target:s,title:o}),r.insertAfter(n),n.append(r))}})}const m=/^\+?[0-9\s()-]{5,}$/;function x(e){return e.match(/^[a-z][a-z0-9+.-]*:/i)||e.match(/^[/#.]/)?e:e.includes("@")?`mailto:${e}`:m.test(e)?`tel:${e}`:`https://${e}`}const N={attributes:void 0,validateUrl:void 0};function k(r,s){return e.mergeRegister(r.registerNodeTransform(i,l),r.registerCommand(h,e=>{const t=s.validateUrl.peek(),n=s.attributes.peek();if(null===e)return _(null),!0;if("string"==typeof e)return!(void 0!==t&&!t(e))&&(_(e,n),!0);{const{url:t,target:r,rel:i,title:s}=e;return _(t,{...n,rel:i,target:r,title:s}),!0}},t.COMMAND_PRIORITY_EDITOR),n.effect(()=>{const n=s.validateUrl.value;if(!n)return;const i=s.attributes.value;return r.registerCommand(t.PASTE_COMMAND,s=>{const o=t.$getSelection();if(!t.$isRangeSelection(o)||o.isCollapsed()||!e.objectKlassEquals(s,ClipboardEvent))return!1;if(null===s.clipboardData)return!1;const l=s.clipboardData.getData("text");return!!n(l)&&(!o.getNodes().some(e=>t.$isElementNode(e))&&(r.dispatchCommand(h,{...i,url:l}),s.preventDefault(),!0))},t.COMMAND_PRIORITY_LOW)}))}const $=t.defineExtension({build:(e,t,r)=>n.namedSignals(t),config:N,mergeConfig(e,n){const r=t.shallowMergeConfig(e,n);return e.attributes&&(r.attributes=t.shallowMergeConfig(e.attributes,r.attributes)),r},name:"@lexical/link/Link",nodes:()=>[i],register:(e,t,n)=>k(e,n.getOutput())});function S(n,r,i={}){const s=i=>{const s=i.target;if(!t.isDOMNode(s))return;const o=t.getNearestEditorFromDOMNode(s);if(null===o)return;let l=null,a=null;if(o.update(()=>{const n=t.$getNearestNodeFromDOMNode(s);if(null!==n){const i=e.$findMatchingParent(n,t.$isElementNode);if(!r.disabled.peek())if(c(i))l=i.sanitizeUrl(i.getURL()),a=i.getTarget();else{const t=function(e,t){let n=e;for(;null!=n;){if(t(n))return n;n=n.parentNode}return null}(s,e.isHTMLAnchorElement);null!==t&&(l=t.href,a=t.target)}}}),null===l||""===l)return;const u=n.getEditorState().read(t.$getSelection,{editor:n});if(t.$isRangeSelection(u)&&!u.isCollapsed())return void i.preventDefault();const g="auxclick"===i.type&&1===i.button;window.open(l,r.newTab.peek()||g||i.metaKey||i.ctrlKey||"_blank"===a?"_blank":"_self"),i.preventDefault()},o=e=>{1===e.button&&s(e)};return n.registerRootListener(e=>{if(e)return e.addEventListener("click",s,i),e.addEventListener("mouseup",o,i),()=>{e.removeEventListener("click",s),e.removeEventListener("mouseup",o)}})}const T=t.defineExtension({build:(e,t,r)=>n.namedSignals(t),config:t.safeCast({disabled:!1,newTab:!1}),dependencies:[$],name:"@lexical/link/ClickableLink",register:(e,t,n)=>S(e,n.getOutput())});function L(e,t){for(let n=0;n<t.length;n++){const r=t[n](e);if(r)return r}return null}const C=/[.,;\s]/;function b(e,t){return t.test(e)}function v(e,t){return b(e[e.length-1],t)}function R(e,t){return b(e[0],t)}function U(e,n){let r=e.getPreviousSibling();return t.$isElementNode(r)&&(r=r.getLastDescendant()),null===r||t.$isLineBreakNode(r)||t.$isTextNode(r)&&v(r.getTextContent(),n)}function E(e,n){let r=e.getNextSibling();return t.$isElementNode(r)&&(r=r.getFirstDescendant()),null===r||t.$isLineBreakNode(r)||t.$isTextNode(r)&&R(r.getTextContent(),n)}function O(e,t,n,r,i){if(!(e>0?b(r[e-1],n):U(i[0],n)))return!1;return t<r.length?b(r[t],n):E(i[i.length-1],n)}function P(e,t,n){const r=[],i=[],s=[];let o=0,l=0;const a=[...e];for(;a.length>0;){const e=a[0],u=e.getTextContent().length,c=l;l+u<=t?(r.push(e),o+=u):c>=n?s.push(e):i.push(e),l+=u,a.shift()}return[o,r,i,s]}function M(e,n,r,i){const s=d(i.url,i.attributes);if(1===e.length){let o,l=e[0];0===n?[o,l]=l.splitText(r):[,o,l]=l.splitText(n,r);const a=t.$createTextNode(i.text);return a.setFormat(o.getFormat()),a.setDetail(o.getDetail()),a.setStyle(o.getStyle()),s.append(a),o.replace(s),l}if(e.length>1){const i=e[0];let o,l=i.getTextContent().length;0===n?o=i:[,o]=i.splitText(n);const a=[];let u;for(let t=1;t<e.length;t++){const n=e[t],i=n.getTextContent().length,s=l;if(s<r)if(l+i<=r)a.push(n);else{const[e,t]=n.splitText(r-s);a.push(e),u=t}l+=i}const c=t.$getSelection(),g=c?c.getNodes().find(t.$isTextNode):void 0,d=t.$createTextNode(o.getTextContent());return d.setFormat(o.getFormat()),d.setDetail(o.getDetail()),d.setStyle(o.getStyle()),s.append(d,...a),g&&g===o&&(t.$isRangeSelection(c)?d.select(c.anchor.offset,c.focus.offset):t.$isNodeSelection(c)&&d.select(0,d.getTextContent().length)),o.replace(s),u}}function A(e,n,r,i){const s=e.getChildren(),o=s.length;for(let n=0;n<o;n++){const i=s[n];if(!t.$isTextNode(i)||!i.isSimpleText())return y(e),void r(null,e.getURL())}const l=e.getTextContent(),a=L(l,n);if(null===a||a.text!==l)return y(e),void r(null,e.getURL());if(!U(e,i)||!E(e,i))return y(e),void r(null,e.getURL());const u=e.getURL();if(u!==a.url&&(e.setURL(a.url),r(a.url,u)),a.attributes){const t=e.getRel();t!==a.attributes.rel&&(e.setRel(a.attributes.rel||null),r(a.attributes.rel||null,t));const n=e.getTarget();n!==a.attributes.target&&(e.setTarget(a.attributes.target||null),r(a.attributes.target||null,n))}}function y(e){const t=e.getChildren();for(let n=t.length-1;n>=0;n--)e.insertAfter(t[n]);return e.remove(),t.map(e=>e.getLatest())}const I={changeHandlers:[],excludeParents:[],matchers:[],separatorRegex:C};function D(n,r=I){const{matchers:i,changeHandlers:s,excludeParents:o,separatorRegex:l=C}=r,a=(e,t)=>{for(const n of s)n(e,t)};return e.mergeRegister(n.registerNodeTransform(t.TextNode,e=>{const n=e.getParentOrThrow(),r=e.getPreviousSibling();if(f(n))A(n,i,a,l);else if(!c(n)&&!o.some(e=>e(n))){if(e.isSimpleText()&&(R(e.getTextContent(),l)||!f(r))){const n=function(e){const n=[e];let r=e.getNextSibling();for(;null!==r&&t.$isTextNode(r)&&r.isSimpleText()&&(n.push(r),!/[\s]/.test(r.getTextContent()));)r=r.getNextSibling();return n}(e);!function(e,t,n,r){for(const t of e){const e=t.getParent();if(f(e)&&!e.getIsUnlinked())return}let i=[...e];const s=i.map(e=>e.getTextContent()).join("");let o,l=s,a=0;for(;(o=L(l,t))&&null!==o;){const e=o.index,t=e+o.length;if(O(a+e,a+t,r,s,i)){const[r,,s,u]=P(i,a+e,a+t);let c=!1;for(const e of s){const t=e.getParent();if(f(t)&&!t.getIsUnlinked()){c=!0;break}}if(c){a+=t,l=l.substring(t);continue}const g=M(s,a+e-r,a+t-r,o);i=g?[g,...u]:u,n(o.url,null),a=0}else a+=t;l=l.substring(t)}}(n,i,a,l)}!function(e,t,n,r){const i=e.getParent(),s=e.getPreviousSibling(),o=e.getNextSibling(),l=e.getTextContent();if(!f(i)||i.getIsUnlinked()){if(f(s)&&!s.getIsUnlinked()&&s.is(e.getPreviousSibling())&&e.getParent()===s.getParent()){if(!R(l,r))return y(s),void n(null,s.getURL());if(a=l,s.isEmailURI()?/^\.[a-zA-Z]{2,}/.test(a):/^\.[a-zA-Z0-9]{1,}/.test(a)){const i=s.getTextContent()+l,o=L(i,t);null!==o&&o.text===i&&(s.append(e),A(s,t,n,r),n(null,s.getURL()))}}var a;!f(o)||o.getIsUnlinked()||v(l,r)||o.is(e.getNextSibling())&&e.getParent()===o.getParent()&&(y(o),n(null,o.getURL()))}}(e,i,a,l)}}),n.registerCommand(h,e=>{const n=t.$getSelection();if(null!==e||!t.$isRangeSelection(n))return!1;return n.extract().forEach(e=>{const t=e.getParent();f(t)&&(t.setIsUnlinked(!t.getIsUnlinked()),t.markDirty())}),!1},t.COMMAND_PRIORITY_LOW))}const w=t.defineExtension({config:I,dependencies:[$],mergeConfig(e,n){const r=t.shallowMergeConfig(e,n);for(const t of["matchers","changeHandlers","excludeParents"]){const i=n[t];Array.isArray(i)&&(r[t]=[...e[t],...i])}return r},name:"@lexical/link/AutoLink",nodes:[g],register:D}),F=_;exports.$createAutoLinkNode=d,exports.$createLinkNode=u,exports.$isAutoLinkNode=f,exports.$isLinkNode=c,exports.$toggleLink=_,exports.AutoLinkExtension=w,exports.AutoLinkNode=g,exports.ClickableLinkExtension=T,exports.LinkExtension=$,exports.LinkNode=i,exports.TOGGLE_LINK_COMMAND=h,exports.createLinkMatcherWithRegExp=function(e,t=e=>e){return n=>{const r=e.exec(n);return null===r?null:{index:r.index,length:r[0].length,text:r[0],url:t(r[0])}}},exports.formatUrl=x,exports.registerAutoLink=D,exports.registerClickableLink=S,exports.registerLink=k,exports.toggleLink=F;
@@ -1,9 +0,0 @@
1
- /**
2
- * Copyright (c) Meta Platforms, Inc. and affiliates.
3
- *
4
- * This source code is licensed under the MIT license found in the
5
- * LICENSE file in the root directory of this source tree.
6
- *
7
- */
8
-
9
- import{addClassNamesToElement as t,isHTMLAnchorElement as e,$findMatchingParent as n,$insertNodeToNearestRootAtCaret as r,mergeRegister as i,objectKlassEquals as s}from"@lexical/utils";import{ElementNode as l,$copyNode as o,$isRangeSelection as u,$applyNodeReplacement as a,createCommand as c,$getSelection as g,$isNodeSelection as f,$normalizeSelection__EXPERIMENTAL as d,$setSelection as h,$getChildCaret as _,$isElementNode as p,$rewindSiblingCaret as m,$caretFromPoint as x,$normalizeCaret as k,$isSiblingCaret as v,$setPointFromCaret as b,defineExtension as U,shallowMergeConfig as T,COMMAND_PRIORITY_EDITOR as L,PASTE_COMMAND as C,COMMAND_PRIORITY_LOW as S,safeCast as O,isDOMNode as P,getNearestEditorFromDOMNode as y,$getNearestNodeFromDOMNode as N,TextNode as R,$isTextNode as A,$isLineBreakNode as D,$createTextNode as I}from"lexical";import{namedSignals as w,effect as E}from"@lexical/extension";const M=new Set(["http:","https:","mailto:","sms:","tel:"]);class F extends l{__url;__target;__rel;__title;static getType(){return"link"}static clone(t){return new F(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}afterCloneFrom(t){super.afterCloneFrom(t),this.__url=t.__url,this.__rel=t.__rel,this.__target=t.__target,this.__title=t.__title}createDOM(e){const n=document.createElement("a");return this.updateLinkDOM(null,n,e),t(n,e.theme.link),n}updateLinkDOM(t,n,r){if(e(n)){t&&t.__url===this.__url||(n.href=this.sanitizeUrl(this.__url));for(const e of["target","rel","title"]){const r=`__${e}`,i=this[r];t&&t[r]===i||(i?n[e]=i:n.removeAttribute(e))}}}updateDOM(t,e,n){return this.updateLinkDOM(t,e,n),!1}static importDOM(){return{a:t=>({conversion:B,priority:1})}}static importJSON(t){return $().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=X(t);try{const e=new URL(X(t));if(!M.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=o(this);return this.insertAfter(n,e),n}canInsertTextBefore(){return!1}canInsertTextAfter(){return!1}canBeEmpty(){return!1}isInline(){return!0}extractWithChild(t,e,n){if(!u(e))return!1;const r=e.anchor.getNode(),i=e.focus.getNode();return this.isParentOf(r)&&this.isParentOf(i)&&e.getTextContent().length>0}isEmailURI(){return this.__url.startsWith("mailto:")}isWebSiteURI(){return this.__url.startsWith("https://")||this.__url.startsWith("http://")}shouldMergeAdjacentLink(t){return this.getType()===t.getType()&&this.__url===t.__url&&this.__target===t.__target&&this.__rel===t.__rel&&this.__title===t.__title}}function W(t){const e=x(t,"next");return[e,e.getFlipped()]}function J(t,e){for(const n of e)if(n.origin.isAttached()){const e=k(n);return void b(t,e)}}function K(t){const e=g();let n=null,i=null;function s(){u(e)&&(J(e.anchor,n),J(e.focus,i),d(e))}u(e)&&(n=W(e.anchor),i=W(e.focus));let l=!1;for(const e of _(t,"next")){const n=e.origin;if(p(n)&&!n.isInline()){const i=n.getChildren();if(i.length>0){const e=o(t);e.append(...i),n.append(e),l=!0}r(n,m(e),{$shouldSplit:()=>!1})}}function a(t,e,n){const[r,i]=t,s=t=>v(t)&&t.origin.is(e);if(!s(r)&&!s(i))return t;const l=k(_(n,"next"));return[l,l.getFlipped()]}if(t.isAttached()){const e=t.getPreviousSibling();if(z(e)&&e.shouldMergeAdjacentLink(t))return n&&(n=a(n,e,t)),i&&(i=a(i,e,t)),e.append(...t.getChildren()),t.remove(),void s();const r=t.getNextSibling();z(r)&&t.shouldMergeAdjacentLink(r)&&(n&&(n=a(n,t,r)),i&&(i=a(i,t,r)),t.append(...r.getChildren()),r.remove(),l=!0)}if(l){if(!t.canBeEmpty()&&t.isEmpty()){const e=t.getParent();t.remove(),e&&e.isEmpty()&&e.remove()}s()}}function B(t){let n=null;if(e(t)){const e=t.textContent;(null!==e&&""!==e||t.children.length>0)&&(n=$(t.getAttribute("href")||"",{rel:t.getAttribute("rel"),target:t.getAttribute("target"),title:t.getAttribute("title")}))}return{node:n}}function $(t="",e){return a(new F(t,e))}function z(t){return t instanceof F}class j extends F{__isUnlinked;constructor(t="",e={},n){super(t,e,n),this.__isUnlinked=void 0!==e.isUnlinked&&null!==e.isUnlinked&&e.isUnlinked}afterCloneFrom(t){super.afterCloneFrom(t),this.__isUnlinked=t.__isUnlinked}static getType(){return"autolink"}static clone(t){return new j(t.__url,{isUnlinked:t.__isUnlinked,rel:t.__rel,target:t.__target,title:t.__title},t.__key)}shouldMergeAdjacentLink(t){return!1}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 H().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 n=H(this.__url,{isUnlinked:this.__isUnlinked,rel:this.__rel,target:this.__target,title:this.__title});return this.insertAfter(n,e),n}}function H(t="",e){return a(new j(t,e))}function G(t){return t instanceof j}const Z=c("TOGGLE_LINK_COMMAND");function q(t,e){if("element"===t.type){const n=t.getNode();p(n)||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 n.getChildren()[t.offset+e]||null}return null}function Q(t,e={}){let r;if(t&&"object"==typeof t){const{url:n,...i}=t;r=n,e={...i,...e}}else r=t;const{target:i,title:s}=e,l=void 0===e.rel?"noreferrer":e.rel,a=g();if(null===a||!u(a)&&!f(a))return;if(f(a)){const t=a.getNodes();if(0===t.length)return;return void t.forEach(t=>{if(null===r){const e=n(t,t=>!G(t)&&z(t));e&&(e.insertBefore(t),0===e.getChildren().length&&e.remove())}else{const e=n(t,t=>!G(t)&&z(t));if(e)e.setURL(r),void 0!==i&&e.setTarget(i),void 0!==l&&e.setRel(l);else{const e=$(r,{rel:l,target:i});t.insertBefore(e),e.append(t)}}})}if(a.isCollapsed()&&null===r)for(const t of a.getNodes()){const e=n(t,t=>!G(t)&&z(t));return void(null!==e&&(e.getParentOrThrow().splice(e.getIndexWithinParent(),0,e.getChildren()),e.remove()))}const c=a.extract();if(null===r){const t=new Set;return void c.forEach(e=>{const r=n(e,t=>!G(t)&&z(t));if(null!==r){const e=r.getKey();if(t.has(e))return;!function(t,e){const n=new Set(e.filter(e=>t.isParentOf(e)).map(t=>t.getKey())),r=t.getChildren(),i=r=>n.has(r.getKey())||p(r)&&e.some(e=>t.isParentOf(e)&&r.isParentOf(e)),s=r.filter(i);if(s.length===r.length)return r.forEach(e=>t.insertBefore(e)),void t.remove();const l=r.findIndex(i),u=r.findLastIndex(i),a=0===l,c=u===r.length-1;if(a)s.forEach(e=>t.insertBefore(e));else if(c)for(let e=s.length-1;e>=0;e--)t.insertAfter(s[e]);else{for(let e=s.length-1;e>=0;e--)t.insertAfter(s[e]);const e=r.slice(u+1);if(e.length>0){const n=o(t);s[s.length-1].insertAfter(n),e.forEach(t=>n.append(t))}}}(r,c),t.add(e)}})}const _=new Set,m=t=>{_.has(t.getKey())||(_.add(t.getKey()),t.setURL(r),void 0!==i&&t.setTarget(i),void 0!==l&&t.setRel(l),void 0!==s&&t.setTitle(s))};if(1===c.length){const t=c[0],e=n(t,z);if(null!==e)return m(e)}!function(t){const e=g();if(!u(e))return t();const n=d(e),r=n.isBackward(),i=q(n.anchor,r?-1:0),s=q(n.focus,r?0:-1),l=t();if(i||s){const t=g();if(u(t)){const e=t.clone();if(i){const t=i.getParent();t&&e.anchor.set(t.getKey(),i.getIndexWithinParent()+(r?1:0),"element")}if(s){const t=s.getParent();t&&e.focus.set(t.getKey(),s.getIndexWithinParent()+(r?0:1),"element")}h(d(e))}}}(()=>{let t=null;for(const e of c){if(!e.isAttached())continue;const o=n(e,z);if(o){m(o);continue}if(p(e)){if(!e.isInline())continue;if(z(e)){if(!(G(e)||null!==t&&t.getParentOrThrow().isParentOf(e))){m(e),t=e;continue}for(const t of e.getChildren())e.insertBefore(t);e.remove();continue}}const u=e.getPreviousSibling();z(u)&&u.is(t)?u.append(e):(t=$(r,{rel:l,target:i,title:s}),e.insertAfter(t),t.append(e))}})}const V=/^\+?[0-9\s()-]{5,}$/;function X(t){return t.match(/^[a-z][a-z0-9+.-]*:/i)||t.match(/^[/#.]/)?t:t.includes("@")?`mailto:${t}`:V.test(t)?`tel:${t}`:`https://${t}`}function Y(t,e){return i(t.registerNodeTransform(F,K),t.registerCommand(Z,t=>{const n=e.validateUrl.peek(),r=e.attributes.peek();if(null===t)return Q(null),!0;if("string"==typeof t)return!(void 0!==n&&!n(t))&&(Q(t,r),!0);{const{url:e,target:n,rel:i,title:s}=t;return Q(e,{...r,rel:i,target:n,title:s}),!0}},L),E(()=>{const n=e.validateUrl.value;if(!n)return;const r=e.attributes.value;return t.registerCommand(C,e=>{const i=g();if(!u(i)||i.isCollapsed()||!s(e,ClipboardEvent))return!1;if(null===e.clipboardData)return!1;const l=e.clipboardData.getData("text");return!!n(l)&&(!i.getNodes().some(t=>p(t))&&(t.dispatchCommand(Z,{...r,url:l}),e.preventDefault(),!0))},S)}))}const tt=U({build:(t,e,n)=>w(e),config:{attributes:void 0,validateUrl:void 0},mergeConfig(t,e){const n=T(t,e);return t.attributes&&(n.attributes=T(t.attributes,n.attributes)),n},name:"@lexical/link/Link",nodes:()=>[F],register:(t,e,n)=>Y(t,n.getOutput())});function et(t,r,i={}){const s=i=>{const s=i.target;if(!P(s))return;const l=y(s);if(null===l)return;let o=null,a=null;if(l.update(()=>{const t=N(s);if(null!==t){const i=n(t,p);if(!r.disabled.peek())if(z(i))o=i.sanitizeUrl(i.getURL()),a=i.getTarget();else{const t=function(t,e){let n=t;for(;null!=n;){if(e(n))return n;n=n.parentNode}return null}(s,e);null!==t&&(o=t.href,a=t.target)}}}),null===o||""===o)return;const c=t.getEditorState().read(g,{editor:t});if(u(c)&&!c.isCollapsed())return void i.preventDefault();const f="auxclick"===i.type&&1===i.button;window.open(o,r.newTab.peek()||f||i.metaKey||i.ctrlKey||"_blank"===a?"_blank":"_self"),i.preventDefault()},l=t=>{1===t.button&&s(t)};return t.registerRootListener(t=>{if(t)return t.addEventListener("click",s,i),t.addEventListener("mouseup",l,i),()=>{t.removeEventListener("click",s),t.removeEventListener("mouseup",l)}})}const nt=U({build:(t,e,n)=>w(e),config:O({disabled:!1,newTab:!1}),dependencies:[tt],name:"@lexical/link/ClickableLink",register:(t,e,n)=>et(t,n.getOutput())});function rt(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 it(t,e){for(let n=0;n<e.length;n++){const r=e[n](t);if(r)return r}return null}const st=/[.,;\s]/;function lt(t,e){return e.test(t)}function ot(t,e){return lt(t[t.length-1],e)}function ut(t,e){return lt(t[0],e)}function at(t,e){let n=t.getPreviousSibling();return p(n)&&(n=n.getLastDescendant()),null===n||D(n)||A(n)&&ot(n.getTextContent(),e)}function ct(t,e){let n=t.getNextSibling();return p(n)&&(n=n.getFirstDescendant()),null===n||D(n)||A(n)&&ut(n.getTextContent(),e)}function gt(t,e,n,r,i){if(!(t>0?lt(r[t-1],n):at(i[0],n)))return!1;return e<r.length?lt(r[e],n):ct(i[i.length-1],n)}function ft(t,e,n){const r=[],i=[],s=[];let l=0,o=0;const u=[...t];for(;u.length>0;){const t=u[0],a=t.getTextContent().length,c=o;o+a<=e?(r.push(t),l+=a):c>=n?s.push(t):i.push(t),o+=a,u.shift()}return[l,r,i,s]}function dt(t,e,n,r){const i=H(r.url,r.attributes);if(1===t.length){let s,l=t[0];0===e?[s,l]=l.splitText(n):[,s,l]=l.splitText(e,n);const o=I(r.text);return o.setFormat(s.getFormat()),o.setDetail(s.getDetail()),o.setStyle(s.getStyle()),i.append(o),s.replace(i),l}if(t.length>1){const r=t[0];let s,l=r.getTextContent().length;0===e?s=r:[,s]=r.splitText(e);const o=[];let a;for(let e=1;e<t.length;e++){const r=t[e],i=r.getTextContent().length,s=l;if(s<n)if(l+i<=n)o.push(r);else{const[t,e]=r.splitText(n-s);o.push(t),a=e}l+=i}const c=g(),d=c?c.getNodes().find(A):void 0,h=I(s.getTextContent());return h.setFormat(s.getFormat()),h.setDetail(s.getDetail()),h.setStyle(s.getStyle()),i.append(h,...o),d&&d===s&&(u(c)?h.select(c.anchor.offset,c.focus.offset):f(c)&&h.select(0,h.getTextContent().length)),s.replace(i),a}}function ht(t,e,n,r){const i=t.getChildren(),s=i.length;for(let e=0;e<s;e++){const r=i[e];if(!A(r)||!r.isSimpleText())return _t(t),void n(null,t.getURL())}const l=t.getTextContent(),o=it(l,e);if(null===o||o.text!==l)return _t(t),void n(null,t.getURL());if(!at(t,r)||!ct(t,r))return _t(t),void n(null,t.getURL());const u=t.getURL();if(u!==o.url&&(t.setURL(o.url),n(o.url,u)),o.attributes){const e=t.getRel();e!==o.attributes.rel&&(t.setRel(o.attributes.rel||null),n(o.attributes.rel||null,e));const r=t.getTarget();r!==o.attributes.target&&(t.setTarget(o.attributes.target||null),n(o.attributes.target||null,r))}}function _t(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())}const pt={changeHandlers:[],excludeParents:[],matchers:[],separatorRegex:st};function mt(t,e=pt){const{matchers:n,changeHandlers:r,excludeParents:s,separatorRegex:l=st}=e,o=(t,e)=>{for(const n of r)n(t,e)};return i(t.registerNodeTransform(R,t=>{const e=t.getParentOrThrow(),r=t.getPreviousSibling();if(G(e))ht(e,n,o,l);else if(!z(e)&&!s.some(t=>t(e))){if(t.isSimpleText()&&(ut(t.getTextContent(),l)||!G(r))){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,r){for(const e of t){const t=e.getParent();if(G(t)&&!t.getIsUnlinked())return}let i=[...t];const s=i.map(t=>t.getTextContent()).join("");let l,o=s,u=0;for(;(l=it(o,e))&&null!==l;){const t=l.index,e=t+l.length;if(gt(u+t,u+e,r,s,i)){const[r,,s,a]=ft(i,u+t,u+e);let c=!1;for(const t of s){const e=t.getParent();if(G(e)&&!e.getIsUnlinked()){c=!0;break}}if(c){u+=e,o=o.substring(e);continue}const g=dt(s,u+t-r,u+e-r,l);i=g?[g,...a]:a,n(l.url,null),u=0}else u+=e;o=o.substring(e)}}(e,n,o,l)}!function(t,e,n,r){const i=t.getParent(),s=t.getPreviousSibling(),l=t.getNextSibling(),o=t.getTextContent();if(!G(i)||i.getIsUnlinked()){if(G(s)&&!s.getIsUnlinked()&&s.is(t.getPreviousSibling())&&t.getParent()===s.getParent()){if(!ut(o,r))return _t(s),void n(null,s.getURL());if(u=o,s.isEmailURI()?/^\.[a-zA-Z]{2,}/.test(u):/^\.[a-zA-Z0-9]{1,}/.test(u)){const i=s.getTextContent()+o,l=it(i,e);null!==l&&l.text===i&&(s.append(t),ht(s,e,n,r),n(null,s.getURL()))}}var u;!G(l)||l.getIsUnlinked()||ot(o,r)||l.is(t.getNextSibling())&&t.getParent()===l.getParent()&&(_t(l),n(null,l.getURL()))}}(t,n,o,l)}}),t.registerCommand(Z,t=>{const e=g();if(null!==t||!u(e))return!1;return e.extract().forEach(t=>{const e=t.getParent();G(e)&&(e.setIsUnlinked(!e.getIsUnlinked()),e.markDirty())}),!1},S))}const xt=U({config:pt,dependencies:[tt],mergeConfig(t,e){const n=T(t,e);for(const r of["matchers","changeHandlers","excludeParents"]){const i=e[r];Array.isArray(i)&&(n[r]=[...t[r],...i])}return n},name:"@lexical/link/AutoLink",nodes:[j],register:mt}),kt=Q;export{H as $createAutoLinkNode,$ as $createLinkNode,G as $isAutoLinkNode,z as $isLinkNode,Q as $toggleLink,xt as AutoLinkExtension,j as AutoLinkNode,nt as ClickableLinkExtension,tt as LinkExtension,F as LinkNode,Z as TOGGLE_LINK_COMMAND,rt as createLinkMatcherWithRegExp,X as formatUrl,mt as registerAutoLink,et as registerClickableLink,Y as registerLink,kt as toggleLink};
File without changes