@astryxdesign/richtext 0.7.11-canary.ed0adf3 → 0.7.11

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.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,22 @@
1
1
  # @astryxdesign/richtext
2
2
 
3
+ # 0.7.11
4
+
5
+ #### Fixes
6
+
7
+ - In `RichTextEditor`, Backspace at the very start of a code block turns it into a paragraph holding its lines, with the caret at its start. It used to merge the code into the block above, so a heading above took the code in, and the Markdown read back as a longer heading and a stray paragraph. A code block with nothing above it, which already became a paragraph, now keeps its lines when its Markdown is read back. (#8581)
8
+ - RichText: an image in Markdown, inline (`![alt](url)`) or by reference (`![alt][ref]`), comes back exactly as written after you edit the text around it; a shortcut reference image (`![alt]`) followed directly by a link is written in its collapsed form (`![alt][]`), so the link stays a link. Before, an edit in an image's paragraph could escape its brackets (`!\[\](…)`), so other Markdown readers saw text instead of an image. RichText has no image node: an image shows as its Markdown source, one token that Backspace deletes whole and autolinking leaves alone. A caret placed inside it moves to its edge, so typing adds text beside the image instead of replacing it. Copying it, or splitting or joining its paragraph, keeps the source. (#10356)
9
+
10
+ #### Contributors
11
+
12
+ Thanks to everyone who contributed to this release:
13
+
14
+ - @cixzhang
15
+ - @emilyhorsman
16
+ - @ernestt
17
+
18
+ ---
19
+
3
20
  # 0.7.10
4
21
 
5
22
  ---
package/README.md CHANGED
@@ -69,6 +69,11 @@ so your theme's token overrides (`@layer astryx-theme`) win over it, as long
69
69
  as Core's `reset.css` (or a `@layer reset, astryx-base, astryx-theme;` line,
70
70
  if your app has its own reset) loads first.
71
71
 
72
+ ## Editing code blocks
73
+
74
+ Backspace at the very start of a code block turns it into a paragraph of its
75
+ lines, instead of merging the code into the block above.
76
+
72
77
  ## Examples
73
78
 
74
79
  Interactive examples live in Storybook under **RichText/RichTextEditor**
