dsh-sticky-user-bubble 0.1.3
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 +64 -0
- package/LICENSE +21 -0
- package/PUBLISHING.md +26 -0
- package/README.md +92 -0
- package/client.js +1396 -0
- package/cordis.patch.yml +3 -0
- package/lib/index.js +77 -0
- package/package.json +74 -0
package/client.js
ADDED
|
@@ -0,0 +1,1396 @@
|
|
|
1
|
+
// dsh-sticky-user-bubble — browser half loaded by dsh-client-modules.
|
|
2
|
+
// The package owns one additive shell overlay entry and leaves core chat markup
|
|
3
|
+
// and session state untouched.
|
|
4
|
+
window.__ModuleLoader__.load({
|
|
5
|
+
id: 'dsh-sticky-user-bubble',
|
|
6
|
+
factory: (require) => {
|
|
7
|
+
var module = { exports: {} }
|
|
8
|
+
var exports = module.exports
|
|
9
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' })
|
|
10
|
+
|
|
11
|
+
const React = require('react')
|
|
12
|
+
const PLUGIN_ID = 'dsh-sticky-user-bubble'
|
|
13
|
+
const OVERLAY_ID = PLUGIN_ID
|
|
14
|
+
const SCROLL_SELECTOR = '[data-conversation-scroll]'
|
|
15
|
+
const FLOW_SELECTOR = '[data-chat-flow]'
|
|
16
|
+
const ROW_SELECTOR = '[data-chat-anchor-key]'
|
|
17
|
+
// The composer is sticky at the bottom inside the conversation scrollport, so the
|
|
18
|
+
// scrollport's own bottom edge is not the end of the reading area.
|
|
19
|
+
const COMPOSER_SELECTOR = '[data-composer-seat]'
|
|
20
|
+
const USER_KINDS = new Set(['user', 'steering'])
|
|
21
|
+
const BUBBLE_MARKERS = ['[data-user-bubble]', '[data-chat-user-bubble]']
|
|
22
|
+
// DSH's user-text projector emits `data-ref-chip="<kind>"` plus `title="<raw token>"`
|
|
23
|
+
// for each projected `@`/slash token; the rendered chip text is only the token's last
|
|
24
|
+
// path segment with an icon in front of it.
|
|
25
|
+
const REFERENCE_CHIP_MARKER = 'data-ref-chip'
|
|
26
|
+
const REFERENCE_CHIP_TITLE = 'title'
|
|
27
|
+
const MAX_PROJECTION_NODES = 512
|
|
28
|
+
const EPSILON = 0.75
|
|
29
|
+
const LINE_MERGE_EPSILON = 2
|
|
30
|
+
const MAX_COLLAPSED_LINES = 3
|
|
31
|
+
const MAX_LINE_TEXT_NODES = 256
|
|
32
|
+
const MAX_SOURCE_ANCESTORS = 12
|
|
33
|
+
const MAX_STYLE_TREE_NODES = 128
|
|
34
|
+
// Clearance kept between the yielding clone and the incoming card. The audited DSH flow
|
|
35
|
+
// spaces consecutive items with a 16px `margin-top` (`--dsh-chat-flow-gap`), so the clone
|
|
36
|
+
// follows that spacing instead of sliding flush against the next card.
|
|
37
|
+
const MIN_PUSH_GAP = 16
|
|
38
|
+
const MAX_PUSH_GAP = 48
|
|
39
|
+
const JUMP_TITLE = 'Jump to original message'
|
|
40
|
+
const DESCENDANT_STYLE_PROPERTIES = [
|
|
41
|
+
'background', 'backgroundClip', 'backgroundOrigin', 'backgroundPosition',
|
|
42
|
+
'backgroundRepeat', 'backgroundSize', 'border', 'borderRadius', 'color',
|
|
43
|
+
'display', 'fontFamily', 'fontFeatureSettings', 'fontKerning', 'fontSize',
|
|
44
|
+
'fontStretch', 'fontStyle', 'fontVariant', 'fontWeight', 'hyphens',
|
|
45
|
+
'letterSpacing', 'lineHeight', 'margin', 'overflowWrap', 'padding',
|
|
46
|
+
'tabSize', 'textAlign', 'textDecoration', 'textIndent', 'textShadow',
|
|
47
|
+
'textTransform', 'verticalAlign', 'whiteSpace', 'wordBreak'
|
|
48
|
+
]
|
|
49
|
+
|
|
50
|
+
const EMPTY_SESSION_SOURCE = {
|
|
51
|
+
subscribe() { return () => {} },
|
|
52
|
+
getSnapshot() { return undefined }
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
const EMPTY_CHAT_SOURCE = {
|
|
56
|
+
subscribe() { return () => {} },
|
|
57
|
+
getSnapshot() { return undefined }
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
function textFromContent(content) {
|
|
61
|
+
if (typeof content === 'string') return content
|
|
62
|
+
if (!Array.isArray(content)) return ''
|
|
63
|
+
return content.map((block) => {
|
|
64
|
+
if (block === null || typeof block !== 'object') return ''
|
|
65
|
+
if (block.type !== 'text' && block.type !== 'reasoning') return ''
|
|
66
|
+
return typeof block.text === 'string' ? block.text : ''
|
|
67
|
+
}).join('')
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
// Chat data is a separate target snapshot in the audited DSH line. Keep the old
|
|
71
|
+
// nested shape as a compatibility fallback for older DSH bundles.
|
|
72
|
+
function buildUserIndex(snapshot, chatSnapshot) {
|
|
73
|
+
const index = new Map()
|
|
74
|
+
const chat = chatSnapshot !== undefined
|
|
75
|
+
? chatSnapshot
|
|
76
|
+
: snapshot?.chat ?? (Array.isArray(snapshot?.order) ? snapshot : undefined)
|
|
77
|
+
const order = chat?.order
|
|
78
|
+
const nodes = chat?.nodes
|
|
79
|
+
if (!Array.isArray(order) || nodes === undefined || typeof nodes.get !== 'function') return index
|
|
80
|
+
|
|
81
|
+
for (const orderKey of order) {
|
|
82
|
+
const node = nodes.get(orderKey)
|
|
83
|
+
if (node === undefined || node === null || !USER_KINDS.has(node.kind)) continue
|
|
84
|
+
const key = typeof node.key === 'string' ? node.key : String(orderKey)
|
|
85
|
+
const text = textFromContent(node.data?.content)
|
|
86
|
+
if (text.trim() === '') continue
|
|
87
|
+
index.set(key, { key, text })
|
|
88
|
+
}
|
|
89
|
+
return index
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
function viewportOf() {
|
|
93
|
+
return typeof window === 'undefined' ? undefined : window
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
function computedStyleOf(element) {
|
|
97
|
+
const viewport = viewportOf()
|
|
98
|
+
if (viewport !== undefined && typeof viewport.getComputedStyle === 'function') {
|
|
99
|
+
return viewport.getComputedStyle(element)
|
|
100
|
+
}
|
|
101
|
+
return element?.currentStyle || {}
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
function finiteDimension(value) {
|
|
105
|
+
return typeof value === 'number' && Number.isFinite(value) && value > 0
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
function rectOf(element) {
|
|
109
|
+
if (element === null || element === undefined || typeof element.getBoundingClientRect !== 'function') return null
|
|
110
|
+
const rect = element.getBoundingClientRect()
|
|
111
|
+
return finiteDimension(rect.width) && finiteDimension(rect.height) ? rect : null
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
function visibleFlow(scroll) {
|
|
115
|
+
if (scroll === null || typeof scroll.querySelectorAll !== 'function') return null
|
|
116
|
+
const flows = scroll.querySelectorAll(FLOW_SELECTOR)
|
|
117
|
+
for (const flow of flows) if (rectOf(flow) !== null) return flow
|
|
118
|
+
return null
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
function matchingRowCount(flow, userIndex) {
|
|
122
|
+
if (flow === null || typeof flow.querySelectorAll !== 'function') return 0
|
|
123
|
+
let count = 0
|
|
124
|
+
for (const row of flow.querySelectorAll(ROW_SELECTOR)) {
|
|
125
|
+
const kind = row.getAttribute?.('data-chat-flow-kind')
|
|
126
|
+
if (!USER_KINDS.has(kind)) continue
|
|
127
|
+
const key = row.getAttribute?.('data-chat-flow-key') || row.getAttribute?.('data-chat-anchor-key')
|
|
128
|
+
if (key !== null && key !== undefined && userIndex.has(key)) count += 1
|
|
129
|
+
}
|
|
130
|
+
return count
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
function findConversationScroll(userIndex) {
|
|
134
|
+
if (typeof document === 'undefined' || typeof document.querySelectorAll !== 'function') return null
|
|
135
|
+
let best = null
|
|
136
|
+
let bestScore = -1
|
|
137
|
+
for (const candidate of document.querySelectorAll(SCROLL_SELECTOR)) {
|
|
138
|
+
const flow = visibleFlow(candidate)
|
|
139
|
+
if (flow === null || rectOf(candidate) === null) continue
|
|
140
|
+
const score = matchingRowCount(flow, userIndex)
|
|
141
|
+
if (score > bestScore) {
|
|
142
|
+
best = candidate
|
|
143
|
+
bestScore = score
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
return best
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
function parsePixels(value) {
|
|
150
|
+
const parsed = Number.parseFloat(value)
|
|
151
|
+
return Number.isFinite(parsed) ? parsed : 0
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
function verticalShorthand(value) {
|
|
155
|
+
if (typeof value !== 'string') return 0
|
|
156
|
+
const parts = value.trim().split(/\s+/).filter(Boolean).map(parsePixels)
|
|
157
|
+
if (parts.length === 0) return 0
|
|
158
|
+
if (parts.length === 1) return parts[0] * 2
|
|
159
|
+
if (parts.length === 2) return parts[0] * 2
|
|
160
|
+
return parts[0] + parts[2]
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
function verticalBoxInset(style) {
|
|
164
|
+
const explicitPadding = parsePixels(style.paddingTop) + parsePixels(style.paddingBottom)
|
|
165
|
+
const padding = explicitPadding > 0 ? explicitPadding : verticalShorthand(style.padding)
|
|
166
|
+
const border = parsePixels(style.borderTopWidth) + parsePixels(style.borderBottomWidth)
|
|
167
|
+
return padding + border
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
function horizontalBoxInset(style) {
|
|
171
|
+
const explicitPadding = parsePixels(style.paddingLeft) + parsePixels(style.paddingRight)
|
|
172
|
+
const padding = explicitPadding > 0
|
|
173
|
+
? explicitPadding
|
|
174
|
+
: (() => {
|
|
175
|
+
const parts = String(style.padding || '').trim().split(/\s+/).filter(Boolean).map(parsePixels)
|
|
176
|
+
if (parts.length === 0) return 0
|
|
177
|
+
if (parts.length === 1) return parts[0] * 2
|
|
178
|
+
if (parts.length === 2) return parts[1] * 2
|
|
179
|
+
return (parts[1] || 0) + (parts[3] ?? parts[1] ?? 0)
|
|
180
|
+
})()
|
|
181
|
+
const border = parsePixels(style.borderLeftWidth) + parsePixels(style.borderRightWidth)
|
|
182
|
+
return padding + border
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
function resolvedLineHeight(style) {
|
|
186
|
+
const fontSize = parsePixels(style.fontSize) || 16
|
|
187
|
+
return parsePixels(style.lineHeight) || fontSize * 1.5
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
function readingInset(flow, scroll) {
|
|
191
|
+
const candidates = [flow?.parentElement, scroll]
|
|
192
|
+
for (const candidate of candidates) {
|
|
193
|
+
if (candidate === null || candidate === undefined) continue
|
|
194
|
+
const top = parsePixels(computedStyleOf(candidate).paddingTop)
|
|
195
|
+
if (top > 0) return top
|
|
196
|
+
}
|
|
197
|
+
return 16
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
function clipsVertically(style) {
|
|
201
|
+
const overflow = String(style?.overflowY || style?.overflow || '').trim().toLowerCase()
|
|
202
|
+
return overflow !== '' && overflow !== 'visible'
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
// Spacing the clone keeps above the incoming card: follow the row's own leading margin
|
|
206
|
+
// (the core's flow gap), never less than `MIN_PUSH_GAP` and never a runaway value.
|
|
207
|
+
function pushGapOf(row) {
|
|
208
|
+
const marginTop = parsePixels(computedStyleOf(row).marginTop)
|
|
209
|
+
if (!Number.isFinite(marginTop) || marginTop <= 0) return MIN_PUSH_GAP
|
|
210
|
+
return Math.min(MAX_PUSH_GAP, Math.max(MIN_PUSH_GAP, marginTop))
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
// Bottom of the reading area. The scrollport ends at the frame edge, but its last child is
|
|
214
|
+
// the sticky composer seat, so an expanded bubble capped only by the scrollport would grow
|
|
215
|
+
// over the composer and the frame's status bar below it. Missing or unmeasurable seats keep
|
|
216
|
+
// the old viewport-only limit.
|
|
217
|
+
function readingLowerBoundary(scroll, scrollRect, overlayRect) {
|
|
218
|
+
let boundary = Math.min(scrollRect.bottom, overlayRect.bottom)
|
|
219
|
+
const composerRect = rectOf(scroll?.querySelector?.(COMPOSER_SELECTOR) ?? null)
|
|
220
|
+
if (composerRect !== null && composerRect.top < boundary) boundary = composerRect.top
|
|
221
|
+
return boundary
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
// Top of the band where the browser still paints the source bubble. The reading line
|
|
225
|
+
// (`scrollRect.top + readingInset`) only places the clone, so it must not gate
|
|
226
|
+
// visibility; painting really stops at the scrollport's own clip edge, narrowed by a
|
|
227
|
+
// clipping or independently scrolling ancestor in between.
|
|
228
|
+
function paintedTopBoundary(source, scroll, scrollRect) {
|
|
229
|
+
let boundary = scrollRect.top + parsePixels(computedStyleOf(scroll).borderTopWidth)
|
|
230
|
+
let ancestor = source?.parentElement
|
|
231
|
+
let depth = 0
|
|
232
|
+
while (ancestor !== null && ancestor !== undefined && depth <= MAX_SOURCE_ANCESTORS) {
|
|
233
|
+
const rect = rectOf(ancestor)
|
|
234
|
+
if (rect !== null) {
|
|
235
|
+
const style = computedStyleOf(ancestor)
|
|
236
|
+
if (clipsVertically(style)) {
|
|
237
|
+
boundary = Math.max(boundary, rect.top + parsePixels(style.borderTopWidth))
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
if (ancestor === scroll) break
|
|
241
|
+
ancestor = ancestor.parentElement
|
|
242
|
+
depth += 1
|
|
243
|
+
}
|
|
244
|
+
return boundary
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
function normalizedText(value) {
|
|
248
|
+
return typeof value === 'string' ? value.replace(/\s+/gu, ' ').trim() : ''
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
function comparableText(value) {
|
|
252
|
+
const normalized = normalizedText(value).toLocaleLowerCase()
|
|
253
|
+
return normalized.replace(/[^\p{L}\p{N}]+/gu, '') || normalized
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
function textAffinity(candidateText, expectedText) {
|
|
257
|
+
const candidate = comparableText(candidateText)
|
|
258
|
+
const expected = comparableText(expectedText)
|
|
259
|
+
if (candidate === '' || expected === '') return 0
|
|
260
|
+
if (candidate === expected) return 5000
|
|
261
|
+
if (candidate.includes(expected)) return 4500
|
|
262
|
+
if (expected.includes(candidate) && candidate.length >= Math.max(8, expected.length * 0.6)) return 3000
|
|
263
|
+
return 0
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
function nodeAttribute(node, name) {
|
|
267
|
+
if (node === null || node === undefined || typeof node.getAttribute !== 'function') return null
|
|
268
|
+
return node.getAttribute(name)
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
// DSH renders each projected `@`/slash token as a reference chip whose visible text is
|
|
272
|
+
// only the token's last path segment (preceded by an icon), while the raw token the
|
|
273
|
+
// snapshot carries survives only in the chip's `title` attribute. A snapshot text that
|
|
274
|
+
// contains such a token therefore never matches the rendered `textContent`, so rebuild
|
|
275
|
+
// the rendered text with every chip replaced by its own `title`. Elements without the
|
|
276
|
+
// marker and bundles without the attribute reproduce the plain `textContent` exactly.
|
|
277
|
+
function referenceProjectedText(element) {
|
|
278
|
+
if (element === null || element === undefined) return ''
|
|
279
|
+
let text = ''
|
|
280
|
+
const pending = [element]
|
|
281
|
+
let visited = 0
|
|
282
|
+
while (pending.length > 0) {
|
|
283
|
+
// Fail closed instead of comparing a truncated reconstruction.
|
|
284
|
+
if (visited >= MAX_PROJECTION_NODES) return ''
|
|
285
|
+
const node = pending.pop()
|
|
286
|
+
visited += 1
|
|
287
|
+
if (node === null || node === undefined) continue
|
|
288
|
+
if (node.nodeType === 3) {
|
|
289
|
+
text += node.nodeValue || ''
|
|
290
|
+
continue
|
|
291
|
+
}
|
|
292
|
+
if (node.nodeType !== undefined && node.nodeType !== 1) continue
|
|
293
|
+
if (nodeAttribute(node, REFERENCE_CHIP_MARKER) !== null) {
|
|
294
|
+
const title = nodeAttribute(node, REFERENCE_CHIP_TITLE)
|
|
295
|
+
if (typeof title === 'string' && title !== '') {
|
|
296
|
+
text += title
|
|
297
|
+
continue
|
|
298
|
+
}
|
|
299
|
+
}
|
|
300
|
+
const children = node.childNodes === undefined || node.childNodes === null
|
|
301
|
+
? Array.from(node.children || [])
|
|
302
|
+
: Array.from(node.childNodes)
|
|
303
|
+
if (children.length === 0) {
|
|
304
|
+
if (typeof node.textContent === 'string') text += node.textContent
|
|
305
|
+
continue
|
|
306
|
+
}
|
|
307
|
+
for (let index = children.length - 1; index >= 0; index -= 1) pending.push(children[index])
|
|
308
|
+
}
|
|
309
|
+
return text
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
function elementTextAffinity(element, expectedText) {
|
|
313
|
+
const plain = textAffinity(element?.textContent, expectedText)
|
|
314
|
+
if (plain === 5000) return plain
|
|
315
|
+
const projected = referenceProjectedText(element)
|
|
316
|
+
if (projected === '') return plain
|
|
317
|
+
return Math.max(plain, textAffinity(projected, expectedText))
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
function nonTransparentColor(value) {
|
|
321
|
+
const color = String(value || '').replace(/\s+/gu, '').toLowerCase()
|
|
322
|
+
if (color === '' || color === 'transparent') return false
|
|
323
|
+
return !/^rgba\([^)]*,0(?:\.0+)?\)$/u.test(color)
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
function bubbleSurfaceOf(style) {
|
|
327
|
+
const backgroundImage = String(style.backgroundImage || '').toLowerCase()
|
|
328
|
+
const borderWidth = Math.max(
|
|
329
|
+
parsePixels(style.borderTopWidth),
|
|
330
|
+
parsePixels(style.borderRightWidth),
|
|
331
|
+
parsePixels(style.borderBottomWidth),
|
|
332
|
+
parsePixels(style.borderLeftWidth)
|
|
333
|
+
)
|
|
334
|
+
const radius = Math.max(
|
|
335
|
+
parsePixels(style.borderTopLeftRadius),
|
|
336
|
+
parsePixels(style.borderTopRightRadius),
|
|
337
|
+
parsePixels(style.borderBottomLeftRadius),
|
|
338
|
+
parsePixels(style.borderBottomRightRadius)
|
|
339
|
+
)
|
|
340
|
+
const padding = verticalBoxInset(style)
|
|
341
|
+
const painted = nonTransparentColor(style.backgroundColor)
|
|
342
|
+
|| (backgroundImage !== '' && backgroundImage !== 'none')
|
|
343
|
+
|| borderWidth > 0
|
|
344
|
+
return { painted, padding, radius }
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
function roundedBackgroundElement(row, expectedText) {
|
|
348
|
+
if (row === null || typeof row.querySelector !== 'function') return null
|
|
349
|
+
const hoverRoot = typeof row.matches === 'function' && row.matches('[data-time-hover-root]')
|
|
350
|
+
? row
|
|
351
|
+
: row.querySelector('[data-time-hover-root]')
|
|
352
|
+
// The audited DSH line removed the hover-root marker. The row itself remains an
|
|
353
|
+
// authoritative boundary, so score only that row and its descendants.
|
|
354
|
+
const root = hoverRoot ?? row
|
|
355
|
+
|
|
356
|
+
for (const selector of BUBBLE_MARKERS) {
|
|
357
|
+
const marked = typeof root.matches === 'function' && root.matches(selector)
|
|
358
|
+
? root
|
|
359
|
+
: root.querySelector(selector)
|
|
360
|
+
if (rectOf(marked) !== null && elementTextAffinity(marked, expectedText) > 0) return marked
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
const candidates = [root]
|
|
364
|
+
if (typeof root.querySelectorAll === 'function') {
|
|
365
|
+
for (const child of root.querySelectorAll('*')) candidates.push(child)
|
|
366
|
+
}
|
|
367
|
+
let best = null
|
|
368
|
+
let bestScore = -1
|
|
369
|
+
let secondScore = -1
|
|
370
|
+
for (const candidate of candidates) {
|
|
371
|
+
const rect = rectOf(candidate)
|
|
372
|
+
if (rect === null) continue
|
|
373
|
+
// Reject non-surfaces before reconstructing projected text, so the bounded
|
|
374
|
+
// chip-title walk only runs for plausible bubbles.
|
|
375
|
+
const surface = bubbleSurfaceOf(computedStyleOf(candidate))
|
|
376
|
+
if (!surface.painted && surface.padding <= 0) continue
|
|
377
|
+
const affinity = elementTextAffinity(candidate, expectedText)
|
|
378
|
+
if (affinity === 0) continue
|
|
379
|
+
const areaPenalty = Math.min(500, rect.width * rect.height / 10000)
|
|
380
|
+
const score = affinity
|
|
381
|
+
+ (surface.painted ? 1000 : 0)
|
|
382
|
+
+ Math.min(500, surface.padding * 10)
|
|
383
|
+
+ Math.min(100, surface.radius)
|
|
384
|
+
- areaPenalty
|
|
385
|
+
if (score > bestScore) {
|
|
386
|
+
secondScore = bestScore
|
|
387
|
+
best = candidate
|
|
388
|
+
bestScore = score
|
|
389
|
+
} else if (score > secondScore) {
|
|
390
|
+
secondScore = score
|
|
391
|
+
}
|
|
392
|
+
}
|
|
393
|
+
|
|
394
|
+
return best !== null && bestScore - secondScore > 25 ? best : null
|
|
395
|
+
}
|
|
396
|
+
|
|
397
|
+
function ownedStyleValues(style, properties) {
|
|
398
|
+
const owned = {}
|
|
399
|
+
for (const property of properties) {
|
|
400
|
+
const value = style[property]
|
|
401
|
+
if (typeof value === 'string' && value !== '') owned[property] = value
|
|
402
|
+
}
|
|
403
|
+
return owned
|
|
404
|
+
}
|
|
405
|
+
|
|
406
|
+
function applyStyleValues(element, values) {
|
|
407
|
+
if (element?.style === undefined) return
|
|
408
|
+
Object.assign(element.style, values)
|
|
409
|
+
}
|
|
410
|
+
|
|
411
|
+
function visualStyleOf(source) {
|
|
412
|
+
const style = computedStyleOf(source)
|
|
413
|
+
const visual = {
|
|
414
|
+
boxSizing: style.boxSizing || 'border-box',
|
|
415
|
+
background: style.background || 'var(--dsw-specific-bubble)',
|
|
416
|
+
backgroundClip: style.backgroundClip || 'border-box',
|
|
417
|
+
backgroundOrigin: style.backgroundOrigin || 'padding-box',
|
|
418
|
+
backgroundPosition: style.backgroundPosition || '0% 0%',
|
|
419
|
+
backgroundRepeat: style.backgroundRepeat || 'repeat',
|
|
420
|
+
backgroundSize: style.backgroundSize || 'auto',
|
|
421
|
+
color: style.color || 'inherit',
|
|
422
|
+
border: style.border || '0px solid transparent',
|
|
423
|
+
borderRadius: style.borderRadius || '22px',
|
|
424
|
+
padding: style.padding || '10px 16px',
|
|
425
|
+
fontFamily: style.fontFamily || 'inherit',
|
|
426
|
+
fontSize: style.fontSize || '16px',
|
|
427
|
+
fontWeight: style.fontWeight || '400',
|
|
428
|
+
fontStyle: style.fontStyle || 'normal',
|
|
429
|
+
fontVariant: style.fontVariant || 'normal',
|
|
430
|
+
fontStretch: style.fontStretch || 'normal',
|
|
431
|
+
fontFeatureSettings: style.fontFeatureSettings || 'normal',
|
|
432
|
+
fontKerning: style.fontKerning || 'auto',
|
|
433
|
+
lineHeight: style.lineHeight || '24px',
|
|
434
|
+
letterSpacing: style.letterSpacing || 'normal',
|
|
435
|
+
textAlign: style.textAlign || 'start',
|
|
436
|
+
textDecoration: style.textDecoration || 'none',
|
|
437
|
+
textIndent: style.textIndent || '0px',
|
|
438
|
+
textShadow: style.textShadow || 'none',
|
|
439
|
+
textTransform: style.textTransform || 'none',
|
|
440
|
+
wordBreak: style.wordBreak || 'normal',
|
|
441
|
+
overflowWrap: style.overflowWrap || 'normal',
|
|
442
|
+
whiteSpace: style.whiteSpace || 'normal',
|
|
443
|
+
direction: style.direction || 'ltr',
|
|
444
|
+
writingMode: style.writingMode || 'horizontal-tb',
|
|
445
|
+
hyphens: style.hyphens || 'manual',
|
|
446
|
+
tabSize: style.tabSize || '8',
|
|
447
|
+
display: style.display || 'block',
|
|
448
|
+
overflow: style.overflow || 'visible',
|
|
449
|
+
overflowX: style.overflowX || style.overflow || 'visible',
|
|
450
|
+
overflowY: style.overflowY || style.overflow || 'visible',
|
|
451
|
+
textOverflow: style.textOverflow || 'clip',
|
|
452
|
+
boxShadow: style.boxShadow || 'none',
|
|
453
|
+
opacity: style.opacity || '1',
|
|
454
|
+
filter: style.filter || 'none',
|
|
455
|
+
backdropFilter: style.backdropFilter || 'none'
|
|
456
|
+
}
|
|
457
|
+
const metrics = {
|
|
458
|
+
horizontalInset: horizontalBoxInset(style),
|
|
459
|
+
lineHeight: resolvedLineHeight(style),
|
|
460
|
+
transform: style.transform || 'none',
|
|
461
|
+
verticalInset: verticalBoxInset(style),
|
|
462
|
+
zoom: style.zoom || '1'
|
|
463
|
+
}
|
|
464
|
+
return {
|
|
465
|
+
style: visual,
|
|
466
|
+
metrics,
|
|
467
|
+
signature: [...Object.values(visual), ...Object.values(metrics)].join('|')
|
|
468
|
+
}
|
|
469
|
+
}
|
|
470
|
+
|
|
471
|
+
function copyDescendantStyles(source, clone) {
|
|
472
|
+
const pending = [[source, clone]]
|
|
473
|
+
let visited = 0
|
|
474
|
+
while (pending.length > 0 && visited < MAX_STYLE_TREE_NODES) {
|
|
475
|
+
const [sourceNode, cloneNode] = pending.shift()
|
|
476
|
+
const sourceChildren = Array.from(sourceNode?.children || [])
|
|
477
|
+
const cloneChildren = Array.from(cloneNode?.children || [])
|
|
478
|
+
const count = Math.min(sourceChildren.length, cloneChildren.length)
|
|
479
|
+
for (let index = 0; index < count; index += 1) {
|
|
480
|
+
const sourceChild = sourceChildren[index]
|
|
481
|
+
const cloneChild = cloneChildren[index]
|
|
482
|
+
applyStyleValues(cloneChild, ownedStyleValues(
|
|
483
|
+
computedStyleOf(sourceChild),
|
|
484
|
+
DESCENDANT_STYLE_PROPERTIES
|
|
485
|
+
))
|
|
486
|
+
pending.push([sourceChild, cloneChild])
|
|
487
|
+
visited += 1
|
|
488
|
+
if (visited >= MAX_STYLE_TREE_NODES) break
|
|
489
|
+
}
|
|
490
|
+
}
|
|
491
|
+
}
|
|
492
|
+
|
|
493
|
+
function measurementState(failure) {
|
|
494
|
+
return { failure }
|
|
495
|
+
}
|
|
496
|
+
|
|
497
|
+
function measurePinned(scroll, overlay, userIndex) {
|
|
498
|
+
if (userIndex.size === 0) return measurementState('inactive-snapshot')
|
|
499
|
+
if (scroll === null) return measurementState('missing-scroll')
|
|
500
|
+
if (overlay === null) return measurementState('missing-overlay')
|
|
501
|
+
const flow = visibleFlow(scroll)
|
|
502
|
+
if (flow === null) return measurementState('missing-flow')
|
|
503
|
+
const scrollRect = rectOf(scroll)
|
|
504
|
+
const overlayRect = rectOf(overlay)
|
|
505
|
+
if (scrollRect === null) return measurementState('invalid-scroll-geometry')
|
|
506
|
+
if (overlayRect === null) return measurementState('invalid-overlay-geometry')
|
|
507
|
+
|
|
508
|
+
const edge = scrollRect.top + readingInset(flow, scroll)
|
|
509
|
+
let active = null
|
|
510
|
+
let incoming = null
|
|
511
|
+
const rows = flow.querySelectorAll(ROW_SELECTOR)
|
|
512
|
+
for (const row of rows) {
|
|
513
|
+
const kind = row.getAttribute?.('data-chat-flow-kind')
|
|
514
|
+
if (!USER_KINDS.has(kind)) continue
|
|
515
|
+
const key = row.getAttribute?.('data-chat-flow-key') || row.getAttribute?.('data-chat-anchor-key')
|
|
516
|
+
const message = key === null || key === undefined ? undefined : userIndex.get(key)
|
|
517
|
+
if (message === undefined) continue
|
|
518
|
+
const rowRect = rectOf(row)
|
|
519
|
+
if (rowRect === null) continue
|
|
520
|
+
if (rowRect.top <= edge + EPSILON) {
|
|
521
|
+
active = { row, message }
|
|
522
|
+
incoming = null
|
|
523
|
+
continue
|
|
524
|
+
}
|
|
525
|
+
// The first user row below the reading line is the card the clone must yield to.
|
|
526
|
+
if (active !== null && incoming === null) incoming = { row, rect: rowRect }
|
|
527
|
+
}
|
|
528
|
+
if (active === null) return measurementState('inactive-before-edge')
|
|
529
|
+
|
|
530
|
+
const source = roundedBackgroundElement(active.row, active.message.text)
|
|
531
|
+
if (source === null) return measurementState('bubble-not-found')
|
|
532
|
+
const sourceRect = rectOf(source)
|
|
533
|
+
if (sourceRect === null) return measurementState('invalid-source-geometry')
|
|
534
|
+
// Do not duplicate the original while any part of its bubble is still painted.
|
|
535
|
+
// The bubble only disappears once its bottom clears the scrollport's clip edge,
|
|
536
|
+
// which sits `readingInset` above the reading line the clone is laid out on.
|
|
537
|
+
if (sourceRect.bottom > paintedTopBoundary(source, scroll, scrollRect) + EPSILON) {
|
|
538
|
+
return measurementState('inactive-source-visible')
|
|
539
|
+
}
|
|
540
|
+
|
|
541
|
+
const visual = visualStyleOf(source)
|
|
542
|
+
const lowerBoundary = readingLowerBoundary(scroll, scrollRect, overlayRect)
|
|
543
|
+
const availableHeight = Math.max(1, lowerBoundary - edge - 16)
|
|
544
|
+
// Hand the slot over to the next user card instead of covering it: once that card's
|
|
545
|
+
// leading edge comes within the flow gap of the clone's bottom, the clone is pushed up
|
|
546
|
+
// — clipped at the scrollport edge — exactly like a sticky list header yielding to the
|
|
547
|
+
// next section, while keeping `gap` of clearance instead of sliding flush against it.
|
|
548
|
+
// `collapsedHeight` is not known before the clone is measured, so cap the estimate at
|
|
549
|
+
// the natural three-line bubble height. At the reading line the required push is
|
|
550
|
+
// exactly `collapsedHeight + gap`, which is also the clamp.
|
|
551
|
+
const collapsedHeight = Math.min(
|
|
552
|
+
sourceRect.height,
|
|
553
|
+
visual.metrics.lineHeight * MAX_COLLAPSED_LINES + visual.metrics.verticalInset
|
|
554
|
+
)
|
|
555
|
+
const gap = incoming === null ? 0 : pushGapOf(incoming.row)
|
|
556
|
+
const push = incoming === null
|
|
557
|
+
? 0
|
|
558
|
+
: Math.max(0, Math.min(collapsedHeight + gap, gap + edge + collapsedHeight - incoming.rect.top))
|
|
559
|
+
// Expanding the pinned bubble may not invade that card either: cap the expanded box by
|
|
560
|
+
// the room between the reading line and the card's reserved gap, so a long pinned
|
|
561
|
+
// message scrolls inside its box instead of growing over the next message.
|
|
562
|
+
const expansionRoom = incoming === null
|
|
563
|
+
? availableHeight
|
|
564
|
+
: Math.min(availableHeight, Math.max(0, incoming.rect.top - gap - edge))
|
|
565
|
+
return {
|
|
566
|
+
key: active.message.key,
|
|
567
|
+
text: active.message.text,
|
|
568
|
+
row: active.row,
|
|
569
|
+
flow,
|
|
570
|
+
source,
|
|
571
|
+
scroll,
|
|
572
|
+
left: sourceRect.left - overlayRect.left,
|
|
573
|
+
top: edge - overlayRect.top,
|
|
574
|
+
width: sourceRect.width,
|
|
575
|
+
sourceHeight: sourceRect.height,
|
|
576
|
+
availableHeight,
|
|
577
|
+
push,
|
|
578
|
+
inset: edge - scrollRect.top,
|
|
579
|
+
expansionRoom,
|
|
580
|
+
visual: visual.style,
|
|
581
|
+
visualMetrics: visual.metrics,
|
|
582
|
+
visualSignature: visual.signature
|
|
583
|
+
}
|
|
584
|
+
}
|
|
585
|
+
|
|
586
|
+
function sameNumber(left, right) {
|
|
587
|
+
return typeof left === 'number' && typeof right === 'number' && Math.abs(left - right) <= EPSILON
|
|
588
|
+
}
|
|
589
|
+
|
|
590
|
+
function samePin(left, right) {
|
|
591
|
+
if (left === right) return true
|
|
592
|
+
if (left === null || right === null) return false
|
|
593
|
+
if (left.failure !== undefined || right.failure !== undefined) return left.failure === right.failure
|
|
594
|
+
return left.key === right.key
|
|
595
|
+
&& left.text === right.text
|
|
596
|
+
&& left.row === right.row
|
|
597
|
+
&& left.flow === right.flow
|
|
598
|
+
&& left.source === right.source
|
|
599
|
+
&& left.scroll === right.scroll
|
|
600
|
+
&& sameNumber(left.left, right.left)
|
|
601
|
+
&& sameNumber(left.top, right.top)
|
|
602
|
+
&& sameNumber(left.width, right.width)
|
|
603
|
+
&& sameNumber(left.sourceHeight, right.sourceHeight)
|
|
604
|
+
&& sameNumber(left.availableHeight, right.availableHeight)
|
|
605
|
+
&& left.visualSignature === right.visualSignature
|
|
606
|
+
}
|
|
607
|
+
|
|
608
|
+
function sanitizeClone(element, root = true) {
|
|
609
|
+
if (element === null || element === undefined) return
|
|
610
|
+
element.removeAttribute?.('id')
|
|
611
|
+
element.removeAttribute?.('for')
|
|
612
|
+
element.removeAttribute?.('href')
|
|
613
|
+
element.removeAttribute?.('target')
|
|
614
|
+
element.removeAttribute?.('tabindex')
|
|
615
|
+
element.removeAttribute?.('contenteditable')
|
|
616
|
+
element.removeAttribute?.('accesskey')
|
|
617
|
+
element.removeAttribute?.('autofocus')
|
|
618
|
+
if ('tabIndex' in element) element.tabIndex = -1
|
|
619
|
+
if (element.disabled !== undefined) element.disabled = true
|
|
620
|
+
|
|
621
|
+
if (typeof element.attributes === 'object' && element.attributes !== null) {
|
|
622
|
+
for (const attribute of Array.from(element.attributes)) {
|
|
623
|
+
const name = String(attribute.name).toLowerCase()
|
|
624
|
+
if (name.startsWith('aria-') || name.startsWith('data-chat-') || name.startsWith('on')) {
|
|
625
|
+
element.removeAttribute(name)
|
|
626
|
+
}
|
|
627
|
+
}
|
|
628
|
+
}
|
|
629
|
+
if (!root && element.style !== undefined) element.style.pointerEvents = 'none'
|
|
630
|
+
if (element.children !== undefined) {
|
|
631
|
+
for (const child of Array.from(element.children)) sanitizeClone(child, false)
|
|
632
|
+
}
|
|
633
|
+
}
|
|
634
|
+
|
|
635
|
+
function clearHost(host) {
|
|
636
|
+
if (host === null || host === undefined) return
|
|
637
|
+
while (host.firstChild !== null && host.firstChild !== undefined) host.removeChild(host.firstChild)
|
|
638
|
+
}
|
|
639
|
+
|
|
640
|
+
function wrapCloneContent(clone) {
|
|
641
|
+
const ownerDocument = clone?.ownerDocument
|
|
642
|
+
|| (typeof document === 'undefined' ? undefined : document)
|
|
643
|
+
if (typeof ownerDocument?.createElement !== 'function') return null
|
|
644
|
+
|
|
645
|
+
const content = ownerDocument.createElement('div')
|
|
646
|
+
content.setAttribute?.('data-dsh-sticky-user-bubble-content', '')
|
|
647
|
+
if (content.style !== undefined) {
|
|
648
|
+
Object.assign(content.style, {
|
|
649
|
+
boxSizing: 'border-box',
|
|
650
|
+
minWidth: '0',
|
|
651
|
+
maxWidth: '100%',
|
|
652
|
+
pointerEvents: 'none'
|
|
653
|
+
})
|
|
654
|
+
}
|
|
655
|
+
const nodes = clone.childNodes !== undefined
|
|
656
|
+
? Array.from(clone.childNodes)
|
|
657
|
+
: Array.from(clone.children || [])
|
|
658
|
+
for (const node of nodes) content.appendChild(node)
|
|
659
|
+
clone.appendChild(content)
|
|
660
|
+
return content
|
|
661
|
+
}
|
|
662
|
+
|
|
663
|
+
function cssPropertyName(property) {
|
|
664
|
+
if (property.startsWith('Webkit')) {
|
|
665
|
+
return `-webkit-${property.slice(6).replace(/[A-Z]/gu, (letter) => `-${letter.toLowerCase()}`).replace(/^-+/u, '')}`
|
|
666
|
+
}
|
|
667
|
+
return property.replace(/[A-Z]/gu, (letter) => `-${letter.toLowerCase()}`)
|
|
668
|
+
}
|
|
669
|
+
|
|
670
|
+
function setStyleValue(element, property, value, important = false) {
|
|
671
|
+
if (element?.style === undefined) return
|
|
672
|
+
if (typeof element.style.setProperty === 'function') {
|
|
673
|
+
element.style.setProperty(cssPropertyName(property), String(value), important ? 'important' : '')
|
|
674
|
+
} else {
|
|
675
|
+
element.style[property] = String(value)
|
|
676
|
+
}
|
|
677
|
+
}
|
|
678
|
+
|
|
679
|
+
function setCriticalStyles(element, values) {
|
|
680
|
+
for (const [property, value] of Object.entries(values)) setStyleValue(element, property, value, true)
|
|
681
|
+
}
|
|
682
|
+
|
|
683
|
+
function rangeRectsOf(ownerDocument, node) {
|
|
684
|
+
let range
|
|
685
|
+
try {
|
|
686
|
+
range = ownerDocument.createRange()
|
|
687
|
+
range.selectNodeContents(node)
|
|
688
|
+
return Array.from(range.getClientRects?.() || [])
|
|
689
|
+
} catch {
|
|
690
|
+
return []
|
|
691
|
+
} finally {
|
|
692
|
+
range?.detach?.()
|
|
693
|
+
}
|
|
694
|
+
}
|
|
695
|
+
|
|
696
|
+
function textRectFragmentsOf(content, ownerDocument) {
|
|
697
|
+
if (typeof ownerDocument.createTreeWalker === 'function') {
|
|
698
|
+
const fragments = []
|
|
699
|
+
try {
|
|
700
|
+
const showText = ownerDocument.defaultView?.NodeFilter?.SHOW_TEXT || 4
|
|
701
|
+
const walker = ownerDocument.createTreeWalker(content, showText)
|
|
702
|
+
let textNode = walker.nextNode()
|
|
703
|
+
let visited = 0
|
|
704
|
+
while (textNode !== null && visited < MAX_LINE_TEXT_NODES) {
|
|
705
|
+
if (/\S/u.test(textNode.nodeValue || '')) {
|
|
706
|
+
fragments.push(...rangeRectsOf(ownerDocument, textNode))
|
|
707
|
+
}
|
|
708
|
+
textNode = walker.nextNode()
|
|
709
|
+
visited += 1
|
|
710
|
+
}
|
|
711
|
+
} catch {
|
|
712
|
+
// Fall through to a whole-content range on older DOM implementations.
|
|
713
|
+
}
|
|
714
|
+
if (fragments.length > 0) return fragments
|
|
715
|
+
}
|
|
716
|
+
return rangeRectsOf(ownerDocument, content)
|
|
717
|
+
}
|
|
718
|
+
|
|
719
|
+
function lineBoxesOf(content) {
|
|
720
|
+
const ownerDocument = content?.ownerDocument
|
|
721
|
+
|| (typeof document === 'undefined' ? undefined : document)
|
|
722
|
+
if (typeof ownerDocument?.createRange !== 'function') return []
|
|
723
|
+
const contentRect = rectOf(content)
|
|
724
|
+
if (contentRect === null) return []
|
|
725
|
+
|
|
726
|
+
const rects = textRectFragmentsOf(content, ownerDocument)
|
|
727
|
+
.filter((rect) => finiteDimension(rect.height) && Number.isFinite(rect.top) && Number.isFinite(rect.bottom))
|
|
728
|
+
.sort((left, right) => left.top - right.top || (left.left || 0) - (right.left || 0))
|
|
729
|
+
const lines = []
|
|
730
|
+
for (const rect of rects) {
|
|
731
|
+
const line = lines.find((candidate) => (
|
|
732
|
+
rect.top < candidate.bottom - LINE_MERGE_EPSILON
|
|
733
|
+
&& rect.bottom > candidate.top + LINE_MERGE_EPSILON
|
|
734
|
+
))
|
|
735
|
+
if (line === undefined) {
|
|
736
|
+
lines.push({ top: rect.top, bottom: rect.bottom })
|
|
737
|
+
} else {
|
|
738
|
+
line.top = Math.min(line.top, rect.top)
|
|
739
|
+
line.bottom = Math.max(line.bottom, rect.bottom)
|
|
740
|
+
}
|
|
741
|
+
}
|
|
742
|
+
return lines.map((line) => ({
|
|
743
|
+
top: line.top - contentRect.top,
|
|
744
|
+
bottom: line.bottom - contentRect.top
|
|
745
|
+
}))
|
|
746
|
+
}
|
|
747
|
+
|
|
748
|
+
function naturalCloneGeometry(content, pin) {
|
|
749
|
+
const fallbackContentHeight = Math.max(
|
|
750
|
+
1,
|
|
751
|
+
pin.sourceHeight - pin.visualMetrics.verticalInset
|
|
752
|
+
)
|
|
753
|
+
const contentRect = rectOf(content)
|
|
754
|
+
const scrollHeight = Number(content?.scrollHeight)
|
|
755
|
+
const measuredContentHeight = Math.max(
|
|
756
|
+
Number.isFinite(scrollHeight) && scrollHeight > 0 ? scrollHeight : 0,
|
|
757
|
+
contentRect?.height || 0
|
|
758
|
+
)
|
|
759
|
+
const naturalContentHeight = measuredContentHeight > 0
|
|
760
|
+
? measuredContentHeight
|
|
761
|
+
: fallbackContentHeight
|
|
762
|
+
const lines = lineBoxesOf(content)
|
|
763
|
+
const fallbackLimit = pin.visualMetrics.lineHeight * MAX_COLLAPSED_LINES
|
|
764
|
+
const measuredLimit = lines.length > MAX_COLLAPSED_LINES
|
|
765
|
+
? lines[MAX_COLLAPSED_LINES].top - lines[0].top
|
|
766
|
+
: lines.length === MAX_COLLAPSED_LINES
|
|
767
|
+
? lines[MAX_COLLAPSED_LINES - 1].bottom + Math.max(0, lines[0].top)
|
|
768
|
+
: fallbackLimit
|
|
769
|
+
const collapsedContentHeight = Math.max(
|
|
770
|
+
pin.visualMetrics.lineHeight,
|
|
771
|
+
Math.min(naturalContentHeight, measuredLimit)
|
|
772
|
+
)
|
|
773
|
+
const clamped = lines.length > MAX_COLLAPSED_LINES
|
|
774
|
+
|| naturalContentHeight > collapsedContentHeight + EPSILON
|
|
775
|
+
const fullHeight = pin.visualMetrics.verticalInset + naturalContentHeight
|
|
776
|
+
const collapsedHeight = pin.visualMetrics.verticalInset + collapsedContentHeight
|
|
777
|
+
const expandedHeight = Math.min(
|
|
778
|
+
fullHeight,
|
|
779
|
+
Math.max(collapsedHeight, expansionRoomOf(pin))
|
|
780
|
+
)
|
|
781
|
+
return {
|
|
782
|
+
clamped,
|
|
783
|
+
collapsedContentHeight,
|
|
784
|
+
collapsedHeight,
|
|
785
|
+
expandedHeight,
|
|
786
|
+
fullHeight,
|
|
787
|
+
lineCount: lines.length,
|
|
788
|
+
naturalContentHeight,
|
|
789
|
+
viewportClamped: fullHeight > expandedHeight + EPSILON
|
|
790
|
+
}
|
|
791
|
+
}
|
|
792
|
+
|
|
793
|
+
// Room the expanded bubble may occupy: the viewport, narrowed by the space the next user
|
|
794
|
+
// card reserves. `collapsedHeight` stays the floor, so a card already inside the
|
|
795
|
+
// footprint means the hovered bubble simply stays collapsed — it never grows over it.
|
|
796
|
+
function expansionRoomOf(pin) {
|
|
797
|
+
return Number.isFinite(pin?.expansionRoom) ? pin.expansionRoom : pin?.availableHeight
|
|
798
|
+
}
|
|
799
|
+
|
|
800
|
+
// Re-derive the expanded height for a newer pin without re-measuring the clone: only the
|
|
801
|
+
// free room changes while the next user card approaches.
|
|
802
|
+
function withExpansionRoom(geometry, pin) {
|
|
803
|
+
const expandedHeight = Math.min(
|
|
804
|
+
geometry.fullHeight,
|
|
805
|
+
Math.max(geometry.collapsedHeight, expansionRoomOf(pin))
|
|
806
|
+
)
|
|
807
|
+
return {
|
|
808
|
+
...geometry,
|
|
809
|
+
expandedHeight,
|
|
810
|
+
viewportClamped: geometry.fullHeight > expandedHeight + EPSILON
|
|
811
|
+
}
|
|
812
|
+
}
|
|
813
|
+
|
|
814
|
+
function compatibleRootLayout(pin) {
|
|
815
|
+
const display = String(pin.visual.display || '').toLowerCase()
|
|
816
|
+
const writingMode = String(pin.visual.writingMode || '').toLowerCase()
|
|
817
|
+
if (writingMode !== '' && writingMode !== 'horizontal-tb') return 'unsupported-writing-mode'
|
|
818
|
+
if (display.includes('flex') || display.includes('grid') || display.includes('table')) return 'unsupported-root-display'
|
|
819
|
+
if (String(pin.visualMetrics.transform || '').toLowerCase() !== 'none') return 'unsupported-transform'
|
|
820
|
+
const zoom = Number.parseFloat(pin.visualMetrics.zoom)
|
|
821
|
+
if (Number.isFinite(zoom) && Math.abs(zoom - 1) > EPSILON / 100) return 'unsupported-zoom'
|
|
822
|
+
return null
|
|
823
|
+
}
|
|
824
|
+
|
|
825
|
+
function containsNode(parent, node) {
|
|
826
|
+
if (parent === null || parent === undefined || node === null || node === undefined) return false
|
|
827
|
+
if (typeof parent.contains === 'function') return parent.contains(node)
|
|
828
|
+
let current = node
|
|
829
|
+
while (current !== null && current !== undefined) {
|
|
830
|
+
if (current === parent) return true
|
|
831
|
+
current = current.parentElement
|
|
832
|
+
}
|
|
833
|
+
return false
|
|
834
|
+
}
|
|
835
|
+
|
|
836
|
+
function cloneCompatibilityState(clone, content, pin, geometry) {
|
|
837
|
+
const layoutFailure = compatibleRootLayout(pin)
|
|
838
|
+
if (layoutFailure !== null) return layoutFailure
|
|
839
|
+
if (!containsNode(pin.flow, pin.source)) return 'detached-source'
|
|
840
|
+
if (!finiteDimension(geometry.fullHeight) || !finiteDimension(geometry.collapsedHeight)) return 'invalid-height'
|
|
841
|
+
const cloneRect = rectOf(clone)
|
|
842
|
+
if (cloneRect === null || Math.abs(cloneRect.width - pin.width) > 2) return 'width-mismatch'
|
|
843
|
+
const contentRect = rectOf(content)
|
|
844
|
+
const expectedContentWidth = Math.max(1, pin.width - pin.visualMetrics.horizontalInset)
|
|
845
|
+
if (contentRect !== null && Math.abs(contentRect.width - expectedContentWidth) > 4) return 'content-width-mismatch'
|
|
846
|
+
if (geometry.clamped && geometry.collapsedContentHeight < pin.visualMetrics.lineHeight - EPSILON) return 'invalid-line-limit'
|
|
847
|
+
if (content === null || content === undefined) return 'missing-content'
|
|
848
|
+
return 'ready'
|
|
849
|
+
}
|
|
850
|
+
|
|
851
|
+
function setHostState(host, state) {
|
|
852
|
+
if (host === null || host === undefined) return
|
|
853
|
+
if (state === null) host.removeAttribute?.('data-dsh-sticky-user-bubble-state')
|
|
854
|
+
else host.setAttribute?.('data-dsh-sticky-user-bubble-state', state)
|
|
855
|
+
}
|
|
856
|
+
|
|
857
|
+
function setCloneExpanded(clone, content, pin, geometry, expanded) {
|
|
858
|
+
if (clone?.style === undefined || content?.style === undefined) return
|
|
859
|
+
// Expanding only makes sense while the extra height really fits: a bubble whose room is
|
|
860
|
+
// taken by the next user card keeps its three-line presentation instead of turning into
|
|
861
|
+
// a sliver-high internal scroller.
|
|
862
|
+
const expandable = expanded && geometry.expandedHeight > geometry.collapsedHeight + EPSILON
|
|
863
|
+
const showFull = expandable || !geometry.clamped
|
|
864
|
+
const targetHeight = showFull ? geometry.expandedHeight : geometry.collapsedHeight
|
|
865
|
+
const scrollable = expandable && geometry.viewportClamped
|
|
866
|
+
setCriticalStyles(clone, {
|
|
867
|
+
height: `${targetHeight}px`,
|
|
868
|
+
maxHeight: `${targetHeight}px`,
|
|
869
|
+
display: pin.visual.display,
|
|
870
|
+
// The bubble itself never scrolls: the copied padding and rounded corners stay intact
|
|
871
|
+
// and the scrollbar of a capped expansion lives inside the content instead.
|
|
872
|
+
overflow: showFull ? (scrollable ? 'hidden' : pin.visual.overflow) : 'hidden',
|
|
873
|
+
overflowX: showFull && !scrollable ? pin.visual.overflowX : 'hidden',
|
|
874
|
+
overflowY: showFull && !scrollable ? pin.visual.overflowY : 'hidden',
|
|
875
|
+
textOverflow: pin.visual.textOverflow,
|
|
876
|
+
WebkitBoxOrient: 'initial',
|
|
877
|
+
WebkitLineClamp: 'unset'
|
|
878
|
+
})
|
|
879
|
+
|
|
880
|
+
const contentHeight = Math.max(1, targetHeight - pin.visualMetrics.verticalInset)
|
|
881
|
+
setCriticalStyles(content, {
|
|
882
|
+
height: showFull ? (scrollable ? `${contentHeight}px` : 'auto') : `${geometry.collapsedContentHeight}px`,
|
|
883
|
+
maxHeight: showFull ? (scrollable ? `${contentHeight}px` : 'none') : `${geometry.collapsedContentHeight}px`,
|
|
884
|
+
display: showFull ? 'block' : '-webkit-box',
|
|
885
|
+
// `overflow-y: visible` next to a clipped axis would compute to `auto`, so the full
|
|
886
|
+
// non-scrollable case keeps the bubble's own axis values (both `visible`).
|
|
887
|
+
overflow: showFull && !scrollable ? 'visible' : 'hidden',
|
|
888
|
+
overflowX: showFull && !scrollable ? pin.visual.overflowX : 'hidden',
|
|
889
|
+
overflowY: scrollable ? 'auto' : (showFull ? pin.visual.overflowY : 'hidden'),
|
|
890
|
+
// A `pointer-events: none` scroller can receive neither wheel scrolling nor a
|
|
891
|
+
// scrollbar drag, so the wrapper becomes an input target exactly while it owns the
|
|
892
|
+
// inner scrollbar. Copied descendants stay inert and clicks still bubble to the
|
|
893
|
+
// clone root, so hovering and jumping are unaffected.
|
|
894
|
+
pointerEvents: scrollable ? 'auto' : 'none',
|
|
895
|
+
textOverflow: showFull ? 'clip' : 'ellipsis',
|
|
896
|
+
WebkitBoxOrient: showFull ? 'initial' : 'vertical',
|
|
897
|
+
WebkitLineClamp: showFull ? 'unset' : String(MAX_COLLAPSED_LINES)
|
|
898
|
+
})
|
|
899
|
+
if (scrollable) {
|
|
900
|
+
// DSH dims the thumb to `l2` for scroll areas inside a card (the composer card does
|
|
901
|
+
// the same), so the inner scrollbar does not stand out against the bubble surface.
|
|
902
|
+
setStyleValue(content, '--dsh-scrollbar-thumb', 'var(--dsw-alias-scrollbar-bg-l2)', true)
|
|
903
|
+
setStyleValue(content, '--dsh-scrollbar-thumb-hover', 'var(--dsw-alias-scrollbar-hover-l2)', true)
|
|
904
|
+
}
|
|
905
|
+
}
|
|
906
|
+
|
|
907
|
+
function jumpToOriginal(pin) {
|
|
908
|
+
const target = rectOf(pin.source) === null ? pin.row : pin.source
|
|
909
|
+
const targetRect = rectOf(target)
|
|
910
|
+
const scrollRect = rectOf(pin.scroll)
|
|
911
|
+
const currentScrollTop = pin.scroll?.scrollTop
|
|
912
|
+
if (targetRect !== null && scrollRect !== null && Number.isFinite(currentScrollTop)) {
|
|
913
|
+
const flow = visibleFlow(pin.scroll)
|
|
914
|
+
const edge = scrollRect.top + readingInset(flow, pin.scroll)
|
|
915
|
+
const top = Math.max(0, currentScrollTop + targetRect.top - edge)
|
|
916
|
+
if (typeof pin.scroll.scrollTo === 'function') {
|
|
917
|
+
try {
|
|
918
|
+
pin.scroll.scrollTo({ top, behavior: 'smooth' })
|
|
919
|
+
return
|
|
920
|
+
} catch {}
|
|
921
|
+
}
|
|
922
|
+
try {
|
|
923
|
+
pin.scroll.scrollTop = top
|
|
924
|
+
return
|
|
925
|
+
} catch {}
|
|
926
|
+
}
|
|
927
|
+
target?.scrollIntoView?.({ behavior: 'smooth', block: 'start' })
|
|
928
|
+
}
|
|
929
|
+
|
|
930
|
+
// The clone is only moved while it yields to the next user card, never rebuilt:
|
|
931
|
+
// re-measuring the whole bubble on every scroll frame would jitter. Only the clone's
|
|
932
|
+
// layout origin and the clip at the scrollport edge change, so both stay cheap
|
|
933
|
+
// `!important` writes on the already rendered clone.
|
|
934
|
+
function applyCloneOffset(host, pin) {
|
|
935
|
+
const clone = host?.firstChild
|
|
936
|
+
if (clone?.style === undefined) return
|
|
937
|
+
const push = Number.isFinite(pin.push) && pin.push > 0 ? pin.push : 0
|
|
938
|
+
const inset = Number.isFinite(pin.inset) && pin.inset > 0 ? pin.inset : 0
|
|
939
|
+
const clipped = Math.max(0, push - inset)
|
|
940
|
+
setStyleValue(clone, 'top', `${pin.top - push}px`, true)
|
|
941
|
+
setStyleValue(clone, 'clipPath', clipped > EPSILON ? `inset(${clipped}px 0 0 0)` : 'none', true)
|
|
942
|
+
}
|
|
943
|
+
|
|
944
|
+
function renderPinnedClone(host, pin, stateRef) {
|
|
945
|
+
if (host === null || host === undefined) return false
|
|
946
|
+
const state = stateRef?.current ?? null
|
|
947
|
+
// A rebuild replaces the mounted clone, so the pointer/keyboard expansion intent is
|
|
948
|
+
// captured here and handed to its successor. Without that, every remeasure while the
|
|
949
|
+
// answer streams collapses the hovered bubble, and the browser's own hover recompute
|
|
950
|
+
// expands the fresh clone again — a collapse/expand flicker on every update.
|
|
951
|
+
const hovered = state?.hovered === true
|
|
952
|
+
const focused = state?.focused === true
|
|
953
|
+
// Nothing of ours is mounted from here on: a clone that is built later starts neutral
|
|
954
|
+
// unless it is told otherwise at the end of this function.
|
|
955
|
+
if (state !== null) {
|
|
956
|
+
state.update = null
|
|
957
|
+
state.hovered = false
|
|
958
|
+
state.focused = false
|
|
959
|
+
}
|
|
960
|
+
clearHost(host)
|
|
961
|
+
if (pin === null) {
|
|
962
|
+
setHostState(host, null)
|
|
963
|
+
return true
|
|
964
|
+
}
|
|
965
|
+
if (pin.failure !== undefined) {
|
|
966
|
+
setHostState(host, pin.failure)
|
|
967
|
+
return pin.failure.startsWith('inactive-')
|
|
968
|
+
}
|
|
969
|
+
if (pin.source === null || typeof pin.source.cloneNode !== 'function') {
|
|
970
|
+
setHostState(host, 'missing-source')
|
|
971
|
+
return false
|
|
972
|
+
}
|
|
973
|
+
const layoutFailure = compatibleRootLayout(pin)
|
|
974
|
+
if (layoutFailure !== null) {
|
|
975
|
+
setHostState(host, layoutFailure)
|
|
976
|
+
return false
|
|
977
|
+
}
|
|
978
|
+
|
|
979
|
+
let clone
|
|
980
|
+
try {
|
|
981
|
+
clone = pin.source.cloneNode(true)
|
|
982
|
+
} catch {
|
|
983
|
+
setHostState(host, 'clone-failed')
|
|
984
|
+
return false
|
|
985
|
+
}
|
|
986
|
+
copyDescendantStyles(pin.source, clone)
|
|
987
|
+
sanitizeClone(clone)
|
|
988
|
+
const content = wrapCloneContent(clone)
|
|
989
|
+
if (content === null) {
|
|
990
|
+
setHostState(host, 'missing-content')
|
|
991
|
+
return false
|
|
992
|
+
}
|
|
993
|
+
|
|
994
|
+
applyStyleValues(clone, pin.visual)
|
|
995
|
+
setCriticalStyles(clone, {
|
|
996
|
+
boxSizing: 'border-box',
|
|
997
|
+
position: 'absolute',
|
|
998
|
+
left: `${pin.left}px`,
|
|
999
|
+
top: `${pin.top}px`,
|
|
1000
|
+
width: `${pin.width}px`,
|
|
1001
|
+
height: 'auto',
|
|
1002
|
+
maxWidth: 'none',
|
|
1003
|
+
maxHeight: 'none',
|
|
1004
|
+
minWidth: '0',
|
|
1005
|
+
minHeight: '0',
|
|
1006
|
+
margin: '0',
|
|
1007
|
+
overflow: 'visible',
|
|
1008
|
+
pointerEvents: 'none',
|
|
1009
|
+
visibility: 'hidden',
|
|
1010
|
+
userSelect: 'none',
|
|
1011
|
+
zIndex: '1'
|
|
1012
|
+
})
|
|
1013
|
+
setCriticalStyles(content, {
|
|
1014
|
+
height: 'auto',
|
|
1015
|
+
maxHeight: 'none',
|
|
1016
|
+
display: 'block',
|
|
1017
|
+
overflow: 'visible',
|
|
1018
|
+
pointerEvents: 'none'
|
|
1019
|
+
})
|
|
1020
|
+
clone.setAttribute?.('data-dsh-sticky-user-bubble-clone', '')
|
|
1021
|
+
host.appendChild(clone)
|
|
1022
|
+
|
|
1023
|
+
const geometry = naturalCloneGeometry(content, pin)
|
|
1024
|
+
const compatibilityState = cloneCompatibilityState(clone, content, pin, geometry)
|
|
1025
|
+
if (compatibilityState !== 'ready') {
|
|
1026
|
+
clearHost(host)
|
|
1027
|
+
setHostState(host, compatibilityState)
|
|
1028
|
+
return false
|
|
1029
|
+
}
|
|
1030
|
+
|
|
1031
|
+
clone.removeAttribute?.('aria-hidden')
|
|
1032
|
+
clone.removeAttribute?.('inert')
|
|
1033
|
+
clone.setAttribute?.('role', 'button')
|
|
1034
|
+
clone.setAttribute?.('tabindex', '0')
|
|
1035
|
+
clone.setAttribute?.('aria-label', JUMP_TITLE)
|
|
1036
|
+
clone.setAttribute?.('title', JUMP_TITLE)
|
|
1037
|
+
|
|
1038
|
+
let pointerInside = hovered
|
|
1039
|
+
let keyboardFocus = focused
|
|
1040
|
+
let activePin = pin
|
|
1041
|
+
const rememberIntent = () => {
|
|
1042
|
+
if (state === null) return
|
|
1043
|
+
state.hovered = pointerInside
|
|
1044
|
+
state.focused = keyboardFocus
|
|
1045
|
+
}
|
|
1046
|
+
const updateExpanded = () => setCloneExpanded(
|
|
1047
|
+
clone,
|
|
1048
|
+
content,
|
|
1049
|
+
activePin,
|
|
1050
|
+
withExpansionRoom(geometry, activePin),
|
|
1051
|
+
pointerInside || keyboardFocus
|
|
1052
|
+
)
|
|
1053
|
+
clone.addEventListener?.('mouseenter', () => {
|
|
1054
|
+
pointerInside = true
|
|
1055
|
+
rememberIntent()
|
|
1056
|
+
updateExpanded()
|
|
1057
|
+
})
|
|
1058
|
+
clone.addEventListener?.('mouseleave', () => {
|
|
1059
|
+
pointerInside = false
|
|
1060
|
+
rememberIntent()
|
|
1061
|
+
updateExpanded()
|
|
1062
|
+
})
|
|
1063
|
+
clone.addEventListener?.('focus', () => {
|
|
1064
|
+
keyboardFocus = true
|
|
1065
|
+
rememberIntent()
|
|
1066
|
+
updateExpanded()
|
|
1067
|
+
})
|
|
1068
|
+
clone.addEventListener?.('blur', () => {
|
|
1069
|
+
keyboardFocus = false
|
|
1070
|
+
rememberIntent()
|
|
1071
|
+
updateExpanded()
|
|
1072
|
+
})
|
|
1073
|
+
clone.addEventListener?.('click', (event) => {
|
|
1074
|
+
event.preventDefault?.()
|
|
1075
|
+
event.stopPropagation?.()
|
|
1076
|
+
jumpToOriginal(activePin)
|
|
1077
|
+
})
|
|
1078
|
+
clone.addEventListener?.('keydown', (event) => {
|
|
1079
|
+
if (event.key !== 'Enter' && event.key !== ' ' && event.key !== 'Spacebar') return
|
|
1080
|
+
event.preventDefault?.()
|
|
1081
|
+
event.stopPropagation?.()
|
|
1082
|
+
jumpToOriginal(activePin)
|
|
1083
|
+
})
|
|
1084
|
+
if (keyboardFocus) {
|
|
1085
|
+
// Keyboard expansion has to move to the rebuilt node, otherwise no `blur` can end it.
|
|
1086
|
+
try {
|
|
1087
|
+
clone.focus?.({ preventScroll: true })
|
|
1088
|
+
} catch {
|
|
1089
|
+
keyboardFocus = false
|
|
1090
|
+
}
|
|
1091
|
+
}
|
|
1092
|
+
rememberIntent()
|
|
1093
|
+
updateExpanded()
|
|
1094
|
+
applyCloneOffset(host, pin)
|
|
1095
|
+
setCriticalStyles(clone, {
|
|
1096
|
+
cursor: 'pointer',
|
|
1097
|
+
pointerEvents: 'auto',
|
|
1098
|
+
touchAction: 'manipulation',
|
|
1099
|
+
visibility: 'visible'
|
|
1100
|
+
})
|
|
1101
|
+
// Keep an already expanded bubble inside the free room as the next card approaches:
|
|
1102
|
+
// the measurement loop hands each newer pin to the rendered clone instead of
|
|
1103
|
+
// rebuilding it, so hovering survives the yield and can never cover that card.
|
|
1104
|
+
if (state !== null) {
|
|
1105
|
+
state.update = (next) => {
|
|
1106
|
+
activePin = next
|
|
1107
|
+
if (pointerInside || keyboardFocus) updateExpanded()
|
|
1108
|
+
}
|
|
1109
|
+
}
|
|
1110
|
+
setHostState(host, 'ready')
|
|
1111
|
+
return true
|
|
1112
|
+
}
|
|
1113
|
+
|
|
1114
|
+
function usePinnedMeasurement(currentId, userIndex) {
|
|
1115
|
+
const overlayRef = React.useRef(null)
|
|
1116
|
+
const copyHostRef = React.useRef(null)
|
|
1117
|
+
const modelRef = React.useRef(userIndex)
|
|
1118
|
+
const refreshRef = React.useRef(null)
|
|
1119
|
+
modelRef.current = userIndex
|
|
1120
|
+
|
|
1121
|
+
React.useEffect(() => {
|
|
1122
|
+
let disposed = false
|
|
1123
|
+
let scroll = null
|
|
1124
|
+
let currentPin = null
|
|
1125
|
+
// Shared with the rendered clone: the in-place pin updater (null while nothing is
|
|
1126
|
+
// mounted) plus the pointer/keyboard expansion intent, which has to outlive the clone
|
|
1127
|
+
// rebuilds that streaming updates trigger.
|
|
1128
|
+
const cloneStateRef = { current: { update: null, hovered: false, focused: false } }
|
|
1129
|
+
let frameId = null
|
|
1130
|
+
let timeoutId = null
|
|
1131
|
+
let forceMeasure = false
|
|
1132
|
+
let resizeObserver = null
|
|
1133
|
+
let structureMutationObserver = null
|
|
1134
|
+
let sourceMutationObserver = null
|
|
1135
|
+
let styleMutationObserver = null
|
|
1136
|
+
let observedFlow = null
|
|
1137
|
+
let observedSource = null
|
|
1138
|
+
let observedSourceRoot = null
|
|
1139
|
+
const viewport = viewportOf()
|
|
1140
|
+
const visualViewport = viewport?.visualViewport
|
|
1141
|
+
const fontSet = typeof document === 'undefined' ? undefined : document.fonts
|
|
1142
|
+
|
|
1143
|
+
const observeSource = (next, root) => {
|
|
1144
|
+
if (next === observedSource && root === observedSourceRoot) return
|
|
1145
|
+
if (observedSource !== null) resizeObserver?.unobserve(observedSource)
|
|
1146
|
+
sourceMutationObserver?.disconnect()
|
|
1147
|
+
observedSource = next
|
|
1148
|
+
observedSourceRoot = root
|
|
1149
|
+
if (observedSource === null) return
|
|
1150
|
+
resizeObserver?.observe(observedSource)
|
|
1151
|
+
const attributeFilter = ['class', 'style', 'dir', 'hidden', 'data-theme', 'data-ds-dark-theme']
|
|
1152
|
+
sourceMutationObserver?.observe(observedSource, {
|
|
1153
|
+
attributes: true,
|
|
1154
|
+
attributeFilter,
|
|
1155
|
+
characterData: true,
|
|
1156
|
+
childList: true,
|
|
1157
|
+
subtree: true
|
|
1158
|
+
})
|
|
1159
|
+
let ancestor = observedSource.parentElement
|
|
1160
|
+
let visited = 0
|
|
1161
|
+
while (ancestor !== null && ancestor !== undefined && visited < MAX_SOURCE_ANCESTORS) {
|
|
1162
|
+
sourceMutationObserver?.observe(ancestor, { attributes: true, attributeFilter })
|
|
1163
|
+
if (ancestor === observedSourceRoot) break
|
|
1164
|
+
ancestor = ancestor.parentElement
|
|
1165
|
+
visited += 1
|
|
1166
|
+
}
|
|
1167
|
+
}
|
|
1168
|
+
|
|
1169
|
+
const measure = (forced) => {
|
|
1170
|
+
if (disposed) return
|
|
1171
|
+
const next = measurePinned(scroll, overlayRef.current, modelRef.current)
|
|
1172
|
+
observeSource(next?.source || null, next?.scroll || null)
|
|
1173
|
+
if (!forced && samePin(currentPin, next)) {
|
|
1174
|
+
// `samePin` intentionally ignores `push`/`expansionRoom`: yielding to the next
|
|
1175
|
+
// user card only moves the rendered clone and re-caps its expanded height, so
|
|
1176
|
+
// keep the clone (and the hover state) and update it in place.
|
|
1177
|
+
if (next !== null && next.failure === undefined) {
|
|
1178
|
+
applyCloneOffset(copyHostRef.current, next)
|
|
1179
|
+
cloneStateRef.current.update?.(next)
|
|
1180
|
+
}
|
|
1181
|
+
currentPin = next
|
|
1182
|
+
return
|
|
1183
|
+
}
|
|
1184
|
+
currentPin = next
|
|
1185
|
+
renderPinnedClone(copyHostRef.current, next, cloneStateRef)
|
|
1186
|
+
}
|
|
1187
|
+
|
|
1188
|
+
const runMeasure = () => {
|
|
1189
|
+
frameId = null
|
|
1190
|
+
timeoutId = null
|
|
1191
|
+
const forced = forceMeasure
|
|
1192
|
+
forceMeasure = false
|
|
1193
|
+
measure(forced)
|
|
1194
|
+
}
|
|
1195
|
+
|
|
1196
|
+
const schedule = (force = false) => {
|
|
1197
|
+
if (disposed) return
|
|
1198
|
+
if (force) forceMeasure = true
|
|
1199
|
+
if (frameId !== null || timeoutId !== null) return
|
|
1200
|
+
if (viewport !== undefined && typeof viewport.requestAnimationFrame === 'function') {
|
|
1201
|
+
frameId = viewport.requestAnimationFrame(runMeasure)
|
|
1202
|
+
} else {
|
|
1203
|
+
timeoutId = setTimeout(runMeasure, 0)
|
|
1204
|
+
}
|
|
1205
|
+
}
|
|
1206
|
+
const onScroll = () => schedule(false)
|
|
1207
|
+
const onResize = () => schedule(true)
|
|
1208
|
+
// A resized scrollport, flow or source bubble is re-measured through the ordinary pin
|
|
1209
|
+
// comparison, which rebuilds the clone whenever any measured geometry or style really
|
|
1210
|
+
// changed. Forcing it instead would rebuild the clone — and drop the hovered expansion —
|
|
1211
|
+
// on every token of a streaming answer, because that keeps resizing the flow.
|
|
1212
|
+
const onLayoutChange = () => schedule(false)
|
|
1213
|
+
|
|
1214
|
+
const observeScroll = (next) => {
|
|
1215
|
+
const nextFlow = visibleFlow(next)
|
|
1216
|
+
if (next === scroll && nextFlow === observedFlow) return
|
|
1217
|
+
if (scroll !== null) scroll.removeEventListener('scroll', onScroll)
|
|
1218
|
+
if (scroll !== null) resizeObserver?.unobserve(scroll)
|
|
1219
|
+
if (observedFlow !== null) resizeObserver?.unobserve(observedFlow)
|
|
1220
|
+
scroll = next
|
|
1221
|
+
observedFlow = nextFlow
|
|
1222
|
+
if (scroll !== null) scroll.addEventListener('scroll', onScroll, { passive: true })
|
|
1223
|
+
if (scroll !== null) resizeObserver?.observe(scroll)
|
|
1224
|
+
if (observedFlow !== null) resizeObserver?.observe(observedFlow)
|
|
1225
|
+
}
|
|
1226
|
+
|
|
1227
|
+
const refresh = (force = true) => {
|
|
1228
|
+
observeScroll(findConversationScroll(modelRef.current))
|
|
1229
|
+
schedule(force)
|
|
1230
|
+
}
|
|
1231
|
+
const mutationOutsideCloneHost = (records) => {
|
|
1232
|
+
const host = copyHostRef.current
|
|
1233
|
+
return Array.from(records || []).some((record) => (
|
|
1234
|
+
host === null || host === undefined || !containsNode(host, record.target)
|
|
1235
|
+
))
|
|
1236
|
+
}
|
|
1237
|
+
refreshRef.current = refresh
|
|
1238
|
+
|
|
1239
|
+
if (typeof document !== 'undefined') {
|
|
1240
|
+
if (typeof ResizeObserver !== 'undefined') resizeObserver = new ResizeObserver(onLayoutChange)
|
|
1241
|
+
if (typeof MutationObserver !== 'undefined') {
|
|
1242
|
+
structureMutationObserver = new MutationObserver((records) => {
|
|
1243
|
+
if (mutationOutsideCloneHost(records)) refresh(false)
|
|
1244
|
+
})
|
|
1245
|
+
sourceMutationObserver = new MutationObserver(() => schedule(true))
|
|
1246
|
+
styleMutationObserver = new MutationObserver(() => schedule(true))
|
|
1247
|
+
}
|
|
1248
|
+
const observationRoot = document.body || document.documentElement
|
|
1249
|
+
if (observationRoot !== undefined) {
|
|
1250
|
+
structureMutationObserver?.observe(observationRoot, { childList: true, subtree: true })
|
|
1251
|
+
}
|
|
1252
|
+
const themeTargets = [document.documentElement, document.body]
|
|
1253
|
+
.filter((target, index, values) => target !== undefined && target !== null && values.indexOf(target) === index)
|
|
1254
|
+
for (const target of themeTargets) {
|
|
1255
|
+
styleMutationObserver?.observe(target, {
|
|
1256
|
+
attributes: true,
|
|
1257
|
+
attributeFilter: ['class', 'style', 'dir', 'data-theme', 'data-ds-dark-theme']
|
|
1258
|
+
})
|
|
1259
|
+
}
|
|
1260
|
+
if (document.head !== undefined && document.head !== null) {
|
|
1261
|
+
styleMutationObserver?.observe(document.head, {
|
|
1262
|
+
attributes: true,
|
|
1263
|
+
attributeFilter: ['class', 'style', 'href', 'media', 'disabled'],
|
|
1264
|
+
characterData: true,
|
|
1265
|
+
childList: true,
|
|
1266
|
+
subtree: true
|
|
1267
|
+
})
|
|
1268
|
+
}
|
|
1269
|
+
fontSet?.addEventListener?.('loadingdone', onResize)
|
|
1270
|
+
fontSet?.ready?.then?.(() => schedule(true)).catch?.(() => {})
|
|
1271
|
+
refresh(true)
|
|
1272
|
+
}
|
|
1273
|
+
viewport?.addEventListener?.('resize', onResize)
|
|
1274
|
+
visualViewport?.addEventListener?.('resize', onResize)
|
|
1275
|
+
schedule(true)
|
|
1276
|
+
|
|
1277
|
+
return () => {
|
|
1278
|
+
disposed = true
|
|
1279
|
+
refreshRef.current = null
|
|
1280
|
+
cloneStateRef.current.update = null
|
|
1281
|
+
cloneStateRef.current.hovered = false
|
|
1282
|
+
cloneStateRef.current.focused = false
|
|
1283
|
+
if (frameId !== null && viewport !== undefined && typeof viewport.cancelAnimationFrame === 'function') viewport.cancelAnimationFrame(frameId)
|
|
1284
|
+
if (timeoutId !== null) clearTimeout(timeoutId)
|
|
1285
|
+
if (scroll !== null) scroll.removeEventListener('scroll', onScroll)
|
|
1286
|
+
structureMutationObserver?.disconnect()
|
|
1287
|
+
sourceMutationObserver?.disconnect()
|
|
1288
|
+
styleMutationObserver?.disconnect()
|
|
1289
|
+
resizeObserver?.disconnect()
|
|
1290
|
+
fontSet?.removeEventListener?.('loadingdone', onResize)
|
|
1291
|
+
viewport?.removeEventListener?.('resize', onResize)
|
|
1292
|
+
visualViewport?.removeEventListener?.('resize', onResize)
|
|
1293
|
+
clearHost(copyHostRef.current)
|
|
1294
|
+
setHostState(copyHostRef.current, null)
|
|
1295
|
+
currentPin = null
|
|
1296
|
+
observedSource = null
|
|
1297
|
+
observedSourceRoot = null
|
|
1298
|
+
}
|
|
1299
|
+
}, [currentId])
|
|
1300
|
+
|
|
1301
|
+
React.useEffect(() => {
|
|
1302
|
+
refreshRef.current?.(false)
|
|
1303
|
+
}, [currentId, userIndex])
|
|
1304
|
+
|
|
1305
|
+
return { overlayRef, copyHostRef }
|
|
1306
|
+
}
|
|
1307
|
+
|
|
1308
|
+
function createStickyUserBubbleOverlay(sessions, uiConversation) {
|
|
1309
|
+
return function StickyUserBubbleOverlay({ useSessions }) {
|
|
1310
|
+
const currentId = useSessions((state) => state?.current)
|
|
1311
|
+
let binding
|
|
1312
|
+
let session
|
|
1313
|
+
let chatSource = EMPTY_CHAT_SOURCE
|
|
1314
|
+
if (currentId !== undefined && currentId !== null) {
|
|
1315
|
+
try {
|
|
1316
|
+
binding = sessions.binding(currentId)
|
|
1317
|
+
session = binding?.session
|
|
1318
|
+
const conversation = typeof uiConversation?.binding === 'function'
|
|
1319
|
+
? uiConversation.binding(currentId)
|
|
1320
|
+
: undefined
|
|
1321
|
+
const target = conversation?.target?.('chat')
|
|
1322
|
+
if (target !== undefined
|
|
1323
|
+
&& target !== null
|
|
1324
|
+
&& typeof target.subscribe === 'function'
|
|
1325
|
+
&& typeof target.getSnapshot === 'function') {
|
|
1326
|
+
chatSource = target
|
|
1327
|
+
}
|
|
1328
|
+
} catch {
|
|
1329
|
+
binding = undefined
|
|
1330
|
+
session = undefined
|
|
1331
|
+
chatSource = EMPTY_CHAT_SOURCE
|
|
1332
|
+
}
|
|
1333
|
+
}
|
|
1334
|
+
const source = session !== undefined
|
|
1335
|
+
&& typeof session.subscribe === 'function'
|
|
1336
|
+
&& typeof session.getSnapshot === 'function'
|
|
1337
|
+
? session
|
|
1338
|
+
: EMPTY_SESSION_SOURCE
|
|
1339
|
+
const subscribe = React.useCallback((callback) => source.subscribe(callback), [source])
|
|
1340
|
+
const getSnapshot = React.useCallback(() => source.getSnapshot(), [source])
|
|
1341
|
+
const snapshot = React.useSyncExternalStore(subscribe, getSnapshot, getSnapshot)
|
|
1342
|
+
const subscribeChat = React.useCallback((callback) => chatSource.subscribe(callback), [chatSource])
|
|
1343
|
+
const getChatSnapshot = React.useCallback(() => chatSource.getSnapshot(), [chatSource])
|
|
1344
|
+
const chatSnapshot = React.useSyncExternalStore(subscribeChat, getChatSnapshot, getChatSnapshot)
|
|
1345
|
+
const activeSnapshot = snapshot !== undefined
|
|
1346
|
+
&& snapshot !== null
|
|
1347
|
+
&& snapshot.sessionId === currentId
|
|
1348
|
+
&& snapshot.removed !== true
|
|
1349
|
+
&& snapshot.openState === 'open'
|
|
1350
|
+
? snapshot
|
|
1351
|
+
: undefined
|
|
1352
|
+
const userIndex = activeSnapshot === undefined
|
|
1353
|
+
? new Map()
|
|
1354
|
+
: buildUserIndex(activeSnapshot, chatSnapshot)
|
|
1355
|
+
const measured = usePinnedMeasurement(currentId, userIndex)
|
|
1356
|
+
|
|
1357
|
+
const layerStyle = {
|
|
1358
|
+
position: 'absolute',
|
|
1359
|
+
inset: 0,
|
|
1360
|
+
width: '100%',
|
|
1361
|
+
height: '100%',
|
|
1362
|
+
pointerEvents: 'none',
|
|
1363
|
+
zIndex: 0
|
|
1364
|
+
}
|
|
1365
|
+
const hostStyle = {
|
|
1366
|
+
position: 'absolute',
|
|
1367
|
+
inset: 0,
|
|
1368
|
+
width: '100%',
|
|
1369
|
+
height: '100%',
|
|
1370
|
+
pointerEvents: 'none'
|
|
1371
|
+
}
|
|
1372
|
+
return React.createElement('div', {
|
|
1373
|
+
ref: measured.overlayRef,
|
|
1374
|
+
'data-dsh-sticky-user-bubble-layer': '',
|
|
1375
|
+
style: layerStyle
|
|
1376
|
+
}, React.createElement('div', {
|
|
1377
|
+
ref: measured.copyHostRef,
|
|
1378
|
+
'data-dsh-sticky-user-bubble-host': '',
|
|
1379
|
+
style: hostStyle
|
|
1380
|
+
}))
|
|
1381
|
+
}
|
|
1382
|
+
}
|
|
1383
|
+
|
|
1384
|
+
const inject = ['slots', 'sessions', 'uiConversation']
|
|
1385
|
+
function apply(ctx) {
|
|
1386
|
+
ctx.slots.inject('shell.overlay', () => ctx.slots.register(
|
|
1387
|
+
{ name: 'shell.overlay', id: OVERLAY_ID, order: 0 },
|
|
1388
|
+
createStickyUserBubbleOverlay(ctx.sessions, ctx.uiConversation)
|
|
1389
|
+
))
|
|
1390
|
+
}
|
|
1391
|
+
|
|
1392
|
+
exports.inject = inject
|
|
1393
|
+
exports.apply = apply
|
|
1394
|
+
return module.exports
|
|
1395
|
+
}
|
|
1396
|
+
})
|