@glpecile/norph 0.1.0 ā 0.2.0
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/README.md +11 -4
- package/package.json +4 -2
- package/src/diff.ts +80 -75
- package/src/index.tsx +118 -60
package/README.md
CHANGED
|
@@ -4,7 +4,7 @@ Text that morphs into its next value, on iOS, Android, and web. It is a
|
|
|
4
4
|
Reanimated port of [torph](https://github.com/lochie/torph)'s React component.
|
|
5
5
|
One implementation runs everywhere, on the web through react-native-web.
|
|
6
6
|
|
|
7
|
-
[Try the live demo](https://glpecile.
|
|
7
|
+
[Try the live demo](https://norph.glpecile.xyz).
|
|
8
8
|
|
|
9
9
|
## Install
|
|
10
10
|
|
|
@@ -29,9 +29,14 @@ import { MorphText } from '@glpecile/norph';
|
|
|
29
29
|
## Behavior
|
|
30
30
|
|
|
31
31
|
- Words and characters that the old and new text share slide to their new
|
|
32
|
-
places. New ones
|
|
32
|
+
places. New ones fade in, dropped ones fade out, and the box animates its
|
|
33
|
+
width. As in torph 0.2.0, text does not scale; a long run that is replaced
|
|
34
|
+
as a whole still shrinks and grows as one block.
|
|
35
|
+
- Glyphs move to the places the plain text has, kerning included, so the text
|
|
36
|
+
does not jump when the morph ends. Emoji sequences and flags stay one glyph.
|
|
33
37
|
- Numbers match digits by place value and align on the decimal point. A
|
|
34
|
-
changed digit rolls in a clipped slot. Other characters crossfade.
|
|
38
|
+
changed digit rolls in a clipped slot. Other characters crossfade. A
|
|
39
|
+
no-break space separates words, so `1.156,11 ā¬` keeps its digits.
|
|
35
40
|
- The first render does not animate. At rest, and with reduced motion or
|
|
36
41
|
`numberOfLines`, it renders one plain `Text`.
|
|
37
42
|
- Screen readers read the whole string once.
|
|
@@ -52,7 +57,8 @@ import { MorphText } from '@glpecile/norph';
|
|
|
52
57
|
an ellipsis. Newlines are not supported.
|
|
53
58
|
- While it morphs, the text sits at the start edge. Shrink-wrap it
|
|
54
59
|
(`alignSelf`) when alignment matters.
|
|
55
|
-
-
|
|
60
|
+
- Glyphs keep the font's kerning while they move, but a ligature such as `fi`
|
|
61
|
+
shows as separate letters until the morph ends.
|
|
56
62
|
- Unlike torph, the digit slot has a hard edge (no gradient mask) and a morph
|
|
57
63
|
that is interrupted does not carry its velocity over.
|
|
58
64
|
|
|
@@ -66,6 +72,7 @@ The package ships TypeScript source for Metro to compile.
|
|
|
66
72
|
`bun test` runs the diff tests and `bun run typecheck` checks types. The demo
|
|
67
73
|
lives in `example/`: `cd example && bun install && bun web` runs it, and
|
|
68
74
|
`bun run export` builds it into `site/demo/` for the site in `site/`.
|
|
75
|
+
`bun run deploy` builds the demo and deploys the site to norph.glpecile.xyz.
|
|
69
76
|
|
|
70
77
|
## License
|
|
71
78
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@glpecile/norph",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.2.0",
|
|
4
4
|
"description": "Text that morphs into its next value, on iOS, Android, and web. A Reanimated port of torph.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": "Gian Luca Pecile",
|
|
@@ -19,7 +19,9 @@
|
|
|
19
19
|
"files": ["src", "!src/**/*.test.ts"],
|
|
20
20
|
"scripts": {
|
|
21
21
|
"test": "bun test",
|
|
22
|
-
"typecheck": "tsc --noEmit"
|
|
22
|
+
"typecheck": "tsc --noEmit",
|
|
23
|
+
"build:site": "bun build scripts/site-morph.ts --outfile site/morph.js --minify --target browser && cd example && bun run export",
|
|
24
|
+
"deploy": "bun run build:site && bunx wrangler deploy"
|
|
23
25
|
},
|
|
24
26
|
"peerDependencies": {
|
|
25
27
|
"react": ">=19",
|
package/src/diff.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
// Ported from torph 0.1.3 (MIT, Lochie Axon): `segmentText`, `diffSegments`
|
|
2
|
-
// and `segmentNumber`, without `cursorIndex
|
|
3
|
-
// component renders one line.
|
|
2
|
+
// and `segmentNumber`, without `cursorIndex`, with the diff fixes of torph
|
|
3
|
+
// 0.2.0. Newlines are not supported: the component renders one line.
|
|
4
4
|
|
|
5
5
|
export type SegmentKind = 'digit' | 'symbol';
|
|
6
6
|
|
|
@@ -45,6 +45,14 @@ const hasDigit = (text: string) => Array.from(text).some(isDigit);
|
|
|
45
45
|
const kindOf = (char: string): SegmentKind => (isDigit(char) ? 'digit' : 'symbol');
|
|
46
46
|
const affix = (char: string, list: string) => list.includes(char) || CURRENCY.test(char);
|
|
47
47
|
|
|
48
|
+
// ponytail: approximates extended grapheme clusters; Hangul jamo and some
|
|
49
|
+
// Indic conjuncts split. Use Intl.Segmenter once Hermes ships it.
|
|
50
|
+
const GRAPHEME =
|
|
51
|
+
/\p{RI}\p{RI}|[^\p{M}\u200d][\p{M}\p{Emoji_Modifier}\u{E0020}-\u{E007F}]*(?:\u200d[^\p{M}\u200d][\p{M}\p{Emoji_Modifier}]*)*|[\s\S]/gu;
|
|
52
|
+
|
|
53
|
+
/** The characters a reader sees, so `šØāš©āš§` or `e` with a combining accent stays one glyph. */
|
|
54
|
+
const graphemes = (text: string) => text.match(GRAPHEME) ?? [];
|
|
55
|
+
|
|
48
56
|
/** The text without digits and digit separators, so `S01E04` and `S01E05` compare equal. */
|
|
49
57
|
const letters = (text: string) =>
|
|
50
58
|
Array.from(text)
|
|
@@ -53,27 +61,47 @@ const letters = (text: string) =>
|
|
|
53
61
|
|
|
54
62
|
/** Index pairs of a longest common subsequence, in order. */
|
|
55
63
|
export function lcs<T>(a: T[], b: T[]): [number[], number[]] {
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
64
|
+
let start = 0;
|
|
65
|
+
while (start < a.length && start < b.length && a[start] === b[start]) start++;
|
|
66
|
+
let aEnd = a.length;
|
|
67
|
+
let bEnd = b.length;
|
|
68
|
+
while (aEnd > start && bEnd > start && a[aEnd - 1] === b[bEnd - 1]) {
|
|
69
|
+
aEnd--;
|
|
70
|
+
bEnd--;
|
|
62
71
|
}
|
|
63
72
|
const from: number[] = [];
|
|
64
73
|
const to: number[] = [];
|
|
74
|
+
for (let k = 0; k < start; k++) {
|
|
75
|
+
from.push(k);
|
|
76
|
+
to.push(k);
|
|
77
|
+
}
|
|
78
|
+
const rows = aEnd - start;
|
|
79
|
+
const columns = bEnd - start + 1;
|
|
80
|
+
const suffix = new Uint32Array((rows + 1) * columns);
|
|
81
|
+
for (let i = rows - 1; i >= 0; i--) {
|
|
82
|
+
for (let j = columns - 2; j >= 0; j--) {
|
|
83
|
+
suffix[i * columns + j] =
|
|
84
|
+
a[start + i] === b[start + j]
|
|
85
|
+
? suffix[(i + 1) * columns + j + 1] + 1
|
|
86
|
+
: Math.max(suffix[(i + 1) * columns + j], suffix[i * columns + j + 1]);
|
|
87
|
+
}
|
|
88
|
+
}
|
|
65
89
|
let i = 0;
|
|
66
90
|
let j = 0;
|
|
67
|
-
while (i <
|
|
68
|
-
if (a[i] === b[j]) {
|
|
69
|
-
from.push(i++);
|
|
70
|
-
to.push(j++);
|
|
71
|
-
} else if (suffix[i + 1
|
|
91
|
+
while (i < rows && j < columns - 1) {
|
|
92
|
+
if (a[start + i] === b[start + j]) {
|
|
93
|
+
from.push(start + i++);
|
|
94
|
+
to.push(start + j++);
|
|
95
|
+
} else if (suffix[(i + 1) * columns + j] >= suffix[i * columns + j + 1]) {
|
|
72
96
|
i++;
|
|
73
97
|
} else {
|
|
74
98
|
j++;
|
|
75
99
|
}
|
|
76
100
|
}
|
|
101
|
+
for (let k = 0; k < a.length - aEnd; k++) {
|
|
102
|
+
from.push(aEnd + k);
|
|
103
|
+
to.push(bEnd + k);
|
|
104
|
+
}
|
|
77
105
|
return [from, to];
|
|
78
106
|
}
|
|
79
107
|
|
|
@@ -254,14 +282,14 @@ function words(segments: Segment[]): Word[] {
|
|
|
254
282
|
* string takes its offset as id, so `0` and `0-4` stay apart.
|
|
255
283
|
*/
|
|
256
284
|
export function segmentText(text: string): Segment[] {
|
|
257
|
-
const wordMode = text
|
|
285
|
+
const wordMode = /[ \u00A0]/.test(text);
|
|
258
286
|
const ids = idAllocator();
|
|
259
|
-
const parts = wordMode ? text.split(
|
|
287
|
+
const parts = wordMode ? text.split(/[ \u00A0]/) : graphemes(text);
|
|
260
288
|
const segments: Segment[] = [];
|
|
261
289
|
let offset = 0;
|
|
262
290
|
parts.forEach((part, index) => {
|
|
263
291
|
if (wordMode && index > 0) {
|
|
264
|
-
segments.push({ id: ids.take(
|
|
292
|
+
segments.push({ id: ids.take(`\0s-${offset}`), string: NBSP });
|
|
265
293
|
offset += 1;
|
|
266
294
|
}
|
|
267
295
|
if (part.length > 0) {
|
|
@@ -294,10 +322,11 @@ function splitNumbers(segments: Segment[]) {
|
|
|
294
322
|
}
|
|
295
323
|
|
|
296
324
|
/** Splits a one-segment word into characters with derived ids, recording the split. */
|
|
297
|
-
function splitWord(word: Word, splits: Map<string, Segment[]
|
|
298
|
-
|
|
325
|
+
function splitWord(word: Word, splits: Map<string, Segment[]>, take: (id: string) => string): Segment[] {
|
|
326
|
+
const chars = graphemes(word.word);
|
|
327
|
+
if (word.segments.length !== 1 || chars.length <= 1) return word.segments;
|
|
299
328
|
const [segment] = word.segments;
|
|
300
|
-
const pieces =
|
|
329
|
+
const pieces = chars.map((string, index) => ({ id: take(`${segment.id}:${index}`), string }));
|
|
301
330
|
splits.set(segment.id, pieces);
|
|
302
331
|
return pieces;
|
|
303
332
|
}
|
|
@@ -321,10 +350,6 @@ function rankBefore(length: number, matched: Set<number>) {
|
|
|
321
350
|
return ranks;
|
|
322
351
|
}
|
|
323
352
|
|
|
324
|
-
type WordPlan =
|
|
325
|
-
| { mode: 'fresh' }
|
|
326
|
-
| { mode: 'reuse' | 'number' | 'morph'; from: number };
|
|
327
|
-
|
|
328
353
|
/**
|
|
329
354
|
* Re-segments `text` against the segments on screen. Words pair up by LCS,
|
|
330
355
|
* then by exact match, then by similarity at the same position. A kept word
|
|
@@ -336,15 +361,15 @@ export function diffSegments(previous: Segment[], text: string, locale = 'en'):
|
|
|
336
361
|
const oldWords = words(previous);
|
|
337
362
|
const key = (word: string) => (isNumericWord(word) ? NUMBER_KEY : word);
|
|
338
363
|
const anyDigits = hasDigit(text) || oldWords.some((word) => hasDigit(word.word));
|
|
339
|
-
if (oldWords.length <= 1 &&
|
|
364
|
+
if (oldWords.length <= 1 && !/[ \u00A0]/.test(text) && !anyDigits) {
|
|
340
365
|
return { segments: segmentText(text), splits: new Map() };
|
|
341
366
|
}
|
|
342
367
|
|
|
343
368
|
const newWords: string[] = [];
|
|
344
369
|
const gaps: number[] = [];
|
|
345
370
|
let gap = 0;
|
|
346
|
-
for (const part of text.split(/( )/)) {
|
|
347
|
-
if (part === ' ') {
|
|
371
|
+
for (const part of text.split(/([ \u00A0])/)) {
|
|
372
|
+
if (part === ' ' || part === NBSP) {
|
|
348
373
|
gap++;
|
|
349
374
|
} else if (part.length > 0) {
|
|
350
375
|
gaps.push(gap);
|
|
@@ -397,68 +422,48 @@ export function diffSegments(previous: Segment[], text: string, locale = 'en'):
|
|
|
397
422
|
}
|
|
398
423
|
}
|
|
399
424
|
|
|
400
|
-
const plans = newWords.map((word, n): WordPlan => {
|
|
401
|
-
const kept = exact.get(n);
|
|
402
|
-
const from = kept ?? fuzzy.get(n);
|
|
403
|
-
if (from === undefined) return { mode: 'fresh' };
|
|
404
|
-
if (isNumericWord(word)) return { mode: 'number', from };
|
|
405
|
-
return { mode: kept !== undefined ? 'reuse' : 'morph', from };
|
|
406
|
-
});
|
|
407
|
-
|
|
408
425
|
const ids = idAllocator();
|
|
409
|
-
for (const
|
|
410
|
-
if (
|
|
411
|
-
const word = oldWords[plan.from];
|
|
412
|
-
if (plan.mode !== 'reuse' && word.segments.length === 1) {
|
|
413
|
-
for (let i = 0; i < Array.from(word.word).length; i++) ids.reserve(`${word.segments[0].id}:${i}`);
|
|
414
|
-
} else {
|
|
415
|
-
for (const segment of word.segments) ids.reserve(segment.id);
|
|
416
|
-
}
|
|
426
|
+
for (const segment of previous) {
|
|
427
|
+
if (segment.string !== NBSP) ids.reserve(segment.id);
|
|
417
428
|
}
|
|
418
429
|
|
|
419
|
-
const segments: Segment[] = [];
|
|
420
430
|
const splits = new Map<string, Segment[]>();
|
|
431
|
+
const segments: Segment[] = [];
|
|
421
432
|
const decimal = decimalSeparator(locale);
|
|
422
433
|
let offset = 0;
|
|
423
434
|
const pushSpaces = (count: number) => {
|
|
424
435
|
for (let i = 0; i < count; i++) {
|
|
425
|
-
segments.push({ id: ids.take(
|
|
436
|
+
segments.push({ id: ids.take(`\0s-${offset}`), string: NBSP });
|
|
426
437
|
offset++;
|
|
427
438
|
}
|
|
428
439
|
};
|
|
429
440
|
newWords.forEach((word, n) => {
|
|
430
441
|
pushSpaces(gaps[n]);
|
|
431
|
-
const
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
break;
|
|
457
|
-
}
|
|
458
|
-
case 'fresh':
|
|
459
|
-
if (isNumericWord(word)) segments.push(...segmentNumber(word));
|
|
460
|
-
else segments.push({ id: ids.take(word), string: word });
|
|
461
|
-
break;
|
|
442
|
+
const kept = exact.get(n);
|
|
443
|
+
const from = kept ?? fuzzy.get(n);
|
|
444
|
+
if (from === undefined) {
|
|
445
|
+
if (isNumericWord(word)) segments.push(...segmentNumber(word));
|
|
446
|
+
else segments.push({ id: ids.take(word), string: word });
|
|
447
|
+
} else if (isNumericWord(word)) {
|
|
448
|
+
const pieces = splitWord(oldWords[from], splits, ids.take).map((segment) => ({
|
|
449
|
+
...segment,
|
|
450
|
+
kind: segment.kind ?? kindOf(segment.string),
|
|
451
|
+
}));
|
|
452
|
+
segments.push(...segmentNumber(word, pieces, decimal));
|
|
453
|
+
} else if (kept !== undefined) {
|
|
454
|
+
segments.push(...oldWords[from].segments);
|
|
455
|
+
} else {
|
|
456
|
+
const pieces = splitWord(oldWords[from], splits, ids.take);
|
|
457
|
+
const chars = graphemes(word);
|
|
458
|
+
const [a, b] = lcs(
|
|
459
|
+
pieces.map((piece) => piece.string),
|
|
460
|
+
chars,
|
|
461
|
+
);
|
|
462
|
+
const matched = new Map<number, Segment>();
|
|
463
|
+
b.forEach((to, i) => matched.set(to, pieces[a[i]]));
|
|
464
|
+
chars.forEach((string, i) => {
|
|
465
|
+
segments.push({ id: matched.get(i)?.id ?? ids.take(`${word}~${i}`), string });
|
|
466
|
+
});
|
|
462
467
|
}
|
|
463
468
|
offset += word.length;
|
|
464
469
|
});
|
package/src/index.tsx
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { type ReactNode, useRef, useState } from 'react';
|
|
1
|
+
import { type ReactNode, useLayoutEffect, useMemo, useRef, useState } from 'react';
|
|
2
2
|
import {
|
|
3
3
|
type LayoutRectangle,
|
|
4
4
|
type StyleProp,
|
|
@@ -52,7 +52,7 @@ export interface MorphTextProps {
|
|
|
52
52
|
// torph's defaults.
|
|
53
53
|
const DURATION = 400;
|
|
54
54
|
const EASE = Easing.bezier(0.19, 1, 0.22, 1);
|
|
55
|
-
const EMPTY: Segment = { id: '
|
|
55
|
+
const EMPTY: Segment = { id: '\0empty', string: 'ā' };
|
|
56
56
|
|
|
57
57
|
const glide = (to: number) => withTiming(to, { duration: DURATION, easing: EASE });
|
|
58
58
|
/** A linear fade over `share` of the duration, after `delay` of it. */
|
|
@@ -85,14 +85,18 @@ interface Motion {
|
|
|
85
85
|
height: number;
|
|
86
86
|
}
|
|
87
87
|
|
|
88
|
+
type Row = 'before' | 'after';
|
|
89
|
+
|
|
88
90
|
interface Measures {
|
|
89
|
-
|
|
91
|
+
id: number;
|
|
90
92
|
done: boolean;
|
|
91
93
|
before: Map<string, LayoutRectangle>;
|
|
92
94
|
after: Map<string, LayoutRectangle>;
|
|
95
|
+
ends: Record<Row, Map<string, number>>;
|
|
93
96
|
}
|
|
94
97
|
|
|
95
98
|
let glyphKey = 0;
|
|
99
|
+
let diffId = 0;
|
|
96
100
|
const newGlyph = (segment: Segment, width: number, x: number, from: Partial<Record<'scale' | 'opacity' | 'rollY' | 'rollOpacity', number>> = {}): Glyph => ({
|
|
97
101
|
key: glyphKey++,
|
|
98
102
|
id: segment.id,
|
|
@@ -128,8 +132,23 @@ export function MorphText({ children, className, style, numberOfLines }: MorphTe
|
|
|
128
132
|
const [shown, setShown] = useState(() => ({ text, segments: segmentText(text) }));
|
|
129
133
|
const [motion, setMotion] = useState<Motion | null>(null);
|
|
130
134
|
const measures = useRef<Measures | null>(null);
|
|
135
|
+
const activeDiff = useRef<number | null>(null);
|
|
131
136
|
const generation = useRef(0);
|
|
132
137
|
|
|
138
|
+
const pending = !reduceMotion && numberOfLines == null && shown.text !== text;
|
|
139
|
+
// Keeps segment ids stable across re-renders while onLayout measures a morph.
|
|
140
|
+
const diff = useMemo(
|
|
141
|
+
() => (pending ? { id: ++diffId, ...diffSegments(shown.segments, text) } : null),
|
|
142
|
+
[pending, shown, text],
|
|
143
|
+
);
|
|
144
|
+
const currentDiffId = diff?.id ?? null;
|
|
145
|
+
useLayoutEffect(() => {
|
|
146
|
+
activeDiff.current = currentDiffId;
|
|
147
|
+
return () => {
|
|
148
|
+
activeDiff.current = null;
|
|
149
|
+
};
|
|
150
|
+
}, [currentDiffId]);
|
|
151
|
+
|
|
133
152
|
if (reduceMotion || numberOfLines != null) {
|
|
134
153
|
if (shown.text !== text) {
|
|
135
154
|
setShown({ text, segments: segmentText(text) });
|
|
@@ -142,8 +161,6 @@ export function MorphText({ children, className, style, numberOfLines }: MorphTe
|
|
|
142
161
|
);
|
|
143
162
|
}
|
|
144
163
|
|
|
145
|
-
const pending = shown.text !== text;
|
|
146
|
-
const diff = pending ? diffSegments(shown.segments, text) : null;
|
|
147
164
|
const before = diff
|
|
148
165
|
? shown.segments.flatMap((segment) => diff.splits.get(segment.id) ?? [segment])
|
|
149
166
|
: [];
|
|
@@ -153,7 +170,7 @@ export function MorphText({ children, className, style, numberOfLines }: MorphTe
|
|
|
153
170
|
if (generation.current === done) setMotion(null);
|
|
154
171
|
};
|
|
155
172
|
|
|
156
|
-
const start = ({ before: previous, after: next }:
|
|
173
|
+
const start = ({ before: previous, after: next }: Record<Row, Map<string, Box & { height: number }>>) => {
|
|
157
174
|
const moves = planMoves(
|
|
158
175
|
before.map((segment) => segment.id),
|
|
159
176
|
after,
|
|
@@ -181,7 +198,6 @@ export function MorphText({ children, className, style, numberOfLines }: MorphTe
|
|
|
181
198
|
glyph.rollOpacity.set(fade(0, 0.45));
|
|
182
199
|
} else {
|
|
183
200
|
glyph.x.set(glide(move.x));
|
|
184
|
-
glyph.scale.set(glide(0.95));
|
|
185
201
|
glyph.opacity.set(fade(0, 0.25));
|
|
186
202
|
}
|
|
187
203
|
glyphs.push(glyph);
|
|
@@ -216,9 +232,8 @@ export function MorphText({ children, className, style, numberOfLines }: MorphTe
|
|
|
216
232
|
glyph.rollOpacity.set(fade(1, 0.25));
|
|
217
233
|
glyphs.push(glyph);
|
|
218
234
|
} else {
|
|
219
|
-
const glyph = newGlyph(segment, box.width, move.x, {
|
|
235
|
+
const glyph = newGlyph(segment, box.width, move.x, { opacity: 0 });
|
|
220
236
|
glyph.x.set(glide(box.x));
|
|
221
|
-
glyph.scale.set(glide(1));
|
|
222
237
|
glyph.opacity.set(fade(1, 0.5, 0.25));
|
|
223
238
|
glyphs.push(glyph);
|
|
224
239
|
}
|
|
@@ -240,59 +255,96 @@ export function MorphText({ children, className, style, numberOfLines }: MorphTe
|
|
|
240
255
|
setShown({ text, segments: after });
|
|
241
256
|
};
|
|
242
257
|
|
|
243
|
-
const measure = (
|
|
244
|
-
if (
|
|
245
|
-
|
|
258
|
+
const measure = (targetId: number, row: Row, id: string, layout: LayoutRectangle, end: boolean) => {
|
|
259
|
+
if (targetId !== activeDiff.current) return;
|
|
260
|
+
if (measures.current?.id !== targetId) {
|
|
261
|
+
measures.current = {
|
|
262
|
+
id: targetId,
|
|
263
|
+
done: false,
|
|
264
|
+
before: new Map(),
|
|
265
|
+
after: new Map(),
|
|
266
|
+
ends: { before: new Map(), after: new Map() },
|
|
267
|
+
};
|
|
246
268
|
}
|
|
247
269
|
const current = measures.current;
|
|
248
|
-
current[row].set(id, layout);
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
270
|
+
if (end) current.ends[row].set(id, layout.width);
|
|
271
|
+
else current[row].set(id, layout);
|
|
272
|
+
const complete = (key: Row, segments: Segment[]) =>
|
|
273
|
+
segments.every((segment) => current[key].has(segment.id) && current.ends[key].has(segment.id));
|
|
274
|
+
if (current.done || !complete('before', before) || !complete('after', after)) return;
|
|
275
|
+
current.done = true;
|
|
276
|
+
const place = (key: Row, segments: Segment[]) =>
|
|
277
|
+
new Map(
|
|
278
|
+
segments.map((segment) => {
|
|
279
|
+
const box = current[key].get(segment.id)!;
|
|
280
|
+
return [
|
|
281
|
+
segment.id,
|
|
282
|
+
{ width: box.width, height: box.height, x: Math.max(0, current.ends[key].get(segment.id)! - box.width) },
|
|
283
|
+
];
|
|
284
|
+
}),
|
|
285
|
+
);
|
|
286
|
+
start({ before: place('before', before), after: place('after', after) });
|
|
253
287
|
};
|
|
254
288
|
|
|
289
|
+
// One line at rest too, so a morph never unwraps the text. While the glyphs
|
|
290
|
+
// move, a copy of it in the sizer is the reader, so the box keeps its baseline.
|
|
291
|
+
const label = (
|
|
292
|
+
<Text className={className} style={[style, RESET, motion && styles.reader]} numberOfLines={1}>
|
|
293
|
+
{shown.text}
|
|
294
|
+
</Text>
|
|
295
|
+
);
|
|
296
|
+
|
|
255
297
|
return (
|
|
256
|
-
<View className={className} style={style}>
|
|
298
|
+
<View className={className} style={[styles.root, style]}>
|
|
257
299
|
{motion ? (
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
))}
|
|
269
|
-
</Sizer>
|
|
270
|
-
<Text style={styles.reader} numberOfLines={1}>
|
|
271
|
-
{shown.text}
|
|
272
|
-
</Text>
|
|
273
|
-
</>
|
|
274
|
-
) : (
|
|
275
|
-
// One line at rest too, so a morph never unwraps the text.
|
|
276
|
-
<Text className={className} style={[style, RESET]} numberOfLines={1}>
|
|
277
|
-
{shown.text}
|
|
278
|
-
</Text>
|
|
279
|
-
)}
|
|
280
|
-
{pending ? (
|
|
281
|
-
<View key={text} style={styles.measure} aria-hidden>
|
|
282
|
-
{(['before', 'after'] as const).map((row) => (
|
|
283
|
-
<View key={row} style={styles.row}>
|
|
284
|
-
{(row === 'before' ? before : after).map((segment) => (
|
|
285
|
-
<Text
|
|
286
|
-
key={segment.id}
|
|
287
|
-
className={className}
|
|
288
|
-
style={[style, RESET]}
|
|
289
|
-
onLayout={(event) => measure(row, segment.id, event.nativeEvent.layout)}
|
|
290
|
-
>
|
|
291
|
-
{segment.string}
|
|
292
|
-
</Text>
|
|
293
|
-
))}
|
|
294
|
-
</View>
|
|
300
|
+
<Sizer width={motion.width} height={motion.height}>
|
|
301
|
+
{label}
|
|
302
|
+
{motion.glyphs.map((glyph) => (
|
|
303
|
+
<GlyphView
|
|
304
|
+
key={glyph.key}
|
|
305
|
+
glyph={glyph}
|
|
306
|
+
height={motion.height}
|
|
307
|
+
className={className}
|
|
308
|
+
style={style}
|
|
309
|
+
/>
|
|
295
310
|
))}
|
|
311
|
+
</Sizer>
|
|
312
|
+
) : label}
|
|
313
|
+
{diff ? (
|
|
314
|
+
<View key={diff.id} style={styles.measure} aria-hidden>
|
|
315
|
+
{(['before', 'after'] as const).map((row) => {
|
|
316
|
+
const segments = row === 'before' ? before : after;
|
|
317
|
+
let prefix = '';
|
|
318
|
+
return (
|
|
319
|
+
<View key={row} style={styles.row}>
|
|
320
|
+
{segments.map((segment) => (
|
|
321
|
+
<Text
|
|
322
|
+
key={`s:${segment.id}`}
|
|
323
|
+
className={className}
|
|
324
|
+
style={[style, RESET]}
|
|
325
|
+
onLayout={(event) => measure(diff.id, row, segment.id, event.nativeEvent.layout, false)}
|
|
326
|
+
>
|
|
327
|
+
{segment.string}
|
|
328
|
+
</Text>
|
|
329
|
+
))}
|
|
330
|
+
{/* ponytail: one Text per prefix lays out O(n²) characters; fine for a
|
|
331
|
+
line of text, measure glyph positions natively if it ever is not. */}
|
|
332
|
+
{segments.map((segment) => {
|
|
333
|
+
prefix += segment.string;
|
|
334
|
+
return (
|
|
335
|
+
<Text
|
|
336
|
+
key={`p:${segment.id}`}
|
|
337
|
+
className={className}
|
|
338
|
+
style={[style, RESET]}
|
|
339
|
+
onLayout={(event) => measure(diff.id, row, segment.id, event.nativeEvent.layout, true)}
|
|
340
|
+
>
|
|
341
|
+
{prefix}
|
|
342
|
+
</Text>
|
|
343
|
+
);
|
|
344
|
+
})}
|
|
345
|
+
</View>
|
|
346
|
+
);
|
|
347
|
+
})}
|
|
296
348
|
</View>
|
|
297
349
|
) : null}
|
|
298
350
|
</View>
|
|
@@ -302,7 +354,7 @@ export function MorphText({ children, className, style, numberOfLines }: MorphTe
|
|
|
302
354
|
function Sizer({ width, height, children }: { width: SharedValue<number>; height: number; children: ReactNode }) {
|
|
303
355
|
const animated = useAnimatedStyle(() => ({ width: width.value }));
|
|
304
356
|
return (
|
|
305
|
-
<Animated.View
|
|
357
|
+
<Animated.View style={[{ height }, animated]}>
|
|
306
358
|
{children}
|
|
307
359
|
</Animated.View>
|
|
308
360
|
);
|
|
@@ -329,7 +381,7 @@ function GlyphView({
|
|
|
329
381
|
}));
|
|
330
382
|
return (
|
|
331
383
|
// One spare point of width, so rounding never wraps the glyph.
|
|
332
|
-
<Animated.View style={[styles.glyph, { width: width + 1, height }, rolls && styles.slot, outer]}>
|
|
384
|
+
<Animated.View aria-hidden style={[styles.glyph, { width: width + 1, height }, rolls && styles.slot, outer]}>
|
|
333
385
|
<Animated.View style={inner}>
|
|
334
386
|
<Text className={className} style={[style, RESET]}>
|
|
335
387
|
{string}
|
|
@@ -344,6 +396,7 @@ function GlyphView({
|
|
|
344
396
|
* Longhands, because a class's `marginTop` beats a `margin` shorthand.
|
|
345
397
|
*/
|
|
346
398
|
const RESET: TextStyle = {
|
|
399
|
+
alignSelf: 'auto',
|
|
347
400
|
marginTop: 0,
|
|
348
401
|
marginBottom: 0,
|
|
349
402
|
marginLeft: 0,
|
|
@@ -363,12 +416,17 @@ const RESET: TextStyle = {
|
|
|
363
416
|
};
|
|
364
417
|
|
|
365
418
|
const styles = StyleSheet.create({
|
|
419
|
+
root: { maxWidth: '100%' },
|
|
366
420
|
glyph: { position: 'absolute', left: 0, top: 0 },
|
|
367
421
|
// A rolling digit slides a full line, so its slot clips it.
|
|
368
422
|
slot: { overflow: 'hidden' },
|
|
369
|
-
// The whole string once
|
|
370
|
-
|
|
423
|
+
// The whole string once, unseen, while the glyphs move. Screen readers read
|
|
424
|
+
// it, and it is the box's only text in the layout flow, so it gives the box
|
|
425
|
+
// the baseline the text at rest has. Not `transparent`: Android reads that
|
|
426
|
+
// value as no color and draws the text in the default one. A shadow and an
|
|
427
|
+
// underline keep their own color, so they go too.
|
|
428
|
+
reader: { color: '#00000001', textShadowRadius: 0, textShadowOffset: { width: 0, height: 0 }, textDecorationLine: 'none' },
|
|
371
429
|
// Lays both rows out unconstrained and unseen; the wide row never wraps a word.
|
|
372
430
|
measure: { position: 'absolute', width: 0, height: 0, overflow: 'hidden' },
|
|
373
|
-
row: { position: 'absolute', top: 0, left: 0, width: 10000,
|
|
431
|
+
row: { position: 'absolute', top: 0, left: 0, width: 10000, alignItems: 'flex-start' },
|
|
374
432
|
});
|