Open History to see the last commit
1import type { AssistantTextSelector } from "./assistantTextSelection";
2import {
3 ASSISTANT_CITATION_CONTEXT_LENGTH,
4 ASSISTANT_CITATION_MAX_TEXT_LENGTH,
5} from "@t3tools/contracts";
6
7export type HtmlSelectionRect = { left: number; top: number; width: number; height: number };
8export type HtmlSelection = {
9 selector: AssistantTextSelector;
10 rect: HtmlSelectionRect;
11 pointer: { x: number; y: number } | null;
12};
13
14export function readHtmlSelectionRect(data: unknown): HtmlSelectionRect | null {
15 if (typeof data !== "object" || data === null) return null;
16 const r = data as Record<string, unknown>;
17 if (
18 ![r.left, r.top, r.width, r.height].every(
19 (n) => typeof n === "number" && Number.isFinite(n) && Math.abs(n) <= 100_000,
20 )
21 )
22 return null;
23 if ((r.width as number) <= 0 || (r.height as number) <= 0) return null;
24 return {
25 left: r.left as number,
26 top: r.top as number,
27 width: r.width as number,
28 height: r.height as number,
29 };
30}
31
32export function htmlSelectionParams(data: unknown): Record<string, unknown> | null | undefined {
33 if (typeof data !== "object" || data === null) return undefined;
34 const d = data as Record<string, unknown>;
35 if (d.jsonrpc !== "2.0" || d.method !== "t3/selection") return undefined;
36 if (d.params === null) return null;
37 return typeof d.params === "object" && d.params !== null
38 ? (d.params as Record<string, unknown>)
39 : undefined;
40}
41
42export function readHtmlSelection(data: unknown): HtmlSelection | null {
43 const p = htmlSelectionParams(data);
44 if (!p || typeof p.selector !== "object" || p.selector === null) return null;
45 const s = p.selector as Record<string, unknown>;
46 const rect = readHtmlSelectionRect(p.rect);
47 if (
48 !rect ||
49 typeof s.text !== "string" ||
50 !s.text.trim() ||
51 s.text.length > ASSISTANT_CITATION_MAX_TEXT_LENGTH ||
52 typeof s.prefix !== "string" ||
53 s.prefix.length > ASSISTANT_CITATION_CONTEXT_LENGTH ||
54 typeof s.suffix !== "string" ||
55 s.suffix.length > ASSISTANT_CITATION_CONTEXT_LENGTH ||
56 !Number.isSafeInteger(s.start) ||
57 !Number.isSafeInteger(s.end) ||
58 (s.start as number) < 0 ||
59 (s.end as number) <= (s.start as number)
60 )
61 return null;
62 const pointer = p.pointer as Record<string, unknown> | null;
63 if (
64 pointer !== null &&
65 (typeof pointer !== "object" || !Number.isFinite(pointer.x) || !Number.isFinite(pointer.y))
66 )
67 return null;
68 return {
69 selector: {
70 text: s.text,
71 start: s.start as number,
72 end: s.end as number,
73 prefix: s.prefix,
74 suffix: s.suffix,
75 },
76 rect,
77 pointer: pointer as HtmlSelection["pointer"],
78 };
79}
80
81export function htmlSelectionCommand(
82 frame: HTMLIFrameElement,
83 action: "clear" | "dismiss" | "mark" | "unmark" | "target" | "untarget",
84 selector?: AssistantTextSelector,
85) {
86 frame.contentWindow?.postMessage(
87 {
88 method: "t3/selection-command",
89 params: {
90 action,
91 selector: selector && {
92 text: selector.text,
93 start: selector.start,
94 end: selector.end,
95 prefix: selector.prefix,
96 suffix: selector.suffix,
97 },
98 },
99 },
100 "*",
101 );
102}
103
104export function htmlSelectionClientRect(
105 frame: HTMLIFrameElement,
106 rect: HtmlSelectionRect,
107): DOMRect {
108 const bounds = frame.getBoundingClientRect();
109 const scaleX = frame.clientWidth ? bounds.width / frame.clientWidth : 1;
110 const scaleY = frame.clientHeight ? bounds.height / frame.clientHeight : 1;
111 return new DOMRect(
112 bounds.left + rect.left * scaleX,
113 bounds.top + rect.top * scaleY,
114 rect.width * scaleX,
115 rect.height * scaleY,
116 );
117}