@astryxdesign/richtext 0.7.7-canary.559b005 → 0.7.7-canary.58c5b8a

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.
@@ -85,7 +85,9 @@ export interface RichTextEditorToolbarProps extends BaseProps<HTMLDivElement> {
85
85
  /**
86
86
  * Whether to show the insert/edit-link button. By default, pressing it opens
87
87
  * an Astryx Dialog. The form can add, update, or remove a link without
88
- * losing the editor selection.
88
+ * losing the editor selection. With a collapsed selection, Insert link
89
+ * inserts the entered URL as link text at the caret; with a non-empty
90
+ * selection it links the selected text.
89
91
  *
90
92
  * Requires `LinkNode` to be registered (it is, by default) — no extra setup.
91
93
  * The entered URL is sanitized (see `sanitizeUrl`) so only
@@ -1 +1 @@
1
- {"version":3,"file":"RichTextEditorToolbar.d.ts","sourceRoot":"","sources":["../src/RichTextEditorToolbar.tsx"],"names":[],"mappings":"AAIA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAiCG;AAEH,OAAO,EAML,KAAK,SAAS,EACd,KAAK,GAAG,EACT,MAAM,OAAO,CAAC;AAmBf,OAAO,KAAK,EAAC,SAAS,EAAC,MAAM,oBAAoB,CAAC;AA8HlD;;;;;GAKG;AACH,eAAO,MAAM,kBAAkB;;;;;;;;;;;;;;;;CAgBrB,CAAC;AAmPX,MAAM,WAAW,0BAA2B,SAAQ,SAAS,CAAC,cAAc,CAAC;IAC3E;;;;;;OAMG;IACH,GAAG,CAAC,EAAE,GAAG,CAAC,cAAc,CAAC,CAAC;IAC1B;;;OAGG;IACH,KAAK,CAAC,EAAE,MAAM,CAAC;IACf;;;OAGG;IACH,aAAa,CAAC,EAAE,aAAa,CAAC,IAAI,GAAG,IAAI,GAAG,IAAI,CAAC,CAAC;IAClD;;;;OAIG;IACH,IAAI,CAAC,EAAE,IAAI,GAAG,IAAI,GAAG,IAAI,CAAC;IAC1B;;;;;;;;;OASG;IACH,OAAO,CAAC,EAAE,OAAO,CAAC;IA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ACH,YAAY,CAAC,EAAE,CAAC,YAAY,EAAE,MAAM,KAAK,MAAM,GAAG,IAAI,GAAG,SAAS,CAAC;IACnE;;;;;;;OAOG;IACH,cAAc,CAAC,EAAE,OAAO,CAAC;IACzB;;;;OAIG;IACH,UAAU,CAAC,EAAE,SAAS,CAAC;CACxB;AAED;;;;;;;;;;;;;;;;;;;GAmBG;AACH,wBAAgB,qBAAqB,CAAC,EACpC,KAAK,EAAE,cAAc,EACrB,aAAkC,EAClC,IAAW,EACX,OAAc,EACd,YAAY,EACZ,cAAqB,EACrB,UAAU,EACV,MAAM,EACN,GAAG,EACH,GAAG,IAAI,EACR,EAAE,0BAA0B,+BAmhB5B;yBA9hBe,qBAAqB"}
@@ -541,27 +541,39 @@ export function RichTextEditorToolbar({
541
541
  if (url === 'about:blank') {
542
542
  return false;
543
543
  }
544
- restoreLinkSelection();
545
544
  const linkAttributes = hasNewTabLinks ? {
546
545
  target: '_blank',
547
546
  rel: 'noopener noreferrer'
548
547
  } : {};
549
- const handled = editor.dispatchCommand(TOGGLE_LINK_COMMAND, {
550
- url,
551
- ...linkAttributes
552
- });
553
- if (!handled) {
554
- editor.update(() => {
555
- const selection = $getSelection();
556
- if ($isRangeSelection(selection) && selection.isCollapsed()) {
548
+ const savedSelection = savedSelectionRef.current;
549
+ // One update restores the selection, decides, and applies. Inside a
550
+ // command listener (Cmd/Ctrl+K) Lexical queues an update, so a flag set
551
+ // in one update cannot steer code that runs after it.
552
+ editor.update(() => {
553
+ if (savedSelection) {
554
+ $setSelection(savedSelection.clone());
555
+ }
556
+ const selection = $getSelection();
557
+ if ($isRangeSelection(selection) && selection.isCollapsed()) {
558
+ const node = selection.anchor.getNode();
559
+ const isInLink = $isLinkNode(node) || $isLinkNode(node.getParent());
560
+ // At a caret outside a link there is no text to link, so the URL
561
+ // becomes the link's text. The toggle command would link the whole
562
+ // text node around the caret instead.
563
+ if (!isInLink) {
557
564
  const linkNode = $createLinkNode(url, linkAttributes);
558
565
  linkNode.append($createTextNode(trimmed));
559
566
  selection.insertNodes([linkNode]);
567
+ return;
560
568
  }
569
+ }
570
+ editor.dispatchCommand(TOGGLE_LINK_COMMAND, {
571
+ url,
572
+ ...linkAttributes
561
573
  });
562
- }
574
+ });
563
575
  return true;
564
- }, [editor, hasNewTabLinks, restoreLinkSelection]);
576
+ }, [editor, hasNewTabLinks]);
565
577
  const toggleLink = useCallback(() => {
566
578
  if (!editor.isEditable()) {
567
579
  return;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@astryxdesign/richtext",
3
3
  "displayName": "Rich Text",
4
- "version": "0.7.7-canary.559b005",
4
+ "version": "0.7.7-canary.58c5b8a",
5
5
  "description": "Astryx rich text — a Lexical-based rich text editor and viewer, styled with Astryx design tokens.",
6
6
  "author": "Meta Open Source",
7
7
  "license": "MIT",
@@ -66,7 +66,7 @@
66
66
  },
67
67
  "peerDependencies": {
68
68
  "@stylexjs/stylex": ">=0.10.0",
69
- "@astryxdesign/core": "0.7.7-canary.559b005",
69
+ "@astryxdesign/core": "0.7.7-canary.58c5b8a",
70
70
  "react": ">=19.0.0",
71
71
  "react-dom": ">=19.0.0",
72
72
  "lexical": "^0.52.0",
@@ -125,8 +125,8 @@
125
125
  }
126
126
  },
127
127
  "devDependencies": {
128
- "@astryxdesign/a11y-spec": "0.7.7-canary.559b005",
129
- "@astryxdesign/cli": "0.7.7-canary.559b005",
128
+ "@astryxdesign/a11y-spec": "0.7.7-canary.58c5b8a",
129
+ "@astryxdesign/cli": "0.7.7-canary.58c5b8a",
130
130
  "@babel/cli": "^7.29.7",
131
131
  "@babel/core": "^7.29.7",
132
132
  "@babel/preset-react": "^7.29.7",
@@ -483,7 +483,9 @@ export interface RichTextEditorToolbarProps extends BaseProps<HTMLDivElement> {
483
483
  /**
484
484
  * Whether to show the insert/edit-link button. By default, pressing it opens
485
485
  * an Astryx Dialog. The form can add, update, or remove a link without
486
- * losing the editor selection.
486
+ * losing the editor selection. With a collapsed selection, Insert link
487
+ * inserts the entered URL as link text at the caret; with a non-empty
488
+ * selection it links the selected text.
487
489
  *
488
490
  * Requires `LinkNode` to be registered (it is, by default) — no extra setup.
489
491
  * The entered URL is sanitized (see `sanitizeUrl`) so only
@@ -694,27 +696,36 @@ export function RichTextEditorToolbar({
694
696
  return false;
695
697
  }
696
698
 
697
- restoreLinkSelection();
698
699
  const linkAttributes = hasNewTabLinks
699
700
  ? {target: '_blank', rel: 'noopener noreferrer'}
700
701
  : {};
701
- const handled = editor.dispatchCommand(TOGGLE_LINK_COMMAND, {
702
- url,
703
- ...linkAttributes,
704
- });
705
- if (!handled) {
706
- editor.update(() => {
707
- const selection = $getSelection();
708
- if ($isRangeSelection(selection) && selection.isCollapsed()) {
702
+ const savedSelection = savedSelectionRef.current;
703
+ // One update restores the selection, decides, and applies. Inside a
704
+ // command listener (Cmd/Ctrl+K) Lexical queues an update, so a flag set
705
+ // in one update cannot steer code that runs after it.
706
+ editor.update(() => {
707
+ if (savedSelection) {
708
+ $setSelection(savedSelection.clone());
709
+ }
710
+ const selection = $getSelection();
711
+ if ($isRangeSelection(selection) && selection.isCollapsed()) {
712
+ const node = selection.anchor.getNode();
713
+ const isInLink = $isLinkNode(node) || $isLinkNode(node.getParent());
714
+ // At a caret outside a link there is no text to link, so the URL
715
+ // becomes the link's text. The toggle command would link the whole
716
+ // text node around the caret instead.
717
+ if (!isInLink) {
709
718
  const linkNode = $createLinkNode(url, linkAttributes);
710
719
  linkNode.append($createTextNode(trimmed));
711
720
  selection.insertNodes([linkNode]);
721
+ return;
712
722
  }
713
- });
714
- }
723
+ }
724
+ editor.dispatchCommand(TOGGLE_LINK_COMMAND, {url, ...linkAttributes});
725
+ });
715
726
  return true;
716
727
  },
717
- [editor, hasNewTabLinks, restoreLinkSelection],
728
+ [editor, hasNewTabLinks],
718
729
  );
719
730
 
720
731
  const toggleLink = useCallback(() => {