Skip to content

Commit 69d78eb

Browse files
committed
fix(web): preserve html citation selection and comment tracking
1 parent 6565c6e commit 69d78eb

5 files changed

Lines changed: 22 additions & 12 deletions

File tree

‎apps/web/src/components/chat/htmlRenderCitationSource.browser.test.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ describe("HTML citation source navigation", () => {
198198
p.dispose();
199199
expect(p.stopPosition).toHaveBeenCalledOnce();
200200
expect(p.post).toHaveBeenLastCalledWith(
201-
{ method: "t3/selection-command", params: { action: "unmark", selector: undefined } },
201+
{ method: "t3/selection-command", params: { action: "untarget", selector: undefined } },
202202
"*",
203203
);
204204
disposers.pop();

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ export function observeHtmlRenderCitationSource({
4646
responseTimeout ??= setTimeout(() => {
4747
if (stopped || activation.dismissed) return;
4848
activation.dismissed = true;
49-
if (frame) htmlSelectionCommand(frame, "unmark");
49+
if (frame) htmlSelectionCommand(frame, "untarget");
5050
request.onComplete();
5151
toastManager.add({
5252
type: "warning",
@@ -129,7 +129,7 @@ export function observeHtmlRenderCitationSource({
129129
expiry = setTimeout(
130130
() => {
131131
activation.dismissed = true;
132-
if (frame) htmlSelectionCommand(frame, "unmark");
132+
if (frame) htmlSelectionCommand(frame, "untarget");
133133
},
134134
Math.max(0, 3000 - (performance.now() - pulse.startedAt)),
135135
);
@@ -165,6 +165,6 @@ export function observeHtmlRenderCitationSource({
165165
root.removeEventListener("t3-html-selection-ready", resolve);
166166
window.removeEventListener("message", receive);
167167
if (activation.cancelScroll === cancelScroll) delete activation.cancelScroll;
168-
if (frame) htmlSelectionCommand(frame, "unmark");
168+
if (frame) htmlSelectionCommand(frame, "untarget");
169169
};
170170
}

‎apps/web/src/lib/htmlRenderSelection.browser.test.ts‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,9 @@ describe("HTML render selection bridge", () => {
290290
suffix: " after. Second paragraph",
291291
};
292292
p.command("mark", quote);
293+
p.command("untarget");
294+
expect(highlights.has("t3-html-citation")).toBe(true);
295+
p.command("target", { ...quote, text: "missing" });
293296
expect(highlights.has("t3-html-citation")).toBe(true);
294297
p.view.document.querySelector("strong")!.textContent = "different text";
295298
await p.finish();
@@ -300,6 +303,7 @@ describe("HTML render selection bridge", () => {
300303
selector: quote,
301304
action: "mark",
302305
});
306+
p.command("unmark");
303307
p.command("target", { ...quote, text: "different text" });
304308
expect(highlights.has("t3-html-citation")).toBe(true);
305309
p.command("target", { ...quote, text: "missing" });
@@ -390,7 +394,7 @@ describe("HTML render selection bridge", () => {
390394
await using p = await page();
391395
const paragraph = p.view.document.querySelector("p")!;
392396
Object.assign(p.view.Range.prototype, {
393-
getClientRects: () => [p.rect, { ...p.rect, left: 90, width: 0 }],
397+
getClientRects: () => [p.rect, { ...p.rect, top: 1200 }, { ...p.rect, left: 90, width: 0 }],
394398
});
395399
p.pointer("pointerdown", paragraph);
396400
p.select(paragraph.firstChild!, 0, paragraph.lastChild!);

‎apps/web/src/lib/htmlRenderSelection.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ export function readHtmlSelection(data: unknown): HtmlSelection | null {
8080

8181
export function htmlSelectionCommand(
8282
frame: HTMLIFrameElement,
83-
action: "clear" | "dismiss" | "mark" | "unmark" | "target",
83+
action: "clear" | "dismiss" | "mark" | "unmark" | "target" | "untarget",
8484
selector?: AssistantTextSelector,
8585
) {
8686
frame.contentWindow?.postMessage(

‎packages/shared/src/htmlRenderSelectionScript.ts‎

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,11 @@ export const HTML_RENDER_SELECTION_SCRIPT = String.raw`// Loaded as raw source i
4545
if (document.body) visit(document.body);
4646
return { text, chunks };
4747
};
48-
const rect = (range) => {
48+
const rect = (range, entire = false) => {
4949
// Paragraph selection may include a trailing newline with an empty rect.
5050
const rects = Array.from(range.getClientRects());
5151
const r =
52-
rects.findLast((rect) => rect.width > 0 && rect.height > 0) || range.getBoundingClientRect();
52+
(!entire && rects.findLast((rect) => rect.width > 0 && rect.height > 0)) || range.getBoundingClientRect();
5353
return { left: r.left, top: r.top, width: r.width, height: r.height };
5454
};
5555
const selector = (text, start, end) => {
@@ -113,8 +113,8 @@ export const HTML_RENDER_SELECTION_SCRIPT = String.raw`// Loaded as raw source i
113113
}
114114
if (start === null || !text.slice(start, end).trim()) return null;
115115
// Keep oversized quotes local; the parent can display its normal shorten action.
116-
if (end - start > 8000) return { tooLong: true, rect: rect(range), pointer };
117-
return { selector: selector(text, start, end), rect: rect(range), pointer };
116+
if (end - start > 8000) return { tooLong: true, rect: rect(range, true), pointer };
117+
return { selector: selector(text, start, end), rect: rect(range, true), pointer };
118118
};
119119
const schedule = (delay = 0) => {
120120
clearTimeout(timer);
@@ -188,7 +188,8 @@ export const HTML_RENDER_SELECTION_SCRIPT = String.raw`// Loaded as raw source i
188188
marked = null;
189189
};
190190
const markQuote = (quote, action, changesOnly = false) => {
191-
clearMark();
191+
const ownsMark = action === "mark" || !commentQuote;
192+
if (ownsMark) clearMark();
192193
const reply = (target) => {
193194
if (changesOnly && (
194195
target === commentTarget ||
@@ -296,7 +297,7 @@ export const HTML_RENDER_SELECTION_SCRIPT = String.raw`// Loaded as raw source i
296297
// Page CSS can request smooth scrolling; return geometry only after positioning.
297298
if (dx || dy) window.scrollBy({ left: dx, top: dy, behavior: "instant" });
298299
}
299-
if (window.CSS?.highlights && window.Highlight)
300+
if (ownsMark && window.CSS?.highlights && window.Highlight)
300301
CSS.highlights.set("t3-html-citation", new Highlight(marked));
301302
reply(rect(marked));
302303
};
@@ -321,6 +322,11 @@ export const HTML_RENDER_SELECTION_SCRIPT = String.raw`// Loaded as raw source i
321322
window.getSelection()?.removeAllRanges();
322323
}
323324
if (action === "dismiss") cancel();
325+
if (action === "untarget" && !commentQuote) clearMark();
326+
if (action === "target") {
327+
markQuote(quote, action);
328+
return;
329+
}
324330
if (action === "unmark") {
325331
commentQuote = null;
326332
commentTarget = null;

0 commit comments

Comments
 (0)