File size: 23,880 Bytes
dac2ca2
49a03ba
3a37705
da2d458
dac2ca2
 
 
 
 
 
da2d458
 
dac2ca2
49a03ba
 
 
 
dac2ca2
49a03ba
 
 
 
 
 
 
ab9f351
 
 
 
49a03ba
 
 
dac2ca2
 
49a03ba
dac2ca2
 
 
 
0c9b4f9
 
 
49a03ba
 
 
 
 
 
 
 
dac2ca2
49a03ba
 
 
0c9b4f9
49a03ba
 
 
 
 
dac2ca2
49a03ba
dac2ca2
49a03ba
 
 
dac2ca2
 
 
49a03ba
 
 
 
 
 
 
 
 
0c9b4f9
 
 
 
49a03ba
 
 
 
 
4eb059b
49a03ba
 
6c8fd46
49a03ba
 
 
 
 
 
 
 
 
 
0c9b4f9
 
 
49a03ba
0c9b4f9
 
 
 
f817d67
 
6c8fd46
0c9b4f9
4eb059b
0c9b4f9
 
4eb059b
0c9b4f9
 
 
 
 
 
 
 
 
 
 
49a03ba
0c9b4f9
4eb059b
 
 
 
 
 
0c9b4f9
49a03ba
0c9b4f9
 
 
49a03ba
0c9b4f9
 
 
 
49a03ba
 
 
 
3fed1fd
 
 
 
0c9b4f9
3fed1fd
 
 
 
 
 
 
0c9b4f9
 
49a03ba
 
 
 
 
 
 
 
6c8fd46
 
0c9b4f9
 
 
49a03ba
 
dac2ca2
f2048b2
49a03ba
ae3a71a
 
8c62083
ae3a71a
49a03ba
1dc63a4
49a03ba
 
 
 
8c62083
 
 
 
49a03ba
 
 
 
 
dac2ca2
49a03ba
 
 
 
3944cfe
 
49a03ba
 
 
 
c0f9fe7
 
 
 
 
 
49a03ba
 
 
3944cfe
49a03ba
 
 
 
dac2ca2
49a03ba
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d95f083
 
 
49a03ba
 
 
 
ec782cb
5f30692
 
 
 
 
d95f083
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5f30692
d95f083
ae3a71a
 
 
d95f083
 
d3655e4
d95f083
c0f9fe7
d95f083
 
 
 
 
 
49a03ba
 
 
 
dac2ca2
 
 
473eb1a
 
 
fc8aaf1
473eb1a
 
 
 
abcbf52
 
 
 
 
fc8aaf1
 
 
 
 
abcbf52
fc8aaf1
 
 
 
 
 
 
 
 
 
 
 
 
4468c93
 
 
0144b91
fc8aaf1
 
abcbf52
dac2ca2
abcbf52
 
 
dac2ca2
 
79c1385
abcbf52
 
 
 
 
 
 
 
473eb1a
abcbf52
 
fc8aaf1
 
 
 
 
 
 
 
 
 
 
abcbf52
 
dac2ca2
49a03ba
 
 
 
d3655e4
49a03ba
da2d458
 
 
 
6c8fd46
9ceac12
49a03ba
 
dac2ca2
9ceac12
49a03ba
 
5f30692
 
49a03ba
 
 
dac2ca2
49a03ba
 
 
 
 
 
 
d3655e4
49a03ba
 
 
 
 
5f30692
 
49a03ba
 
d3655e4
49a03ba
 
 
 
 
d3655e4
49a03ba
 
d3655e4
49a03ba
5f30692
49a03ba
9425d91
 
 
 
 
abcbf52
82af23b
49a03ba
 
dac2ca2
49a03ba
 
 
 
 
 
 
 
 
cb3668d
 
3a37705
cb3668d
 
da2d458
 
dac2ca2
da2d458
 
cb3668d
 
da2d458
 
 
 
 
 
