@react-fabric/lexical 1.0.0-beta.6 → 1.0.0-beta.60

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.
Files changed (76) hide show
  1. package/{src/editor/Lexical.module.css → css/_lexical.css} +8 -4
  2. package/css/styles.css +24 -0
  3. package/dist/cjs/node_modules/@lexical/clipboard/LexicalClipboard.dev.js +62 -20
  4. package/dist/cjs/node_modules/@lexical/clipboard/LexicalClipboard.js +1 -1
  5. package/dist/cjs/node_modules/@lexical/clipboard/LexicalClipboard.prod.js +11 -11
  6. package/dist/cjs/node_modules/@lexical/code/LexicalCode.dev.js +643 -245
  7. package/dist/cjs/node_modules/@lexical/code/LexicalCode.js +1 -1
  8. package/dist/cjs/node_modules/@lexical/code/LexicalCode.prod.js +29 -22
  9. package/dist/cjs/node_modules/@lexical/html/LexicalHtml.dev.js +13 -6
  10. package/dist/cjs/node_modules/@lexical/html/LexicalHtml.js +1 -1
  11. package/dist/cjs/node_modules/@lexical/html/LexicalHtml.prod.js +3 -3
  12. package/dist/cjs/node_modules/@lexical/link/LexicalLink.dev.js +242 -141
  13. package/dist/cjs/node_modules/@lexical/link/LexicalLink.js +1 -1
  14. package/dist/cjs/node_modules/@lexical/link/LexicalLink.prod.js +11 -10
  15. package/dist/cjs/node_modules/@lexical/list/LexicalList.dev.js +594 -255
  16. package/dist/cjs/node_modules/@lexical/list/LexicalList.js +1 -1
  17. package/dist/cjs/node_modules/@lexical/list/LexicalList.prod.js +23 -16
  18. package/dist/cjs/node_modules/@lexical/rich-text/LexicalRichText.dev.js +102 -53
  19. package/dist/cjs/node_modules/@lexical/rich-text/LexicalRichText.js +1 -1
  20. package/dist/cjs/node_modules/@lexical/rich-text/LexicalRichText.prod.js +10 -10
  21. package/dist/cjs/node_modules/@lexical/selection/LexicalSelection.dev.js +275 -194
  22. package/dist/cjs/node_modules/@lexical/selection/LexicalSelection.js +5 -1
  23. package/dist/cjs/node_modules/@lexical/selection/LexicalSelection.prod.js +28 -18
  24. package/dist/cjs/node_modules/@lexical/table/LexicalTable.dev.js +2293 -878
  25. package/dist/cjs/node_modules/@lexical/table/LexicalTable.js +1 -1
  26. package/dist/cjs/node_modules/@lexical/table/LexicalTable.prod.js +53 -40
  27. package/dist/cjs/node_modules/@lexical/utils/LexicalUtils.dev.js +471 -193
  28. package/dist/cjs/node_modules/@lexical/utils/LexicalUtils.js +13 -1
  29. package/dist/cjs/node_modules/@lexical/utils/LexicalUtils.prod.js +47 -32
  30. package/dist/cjs/node_modules/prismjs/prism.js +1 -1
  31. package/dist/cjs/packages/lexical/src/components/StickyComponent.js +1 -1
  32. package/dist/cjs/packages/lexical/src/editor/LexicalEditor.js +30 -5
  33. package/dist/cjs/packages/lexical/src/hooks/useFormatWatcher.js +86 -83
  34. package/dist/cjs/packages/lexical/src/plugins/ToolbarPlugin.js +25 -17
  35. package/dist/esm/node_modules/@lexical/clipboard/LexicalClipboard.dev.js +62 -20
  36. package/dist/esm/node_modules/@lexical/clipboard/LexicalClipboard.js +1 -1
  37. package/dist/esm/node_modules/@lexical/clipboard/LexicalClipboard.prod.js +3 -3
  38. package/dist/esm/node_modules/@lexical/code/LexicalCode.dev.js +637 -246
  39. package/dist/esm/node_modules/@lexical/code/LexicalCode.js +1 -1
  40. package/dist/esm/node_modules/@lexical/code/LexicalCode.prod.js +4 -4
  41. package/dist/esm/node_modules/@lexical/html/LexicalHtml.dev.js +14 -7
  42. package/dist/esm/node_modules/@lexical/html/LexicalHtml.js +1 -1
  43. package/dist/esm/node_modules/@lexical/html/LexicalHtml.prod.js +3 -3
  44. package/dist/esm/node_modules/@lexical/link/LexicalLink.dev.js +244 -144
  45. package/dist/esm/node_modules/@lexical/link/LexicalLink.js +1 -1
  46. package/dist/esm/node_modules/@lexical/link/LexicalLink.prod.js +4 -4
  47. package/dist/esm/node_modules/@lexical/list/LexicalList.dev.js +590 -257
  48. package/dist/esm/node_modules/@lexical/list/LexicalList.js +1 -1
  49. package/dist/esm/node_modules/@lexical/list/LexicalList.prod.js +5 -4
  50. package/dist/esm/node_modules/@lexical/rich-text/LexicalRichText.dev.js +104 -55
  51. package/dist/esm/node_modules/@lexical/rich-text/LexicalRichText.js +1 -1
  52. package/dist/esm/node_modules/@lexical/rich-text/LexicalRichText.prod.js +4 -4
  53. package/dist/esm/node_modules/@lexical/selection/LexicalSelection.dev.js +267 -196
  54. package/dist/esm/node_modules/@lexical/selection/LexicalSelection.js +4 -2
  55. package/dist/esm/node_modules/@lexical/selection/LexicalSelection.prod.js +4 -4
  56. package/dist/esm/node_modules/@lexical/table/LexicalTable.dev.js +2283 -881
  57. package/dist/esm/node_modules/@lexical/table/LexicalTable.js +1 -1
  58. package/dist/esm/node_modules/@lexical/table/LexicalTable.prod.js +4 -4
  59. package/dist/esm/node_modules/@lexical/utils/LexicalUtils.dev.js +457 -194
  60. package/dist/esm/node_modules/@lexical/utils/LexicalUtils.js +8 -2
  61. package/dist/esm/node_modules/@lexical/utils/LexicalUtils.prod.js +3 -3
  62. package/dist/esm/node_modules/prismjs/prism.js +1 -1
  63. package/dist/esm/packages/lexical/src/components/StickyComponent.js +1 -1
  64. package/dist/esm/packages/lexical/src/editor/LexicalEditor.js +30 -5
  65. package/dist/esm/packages/lexical/src/hooks/useFormatWatcher.js +88 -85
  66. package/dist/esm/packages/lexical/src/plugins/ToolbarPlugin.js +25 -17
  67. package/dist/types/editor/LexicalEditor.d.ts +25 -0
  68. package/dist/types/hooks/useFormatWatcher.d.ts +2 -2
  69. package/dist/types/nodes/StickyNode.d.ts +0 -1
  70. package/package.json +14 -13
  71. package/src/components/StickyComponent.tsx +2 -2
  72. package/src/editor/LexicalEditor.tsx +57 -26
  73. package/src/hooks/useFormatWatcher.ts +35 -31
  74. package/src/plugins/ToolbarPlugin.tsx +27 -17
  75. package/dist/cjs/editor/Lexical.module.css +0 -1
  76. package/dist/esm/editor/Lexical.module.css +0 -1
