@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
|
@@ -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,11 +15,12 @@
|
|
|
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
|
|
|
19
22
|
import * as stylex from '@stylexjs/stylex';
|
|
20
|
-
import {useEffect, useState, type JSX} from 'react';
|
|
23
|
+
import {useCallback, useEffect, useRef, useState, type JSX} from 'react';
|
|
21
24
|
import {useLexicalComposerContext} from '@lexical/react/LexicalComposerContext';
|
|
22
25
|
import {$isCodeNode} from '@lexical/code';
|
|
23
26
|
import {$getNodeByKey, $getRoot} from 'lexical';
|
|
@@ -25,6 +28,7 @@ import {Icon} from '@astryxdesign/core/Icon';
|
|
|
25
28
|
import {IconButton} from '@astryxdesign/core/IconButton';
|
|
26
29
|
import {useClipboard} from '@astryxdesign/core/hooks';
|
|
27
30
|
import {useTranslator} from '@astryxdesign/core/i18n';
|
|
31
|
+
import {observeResize} from '@astryxdesign/core/utils';
|
|
28
32
|
import type {CodeLanguage} from '@astryxdesign/core/CodeBlock';
|
|
29
33
|
import {listedLanguage} from '../codeLanguages';
|
|
30
34
|
import {
|
|
@@ -67,6 +71,8 @@ const styles = stylex.create({
|
|
|
67
71
|
cornerButton: {
|
|
68
72
|
pointerEvents: 'auto',
|
|
69
73
|
},
|
|
74
|
+
// A long label wraps and keeps its own spacing, as core CodeBlock's title
|
|
75
|
+
// does. The header grows with it.
|
|
70
76
|
title: {
|
|
71
77
|
fontSize: typeScaleVars['--text-supporting-size'],
|
|
72
78
|
fontFamily: typographyVars['--font-family-code'],
|
|
@@ -74,9 +80,9 @@ const styles = stylex.create({
|
|
|
74
80
|
lineHeight: typeScaleVars['--text-supporting-leading'],
|
|
75
81
|
color: 'var(--color-syntax-comment)',
|
|
76
82
|
minWidth: 0,
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
83
|
+
whiteSpace: 'pre-wrap',
|
|
84
|
+
overflowWrap: 'anywhere',
|
|
85
|
+
'--_astryx-text-overflow-wrap': 'anywhere',
|
|
80
86
|
},
|
|
81
87
|
copyButton: {
|
|
82
88
|
color: 'var(--color-syntax-comment)',
|
|
@@ -122,10 +128,13 @@ function samePlacements(
|
|
|
122
128
|
function CodeBlockHeader({
|
|
123
129
|
placement,
|
|
124
130
|
label,
|
|
131
|
+
registerHeader,
|
|
125
132
|
}: {
|
|
126
133
|
readonly placement: HeaderPlacement;
|
|
127
134
|
/** The matched language value's name, when a listed value claims the fence. */
|
|
128
135
|
readonly label?: string;
|
|
136
|
+
/** Hands the plugin the drawn header, which its block reserves room for. */
|
|
137
|
+
readonly registerHeader: (key: string, parts: HeaderParts) => () => void;
|
|
129
138
|
}): JSX.Element {
|
|
130
139
|
const [editor] = useLexicalComposerContext();
|
|
131
140
|
const t = useTranslator();
|
|
@@ -162,6 +171,18 @@ function CodeBlockHeader({
|
|
|
162
171
|
placement.left,
|
|
163
172
|
placement.width,
|
|
164
173
|
);
|
|
174
|
+
const titleRef = useRef<HTMLSpanElement>(null);
|
|
175
|
+
// React attaches a child's ref before its parent's, so the label is set
|
|
176
|
+
// when the header's ref runs.
|
|
177
|
+
const headerRef = useCallback(
|
|
178
|
+
(header: HTMLDivElement | null) => {
|
|
179
|
+
const title = titleRef.current;
|
|
180
|
+
return header == null || title == null
|
|
181
|
+
? undefined
|
|
182
|
+
: registerHeader(placement.key, {header, title});
|
|
183
|
+
},
|
|
184
|
+
[registerHeader, placement.key],
|
|
185
|
+
);
|
|
165
186
|
if (placement.language == null) {
|
|
166
187
|
return (
|
|
167
188
|
<div
|
|
@@ -173,14 +194,82 @@ function CodeBlockHeader({
|
|
|
173
194
|
}
|
|
174
195
|
return (
|
|
175
196
|
<div
|
|
197
|
+
ref={headerRef}
|
|
176
198
|
data-richtext-code-header=""
|
|
177
199
|
{...stylex.props(styles.header, position)}>
|
|
178
|
-
<span {...stylex.props(styles.title)}>
|
|
200
|
+
<span ref={titleRef} {...stylex.props(styles.title)}>
|
|
201
|
+
{label ?? placement.language}
|
|
202
|
+
</span>
|
|
179
203
|
{copyButton}
|
|
180
204
|
</div>
|
|
181
205
|
);
|
|
182
206
|
}
|
|
183
207
|
|
|
208
|
+
/** The variable the code block's theme class reserves the header with. */
|
|
209
|
+
const HEADER_BLOCK_SIZE = '--_richtext-code-header-block-size';
|
|
210
|
+
/** The variable the code block's theme class takes its least width from. */
|
|
211
|
+
const HEADER_INLINE_SIZE = '--_richtext-code-header-inline-size';
|
|
212
|
+
|
|
213
|
+
/** A drawn language header and its label. */
|
|
214
|
+
interface HeaderParts {
|
|
215
|
+
header: HTMLDivElement;
|
|
216
|
+
title: HTMLSpanElement;
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
/**
|
|
220
|
+
* The header's width with its label on one line: its padding, the gaps, the
|
|
221
|
+
* controls beside the label, and the label's one-line width.
|
|
222
|
+
*/
|
|
223
|
+
function naturalWidth({header, title}: HeaderParts, label: number): number {
|
|
224
|
+
const style = getComputedStyle(header);
|
|
225
|
+
// A control's tooltip layer, its marker or a dialog sits beside it in the
|
|
226
|
+
// header but takes no room in the row.
|
|
227
|
+
const others = [...header.children].filter(
|
|
228
|
+
child => child !== title && !child.matches('[popover], template, dialog'),
|
|
229
|
+
);
|
|
230
|
+
const gap = parseFloat(style.columnGap) || 0;
|
|
231
|
+
return (
|
|
232
|
+
parseFloat(style.paddingInlineStart) +
|
|
233
|
+
parseFloat(style.paddingInlineEnd) +
|
|
234
|
+
others.reduce((sum, child) => sum + (child as HTMLElement).offsetWidth, 0) +
|
|
235
|
+
gap * others.length +
|
|
236
|
+
label
|
|
237
|
+
);
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
/**
|
|
241
|
+
* The width of `title`'s text on one line, in its own font, or null where no
|
|
242
|
+
* canvas measures text (a DOM without layout). Chromium returns an empty
|
|
243
|
+
* `font` shorthand when the text inherits a font-variant, feature-settings or
|
|
244
|
+
* kerning value. So the font is built from its longhands. Letter spacing is
|
|
245
|
+
* added per character. Core's table measure works the same way. Core's
|
|
246
|
+
* internal entries are for Core and Lab only, so RichText keeps this copy.
|
|
247
|
+
*/
|
|
248
|
+
function oneLineWidth(
|
|
249
|
+
title: HTMLSpanElement,
|
|
250
|
+
canvas: {current: CanvasRenderingContext2D | null | undefined},
|
|
251
|
+
): number | null {
|
|
252
|
+
if (canvas.current === undefined) {
|
|
253
|
+
canvas.current = document.createElement('canvas').getContext('2d');
|
|
254
|
+
}
|
|
255
|
+
const context = canvas.current;
|
|
256
|
+
if (context == null) {
|
|
257
|
+
return null;
|
|
258
|
+
}
|
|
259
|
+
const style = getComputedStyle(title);
|
|
260
|
+
if (style.fontSize === '' || style.fontFamily === '') {
|
|
261
|
+
return null;
|
|
262
|
+
}
|
|
263
|
+
context.font = [style.fontStyle, style.fontWeight, style.fontSize]
|
|
264
|
+
.filter(part => part !== '')
|
|
265
|
+
.concat(style.fontFamily)
|
|
266
|
+
.join(' ');
|
|
267
|
+
const text = title.textContent ?? '';
|
|
268
|
+
const spacing =
|
|
269
|
+
style.letterSpacing === 'normal' ? 0 : parseFloat(style.letterSpacing) || 0;
|
|
270
|
+
return context.measureText(text).width + spacing * text.length;
|
|
271
|
+
}
|
|
272
|
+
|
|
184
273
|
/**
|
|
185
274
|
* Draws a header over each top-level code block and keeps it there as the
|
|
186
275
|
* document, the editor's size, or the window changes.
|
|
@@ -195,6 +284,35 @@ export function CodeBlockHeaderPlugin({
|
|
|
195
284
|
const [placements, setPlacements] = useState<ReadonlyArray<HeaderPlacement>>(
|
|
196
285
|
[],
|
|
197
286
|
);
|
|
287
|
+
// The drawn language headers, by code block key. The measure frame reads
|
|
288
|
+
// their sizes with the blocks' boxes. Then it writes each size on its block.
|
|
289
|
+
// The block's top padding reserves the height, and its least width is the
|
|
290
|
+
// header's own.
|
|
291
|
+
const headersRef = useRef(new Map<string, HeaderParts>());
|
|
292
|
+
const canvasRef = useRef<CanvasRenderingContext2D | null | undefined>(
|
|
293
|
+
undefined,
|
|
294
|
+
);
|
|
295
|
+
const scheduleRef = useRef<(() => void) | null>(null);
|
|
296
|
+
const registerHeader = useCallback(
|
|
297
|
+
(key: string, parts: HeaderParts) => {
|
|
298
|
+
headersRef.current.set(key, parts);
|
|
299
|
+
// A header that wraps to a new line count measures again; the shared
|
|
300
|
+
// observer only schedules the frame.
|
|
301
|
+
const unobserve = observeResize(parts.header, () =>
|
|
302
|
+
scheduleRef.current?.(),
|
|
303
|
+
);
|
|
304
|
+
return () => {
|
|
305
|
+
unobserve();
|
|
306
|
+
if (headersRef.current.get(key) === parts) {
|
|
307
|
+
headersRef.current.delete(key);
|
|
308
|
+
}
|
|
309
|
+
const block = editor.getElementByKey(key);
|
|
310
|
+
block?.style.removeProperty(HEADER_BLOCK_SIZE);
|
|
311
|
+
block?.style.removeProperty(HEADER_INLINE_SIZE);
|
|
312
|
+
};
|
|
313
|
+
},
|
|
314
|
+
[editor],
|
|
315
|
+
);
|
|
198
316
|
|
|
199
317
|
useEffect(() => {
|
|
200
318
|
let frame = 0;
|
|
@@ -207,6 +325,23 @@ export function CodeBlockHeaderPlugin({
|
|
|
207
325
|
return;
|
|
208
326
|
}
|
|
209
327
|
const origin = container.getBoundingClientRect();
|
|
328
|
+
// Each header's drawn height, and the width it takes with its label
|
|
329
|
+
// on one line, border included, as core's frame grows to fit it.
|
|
330
|
+
const sizes = [...headersRef.current].map(([key, parts]) => {
|
|
331
|
+
const block = editor.getElementByKey(key);
|
|
332
|
+
const border =
|
|
333
|
+
block == null ? 0 : block.offsetWidth - block.clientWidth;
|
|
334
|
+
const label = oneLineWidth(parts.title, canvasRef);
|
|
335
|
+
return {
|
|
336
|
+
key,
|
|
337
|
+
block,
|
|
338
|
+
height: `${parts.header.offsetHeight}px`,
|
|
339
|
+
width:
|
|
340
|
+
label == null
|
|
341
|
+
? null
|
|
342
|
+
: `${Math.ceil(naturalWidth(parts, label) + border)}px`,
|
|
343
|
+
};
|
|
344
|
+
});
|
|
210
345
|
const next = editor.getEditorState().read(() =>
|
|
211
346
|
$getRoot()
|
|
212
347
|
.getChildren()
|
|
@@ -230,6 +365,22 @@ export function CodeBlockHeaderPlugin({
|
|
|
230
365
|
];
|
|
231
366
|
}),
|
|
232
367
|
);
|
|
368
|
+
// Writes after every read in this frame. A block that grows moves the
|
|
369
|
+
// blocks below it; the root's resize measures them again next frame.
|
|
370
|
+
for (const {block, height, width} of sizes) {
|
|
371
|
+
if (block == null) {
|
|
372
|
+
continue;
|
|
373
|
+
}
|
|
374
|
+
if (block.style.getPropertyValue(HEADER_BLOCK_SIZE) !== height) {
|
|
375
|
+
block.style.setProperty(HEADER_BLOCK_SIZE, height);
|
|
376
|
+
}
|
|
377
|
+
if (
|
|
378
|
+
width != null &&
|
|
379
|
+
block.style.getPropertyValue(HEADER_INLINE_SIZE) !== width
|
|
380
|
+
) {
|
|
381
|
+
block.style.setProperty(HEADER_INLINE_SIZE, width);
|
|
382
|
+
}
|
|
383
|
+
}
|
|
233
384
|
setPlacements(current =>
|
|
234
385
|
samePlacements(current, next) ? current : next,
|
|
235
386
|
);
|
|
@@ -239,15 +390,11 @@ export function CodeBlockHeaderPlugin({
|
|
|
239
390
|
frame = requestAnimationFrame(measure);
|
|
240
391
|
}
|
|
241
392
|
};
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
? null
|
|
245
|
-
: new ResizeObserver(schedule);
|
|
393
|
+
scheduleRef.current = schedule;
|
|
394
|
+
let unobserveRoot: (() => void) | null = null;
|
|
246
395
|
const unregisterRoot = editor.registerRootListener(root => {
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
observer?.observe(root);
|
|
250
|
-
}
|
|
396
|
+
unobserveRoot?.();
|
|
397
|
+
unobserveRoot = root == null ? null : observeResize(root, schedule);
|
|
251
398
|
schedule();
|
|
252
399
|
});
|
|
253
400
|
const unregisterUpdate = editor.registerUpdateListener(schedule);
|
|
@@ -255,7 +402,8 @@ export function CodeBlockHeaderPlugin({
|
|
|
255
402
|
schedule();
|
|
256
403
|
return () => {
|
|
257
404
|
cancelAnimationFrame(frame);
|
|
258
|
-
|
|
405
|
+
unobserveRoot?.();
|
|
406
|
+
scheduleRef.current = null;
|
|
259
407
|
unregisterRoot();
|
|
260
408
|
unregisterUpdate();
|
|
261
409
|
window.removeEventListener('resize', schedule);
|
|
@@ -268,6 +416,7 @@ export function CodeBlockHeaderPlugin({
|
|
|
268
416
|
<CodeBlockHeader
|
|
269
417
|
key={placement.key}
|
|
270
418
|
placement={placement}
|
|
419
|
+
registerHeader={registerHeader}
|
|
271
420
|
// A fence matched in `codeLanguages` shows the value's name, as
|
|
272
421
|
// Markdown does (spec:AST-061).
|
|
273
422
|
label={
|
|
@@ -0,0 +1,111 @@
|
|
|
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(
|
|
40
|
+
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}]`,
|
|
41
|
+
'u',
|
|
42
|
+
);
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* The characters the scan passes over: Bidi_Class `AN`, `EN`, `ET` and
|
|
46
|
+
* `ON`, none of which decides a direction. Most are the right-to-left
|
|
47
|
+
* scripts' own, and the digits that are weak everywhere are here too: the
|
|
48
|
+
* ASCII, fullwidth and mathematical ones are `EN`.
|
|
49
|
+
*
|
|
50
|
+
* The first group is the Arabic script's own: its digits, the number signs
|
|
51
|
+
* before them, and the currency and per-cent marks beside them.
|
|
52
|
+
*
|
|
53
|
+
* The rest sit inside the two supplementary ranges above, which are drawn
|
|
54
|
+
* around whole planes and so sweep up every digit and mark those scripts
|
|
55
|
+
* carry as well as their letters: the Phoenician word separator, Avestan
|
|
56
|
+
* punctuation, the Hanifi Rohingya, Garay, Yezidi and Rumi digits, a Garay
|
|
57
|
+
* mark, and the Arabic mathematical operators. Each one is verified against
|
|
58
|
+
* the browser's own implementation of the algorithm in
|
|
59
|
+
* firstStrongDirection.test.ts.
|
|
60
|
+
*
|
|
61
|
+
* Membership here is about Bidi_Class, never about looking like a number.
|
|
62
|
+
* The Indic Siyaq and Ottoman Siyaq numerals sit beside Rumi and are
|
|
63
|
+
* Bidi_Class `AL`, so they stay strong; NKo and Adlam digits are `R`, so
|
|
64
|
+
* they do too.
|
|
65
|
+
*/
|
|
66
|
+
const WEAK_NUMBER_OR_NEUTRAL =
|
|
67
|
+
/[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;
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* Bidi_Class `L`: every other letter, the letter numbers (`\p{Letter}` does
|
|
71
|
+
* not include them), the left-to-right mark, and the decimal digits.
|
|
72
|
+
*
|
|
73
|
+
* Most scripts' digits are `L` — Devanagari, Bengali, Tamil, Thai and the
|
|
74
|
+
* rest — so a line opening with them reads left-to-right. The digits that
|
|
75
|
+
* are not are read before this: the European ones are `EN` and the
|
|
76
|
+
* Arabic-Indic ones `AN`, both in the weak set above, and NKo's and Adlam's
|
|
77
|
+
* are `R`, matched as their own scripts. Punctuation and symbols are not
|
|
78
|
+
* here, so the scan passes over them.
|
|
79
|
+
*/
|
|
80
|
+
const STRONG_LTR = new RegExp(String.raw`[\p{Letter}\p{Nl}\p{Nd}\u200E]`, 'u');
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* Bidi_Class `NSM`: a combining mark takes the direction of its base
|
|
84
|
+
* character. A mark has its base letter's script, so without this check a
|
|
85
|
+
* leading Hebrew point would read as right-to-left.
|
|
86
|
+
*/
|
|
87
|
+
const COMBINING_MARK = new RegExp(String.raw`\p{Mark}`, 'u');
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* Approximates rules P2 and P3 of the Unicode Bidirectional Algorithm from
|
|
91
|
+
* script and category: the direction of the first strongly directional
|
|
92
|
+
* character, or null when there is none. It does not skip isolated runs. The
|
|
93
|
+
* loop reads whole code points, so a surrogate pair counts as one character.
|
|
94
|
+
*/
|
|
95
|
+
export function firstStrongDirection(text: string): 'ltr' | 'rtl' | null {
|
|
96
|
+
for (const character of text) {
|
|
97
|
+
if (
|
|
98
|
+
COMBINING_MARK.test(character) ||
|
|
99
|
+
WEAK_NUMBER_OR_NEUTRAL.test(character)
|
|
100
|
+
) {
|
|
101
|
+
continue;
|
|
102
|
+
}
|
|
103
|
+
if (STRONG_RTL.test(character)) {
|
|
104
|
+
return 'rtl';
|
|
105
|
+
}
|
|
106
|
+
if (STRONG_LTR.test(character)) {
|
|
107
|
+
return 'ltr';
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
return null;
|
|
111
|
+
}
|