Open History to see the last commit
1export interface ComposerNativeEventSnapshot {
2 readonly eventCount: number;
3 readonly value: string;
4 readonly selection: ComposerEditorSelection | null;
5}
6
7interface ComposerEditorSelection {
8 readonly start: number;
9 readonly end: number;
10}
11
12export function acknowledgeComposerNativeEvent(
13 mostRecentEventCount: number,
14 incomingEventCount: number,
15): number | null {
16 if (!Number.isSafeInteger(incomingEventCount) || incomingEventCount < mostRecentEventCount) {
17 return null;
18 }
19 return incomingEventCount;
20}
21
22export function resolveComposerControlledEventCount(
23 value: string,
24 selection: ComposerEditorSelection | null,
25 mostRecentEventCount: number,
26 snapshots: ReadonlyArray<ComposerNativeEventSnapshot>,
27): number {
28 let newestValueEventCount: number | null = null;
29 for (let index = snapshots.length - 1; index >= 0; index -= 1) {
30 const snapshot = snapshots[index];
31 if (snapshot?.value !== value) continue;
32
33 newestValueEventCount ??= snapshot.eventCount;
34 if (selection === null || snapshotSelectionMatches(snapshot, selection)) {
35 return snapshot.eventCount;
36 }
37 }
38
39 // Mismatched native text/selection pairs are intermediate renders; keep their revision behind to avoid moving the caret.
40 if (newestValueEventCount !== null && mostRecentEventCount > 0) {
41 return Math.min(newestValueEventCount, mostRecentEventCount - 1);
42 }
43
44 return mostRecentEventCount;
45}
46
47// Null-selection snapshots allow assumed bounded carets; echoes must not use that wildcard or drop caret moves.
48function snapshotSelectionMatches(
49 snapshot: ComposerNativeEventSnapshot,
50 selection: ComposerEditorSelection,
51): boolean {
52 if (snapshot.selection === null) return true;
53 return snapshot.selection.start === selection.start && snapshot.selection.end === selection.end;
54}
55
56export function isComposerNativeEcho(
57 value: string,
58 selection: ComposerEditorSelection | null,
59 eventCount: number,
60 snapshots: ReadonlyArray<ComposerNativeEventSnapshot>,
61): boolean {
62 for (let index = snapshots.length - 1; index >= 0; index -= 1) {
63 const snapshot = snapshots[index];
64 if (
65 snapshot !== undefined &&
66 snapshot.eventCount === eventCount &&
67 snapshot.value === value &&
68 (selection === null ||
69 (snapshot.selection !== null &&
70 snapshot.selection.start === selection.start &&
71 snapshot.selection.end === selection.end))
72 ) {
73 return true;
74 }
75 }
76 return false;
77}
78
79/** Replace history with assumed controlled state so returning to old native values counts as fresh parent edits. */
80export function assumeComposerControlledState(
81 snapshots: ReadonlyArray<ComposerNativeEventSnapshot>,
82 eventCount: number,
83 value: string,
84): ComposerNativeEventSnapshot[] {
85 return [
86 { eventCount, value, selection: null },
87 ...snapshots.filter((snapshot) => snapshot.eventCount > eventCount),
88 ];
89}
90
91export function pruneAcknowledgedComposerNativeEvents(
92 snapshots: ReadonlyArray<ComposerNativeEventSnapshot>,
93 acknowledgedEventCount: number,
94): ComposerNativeEventSnapshot[] {
95 // Retain the latest acknowledgement so unrelated renders stay echoes and do not reset caret or iOS autocorrect.
96 let latestAcknowledgedIndex = -1;
97 for (let index = snapshots.length - 1; index >= 0; index -= 1) {
98 const snapshot = snapshots[index];
99 if (snapshot !== undefined && snapshot.eventCount <= acknowledgedEventCount) {
100 latestAcknowledgedIndex = index;
101 break;
102 }
103 }
104 return snapshots.filter(
105 (snapshot, index) =>
106 index === latestAcknowledgedIndex || snapshot.eventCount > acknowledgedEventCount,
107 );
108}