@astryxdesign/richtext 0.7.1-canary.ff40b78 → 0.7.1

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 (48) hide show
  1. package/CHANGELOG.md +23 -0
  2. package/README.md +47 -57
  3. package/astryx.integration.mjs +1 -1
  4. package/dist/RichTextEditor.d.ts +16 -12
  5. package/dist/RichTextEditor.d.ts.map +1 -1
  6. package/dist/RichTextEditor.js +40 -45
  7. package/dist/RichTextEditorAutoLinkPlugin.d.ts +3 -3
  8. package/dist/RichTextEditorAutoLinkPlugin.js +3 -3
  9. package/dist/RichTextEditorToolbar.d.ts +19 -8
  10. package/dist/RichTextEditorToolbar.d.ts.map +1 -1
  11. package/dist/RichTextEditorToolbar.js +16 -9
  12. package/dist/RichTextView.d.ts +1 -1
  13. package/dist/RichTextView.js +1 -1
  14. package/dist/editorTheme.d.ts.map +1 -1
  15. package/dist/editorTheme.js +15 -4
  16. package/dist/index.d.ts +10 -5
  17. package/dist/index.d.ts.map +1 -1
  18. package/dist/index.js +9 -4
  19. package/dist/linkUtils.js +1 -1
  20. package/dist/markdownLink.d.ts.map +1 -1
  21. package/dist/markdownLink.js +25 -33
  22. package/dist/markdownListExport.d.ts.map +1 -1
  23. package/dist/markdownListExport.js +4 -1
  24. package/dist/markdownSerializers.d.ts.map +1 -1
  25. package/dist/markdownSerializers.js +2 -3
  26. package/dist/richtext.css +6 -1
  27. package/package.json +34 -36
  28. package/src/RichTextEditor.doc.mjs +3 -3
  29. package/src/RichTextEditor.test.tsx +36 -2
  30. package/src/RichTextEditor.tsx +54 -54
  31. package/src/RichTextEditorAutoLinkPlugin.tsx +3 -3
  32. package/src/RichTextEditorToolbar.tsx +27 -10
  33. package/src/RichTextView.tsx +1 -1
  34. package/src/__tests__/MarkdownParitySandbox.a11y.chromium.spec.ts +40 -23
  35. package/src/__tests__/MarkdownPlugins.a11y.chromium.spec.ts +2 -2
  36. package/src/__tests__/MarkdownPluginsImportExport.a11y.chromium.spec.ts +35 -1
  37. package/src/__tests__/Playground.a11y.chromium.spec.ts +298 -0
  38. package/src/__tests__/RichTextEditorToolbar.a11y.chromium.spec.ts +1 -1
  39. package/src/__tests__/RichTextView.a11y.chromium.spec.ts +1 -1
  40. package/src/__tests__/TableCellSelection.a11y.chromium.spec.ts +379 -0
  41. package/src/editorTheme.ts +53 -3
  42. package/src/index.ts +9 -11
  43. package/src/linkUtils.ts +1 -1
  44. package/src/markdownAngleDestinations.test.ts +29 -0
  45. package/src/markdownLink.ts +29 -35
  46. package/src/markdownListExport.ts +6 -1
  47. package/src/markdownSerializers.ts +2 -3
  48. package/src/publicSurface.test.ts +42 -0