@@ -0,0 +1,2 @@
1
+ export declare function CodeBackspacePlugin(): null;
2
+ //# sourceMappingURL=CodeBackspacePlugin.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"CodeBackspacePlugin.d.ts","sourceRoot":"","sources":["../src/CodeBackspacePlugin.tsx"],"names":[],"mappings":"AAsEA,wBAAgB,mBAAmB,IAAI,IAAI,CAyB1C"}
@@ -0,0 +1,70 @@
1
+ // Copyright (c) Meta Platforms, Inc. and affiliates.
2
+
3
+ 'use client';
4
+
5
+ /**
6
+ * @file CodeBackspacePlugin.tsx
7
+ * @input Uses @lexical/react (composer context), @lexical/code (CodeNode), and
8
+ * lexical.
9
+ * @output Exports CodeBackspacePlugin, which makes Backspace at the very start
10
+ * of a code block turn the block into a paragraph holding its lines.
11
+ * @position Rendered by RichTextEditor; always on, not exported. Lexical's
12
+ * own Backspace there merges the code into the block above, and a heading
13
+ * above reads back as a longer heading and a stray paragraph.
14
+ */
15
+ import { useEffect } from 'react';
16
+ import { useLexicalComposerContext } from '@lexical/react/LexicalComposerContext';
17
+ import { $isCodeNode } from '@lexical/code';
18
+ import { $createLineBreakNode, $createParagraphNode, $createTextNode, $getSelection, $isRangeSelection, COMMAND_PRIORITY_LOW, DELETE_CHARACTER_COMMAND } from 'lexical';
19
+
20
+ /** Start is offset 0 of the block or of its first child, not the start of a later line. */
21
+ function $codeBlockAtCaretStart() {
22
+ const selection = $getSelection();
23
+ if (!$isRangeSelection(selection) || !selection.isCollapsed() || selection.anchor.offset !== 0) {
24
+ return null;
25
+ }
26
+ const node = selection.anchor.getNode();
27
+ const code = $isCodeNode(node) ? node : node.getParent();
28
+ if (!$isCodeNode(code)) {
29
+ return null;
30
+ }
31
+ return node.is(code) || node.is(code.getFirstChild()) ? code : null;
32
+ }
33
+
34
+ /**
35
+ * A newline inside text exports as a soft break, and the lines read back as
36
+ * one. A line break node exports as a hard break. Empty lines at the end of
37
+ * the code are dropped: a hard break that ends a paragraph reads back as a
38
+ * literal backslash.
39
+ */
40
+ function $paragraphOfLines(code) {
41
+ const paragraph = $createParagraphNode();
42
+ code.getTextContent().replace(/\n+$/, '').split('\n').forEach((line, index) => {
43
+ if (index > 0) {
44
+ paragraph.append($createLineBreakNode());
45
+ }
46
+ if (line !== '') {
47
+ paragraph.append($createTextNode(line));
48
+ }
49
+ });
50
+ return paragraph;
51
+ }
52
+ export function CodeBackspacePlugin() {
53
+ const [editor] = useLexicalComposerContext();
54
+ useEffect(() => editor.registerCommand(DELETE_CHARACTER_COMMAND, isBackward => {
55
+ if (!isBackward || !editor.isEditable()) {
56
+ return false;
57
+ }
58
+ const code = $codeBlockAtCaretStart();
59
+ if (code == null) {
60
+ return false;
61
+ }
62
+ const paragraph = $paragraphOfLines(code);
63
+ code.replace(paragraph);
64
+ paragraph.selectStart();
65
+ return true;
66
+ },
67
+ // Ahead of the rich-text deletion, which would do the merge.
68
+ COMMAND_PRIORITY_LOW), [editor]);
69
+ return null;
70
+ }
@@ -1 +1 @@
1
- {"version":3,"file":"RichTextEditor.d.ts","sourceRoot":"","sources":["../src/RichTextEditor.tsx"],"names":[],"mappings":"AAIA;;;;;;;;;;;;;;;;;;;;GAoBG;AAEH,OAAO,EAOL,KAAK,SAAS,EACd,KAAK,GAAG,EACT,MAAM,OAAO,CAAC;AASf,OAAO,EAML,KAAK,kBAAkB,EACxB,MAAM,0BAA0B,CAAC;AAClC,OAAO,KAAK,EAAC,SAAS,EAAC,MAAM,oBAAoB,CAAC;AAIlD,OAAO,EAIL,KAAK,SAAS,EACf,MAAM,0BAA0B,CAAC;AAqBlC,OAAO,KAAK,EAAC,yBAAyB,EAAC,MAAM,sBAAsB,CAAC;AACpE,OAAO,KAAK,EAAC,YAAY,EAAC,MAAM,8BAA8B,CAAC;AAI/D,OAAO,EAAC,KAAK,WAAW,EAAC,MAAM,mBAAmB,CAAC;AAKnD,OAAO,EASL,KAAK,WAAW,EAChB,KAAK,KAAK,EACV,KAAK,aAAa,EAClB,KAAK,WAAW,EAEjB,MAAM,SAAS,CAAC;AAwJjB,MAAM,MAAM,wBAAwB,GAAG,SAAS,GAAG,OAAO,GAAG,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AAC;IAC/B;;;;;;;;;;;;;;;;;OAiBG;IACH,YAAY,CAAC,EAAE,aAAa,CAAC,WAAW,CAAC,CAAC;IAC1C;;;;;;;OAOG;IACH,kBAAkB,CAAC,EAAE,aAAa,CAAC,yBAAyB,CAAC,CAAC;IAC9D;;;;;;OAMG;IACH,aAAa,CAAC,EAAE,aAAa,CAAC,YAAY,CAAC,CAAC;IAC5C,yEAAyE;IACzE,YAAY,CAAC,EAAE,OAAO,CAAC;IACvB;;;;;;;OAOG;IACH,aAAa,CAAC,EAAE,MAAM,CAAC;IACvB;;;;;;OAMG;IACH,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB;;;OAGG;IACH,SAAS,CAAC,EAAE,MAAM,CAAC;CACpB;AAED;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AACH,eAAO,MAAM,cAAc;2ZAiCxB,mBAAmB;;CA0PrB,CAAC"}
@@ -49,6 +49,7 @@ import { TablePlugin } from '@lexical/react/LexicalTablePlugin';
49
49
  import { HorizontalRuleExtension } from '@lexical/extension';
50
50
  import { TextSemanticsExtension } from "./textSemantics.js";
51
51
  import { TableColumnFloorPlugin } from "./TableColumnFloorPlugin.js";
52
+ import { CodeBackspacePlugin } from "./CodeBackspacePlugin.js";
52
53
  import { CodeBlockHeaderPlugin } from "./CodeBlockHeaderPlugin.js";
