Skip to content

Commit 7178dfc

Browse files
Merge main into feat/gitlab-maintainer-edits
Keeps the optional-read helper beside #30549's mutation helper. Co-Authored-By: Claude Opus 5.5 <[email protected]>
2 parents 0926899 + f829c05 commit 7178dfc

29 files changed

Lines changed: 886 additions & 48 deletions

‎apps/mobile/modules/t3-composer-editor/ios/T3ComposerEditorModule.swift‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,9 @@ public class T3ComposerEditorModule: Module {
132132
AsyncFunction("setSelection") { (view: T3ComposerEditorView, start: Int, end: Int) in
133133
view.setSelection(start: start, end: end)
134134
}
135+
AsyncFunction("readState") { (view: T3ComposerEditorView) -> [String: Any] in
136+
view.readState()
137+
}
135138
}
136139
}
137140
}

‎apps/mobile/modules/t3-composer-editor/ios/T3ComposerEditorView.swift‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -447,6 +447,8 @@ public final class T3ComposerEditorView: ExpoView, UITextViewDelegate, UITextDro
447447
private var isReadOnly = false
448448
private var isApplyingControlledValue = false
449449
private var nativeEventCount = 0
450+
// Count of the latest intercepted paste, so a flush can tell whether JS has received it.
451+
private var lastPasteEventCount = 0
450452
private var hasControlledDocument = false
451453
private var resetGeneration = 0
452454
private var lastContentSize = CGSize.zero
@@ -499,6 +501,7 @@ public final class T3ComposerEditorView: ExpoView, UITextViewDelegate, UITextDro
499501
guard let self else { return }
500502
let selection = self.sourceSelection()
501503
self.nativeEventCount += 1
504+
self.lastPasteEventCount = self.nativeEventCount
502505
var payload: [String: Any] = context
503506
payload["value"] = self.textView.serializedText()
504507
payload["eventCount"] = self.nativeEventCount
@@ -510,6 +513,7 @@ public final class T3ComposerEditorView: ExpoView, UITextViewDelegate, UITextDro
510513
guard let self else { return }
511514
let selection = self.sourceSelection()
512515
self.nativeEventCount += 1
516+
self.lastPasteEventCount = self.nativeEventCount
513517
self.onComposerPasteText([
514518
"value": self.textView.serializedText(),
515519
"eventCount": self.nativeEventCount,
@@ -677,6 +681,8 @@ public final class T3ComposerEditorView: ExpoView, UITextViewDelegate, UITextDro
677681
}
678682
if isReset {
679683
resetGeneration = document.resetGeneration
684+
// JS drops a paste from before the reset by generation, so the flush must not wait for it.
685+
lastPasteEventCount = 0
680686
endComposition()
681687
}
682688
// iOS destroys this view under display: none and mounts a fresh one while JS keeps its
@@ -807,6 +813,17 @@ public final class T3ComposerEditorView: ExpoView, UITextViewDelegate, UITextDro
807813
textView.resignFirstResponder()
808814
}
809815

