File size: 17,067 Bytes
dac2ca2 49a03ba dac2ca2 49a03ba dac2ca2 49a03ba dac2ca2 49a03ba dac2ca2 49a03ba 1dc63a4 49a03ba 472fce5 49a03ba 472fce5 7a367e7 49a03ba a2d9f99 1dc63a4 49a03ba dac2ca2 49a03ba 1dc63a4 dac2ca2 56b51ea 49a03ba 39e321e 49a03ba 39e321e 49a03ba a2d9f99 49a03ba 1dc63a4 49a03ba dac2ca2 49a03ba dac2ca2 49a03ba dac2ca2 49a03ba dac2ca2 49a03ba dac2ca2 5f30692 dac2ca2 5f30692 dac2ca2 5f30692 49a03ba 086a293 5f30692 086a293 49a03ba 086a293 5f30692 086a293 5f30692 49a03ba 086a293 49a03ba dac2ca2 9c77549 49a03ba 9ceac12 9c77549 49a03ba dac2ca2 49a03ba 9c77549 49a03ba 3944cfe 9c77549 9ceac12 9c77549 49a03ba 9c77549 49a03ba a59751e 3944cfe 9ceac12 49a03ba dac2ca2 49a03ba dac2ca2 49a03ba 9c77549 49a03ba 5f30692 49a03ba a59751e 49a03ba dac2ca2 49a03ba 8c62083 49a03ba 9ceac12 49a03ba 6c8fd46 49a03ba 9ceac12 49a03ba 3944cfe 49a03ba dac2ca2 49a03ba dac2ca2 49a03ba 63e3e38 49a03ba 9c77549 49a03ba dac2ca2 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 | /** @param {import('../../../typings/reader-ui-component-strict').ReaderUiContext} readerContext */
export function createReaderUi(readerContext) {
/** @param {string} message @param {number} [duration] */
function showDownloadFeedback(message, duration = 4000) {
clearTimeout(readerContext.downloadFeedbackTimer);
readerContext.downloadFeedback.textContent = message;
readerContext.downloadFeedback.hidden = !message;
if (message && duration) readerContext.downloadFeedbackTimer = setTimeout(() => readerContext.showDownloadFeedback(""), duration);
}
/** @param {import('../../../typings/reader-ui-component-strict').ReaderUiDocumentPatch} patch */
function updateDocumentState(patch) {
return readerContext.readerRuntime.update("document", patch);
}
/** @param {import('../../../typings/reader-ui-component-strict').ReaderUiPanelPatch} patch */
function updatePanelState(patch) {
return readerContext.readerRuntime.update("panel", patch);
}
function applyPageInversion(persist = false) {
document.documentElement.dataset.pageInverted = String(readerContext.pageInverted);
readerContext.pageInvertToggle.textContent = readerContext.pageInverted ? "关闭页面反色" : "页面反色";
readerContext.pageInvertToggle.title = readerContext.pageInvertToggle.textContent;
readerContext.pageInvertToggle.setAttribute("aria-label", readerContext.pageInvertToggle.textContent);
readerContext.pageInvertToggle.setAttribute("aria-pressed", String(readerContext.pageInverted));
if (persist) {
try { localStorage.setItem("reader-page-inversion", String(readerContext.pageInverted)); } catch (_) {}
}
}
/** @param {string} theme @param {boolean} [persist] @param {boolean} [animate] */
function applyReaderTheme(theme, persist = true, animate = true) {
const anchor = readerContext.chapterScrollAnchors.capture();
if (animate) {
clearTimeout(readerContext.themeAnimationTimer);
document.documentElement.classList.add("theme-transition");
void document.documentElement.offsetWidth;
} else {
clearTimeout(readerContext.themeAnimationTimer);
document.documentElement.classList.remove("theme-transition");
readerContext.readerThemeToggle.classList.remove("is-changing");
}
readerContext.readerTheme = theme === "light" ? "light" : "dark";
document.documentElement.dataset.theme = readerContext.readerTheme;
document.querySelector('meta[name="theme-color"]')?.setAttribute("content", readerContext.readerTheme === "light" ? "#f4f5f6" : "#111315");
const frame = readerContext.htmlFrame;
const frameRoot = frame?.contentDocument?.documentElement;
if (frame && frameRoot?.hasAttribute("data-reader-theme")) {
frameRoot.dataset.readerTheme = readerContext.readerTheme;
readerContext.repairHtmlContrast(frame);
}
readerContext.readerThemeToggle.innerHTML = readerContext.readerTheme === "dark" ? readerContext.THEME_MOON_ICON : readerContext.THEME_SUN_ICON;
if (animate) {
readerContext.readerThemeToggle.classList.remove("is-changing");
void readerContext.readerThemeToggle.offsetWidth;
readerContext.readerThemeToggle.classList.add("is-changing");
readerContext.themeAnimationTimer = setTimeout(() => {
document.documentElement.classList.remove("theme-transition");
readerContext.readerThemeToggle.classList.remove("is-changing");
}, 280);
}
readerContext.readerThemeToggle.title = readerContext.readerTheme === "dark" ? "切换到白天模式" : "切换到夜间模式";
readerContext.readerThemeToggle.setAttribute("aria-label", readerContext.readerThemeToggle.title);
readerContext.readerThemeToggle.setAttribute("aria-pressed", String(readerContext.readerTheme === "light"));
if (persist) {
try { localStorage.setItem("theme", readerContext.readerTheme); } catch (_) {}
}
readerContext.chapterScrollAnchors.restore(anchor);
}
/** @param {number} percent @param {boolean} [persist] */
function setZoom(percent, persist = true) {
const anchor = readerContext.chapterScrollAnchors.capture();
const pdfAnchor = ["pdf", "pdf-pages", "image-pages"].includes(readerContext.capability.mode || "unsupported")
? readerContext.pageAtMarker() : null;
const pdfBounds = pdfAnchor?.getBoundingClientRect();
const pdfOffset = pdfBounds ? (readerContext.viewport.getBoundingClientRect().top - pdfBounds.top) / Math.max(1, pdfBounds.height) : 0;
const pdfAtEnd = !!pdfBounds && readerContext.viewport.scrollTop > 0 &&
readerContext.viewport.scrollHeight - readerContext.viewport.clientHeight - readerContext.viewport.scrollTop <= 2;
const normalized = VoiceOfMLReader.clampNumber(percent, 25, 400, 100);
const horizontalCenter = readerContext.viewport.scrollWidth
? (readerContext.viewport.scrollLeft + readerContext.viewport.clientWidth / 2) / readerContext.viewport.scrollWidth
: 0;
readerContext.updateDocumentState({ zoom: normalized / 100 });
readerContext.content.style.setProperty("--reader-zoom", String(readerContext.documentState.zoom));
readerContext.zoomInput.value = String(normalized);
readerContext.pdfShellWindow?.resize();
if (pdfAnchor?.isConnected) {
if (pdfAtEnd) readerContext.viewport.scrollTop = readerContext.viewport.scrollHeight - readerContext.viewport.clientHeight;
else {
const bounds = pdfAnchor.getBoundingClientRect();
readerContext.viewport.scrollTop += bounds.top - readerContext.viewport.getBoundingClientRect().top + pdfOffset * bounds.height;
}
}
if (readerContext.htmlFrame && readerContext.htmlFrame.contentDocument)
readerContext.htmlFrame.contentDocument.documentElement.style.zoom = String(readerContext.documentState.zoom);
if (readerContext.pdfDocument) readerContext.rerenderVisiblePdfPages();
readerContext.viewport.scrollLeft = horizontalCenter * readerContext.viewport.scrollWidth - readerContext.viewport.clientWidth / 2;
if (persist) {
readerContext.zoomChangedByUser = true;
try { localStorage.setItem("reader-zoom", String(normalized)); } catch (_) {}
}
readerContext.chapterScrollAnchors.restore(anchor);
if (persist) readerContext.scheduleSave();
}
/** @param {Element} list @param {string} message */
function emptyPanel(list, message) {
list.innerHTML = `<div class="panel-empty">${message}</div>`;
}
/** @param {HTMLElement} item @param {string} query */
function highlightPanelItem(item, query) {
/** @type {Text[]} */
const nodes = [];
const walker = document.createTreeWalker(item, NodeFilter.SHOW_TEXT);
while (walker.nextNode()) {
const node = /** @type {Text} */ (walker.currentNode);
if (!/** @type {HTMLElement} */ (node.parentElement).closest(".panel-item-remove")) nodes.push(node);
}
const pattern = new RegExp(query.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"), "giu");
for (const node of nodes) {
const text = node.data,
matches = [...text.matchAll(pattern)];
if (!matches.length) continue;
let start = 0;
const fragment = document.createDocumentFragment();
for (const match of matches) {
fragment.append(text.slice(start, match.index));
const mark = document.createElement("mark");
mark.className = "search-match";
mark.textContent = match[0];
fragment.append(mark);
start = match.index + match[0].length;
}
fragment.append(text.slice(start));
node.replaceWith(fragment);
}
}
/** @param {HTMLElement} view @param {{resetScroll?: boolean}} [options] */
function filterPanel(view, {resetScroll = true} = {}) {
const input = /** @type {HTMLInputElement} */ (view.querySelector(".panel-search"));
const query = input.hidden ? "" : (input.value || "").trim().toLowerCase();
let status = /** @type {HTMLElement | null} */ (view.querySelector(".panel-filter-empty"));
if (!status) {
status = document.createElement("div");
status.className = "panel-filter-empty panel-empty";
status.setAttribute("role", "status");
status.textContent = "没有匹配的内容";
status.hidden = true;
view.appendChild(status);
}
const state = view.id === "toc-panel" ? readerContext.tocVirtualState : null;
if (state) {
// Filter the full logical TOC, not just the currently mounted window.
if (query === state.query) {
status.hidden = !query || state.matches.length > 0;
return;
}
readerContext.clearSearchHighlights(view);
state.query = query;
state.matches = [];
state.entries.forEach((entry, index) => {
if (!query || entry.label.toLowerCase().includes(query)) state.matches.push(index);
});
state.list.style.height = `${state.matches.length * readerContext.TOC_VIRTUAL_ROW_HEIGHT}px`;
if (resetScroll) state.panelView.scrollTop = 0;
state.render();
status.hidden = !query || state.matches.length > 0;
return;
}
readerContext.clearSearchHighlights(view);
const items = /** @type {NodeListOf<HTMLElement>} */ (view.querySelectorAll(".panel-item"));
if (!query) {
for (const item of items) if (item.hidden) item.hidden = false;
status.hidden = true;
return;
}
let count = 0;
for (const item of items) {
const searchableText = [...item.children]
.filter((child) => !child.classList.contains("panel-item-remove"))
.map((child) => child.textContent)
.join(" ")
.toLowerCase();
item.hidden = !searchableText.includes(query);
if (!item.hidden) {
count++;
readerContext.highlightPanelItem(item, query);
}
}
status.hidden = count > 0 || !!view.querySelector(".panel-list > .panel-empty");
}
/** @param {boolean} open @param {boolean} [restoreFocus] @param {boolean} [focusPanel] */
function setReaderPanelOpen(open, restoreFocus = false, focusPanel = false) {
readerContext.readerRuntime.update("panel", { open });
const panel = /** @type {HTMLElement} */ (document.querySelector("#history-panel"));
const focusWasInside = panel.contains(document.activeElement);
clearTimeout(readerContext.panelAnimationTimer);
/** @type {HTMLElement} */ (document.querySelector("#history")).setAttribute("aria-expanded", String(open));
if (open) {
readerContext.readerPanelInteractionSerial++;
panel.hidden = false;
panel.inert = false;
void panel.offsetWidth;
panel.classList.add("is-open");
readerContext.selectPanel(readerContext.panelState.selected === "full-search" ? "full-search" : readerContext.mediaElement ? "media" : "toc");
readerContext.updateTocCurrentMark();
readerContext.readerUiHistoryState = readerContext.panelState.selected === "full-search" ? "full-search" : "panel";
// Only an explicit keyboard/assistive activation moves focus. Automatic
// directory discovery and ordinary pointer opening leave reading alone.
if (focusPanel) /** @type {HTMLElement} */ (panel.querySelector('#full-search-view:not([hidden]) input') ||
panel.querySelector('.reader-panel-tabs button[aria-selected="true"]:not([hidden])') ||
panel.querySelector('#history-close')).focus({ preventScroll: true });
return;
}
panel.classList.remove("is-open");
panel.inert = true;
readerContext.panelAnimationTimer = setTimeout(() => {
if (!panel.classList.contains("is-open")) panel.hidden = true;
}, matchMedia("(prefers-reduced-motion: reduce)").matches ? 0 : 190);
readerContext.readerUiHistoryState = "base";
if (restoreFocus || focusWasInside) /** @type {HTMLElement} */ (document.querySelector("#history")).focus({ preventScroll: true });
}
/** @param {HTMLElement} button @param {boolean} open */
function setPanelSearchOpen(button, open) {
const view = /** @type {HTMLElement} */ (button.closest(".reader-panel-view")), input = /** @type {HTMLInputElement} */ (view.querySelector(".panel-search"));
button.setAttribute("aria-expanded", String(open));
input.inert = !open;
if (open) {
input.hidden = false;
void input.offsetWidth;
input.classList.add("is-open");
input.focus();
return;
}
if (input.value) { input.value = ""; readerContext.filterPanel(view); }
if (view.id === "toc-panel") view.querySelector(".panel-filter-empty")?.setAttribute("hidden", "");
input.classList.remove("is-open");
setTimeout(() => {
if (!input.classList.contains("is-open")) input.hidden = true;
}, matchMedia("(prefers-reduced-motion: reduce)").matches ? 0 : 150);
button.focus();
}
/** @param {string} name */
function selectPanel(name) {
if (name === "media" && readerContext.mediaTab.hidden) name = "bookmarks";
if (readerContext.panelState.open && name !== readerContext.panelState.selected)
readerContext.readerPanelInteractionSerial++;
readerContext.updatePanelState({ selected: name });
if (readerContext.progressTools) readerContext.progressTools.hidden = !["toc", "media"].includes(name);
for (const button of /** @type {NodeListOf<HTMLButtonElement>} */ (document.querySelectorAll(".reader-panel-tabs button"))) {
const selected = button.dataset.panel === name;
// With no TOC this view still contains reading progress. Give the
// visible tablist a real selected tab rather than a hidden tab stop.
if (selected && name === "toc" && button.hidden) {
button.textContent = "阅读";
button.hidden = false;
}
button.setAttribute("aria-selected", String(selected));
button.tabIndex = selected ? 0 : -1;
}
for (const view of /** @type {NodeListOf<HTMLElement>} */ (document.querySelectorAll(".reader-panel-view")))
view.hidden = view.dataset.panelView !== name;
if (name === "full-search") readerContext.readerUiHistoryState = "full-search";
else if (readerContext.panelState.open) readerContext.readerUiHistoryState = "panel";
if (name === "bookmarks") readerContext.renderBookmarks();
if (name === "history") readerContext.renderHistory();
if (name === "media" && readerContext.mediaElement)
/** @type {HTMLElement} */ (readerContext.mediaPanel.querySelector(".media-panel-time")).textContent =
`${readerContext.formatMediaTime(readerContext.mediaElement.currentTime)} / ${readerContext.formatMediaTime(readerContext.mediaElement.duration)}`;
readerContext.updateProgressTools();
}
async function renderHistory() {
const list = /** @type {HTMLElement} */ (document.querySelector("#history-list")),
generation = readerContext.nextReaderGeneration("history");
try {
const entries = await VoiceOfMLReaderStore.list();
if (!readerContext.isReaderGenerationCurrent("history", generation)) return;
list.textContent = "";
for (const entry of entries) {
const row = document.createElement("div");
row.className = "panel-item";
if (entry.mediaTime != null) row.dataset.mediaTime = String(entry.mediaTime);
const link = document.createElement("button");
link.type = "button";
link.className = "panel-item-main";
link.textContent = entry.title || entry.url;
link.addEventListener("click", () => readerContext.navigateReader(/** @type {string} */ (entry.readerUrl), null, true));
const meta = document.createElement("small");
meta.textContent = `${entry.pageCount ? `第 ${entry.page || 1} / ${entry.pageCount} 页 · ` : ""}${new Date(entry.lastReadAt).toLocaleString()}`;
const remove = document.createElement("button");
remove.type = "button";
remove.className = "panel-item-remove";
remove.textContent = "删除";
remove.setAttribute("aria-label", `删除阅读记录:${entry.title || entry.url}`);
remove.addEventListener("click", async () => {
if (entry.url === readerContext.sourceUrl) {
readerContext.historySuppressed = true;
await readerContext.progressSaveChain;
}
await VoiceOfMLReaderStore.remove(entry.url);
readerContext.renderHistory();
});
row.append(link, remove, meta);
list.appendChild(row);
}
if (!list.childElementCount) readerContext.emptyPanel(list, "暂无阅读记录");
readerContext.filterPanel(/** @type {HTMLElement} */ (document.querySelector("#history-view")));
} catch (_) {
if (readerContext.isReaderGenerationCurrent("history", generation)) readerContext.emptyPanel(list, "无法读取本地记录");
}
}
return {
showDownloadFeedback,
updateDocumentState,
updatePanelState,
applyPageInversion,
applyReaderTheme,
setZoom,
emptyPanel,
highlightPanelItem,
filterPanel,
setReaderPanelOpen,
setPanelSearchOpen,
selectPanel,
renderHistory
};
}
|