53
54
  import { CodeSyntaxPlugin } from "./CodeSyntaxPlugin.js";
54
55
  import { MarkdownExtensionsPlugin } from "./MarkdownExtensionsPlugin.js";
@@ -360,7 +361,7 @@ export const RichTextEditor = function RichTextEditor({
360
361
  codeLanguages: codeLanguages
361
362
  }), /*#__PURE__*/_jsx(CodeSyntaxPlugin, {
362
363
  codeLanguages: codeLanguages
363
- }), markdownExtensions != null && markdownExtensions.length > 0 ? /*#__PURE__*/_jsx(MarkdownExtensionsPlugin, {
364
+ }), /*#__PURE__*/_jsx(CodeBackspacePlugin, {}), markdownExtensions != null && markdownExtensions.length > 0 ? /*#__PURE__*/_jsx(MarkdownExtensionsPlugin, {
364
365
  extensions: markdownExtensions,
365
366
  transformers: markdownTransformers
366
367
  }) : null, /*#__PURE__*/_jsx(TabIndentationPlugin, {}), /*#__PURE__*/_jsx(TabFocusEscapePlugin, {}), /*#__PURE__*/_jsx(SelectAllScopePlugin, {}), /*#__PURE__*/_jsx(ImageSourceCaretPlugin, {}), hasMarkdownShortcuts && /*#__PURE__*/_jsx(MarkdownShortcutPlugin, {
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@astryxdesign/richtext",
3
3
  "displayName": "Rich Text",
4
- "version": "0.7.11-canary.ed0adf3",
4
+ "version": "0.7.11",
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.11-canary.ed0adf3",
69
+ "@astryxdesign/core": "0.7.11",
70
70
  "react": ">=19.0.0",
71
71
  "react-dom": ">=19.0.0",
72
72
  "lexical": "^0.52.0",
@@ -125,8 +125,6 @@
125
125
  }
126
126
  },
127
127
  "devDependencies": {
128
- "@astryxdesign/a11y-spec": "0.7.11-canary.ed0adf3",
129
- "@astryxdesign/cli": "0.7.11-canary.ed0adf3",
130
128
  "@babel/cli": "^7.29.7",
131
129
  "@babel/core": "^7.29.7",
132
130
  "@babel/preset-react": "^7.29.7",
@@ -145,7 +143,9 @@
145
143
  "@lexical/utils": "^0.52.0",
146
144
  "@stylexjs/babel-plugin": "^0.19.0",
147
145
  "@testing-library/react": "^16.3.2",
148
- "lexical": "^0.52.0"
146
+ "lexical": "^0.52.0",
147
+ "@astryxdesign/a11y-spec": "0.0.0",
148
+ "@astryxdesign/cli": "0.7.11"
149
149
  },
