forked from pingdotgg/t3code
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathassistantTextSelection.ts
More file actions
282 lines (255 loc) · 10.7 KB
/
Copy pathassistantTextSelection.ts
File metadata and controls
282 lines (255 loc) · 10.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
import { ASSISTANT_CITATION_CONTEXT_LENGTH, type AssistantCitation } from "@t3tools/contracts";
export type AssistantTextSelector = {
readonly text: string;
readonly start: number;
readonly end: number;
readonly prefix: string;
readonly suffix: string;
};
/** Live DOM state for an open comment, never part of a saved citation. */
export type AssistantCitationSourceAnchor = {
source: HTMLElement;
range: Range;
viewport: HTMLElement;
};
export function findAssistantCitationSourceAnchor(
document: Document,
citation: AssistantCitation,
): AssistantCitationSourceAnchor | null {
const source = [
...document.querySelectorAll<HTMLElement>("[data-assistant-citation-source]"),
].find(
(element) =>
element.dataset.assistantCitationSource === citation.messageId &&
element.dataset.assistantCitationEnvironment === citation.environmentId &&
element.dataset.assistantCitationThread === citation.threadId,
);
const viewport = source?.closest<HTMLElement>("[data-assistant-citation-viewport]");
if (!source || !viewport) return null;
const range = resolveAssistantCitationRange(source, citation);
return range ? { source, range, viewport } : null;
}
const CONTROL_SELECTOR = "button, input, textarea, select, [role=button], [contenteditable]";
const EXCLUDED_SELECTOR = `${CONTROL_SELECTOR}, [hidden], [aria-hidden=true], script, style, template, noscript, svg`;
const BLOCK_SELECTOR =
"address, article, aside, blockquote, dd, div, dl, dt, figcaption, figure, footer, h1, h2, h3, h4, h5, h6, header, hr, li, main, nav, ol, p, pre, section, table, td, th, tr, ul";
function normalizeWhitespace(text: string): string {
return text.replace(/\s+/g, " ");
}
function splitsSurrogatePair(text: string, offset: number): boolean {
const before = text.charCodeAt(offset - 1);
const after = text.charCodeAt(offset);
return before >= 0xd800 && before <= 0xdbff && after >= 0xdc00 && after <= 0xdfff;
}
/** Keeps the exact captured text while storing normalized UTF-16 positions and context. */
export function createAssistantTextSelector(
text: string,
rawStart: number,
rawEnd: number,
): AssistantTextSelector | null {
const quote = text.slice(rawStart, rawEnd);
if (quote.trim().length === 0) return null;
const normalized = normalizeWhitespace(text);
let start = normalizeWhitespace(text.slice(0, rawStart)).length;
// A selection starting inside a whitespace run includes its normalized space.
if (rawStart > 0 && /\s/.test(text[rawStart - 1]!) && /\s/.test(text[rawStart]!)) {
start -= 1;
}
const end = normalizeWhitespace(text.slice(0, rawEnd)).length;
let prefixStart = Math.max(0, start - ASSISTANT_CITATION_CONTEXT_LENGTH);
let suffixEnd = Math.min(normalized.length, end + ASSISTANT_CITATION_CONTEXT_LENGTH);
// A split pair becomes a replacement character when the context enters a URL.
if (splitsSurrogatePair(normalized, prefixStart)) prefixStart += 1;
if (splitsSurrogatePair(normalized, suffixEnd)) suffixEnd -= 1;
return {
text: quote,
start,
end,
prefix: normalized.slice(prefixStart, start),
suffix: normalized.slice(end, suffixEnd),
};
}
/**
* Matches case-sensitive text after collapsing each JS whitespace run to one
* space, without trimming. All positions and context lengths use UTF-16 units
* in that normalized stream, not markdown offsets or Unicode code points.
* `selector.text` can retain the original line breaks and code indentation.
* Repeated quotes require one match for all supplied context. Even matching
* saved offsets cannot break a context tie, since those offsets may have drifted.
*/
export function findAssistantCitationText(
text: string,
selector: AssistantTextSelector,
): { start: number; end: number } | null {
const normalized = normalizeWhitespace(text);
const quote = normalizeWhitespace(selector.text);
if (quote.trim().length === 0) return null;
const prefix = normalizeWhitespace(selector.prefix);
const suffix = normalizeWhitespace(selector.suffix);
const matchesContext = (start: number, end: number) =>
normalized.slice(Math.max(0, start - prefix.length), start) === prefix &&
normalized.slice(end, end + suffix.length) === suffix;
let match =
Number.isSafeInteger(selector.start) &&
Number.isSafeInteger(selector.end) &&
selector.start >= 0 &&
selector.end - selector.start === quote.length &&
normalized.slice(selector.start, selector.end) === quote &&
matchesContext(selector.start, selector.end)
? { start: selector.start, end: selector.end }
: null;
let onlyQuote: { start: number; end: number } | null = null;
let quoteCount = 0;
for (
let start = normalized.indexOf(quote);
start !== -1;
start = normalized.indexOf(quote, start + 1)
) {
const end = start + quote.length;
quoteCount += 1;
onlyQuote = { start, end };
if (!matchesContext(start, end)) continue;
if (match !== null && match.start !== start) return null;
match = { start, end };
}
return match ?? (quoteCount === 1 ? onlyQuote : null);
}
type TextChunk = { node: Text; start: number; end: number };
/**
* Uses DOM text order, with a line break between HTML blocks and at <br>.
* Inline markup, including code and links, contributes its displayed text.
* Controls and subtrees marked hidden/aria-hidden do not contribute. No layout
* reads, CSS-generated content, or soft-wrap line breaks enter the stream, so
* reflow cannot move it.
*/
function readAssistantText(root: HTMLElement) {
const parts: string[] = [];
const chunks: TextChunk[] = [];
let length = 0;
let separator = false;
const visit = (node: Node) => {
if (node.nodeType === 3) {
const text = node as Text;
if (text.length === 0) return;
if (separator && length > 0) {
parts.push("\n");
length += 1;
}
separator = false;
chunks.push({ node: text, start: length, end: length + text.length });
parts.push(text.data);
length += text.length;
return;
}
if (node.nodeType !== 1) return;
const element = node as Element;
if (element.matches(EXCLUDED_SELECTOR)) return;
const block = element.matches(BLOCK_SELECTOR);
if (block || element.tagName === "BR") separator = true;
for (const child of element.childNodes) visit(child);
if (block) separator = true;
};
visit(root);
return { text: parts.join(""), chunks };
}
function excludedAncestor(node: Node): Element | null {
const element = node.nodeType === 1 ? (node as Element) : node.parentElement;
return element?.closest(EXCLUDED_SELECTOR) ?? null;
}
function isUsableRange(root: HTMLElement, range: Range): boolean {
if (
range.collapsed ||
!root.contains(range.startContainer) ||
!root.contains(range.endContainer) ||
excludedAncestor(range.startContainer) !== null ||
excludedAncestor(range.endContainer) !== null
) {
return false;
}
// Interior controls are allowed; readAssistantText omits them from the stream.
return true;
}
function selectedTextBoundary(range: Range, node: Node, last: boolean): Text | null {
if (!range.intersectsNode(node)) return null;
if (node.nodeType === 3) {
const text = node as Text;
const start = node === range.startContainer ? range.startOffset : 0;
const end = node === range.endContainer ? range.endOffset : text.length;
return start < end ? text : null;
}
for (
let child = last ? node.lastChild : node.firstChild;
child !== null;
child = last ? child.previousSibling : child.nextSibling
) {
const boundary = selectedTextBoundary(range, child, last);
if (boundary !== null) return boundary;
}
return null;
}
/** Captures the ordered native range, including selections dragged backwards. */
export function captureAssistantTextSelection(
viewport: HTMLElement,
selection: Selection | null,
): { source: HTMLElement; selector: AssistantTextSelector; range: Range } | null {
if (selection === null || selection.isCollapsed || selection.rangeCount !== 1) return null;
const range = selection.getRangeAt(0).cloneRange();
const first = selectedTextBoundary(range, range.commonAncestorContainer, false);
const last = selectedTextBoundary(range, range.commonAncestorContainer, true);
if (first === null || last === null) return null;
const source = first.parentElement?.closest<HTMLElement>("[data-assistant-citation-source]");
if (!source || !viewport.contains(source)) return null;
// Paragraph selection can end at the next block's offset 0 or a parent
// boundary. Validate the text actually selected, not that empty endpoint.
range.setStart(first, first === range.startContainer ? range.startOffset : 0);
range.setEnd(last, last === range.endContainer ? range.endOffset : last.length);
if (!isUsableRange(source, range)) return null;
const stream = readAssistantText(source);
let rawStart: number | null = null;
let rawEnd = 0;
for (const chunk of stream.chunks) {
if (!range.intersectsNode(chunk.node)) continue;
const start = range.startContainer === chunk.node ? range.startOffset : 0;
const end = range.endContainer === chunk.node ? range.endOffset : chunk.node.length;
if (start === end) continue;
rawStart ??= chunk.start + start;
rawEnd = chunk.start + end;
}
if (rawStart === null) return null;
const selector = createAssistantTextSelector(stream.text, rawStart, rawEnd);
return selector === null ? null : { source, selector, range };
}
function rawTextOffset(text: string, normalizedOffset: number): number {
let offset = 0;
for (const match of text.matchAll(/\s+|\S+/g)) {
const whitespace = /\s/.test(match[0][0]!);
const length = whitespace ? 1 : match[0].length;
if (normalizedOffset <= offset + length) {
return (
match.index +
(whitespace && normalizedOffset > offset ? match[0].length : normalizedOffset - offset)
);
}
offset += length;
}
return text.length;
}
/** Resolves against the current DOM without changing the user's selection. */
export function resolveAssistantCitationRange(
root: HTMLElement,
selector: AssistantTextSelector,
): Range | null {
if (excludedAncestor(root) !== null) return null;
const stream = readAssistantText(root);
const match = findAssistantCitationText(stream.text, selector);
if (match === null) return null;
const start = rawTextOffset(stream.text, match.start);
const end = rawTextOffset(stream.text, match.end);
const first = stream.chunks.find((chunk) => chunk.end > start);
const last = stream.chunks.findLast((chunk) => chunk.start < end);
if (first === undefined || last === undefined) return null;
const range = root.ownerDocument.createRange();
range.setStart(first.node, Math.max(0, start - first.start));
range.setEnd(last.node, Math.min(last.node.length, end - last.start));
return isUsableRange(root, range) ? range : null;
}