@@ -67,13 +67,20 @@ const editorTheme = stylex.create({
67
67
  paragraph: {
68
68
  // The first block's leading margin would stack with the input inset, so
69
69
  // the first line aligns with TextArea and the empty-editor placeholder.
70
+ // A Markdown plugin block takes this style and is a decorator, so at
71
+ // either edge of the document it sits beside Lexical's zero-size caret
72
+ // boundary element (see `hr`) and takes no outer margin there either.
70
73
  marginBlockStart: {
71
74
  default: TEXT_BLOCK_SPACE,
72
75
  ':first-child': spacingVars['--spacing-0'],
76
+ ':is([data-lexical-decorator-boundary]:first-child + *)':
77
+ spacingVars['--spacing-0'],
73
78
  },
74
79
  marginBlockEnd: {
75
80
  default: TEXT_BLOCK_SPACE,
76
81
  ':last-child': spacingVars['--spacing-0'],
82
+ ':has(+ [data-lexical-decorator-boundary]:last-child)':
83
+ spacingVars['--spacing-0'],
77
84
  },
78
85
  maxWidth: PROSE_MEASURE,
79
86
  // The base reset gives every <p> body weight and primary color; a
@@ -333,7 +340,41 @@ const editorTheme = stylex.create({
333
340
  fontWeight: fontWeightVars['--font-weight-semibold'],
334
341
  color: colorVars['--color-text-secondary'],
335
342
  },
336
- // Thematic breaks, drawn like core Markdown's rule.
343
+ // Cells in a table selection — dragging across cells, Shift+arrow between
344
+ // them, or selecting a row or column — are ringed, for the reason
345
+ // `hrSelected` gives: a person has to see what the next keystroke will
346
+ // replace. Lexical names this class on every highlighted cell and takes it
347
+ // off when the selection goes; left unnamed, a cell inside the selection is
348
+ // painted exactly like one outside it.
349
+ //
350
+ // A ring, not a background wash. A wash sits behind the cell's text and
351
+ // eats into its contrast, and no one token stays clear of the field in
352
+ // every theme: `--color-accent-muted` resolves to the field's own colour
353
+ // under Neutral dark, so the selection would be invisible there, and a
354
+ // 10% accent tint drops Neutral dark's header text from 5.65:1 to 4.25:1.
355
+ // The focus ring is drawn for exactly this job — it clears the field by
356
+ // 5.24:1 at worst across every shipped theme and mode — and sits beside
357
+ // the text rather than behind it, so no cell's contrast moves at all.
358
+ // `outline` takes no space, so the document's geometry is untouched
359
+ // (spec:AST-061 FR9), and the inset offset keeps each ring inside its own
360
+ // cell instead of bleeding over the neighbour it is not marking.
361
+ //
362
+ // `RichTextView` and a read-only editor ring a dragged cell selection too:
363
+ // Lexical's table pointer handler does not ask whether the surface is
364
+ // editable, and a person selects cells in a read surface to copy them, so
365
+ // seeing the selection is as right there. Both surfaces draw the same ring
366
+ // and neither moves a block, so they still render the same document the
367
+ // same way.
368
+ tableCellSelected: {
369
+ outlineWidth: focusVars['--focus-outline-width'],
370
+ outlineStyle: focusVars['--focus-outline-style'],
371
+ outlineColor: focusVars['--focus-outline-color'],
372
+ outlineOffset: `calc(-1 * ${focusVars['--focus-outline-width']})`,
373
+ },
374
+ // Thematic breaks, drawn like core Markdown's rule. A rule is a decorator,
375
+ // and Lexical puts a zero-size caret boundary element beside a decorator at
376
+ // either edge of the document, so a rule there is not literally the first
377
+ // or last child; it still takes no outer margin.
337
378
  hr: {
338
379
  borderWidth: 0,
339
380
  borderTopWidth: borderVars['--border-width'],
@@ -342,10 +383,14 @@ const editorTheme = stylex.create({
342
383
  marginBlockStart: {
343
384
  default: RULE_SPACE,
344
385
  ':first-child': spacingVars['--spacing-0'],
386
+ ':is([data-lexical-decorator-boundary]:first-child + *)':
387
+ spacingVars['--spacing-0'],
345
388
  },
346
389
  marginBlockEnd: {
347
390
  default: RULE_SPACE,
348
391
  ':last-child': spacingVars['--spacing-0'],
392
+ ':has(+ [data-lexical-decorator-boundary]:last-child)':
393
+ spacingVars['--spacing-0'],
349
394
  },
350
395
  },
351
396
  // A selected rule (click, or arrow onto it) or Markdown plugin node (click)
@@ -396,9 +441,11 @@ const editorTheme = stylex.create({
396
441
  default: WIDE_BLOCK_SPACE,
397
442
  ':last-child': spacingVars['--spacing-0'],
398
443
  },
444
+ // Long lines wrap exactly as core CodeBlock's wrapped lines do, so a code
445
+ // block is as tall here as in core Markdown (spec:AST-061 FR5, DEC-9).
399
446
  whiteSpace: 'pre-wrap',
400
- // An unbroken token wider than the block (a long URL or hash) wraps too.
401
- overflowWrap: 'anywhere',
447
+ wordBreak: 'break-all',
448
+ overflowWrap: 'break-word',
402
449
  },
403
450
  });
404
451
 
@@ -465,6 +512,9 @@ export function sharedEditorTheme(): EditorThemeClasses {
465
512
  tableRow: block(editorTheme.tableRow),
466
513
  tableCell: block(editorTheme.tableCell),
467
514
  tableCellHeader: block(editorTheme.tableCellHeader),
515
+ // Added on top of the cell's own class by Lexical while the cell is in a
516
+ // table selection, so it carries no direction of its own.
517
+ tableCellSelected: className(editorTheme.tableCellSelected),
468
518
  tableScrollableWrapper: block(editorTheme.tableScrollableWrapper),
469
519
  };
470
520
  }
package/src/index.ts CHANGED
@@ -10,8 +10,14 @@
10
10
  * Lexical-based rich text editor and viewer, promoted out of
11
11
  * @astryxdesign/lab so it can be canaried independently.
12
12
  *
13
- * SYNC: When modified, update this header, /packages/richtext/README.md, and
14
- * the editor component doc files.
13
+ * Exports only what a caller builds with (spec:AST-061/DEC-8): the generated
14
+ * editor theme classes and the autolink regular expressions stay internal,
15
+ * and Lexical's `Transformer` type comes from `@lexical/markdown`.
16
+ * `publicSurface.test.ts` holds the runtime list.
17
+ *
18
+ * SYNC: When modified, update this header, /packages/richtext/README.md,
19
+ * /packages/richtext/src/publicSurface.test.ts, and the editor component doc
20
+ * files.
15
21
  */
16
22
 
17
23
  export {RichTextEditor} from './RichTextEditor';
@@ -21,14 +27,11 @@ export type {
21
27
  RichTextEditorStatus,
22
28
  RichTextEditorStatusType,
23
29
  RichTextEditorSize,
24
- Transformer,
25
30
  } from './RichTextEditor';
26
31
 
27
32
  export {RichTextView} from './RichTextView';
28
33
  export type {RichTextViewProps} from './RichTextView';
29
34
 
30
- export {sharedEditorTheme} from './editorTheme';
31
-
32
35
  export {
33
36
  markdownToEditorStateJSON,
34
37
  editorStateJSONToMarkdown,
@@ -53,9 +56,4 @@ export {
53
56
  } from './RichTextEditorAutoLinkPlugin';
54
57
  export type {RichTextEditorAutoLinkPluginProps} from './RichTextEditorAutoLinkPlugin';
55
58
 
56
- export {
57
- sanitizeUrl,
58
- validateUrl,
59
- URL_MATCHER,
60
- EMAIL_MATCHER,
61
- } from './linkUtils';
59
+ export {sanitizeUrl, validateUrl} from './linkUtils';
package/src/linkUtils.ts CHANGED
@@ -5,7 +5,7 @@
5
5
  * @input Uses only the DOM URL constructor (guarded via try/catch for SSR/edge).
6
6
  * @output Exports `sanitizeUrl`, `validateUrl`, and the URL/email matcher regexes
7
7
  * used by the AutoLink wiring.
8
- * @position Experimental (lab) helper shared by RichTextEditorToolbar (link
8
+ * @position @astryxdesign/richtext helper shared by RichTextEditorToolbar (link
9
9
  * button) and the AutoLink matchers in RichTextEditor. Kept dependency-free so
10
10
  * both can import it without pulling in extra `@lexical/*` surface.
11
11
  *
@@ -164,6 +164,35 @@ describe('angle-bracket destination edges', () => {
164
164
  expect(richLinks(exported)).toEqual([{url: '<b', title: null}]);
165
165
  });
166
166
 
167
+ it('writes a reference-like `&` in an angle destination and its title so it reads back', () => {
168
+ const editor = newEditor();
169
+ editor.update(
170
+ () => {
171
+ $getRoot()
172
+ .clear()
173
+ .append(
174
+ $createParagraphNode().append(
175
+ $createLinkNode('https://e.com/a b&amp;c', {
176
+ title: 'x &copy; y',
177
+ }).append($createTextNode('x')),
178
+ ),
179
+ );
180
+ },
181
+ {discrete: true},
182
+ );
183
+ const exported = editor
184
+ .getEditorState()
185
+ .read(() => $exportMarkdownKeepingSource([...DEFAULT_TRANSFORMERS]));
186
+ // Import decodes character references in a destination and a title, so
187
+ // the `&` that would start one is written as a reference itself.
188
+ expect(exported).toBe(
189
+ '[x](<https://e.com/a b&#38;amp;c> "x &#38;copy; y")',
190
+ );
191
+ expect(richLinks(exported)).toEqual([
192
+ {url: 'https://e.com/a b&amp;c', title: 'x &copy; y'},
193
+ ]);
194
+ });
195
+
167
196
  it('stores no unsafe address, however an angle destination is written', () => {
168
197
  let state = 40;
169
198
  const random = () => {
@@ -4,17 +4,20 @@
4
4
  * @file markdownLink.ts
5
5
  * @input Uses @lexical/link and @lexical/markdown (LINK).
6
6
  * @output Exports LINK_KEEPING_DESTINATIONS, Lexical's link transformer with
7
- * two changes: it reads an angle-bracket destination (`<https://e.com/a b>`)
8
- * as core Markdown does, without its brackets, and it writes a destination
9
- * so it reads back: one holding a space in angle brackets, and one whose
10
- * parentheses do not balance with each parenthesis escaped.
7
+ * one change on export. Import is Lexical's own, which reads every
8
+ * CommonMark destination and title shape, an angle-bracket destination
9
+ * (`<https://e.com/a b>`) included, and decodes backslash escapes and
10
+ * character references in both. A destination that needs no escape is
11
+ * written by Lexical's export too, which escapes parentheses and an `&`
12
+ * that starts a character reference (`https://e.com/a_(b)` as
13
+ * `https://e.com/a_\(b\)`). One that holds whitespace or opens with `<`
14
+ * is written here in angle brackets, and one with a backslash or
15
+ * unbalanced parentheses with those escaped; both escape a
16
+ * reference-starting `&` as Lexical does, so every link reads back as
17
+ * the same link, whether it was imported or made in the editor
18
+ * (spec:AST-062 FR3).
11
19
  * @position Replaces Lexical's LINK in DEFAULT_TRANSFORMERS and in table
12
- * cells (markdownTable.ts). Import reads balanced parentheses in a
13
- * destination and backslash escapes (markdownCharacterReferences.ts), so a
14
- * destination written this way — `https://e.com/a(b` as
15
- * `https://e.com/a\(b` — reads back as the same link, whether it was
16
- * imported or made in the editor (spec:AST-062 FR3). A balanced
17
- * destination is written as it is.
20
+ * cells (markdownTable.ts).
18
21
  */
19
22
 
20
23
  import {$isAutoLinkNode, $isLinkNode} from '@lexical/link';
@@ -36,6 +39,17 @@ function isBalanced(url: string): boolean {
36
39
  return depth === 0;
37
40
  }
38
41
 
42
+ /**
43
+ * `value` with every `&` that would start a character reference written as
44
+ * `&#38;`, since import decodes references in a destination and a title.
45
+ */
46
+ function escapeCharacterReferences(value: string): string {
47
+ return value.replace(
48
+ /&(?=#\d+;|#[Xx][\dA-Fa-f]+;|[A-Za-z][\dA-Za-z]*;)/g,
49
+ '&#38;',
50
+ );
51
+ }
52
+
39
53
  /** `url` as a Markdown destination that reads back as `url`. */
40
54
  export function markdownDestination(url: string): string {
41
55
  if (/\s/.test(url) || url.startsWith('<')) {
@@ -49,31 +63,10 @@ export function markdownDestination(url: string): string {
49
63
  return url.replace(isBalanced(url) ? /\\/g : /[\\()]/g, '\\$&');
50
64
  }
51
65
 
52
- /**
53
- * Lexical's link pattern, with a destination in angle brackets too: any
54
- * characters but brackets and line endings, spaces included. A destination
55
- * that opens with `<` is an angle-bracket one or no destination at all
56
- * (CommonMark §6.3), so `[a](<b>c>)` stays text, as core reads it.
57
- */
58
- const LINK_WITH_ANGLE_DESTINATION =
59
- /(?:\[(.+?)\])(?:\((?:(<[^<>\n]*>|[^()\s<][^()\s]*)(?:\s"((?:[^"]*\\")*[^"]*)"\s*)?)\))/;
60
-
61
66
  export const LINK_KEEPING_DESTINATIONS: TextMatchTransformer = {
67
+ // Lexical's own import reads every CommonMark destination and title shape,
68
+ // an angle-bracket destination included.
62
69
  ...LINK,
63
- importRegExp: LINK_WITH_ANGLE_DESTINATION,
64
- replace: (textNode, match) => {
65
- const destination = match[2];
66
- if (destination == null || !destination.startsWith('<')) {
67
- return LINK.replace?.(textNode, match);
68
- }
69
- // The link's address is what the angle brackets hold.
70
- const withoutBrackets = Object.assign([...match], {
71
- index: match.index,
72
- input: match.input,
73
- }) as RegExpMatchArray;
74
- withoutBrackets[2] = destination.slice(1, -1);
75
- return LINK.replace?.(textNode, withoutBrackets);
76
- },
77
70
  export: (node, exportChildren, exportFormat) => {
78
71
  if (!$isLinkNode(node) || $isAutoLinkNode(node)) {
79
72
  return null;
@@ -84,9 +77,10 @@ export const LINK_KEEPING_DESTINATIONS: TextMatchTransformer = {
84
77
  return LINK.export?.(node, exportChildren, exportFormat) ?? null;
85
78
  }
86
79
  const text = exportChildren(node);
80
+ const written = escapeCharacterReferences(destination);
87
81
  const title = node.getTitle();
88
82
  return title == null || title === ''
89
- ? `[${text}](${destination})`
90
- : `[${text}](${destination} "${title.replace(/([\\"])/g, '\\$1')}")`;
83
+ ? `[${text}](${written})`
84
+ : `[${text}](${written} "${escapeCharacterReferences(title).replace(/([\\"])/g, '\\$1')}")`;
91
85
  },
92
86
  };
@@ -28,7 +28,12 @@ import {$getTaskState} from './markdownTaskList';
28
28
 
29
29
  /** The bullet Lexical recorded for a list on import (`-`, `*`, or `+`). */
30
30
  function bulletOf(list: ListNode): string {
31
- const serialized = list.exportJSON() as {$?: {mdListMarker?: unknown}};
31
+ // The export may hand over a view of the list (an object inheriting from
32
+ // the node, see markdownSource.ts); Lexical validates the fields of the
33
+ // node it serializes, so read the recorded state from the node itself.
34
+ const serialized = list.getLatest().exportJSON() as {
35
+ $?: {mdListMarker?: unknown};
36
+ };
32
37
  const marker = serialized.$?.mdListMarker;
33
38
  return marker === '*' || marker === '+' ? marker : '-';
34
39
  }
@@ -17,9 +17,8 @@
17
17
  * - /packages/richtext/src/RichTextEditor.test.tsx (tests)
18
18
  *
19
19
  * NOTE: `@lexical/headless`, `@lexical/markdown`, and `lexical` are OPTIONAL
20
- * peer dependencies (this is a canary lab module). Install them to use these
21
- * helpers. `@lexical/headless` runs Lexical without a DOM, so these work in
22
- * Node / SSR contexts.
20
+ * peer dependencies. Install them to use these helpers. `@lexical/headless`
21
+ * runs Lexical without a DOM, so these work in Node / SSR contexts.
23
22
  */
24
23
 
25
24
  import {createHeadlessEditor} from '@lexical/headless';
@@ -0,0 +1,42 @@
1
+ // Copyright (c) Meta Platforms, Inc. and affiliates.
2
+
3
+ /**
4
+ * @file publicSurface.test.ts
5
+ * @input The package entry, packages/richtext/src/index.ts
6
+ * @output Pins the runtime exports of @astryxdesign/richtext to the list
7
+ * spec:AST-061/DEC-8 admits, and the internals it keeps out of the entry.
8
+ * @position Package-level contract test.
9
+ */
10
+
11
+ import {describe, expect, it} from 'vitest';
12
+ // @ts-expect-error Transformer is Lexical's type; import it from @lexical/markdown.
13
+ import type {Transformer as _NotReExported} from './index';
14
+ import * as richtext from './index';
15
+
16
+ describe('@astryxdesign/richtext public surface', () => {
17
+ it('exports exactly what a caller builds with', () => {
18
+ expect(Object.keys(richtext).sort()).toEqual(
19
+ [
20
+ 'DEFAULT_LINK_MATCHERS',
21
+ 'NEW_TAB_LINK_ATTRIBUTES',
22
+ 'RICHTEXT_ICON_KEYS',
23
+ 'RichTextEditor',
24
+ 'RichTextEditorAutoLinkPlugin',
25
+ 'RichTextEditorToolbar',
26
+ 'RichTextExtensionError',
27
+ 'RichTextView',
28
+ 'createRichTextExtension',
29
+ 'editorStateJSONToMarkdown',
30
+ 'markdownToEditorStateJSON',
31
+ 'sanitizeUrl',
32
+ 'validateUrl',
33
+ ].sort(),
34
+ );
35
+ });
36
+
37
+ it('keeps the editor theme classes and autolink patterns internal', () => {
38
+ for (const name of ['sharedEditorTheme', 'URL_MATCHER', 'EMAIL_MATCHER']) {
39
+ expect(richtext).not.toHaveProperty(name);
40
+ }
41
+ });
42
+ });