dsh-selection-annotations 0.0.0-stage → 2.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +171 -0
- package/LICENSE +21 -0
- package/README.md +161 -3
- package/README.zh-CN.md +161 -0
- package/client.js +11136 -0
- package/cordis.patch.yml +3 -0
- package/icon.svg +7 -0
- package/lib/annotations-db.js +1157 -0
- package/lib/index.js +1304 -0
- package/locale/en.json +6 -0
- package/locale/zh.json +6 -0
- package/package.json +76 -6
- package/scripts/build.mjs +37 -0
- package/scripts/check-build.mjs +10 -0
- package/src/core/annotation-clipboard.js +93 -0
- package/src/core/annotation-utils.js +27 -0
- package/src/core/cards.js +502 -0
- package/src/core/config.js +91 -0
- package/src/core/constants.js +34 -0
- package/src/core/draft-codec.js +77 -0
- package/src/core/draft-store.js +85 -0
- package/src/core/hidden-payload.js +73 -0
- package/src/core/image-codec.js +111 -0
- package/src/core/jump-icon.js +25 -0
- package/src/core/locale.js +59 -0
- package/src/core/log.js +19 -0
- package/src/core/markdown.js +80 -0
- package/src/core/messages.js +311 -0
- package/src/core/note-tone.js +77 -0
- package/src/core/panel-render.js +315 -0
- package/src/core/payload.js +191 -0
- package/src/core/preview-bridge.js +216 -0
- package/src/core/remote-sync.js +206 -0
- package/src/core/scroll-trace.js +177 -0
- package/src/core/sent-store.js +148 -0
- package/src/core/session.js +60 -0
- package/src/core/settings.js +168 -0
- package/src/core/side-tab-state.js +30 -0
- package/src/core/source-capture.js +530 -0
- package/src/core/source-locate.js +220 -0
- package/src/core/source-resolve.js +89 -0
- package/src/core/styles.js +590 -0
- package/src/core/turn-anchor.js +54 -0
- package/src/core/util.js +55 -0
- package/src/core/uuid.js +23 -0
- package/src/core/web-picker.js +426 -0
- package/src/entry.js +22 -0
- package/src/plugin.js +8706 -0
|
@@ -0,0 +1,530 @@
|
|
|
1
|
+
import { safe, cssEscape } from './util.js'
|
|
2
|
+
import { offsetRange, pathFromPreviewUrl, lineRangeWithin, rangeFromOffsets } from './source-locate.js'
|
|
3
|
+
|
|
4
|
+
/** 一条批注最多存几段;超出的先并段,仍超就保留首末段并标 segmentsTruncated。*/
|
|
5
|
+
const MAX_SEGMENTS = 8
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* 会打断阅读顺序的块级标签:跨过它们就是另一段。
|
|
9
|
+
* 行内元素(strong/em/code/a/span)不在这里 —— 一句话被加粗、链接或行内代码切开时仍算一段,
|
|
10
|
+
* 否则同一行会被切成好几段,反而更难定位。
|
|
11
|
+
*/
|
|
12
|
+
const BLOCK_TAGS = new Set([
|
|
13
|
+
'p', 'li', 'pre', 'blockquote', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6',
|
|
14
|
+
'td', 'th', 'dd', 'dt', 'figcaption', 'div', 'section', 'article', 'tr',
|
|
15
|
+
])
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* 行内标签:永不作为切段边界,哪怕它自身带样式(DSH 正文里的行内代码就是
|
|
19
|
+
* `<code>`,display:inline-flex,左右各 5px padding)。清单与 BLOCK_TAGS 互斥;
|
|
20
|
+
* 两处都显式检查,以后增删任一侧都不会把行内元素误判成块。
|
|
21
|
+
*/
|
|
22
|
+
const INLINE_TAGS = new Set([
|
|
23
|
+
'a', 'abbr', 'b', 'code', 'em', 'i', 'kbd', 'mark', 'q', 's', 'samp',
|
|
24
|
+
'small', 'span', 'strong', 'sub', 'sup', 'time', 'u', 'var',
|
|
25
|
+
])
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* 文本节点所属的块:从元素自身向上找第一个块级标签,最多找到 root(含 root)。
|
|
29
|
+
* 只有真的落到块元素才切段;行内元素与自定义标签都跳过,也不允许越过 root
|
|
30
|
+
* 去认外面更大的块 —— 那会把相邻字段甚至别的消息行并进同一段。
|
|
31
|
+
* 在 root 内找不到块元素时返回 null:整根按同一段处理,只会少切、不会多切。
|
|
32
|
+
* @param element - 文本节点的父元素。
|
|
33
|
+
* @param root - 本次遍历的根,切段边界必须落在它里面。
|
|
34
|
+
*/
|
|
35
|
+
function blockOf(element, root) {
|
|
36
|
+
if (element === null || element === undefined) return null
|
|
37
|
+
const limit = root ?? null
|
|
38
|
+
let node = element
|
|
39
|
+
while (node !== null && node !== undefined) {
|
|
40
|
+
if (node.nodeType === 1) {
|
|
41
|
+
const tag = String(node.tagName ?? '').toLowerCase()
|
|
42
|
+
if (BLOCK_TAGS.has(tag) && !INLINE_TAGS.has(tag)) return node
|
|
43
|
+
}
|
|
44
|
+
if (node === limit) return null
|
|
45
|
+
node = node.parentElement
|
|
46
|
+
}
|
|
47
|
+
return null
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
function anchorKeyOfNode(node) {
|
|
51
|
+
const element = node === null || node === undefined ? null : node.parentElement
|
|
52
|
+
if (element === null || element === undefined) return ''
|
|
53
|
+
return safe(() => element.closest('[data-chat-anchor-key]')?.getAttribute('data-chat-anchor-key')) ?? ''
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
function rowOfNode(node) {
|
|
57
|
+
const element = node === null || node === undefined ? null : node.parentElement
|
|
58
|
+
if (element === null || element === undefined) return null
|
|
59
|
+
return safe(() => element.closest('[data-chat-anchor-key]')) ?? null
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* 把一次选区拆成「按块分段的引文区间」。
|
|
64
|
+
*
|
|
65
|
+
* 只存每段的 `quoteStart/quoteEnd`(引文里的位置)与 `charStart/charEnd`(所属根元素里的偏移),
|
|
66
|
+
* 不存文本:引文本身已经在条目上,段只是它的一段切片。不变量:
|
|
67
|
+
* `charEnd - charStart === quoteEnd - quoteStart`,且每一段都能单独定位。
|
|
68
|
+
*
|
|
69
|
+
* 空白段丢弃,但游标照常前进 —— 这样后一段的 `quoteStart` 仍然对得上原引文。
|
|
70
|
+
* 超过 MAX_SEGMENTS 时先并同根相邻段(中间只有空白或行内元素),仍超就取前 7 段加末段。
|
|
71
|
+
* @param root - 遍历范围的根(消息行、答复字段或已发送条目)。
|
|
72
|
+
* @param range - 用户选区。
|
|
73
|
+
* @param source - 已捕获的来源,`anchorKey` 在文本节点不在锚点行内时兜底。
|
|
74
|
+
* @returns `{ segments, coverage, truncated }`;coverage 是走到的原文长度,用来校验是否覆盖整段选区。
|
|
75
|
+
*/
|
|
76
|
+
export function buildSegments(root, range, source) {
|
|
77
|
+
if (root === null || root === undefined || range === null || range === undefined) {
|
|
78
|
+
return { segments: [], coverage: 0, truncated: false }
|
|
79
|
+
}
|
|
80
|
+
const quoteLength = safe(() => range.toString().length) ?? 0
|
|
81
|
+
if (quoteLength <= 0) return { segments: [], coverage: 0, truncated: false }
|
|
82
|
+
// 只有消息行有锚点键可兜底:答复卡与已发送卡的根就是字段元素本身,
|
|
83
|
+
// 段上留空锚点键,定位时才会回退到 anchorRoot,而不是去翻同名的消息行。
|
|
84
|
+
const fallbackAnchorKey =
|
|
85
|
+
source?.kind === 'message' && typeof source?.anchorKey === 'string' ? source.anchorKey : ''
|
|
86
|
+
const walker = document.createTreeWalker(root, NodeFilter.SHOW_TEXT)
|
|
87
|
+
const blocks = []
|
|
88
|
+
let cursor = 0
|
|
89
|
+
let node = walker.nextNode()
|
|
90
|
+
while (node !== null) {
|
|
91
|
+
const inside = safe(() => range.intersectsNode(node)) === true
|
|
92
|
+
if (inside) {
|
|
93
|
+
const value = node.nodeValue ?? ''
|
|
94
|
+
const start = node === range.startContainer ? range.startOffset : 0
|
|
95
|
+
const end = node === range.endContainer ? range.endOffset : value.length
|
|
96
|
+
if (end > start) {
|
|
97
|
+
const anchorKey = anchorKeyOfNode(node) || fallbackAnchorKey
|
|
98
|
+
const block = blockOf(node.parentElement, root)
|
|
99
|
+
const quoteStart = cursor
|
|
100
|
+
const quoteEnd = cursor + (end - start)
|
|
101
|
+
cursor = quoteEnd
|
|
102
|
+
if (value.slice(start, end).trim() !== '') {
|
|
103
|
+
const last = blocks[blocks.length - 1]
|
|
104
|
+
const mergeable = last !== undefined && last.anchorKey === anchorKey && last.block === block
|
|
105
|
+
if (mergeable) {
|
|
106
|
+
// 同块内相邻文本节点(中间只隔行内元素或空白)并成一段,段数才不会被加粗、链接撑爆。
|
|
107
|
+
last.endNode = node
|
|
108
|
+
last.endOffset = end
|
|
109
|
+
last.quoteEnd = quoteEnd
|
|
110
|
+
} else {
|
|
111
|
+
blocks.push({
|
|
112
|
+
anchorKey,
|
|
113
|
+
block,
|
|
114
|
+
startNode: node,
|
|
115
|
+
startOffset: start,
|
|
116
|
+
endNode: node,
|
|
117
|
+
endOffset: end,
|
|
118
|
+
quoteStart,
|
|
119
|
+
quoteEnd,
|
|
120
|
+
})
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
node = walker.nextNode()
|
|
126
|
+
}
|
|
127
|
+
const coverage = cursor
|
|
128
|
+
let picked = blocks
|
|
129
|
+
let truncated = false
|
|
130
|
+
if (picked.length > MAX_SEGMENTS) {
|
|
131
|
+
picked = mergeAdjacentSameRoot(picked)
|
|
132
|
+
if (picked.length > MAX_SEGMENTS) {
|
|
133
|
+
// 优先保首末段:首段代表「从哪开始」,末段保证选区末尾也被标记到。
|
|
134
|
+
picked = [...picked.slice(0, MAX_SEGMENTS - 1), picked[picked.length - 1]]
|
|
135
|
+
truncated = true
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
const segments = []
|
|
139
|
+
for (const block of picked) {
|
|
140
|
+
const measureRoot = rowOfNode(block.startNode) ?? root
|
|
141
|
+
const segmentRange = safe(() => {
|
|
142
|
+
const probe = document.createRange()
|
|
143
|
+
probe.setStart(block.startNode, block.startOffset)
|
|
144
|
+
probe.setEnd(block.endNode, block.endOffset)
|
|
145
|
+
return probe
|
|
146
|
+
})
|
|
147
|
+
if (segmentRange === undefined) continue
|
|
148
|
+
const chars = offsetRange(measureRoot, segmentRange)
|
|
149
|
+
if (!Number.isFinite(chars.start)) continue
|
|
150
|
+
// 保持不变式:字符长度只按引文切片长度算,量出来的 charEnd 只当交叉校验。
|
|
151
|
+
const charStart = chars.start
|
|
152
|
+
const charEnd = charStart + (block.quoteEnd - block.quoteStart)
|
|
153
|
+
if (charEnd <= charStart) continue
|
|
154
|
+
segments.push({
|
|
155
|
+
anchorKey: block.anchorKey,
|
|
156
|
+
textOrigin: 'rendered',
|
|
157
|
+
charStart,
|
|
158
|
+
charEnd,
|
|
159
|
+
quoteStart: block.quoteStart,
|
|
160
|
+
quoteEnd: block.quoteEnd,
|
|
161
|
+
})
|
|
162
|
+
}
|
|
163
|
+
return { segments, coverage, truncated }
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
/** 同根相邻段并成一段:段数超限时的第一道收缩。*/
|
|
167
|
+
function mergeAdjacentSameRoot(blocks) {
|
|
168
|
+
const out = []
|
|
169
|
+
for (const block of blocks) {
|
|
170
|
+
const last = out[out.length - 1]
|
|
171
|
+
if (last !== undefined && last.anchorKey === block.anchorKey && last.block === block.block) {
|
|
172
|
+
last.endNode = block.endNode
|
|
173
|
+
last.endOffset = block.endOffset
|
|
174
|
+
last.quoteEnd = block.quoteEnd
|
|
175
|
+
continue
|
|
176
|
+
}
|
|
177
|
+
out.push({ ...block })
|
|
178
|
+
}
|
|
179
|
+
return out
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
/**
|
|
183
|
+
* 读回判据:只有「每段四个数字都有限、区间合法、且字符长度等于引文切片长度」才算可用。
|
|
184
|
+
* 顶层字段若存在,必须与首段同义 —— 否则是坏数据,宁可不分段。
|
|
185
|
+
* @param source - 条目上的来源。
|
|
186
|
+
* @param quote - 条目上的引文(用来限定 quoteEnd 上界)。
|
|
187
|
+
* @returns 规范化后的段数组;不可用返回 null。
|
|
188
|
+
*/
|
|
189
|
+
export function segmentsOf(source, quote) {
|
|
190
|
+
const list = source?.segments
|
|
191
|
+
if (!Array.isArray(list) || list.length === 0) return null
|
|
192
|
+
const length = typeof quote === 'string' ? quote.length : 0
|
|
193
|
+
if (length <= 0) return null
|
|
194
|
+
const out = []
|
|
195
|
+
for (const segment of list) {
|
|
196
|
+
if (out.length >= MAX_SEGMENTS) break
|
|
197
|
+
if (segment === null || typeof segment !== 'object' || Array.isArray(segment)) return null
|
|
198
|
+
const { charStart, charEnd, quoteStart, quoteEnd } = segment
|
|
199
|
+
if (!Number.isFinite(charStart) || !Number.isFinite(charEnd)) return null
|
|
200
|
+
if (!Number.isFinite(quoteStart) || !Number.isFinite(quoteEnd)) return null
|
|
201
|
+
if (charStart < 0 || charEnd <= charStart) return null
|
|
202
|
+
if (charEnd - charStart !== quoteEnd - quoteStart) return null
|
|
203
|
+
if (quoteStart < 0 || quoteEnd <= quoteStart || quoteEnd > length) return null
|
|
204
|
+
out.push({
|
|
205
|
+
anchorKey: typeof segment.anchorKey === 'string' ? segment.anchorKey : '',
|
|
206
|
+
textOrigin: segment.textOrigin === 'source' ? 'source' : 'rendered',
|
|
207
|
+
charStart,
|
|
208
|
+
charEnd,
|
|
209
|
+
quoteStart,
|
|
210
|
+
quoteEnd,
|
|
211
|
+
...(typeof segment.fingerprintBefore === 'string' ? { fingerprintBefore: segment.fingerprintBefore } : {}),
|
|
212
|
+
...(typeof segment.fingerprintAfter === 'string' ? { fingerprintAfter: segment.fingerprintAfter } : {}),
|
|
213
|
+
})
|
|
214
|
+
}
|
|
215
|
+
if (out.length === 0) return null
|
|
216
|
+
if (Number.isFinite(source?.charStart) && source.charStart !== out[0].charStart) return null
|
|
217
|
+
if (Number.isFinite(source?.charEnd) && source.charEnd !== out[0].charEnd) return null
|
|
218
|
+
return out
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
/** 选段覆盖的原文区间(跨段选区时范围可以很大;这里只判断交集)。*/
|
|
222
|
+
function spanOfSegment(segment) {
|
|
223
|
+
const start = Math.max(0, segment.quoteStart)
|
|
224
|
+
const end = Math.max(start, segment.quoteEnd)
|
|
225
|
+
return { start, end }
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
/**
|
|
229
|
+
* 把分段收紧到最终保存的引文上。
|
|
230
|
+
*
|
|
231
|
+
* `addAnnotation` 会 `trim()` 再 `slice(0, MAX_QUOTE)`:前者让所有偏移整体左移,
|
|
232
|
+
* 后者让末段越界。这里按「原始引文 → 最终引文」的偏移差逐段裁剪、平移,
|
|
233
|
+
* 被裁空的段直接丢弃,并让顶层字段重新等于首段。
|
|
234
|
+
* @param source - 刚捕获的来源。
|
|
235
|
+
* @param rawQuote - 捕获时的原始引文(range.toString())。
|
|
236
|
+
* @param quote - 最终保存的引文。
|
|
237
|
+
* @returns 收紧后的来源;没有可用段时移除 segments。
|
|
238
|
+
*/
|
|
239
|
+
export function fitSegmentsToQuote(source, rawQuote, quote) {
|
|
240
|
+
if (source === null || typeof source !== 'object') return source
|
|
241
|
+
const list = source.segments
|
|
242
|
+
if (!Array.isArray(list) || list.length === 0) return source
|
|
243
|
+
const raw = typeof rawQuote === 'string' ? rawQuote : ''
|
|
244
|
+
const kept = typeof quote === 'string' ? quote : ''
|
|
245
|
+
const lead = Math.max(0, raw.length - raw.trimStart().length)
|
|
246
|
+
const keptEnd = lead + kept.length
|
|
247
|
+
const next = []
|
|
248
|
+
for (const segment of list) {
|
|
249
|
+
if (segment === null || typeof segment !== 'object') continue
|
|
250
|
+
if (![segment.charStart, segment.charEnd, segment.quoteStart, segment.quoteEnd].every(Number.isFinite)) continue
|
|
251
|
+
const span = spanOfSegment(segment)
|
|
252
|
+
const start = Math.max(span.start, lead)
|
|
253
|
+
const end = Math.min(span.end, keptEnd)
|
|
254
|
+
if (end <= start) continue
|
|
255
|
+
const shift = start - segment.quoteStart
|
|
256
|
+
const charStart = segment.charStart + shift
|
|
257
|
+
next.push({
|
|
258
|
+
...segment,
|
|
259
|
+
charStart,
|
|
260
|
+
charEnd: charStart + (end - start),
|
|
261
|
+
quoteStart: start - lead,
|
|
262
|
+
quoteEnd: end - lead,
|
|
263
|
+
})
|
|
264
|
+
}
|
|
265
|
+
if (next.length === 0) {
|
|
266
|
+
const cleaned = { ...source }
|
|
267
|
+
delete cleaned.segments
|
|
268
|
+
delete cleaned.segmentsTruncated
|
|
269
|
+
return cleaned
|
|
270
|
+
}
|
|
271
|
+
const first = next[0]
|
|
272
|
+
return { ...source, charStart: first.charStart, charEnd: first.charEnd, segments: next }
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
const elementOf = (node) => {
|
|
276
|
+
if (node === null || node === undefined) return null
|
|
277
|
+
if (node.nodeType === 1) return node
|
|
278
|
+
return node.parentElement ?? null
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
const REPLY_FIELD_BY_CLASS = {
|
|
282
|
+
'sa-q-quote': 'quote',
|
|
283
|
+
'sa-q-note': 'note',
|
|
284
|
+
'sa-a-reason': 'reason',
|
|
285
|
+
'sa-a-next': 'next',
|
|
286
|
+
}
|
|
287
|
+
const REPLY_CLASS_BY_FIELD = { quote: 'sa-q-quote', note: 'sa-q-note', reason: 'sa-a-reason', next: 'sa-a-next' }
|
|
288
|
+
|
|
289
|
+
/**
|
|
290
|
+
* 把一次选区翻译成可持久化的出处:
|
|
291
|
+
* 消息(role + anchorKey + 字符区间)、工作区文件(path + 行号 + 字符区间)、
|
|
292
|
+
* 答复卡(批次 + 批注编号 + 字段)与已发送卡(消息键 + 编号 + 字段)。
|
|
293
|
+
*/
|
|
294
|
+
export function createSourceCapture() {
|
|
295
|
+
/**
|
|
296
|
+
* 给来源挂上分段;选区没被完整覆盖(端点跨出 root)时保持旧的单段字段。
|
|
297
|
+
* 顶层 charStart/charEnd 恒等于首段,只有一段时与旧数据完全同形。
|
|
298
|
+
*/
|
|
299
|
+
function withSegments(source, root, range) {
|
|
300
|
+
const quoteLength = safe(() => range.toString().length) ?? 0
|
|
301
|
+
if (quoteLength <= 0) return source
|
|
302
|
+
const built = safe(() => buildSegments(root, range, source))
|
|
303
|
+
if (built === undefined || built.segments.length === 0) return source
|
|
304
|
+
// 只覆盖到一部分时不能挂:段里的 quoteEnd 会与整段引文对不上,定位会错位。
|
|
305
|
+
if (built.coverage !== quoteLength) return source
|
|
306
|
+
const first = built.segments[0]
|
|
307
|
+
return {
|
|
308
|
+
...source,
|
|
309
|
+
charStart: first.charStart,
|
|
310
|
+
charEnd: first.charEnd,
|
|
311
|
+
segments: built.segments,
|
|
312
|
+
...(built.truncated === true ? { segmentsTruncated: true } : {}),
|
|
313
|
+
}
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
function captureSource(range) {
|
|
317
|
+
const startEl = elementOf(range.startContainer)
|
|
318
|
+
const endEl = elementOf(range.endContainer)
|
|
319
|
+
if (startEl === null) return { kind: 'dom' }
|
|
320
|
+
|
|
321
|
+
const replyItem = startEl.closest('.sa-reply-item')
|
|
322
|
+
if (replyItem !== null && replyItem.closest('[data-sa-ui="replies"]') !== null) {
|
|
323
|
+
// 老卡片(本次改动之前渲染的 DOM)没有 data-sa-field,按类名兜底;
|
|
324
|
+
// 批注编号也能从「批注 N」标题里读回来,免得整条只剩一个模糊的“整条答复”。
|
|
325
|
+
const fieldOf = (element) => {
|
|
326
|
+
if (element === null) return null
|
|
327
|
+
const marked = element.closest('[data-sa-field]')
|
|
328
|
+
if (marked !== null) return marked.getAttribute('data-sa-field') ?? 'item'
|
|
329
|
+
for (const name of Object.keys(REPLY_FIELD_BY_CLASS)) {
|
|
330
|
+
if (element.closest(`.${name}`) !== null) return REPLY_FIELD_BY_CLASS[name]
|
|
331
|
+
}
|
|
332
|
+
return null
|
|
333
|
+
}
|
|
334
|
+
const startField = fieldOf(startEl)
|
|
335
|
+
const endField = fieldOf(endEl)
|
|
336
|
+
const field = startField !== null && (endField === null || startField === endField) ? startField : 'item'
|
|
337
|
+
let fieldElement = replyItem
|
|
338
|
+
if (field !== 'item') {
|
|
339
|
+
fieldElement =
|
|
340
|
+
startEl.closest(`[data-sa-field="${cssEscape(field)}"]`) ??
|
|
341
|
+
startEl.closest(`.${REPLY_CLASS_BY_FIELD[field]}`) ??
|
|
342
|
+
replyItem
|
|
343
|
+
}
|
|
344
|
+
const chars = offsetRange(fieldElement, range)
|
|
345
|
+
let index = Number(replyItem.getAttribute('data-sa-reply-index'))
|
|
346
|
+
if (!Number.isFinite(index) || index === 0) {
|
|
347
|
+
const digits = (replyItem.querySelector('.sa-reply-index')?.textContent ?? '').match(/(\d+)/)
|
|
348
|
+
index = digits === null ? 0 : Number(digits[1])
|
|
349
|
+
}
|
|
350
|
+
return withSegments(
|
|
351
|
+
{
|
|
352
|
+
kind: 'reply',
|
|
353
|
+
batchAt: Number(replyItem.getAttribute('data-sa-batch-at')) || 0,
|
|
354
|
+
index,
|
|
355
|
+
field,
|
|
356
|
+
charStart: chars.start,
|
|
357
|
+
charEnd: chars.end,
|
|
358
|
+
textOrigin: 'rendered',
|
|
359
|
+
},
|
|
360
|
+
fieldElement,
|
|
361
|
+
range,
|
|
362
|
+
)
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
const sentItem = startEl.closest('.sa-sent-item')
|
|
366
|
+
if (sentItem !== null) {
|
|
367
|
+
const sentCard = sentItem.closest('[data-sa-ui="sent"]')
|
|
368
|
+
if (sentCard !== null) {
|
|
369
|
+
const fieldOf = (element) => {
|
|
370
|
+
if (element === null) return null
|
|
371
|
+
const note = element.closest('.sa-sent-note')
|
|
372
|
+
if (note !== null) return { key: 'note', el: note }
|
|
373
|
+
const quote = element.closest('.sa-sent-quote')
|
|
374
|
+
if (quote !== null) return { key: 'quote', el: quote }
|
|
375
|
+
return null
|
|
376
|
+
}
|
|
377
|
+
const startField = fieldOf(startEl)
|
|
378
|
+
const endField = fieldOf(endEl)
|
|
379
|
+
const field =
|
|
380
|
+
startField !== null && (endField === null || endField.key === startField.key) ? startField.key : 'item'
|
|
381
|
+
const fieldEl = field === 'item' ? sentItem : startField.el
|
|
382
|
+
const chars = offsetRange(fieldEl, range)
|
|
383
|
+
let index = Number(sentItem.getAttribute('data-sa-sent-index'))
|
|
384
|
+
if (!Number.isFinite(index) || index === 0) {
|
|
385
|
+
const digits = (sentItem.querySelector('.sa-sent-num')?.textContent ?? '').match(/(\d+)/)
|
|
386
|
+
index = digits === null ? 0 : Number(digits[1])
|
|
387
|
+
}
|
|
388
|
+
return withSegments(
|
|
389
|
+
{
|
|
390
|
+
kind: 'sent',
|
|
391
|
+
anchorKey: sentCard.getAttribute('data-anchor') ?? '',
|
|
392
|
+
index,
|
|
393
|
+
field,
|
|
394
|
+
charStart: chars.start,
|
|
395
|
+
charEnd: chars.end,
|
|
396
|
+
textOrigin: 'rendered',
|
|
397
|
+
},
|
|
398
|
+
fieldEl,
|
|
399
|
+
range,
|
|
400
|
+
)
|
|
401
|
+
}
|
|
402
|
+
}
|
|
403
|
+
|
|
404
|
+
const row = startEl.closest('[data-chat-anchor-key]')
|
|
405
|
+
if (row !== null) {
|
|
406
|
+
const chars = offsetRange(row, range)
|
|
407
|
+
// 记下所属轮次:之后那一行可能被宿主卸载,靠轮次号才能让导航按钮把它加载回来。
|
|
408
|
+
const turnText = row.closest('[data-chat-turn]')?.getAttribute('data-chat-turn') ?? ''
|
|
409
|
+
const turn = turnText === '' ? null : Number(turnText)
|
|
410
|
+
const source = {
|
|
411
|
+
kind: 'message',
|
|
412
|
+
role: (row.getAttribute('data-chat-flow-kind') ?? '') === 'user' ? 'user' : 'assistant',
|
|
413
|
+
anchorKey: row.getAttribute('data-chat-anchor-key') ?? '',
|
|
414
|
+
flowKind: row.getAttribute('data-chat-flow-kind') ?? '',
|
|
415
|
+
charStart: chars.start,
|
|
416
|
+
charEnd: chars.end,
|
|
417
|
+
...(Number.isFinite(turn) ? { turn } : {}),
|
|
418
|
+
}
|
|
419
|
+
// 选区跨出这一行时,从共同祖先里走,段内偏移仍按各自所属的消息行量。
|
|
420
|
+
const walkRoot = row.contains(range.endContainer) ? row : safe(() => {
|
|
421
|
+
const common = range.commonAncestorContainer
|
|
422
|
+
if (common === null || common === undefined) return row
|
|
423
|
+
return common.nodeType === 1 ? common : common.parentElement ?? row
|
|
424
|
+
}) ?? row
|
|
425
|
+
return withSegments(source, walkRoot, range)
|
|
426
|
+
}
|
|
427
|
+
|
|
428
|
+
const doc = startEl.closest('[data-textpreview-url]')
|
|
429
|
+
if (doc !== null) {
|
|
430
|
+
const url = doc.getAttribute('data-textpreview-url') ?? ''
|
|
431
|
+
const lines = lineRangeWithin(doc, startEl, endEl)
|
|
432
|
+
const chars = offsetRange(doc, range)
|
|
433
|
+
return {
|
|
434
|
+
kind: 'file',
|
|
435
|
+
path: pathFromPreviewUrl(url),
|
|
436
|
+
sourceUrl: url,
|
|
437
|
+
lineStart: lines.start,
|
|
438
|
+
lineEnd: lines.end,
|
|
439
|
+
charStart: chars.start,
|
|
440
|
+
charEnd: chars.end,
|
|
441
|
+
// 默认口径是渲染后的正文;保存时会再试着读源文件改成 source。
|
|
442
|
+
textOrigin: 'rendered',
|
|
443
|
+
}
|
|
444
|
+
}
|
|
445
|
+
|
|
446
|
+
const code = startEl.closest('[data-code-preview]')
|
|
447
|
+
if (code !== null) {
|
|
448
|
+
const lines = lineRangeWithin(code, startEl, endEl)
|
|
449
|
+
const chars = offsetRange(code, range)
|
|
450
|
+
return { kind: 'file', lineStart: lines.start, lineEnd: lines.end, charStart: chars.start, charEnd: chars.end, textOrigin: 'rendered' }
|
|
451
|
+
}
|
|
452
|
+
|
|
453
|
+
return { kind: 'dom' }
|
|
454
|
+
}
|
|
455
|
+
|
|
456
|
+
/** 锚点根元素:消息行、文件预览外壳、已发送卡或答复卡。 */
|
|
457
|
+
function anchorRoot(source) {
|
|
458
|
+
if (source === null || source === undefined) return null
|
|
459
|
+
if (source.kind === 'message' && source.anchorKey) {
|
|
460
|
+
return (
|
|
461
|
+
document.querySelector(`[data-chat-anchor-key="${cssEscape(source.anchorKey)}"]`) ??
|
|
462
|
+
document.querySelector(`[data-chat-flow-key="${cssEscape(source.anchorKey)}"]`)
|
|
463
|
+
)
|
|
464
|
+
}
|
|
465
|
+
if (source.kind === 'file' && source.sourceUrl) {
|
|
466
|
+
return document.querySelector(`[data-textpreview-url="${cssEscape(source.sourceUrl)}"]`)
|
|
467
|
+
}
|
|
468
|
+
if (source.kind === 'file') return document.querySelector('[data-code-preview]')
|
|
469
|
+
if (source.kind === 'sent' && source.anchorKey) {
|
|
470
|
+
const card = document.querySelector(`[data-sa-ui="sent"][data-anchor="${cssEscape(source.anchorKey)}"]`)
|
|
471
|
+
if (card !== null) {
|
|
472
|
+
const wanted = Number(source.index)
|
|
473
|
+
for (const item of card.querySelectorAll('.sa-sent-item')) {
|
|
474
|
+
const digits = (item.querySelector('.sa-sent-num')?.textContent ?? '').match(/(\d+)/)
|
|
475
|
+
const index = Number(item.getAttribute('data-sa-sent-index')) || (digits === null ? 0 : Number(digits[1]))
|
|
476
|
+
if (index !== wanted) continue
|
|
477
|
+
if (typeof source.field === 'string' && source.field !== '' && source.field !== 'item') {
|
|
478
|
+
const field = item.querySelector(`[data-sa-field="${cssEscape(source.field)}"]`)
|
|
479
|
+
return field ?? item
|
|
480
|
+
}
|
|
481
|
+
return item
|
|
482
|
+
}
|
|
483
|
+
}
|
|
484
|
+
}
|
|
485
|
+
if (source.kind === 'reply') {
|
|
486
|
+
const batchAt = Number(source.batchAt)
|
|
487
|
+
const replyIndex = Number(source.index)
|
|
488
|
+
for (const item of document.querySelectorAll('.sa-reply-item')) {
|
|
489
|
+
if (item.closest('[data-sa-ui="replies"]') === null) continue
|
|
490
|
+
if (Number(item.getAttribute('data-sa-batch-at')) !== batchAt) continue
|
|
491
|
+
if (Number(item.getAttribute('data-sa-reply-index')) !== replyIndex) continue
|
|
492
|
+
if (typeof source.field === 'string' && source.field !== '' && source.field !== 'item') {
|
|
493
|
+
const field = item.querySelector(`[data-sa-field="${cssEscape(source.field)}"]`)
|
|
494
|
+
return field ?? item
|
|
495
|
+
}
|
|
496
|
+
return item
|
|
497
|
+
}
|
|
498
|
+
}
|
|
499
|
+
return null
|
|
500
|
+
}
|
|
501
|
+
|
|
502
|
+
/** 在根元素里定位原文;重复片段用保存时的偏移择优。 */
|
|
503
|
+
function matchInRoot(root, quote, savedOffset) {
|
|
504
|
+
if (typeof quote !== 'string' || quote === '') return undefined
|
|
505
|
+
const text = root.textContent ?? ''
|
|
506
|
+
const positions = []
|
|
507
|
+
let from = 0
|
|
508
|
+
for (;;) {
|
|
509
|
+
const at = text.indexOf(quote, from)
|
|
510
|
+
if (at === -1 || positions.length >= 64) break
|
|
511
|
+
positions.push(at)
|
|
512
|
+
from = at + 1
|
|
513
|
+
}
|
|
514
|
+
if (positions.length === 0) return undefined
|
|
515
|
+
let best = positions[0]
|
|
516
|
+
if (positions.length > 1 && Number.isFinite(savedOffset)) {
|
|
517
|
+
best = positions.reduce((a, b) => (Math.abs(b - savedOffset) < Math.abs(a - savedOffset) ? b : a))
|
|
518
|
+
}
|
|
519
|
+
return rangeFromOffsets(root, best, quote.length)
|
|
520
|
+
}
|
|
521
|
+
|
|
522
|
+
function rangeMatchesQuote(range, quote) {
|
|
523
|
+
const current = safe(() => range.toString())
|
|
524
|
+
if (typeof current !== 'string') return false
|
|
525
|
+
const normalize = (value) => value.replace(/\s+/g, ' ').trim()
|
|
526
|
+
return normalize(current) === normalize(quote)
|
|
527
|
+
}
|
|
528
|
+
|
|
529
|
+
return { captureSource, anchorRoot, rangeFromOffsets, matchInRoot, rangeMatchesQuote }
|
|
530
|
+
}
|