@@ -1,7 +1,7 @@
1
1
  'use client';
2
2
 
3
- import { addClassNamesToElement, isHTMLAnchorElement } from '../utils/LexicalUtils.js';
4
- import { ElementNode, $isRangeSelection, $applyNodeReplacement, $isElementNode, createCommand, $getSelection } from 'lexical';
3
+ import { addClassNamesToElement, isHTMLAnchorElement, $findMatchingParent } from '../utils/LexicalUtils.js';
4
+ import { ElementNode, $isRangeSelection, $applyNodeReplacement, $isElementNode, createCommand, $getSelection, $isNodeSelection, $normalizeSelection__EXPERIMENTAL, $setSelection } from 'lexical';
5
5
 
6
6
  /**
7
7
  * Copyright (c) Meta Platforms, Inc. and affiliates.
@@ -20,6 +20,12 @@ import { ElementNode, $isRangeSelection, $applyNodeReplacement, $isElementNode,
20
20
  *
21
21
  */
22
22
 
23
+ // Do not require this module directly! Use normal `invariant` calls.
24
+
25
+ function formatDevErrorMessage(message) {
26
+ throw new Error(message);
27
+ }
28
+
23
29
  const SUPPORTED_URL_PROTOCOLS = new Set(['http:', 'https:', 'mailto:', 'sms:', 'tel:']);
24
30
 
25
31
  /** @noInheritDoc */
@@ -42,7 +48,7 @@ class LinkNode extends ElementNode {
42
48
  title: node.__title
43
49
  }, node.__key);
44
50
  }