816+
func readState() -> [String: Any] {
817+
let selection = sourceSelection()
818+
return [
819+
"value": value,
820+
"selection": ["start": selection.start, "end": selection.end],
821+
"eventCount": nativeEventCount,
822+
"resetGeneration": resetGeneration,
823+
"pasteEventCount": lastPasteEventCount,
824+
]
825+
}
826+
810827
func setSelection(start: Int, end: Int) {
811828
let previousSelection = sourceSelection()
812829
requestedSelection = ComposerSelectionPayload(start: start, end: end)

‎apps/mobile/src/components/ComposerEditor.ios.test.tsx‎

Lines changed: 91 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
import { RegistryContext } from "@effect/atom-react";
2-
import { act, useLayoutEffect, useRef, type ReactNode } from "react";
2+
import {
3+
act,
4+
createRef,
5+
useImperativeHandle,
6+
useLayoutEffect,
7+
useRef,
8+
type ReactNode,
9+
type Ref,
10+
} from "react";
311
import { createRoot } from "react-dom/client";
412
import { expect, it, vi } from "vite-plus/test";
513

@@ -11,6 +19,7 @@ interface NativeEvent {
1119
}
1220

1321
interface NativeProps {
22+
readonly ref?: Ref<unknown>;
1423
readonly controlledDocumentJson: string;
1524
readonly onComposerChange: (event: { readonly nativeEvent: NativeEvent }) => void;
1625
}
@@ -20,17 +29,27 @@ const native = vi.hoisted(() => ({
2029
text: "",
2130
eventCount: 0,
2231
resetGeneration: 0,
32+
pasteEventCount: 0,
2333
mounts: 0,
2434
props: null as NativeProps | null,
2535
deliver: null as ((event: NativeEvent) => void) | null,
2636
}));
2737

28-
function NativeView(props: NativeProps) {
38+
function NativeView({ ref, ...props }: NativeProps) {
2939
const propsRef = useRef(props);
3040
useLayoutEffect(() => {
31-
Object.assign(native, { text: "", eventCount: 0, resetGeneration: 0 });
41+
Object.assign(native, { text: "", eventCount: 0, resetGeneration: 0, pasteEventCount: 0 });
3242
native.mounts += 1;
3343
}, []);
44+
useImperativeHandle(ref, () => ({
45+
readState: async () => ({
46+
value: native.text,
47+
selection: { start: native.text.length, end: native.text.length },
48+
eventCount: native.eventCount,
49+
resetGeneration: native.resetGeneration,
50+
pasteEventCount: native.pasteEventCount,
51+
}),
52+
}));
3453
useLayoutEffect(() => {
3554
propsRef.current = props;
3655
native.props = props;
@@ -55,6 +74,18 @@ function NativeView(props: NativeProps) {
5574
return null;
5675
}
5776

77+
// A native edit whose event has not reached JS yet.
78+
function pend(value: string): NativeEvent {
79+
native.eventCount += 1;
80+
native.text = value;
81+
return {
82+
value,
83+
selection: { start: value.length, end: value.length },
84+
eventCount: native.eventCount,
85+
resetGeneration: native.resetGeneration,
86+
};
87+
}
88+
5889
function emit(value: string, resetGeneration = native.resetGeneration) {
5990
native.eventCount += 1;
6091
native.text = value;
@@ -116,13 +147,21 @@ import {
116147
useComposerDraft,
117148
} from "../state/use-composer-drafts";
118149
import { ComposerEditor } from "./ComposerEditor";
150+
import type { ComposerEditorHandle } from "../native/T3ComposerEditor.types";
119151

120152
const DRAFT_KEY = "environment:thread";
121153

122-
function ThreadComposer({ draftKey = DRAFT_KEY }: { draftKey?: string }) {
154+
function ThreadComposer({
155+
draftKey = DRAFT_KEY,
156+
editorRef,
157+
}: {
158+
draftKey?: string;
159+
editorRef?: Ref<ComposerEditorHandle>;
160+
}) {
123161
const draft = useComposerDraft(draftKey);
124162
return (
125163
<ComposerEditor
164+
ref={editorRef}
126165
draftKey={draftKey}
127166
value={draft.text}
128167
onChangeText={(text) => setComposerDraftText(draftKey, text)}
@@ -190,3 +229,51 @@ it("keeps a pending event from the previous native editor out of the selected dr
190229
expect(getComposerDraftSnapshot(second).text).toBe("Draft B");
191230
expect(native.text).toBe("Draft B");
192231
});
232+
233+
async function renderFlushableComposer() {
234+
(globalThis as { IS_REACT_ACT_ENVIRONMENT?: boolean }).IS_REACT_ACT_ENVIRONMENT = true;
235+
const editor = createRef<ComposerEditorHandle>();
236+
const root = createRoot(document.createElement("div"));
237+
await act(async () =>
238+
root.render(
239+
<RegistryContext.Provider value={appAtomRegistry}>
240+
<ThreadComposer editorRef={editor} />
241+
</RegistryContext.Provider>,
242+
),
243+
);
244+
return {
245+
editor,
246+
[Symbol.asyncDispose]: async () => act(() => root.unmount()),
247+
};
248+
}
249+
250+
it("sends the autocorrection the Send tap accepted, and its late event cannot refill the draft", async () => {
251+
await using screen = await renderFlushableComposer();
252+
await act(async () => emit("Hello wrold"));
253+
const lateCorrection = pend("Hello world");
254+
255+
await act(async () => screen.editor.current!.flushPendingInput());
256+
expect(getComposerDraftSnapshot(DRAFT_KEY).text).toBe("Hello world");
257+
258+
await act(async () => {
259+
clearComposerDraftContent(DRAFT_KEY);
260+
native.deliver!(lateCorrection);
261+
});
262+
expect(getComposerDraftSnapshot(DRAFT_KEY).text).toBe("");
263+
});
264+
265+
it("does not flush past a paste whose event has not reached JS", async () => {
266+
await using screen = await renderFlushableComposer();
267+
await act(async () => emit("Before"));
268+
const paste = pend("Before pasted");
269+
native.pasteEventCount = paste.eventCount;
270+
const caretMove = pend("Before pasted");
271+
272+
await act(async () => screen.editor.current!.flushPendingInput());
273+
expect(getComposerDraftSnapshot(DRAFT_KEY).text).toBe("Before");
274+
275+
await act(async () => native.deliver!(paste));
276+
expect(getComposerDraftSnapshot(DRAFT_KEY).text).toBe("Before pasted");
277+
await act(async () => native.deliver!(caretMove));
278+
expect(getComposerDraftSnapshot(DRAFT_KEY).text).toBe("Before pasted");
279+
});

‎apps/mobile/src/features/pull-requests/pullRequestAutolinks.test.ts‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,10 @@ describe("linkReferenceAutolinks", () => {
2929
});
3030

3131
it("leaves markdown unchanged for a host that links nothing", () => {
32-
const gitlab = changeRequestReferenceAutolinker({
33-
provider: "gitlab",
34-
url: "https://gitlab.com/group/project/-/merge_requests/7",
32+
const forgejo = changeRequestReferenceAutolinker({
33+
provider: "forgejo",
34+
url: "https://codeberg.org/acme/web/pulls/7",
3535
});
36-
expect(linkReferenceAutolinks("Closes #123", gitlab)).toBe("Closes #123");
36+
expect(linkReferenceAutolinks("Closes #123", forgejo)).toBe("Closes #123");
3737
});
3838
});

‎apps/mobile/src/features/threads/ThreadComposer.tsx‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -683,6 +683,7 @@ export const ThreadComposer = function ThreadComposer(props: ThreadComposerProps
683683
const threadKey = scopedThreadKey(props.environmentId, props.selectedThread.id);
684684
if (inFlightThreadIdsRef.current.has(threadKey)) return;
685685
inFlightThreadIdsRef.current.add(threadKey);
686+
await inputRef.current?.flushPendingInput();
686687
const draftAtSend = getComposerDraftSnapshot(composerDraftKey);
687688
const stillSendable = () => {
688689
const draft = getComposerDraftSnapshot(composerDraftKey);

‎apps/mobile/src/native/T3ComposerEditor.ios.tsx‎

Lines changed: 33 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,13 @@ interface NativeComposerEditorRef {
7272
focus: () => Promise<void>;
7373
blur: () => Promise<void>;
7474
setSelection: (start: number, end: number) => Promise<void>;
75+
readState: () => Promise<{
76+
readonly value: string;
77+
readonly selection: ComposerEditorSelection;
78+
readonly eventCount: number;
79+
readonly resetGeneration: number;
80+
readonly pasteEventCount: number;
81+
}>;
7582
}
7683

7784
interface NativeComposerEditorProps extends ViewProps {
@@ -170,17 +177,6 @@ export function ComposerEditor({
170177
const theme = useUniwindTheme();
171178
const fontFamily = FONT_FAMILIES.regular;
172179

173-
useImperativeHandle(
174-
ref,
175-
() => ({
176-
focus: () => settleViewCommand(nativeRef.current?.focus()),
177-
blur: () => settleViewCommand(nativeRef.current?.blur()),
178-
setSelection: (nextSelection) =>
179-
settleViewCommand(nativeRef.current?.setSelection(nextSelection.start, nextSelection.end)),
180-
}),
181-
[],
182-
);
183-
184180
const skillLabels = useMemo(() => composerSkillLabels(skills), [skills]);
185181
const chipTextColor = createNativeComposerTheme(theme).text;
186182
const tokensJson = useMemo(() => {
@@ -299,6 +295,31 @@ export function ComposerEditor({
299295
},
300296
[nativeEventSnapshotsRef, readResetGeneration],
301297
);
298+
const applyNativeChange = (event: NativeEditorEvent["nativeEvent"]) => {
299+
const acknowledgedEventCount = acceptNativeEvent(event);
300+
if (acknowledgedEventCount === false) return;
301+
onChangeText(event.value);
302+
onSelectionChange?.(event.selection);
303+
setMostRecentEventCount(acknowledgedEventCount);
304+
forceNativeEventRender((sequence) => sequence + 1);
305+
};
306+
useImperativeHandle(ref, () => ({
307+
focus: () => settleViewCommand(nativeRef.current?.focus()),
308+
blur: () => settleViewCommand(nativeRef.current?.blur()),
309+
setSelection: (nextSelection) =>
310+
settleViewCommand(nativeRef.current?.setSelection(nextSelection.start, nextSelection.end)),
311+
// Tapping outside the editor makes iOS accept a pending autocorrection, and that edit's
312+
// event can reach JS after the tap's handler has already read the draft.
313+
flushPendingInput: async () => {
314+
const state = await Promise.resolve()
315+
.then(() => nativeRef.current?.readState())
316+
.catch(() => undefined);
317+
// Acknowledging past a paste JS has not received yet would get the paste rejected.
318+
if (state && state.pasteEventCount <= mostRecentEventCountRef.current) {
319+
applyNativeChange(state);
320+
}
321+
},
322+
}));
302323
const themeJson = JSON.stringify(createNativeComposerTheme(theme));
303324
const resolvedTextStyle = StyleSheet.flatten(textStyle) ?? {};
304325
return (
@@ -334,14 +355,7 @@ export function ComposerEditor({
334355
textPasteThresholdBytes={onPasteText ? PASTED_TEXT_ATTACHMENT_THRESHOLD_BYTES : 0}
335356
maxInputChars={PROVIDER_SEND_TURN_MAX_INPUT_CHARS}
336357
style={style as StyleProp<ViewStyle>}
337-
onComposerChange={(event) => {
338-
const acknowledgedEventCount = acceptNativeEvent(event.nativeEvent);
339-
if (acknowledgedEventCount === false) return;
340-
onChangeText(event.nativeEvent.value);
341-
onSelectionChange?.(event.nativeEvent.selection);
342-
setMostRecentEventCount(acknowledgedEventCount);
343-
forceNativeEventRender((sequence) => sequence + 1);
344-
}}
358+
onComposerChange={(event) => applyNativeChange(event.nativeEvent)}
345359
onComposerSelectionChange={(event) => {
346360
const acknowledgedEventCount = acceptNativeEvent(event.nativeEvent);
347361
if (acknowledgedEventCount === false) return;

‎apps/mobile/src/native/T3ComposerEditor.native.tsx‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,7 @@ export function ComposerEditor({
167167
blur: () => settleViewCommand(nativeRef.current?.blur()),
168168
setSelection: (nextSelection) =>
169169
settleViewCommand(nativeRef.current?.setSelection(nextSelection.start, nextSelection.end)),
170+
flushPendingInput: async () => {},
170171
}),
171172
[],
172173
);

‎apps/mobile/src/native/T3ComposerEditor.tsx‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ export function ComposerEditor({
3535
blur: () => inputRef.current?.blur(),
3636
setSelection: (nextSelection) =>
3737
inputRef.current?.setSelection(nextSelection.start, nextSelection.end),
38+
flushPendingInput: async () => {},
3839
}),
3940
[],
4041
);

‎apps/mobile/src/native/T3ComposerEditor.types.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ export interface ComposerEditorHandle {
2222
focus: () => void;
2323
blur: () => void;
2424
setSelection: (selection: ComposerEditorSelection) => void;
25+
/** iOS: resolves once a native edit still in flight has reached `onChangeText`. */
26+
flushPendingInput: () => Promise<void>;
2527
}
2628

2729
export interface ComposerEditorProps {

‎apps/mobile/src/native/composerEditorRevision.test.ts‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,3 +164,10 @@ describe("typing immediately before an intercepted paste", () => {
164164
expect(acknowledgeComposerNativeEvent(2, 1)).toBeNull();
165165
});
166166
});
167+
168+
describe("acknowledgeComposerNativeEvent", () => {
169+
it("drops a native edit a send already read, so it cannot refill the cleared draft", () => {
170+
expect(acknowledgeComposerNativeEvent(76, 77)).toBe(77);
171+
expect(acknowledgeComposerNativeEvent(77, 77)).toBeNull();
172+
});
173+
});

0 commit comments

Comments
 (0)