150
150
  "scripts": {
151
151
  "api:report": "node ../../scripts/api-report/cli.mjs --package .",
@@ -0,0 +1,96 @@
1
+ // Copyright (c) Meta Platforms, Inc. and affiliates.
2
+
3
+ 'use client';
4
+
5
+ /**
6
+ * @file CodeBackspacePlugin.tsx
7
+ * @input Uses @lexical/react (composer context), @lexical/code (CodeNode), and
8
+ * lexical.
9
+ * @output Exports CodeBackspacePlugin, which makes Backspace at the very start
10
+ * of a code block turn the block into a paragraph holding its lines.
11
+ * @position Rendered by RichTextEditor; always on, not exported. Lexical's
12
+ * own Backspace there merges the code into the block above, and a heading
13
+ * above reads back as a longer heading and a stray paragraph.
14
+ */
15
+
16
+ import {useEffect} from 'react';
17
+ import {useLexicalComposerContext} from '@lexical/react/LexicalComposerContext';
18
+ import {$isCodeNode, type CodeNode} from '@lexical/code';
19
+ import {
20
+ $createLineBreakNode,
21
+ $createParagraphNode,
22
+ $createTextNode,
23
+ $getSelection,
24
+ $isRangeSelection,
25
+ COMMAND_PRIORITY_LOW,
26
+ DELETE_CHARACTER_COMMAND,
27
+ type ParagraphNode,
28
+ } from 'lexical';
29
+
30
+ /** Start is offset 0 of the block or of its first child, not the start of a later line. */
31
+ function $codeBlockAtCaretStart(): CodeNode | null {
32
+ const selection = $getSelection();
33
+ if (
34
+ !$isRangeSelection(selection) ||
35
+ !selection.isCollapsed() ||
36
+ selection.anchor.offset !== 0
37
+ ) {
38
+ return null;
39
+ }
40
+ const node = selection.anchor.getNode();
41
+ const code = $isCodeNode(node) ? node : node.getParent();
42
+ if (!$isCodeNode(code)) {
43
+ return null;
44
+ }
45
+ return node.is(code) || node.is(code.getFirstChild()) ? code : null;
46
+ }
47
+
48
+ /**
49
+ * A newline inside text exports as a soft break, and the lines read back as
50
+ * one. A line break node exports as a hard break. Empty lines at the end of
51
+ * the code are dropped: a hard break that ends a paragraph reads back as a
52
+ * literal backslash.
53
+ */
54
+ function $paragraphOfLines(code: CodeNode): ParagraphNode {
55
+ const paragraph = $createParagraphNode();
56
+ code
57
+ .getTextContent()
58
+ .replace(/\n+$/, '')
59
+ .split('\n')
60
+ .forEach((line, index) => {
61
+ if (index > 0) {
62
+ paragraph.append($createLineBreakNode());
63
+ }
64
+ if (line !== '') {
65
+ paragraph.append($createTextNode(line));
66
+ }
67
+ });
68
+ return paragraph;
69
+ }
70
+
71
+ export function CodeBackspacePlugin(): null {
72
+ const [editor] = useLexicalComposerContext();
73
+ useEffect(
74
+ () =>
75
+ editor.registerCommand(
76
+ DELETE_CHARACTER_COMMAND,
77
+ isBackward => {
78
+ if (!isBackward || !editor.isEditable()) {
79
+ return false;
80
+ }
81
+ const code = $codeBlockAtCaretStart();
82
+ if (code == null) {
83
+ return false;
84
+ }
85
+ const paragraph = $paragraphOfLines(code);
86
+ code.replace(paragraph);
87
+ paragraph.selectStart();
88
+ return true;
89
+ },
90
+ // Ahead of the rich-text deletion, which would do the merge.
91
+ COMMAND_PRIORITY_LOW,
92
+ ),
93
+ [editor],
94
+ );
95
+ return null;
96
+ }
@@ -238,6 +238,11 @@ export const docs = {
238
238
  description:
239
239
  'GFM pipe tables import, edit, and export by default. The first row renders as header cells, delimiter-row colons set each column alignment, rows shorter than the widest row gain empty cells, and a wide table scrolls inside its own wrapper. Arrow keys move between cells; Tab keeps its editor meaning, so Escape then Tab still leaves the editor. Tables export in the canonical form: outer pipes and three-dash delimiters. RichTextView renders the same tables. Install @lexical/table with the other lexical peers.',
240
240
  },
241
+ {
242
+ guidance: true,
243
+ description:
244
+ 'Backspace at the very start of a code block turns it into a paragraph holding its lines, each on its own line, with the caret at its start. The code never merges into the block above, so a heading above keeps its own text. Backspace anywhere else in a code block deletes as usual.',
245
+ },
241
246
  {
242
247
  guidance: true,
243
248
  description:
@@ -77,6 +77,7 @@ import {TablePlugin} from '@lexical/react/LexicalTablePlugin';
77
77
  import {HorizontalRuleExtension} from '@lexical/extension';
78
78
  import {TextSemanticsExtension} from './textSemantics';
79
79
  import {TableColumnFloorPlugin} from './TableColumnFloorPlugin';
80
+ import {CodeBackspacePlugin} from './CodeBackspacePlugin';
80
81
  import {CodeBlockHeaderPlugin} from './CodeBlockHeaderPlugin';
81
82
  import {CodeSyntaxPlugin} from './CodeSyntaxPlugin';
82
83
  import {MarkdownExtensionsPlugin} from './MarkdownExtensionsPlugin';
@@ -724,6 +725,9 @@ export const RichTextEditor = function RichTextEditor({
724
725
  <TableColumnFloorPlugin />
725
726
  <CodeBlockHeaderPlugin codeLanguages={codeLanguages} />
726
727
  <CodeSyntaxPlugin codeLanguages={codeLanguages} />
728
+ {/* Backspace at the very start of a code block makes it a
729
+ paragraph, instead of merging it into the block above. */}
730
+ <CodeBackspacePlugin />
727
731
  {markdownExtensions != null && markdownExtensions.length > 0 ? (
728
732
  <MarkdownExtensionsPlugin
729
733
  extensions={markdownExtensions}