Skip to content

Commit f8372d1

Browse files
fix(web): long screen-reader announcements keep emoji whole at the cutoff (pingdotgg#35681)
1 parent c1b42c1 commit f8372d1

2 files changed

Lines changed: 26 additions & 6 deletions

File tree

‎apps/web/src/components/chat/turnDetailAnnouncement.test.ts‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,11 @@ describe("turnDetail", () => {
133133
expect(turnDetail(entries, undefined, null).activity).toBe(activity);
134134
});
135135

136+
it("cuts a long step without splitting an emoji", () => {
137+
const edit = work("edit", { itemType: "file_change", changedFiles: [`${"a".repeat(111)}😀z`] });
138+
expect(turnDetail([prompt, edit], undefined, null).activity).not.toMatch(/[\uD800-\uDFFF]/u);
139+
});
140+
136141
it.each<{
137142
name: string;
138143
entries: ReadonlyArray<TimelineEntry>;
@@ -297,6 +302,12 @@ describe("nextTurnDetailAnnouncement", () => {
297302
).toEqual(["T3 Code: Tests pass in web. See the docs"]);
298303
});
299304

305+
it("cuts a long message without splitting an emoji", () => {
306+
const long = { ...said("long", true, true), text: `${"a".repeat(1999)}😀z` };
307+
const [spoken] = speak("messages", [frame(null), frame(null, [long])]);
308+
expect(spoken).not.toMatch(/[\uD800-\uDFFF]/u);
309+
});
310+
300311
it("says a step again after waiting on the user", () => {
301312
expect(
302313
speak("everything", [

‎apps/web/src/components/chat/turnDetailAnnouncement.ts‎

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,13 @@ const ACTIVITY_MAX_LENGTH = 120;
3434
// Parsing is synchronous, and nobody listens to a longer message through a live region.
3535
const MESSAGE_MAX_LENGTH = 2_000;
3636

37+
/** The first `maxLength` code points of `text`, or null when it has no more than that. */
38+
function cutText(text: string, maxLength: number): string | null {
39+
if (text.length <= maxLength) return null;
40+
const characters = Array.from(text);
41+
return characters.length > maxLength ? characters.slice(0, maxLength).join("") : null;
42+
}
43+
3744
// On screen these labels are a path or a query beside an icon.
3845
function withVerb(work: WorkLogEntry, label: string) {
3946
if (work.itemType === "file_change") return `Editing ${label}`;
@@ -66,7 +73,8 @@ function stepLabel(work: WorkLogEntry, workspaceRoot: string | undefined): strin
6673
const label = liveWorkEntryLabel(work, workspaceRoot, true).replace(/\s+/g, " ").trim();
6774
if (label === "") return null;
6875
const spoken = withVerb(work, label);
69-
return spoken.length > ACTIVITY_MAX_LENGTH ? `${spoken.slice(0, ACTIVITY_MAX_LENGTH)}…` : spoken;
76+
const cut = cutText(spoken, ACTIVITY_MAX_LENGTH);
77+
return cut === null ? spoken : `${cut}…`;
7078
}
7179

7280
/**
@@ -189,11 +197,12 @@ export function nextTurnDetailAnnouncement(
189197
previous.messages.get(message.id) !== true &&
190198
(message.fresh || previous.messages.has(message.id)),
191199
)
192-
.map((message) =>
193-
message.text.length > MESSAGE_MAX_LENGTH
194-
? `${markdownPlainText(message.text.slice(0, MESSAGE_MAX_LENGTH))} More in the thread.`
195-
: markdownPlainText(message.text),
196-
)
200+
.map((message) => {
201+
const cut = cutText(message.text, MESSAGE_MAX_LENGTH);
202+
return cut === null
203+
? markdownPlainText(message.text)
204+
: `${markdownPlainText(cut)} More in the thread.`;
205+
})
197206
.filter((text) => text !== "")
198207
.map((text) => `${MESSAGE_AUTHOR}: ${text}`),
199208
activity:

0 commit comments

Comments
 (0)