@astryxdesign/richtext 0.7.17-canary.gffd1c8e → 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
|
@@ -5,7 +5,9 @@
|
|
|
5
5
|
/**
|
|
6
6
|
* @file CodeBlockHeaderPlugin.tsx
|
|
7
7
|
* @input Uses @lexical/react (composer context), @lexical/code (CodeNode),
|
|
8
|
-
* lexical,
|
|
8
|
+
* lexical, core IconButton, Icon, useClipboard, useTranslator and
|
|
9
|
+
* observeResize, which watches the editor root and each header and only
|
|
10
|
+
* schedules the measure frame.
|
|
9
11
|
* @output Exports CodeBlockHeaderPlugin, which draws core CodeBlock's header
|
|
10
12
|
* over every fenced code block: the language label and a copy button, or
|
|
11
13
|
* just the copy button in the corner when the block names no language.
|
|
@@ -13,10 +15,11 @@
|
|
|
13
15
|
* element that holds the content editable. The headers are siblings of the
|
|
14
16
|
* editable text, never inside it, so they are not part of the document and
|
|
15
17
|
* cannot be edited (spec:AST-061 FR8, DEC-4). The code block's theme class
|
|
16
|
-
* reserves the header's height,
|
|
18
|
+
* reserves the header's drawn height, which this plugin writes on the block,
|
|
19
|
+
* so the code sits where core Markdown's does.
|
|
17
20
|
*/
|
|
18
21
|
import * as stylex from '@stylexjs/stylex';
|
|
19
|
-
import { useEffect, useState } from 'react';
|
|
22
|
+
import { useCallback, useEffect, useRef, useState } from 'react';
|
|
20
23
|
import { useLexicalComposerContext } from '@lexical/react/LexicalComposerContext';
|
|
21
24
|
import { $isCodeNode } from '@lexical/code';
|
|
22
25
|
import { $getNodeByKey, $getRoot } from 'lexical';
|
|
@@ -24,6 +27,7 @@ import { Icon } from '@astryxdesign/core/Icon';
|
|
|
24
27
|
import { IconButton } from '@astryxdesign/core/IconButton';
|
|
25
28
|
import { useClipboard } from '@astryxdesign/core/hooks';
|
|
26
29
|
import { useTranslator } from '@astryxdesign/core/i18n';
|
|
30
|
+
import { observeResize } from '@astryxdesign/core/utils';
|
|
27
31
|
import { listedLanguage } from "../codeLanguages.js";
|
|
28
32
|
import '@astryxdesign/core/theme/tokens.stylex';
|
|
29
33
|
import { fontWeightVars, spacingVars, typeScaleVars, typographyVars } from '@astryxdesign/core/theme/tokens.stylex';
|
|
@@ -84,7 +88,8 @@ function samePlacements(a, b) {
|
|
|
84
88
|
}
|
|
85
89
|
function CodeBlockHeader({
|
|
86
90
|
placement,
|
|
87
|
-
label
|
|
91
|
+
label,
|
|
92
|
+
registerHeader
|
|
88
93
|
}) {
|
|
89
94
|
const [editor] = useLexicalComposerContext();
|
|
90
95
|
const t = useTranslator();
|
|
@@ -111,6 +116,16 @@ function CodeBlockHeader({
|
|
|
111
116
|
xstyle: [styles.copyButton, placement.language == null && styles.cornerButton]
|
|
112
117
|
});
|
|
113
118
|
const position = styles.placement(placement.top, placement.left, placement.width);
|
|
119
|
+
const titleRef = useRef(null);
|
|
120
|
+
// React attaches a child's ref before its parent's, so the label is set
|
|
121
|
+
// when the header's ref runs.
|
|
122
|
+
const headerRef = useCallback(header => {
|
|
123
|
+
const title = titleRef.current;
|
|
124
|
+
return header == null || title == null ? undefined : registerHeader(placement.key, {
|
|
125
|
+
header,
|
|
126
|
+
title
|
|
127
|
+
});
|
|
128
|
+
}, [registerHeader, placement.key]);
|
|
114
129
|
if (placement.language == null) {
|
|
115
130
|
return /*#__PURE__*/_jsx("div", {
|
|
116
131
|
"data-richtext-code-header": "",
|
|
@@ -119,17 +134,68 @@ function CodeBlockHeader({
|
|
|
119
134
|
});
|
|
120
135
|
}
|
|
121
136
|
return /*#__PURE__*/_jsxs("div", {
|
|
137
|
+
ref: headerRef,
|
|
122
138
|
"data-richtext-code-header": "",
|
|
123
139
|
...stylex.props(styles.header, position),
|
|
124
140
|
children: [/*#__PURE__*/_jsx("span", {
|
|
141
|
+
ref: titleRef,
|
|
125
142
|
...{
|
|
126
|
-
className: "x141an7d x9m5x89 x1e4wzip x1ltkj2j x1mjzq1q xeuugli
|
|
143
|
+
className: "x141an7d x9m5x89 x1e4wzip x1ltkj2j x1mjzq1q xeuugli x126k92a xj0a0fe x5ujdhh"
|
|
127
144
|
},
|
|
128
145
|
children: label ?? placement.language
|
|
129
146
|
}), copyButton]
|
|
130
147
|
});
|
|
131
148
|
}
|
|
132
149
|
|
|
150
|
+
/** The variable the code block's theme class reserves the header with. */
|
|
151
|
+
const HEADER_BLOCK_SIZE = '--_richtext-code-header-block-size';
|
|
152
|
+
/** The variable the code block's theme class takes its least width from. */
|
|
153
|
+
const HEADER_INLINE_SIZE = '--_richtext-code-header-inline-size';
|
|
154
|
+
|
|
155
|
+
/** A drawn language header and its label. */
|
|
156
|
+
|
|
157
|
+
/**
|
|
158
|
+
* The header's width with its label on one line: its padding, the gaps, the
|
|
159
|
+
* controls beside the label, and the label's one-line width.
|
|
160
|
+
*/
|
|
161
|
+
function naturalWidth({
|
|
162
|
+
header,
|
|
163
|
+
title
|
|
164
|
+
}, label) {
|
|
165
|
+
const style = getComputedStyle(header);
|
|
166
|
+
// A control's tooltip layer, its marker or a dialog sits beside it in the
|
|
167
|
+
// header but takes no room in the row.
|
|
168
|
+
const others = [...header.children].filter(child => child !== title && !child.matches('[popover], template, dialog'));
|
|
169
|
+
const gap = parseFloat(style.columnGap) || 0;
|
|
170
|
+
return parseFloat(style.paddingInlineStart) + parseFloat(style.paddingInlineEnd) + others.reduce((sum, child) => sum + child.offsetWidth, 0) + gap * others.length + label;
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
/**
|
|
174
|
+
* The width of `title`'s text on one line, in its own font, or null where no
|
|
175
|
+
* canvas measures text (a DOM without layout). Chromium returns an empty
|
|
176
|
+
* `font` shorthand when the text inherits a font-variant, feature-settings or
|
|
177
|
+
* kerning value. So the font is built from its longhands. Letter spacing is
|
|
178
|
+
* added per character. Core's table measure works the same way. Core's
|
|
179
|
+
* internal entries are for Core and Lab only, so RichText keeps this copy.
|
|
180
|
+
*/
|
|
181
|
+
function oneLineWidth(title, canvas) {
|
|
182
|
+
if (canvas.current === undefined) {
|
|
183
|
+
canvas.current = document.createElement('canvas').getContext('2d');
|
|
184
|
+
}
|
|
185
|
+
const context = canvas.current;
|
|
186
|
+
if (context == null) {
|
|
187
|
+
return null;
|
|
188
|
+
}
|
|
189
|
+
const style = getComputedStyle(title);
|
|
190
|
+
if (style.fontSize === '' || style.fontFamily === '') {
|
|
191
|
+
return null;
|
|
192
|
+
}
|
|
193
|
+
context.font = [style.fontStyle, style.fontWeight, style.fontSize].filter(part => part !== '').concat(style.fontFamily).join(' ');
|
|
194
|
+
const text = title.textContent ?? '';
|
|
195
|
+
const spacing = style.letterSpacing === 'normal' ? 0 : parseFloat(style.letterSpacing) || 0;
|
|
196
|
+
return context.measureText(text).width + spacing * text.length;
|
|
197
|
+
}
|
|
198
|
+
|
|
133
199
|
/**
|
|
134
200
|
* Draws a header over each top-level code block and keeps it there as the
|
|
135
201
|
* document, the editor's size, or the window changes.
|
|
@@ -139,6 +205,28 @@ export function CodeBlockHeaderPlugin({
|
|
|
139
205
|
} = {}) {
|
|
140
206
|
const [editor] = useLexicalComposerContext();
|
|
141
207
|
const [placements, setPlacements] = useState([]);
|
|
208
|
+
// The drawn language headers, by code block key. The measure frame reads
|
|
209
|
+
// their sizes with the blocks' boxes. Then it writes each size on its block.
|
|
210
|
+
// The block's top padding reserves the height, and its least width is the
|
|
211
|
+
// header's own.
|
|
212
|
+
const headersRef = useRef(new Map());
|
|
213
|
+
const canvasRef = useRef(undefined);
|
|
214
|
+
const scheduleRef = useRef(null);
|
|
215
|
+
const registerHeader = useCallback((key, parts) => {
|
|
216
|
+
headersRef.current.set(key, parts);
|
|
217
|
+
// A header that wraps to a new line count measures again; the shared
|
|
218
|
+
// observer only schedules the frame.
|
|
219
|
+
const unobserve = observeResize(parts.header, () => scheduleRef.current?.());
|
|
220
|
+
return () => {
|
|
221
|
+
unobserve();
|
|
222
|
+
if (headersRef.current.get(key) === parts) {
|
|
223
|
+
headersRef.current.delete(key);
|
|
224
|
+
}
|
|
225
|
+
const block = editor.getElementByKey(key);
|
|
226
|
+
block?.style.removeProperty(HEADER_BLOCK_SIZE);
|
|
227
|
+
block?.style.removeProperty(HEADER_INLINE_SIZE);
|
|
228
|
+
};
|
|
229
|
+
}, [editor]);
|
|
142
230
|
useEffect(() => {
|
|
143
231
|
let frame = 0;
|
|
144
232
|
const measure = () => {
|
|
@@ -150,6 +238,19 @@ export function CodeBlockHeaderPlugin({
|
|
|
150
238
|
return;
|
|
151
239
|
}
|
|
152
240
|
const origin = container.getBoundingClientRect();
|
|
241
|
+
// Each header's drawn height, and the width it takes with its label
|
|
242
|
+
// on one line, border included, as core's frame grows to fit it.
|
|
243
|
+
const sizes = [...headersRef.current].map(([key, parts]) => {
|
|
244
|
+
const block = editor.getElementByKey(key);
|
|
245
|
+
const border = block == null ? 0 : block.offsetWidth - block.clientWidth;
|
|
246
|
+
const label = oneLineWidth(parts.title, canvasRef);
|
|
247
|
+
return {
|
|
248
|
+
key,
|
|
249
|
+
block,
|
|
250
|
+
height: `${parts.header.offsetHeight}px`,
|
|
251
|
+
width: label == null ? null : `${Math.ceil(naturalWidth(parts, label) + border)}px`
|
|
252
|
+
};
|
|
253
|
+
});
|
|
153
254
|
const next = editor.getEditorState().read(() => $getRoot().getChildren().filter($isCodeNode).flatMap(node => {
|
|
154
255
|
const element = editor.getElementByKey(node.getKey());
|
|
155
256
|
if (element == null) {
|
|
@@ -166,6 +267,23 @@ export function CodeBlockHeaderPlugin({
|
|
|
166
267
|
width: element.clientWidth
|
|
167
268
|
}];
|
|
168
269
|
}));
|
|
270
|
+
// Writes after every read in this frame. A block that grows moves the
|
|
271
|
+
// blocks below it; the root's resize measures them again next frame.
|
|
272
|
+
for (const {
|
|
273
|
+
block,
|
|
274
|
+
height,
|
|
275
|
+
width
|
|
276
|
+
} of sizes) {
|
|
277
|
+
if (block == null) {
|
|
278
|
+
continue;
|
|
279
|
+
}
|
|
280
|
+
if (block.style.getPropertyValue(HEADER_BLOCK_SIZE) !== height) {
|
|
281
|
+
block.style.setProperty(HEADER_BLOCK_SIZE, height);
|
|
282
|
+
}
|
|
283
|
+
if (width != null && block.style.getPropertyValue(HEADER_INLINE_SIZE) !== width) {
|
|
284
|
+
block.style.setProperty(HEADER_INLINE_SIZE, width);
|
|
285
|
+
}
|
|
286
|
+
}
|
|
169
287
|
setPlacements(current => samePlacements(current, next) ? current : next);
|
|
170
288
|
};
|
|
171
289
|
const schedule = () => {
|
|
@@ -173,12 +291,11 @@ export function CodeBlockHeaderPlugin({
|
|
|
173
291
|
frame = requestAnimationFrame(measure);
|
|
174
292
|
}
|
|
175
293
|
};
|
|
176
|
-
|
|
294
|
+
scheduleRef.current = schedule;
|
|
295
|
+
let unobserveRoot = null;
|
|
177
296
|
const unregisterRoot = editor.registerRootListener(root => {
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
observer?.observe(root);
|
|
181
|
-
}
|
|
297
|
+
unobserveRoot?.();
|
|
298
|
+
unobserveRoot = root == null ? null : observeResize(root, schedule);
|
|
182
299
|
schedule();
|
|
183
300
|
});
|
|
184
301
|
const unregisterUpdate = editor.registerUpdateListener(schedule);
|
|
@@ -186,7 +303,8 @@ export function CodeBlockHeaderPlugin({
|
|
|
186
303
|
schedule();
|
|
187
304
|
return () => {
|
|
188
305
|
cancelAnimationFrame(frame);
|
|
189
|
-
|
|
306
|
+
unobserveRoot?.();
|
|
307
|
+
scheduleRef.current = null;
|
|
190
308
|
unregisterRoot();
|
|
191
309
|
unregisterUpdate();
|
|
192
310
|
window.removeEventListener('resize', schedule);
|
|
@@ -194,7 +312,8 @@ export function CodeBlockHeaderPlugin({
|
|
|
194
312
|
}, [editor]);
|
|
195
313
|
return /*#__PURE__*/_jsx(_Fragment, {
|
|
196
314
|
children: placements.map(placement => /*#__PURE__*/_jsx(CodeBlockHeader, {
|
|
197
|
-
placement: placement
|
|
315
|
+
placement: placement,
|
|
316
|
+
registerHeader: registerHeader
|
|
198
317
|
// A fence matched in `codeLanguages` shows the value's name, as
|
|
199
318
|
// Markdown does (spec:AST-061).
|
|
200
319
|
,
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Approximates rules P2 and P3 of the Unicode Bidirectional Algorithm from
|
|
3
|
+
* script and category: the direction of the first strongly directional
|
|
4
|
+
* character, or null when there is none. It does not skip isolated runs. The
|
|
5
|
+
* loop reads whole code points, so a surrogate pair counts as one character.
|
|
6
|
+
*/
|
|
7
|
+
export declare function firstStrongDirection(text: string): 'ltr' | 'rtl' | null;
|
|
8
|
+
//# sourceMappingURL=firstStrongDirection.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"firstStrongDirection.d.ts","sourceRoot":"","sources":["../../src/internal/firstStrongDirection.ts"],"names":[],"mappings":"AAwFA;;;;;GAKG;AACH,wBAAgB,oBAAoB,CAAC,IAAI,EAAE,MAAM,GAAG,KAAK,GAAG,KAAK,GAAG,IAAI,CAgBvE"}
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
// Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* @file firstStrongDirection.ts
|
|
5
|
+
* @input A string of text
|
|
6
|
+
* @output 'ltr' | 'rtl' | null — the direction of the text's first strongly
|
|
7
|
+
* directional character, or null when it has none
|
|
8
|
+
* @position Classifies a RichText block, so a block reads the way its own
|
|
9
|
+
* text reads. Server-safe: it reads no context.
|
|
10
|
+
*
|
|
11
|
+
* SYNC: a copy of packages/core/src/i18n/firstStrongDirection.ts, which
|
|
12
|
+
* classifies Markdown's blocks. The two surfaces have to answer alike, so a
|
|
13
|
+
* change to either set belongs in both files and in both test files. Core's
|
|
14
|
+
* `internal/*` entry points are reserved for Core and Lab, so RichText keeps
|
|
15
|
+
* its own copy rather than widening that rule; sharing one classifier across
|
|
16
|
+
* packages is its own decision.
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
// Each expression with a Unicode property escape is built at runtime: a
|
|
20
|
+
// source build may rewrite a regular expression literal with one, and the
|
|
21
|
+
// Babel copy that Next.js bundles has no Unicode data to do that.
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* The strongly right-to-left characters of the Unicode Bidirectional
|
|
25
|
+
* Algorithm: Bidi_Class `R` and `AL`. The living scripts are named. The two
|
|
26
|
+
* ranges cover the historic right-to-left scripts in the Supplementary
|
|
27
|
+
* Multilingual Plane, which have no script escapes that every engine knows.
|
|
28
|
+
*
|
|
29
|
+
* JavaScript has no `\p{Bidi_Class=...}` escape, so these sets approximate
|
|
30
|
+
* that property from script and category, and name the characters this
|
|
31
|
+
* surface has needed where the two disagree; others still do, as they did
|
|
32
|
+
* before: some Arabic and NKo `ON` signs, `ON` modifier letters, and `L`
|
|
33
|
+
* punctuation. Three `AL` characters have script `Common` and are written
|
|
34
|
+
* out here: the semicolon, the question mark, and tatweel. Tatweel is also a
|
|
35
|
+
* modifier letter, so without it the scan reads an Arabic word that starts
|
|
36
|
+
* with one as left-to-right. The Arabic letter mark is beside them for the
|
|
37
|
+
* same reason.
|
|
38
|
+
*/
|
|
39
|
+
const STRONG_RTL = new RegExp(String.raw`[\p{Script=Adlam}\p{Script=Arabic}\p{Script=Hebrew}\p{Script=Mandaic}\p{Script=Nko}\p{Script=Samaritan}\p{Script=Syriac}\p{Script=Thaana}\u061B\u061C\u061F\u0640\u200F\u{10800}-\u{10FFF}\u{1E800}-\u{1EFFF}]`, 'u');
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* The characters the scan passes over: Bidi_Class `AN`, `EN`, `ET` and
|
|
43
|
+
* `ON`, none of which decides a direction. Most are the right-to-left
|
|
44
|
+
* scripts' own, and the digits that are weak everywhere are here too: the
|
|
45
|
+
* ASCII, fullwidth and mathematical ones are `EN`.
|
|
46
|
+
*
|
|
47
|
+
* The first group is the Arabic script's own: its digits, the number signs
|
|
48
|
+
* before them, and the currency and per-cent marks beside them.
|
|
49
|
+
*
|
|
50
|
+
* The rest sit inside the two supplementary ranges above, which are drawn
|
|
51
|
+
* around whole planes and so sweep up every digit and mark those scripts
|
|
52
|
+
* carry as well as their letters: the Phoenician word separator, Avestan
|
|
53
|
+
* punctuation, the Hanifi Rohingya, Garay, Yezidi and Rumi digits, a Garay
|
|
54
|
+
* mark, and the Arabic mathematical operators. Each one is verified against
|
|
55
|
+
* the browser's own implementation of the algorithm in
|
|
56
|
+
* firstStrongDirection.test.ts.
|
|
57
|
+
*
|
|
58
|
+
* Membership here is about Bidi_Class, never about looking like a number.
|
|
59
|
+
* The Indic Siyaq and Ottoman Siyaq numerals sit beside Rumi and are
|
|
60
|
+
* Bidi_Class `AL`, so they stay strong; NKo and Adlam digits are `R`, so
|
|
61
|
+
* they do too.
|
|
62
|
+
*/
|
|
63
|
+
const WEAK_NUMBER_OR_NEUTRAL = /[0-9\u0600-\u0605\u0609\u060A\u0660-\u0669\u066A-\u066C\u06DD\u06F0-\u06F9\u0890\u0891\u08E2\uFF10-\uFF19\u{1091F}\u{10B39}-\u{10B3F}\u{10D30}-\u{10D39}\u{10D40}-\u{10D49}\u{10D6E}\u{10E60}-\u{10E7E}\u{10ED0}-\u{10ED8}\u{1CCF0}-\u{1CCF9}\u{1D7CE}-\u{1D7FF}\u{1EEF0}\u{1EEF1}\u{1FBF0}-\u{1FBF9}]/u;
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* Bidi_Class `L`: every other letter, the letter numbers (`\p{Letter}` does
|
|
67
|
+
* not include them), the left-to-right mark, and the decimal digits.
|
|
68
|
+
*
|
|
69
|
+
* Most scripts' digits are `L` — Devanagari, Bengali, Tamil, Thai and the
|
|
70
|
+
* rest — so a line opening with them reads left-to-right. The digits that
|
|
71
|
+
* are not are read before this: the European ones are `EN` and the
|
|
72
|
+
* Arabic-Indic ones `AN`, both in the weak set above, and NKo's and Adlam's
|
|
73
|
+
* are `R`, matched as their own scripts. Punctuation and symbols are not
|
|
74
|
+
* here, so the scan passes over them.
|
|
75
|
+
*/
|
|
76
|
+
const STRONG_LTR = new RegExp(String.raw`[\p{Letter}\p{Nl}\p{Nd}\u200E]`, 'u');
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* Bidi_Class `NSM`: a combining mark takes the direction of its base
|
|
80
|
+
* character. A mark has its base letter's script, so without this check a
|
|
81
|
+
* leading Hebrew point would read as right-to-left.
|
|
82
|
+
*/
|
|
83
|
+
const COMBINING_MARK = new RegExp(String.raw`\p{Mark}`, 'u');
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* Approximates rules P2 and P3 of the Unicode Bidirectional Algorithm from
|
|
87
|
+
* script and category: the direction of the first strongly directional
|
|
88
|
+
* character, or null when there is none. It does not skip isolated runs. The
|
|
89
|
+
* loop reads whole code points, so a surrogate pair counts as one character.
|
|
90
|
+
*/
|
|
91
|
+
export function firstStrongDirection(text) {
|
|
92
|
+
for (const character of text) {
|
|
93
|
+
if (COMBINING_MARK.test(character) || WEAK_NUMBER_OR_NEUTRAL.test(character)) {
|
|
94
|
+
continue;
|
|
95
|
+
}
|
|
96
|
+
if (STRONG_RTL.test(character)) {
|
|
97
|
+
return 'rtl';
|
|
98
|
+
}
|
|
99
|
+
if (STRONG_LTR.test(character)) {
|
|
100
|
+
return 'ltr';
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
return null;
|
|
104
|
+
}
|
package/dist/richtext.css
CHANGED
|
@@ -8,12 +8,15 @@
|
|
|
8
8
|
@property --x-minHeight { syntax: "*"; inherits: false;}
|
|
9
9
|
@property --x-top { syntax: "*"; inherits: false;}
|
|
10
10
|
@property --x-width { syntax: "*"; inherits: false;}
|
|
11
|
+
.x5ujdhh{--_astryx-text-overflow-wrap:anywhere}
|
|
12
|
+
.xrjygrn{--_richtext-document-direction:ltr}
|
|
11
13
|
.x1qgjean{--astryx-section-padding-inline-end:var(--spacing-1)}
|
|
12
14
|
.xcfn0br{--astryx-section-padding-inline-start:var(--spacing-1)}
|
|
13
15
|
.xax9j7h{--container-padding-block-end:0px}
|
|
14
16
|
.x1047aw6{--container-padding-block-start:0px}
|
|
15
17
|
.xjsfl84{--container-padding-inline-end:0px}
|
|
16
18
|
.xrhngw9{--container-padding-inline-start:0px}
|
|
19
|
+
.xss4jc1:dir(rtl){--_richtext-document-direction:rtl}
|
|
17
20
|
.x1717udv:not(#\#){padding:0}
|
|
18
21
|
.xlsj2fj:not(#\#){padding:var(--spacing-2)}
|
|
19
22
|
.x14i3s5s:not(#\#):not(#\#){border-color:var(--color-border)}
|
|
@@ -27,7 +30,6 @@
|
|
|
27
30
|
.xsn7fz1:not(#\#):not(#\#){gap:var(--spacing-0)}
|
|
28
31
|
.xrxpjvj:not(#\#):not(#\#){margin-inline:0}
|
|
29
32
|
.x1a2a7pz:not(#\#):not(#\#){outline:none}
|
|
30
|
-
.xb3r6kr:not(#\#):not(#\#){overflow:hidden}
|
|
31
33
|
.x1hp7d8q:not(#\#):not(#\#){padding-block:var(--spacing-0)}
|
|
32
34
|
.xu0wf1k:not(#\#):not(#\#){padding-block:var(--spacing-1)}
|
|
33
35
|
.xce4md1:not(#\#):not(#\#){padding-block:var(--spacing-2)}
|
|
@@ -112,34 +114,35 @@
|
|
|
112
114
|
.x1rrqm6h:not(#\#):not(#\#):not(#\#){list-style-type:lower-alpha}
|
|
113
115
|
.x19k9dhg:not(#\#):not(#\#):not(#\#){list-style-type:lower-roman}
|
|
114
116
|
.x152237o:not(#\#):not(#\#):not(#\#){list-style-type:square}
|
|
115
|
-
.
|
|
117
|
+
.x1l10yog:not(#\#):not(#\#):not(#\#){margin-inline-start:var(--spacing-6)}
|
|
116
118
|
.x19bwoy8:not(#\#):not(#\#):not(#\#){outline-color:var(--focus-outline-color)}
|
|
117
119
|
.x1c097ug:not(#\#):not(#\#):not(#\#){outline-offset:calc(-1 * var(--focus-outline-width))}
|
|
118
120
|
.x1eqjuhu:not(#\#):not(#\#):not(#\#){outline-offset:var(--focus-outline-offset)}
|
|
119
121
|
.x9ssot5:not(#\#):not(#\#):not(#\#){outline-style:var(--focus-outline-style)}
|
|
120
122
|
.xak0ai5:not(#\#):not(#\#):not(#\#){outline-width:var(--focus-outline-width)}
|
|
123
|
+
.xj0a0fe:not(#\#):not(#\#):not(#\#){overflow-wrap:anywhere}
|
|
121
124
|
.x1mzt3pk:not(#\#):not(#\#):not(#\#){overflow-wrap:break-word}
|
|
122
125
|
.xp74n0j:not(#\#):not(#\#):not(#\#){padding-inline-end:calc(var(--spacing-2) + var(--spacing-6))}
|
|
123
126
|
.xza90q5:not(#\#):not(#\#):not(#\#){padding-inline-end:calc(var(--spacing-4) + var(--size-element-sm) + 2 * var(--spacing-2))}
|
|
124
127
|
.xbj1ybn:not(#\#):not(#\#):not(#\#){padding-inline-start:calc(var(--spacing-0))}
|
|
125
128
|
.x1rey3nv:not(#\#):not(#\#):not(#\#){padding-inline-start:var(--spacing-4)}
|
|
126
|
-
.x31w388:not(#\#):not(#\#):not(#\#){padding-inline-start:var(--spacing-6)}
|
|
127
129
|
.x67bb7w:not(#\#):not(#\#):not(#\#){pointer-events:auto}
|
|
128
130
|
.x47corl:not(#\#):not(#\#):not(#\#){pointer-events:none}
|
|
129
131
|
.x10l6tqk:not(#\#):not(#\#):not(#\#){position:absolute}
|
|
130
132
|
.x1n2onr6:not(#\#):not(#\#):not(#\#){position:relative}
|
|
131
133
|
.x1597r2g:not(#\#):not(#\#):not(#\#){scrollbar-width:thin}
|
|
132
134
|
.x1yc453h:not(#\#):not(#\#):not(#\#){text-align:start}
|
|
133
|
-
.
|
|
135
|
+
.xa7awgv:not(#\#):not(#\#):not(#\#){unicode-bidi:plaintext}
|
|
134
136
|
.x87ps6o:not(#\#):not(#\#):not(#\#){user-select:none}
|
|
135
137
|
.xxymvpz:not(#\#):not(#\#):not(#\#){vertical-align:middle}
|
|
136
|
-
.xuxw1ft:not(#\#):not(#\#):not(#\#){white-space:nowrap}
|
|
137
138
|
.x126k92a:not(#\#):not(#\#):not(#\#){white-space:pre-wrap}
|
|
138
139
|
.x1yn0g08:not(#\#):not(#\#):not(#\#){word-break:break-all}
|
|
139
140
|
.x1vjfegm:not(#\#):not(#\#):not(#\#){z-index:1}
|
|
140
141
|
.x16khyan:is(:disabled,[aria-disabled="true"]):not(#\#):not(#\#):not(#\#){cursor:default}
|
|
142
|
+
.x2dh0mw:is([dir="auto"]):not(#\#):not(#\#):not(#\#){direction:var(--_richtext-document-direction,inherit)}
|
|
143
|
+
.xz38kqs:not([dir]):not(#\#):not(#\#):not(#\#){direction:var(--_richtext-document-direction,inherit)}
|
|
141
144
|
.x2i8j16:is([data-richtext-task]):not(#\#):not(#\#):not(#\#){list-style-type:none}
|
|
142
|
-
.
|
|
145
|
+
.x1w4ozbb:is([data-richtext-task]):not(#\#):not(#\#):not(#\#){margin-inline-start:calc(var(--spacing-0))}
|
|
143
146
|
.x18crcbm:is([data-language]:not([data-language="plaintext"])):not(#\#):not(#\#):not(#\#){padding-inline-end:var(--spacing-4)}
|
|
144
147
|
.x1kuvm45:is([data-richtext-task]):not(#\#):not(#\#):not(#\#){padding-inline-start:calc(20px + var(--spacing-2))}
|
|
145
148
|
.x81k3mc:has(ul):not(#\#):not(#\#):not(#\#){list-style-type:none}
|
|
@@ -172,7 +175,7 @@
|
|
|
172
175
|
.x65nank:not(#\#):not(#\#):not(#\#):not(#\#){min-height:auto}
|
|
173
176
|
.x82snj4:not(#\#):not(#\#):not(#\#):not(#\#){min-height:var(--x-minHeight)}
|
|
174
177
|
.xeuugli:not(#\#):not(#\#):not(#\#):not(#\#){min-width:0}
|
|
175
|
-
.
|
|
178
|
+
.x1o973b8:not(#\#):not(#\#):not(#\#):not(#\#){min-width:max(min(680px,100%),min(var(--_richtext-code-header-inline-size,0px),100%))}
|
|
176
179
|
.xw2csxc:not(#\#):not(#\#):not(#\#):not(#\#){overflow-x:auto}
|
|
177
180
|
.x10wlt62:not(#\#):not(#\#):not(#\#):not(#\#){overflow-y:hidden}
|
|
178
181
|
.x7p5m3t:not(#\#):not(#\#):not(#\#):not(#\#){overscroll-behavior-x:contain}
|
|
@@ -188,7 +191,7 @@
|
|
|
188
191
|
.xeq5yr9:not(#\#):not(#\#):not(#\#):not(#\#){width:fit-content}
|
|
189
192
|
.x5lhr3w:not(#\#):not(#\#):not(#\#):not(#\#){width:var(--x-width)}
|
|
190
193
|
.xe66n16:is([data-lexical-decorator-boundary]:first-child + *):not(#\#):not(#\#):not(#\#):not(#\#){margin-top:var(--spacing-0)}
|
|
191
|
-
.
|
|
194
|
+
.xyyife3:is([data-language]:not([data-language="plaintext"])):not(#\#):not(#\#):not(#\#):not(#\#){padding-top:calc(var(--_richtext-code-header-block-size,calc(2 * var(--spacing-2) + var(--size-element-sm))) + var(--spacing-3) - var(--spacing-2))}
|
|
192
195
|
.x1vugyqv:has(+ [data-lexical-decorator-boundary]:last-child):not(#\#):not(#\#):not(#\#):not(#\#){margin-bottom:var(--spacing-0)}
|
|
193
196
|
.xkuhcwz:has(ul):not(#\#):not(#\#):not(#\#):not(#\#){margin-top:calc(var(--spacing-2))}
|
|
194
197
|
.xsgnyjk:has(ol):not(#\#):not(#\#):not(#\#):not(#\#){margin-top:calc(var(--spacing-2))}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@astryxdesign/richtext",
|
|
3
3
|
"displayName": "Rich Text",
|
|
4
|
-
"version": "0.7.17
|
|
4
|
+
"version": "0.7.17",
|
|
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.17
|
|
69
|
+
"@astryxdesign/core": "0.7.17",
|
|
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.17-canary.gffd1c8e",
|
|
129
|
-
"@astryxdesign/cli": "0.7.17-canary.gffd1c8e",
|
|
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.1",
|
|
147
145
|
"@testing-library/react": "^16.3.3",
|
|
148
|
-
"lexical": "^0.52.0"
|
|
146
|
+
"lexical": "^0.52.0",
|
|
147
|
+
"@astryxdesign/a11y-spec": "0.0.0",
|
|
148
|
+
"@astryxdesign/cli": "0.7.17"
|
|
149
149
|
},
|
|
150
150
|
"scripts": {
|
|
151
151
|
"build": "node ../../scripts/clean-dist.mjs && pnpm build:esm && tsc --project tsconfig.build.json && node ../../scripts/add-declaration-extensions.mjs && pnpm build:css && node ../../scripts/check-no-dev-jsx.mjs && node ../../scripts/check-fully-specified.mjs",
|
package/src/RichTextEditor.tsx
CHANGED
|
@@ -82,6 +82,7 @@ import {CodeBackspacePlugin} from './internal/CodeBackspacePlugin';
|
|
|
82
82
|
import {CodeBlockHeaderPlugin} from './internal/CodeBlockHeaderPlugin';
|
|
83
83
|
import {CodePastePlugin} from './internal/CodePastePlugin';
|
|
84
84
|
import {CodeSyntaxPlugin} from './internal/CodeSyntaxPlugin';
|
|
85
|
+
import {BlockDirectionPlugin} from './internal/BlockDirectionPlugin';
|
|
85
86
|
import {CodeTabPlugin} from './internal/CodeTabPlugin';
|
|
86
87
|
import {MarkdownExtensionsPlugin} from './internal/MarkdownExtensionsPlugin';
|
|
87
88
|
import type {RichTextMarkdownExtension} from './markdownExtensions';
|
|
@@ -197,6 +198,10 @@ const styles = stylex.create({
|
|
|
197
198
|
color: colorVars['--color-text-secondary'],
|
|
198
199
|
},
|
|
199
200
|
editorRoot: {
|
|
201
|
+
// The document's own direction, read where the surface sits. A block
|
|
202
|
+
// whose text decides nothing reads this rather than inheriting, so a
|
|
203
|
+
// list or table that reads the other way cannot change it.
|
|
204
|
+
'--_richtext-document-direction': {default: 'ltr', ':dir(rtl)': 'rtl'},
|
|
200
205
|
position: 'relative',
|
|
201
206
|
width: '100%',
|
|
202
207
|
// ContentEditable and Lexical's sibling placeholder inherit one shared
|
|
@@ -738,6 +743,7 @@ export const RichTextEditor = function RichTextEditor({
|
|
|
738
743
|
<TablePastePlugin />
|
|
739
744
|
<CodeBlockHeaderPlugin codeLanguages={codeLanguages} />
|
|
740
745
|
<CodeSyntaxPlugin codeLanguages={codeLanguages} />
|
|
746
|
+
<BlockDirectionPlugin />
|
|
741
747
|
{/* Backspace at the very start of a code block makes it a
|
|
742
748
|
paragraph, instead of merging it into the block above. */}
|
|
743
749
|
<CodeBackspacePlugin />
|
package/src/RichTextView.tsx
CHANGED
|
@@ -39,6 +39,7 @@ import {TableScrollRegionPlugin} from './internal/TableScrollRegionPlugin';
|
|
|
39
39
|
import {TableColumnFloorPlugin} from './internal/TableColumnFloorPlugin';
|
|
40
40
|
import {CodeBlockHeaderPlugin} from './internal/CodeBlockHeaderPlugin';
|
|
41
41
|
import {CodeSyntaxPlugin} from './internal/CodeSyntaxPlugin';
|
|
42
|
+
import {BlockDirectionPlugin} from './internal/BlockDirectionPlugin';
|
|
42
43
|
import {MarkdownExtensionsPlugin} from './internal/MarkdownExtensionsPlugin';
|
|
43
44
|
import type {RichTextMarkdownExtension} from './markdownExtensions';
|
|
44
45
|
import type {CodeLanguage} from '@astryxdesign/core/CodeBlock';
|
|
@@ -56,6 +57,10 @@ import {DEFAULT_NODES} from './editorNodes';
|
|
|
56
57
|
|
|
57
58
|
const styles = stylex.create({
|
|
58
59
|
root: {
|
|
60
|
+
// The document's own direction, read where the surface sits. A block
|
|
61
|
+
// whose text decides nothing reads this rather than inheriting, so a
|
|
62
|
+
// list or table that reads the other way cannot change it.
|
|
63
|
+
'--_richtext-document-direction': {default: 'ltr', ':dir(rtl)': 'rtl'},
|
|
59
64
|
width: '100%',
|
|
60
65
|
// Holds the code block headers and task checkboxes drawn over the
|
|
61
66
|
// content.
|
|
@@ -335,6 +340,7 @@ export function RichTextView({
|
|
|
335
340
|
in tab order. */}
|
|
336
341
|
<CodeBlockHeaderPlugin codeLanguages={codeLanguages} />
|
|
337
342
|
<CodeSyntaxPlugin codeLanguages={codeLanguages} />
|
|
343
|
+
<BlockDirectionPlugin />
|
|
338
344
|
{markdownExtensions != null && markdownExtensions.length > 0 ? (
|
|
339
345
|
<MarkdownExtensionsPlugin
|
|
340
346
|
extensions={markdownExtensions}
|