@astryxdesign/richtext 0.7.17-canary.gffd1fc0 → 0.7.17
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 +33 -0
- package/README.md +6 -0
- package/dist/RichTextEditor.d.ts.map +1 -1
- package/dist/RichTextEditor.js +3 -2
- package/dist/RichTextView.d.ts.map +1 -1
- package/dist/RichTextView.js +3 -1
- package/dist/editorTheme.d.ts.map +1 -1
- package/dist/editorTheme.js +48 -24
- package/dist/internal/BlockDirectionPlugin.d.ts +14 -0
- package/dist/internal/BlockDirectionPlugin.d.ts.map +1 -0
- package/dist/internal/BlockDirectionPlugin.js +283 -0
- package/dist/internal/CodeBlockHeaderPlugin.d.ts.map +1 -1
- package/dist/internal/CodeBlockHeaderPlugin.js +131 -12
- package/dist/internal/firstStrongDirection.d.ts +8 -0
- package/dist/internal/firstStrongDirection.d.ts.map +1 -0
- package/dist/internal/firstStrongDirection.js +104 -0
- package/dist/richtext.css +11 -8
- package/package.json +5 -5
- package/src/RichTextEditor.tsx +6 -0
- package/src/RichTextView.tsx +6 -0
- package/src/editorTheme.ts +85 -31
- package/src/internal/BlockDirectionPlugin.tsx +326 -0
- package/src/internal/CodeBlockHeaderPlugin.tsx +165 -16
- package/src/internal/firstStrongDirection.ts +111 -0
package/src/editorTheme.ts
CHANGED
|
@@ -50,24 +50,57 @@ const LIST_ROW_FLUSH = `calc(${spacingVars['--spacing-0']})`;
|
|
|
50
50
|
const NESTED_LIST_SPACE = `calc(${spacingVars['--spacing-2']})`;
|
|
51
51
|
const RULE_SPACE = `calc(${spacingVars['--spacing-6']})`;
|
|
52
52
|
const CODE_SPACE = `calc(${spacingVars['--spacing-3']})`;
|
|
53
|
-
// Core CodeBlock's header
|
|
54
|
-
//
|
|
55
|
-
|
|
53
|
+
// Core CodeBlock's header overlaps the code's 12px top padding by 8px. So the
|
|
54
|
+
// code starts 4px below the header. CodeBlockHeaderPlugin writes the header's
|
|
55
|
+
// drawn height; one row is the fallback.
|
|
56
|
+
const CODE_HEADER_SPACE = `calc(var(--_richtext-code-header-block-size, calc(2 * ${spacingVars['--spacing-2']} + ${sizeVars['--size-element-sm']})) + ${spacingVars['--spacing-3']} - ${spacingVars['--spacing-2']})`;
|
|
56
57
|
// A block that names no language has no header: core CodeBlock puts its copy
|
|
57
58
|
// button in a column of its own at the inline end (8px, a small control, 8px),
|
|
58
59
|
// beside the code rather than over it, so the code ends that much earlier.
|
|
59
60
|
const CODE_CORNER_SPACE = `calc(${spacingVars['--spacing-4']} + ${sizeVars['--size-element-sm']} + 2 * ${spacingVars['--spacing-2']})`;
|
|
60
61
|
|
|
61
62
|
const editorTheme = stylex.create({
|
|
62
|
-
//
|
|
63
|
-
// block
|
|
64
|
-
//
|
|
65
|
-
//
|
|
66
|
-
//
|
|
67
|
-
//
|
|
68
|
-
|
|
69
|
-
|
|
63
|
+
// BlockDirectionPlugin writes a prose block's own direction onto it, so a
|
|
64
|
+
// block saying `ltr` or `rtl` keeps the UA rule and its caret, alignment,
|
|
65
|
+
// quote bar and insets all follow its text. `auto` means the block has no
|
|
66
|
+
// direction of its own — digits, punctuation, an image, nothing at all —
|
|
67
|
+
// and it follows the document, so an empty paragraph keeps its caret on a
|
|
68
|
+
// right-to-left document's start edge.
|
|
69
|
+
blockDirection: {
|
|
70
|
+
direction: {
|
|
71
|
+
default: null,
|
|
72
|
+
// A block whose own text decided nothing reads the way the document
|
|
73
|
+
// reads, at any depth, as `Markdown` resolves one against the
|
|
74
|
+
// provider. The surface root sets that direction from `:dir()`, so it
|
|
75
|
+
// arrives through a list or table that reads the other way, where
|
|
76
|
+
// `inherit` would hand the block its container's direction. Lexical
|
|
77
|
+
// leaves such a block at `dir="auto"` at the top level and writes no
|
|
78
|
+
// attribute below it, so both spellings mean the same thing; a block
|
|
79
|
+
// that did decide carries `ltr` or `rtl` and is left alone.
|
|
80
|
+
':is([dir="auto"])': 'var(--_richtext-document-direction, inherit)',
|
|
81
|
+
':not([dir])': 'var(--_richtext-document-direction, inherit)',
|
|
82
|
+
},
|
|
83
|
+
},
|
|
84
|
+
// A container orders its contents in its parent's direction whatever they
|
|
85
|
+
// hold: the document's at the top level, its wrapper item's when nested.
|
|
86
|
+
// It takes that direction unconditionally, so a `dir` arriving with a
|
|
87
|
+
// paste or an older stored document cannot turn a list's markers or a
|
|
88
|
+
// table's columns around.
|
|
89
|
+
surfaceDirection: {
|
|
90
|
+
direction: 'inherit',
|
|
91
|
+
},
|
|
92
|
+
// A fenced code block is several lines under one element, and Lexical
|
|
93
|
+
// gives it no per-line node that could carry a direction. `plaintext`
|
|
94
|
+
// puts each line in its own bidi paragraph taking the direction of its
|
|
95
|
+
// own first strong character, so a Hebrew comment line reads
|
|
96
|
+
// right-to-left while `const x = 1;` keeps its semicolon at the end.
|
|
97
|
+
//
|
|
98
|
+
// Core Markdown reaches the same rule a different way: it renders fences
|
|
99
|
+
// through CodeBlock, which puts each line's code in its own `dir="auto"`
|
|
100
|
+
// element. RichText has no per-line node to carry one.
|
|
101
|
+
codeDirection: {
|
|
70
102
|
direction: 'inherit',
|
|
103
|
+
unicodeBidi: 'plaintext',
|
|
71
104
|
},
|
|
72
105
|
paragraph: {
|
|
73
106
|
// The first block's leading margin would stack with the input inset, so
|
|
@@ -210,7 +243,14 @@ const editorTheme = stylex.create({
|
|
|
210
243
|
':last-child': spacingVars['--spacing-0'],
|
|
211
244
|
},
|
|
212
245
|
maxWidth: PROSE_MEASURE,
|
|
213
|
-
|
|
246
|
+
// The inset lives on the item, not here. An `outside` marker is drawn
|
|
247
|
+
// beyond its item's own start edge, which is the opposite edge from the
|
|
248
|
+
// list's when the two directions disagree; an inset held by the list
|
|
249
|
+
// would reserve room on the side the marker is not on and let it escape
|
|
250
|
+
// the field. Held by the item it is always on the marker's side, and it
|
|
251
|
+
// costs the same measure either way, so a list whose items all agree
|
|
252
|
+
// with it wraps exactly where core Markdown wraps.
|
|
253
|
+
paddingInline: spacingVars['--spacing-0'],
|
|
214
254
|
listStylePosition: 'outside',
|
|
215
255
|
},
|
|
216
256
|
ol: {
|
|
@@ -223,7 +263,9 @@ const editorTheme = stylex.create({
|
|
|
223
263
|
':last-child': spacingVars['--spacing-0'],
|
|
224
264
|
},
|
|
225
265
|
maxWidth: PROSE_MEASURE,
|
|
226
|
-
|
|
266
|
+
// The inset is on each item (see `ul`), so an outside marker always
|
|
267
|
+
// has room on its item's own start side.
|
|
268
|
+
paddingInline: spacingVars['--spacing-0'],
|
|
227
269
|
listStylePosition: 'outside',
|
|
228
270
|
},
|
|
229
271
|
// Markers by depth (spec:AST-061 DEC-6): a list nested inside n lists, of
|
|
@@ -268,12 +310,13 @@ const editorTheme = stylex.create({
|
|
|
268
310
|
':has(ol)': 'none',
|
|
269
311
|
':is([data-richtext-task])': 'none',
|
|
270
312
|
},
|
|
271
|
-
// A task item's text starts 28px in
|
|
272
|
-
//
|
|
273
|
-
// the item cancels the list's bullet indent and pads for the checkbox.
|
|
313
|
+
// A task item's text starts 28px in, after core's small checkbox (20px)
|
|
314
|
+
// and its 8px gap, like core Markdown's task lists.
|
|
274
315
|
marginInlineStart: {
|
|
275
|
-
default:
|
|
276
|
-
|
|
316
|
+
default: spacingVars['--spacing-6'],
|
|
317
|
+
// A task item draws a checkbox inside its own padding instead of a
|
|
318
|
+
// marker, so it needs no room outside itself.
|
|
319
|
+
':is([data-richtext-task])': LIST_ROW_FLUSH,
|
|
277
320
|
},
|
|
278
321
|
paddingInlineStart: {
|
|
279
322
|
default: LIST_ROW_FLUSH,
|
|
@@ -461,7 +504,9 @@ const editorTheme = stylex.create({
|
|
|
461
504
|
// never wider than the editor, where long lines wrap.
|
|
462
505
|
boxSizing: 'border-box',
|
|
463
506
|
width: 'fit-content',
|
|
464
|
-
|
|
507
|
+
// At least the header's own width too, as core's frame grows to fit its
|
|
508
|
+
// header (CodeBlockHeaderPlugin writes it).
|
|
509
|
+
minWidth: `max(min(${PROSE_MEASURE}, 100%), min(var(--_richtext-code-header-inline-size, 0px), 100%))`,
|
|
465
510
|
maxWidth: '100%',
|
|
466
511
|
marginBlockStart: {
|
|
467
512
|
default: WIDE_BLOCK_SPACE,
|
|
@@ -484,9 +529,16 @@ const editorTheme = stylex.create({
|
|
|
484
529
|
* class-name strings, which `stylex.props(...).className` yields.
|
|
485
530
|
*/
|
|
486
531
|
export function sharedEditorTheme(): EditorThemeClasses {
|
|
487
|
-
// A class for a block Lexical
|
|
532
|
+
// A class for a block Lexical can mark `dir="auto"`: it reads its own way
|
|
533
|
+
// once BlockDirectionPlugin has written a direction onto it, and follows
|
|
534
|
+
// the document while it still says `auto`.
|
|
488
535
|
const block = (...styles: Array<stylex.StyleXStyles>): string =>
|
|
489
|
-
stylex.props(...styles, editorTheme.
|
|
536
|
+
stylex.props(...styles, editorTheme.blockDirection).className ?? '';
|
|
537
|
+
// A class for a container — a list, a table and its parts — which orders
|
|
538
|
+
// its contents in its parent's direction: the document's at the top level,
|
|
539
|
+
// its wrapper item's when nested.
|
|
540
|
+
const container = (...styles: Array<stylex.StyleXStyles>): string =>
|
|
541
|
+
stylex.props(...styles, editorTheme.surfaceDirection).className ?? '';
|
|
490
542
|
const className = (style: stylex.StyleXStyles): string =>
|
|
491
543
|
stylex.props(style).className ?? '';
|
|
492
544
|
return {
|
|
@@ -504,11 +556,13 @@ export function sharedEditorTheme(): EditorThemeClasses {
|
|
|
504
556
|
},
|
|
505
557
|
quote: block(editorTheme.quote),
|
|
506
558
|
list: {
|
|
507
|
-
ul:
|
|
508
|
-
ol:
|
|
509
|
-
|
|
559
|
+
ul: container(editorTheme.ul),
|
|
560
|
+
ol: container(editorTheme.ol),
|
|
561
|
+
// An item is prose: it reads the way its own text reads, and the
|
|
562
|
+
// document's way when its text decides nothing.
|
|
563
|
+
listitem: block(editorTheme.listItem),
|
|
510
564
|
nested: {
|
|
511
|
-
listitem:
|
|
565
|
+
listitem: block(editorTheme.listItem),
|
|
512
566
|
},
|
|
513
567
|
// Lexical picks the class at `depth % 3` (spec:AST-061 DEC-6).
|
|
514
568
|
ulDepth: [
|
|
@@ -531,20 +585,20 @@ export function sharedEditorTheme(): EditorThemeClasses {
|
|
|
531
585
|
code: stylex.props(editorTheme.textCode).className,
|
|
532
586
|
},
|
|
533
587
|
// The plain class lets CodeSyntaxPlugin's highlight rules find code blocks.
|
|
534
|
-
code: `${
|
|
588
|
+
code: `${container(editorTheme.code)} ${className(editorTheme.codeDirection)} ${CODE_SYNTAX_CLASS}`,
|
|
535
589
|
hr: stylex.props(editorTheme.hr).className,
|
|
536
590
|
hrSelected: stylex.props(editorTheme.hrSelected).className,
|
|
537
591
|
// MarkdownExtensionsPlugin puts it on a selected plugin node.
|
|
538
592
|
markdownExtensionSelected: stylex.props(editorTheme.hrSelected).className,
|
|
539
|
-
table:
|
|
593
|
+
table: container(editorTheme.table),
|
|
540
594
|
// Lexical stamps rows and cells `dir="auto"` too, which would set a cell
|
|
541
595
|
// of English text left-to-right inside a right-to-left document.
|
|
542
|
-
tableRow:
|
|
543
|
-
tableCell:
|
|
544
|
-
tableCellHeader:
|
|
596
|
+
tableRow: container(editorTheme.tableRow),
|
|
597
|
+
tableCell: container(editorTheme.tableCell),
|
|
598
|
+
tableCellHeader: container(editorTheme.tableCellHeader),
|
|
545
599
|
// Added on top of the cell's own class by Lexical while the cell is in a
|
|
546
600
|
// table selection, so it carries no direction of its own.
|
|
547
601
|
tableCellSelected: className(editorTheme.tableCellSelected),
|
|
548
|
-
tableScrollableWrapper:
|
|
602
|
+
tableScrollableWrapper: container(editorTheme.tableScrollableWrapper),
|
|
549
603
|
};
|
|
550
604
|
}
|
|
@@ -0,0 +1,326 @@
|
|
|
1
|
+
// Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
2
|
+
|
|
3
|
+
'use client';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* @file BlockDirectionPlugin.tsx
|
|
7
|
+
* @input Uses @lexical/react (composer context) and the prose container
|
|
8
|
+
* classes, and classifies text with `firstStrongDirection`.
|
|
9
|
+
* @output Exports BlockDirectionPlugin, which gives each prose container an
|
|
10
|
+
* explicit `dir` taken from its own text, and registerBlockDirection for
|
|
11
|
+
* a caller driving a Lexical editor directly.
|
|
12
|
+
* @position Rendered by RichTextEditor and RichTextView inside the composer,
|
|
13
|
+
* so a document reads the same way in both.
|
|
14
|
+
*
|
|
15
|
+
* SYNC: RichTextEditor.tsx and RichTextView.tsx both mount this plugin and
|
|
16
|
+
* set `--_richtext-document-direction` on their own root.
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
import {useLayoutEffect} from 'react';
|
|
20
|
+
import {firstStrongDirection} from './firstStrongDirection';
|
|
21
|
+
import {useLexicalComposerContext} from '@lexical/react/LexicalComposerContext';
|
|
22
|
+
import {mergeRegister} from '@lexical/utils';
|
|
23
|
+
import {HeadingNode, QuoteNode} from '@lexical/rich-text';
|
|
24
|
+
import {
|
|
25
|
+
$isListItemNode,
|
|
26
|
+
$isListNode,
|
|
27
|
+
ListItemNode,
|
|
28
|
+
ListNode,
|
|
29
|
+
} from '@lexical/list';
|
|
30
|
+
import {CodeNode} from '@lexical/code';
|
|
31
|
+
import {TableCellNode, TableNode, TableRowNode} from '@lexical/table';
|
|
32
|
+
import {
|
|
33
|
+
$getRoot,
|
|
34
|
+
$isRootNode,
|
|
35
|
+
$isDecoratorNode,
|
|
36
|
+
$isElementNode,
|
|
37
|
+
HISTORY_MERGE_TAG,
|
|
38
|
+
ParagraphNode,
|
|
39
|
+
TextNode,
|
|
40
|
+
type ElementNode,
|
|
41
|
+
type Klass,
|
|
42
|
+
type LexicalEditor,
|
|
43
|
+
type LexicalNode,
|
|
44
|
+
} from 'lexical';
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* The containers read as one piece of prose, so one direction covers the
|
|
48
|
+
* whole of each, at any depth — a paragraph inside a table cell included.
|
|
49
|
+
*
|
|
50
|
+
* Lexical leaves a top-level block at `dir="auto"` and writes none below
|
|
51
|
+
* that, and neither state can say "this block has no direction of its own":
|
|
52
|
+
* under `auto`, digits, an image and an empty block all resolve
|
|
53
|
+
* left-to-right, exactly like English. Writing the direction out separates
|
|
54
|
+
* the two, and Lexical serializes it, so a stored document reopens the way
|
|
55
|
+
* it was written.
|
|
56
|
+
*
|
|
57
|
+
* A list, a fenced code block and a table are not here: they hold several
|
|
58
|
+
* items, lines or cells that are read on their own terms.
|
|
59
|
+
*/
|
|
60
|
+
const PROSE_NODES: ReadonlyArray<Klass<ElementNode>> = [
|
|
61
|
+
ParagraphNode,
|
|
62
|
+
HeadingNode,
|
|
63
|
+
QuoteNode,
|
|
64
|
+
ListItemNode,
|
|
65
|
+
];
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* The containers. They take their parent's direction from the theme whatever
|
|
69
|
+
* `dir` says, so an arriving one changes no pixels. It can still arrive from
|
|
70
|
+
* pasted HTML (`$setDirectionFromDOM`) or an older stored document; clearing
|
|
71
|
+
* it keeps the stored state saying what the document shows.
|
|
72
|
+
*
|
|
73
|
+
* A fenced code block has no per-line node to carry a direction, so the
|
|
74
|
+
* theme gives it `unicode-bidi: plaintext` and each line reads its own way.
|
|
75
|
+
* Core `Markdown` reaches the same rule through CodeBlock, which puts each
|
|
76
|
+
* line in its own `dir="auto"` element; `pnpm specs:for` finds the record.
|
|
77
|
+
*/
|
|
78
|
+
const CONTAINER_NODES: ReadonlyArray<Klass<ElementNode>> = [
|
|
79
|
+
ListNode,
|
|
80
|
+
CodeNode,
|
|
81
|
+
TableNode,
|
|
82
|
+
TableRowNode,
|
|
83
|
+
TableCellNode,
|
|
84
|
+
];
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* Whether this node is a prose container that carries its own `dir`.
|
|
88
|
+
*
|
|
89
|
+
* A list item holding a nested list takes one too, from the item it hangs
|
|
90
|
+
* from. The nesting step is an inline margin on the item, so it resolves
|
|
91
|
+
* against that item's own direction: left unwritten it puts the step on the
|
|
92
|
+
* document's side while the items inside start on theirs, and every level of
|
|
93
|
+
* an English list in a right-to-left document then begins at
|
|
94
|
+
* the same edge, with only the marker shape showing the depth.
|
|
95
|
+
*/
|
|
96
|
+
function $isOwnDirectionBlock(node: LexicalNode): node is ElementNode {
|
|
97
|
+
return $isElementNode(node) && PROSE_NODES.some(k => node instanceof k);
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* The direction this container's own text asks for; null when it has none.
|
|
102
|
+
*
|
|
103
|
+
* Only text a reader reads counts. A decorator — an image, a chart, an
|
|
104
|
+
* embed — shows no prose of its own, and `getTextContent` gives its
|
|
105
|
+
* alternative text, so a picture captioned in English would turn a Hebrew
|
|
106
|
+
* item left-to-right. Core Markdown skips the same nodes when it reads a
|
|
107
|
+
* block's direction. A fenced code block is skipped for its own reason:
|
|
108
|
+
* each of its lines reads on its own terms.
|
|
109
|
+
*
|
|
110
|
+
* Lexical puts a nested list in an item of its own after the item it hangs
|
|
111
|
+
* from. That holder shows no text and is not a block a reader sees: it
|
|
112
|
+
* belongs to the item above it, and a nested list is anchored on its parent
|
|
113
|
+
* item's text start. So it takes that item's direction, not the direction of
|
|
114
|
+
* the list inside it. Reading its own contents instead would anchor the
|
|
115
|
+
* nested list on whichever language its first item happened to use, which
|
|
116
|
+
* costs an item that reads the other way its nesting step, and leaves a
|
|
117
|
+
* neutral item — digits, an image — following that first item rather than
|
|
118
|
+
* the document.
|
|
119
|
+
*/
|
|
120
|
+
function $ownDirection(node: ElementNode): 'ltr' | 'rtl' | null {
|
|
121
|
+
const holds = $heldList(node);
|
|
122
|
+
if (holds !== null) {
|
|
123
|
+
const parentItem = $parentItemOf(node);
|
|
124
|
+
return parentItem === null ? null : $ownDirection(parentItem);
|
|
125
|
+
}
|
|
126
|
+
return firstStrongDirection($proseText(node));
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
/** The list a holder item exists to carry, or null when it carries prose. */
|
|
130
|
+
function $heldList(node: ElementNode): ElementNode | null {
|
|
131
|
+
if (!$isListItemNode(node)) {
|
|
132
|
+
return null;
|
|
133
|
+
}
|
|
134
|
+
const children = node.getChildren();
|
|
135
|
+
const lists = children.filter($isListNode);
|
|
136
|
+
return lists.length === 1 && children.length === lists.length
|
|
137
|
+
? lists[0]
|
|
138
|
+
: null;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
/** The item a holder hangs from: the nearest prose item before it. */
|
|
142
|
+
function $parentItemOf(node: ElementNode): ElementNode | null {
|
|
143
|
+
let sibling = node.getPreviousSibling();
|
|
144
|
+
while (sibling !== null) {
|
|
145
|
+
if ($isListItemNode(sibling) && $heldList(sibling) === null) {
|
|
146
|
+
return sibling;
|
|
147
|
+
}
|
|
148
|
+
sibling = sibling.getPreviousSibling();
|
|
149
|
+
}
|
|
150
|
+
return null;
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
/** The text of a node's prose descendants, decorators and code aside. */
|
|
154
|
+
function $proseText(node: ElementNode): string {
|
|
155
|
+
let text = '';
|
|
156
|
+
for (const child of node.getChildren()) {
|
|
157
|
+
if ($isDecoratorNode(child) || child instanceof CodeNode) {
|
|
158
|
+
continue;
|
|
159
|
+
}
|
|
160
|
+
text += $isElementNode(child) ? $proseText(child) : child.getTextContent();
|
|
161
|
+
if (firstStrongDirection(text) !== null) {
|
|
162
|
+
break;
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
return text;
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
/** Whether this node is a container, which carries no direction of its own. */
|
|
169
|
+
function $isContainer(node: LexicalNode): node is ElementNode {
|
|
170
|
+
return (
|
|
171
|
+
$isElementNode(node) && CONTAINER_NODES.some(klass => node instanceof klass)
|
|
172
|
+
);
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
/** The direction this node should carry: its text's, or none at all. */
|
|
176
|
+
function $wantedDirection(node: LexicalNode): 'ltr' | 'rtl' | null {
|
|
177
|
+
return $isOwnDirectionBlock(node) ? $ownDirection(node) : null;
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
/** Whether this node's `dir` is already what it should be. */
|
|
181
|
+
function $isSettled(node: LexicalNode): boolean {
|
|
182
|
+
if ($isRootNode(node)) {
|
|
183
|
+
return node.getDirection() === null;
|
|
184
|
+
}
|
|
185
|
+
if (!$isOwnDirectionBlock(node) && !$isContainer(node)) {
|
|
186
|
+
return true;
|
|
187
|
+
}
|
|
188
|
+
return (node as ElementNode).getDirection() === $wantedDirection(node);
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
/** Writes a node's direction onto it, if that is not what it says. */
|
|
192
|
+
function $writeOwnDirection(node: LexicalNode): void {
|
|
193
|
+
if (!$isSettled(node)) {
|
|
194
|
+
(node as ElementNode).setDirection($wantedDirection(node));
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
/**
|
|
199
|
+
* Every prose container at or above this node, nearest first.
|
|
200
|
+
*
|
|
201
|
+
* A keystroke deep in a list changes its own item, and a wrapper above it
|
|
202
|
+
* follows the item it hangs from, so both are written here.
|
|
203
|
+
* All of them have to be written inside the keystroke's own update: the
|
|
204
|
+
* sweep that follows is tagged `history-merge`, which `OnChangePlugin`
|
|
205
|
+
* does not report, so a wrapper left to the sweep would hold a stale
|
|
206
|
+
* direction in the value a caller stores until the next edit.
|
|
207
|
+
*/
|
|
208
|
+
function $proseContainers(node: LexicalNode): ElementNode[] {
|
|
209
|
+
const found: ElementNode[] = [];
|
|
210
|
+
for (
|
|
211
|
+
let current: LexicalNode | null = node;
|
|
212
|
+
current !== null;
|
|
213
|
+
current = current.getParent()
|
|
214
|
+
) {
|
|
215
|
+
if ($isOwnDirectionBlock(current)) {
|
|
216
|
+
found.push(current);
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
return found;
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
/** Every node whose direction this plugin owns, in document order. */
|
|
223
|
+
function $allProseContainers(): ElementNode[] {
|
|
224
|
+
const found: ElementNode[] = [];
|
|
225
|
+
const visit = (node: LexicalNode): void => {
|
|
226
|
+
if ($isOwnDirectionBlock(node) || $isContainer(node)) {
|
|
227
|
+
found.push(node as ElementNode);
|
|
228
|
+
}
|
|
229
|
+
if ($isElementNode(node)) {
|
|
230
|
+
node.getChildren().forEach(visit);
|
|
231
|
+
}
|
|
232
|
+
};
|
|
233
|
+
// The root is a container too. Lexical writes a stored root direction onto
|
|
234
|
+
// the editable element, and every container under it inherits, so a value
|
|
235
|
+
// saved in one document direction would lay its tables and code frames out
|
|
236
|
+
// that way in the other. It carries no text of its own, so it always gives
|
|
237
|
+
// the direction up and the surface decides.
|
|
238
|
+
found.push($getRoot());
|
|
239
|
+
$getRoot().getChildren().forEach(visit);
|
|
240
|
+
return found;
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
/**
|
|
244
|
+
* Keeps every prose container's `dir` equal to the direction of its own
|
|
245
|
+
* text. Returns the usual teardown.
|
|
246
|
+
*/
|
|
247
|
+
export function registerBlockDirection(editor: LexicalEditor): () => void {
|
|
248
|
+
return mergeRegister(
|
|
249
|
+
// Typing inside an existing paragraph dirties its text node, not the
|
|
250
|
+
// container, so the text node is where a keystroke can be caught.
|
|
251
|
+
// Running here keeps the direction in the update that changed the text,
|
|
252
|
+
// and so in that keystroke's own history entry: a later `editor.update`
|
|
253
|
+
// would be a history step of its own, and undo would bounce off it.
|
|
254
|
+
editor.registerNodeTransform(TextNode, node => {
|
|
255
|
+
$proseContainers(node).forEach($writeOwnDirection);
|
|
256
|
+
}),
|
|
257
|
+
// A container that gains or loses its text without a text node of its
|
|
258
|
+
// own being dirtied — a new empty paragraph, a heading conversion, a
|
|
259
|
+
// list item split, a deletion that empties a block.
|
|
260
|
+
...PROSE_NODES.map(klass =>
|
|
261
|
+
editor.registerNodeTransform(klass, node => {
|
|
262
|
+
$proseContainers(node).forEach($writeOwnDirection);
|
|
263
|
+
}),
|
|
264
|
+
),
|
|
265
|
+
// A container that arrives carrying a direction — pasted HTML, or an
|
|
266
|
+
// older stored document — gives it up, so its contents are ordered by
|
|
267
|
+
// the document as they are everywhere else. Removing a node dirties its
|
|
268
|
+
// parent, so a removed list item reaches its list here; the walk settles
|
|
269
|
+
// the wrapper above it in the same update, which the sweep below would
|
|
270
|
+
// otherwise do a history step later.
|
|
271
|
+
...CONTAINER_NODES.map(klass =>
|
|
272
|
+
editor.registerNodeTransform(klass, node => {
|
|
273
|
+
$writeOwnDirection(node);
|
|
274
|
+
$proseContainers(node).forEach($writeOwnDirection);
|
|
275
|
+
}),
|
|
276
|
+
),
|
|
277
|
+
// Replacing the whole state runs no transform at all, and RichTextView
|
|
278
|
+
// does exactly that whenever its `value` changes. Sweep the document
|
|
279
|
+
// when a container is left disagreeing with its text. The sweep merges
|
|
280
|
+
// into the update that caused it rather than taking an undo step of its
|
|
281
|
+
// own, and writing the direction settles the container, so this does
|
|
282
|
+
// not loop.
|
|
283
|
+
editor.registerUpdateListener(({editorState}) => {
|
|
284
|
+
const stale = editorState.read(() =>
|
|
285
|
+
$allProseContainers().some(node => !$isSettled(node)),
|
|
286
|
+
);
|
|
287
|
+
if (stale) {
|
|
288
|
+
editor.update(
|
|
289
|
+
() => {
|
|
290
|
+
$allProseContainers().forEach($writeOwnDirection);
|
|
291
|
+
},
|
|
292
|
+
{tag: HISTORY_MERGE_TAG},
|
|
293
|
+
);
|
|
294
|
+
}
|
|
295
|
+
}),
|
|
296
|
+
);
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
/**
|
|
300
|
+
* Gives each prose container — paragraph, heading, quote, list item, at any
|
|
301
|
+
* depth — an explicit `dir` taken from its own text, so a strongly
|
|
302
|
+
* directional one reads its own way in either document and one with no
|
|
303
|
+
* direction of its own follows the document.
|
|
304
|
+
*/
|
|
305
|
+
export function BlockDirectionPlugin(): null {
|
|
306
|
+
const [editor] = useLexicalComposerContext();
|
|
307
|
+
// A layout effect, not an ordinary one: it runs after Lexical has written
|
|
308
|
+
// the document into the DOM and before the browser paints, so the first
|
|
309
|
+
// frame a reader sees already has every block on its own side. An
|
|
310
|
+
// ordinary effect runs after that frame, which showed an English
|
|
311
|
+
// paragraph right-aligned in a right-to-left page for about one frame
|
|
312
|
+
// before it moved.
|
|
313
|
+
useLayoutEffect(() => {
|
|
314
|
+
const teardown = registerBlockDirection(editor);
|
|
315
|
+
// Registering only arms the transforms; a document already in the
|
|
316
|
+
// editor has run none of them. Settle it now, in this same frame.
|
|
317
|
+
editor.update(
|
|
318
|
+
() => {
|
|
319
|
+
$allProseContainers().forEach($writeOwnDirection);
|
|
320
|
+
},
|
|
321
|
+
{discrete: true, tag: HISTORY_MERGE_TAG},
|
|
322
|
+
);
|
|
323
|
+
return teardown;
|
|
324
|
+
}, [editor]);
|
|
325
|
+
return null;
|
|
326
|
+
}
|