c0f9fe7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
49a03ba
6c8fd46
0c9b4f9
da2d458
49a03ba
da2d458
 
 
 
 
 
ae3a71a
da2d458
 
ae3a71a
da2d458
 
cb3668d
 
da2d458
49a03ba
6c8fd46
 
49a03ba
c0f9fe7
49a03ba
 
 
9ceac12
49a03ba
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
/** @param {import('../../../typings/reader-toc-strict').TocContext} readerContext */
export function createReaderToc(readerContext) {
  // setToc owns normalized entries. Index explicit page numbers only for those
  // entries; externally replaced arrays retain the original scan path.
  /** @type {import('../../../typings/reader-toc-strict').TocEntry[] | null} */
  let ownedTocEntries = null;
  /** @type {import('../../../typings/reader-toc-strict').TocEntry[] | null} */
  let pageLookupEntries = null;
  /** @type {{page: number, index: number}[]} */
  let pageLookup = [];
  const PAGE_LOOKUP_MIN_ENTRIES = 256;

  /** @param {Partial<{tocEntries: import('../../../typings/reader-toc-strict').TocEntry[], currentChapterIndex: number}>} patch */
  function updateNavigationState(patch) {
    return readerContext.readerRuntime.update("navigation", patch);
  }

  /** @param {ParentNode} root */
  function headingTocEntries(root) {
    return [...root.querySelectorAll("h1,h2,h3,h4,h5,h6")]
      .filter((heading) => heading.textContent.trim())
      .map((target) => ({
        label: target.textContent.trim(),
        depth: Number(target.tagName.slice(1)) - 1,
        target,
        activate: () => {
          target.closest("details.reader-clean-web-reply")?.setAttribute("open", "");
          target.scrollIntoView({ block: "start" });
        }
      }));
  }

  /** @param {HTMLElement} root
   * @param {import('../../../typings/reader-toc-strict').TocEntry[]} entries */
  function createHeadingLocationCache(root, entries) {
    /** @type {{index: number, top: number}[] | null} */
    let positions = null;
    /** @type {{entry: import('../../../typings/reader-toc-strict').TocEntry, index: number, target: Element | null | undefined, connected: boolean}[]} */
    let targets = [];
    let width = -1, extent = -1, zoom = -1, live = false;
    /** @type {boolean | undefined} */
    let wasAnimating = false;
    const invalidate = () => { positions = null; };
    const mutations = new MutationObserver(invalidate), resize = new ResizeObserver(invalidate);
    mutations.observe(root, {subtree:true, childList:true, characterData:true,
      attributes:true, attributeFilter:["style", "class", "hidden", "width", "height"]});
    resize.observe(root);
    root.addEventListener("load", invalidate, true);
    root.ownerDocument.fonts?.addEventListener("loadingdone", invalidate);
    return { root, entries,
      /** @param {number} marker @param {number} scroll */
      at(marker, scroll) {
        if (mutations.takeRecords().length) invalidate();
        const nextWidth = root.clientWidth, nextExtent = root.scrollHeight, nextZoom = readerContext.documentState.zoom;
        const animating = root.getAnimations?.({subtree:true}).some(animation => animation.playState === "running" || animation.pending);
        if (live || animating || wasAnimating || nextWidth !== width || nextExtent !== extent || nextZoom !== zoom ||
            targets.some(row => row.target !== row.entry.target || row.connected !== !!row.target?.isConnected)) invalidate();
        wasAnimating = animating;
        if (!positions) {
          targets = entries.map((entry, index) => ({entry, index, target:entry.target, connected:!!entry.target?.isConnected}));
          /** @type {WeakMap<Element, boolean>} */
          const fixed = new WeakMap();
          /** @param {Element} target */
          const scrollDependent = target => {
            const ancestors = [];
            let value = false;
            for (let node = /** @type {Element | null} */ (target); node; node = node.parentElement) {
              const cached = fixed.get(node);
              if (cached !== undefined) { value = cached; break; }
              ancestors.push(node);
              const position = root.ownerDocument.defaultView?.getComputedStyle(node).position;
              if (position === "fixed" || position === "sticky") { value = true; break; }
              if (node === root) break;
            }
            for (const node of ancestors) fixed.set(node, value);
            return value;
          };
          // Source CSS may pin headings while scrolling; keep their live geometry.
          const connectedTargets = /** @type {{target: Element, index: number}[]} */ (targets.filter(row => row.connected));
          live = connectedTargets.some(({target}) => scrollDependent(target));
          positions = connectedTargets.map(({target, index}) => ({index,
            top:target.getBoundingClientRect().top + scroll})).sort((a,b) => a.top-b.top || a.index-b.index);
          width = root.clientWidth; extent = root.scrollHeight; zoom = nextZoom;
        }
        let low = 0, high = positions.length;
        while (low < high) {
          const middle = (low + high) >> 1;
          if ((positions[middle]?.top ?? Infinity) <= scroll + marker) low = middle + 1;
          else high = middle;
        }
        return positions[low - 1]?.index ?? -1;
      },
      dispose() {
        mutations.disconnect(); resize.disconnect();
        root.removeEventListener("load", invalidate, true);
        root.ownerDocument.fonts?.removeEventListener("loadingdone", invalidate);
        positions = null; targets = [];
      }
    };
  }

  /** @type {ReadonlySet<string | null | undefined>} */
  const pageLocationModes = new Set(["pdf", "pdf-pages", "image-pages"]);

  function syncHeadingLocation() {
    const entries = readerContext.navigationState.tocEntries;
    if (!entries.length) {
      readerContext.headingLocationCache?.dispose();
      readerContext.headingLocationCache = null;
      return;
    }
    const mode = readerContext.capability.mode;
    if (pageLocationModes.has(mode)) return;
    const viewport = readerContext.viewport;
    const frame = readerContext.htmlFrame;
    const viewTop = frame ? 0 : viewport.getBoundingClientRect().top;
    const marker = viewTop + 8;
    let current;
    if (mode === "epub-chapters") {
      current = virtualHeadingIndex(marker, entries);
      if (current === undefined) return;
    } else {
      const doc = frame?.contentDocument;
      const root = doc?.body || readerContext.content;
      let cache = readerContext.headingLocationCache;
      if (!cache || cache.root !== root || cache.entries !== entries) {
        cache?.dispose();
        cache = readerContext.headingLocationCache = createHeadingLocationCache(root, entries);
      }
      const scroller = doc?.scrollingElement || viewport;
      const { scrollTop: scroll, clientHeight: height, scrollHeight } = scroller;
      const end = scrollHeight - height;
      // At the scroll limit, a short final section cannot reach the normal
      // top marker. Include headings visible through the bottom of the viewport.
      const atEnd = scroll > 0 && end > 1 && end - scroll <= 1;
      const bottom = viewTop + (scroller === viewport ? viewport.clientTop : 0) + height;
      current = cache.at(atEnd ? bottom - 1 : marker, scroll);
    }
    if (current !== readerContext.navigationState.currentChapterIndex) {
      readerContext.updateNavigationState({ currentChapterIndex: current });
      readerContext.updateTocCurrentMark();
    }
  }

  /** @param {number} marker @param {import('../../../typings/reader-toc-strict').TocEntry[]} entries */
  function virtualHeadingIndex(marker, entries) {
    let current = -1,
      currentPosition = -Infinity,
      first = -1,
      firstPosition = Infinity;
    // CHM fallbacks and fragment aliases can share a target. Reuse its top
    // only in this read pass: layout and scroll can move it before the next.
    /** @type {Map<Element, number>} */
    const targetTops = new Map();
    for (const [index, entry] of entries.entries()) {
      const target = entry.target;
      if (!target?.isConnected) continue;
      let position = targetTops.get(target);
      if (position === undefined) {
        position = target.getBoundingClientRect().top;
        targetTops.set(target, position);
      }
      // Entries are visited in index order; the later index wins a tie.
      if (position <= marker && position >= currentPosition) {
        current = index;
        currentPosition = position;
      }
      if (position > marker && position < firstPosition) {
        first = index;
        firstPosition = position;
      }
    }
    // Only the start of the directory is a preface. At a virtual window's
    // leading edge the closest mounted target can be a later chapter.
    if (current >= 0) return current;
    if (first < 0) return undefined;
    return first === entries.findIndex((entry) => !entry.group) ? -1 : first;
  }

  /** @param {number} index @param {{closePanel?: boolean}} [options] */
  async function navigateTocEntry(index, { closePanel = true } = {}) {
    if (index < 0 || index >= readerContext.navigationState.tocEntries.length) return;
    const entry = readerContext.navigationState.tocEntries[index];
    if (!entry || entry.group) return;
    const panelSerial = readerContext.readerPanelInteractionSerial;
    const generation = readerContext.beginReaderNavigation();
    try {
      const activated = await entry.activate(generation);
      if (activated === false || !readerContext.isReaderGenerationCurrent("navigation", generation)) return;
      readerContext.updateNavigationState({ currentChapterIndex: index });
      readerContext.updateTocCurrentMark();
      readerContext.updateProgressTools();
      // Landing still belongs to this navigation, but closing belongs to the
      // drawer as it was when the row was activated.
      if (closePanel && readerContext.panelState.open && panelSerial === readerContext.readerPanelInteractionSerial)
        readerContext.setReaderPanelOpen(false, true);
    } catch (error) {
      readerContext.reportNavigationError(error, generation);
    }
  }

  /** @param {import('../../../typings/reader-toc-strict').TocEntry} entry @param {number} index */
  function createTocRow(entry, index) {
    const row = document.createElement("div");
    row.className = "panel-item toc-item";
    row.dataset.tocIndex = String(index);
    const depth = entry.depth || 0;
    row.style.setProperty("--toc-depth", String(depth));
    const link = document.createElement("div");
    link.className = "panel-item-main";
    link.textContent = entry.label;
    link.title = entry.label;
    const mark = document.createElement("span");
    mark.className = "toc-current-mark";
    mark.setAttribute("aria-hidden", "true");
    mark.textContent = "✓";
    mark.hidden = true;
    row.append(link, mark);
    if (entry.group) {
      row.classList.add("toc-group");
      link.setAttribute("role", "heading");
      link.setAttribute("aria-level", String(Math.min(6, depth + 1)));
      return row;
    }
    link.tabIndex = 0;
    link.setAttribute("role", "link");
    /** @param {Event} [event] */
    const activate = (event) => {
      const selection = getSelection();
      if (event?.type === "click" && selection?.toString() && selection.containsNode(link, true)) return;
      readerContext.navigateTocEntry(index);
    };
    link.addEventListener("click", activate);
    link.addEventListener("keydown", (event) => {
      if (event.key === "Enter" || event.key === " ") {
        event.preventDefault();
        activate();
      }
    });
    return row;
  }

  // State identity follows setToc's lifecycle without retaining old lists.
  const virtualTocRows = new WeakMap();

  function renderVirtualToc() {
    const state = readerContext.tocVirtualState;
    if (!state) return;
    const height = Math.max(320, state.panelView.clientHeight),
      listTop = state.list.getBoundingClientRect().top - state.panelView.getBoundingClientRect().top + state.panelView.scrollTop,
      visibleTop = Math.max(0, state.panelView.scrollTop - listTop),
      visibleHeight = Math.max(0, height - Math.max(0, listTop - state.panelView.scrollTop)),
      indices = state.matches,
      first = Math.floor(visibleTop / readerContext.TOC_VIRTUAL_ROW_HEIGHT),
      start = Math.max(0, first - 12),
      end = Math.min(indices.length, first + Math.ceil(visibleHeight / readerContext.TOC_VIRTUAL_ROW_HEIGHT) + 13);
    let cache = virtualTocRows.get(state);
    if (!cache || cache.query !== state.query || cache.entries !== state.entries) {
      cache = { query: state.query, entries: state.entries, rows: new Map() };
      virtualTocRows.set(state, cache);
      state.list.replaceChildren();
    }
    const wanted = new Set(indices.slice(start, end));
    for (const [index, row] of cache.rows) {
      if (wanted.has(index)) continue;
      row.remove();
      cache.rows.delete(index);
    }
    // Insert around retained rows: detaching a focused row would lose focus.
    let next = state.list.firstElementChild;
    for (let position = start; position < end; position++) {
      const index = indices[position];
      if (index === undefined) continue;
      const entry = state.entries[index];
      if (!entry) continue;
      let row = cache.rows.get(index);
      if (!row) {
        row = createTocRow(entry, index);
        row.classList.add("toc-virtual-row");
        if (state.query) readerContext.highlightPanelItem(/** @type {HTMLElement} */ (row.firstElementChild), state.query);
        cache.rows.set(index, row);
      }
      const top = `${position * readerContext.TOC_VIRTUAL_ROW_HEIGHT}px`;
      if (row.style.top !== top) row.style.top = top;
      if (row !== next) state.list.insertBefore(row, next);
      next = row.nextElementSibling;
    }
    readerContext.updateTocCurrentMark();
  }

  let tocHintShown = false, tocHintPending = false;
  /** @type {ReturnType<typeof setTimeout> | 0} */
  let tocHintTimer = 0;
  // Opening the Reader must not pop the directory for a trivial one (a cover, a
  // preface, a single chapter); the panel stays one tap away in the toolbar.
  const TOC_HINT_MIN_ENTRIES = 3;
  const TOC_HINT_HISTORY_WAIT_MS = 150; // Same budget as startup's restoration grace period.
  // A one- or two-entry directory (a cover, a preface) is not worth opening the
  // panel for; the reader opens on the content and the panel stays one tap away.
  const tocHintHasEnoughEntries = () =>
    readerContext.navigationState.tocEntries.filter((entry) => !entry.group).length > TOC_HINT_MIN_ENTRIES;
  const cancelTocHint = () => {
    clearTimeout(tocHintTimer);
    tocHintTimer = 0;
    tocHintPending = false;
  };
  /** @param {boolean} unread */
  const finishTocHint = (unread) => {
    if (!tocHintPending) return;
    cancelTocHint();
    if (readerContext.readerLifecycle.disposed || readerContext.readerAbortController.signal.aborted) return;
    tocHintShown = true;
    if (!unread || !tocHintHasEnoughEntries() ||
        readerContext.panelState.open || readerContext.readerPanelInteractionSerial > 0 ||
        readerContext.readerUiHistoryState !== "base") return;
    let key = "";
    try {
      const { downloadUrl, sourceUrl, chapterManifestUrl } = readerContext;
      // Resolved links can point at converted assets. Use the original catalog
      // URL, when known, so old/current IDs and direct links share a reminder.
      const source = downloadUrl.startsWith("https://huggingface.co/datasets/VoiceOfML/")
        ? downloadUrl : sourceUrl || chapterManifestUrl;
      if (source) key = `reader-toc-hint:v1:${VoiceOfMLReader.shortSourceId(source)}`;
      if (key && localStorage.getItem(key) === "1") return;
    } catch (_) { /* Storage unavailable: keep the in-page reminder. */ }
    // Keep the directory open using the normal panel owner: toolbar, Escape,
    // system Back and aria-expanded must all agree with the visible drawer.
    readerContext.setReaderPanelOpen(true);
    readerContext.selectPanel("toc");
    // An interaction that preempted the hint above does not count as showing it.
    try { if (key) localStorage.setItem(key, "1"); } catch (_) { /* Best effort. */ }
  };
  /** @type {(() => void) | null} */
  let phaseCleanup = null;
  function ensureTocHintPhaseListener() {
    if (phaseCleanup) return;
    phaseCleanup = readerContext.readerRuntime.events.on("phase", (detail) => {
      const { phase } = /** @type {{phase: string}} */ (detail);
      if (phase === "failed" || phase === "disposed") {
        cancelTocHint();
        phaseCleanup?.();
        phaseCleanup = null;
      }
    });
    readerContext.readerRuntime.track(() => { cancelTocHint(); phaseCleanup?.(); phaseCleanup = null; });
  }
  function hintReaderToc() {
    if (tocHintShown || tocHintPending || !tocHintHasEnoughEntries()) return;
    ensureTocHintPhaseListener();
    tocHintPending = true;
    // Reuse the startup read, not restoration completion or the Reader phase.
    // If history is still unknown at the deadline, leave the drawer closed
    // without consuming the hint; never pop it later when the read settles.
    const restoration = readerContext.readerRestorationPromise;
    tocHintTimer = setTimeout(() => finishTocHint(!restoration), restoration ? TOC_HINT_HISTORY_WAIT_MS : 0);
    void restoration?.then(entry => {
      if (!tocHintPending) return;
      clearTimeout(tocHintTimer);
      const unread = !entry && !readerContext.readerHasHistory && !readerContext.restorationFailed;
      tocHintTimer = setTimeout(() => finishTocHint(unread), 0);
    }, () => finishTocHint(false));
  }

  /** @param {import('../../../typings/reader-toc-strict').TocEntry[] | null | undefined} entries */
  function setToc(entries) {
    const normalizedEntries = (entries || []).map((entry) => ({
      ...entry,
      href: String(entry.href || ""),
      label: cleanTocLabel(entry.label)
    }));
    // Invalidate before publishing; event listeners may replace the entries.
    ownedTocEntries = normalizedEntries;
    pageLookupEntries = null;
    pageLookup = [];
    readerContext.updateNavigationState({ tocEntries: normalizedEntries, currentChapterIndex: -1 });
    const tocTab = /** @type {HTMLElement} */ (document.querySelector("#toc-tab"));
    tocTab.hidden = false;
    tocTab.textContent = normalizedEntries.length ? "目录" : "阅读";
    const previousList = /** @type {HTMLElement} */ (document.querySelector("#toc-list")),
      list = /** @type {HTMLElement} */ (previousList.cloneNode(false));
    readerContext.tocVirtualState?.destroy();
    readerContext.tocVirtualState = null;
    list.classList.remove("toc-list-virtualized");
    list.style.removeProperty("height");
    previousList.replaceWith(list);
    const virtualizationThreshold = readerContext.chapterManifestUrl ? 2000 : readerContext.TOC_VIRTUALIZATION_THRESHOLD;
    if (normalizedEntries.length > virtualizationThreshold) {
      const panelView = /** @type {HTMLElement} */ (list.parentElement);
      list.classList.add("toc-list-virtualized");
      list.style.height = `${normalizedEntries.length * readerContext.TOC_VIRTUAL_ROW_HEIGHT}px`;
      let frame = 0;
      const onScroll = () => {
        if (frame) return;
        frame = requestAnimationFrame(() => {
          frame = 0;
          renderVirtualToc();
        });
      };
      panelView.addEventListener("scroll", onScroll, { passive: true });
      readerContext.tocVirtualState = {
        entries: normalizedEntries,
        matches: normalizedEntries.map((_, index) => index),
        query: "",
        list,
        panelView,
        render: renderVirtualToc,
        destroy: () => {
          panelView.removeEventListener("scroll", onScroll);
          if (frame) cancelAnimationFrame(frame);
        }
      };
      renderVirtualToc();
    } else {
      for (const [index, entry] of normalizedEntries.entries())
        list.appendChild(createTocRow(entry, index));
    }
    readerContext.filterPanel(document.querySelector('#toc-panel'), {resetScroll:false});
    readerContext.updateTocCurrentMark();
    // PDF outlines can arrive after the reader is already ready and the
    // no-TOC progress summary has been placed inside this panel.
    readerContext.updateReadingProgressSummary();
    readerContext.placeReadingProgress();
    readerContext.updateProgressTools();
    hintReaderToc();
    return normalizedEntries;
  }

  /** @param {unknown} value */
  function cleanTocLabel(value) {
    return (
      String(value || "未命名章节")
        .replace(/[\uFFFD\u2610-\u2612\u25a1\u0000-\u001F\u007F]/gu, "")
        .replace(/\s{2,}/gu, " ")
        .trim() || "未命名章节"
    );
  }

  /** @param {import('../../../typings/reader-toc-strict').TocEntry} entry */
  function tocPage(entry) {
    return entry.page ?? null;
  }

  // The legacy scan selects the *last directory entry* whose page is at or
  // before the current page, even when the outline itself is not page-sorted.
  /** @param {import('../../../typings/reader-toc-strict').TocEntry[]} entries */
  function indexTocPages(entries) {
    pageLookup = entries.flatMap((entry, index) => {
      const page = tocPage(entry);
      return page === null ? [] : [{page,index}];
    }).sort((a, b) => (a.page < b.page ? -1 : a.page > b.page ? 1 : a.index - b.index));
    let last = -1;
    for (const row of pageLookup) { last = Math.max(last, row.index); row.index = last; }
    pageLookupEntries = entries;
  }

  /** @param {Element} row @param {boolean} current */
  function markTocRow(row, current) {
    const mark = /** @type {HTMLElement} */ (row.querySelector(".toc-current-mark"));
    mark.hidden = !current;
    row.classList.toggle("is-current", current);
    const link = row.querySelector('[role="link"]');
    if (current) link?.setAttribute("aria-current", "location");
    else link?.removeAttribute("aria-current");
  }

  // Track the node, not just the index: filtering can replace it at the same index.
  /** @type {Element | null | undefined} */
  let markedRow;
  function syncMarkedTocRow() {
    const index = readerContext.navigationState.currentChapterIndex, state = readerContext.tocVirtualState;
    const row = state ? virtualTocRows.get(state)?.rows.get(index) : document.getElementById("toc-list")?.children.item(index);
    if (markedRow === row) return;
    if (markedRow?.isConnected) markTocRow(markedRow, false);
    markedRow = row;
    if (row) markTocRow(row, true);
  }

  function updateTocCurrentMark() {
    const mode = readerContext.capability.mode;
    if (readerContext.documentState.page > 0 && (readerContext.documentState.pageCount > 1 || pageLocationModes.has(mode))) {
      const entries = readerContext.navigationState.tocEntries;
      let pageIndex = -1;
      if (entries.length > PAGE_LOOKUP_MIN_ENTRIES && entries === ownedTocEntries && ownedTocEntries !== pageLookupEntries)
        indexTocPages(ownedTocEntries);
      if (entries === pageLookupEntries) {
        let low = 0, high = pageLookup.length;
        while (low < high) {
          const middle = (low + high) >> 1;
          if ((pageLookup[middle]?.page ?? Infinity) <= readerContext.documentState.page) low = middle + 1;
          else high = middle;
        }
        if (low) pageIndex = pageLookup[low - 1]?.index ?? -1;
      } else {
        for (const [index, entry] of entries.entries()) {
          const page = tocPage(entry);
          if (page !== null && page <= readerContext.documentState.page) pageIndex = index;
        }
      }
      if (pageIndex !== readerContext.navigationState.currentChapterIndex)
        readerContext.updateNavigationState({ currentChapterIndex: pageIndex });
    }
    syncMarkedTocRow();
  }

  function syncTocTab() {
    const tab = /** @type {HTMLElement | null} */ (document.querySelector("#toc-tab"));
    if (!tab) return;
    tab.hidden = !readerContext.navigationState.tocEntries.length;
  }

  return {
    updateNavigationState,
    headingTocEntries,
    syncHeadingLocation,
    navigateTocEntry,
    setToc,
    updateTocCurrentMark,
    syncTocTab
  };
}