45
- constructor(url, attributes = {}, key) {
51
+ constructor(url = '', attributes = {}, key) {
46
52
  super(key);
47
53
  const {
48
54
  target = null,
@@ -56,50 +62,30 @@ class LinkNode extends ElementNode {
56
62
  }
57
63
  createDOM(config) {
58
64
  const element = document.createElement('a');
59
- element.href = this.sanitizeUrl(this.__url);
60
- if (this.__target !== null) {
61
- element.target = this.__target;
62
- }
63
- if (this.__rel !== null) {
64
- element.rel = this.__rel;
65
- }
66
- if (this.__title !== null) {
67
- element.title = this.__title;
68
- }
65
+ this.updateLinkDOM(null, element, config);
69
66
  addClassNamesToElement(element, config.theme.link);
70
67
  return element;
71
68
  }
72
- updateDOM(prevNode, anchor, config) {
73
- if (anchor instanceof HTMLAnchorElement) {
74
- const url = this.__url;
75
- const target = this.__target;
76
- const rel = this.__rel;
77
- const title = this.__title;
78
- if (url !== prevNode.__url) {
79
- anchor.href = url;
80
- }
81
- if (target !== prevNode.__target) {
82
- if (target) {
83
- anchor.target = target;
84
- } else {
85
- anchor.removeAttribute('target');
86
- }
69
+ updateLinkDOM(prevNode, anchor, config) {
70
+ if (isHTMLAnchorElement(anchor)) {
71
+ if (!prevNode || prevNode.__url !== this.__url) {
72
+ anchor.href = this.sanitizeUrl(this.__url);
87
73
  }
88
- if (rel !== prevNode.__rel) {
89
- if (rel) {
90
- anchor.rel = rel;
91
- } else {
92
- anchor.removeAttribute('rel');
93
- }
94
- }
95
- if (title !== prevNode.__title) {
96
- if (title) {
97
- anchor.title = title;
98
- } else {
99
- anchor.removeAttribute('title');
74
+ for (const attr of ['target', 'rel', 'title']) {
75
+ const key = `__${attr}`;
76
+ const value = this[key];
77
+ if (!prevNode || prevNode[key] !== value) {
78
+ if (value) {
79
+ anchor[attr] = value;
80
+ } else {
81
+ anchor.removeAttribute(attr);
82
+ }
100
83
  }
101
84
  }
102
85
  }
86
+ }
87
+ updateDOM(prevNode, anchor, config) {
88
+ this.updateLinkDOM(prevNode, anchor, config);
103
89
  return false;
104
90
  }
105
91
  static importDOM() {
@@ -111,19 +97,15 @@ class LinkNode extends ElementNode {
111
97
  };
112
98
  }
113
99
  static importJSON(serializedNode) {
114
- const node = $createLinkNode(serializedNode.url, {
115
- rel: serializedNode.rel,
116
- target: serializedNode.target,
117
- title: serializedNode.title
118
- });
119
- node.setFormat(serializedNode.format);
120
- node.setIndent(serializedNode.indent);
121
- node.setDirection(serializedNode.direction);
122
- return node;
100
+ return $createLinkNode().updateFromJSON(serializedNode);
101
+ }
102
+ updateFromJSON(serializedNode) {
103
+ return super.updateFromJSON(serializedNode).setURL(serializedNode.url).setRel(serializedNode.rel || null).setTarget(serializedNode.target || null).setTitle(serializedNode.title || null);
123
104
  }
124
105
  sanitizeUrl(url) {
106
+ url = formatUrl(url);
125
107
  try {
126
- const parsedUrl = new URL(url);
108
+ const parsedUrl = new URL(formatUrl(url));
127
109
  // eslint-disable-next-line no-script-url
128
110
  if (!SUPPORTED_URL_PROTOCOLS.has(parsedUrl.protocol)) {
129
111
  return 'about:blank';
@@ -139,9 +121,7 @@ class LinkNode extends ElementNode {
139
121
  rel: this.getRel(),
140
122
  target: this.getTarget(),
141
123
  title: this.getTitle(),
142
- type: 'link',
143
- url: this.getURL(),
144
- version: 1
124
+ url: this.getURL()
145
125
  };
146
126
  }
147
127
  getURL() {
@@ -150,6 +130,7 @@ class LinkNode extends ElementNode {
150
130
  setURL(url) {
151
131
  const writable = this.getWritable();
152
132
  writable.__url = url;
133
+ return writable;
153
134
  }
154
135
  getTarget() {
155
136
  return this.getLatest().__target;
@@ -157,6 +138,7 @@ class LinkNode extends ElementNode {
157
138
  setTarget(target) {
158
139
  const writable = this.getWritable();
159
140
  writable.__target = target;
141
+ return writable;
160
142
  }
161
143
  getRel() {
162
144
  return this.getLatest().__rel;
@@ -164,6 +146,7 @@ class LinkNode extends ElementNode {
164
146
  setRel(rel) {
165
147
  const writable = this.getWritable();
166
148
  writable.__rel = rel;
149
+ return writable;
167
150
  }
168
151
  getTitle() {
169
152
  return this.getLatest().__title;
@@ -171,6 +154,7 @@ class LinkNode extends ElementNode {
171
154
  setTitle(title) {
172
155
  const writable = this.getWritable();
173
156
  writable.__title = title;
157
+ return writable;
174
158
  }
175
159
  insertNewAfter(_, restoreSelection = true) {
176
160
  const linkNode = $createLinkNode(this.__url, {
@@ -231,7 +215,7 @@ function $convertAnchorElement(domNode) {
231
215
  * @param attributes - Optional HTML a tag attributes \\{ target, rel, title \\}
232
216
  * @returns The LinkNode.
233
217
  */
234
- function $createLinkNode(url, attributes) {
218
+ function $createLinkNode(url = '', attributes) {
235
219
  return $applyNodeReplacement(new LinkNode(url, attributes));
236
220
  }
237
221
 
@@ -249,7 +233,7 @@ class AutoLinkNode extends LinkNode {
249
233
  /** @internal */
250
234
  /** Indicates whether the autolink was ever unlinked. **/
251
235
 
252
- constructor(url, attributes = {}, key) {
236
+ constructor(url = '', attributes = {}, key) {
253
237
  super(url, attributes, key);
254
238
  this.__isUnlinked = attributes.isUnlinked !== undefined && attributes.isUnlinked !== null ? attributes.isUnlinked : false;
255
239
  }
@@ -283,16 +267,10 @@ class AutoLinkNode extends LinkNode {
283
267
  return super.updateDOM(prevNode, anchor, config) || prevNode.__isUnlinked !== this.__isUnlinked;
284
268
  }
285
269
  static importJSON(serializedNode) {
286
- const node = $createAutoLinkNode(serializedNode.url, {
287
- isUnlinked: serializedNode.isUnlinked,
288
- rel: serializedNode.rel,
289
- target: serializedNode.target,
290
- title: serializedNode.title
291
- });
292
- node.setFormat(serializedNode.format);
293
- node.setIndent(serializedNode.indent);
294
- node.setDirection(serializedNode.direction);
295
- return node;
270
+ return $createAutoLinkNode().updateFromJSON(serializedNode);
271
+ }
272
+ updateFromJSON(serializedNode) {
273
+ return super.updateFromJSON(serializedNode).setIsUnlinked(serializedNode.isUnlinked || false);
296
274
  }
297
275
  static importDOM() {
298
276
  // TODO: Should link node should handle the import over autolink?
@@ -301,9 +279,7 @@ class AutoLinkNode extends LinkNode {
301
279
  exportJSON() {
302
280
  return {
303
281
  ...super.exportJSON(),
304
- isUnlinked: this.__isUnlinked,
305
- type: 'autolink',
306
- version: 1
282
+ isUnlinked: this.__isUnlinked
307
283
  };
308
284
  }
309
285
  insertNewAfter(selection, restoreSelection = true) {
@@ -329,7 +305,7 @@ class AutoLinkNode extends LinkNode {
329
305
  * @param attributes - Optional HTML a tag attributes. \\{ target, rel, title \\}
330
306
  * @returns The LinkNode.
331
307
  */
332
- function $createAutoLinkNode(url, attributes) {
308
+ function $createAutoLinkNode(url = '', attributes) {
333
309
  return $applyNodeReplacement(new AutoLinkNode(url, attributes));
334
310
  }
335
311
 
@@ -342,6 +318,56 @@ function $isAutoLinkNode(node) {
342
318
  return node instanceof AutoLinkNode;
343
319
  }
344
320
  const TOGGLE_LINK_COMMAND = createCommand('TOGGLE_LINK_COMMAND');
321
+ function $getPointNode(point, offset) {
322
+ if (point.type === 'element') {
323
+ const node = point.getNode();
324
+ if (!$isElementNode(node)) {
325
+ formatDevErrorMessage(`$getPointNode: element point is not an ElementNode`);
326
+ }
327
+ const childNode = node.getChildren()[point.offset + offset];
328
+ return childNode || null;
329
+ }
330
+ return null;
331
+ }
332
+
333
+ /**
334
+ * Preserve the logical start/end of a RangeSelection in situations where
335
+ * the point is an element that may be reparented in the callback.
336
+ *
337
+ * @param $fn The function to run
338
+ * @returns The result of the callback
339
+ */
340
+ function $withSelectedNodes($fn) {
341
+ const initialSelection = $getSelection();
342
+ if (!$isRangeSelection(initialSelection)) {
343
+ return $fn();
344
+ }
345
+ const normalized = $normalizeSelection__EXPERIMENTAL(initialSelection);
346
+ const isBackwards = normalized.isBackward();
347
+ const anchorNode = $getPointNode(normalized.anchor, isBackwards ? -1 : 0);
348
+ const focusNode = $getPointNode(normalized.focus, isBackwards ? 0 : -1);
349
+ const rval = $fn();
350
+ if (anchorNode || focusNode) {
351
+ const updatedSelection = $getSelection();
352
+ if ($isRangeSelection(updatedSelection)) {
353
+ const finalSelection = updatedSelection.clone();
354
+ if (anchorNode) {
355
+ const anchorParent = anchorNode.getParent();
356
+ if (anchorParent) {
357
+ finalSelection.anchor.set(anchorParent.getKey(), anchorNode.getIndexWithinParent() + (isBackwards ? 1 : 0), 'element');
358
+ }
359
+ }
360
+ if (focusNode) {
361
+ const focusParent = focusNode.getParent();
362
+ if (focusParent) {
363
+ finalSelection.focus.set(focusParent.getKey(), focusNode.getIndexWithinParent() + (isBackwards ? 0 : 1), 'element');
364
+ }
365
+ }
366
+ $setSelection($normalizeSelection__EXPERIMENTAL(finalSelection));
367
+ }
368
+ }
369
+ return rval;
370
+ }
345
371
 
346
372
  /**
347
373
  * Generates or updates a LinkNode. It can also delete a LinkNode if the URL is null,
@@ -356,99 +382,140 @@ function $toggleLink(url, attributes = {}) {
356
382
  } = attributes;
357
383
  const rel = attributes.rel === undefined ? 'noreferrer' : attributes.rel;
358
384
  const selection = $getSelection();
359
- if (!$isRangeSelection(selection)) {
385
+ if (selection === null || !$isRangeSelection(selection) && !$isNodeSelection(selection)) {
386
+ return;
387
+ }
388
+ if ($isNodeSelection(selection)) {
389
+ const nodes = selection.getNodes();
390
+ if (nodes.length === 0) {
391
+ return;
392
+ }
393
+
394
+ // Handle all selected nodes
395
+ nodes.forEach(node => {
396
+ if (url === null) {
397
+ // Remove link
398
+ const linkParent = $findMatchingParent(node, parent => !$isAutoLinkNode(parent) && $isLinkNode(parent));
399
+ if (linkParent) {
400
+ linkParent.insertBefore(node);
401
+ if (linkParent.getChildren().length === 0) {
402
+ linkParent.remove();
403
+ }
404
+ }
405
+ } else {
406
+ // Add/Update link
407
+ const existingLink = $findMatchingParent(node, parent => !$isAutoLinkNode(parent) && $isLinkNode(parent));
408
+ if (existingLink) {
409
+ existingLink.setURL(url);
410
+ if (target !== undefined) {
411
+ existingLink.setTarget(target);
412
+ }
413
+ if (rel !== undefined) {
414
+ existingLink.setRel(rel);
415
+ }
416
+ } else {
417
+ const linkNode = $createLinkNode(url, {
418
+ rel,
419
+ target
420
+ });
421
+ node.insertBefore(linkNode);
422
+ linkNode.append(node);
423
+ }
424
+ }
425
+ });
360
426
  return;
361
427
  }
428
+
429
+ // Handle RangeSelection
362
430
  const nodes = selection.extract();
363
431
  if (url === null) {
364
432
  // Remove LinkNodes
365
433
  nodes.forEach(node => {
366
- const parent = node.getParent();
367
- if (!$isAutoLinkNode(parent) && $isLinkNode(parent)) {
368
- const children = parent.getChildren();
434
+ const parentLink = $findMatchingParent(node, parent => !$isAutoLinkNode(parent) && $isLinkNode(parent));
435
+ if (parentLink) {
436
+ const children = parentLink.getChildren();
369
437
  for (let i = 0; i < children.length; i++) {
370
- parent.insertBefore(children[i]);
438
+ parentLink.insertBefore(children[i]);
371
439
  }
372
- parent.remove();
440
+ parentLink.remove();
373
441
  }
374
442
  });
375
- } else {
376
- // Add or merge LinkNodes
377
- if (nodes.length === 1) {
378
- const firstNode = nodes[0];
379
- // if the first node is a LinkNode or if its
380
- // parent is a LinkNode, we update the URL, target and rel.
381
- const linkNode = $getAncestor(firstNode, $isLinkNode);
382
- if (linkNode !== null) {
383
- linkNode.setURL(url);
384
- if (target !== undefined) {
385
- linkNode.setTarget(target);
386
- }
387
- if (rel !== null) {
388
- linkNode.setRel(rel);
389
- }
390
- if (title !== undefined) {
391
- linkNode.setTitle(title);
392
- }
393
- return;
394
- }
443
+ return;
444
+ }
445
+ const updatedNodes = new Set();
446
+ const updateLinkNode = linkNode => {
447
+ if (updatedNodes.has(linkNode.getKey())) {
448
+ return;
449
+ }
450
+ updatedNodes.add(linkNode.getKey());
451
+ linkNode.setURL(url);
452
+ if (target !== undefined) {
453
+ linkNode.setTarget(target);
395
454
  }
396
- let prevParent = null;
455
+ if (rel !== undefined) {
456
+ linkNode.setRel(rel);
457
+ }
458
+ if (title !== undefined) {
459
+ linkNode.setTitle(title);
460
+ }
461
+ };
462
+ // Add or merge LinkNodes
463
+ if (nodes.length === 1) {
464
+ const firstNode = nodes[0];
465
+ // if the first node is a LinkNode or if its
466
+ // parent is a LinkNode, we update the URL, target and rel.
467
+ const linkNode = $getAncestor(firstNode, $isLinkNode);
468
+ if (linkNode !== null) {
469
+ return updateLinkNode(linkNode);
470
+ }
471
+ }
472
+ $withSelectedNodes(() => {
397
473
  let linkNode = null;
398
- nodes.forEach(node => {
399
- const parent = node.getParent();
400
- if (parent === linkNode || parent === null || $isElementNode(node) && !node.isInline()) {
401
- return;
474
+ for (const node of nodes) {
475
+ if (!node.isAttached()) {
476
+ continue;
402
477
  }
403
- if ($isLinkNode(parent)) {
404
- linkNode = parent;
405
- parent.setURL(url);
406
- if (target !== undefined) {
407
- parent.setTarget(target);
408
- }
409
- if (rel !== null) {
410
- linkNode.setRel(rel);
411
- }
412
- if (title !== undefined) {
413
- linkNode.setTitle(title);
414
- }
415
- return;
416
- }
417
- if (!parent.is(prevParent)) {
418
- prevParent = parent;
419
- linkNode = $createLinkNode(url, {
420
- rel,
421
- target,
422
- title
423
- });
424
- if ($isLinkNode(parent)) {
425
- if (node.getPreviousSibling() === null) {
426
- parent.insertBefore(linkNode);
427
- } else {
428
- parent.insertAfter(linkNode);
429
- }
430
- } else {
431
- node.insertBefore(linkNode);
432
- }
478
+ const parentLinkNode = $getAncestor(node, $isLinkNode);
479
+ if (parentLinkNode) {
480
+ updateLinkNode(parentLinkNode);
481
+ continue;
433
482
  }
434
- if ($isLinkNode(node)) {
435
- if (node.is(linkNode)) {
436
- return;
483
+ if ($isElementNode(node)) {
484
+ if (!node.isInline()) {
485
+ // Ignore block nodes, if there are any children we will see them
486
+ // later and wrap in a new LinkNode
487
+ continue;
437
488
  }
438
- if (linkNode !== null) {
439
- const children = node.getChildren();
440
- for (let i = 0; i < children.length; i++) {
441
- linkNode.append(children[i]);
489
+ if ($isLinkNode(node)) {
490
+ // If it's not an autolink node and we don't already have a LinkNode
491
+ // in this block then we can update it and re-use it
492
+ if (!$isAutoLinkNode(node) && (linkNode === null || !linkNode.getParentOrThrow().isParentOf(node))) {
493
+ updateLinkNode(node);
494
+ linkNode = node;
495
+ continue;
496
+ }
497
+ // Unwrap LinkNode, we already have one or it's an AutoLinkNode
498
+ for (const child of node.getChildren()) {
499
+ node.insertBefore(child);
442
500
  }
501
+ node.remove();
502
+ continue;
443
503
  }
444
- node.remove();
445
- return;
446
504
  }
447
- if (linkNode !== null) {
448
- linkNode.append(node);
505
+ const prevLinkNode = node.getPreviousSibling();
506
+ if ($isLinkNode(prevLinkNode) && prevLinkNode.is(linkNode)) {
507
+ prevLinkNode.append(node);
508
+ continue;
449
509
  }
450
- });
451
- }
510
+ linkNode = $createLinkNode(url, {
511
+ rel,
512
+ target,
513
+ title
514
+ });
515
+ node.insertAfter(linkNode);
516
+ linkNode.append(node);
517
+ }
518
+ });
452
519
  }
453
520
  /** @deprecated renamed to {@link $toggleLink} by @lexical/eslint-plugin rules-of-lexical */
454
521
  const toggleLink = $toggleLink;
@@ -459,5 +526,38 @@ function $getAncestor(node, predicate) {
459
526
  }
460
527
  return predicate(parent) ? parent : null;
461
528
  }
529
+ const PHONE_NUMBER_REGEX = /^\+?[0-9\s()-]{5,}$/;
530
+
531
+ /**
532
+ * Formats a URL string by adding appropriate protocol if missing
533
+ *
534
+ * @param url - URL to format
535
+ * @returns Formatted URL with appropriate protocol
536
+ */
537
+ function formatUrl(url) {
538
+ // Check if URL already has a protocol
539
+ if (url.match(/^[a-z][a-z0-9+.-]*:/i)) {
540
+ // URL already has a protocol, leave it as is
541
+ return url;
542
+ }
543
+ // Check if it's a relative path (starting with '/', '.', or '#')
544
+ else if (url.match(/^[/#.]/)) {
545
+ // Relative path, leave it as is
546
+ return url;
547
+ }
548
+
549
+ // Check for email address
550
+ else if (url.includes('@')) {
551
+ return `mailto:${url}`;
552
+ }
553
+
554
+ // Check for phone number
555
+ else if (PHONE_NUMBER_REGEX.test(url)) {
556
+ return `tel:${url}`;
557
+ }
558
+
559
+ // For everything else, return with https:// prefix
560
+ return `https://${url}`;
561
+ }
462
562
 
463
- export { $createAutoLinkNode, $createLinkNode, $isAutoLinkNode, $isLinkNode, $toggleLink, AutoLinkNode, LinkNode, TOGGLE_LINK_COMMAND, toggleLink };
563
+ export { $createAutoLinkNode, $createLinkNode, $isAutoLinkNode, $isLinkNode, $toggleLink, AutoLinkNode, LinkNode, TOGGLE_LINK_COMMAND, formatUrl, toggleLink };
@@ -11,7 +11,7 @@ import * as LexicalLink_prod from './LexicalLink.prod.js';
11
11
  *
12
12
  */
13
13
 
14
- const mod = process.env.NODE_ENV === 'development' ? LexicalLink_dev : LexicalLink_prod;
14
+ const mod = process.env.NODE_ENV !== 'production' ? LexicalLink_dev : LexicalLink_prod;
15
15
  const $isLinkNode = mod.$isLinkNode;
16
16
 
17
17
  export { $isLinkNode };
@@ -1,7 +1,7 @@
1
1
  'use client';
2
2
 
3
- import { addClassNamesToElement, isHTMLAnchorElement } from '../utils/LexicalUtils.js';
4
- import { ElementNode, $isRangeSelection, $applyNodeReplacement, $isElementNode, createCommand, $getSelection } from 'lexical';
3
+ import { addClassNamesToElement, isHTMLAnchorElement, $findMatchingParent } from '../utils/LexicalUtils.js';
4
+ import { ElementNode, $isRangeSelection, $applyNodeReplacement, $isElementNode, createCommand, $getSelection, $isNodeSelection, $normalizeSelection__EXPERIMENTAL, $setSelection } from 'lexical';
5
5
 
6
6
  /**
7
7
  * Copyright (c) Meta Platforms, Inc. and affiliates.
@@ -11,6 +11,6 @@ import { ElementNode, $isRangeSelection, $applyNodeReplacement, $isElementNode,
11
11
  *
12
12
  */
13
13
 
14
- const _=new Set(["http:","https:","mailto:","sms:","tel:"]);class o extends ElementNode{static getType(){return "link"}static clone(t){return new o(t.__url,{rel:t.__rel,target:t.__target,title:t.__title},t.__key)}constructor(t,e={},r){super(r);const{target:i=null,rel:n=null,title:l=null}=e;this.__url=t,this.__target=i,this.__rel=n,this.__title=l;}createDOM(e){const r=document.createElement("a");return r.href=this.sanitizeUrl(this.__url),null!==this.__target&&(r.target=this.__target),null!==this.__rel&&(r.rel=this.__rel),null!==this.__title&&(r.title=this.__title),addClassNamesToElement(r,e.theme.link),r}updateDOM(t,e,r){if(e instanceof HTMLAnchorElement){const r=this.__url,i=this.__target,n=this.__rel,l=this.__title;r!==t.__url&&(e.href=r),i!==t.__target&&(i?e.target=i:e.removeAttribute("target")),n!==t.__rel&&(n?e.rel=n:e.removeAttribute("rel")),l!==t.__title&&(l?e.title=l:e.removeAttribute("title"));}return !1}static importDOM(){return {a:t=>({conversion:a,priority:1})}}static importJSON(t){const e=h(t.url,{rel:t.rel,target:t.target,title:t.title});return e.setFormat(t.format),e.setIndent(t.indent),e.setDirection(t.direction),e}sanitizeUrl(t){try{const e=new URL(t);if(!_.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(),type:"link",url:this.getURL(),version:1}}getURL(){return this.getLatest().__url}setURL(t){this.getWritable().__url=t;}getTarget(){return this.getLatest().__target}setTarget(t){this.getWritable().__target=t;}getRel(){return this.getLatest().__rel}setRel(t){this.getWritable().__rel=t;}getTitle(){return this.getLatest().__title}setTitle(t){this.getWritable().__title=t;}insertNewAfter(t,e=!0){const r=h(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(!$isRangeSelection(e))return !1;const i=e.anchor.getNode(),l=e.focus.getNode();return this.isParentOf(i)&&this.isParentOf(l)&&e.getTextContent().length>0}isEmailURI(){return this.__url.startsWith("mailto:")}isWebSiteURI(){return this.__url.startsWith("https://")||this.__url.startsWith("http://")}}function a(t){let r=null;if(isHTMLAnchorElement(t)){const e=t.textContent;(null!==e&&""!==e||t.children.length>0)&&(r=h(t.getAttribute("href")||"",{rel:t.getAttribute("rel"),target:t.getAttribute("target"),title:t.getAttribute("title")}));}return {node:r}}function h(t,e){return $applyNodeReplacement(new o(t,e))}function c(t){return t instanceof o}class g extends o{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 g(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){const e=f(t.url,{isUnlinked:t.isUnlinked,rel:t.rel,target:t.target,title:t.title});return e.setFormat(t.format),e.setIndent(t.indent),e.setDirection(t.direction),e}static importDOM(){return null}exportJSON(){return {...super.exportJSON(),isUnlinked:this.__isUnlinked,type:"autolink",version:1}}insertNewAfter(t,e=!0){const r=this.getParentOrThrow().insertNewAfter(t,e);if($isElementNode(r)){const t=f(this.__url,{isUnlinked:this.__isUnlinked,rel:this.__rel,target:this.__target,title:this.__title});return r.append(t),t}return null}}function f(t,e){return $applyNodeReplacement(new g(t,e))}function d(t){return t instanceof g}const p=createCommand("TOGGLE_LINK_COMMAND");function U(t,e={}){const{target:r,title:i}=e,l=void 0===e.rel?"noreferrer":e.rel,_=$getSelection();if(!$isRangeSelection(_))return;const o=_.extract();if(null===t)o.forEach((t=>{const e=t.getParent();if(!d(e)&&c(e)){const t=e.getChildren();for(let r=0;r<t.length;r++)e.insertBefore(t[r]);e.remove();}}));else {if(1===o.length){const e=function(t,e){let r=t;for(;null!==r&&null!==r.getParent()&&!e(r);)r=r.getParentOrThrow();return e(r)?r:null}(o[0],c);if(null!==e)return e.setURL(t),void 0!==r&&e.setTarget(r),null!==l&&e.setRel(l),void(void 0!==i&&e.setTitle(i))}let e=null,n=null;o.forEach((u=>{const _=u.getParent();if(_!==n&&null!==_&&(!$isElementNode(u)||u.isInline())){if(c(_))return n=_,_.setURL(t),void 0!==r&&_.setTarget(r),null!==l&&n.setRel(l),void(void 0!==i&&n.setTitle(i));if(_.is(e)||(e=_,n=h(t,{rel:l,target:r,title:i}),c(_)?null===u.getPreviousSibling()?_.insertBefore(n):_.insertAfter(n):u.insertBefore(n)),c(u)){if(u.is(n))return;if(null!==n){const t=u.getChildren();for(let e=0;e<t.length;e++)n.append(t[e]);}u.remove();}else null!==n&&n.append(u);}}));}}const m=U;
14
+ const h=new Set(["http:","https:","mailto:","sms:","tel:"]);class g extends ElementNode{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),addClassNamesToElement(r,e.theme.link),r}updateLinkDOM(t,r,n){if(isHTMLAnchorElement(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(!$isRangeSelection(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(isHTMLAnchorElement(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 $applyNodeReplacement(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($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="",e){return $applyNodeReplacement(new m(t,e))}function O(t){return t instanceof m}const k=createCommand("TOGGLE_LINK_COMMAND");function N(t,e){if("element"===t.type){const r=t.getNode();$isElementNode(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=$getSelection();if(null===h||!$isRangeSelection(h)&&!$isNodeSelection(h))return;if($isNodeSelection(h)){const e=h.getNodes();if(0===e.length)return;return void e.forEach((e=>{if(null===t){const t=$findMatchingParent(e,(t=>!O(t)&&p(t)));t&&(t.insertBefore(e),0===t.getChildren().length&&t.remove());}else {const i=$findMatchingParent(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=$findMatchingParent(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=$getSelection();if(!$isRangeSelection(e))return t();const r=$normalizeSelection__EXPERIMENTAL(e),n=r.isBackward(),i=N(r.anchor,n?-1:0),l=N(r.focus,n?0:-1);t();if(i||l){const t=$getSelection();if($isRangeSelection(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");}$setSelection($normalizeSelection__EXPERIMENTAL(e));}}}((()=>{let e=null;for(const r of g){if(!r.isAttached())continue;const s=x(r,p);if(s){m(s);continue}if($isElementNode(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}`}
15
15
 
16
- export { f as $createAutoLinkNode, h as $createLinkNode, d as $isAutoLinkNode, c as $isLinkNode, U as $toggleLink, g as AutoLinkNode, o as LinkNode, p as TOGGLE_LINK_COMMAND, m as toggleLink };
16
+ 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 };