产物级 A/B:0.24.1 的 lib/client.js 里没有这两个 500ms 表、没有 elementsFromPoint 走查;0.25.0 引入、0.26.0 未动。实测合成页面单 tick 0.1→9.7ms、最坏约 3500 次 强制布局读取/秒,且**无论是否可见都在跑**。 - right-panel-open:改全模块一条共享表(引用计数 0→1 起表并立刻读一次、1→0 停表), 隐藏时 clearInterval(不是 tick 内早退),visibilitychange 回可见先读一次再起表; 读法收敛成「先量收起态,命中即返回」,不建临时数组、不量输的那一组。 - dock-fallback:500ms 重对齐表拆进独立 effect,只在入口真的渲染时存在(此前注册在 early return 之前,渲染 null 也照跑);隐藏时不测量不写 state;让位走查 400→100 步。 - settings-nav-icon:观察者回调 rAF 合帧并去掉 characterData——它是全插件唯一 「每次 DOM 变更都触发且回调做全文档查询」的地方,流式 token 下按微任务触发。 - use-center-column:1.5s 兜底表只在「一列都没有」时 armed(周期与复核窗口等长, 注释声称的 query-free 快路径此前永不生效),观察者仍是恢复路径。 - session-phase:隐藏时不发请求,值未变不 setState(blank 会话不再每秒重渲染)。 测试:新增 right-panel-open / session-phase 两份 spec,扩展 dock-fallback / settings-nav-icon / center-column-locator;每条都用「临时回退→变红→还原」验过判别力。
69 lines
2.7 KiB
TypeScript
69 lines
2.7 KiB
TypeScript
/**
|
|
* Mark this plugin's row in the DSH settings navigation so its bundled CSS
|
|
* can replace the shell's fallback gear with the Side card glyph.
|
|
*
|
|
* DSH 0.1.x projects only `id`, `order`, and `label` from a
|
|
* `settings.section` registration, then chooses icons inside the settings
|
|
* shell from a closed list of built-in ids. Until that public contract grows
|
|
* an icon field, the plugin identifies only its own localized row after the
|
|
* dialog mounts. The marker owns no shell structure and is removed on fiber
|
|
* disposal, so the adaptation remains HMR-safe.
|
|
*
|
|
* Mutation bursts are coalesced into one sync per animation frame: the
|
|
* observer watches the whole body, so chat streaming would otherwise run the
|
|
* document-wide row query at microtask cadence.
|
|
*/
|
|
|
|
export const SETTINGS_NAV_MARKER = 'data-dsh-better-sidebar-settings-nav'
|
|
|
|
/**
|
|
* Keep the marker on the settings-nav button whose visible text is this
|
|
* plugin's current localized section label.
|
|
* @param label - locale-aware label resolver used by the section registration.
|
|
* @returns disposer that disconnects observation and removes owned markers.
|
|
*/
|
|
export function registerSettingsNavIcon(label: () => string): () => void {
|
|
let disposed = false
|
|
let frame: number | null = null
|
|
|
|
const sync = (): void => {
|
|
if (disposed) return
|
|
const currentLabel = label().trim()
|
|
const buttons = document.querySelectorAll<HTMLButtonElement>('[role="dialog"] nav button')
|
|
for (const button of buttons) {
|
|
const matches = currentLabel.length > 0 && button.textContent?.trim() === currentLabel
|
|
if (matches) button.setAttribute(SETTINGS_NAV_MARKER, '')
|
|
else button.removeAttribute(SETTINGS_NAV_MARKER)
|
|
}
|
|
}
|
|
|
|
/** One sync per frame at most: the first mutation of a burst schedules it. */
|
|
const schedule = (): void => {
|
|
if (disposed || frame !== null) return
|
|
frame = requestAnimationFrame(() => {
|
|
frame = null
|
|
sync()
|
|
})
|
|
}
|
|
|
|
sync()
|
|
// No `characterData`: the marker is a function of the mounted buttons and
|
|
// their text, and every dialog mount/unmount or row insert/remove already
|
|
// changes childList. Watching character data made this observer fire for
|
|
// every streamed token in the main conversation — a document-wide
|
|
// `querySelectorAll('[role="dialog"] nav button')` per token burst.
|
|
const observer = new MutationObserver(schedule)
|
|
observer.observe(document.body, { childList: true, subtree: true })
|
|
|
|
return () => {
|
|
disposed = true
|
|
observer.disconnect()
|
|
if (frame !== null) {
|
|
cancelAnimationFrame(frame)
|
|
frame = null
|
|
}
|
|
document.querySelectorAll(`[${SETTINGS_NAV_MARKER}]`)
|
|
.forEach(element => { element.removeAttribute(SETTINGS_NAV_MARKER) })
|
|
}
|
|
}
|