File size: 14,706 Bytes
2c1d035
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a8c921b
 
 
 
 
 
 
 
cfb1971
a8c921b
 
 
 
 
 
 
 
cfb1971
 
 
21669e9
a8c921b
 
 
 
 
122c37b
a8c921b
 
 
 
 
 
 
 
 
 
 
122c37b
 
 
a8c921b
 
2c1d035
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a8c921b
2c1d035
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9ebca63
2c1d035
 
 
 
 
9ebca63
 
 
2c1d035
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
export function createReaderToc(readerContext) {
  function updateNavigationState(patch) {
    return readerContext.readerRuntime.update("navigation", patch);
  }

  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.scrollIntoView({ block: "start" })
      }));
  }

  function createHeadingLocationCache(root, entries) {
    let positions = null, targets = [], width = -1, extent = -1, zoom = -1, wasAnimating = false, live = 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,
      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) || false;
        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}));
          const fixed = new WeakMap();
          const scrollDependent = target => {
            const ancestors = [];
            let value = false;
            for (let node = target; node; node = node.parentElement) {
              if (fixed.has(node)) { value = fixed.get(node); 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.
          live = targets.some(row => row.connected && scrollDependent(row.target));
          positions = targets.filter(row => row.connected).map(row => ({index:row.index,
            top:row.target.getBoundingClientRect().top + scroll - marker})).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 <= scroll) low = middle + 1;
          else high = middle;
        }
        return positions[low ? low - 1 : 0]?.index ?? -1;
      },
      dispose() {
        mutations.disconnect(); resize.disconnect();
        root.removeEventListener("load", invalidate, true);
        root.ownerDocument.fonts?.removeEventListener("loadingdone", invalidate);
        positions = null; targets = [];
      }
    };
  }

  function syncHeadingLocation() {
    const marker = readerContext.htmlFrame ? 8 : readerContext.viewport.getBoundingClientRect().top + 8;
    const root = readerContext.htmlFrame?.contentDocument?.body || readerContext.content;
    if (!["foliate", "epub-chapters"].includes(readerContext.capability.mode)) {
      if (readerContext.headingLocationCache?.root !== root || readerContext.headingLocationCache?.entries !== readerContext.navigationState.tocEntries) {
        readerContext.headingLocationCache?.dispose();
        readerContext.headingLocationCache = readerContext.createHeadingLocationCache(root, readerContext.navigationState.tocEntries);
      }
      const current = readerContext.headingLocationCache.at(marker, readerContext.htmlFrame ? readerContext.htmlFrame.contentWindow.scrollY : readerContext.viewport.scrollTop);
      if (current >= 0 && current !== readerContext.navigationState.currentChapterIndex) {
        readerContext.updateNavigationState({currentChapterIndex:current});
        readerContext.updateTocCurrentMark();
      }
      return;
    }
    let current = -1,
      currentPosition = -Infinity,
      first = -1,
      firstPosition = Infinity;
    for (const [index, entry] of readerContext.navigationState.tocEntries.entries()) {
      if (!entry.target?.isConnected) continue;
      const position = entry.target.getBoundingClientRect().top;
      if (position <= marker && (position > currentPosition || (position === currentPosition && index > current))) {
        current = index;
        currentPosition = position;
      }
      if (position > marker && position < firstPosition) {
        first = index;
        firstPosition = position;
      }
    }
    if (current < 0) current = first;
    if (current >= 0 && current !== readerContext.navigationState.currentChapterIndex) {
      readerContext.updateNavigationState({ currentChapterIndex: current });
      readerContext.updateTocCurrentMark();
    }
  }

  async function navigateTocEntry(index) {
    if (index < 0 || index >= readerContext.navigationState.tocEntries.length) return;
    if (readerContext.navigationState.tocEntries[index].group) return;
    const generation = readerContext.beginReaderNavigation(),
      entry = readerContext.navigationState.tocEntries[index];
    try {
      const activated = readerContext.foliateContinuous
        ? await readerContext.activateFoliateTocEntry(entry, generation)
        : await entry.activate(generation);
      if (activated === false || !readerContext.isReaderGenerationCurrent("navigation", generation)) return;
      readerContext.updateNavigationState({ currentChapterIndex: index });
      readerContext.updateTocCurrentMark();
      readerContext.updateProgressTools();
      readerContext.setReaderPanelOpen(false, true);
    } catch (error) {
      readerContext.reportNavigationError(error, generation);
    }
  }

  function createTocRow(entry, index) {
    const row = document.createElement("div");
    row.className = "panel-item toc-item";
    row.dataset.tocIndex = String(index);
    row.style.setProperty("--toc-depth", String(entry.depth || 0));
    const link = document.createElement("div");
    link.className = "panel-item-main";
    link.textContent = entry.label;
    link.title = entry.label;
    if (entry.group) {
      row.classList.add("toc-group");
      link.setAttribute("role", "heading");
      link.setAttribute("aria-level", String(Math.min(6, (entry.depth || 0) + 1)));
      row.appendChild(link);
      return row;
    }
    link.tabIndex = 0;
    link.setAttribute("role", "link");
    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();
      }
    });
    row.appendChild(link);
    return row;
  }

  function renderVirtualToc() {
    const state = readerContext.tocVirtualState;
    if (!state) return;
    const height = Math.max(320, state.panelView.clientHeight),
      start = Math.max(0, Math.floor(state.panelView.scrollTop / readerContext.TOC_VIRTUAL_ROW_HEIGHT) - 12),
      end = Math.min(state.entries.length, start + Math.ceil(height / readerContext.TOC_VIRTUAL_ROW_HEIGHT) + 24),
      fragment = document.createDocumentFragment();
    state.list.replaceChildren();
    for (let index = start; index < end; index++) {
      const row = readerContext.createTocRow(state.entries[index], index);
      row.classList.add("toc-virtual-row");
      row.style.top = `${index * readerContext.TOC_VIRTUAL_ROW_HEIGHT}px`;
      fragment.appendChild(row);
    }
    state.list.appendChild(fragment);
    readerContext.updateTocCurrentMark();
  }

  let tocHintShown = false, tocHintPending = false, tocHintTimer = 0;
  const cancelTocHint = () => {
    clearTimeout(tocHintTimer);
    tocHintTimer = 0;
    tocHintPending = false;
  };
  const finishTocHint = () => {
    tocHintTimer = 0;
    if (readerContext.readerLifecycle.disposed || readerContext.readerAbortController.signal.aborted || !tocHintPending) return;
    tocHintPending = false;
    if (!readerContext.navigationState.tocEntries.length ||
        readerContext.panelState.open || readerContext.readerPanelInteractionSerial > 0 ||
        readerContext.readerUiHistoryState !== "base") {
      tocHintShown = true;
      return;
    }
    tocHintShown = true;
    // 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");
  };
  let phaseCleanup = null;
  function ensureTocHintPhaseListener() {
    if (phaseCleanup) return;
    phaseCleanup = readerContext.readerRuntime.events.on("phase", ({ phase }) => {
      if (phase === "failed" || phase === "disposed") {
        cancelTocHint();
        phaseCleanup?.();
        phaseCleanup = null;
      }
    });
    readerContext.readerRuntime.track(() => { cancelTocHint(); phaseCleanup?.(); phaseCleanup = null; });
  }
  function hintReaderToc() {
    if (tocHintShown || tocHintPending || !readerContext.navigationState.tocEntries.length) return;
    ensureTocHintPhaseListener();
    tocHintPending = true;
    // Do not wait for the overall Reader phase: TOC availability itself is the
    // useful signal, and the hint should be visible near the opening moment.
    tocHintTimer = setTimeout(finishTocHint, 0);
  }

  function setToc(entries) {
    const normalizedEntries = (entries || []).map((entry) => ({
      ...entry,
      href: String(entry.href || ""),
      label: readerContext.cleanTocLabel(entry.label)
    }));
    readerContext.updateNavigationState({
      tocEntries: normalizedEntries,
      currentChapterIndex: normalizedEntries.findIndex((entry) => !entry.group)
    });
    const tocTab = document.querySelector("#toc-tab");
    tocTab.hidden = false;
    tocTab.textContent = normalizedEntries.length ? "目录" : "阅读";
    const previousList = document.querySelector("#toc-list"),
      list = previousList.cloneNode(false);
    readerContext.tocVirtualState?.destroy();
    readerContext.tocVirtualState = null;
    previousList.replaceWith(list);
    const virtualizationThreshold = readerContext.chapterManifestUrl ? 2000 : readerContext.TOC_VIRTUALIZATION_THRESHOLD;
    if (normalizedEntries.length > virtualizationThreshold) {
      const panelView = 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;
          readerContext.renderVirtualToc();
        });
      };
      panelView.addEventListener("scroll", onScroll, { passive: true });
      readerContext.tocVirtualState = {
        entries: normalizedEntries,
        list,
        panelView,
        render: readerContext.renderVirtualToc,
        destroy: () => {
          panelView.removeEventListener("scroll", onScroll);
          if (frame) cancelAnimationFrame(frame);
        }
      };
      readerContext.renderVirtualToc();
    } else {
      for (const [index, entry] of normalizedEntries.entries())
        list.appendChild(readerContext.createTocRow(entry, index));
    }
    readerContext.updateTocCurrentMark();
    hintReaderToc();
  }

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

  function updateTocCurrentMark() {
    const rows = [...document.querySelectorAll("#toc-list .toc-item")];
    if (readerContext.documentState.pageCount > 1 && readerContext.documentState.page > 0) {
      let pageIndex = -1;
      for (const [index, entry] of readerContext.navigationState.tocEntries.entries()) {
        const match = entry.label.match(/第\s*(\d+)\s*页/u);
        if (match && Number(match[1]) <= readerContext.documentState.page) pageIndex = index;
      }
      if (pageIndex >= 0) readerContext.updateNavigationState({ currentChapterIndex: pageIndex });
    }
    for (const row of rows) {
      const index = Number(row.dataset.tocIndex);
      let mark = row.querySelector(".toc-current-mark");
      if (!mark) {
        mark = document.createElement("span");
        mark.className = "toc-current-mark";
        mark.setAttribute("aria-hidden", "true");
        mark.textContent = "✓";
        row.appendChild(mark);
      }
      mark.hidden = index !== readerContext.navigationState.currentChapterIndex;
      row.classList.toggle("is-current", index === readerContext.navigationState.currentChapterIndex);
      const link = row.querySelector('[role="link"]');
      if (index === readerContext.navigationState.currentChapterIndex) link?.setAttribute("aria-current", "location");
      else link?.removeAttribute("aria-current");
    }
  }

  function syncTocTab() {
    const tab = document.querySelector("#toc-tab");
    if (!tab) return;
    tab.hidden = !readerContext.navigationState.tocEntries.length;
    if (readerContext.navigationState.tocEntries.length && readerContext.navigationState.currentChapterIndex < 0)
      readerContext.updateNavigationState({ currentChapterIndex: readerContext.navigationState.tocEntries.findIndex((entry) => !entry.group) });
  }

  return {
    updateNavigationState,
    headingTocEntries,
    createHeadingLocationCache,
    syncHeadingLocation,
    navigateTocEntry,
    createTocRow,
    renderVirtualToc,
    setToc,
    cleanTocLabel,
    updateTocCurrentMark,
    syncTocTab
  };
}