vomebook commited on
Commit ·
de6eb19
1
Parent(s): 8180541
Layer shell appearance and sidebar focus coordination
Browse filesFiles changed (17)
- config/types/jsconfig.check.json+3
- config/types/jsconfig.search-exact.json+3
- config/types/jsconfig.strict.json+3
- eslint.config.mjs+1-1
- scripts/build/check_reader_release.mjs+1
- scripts/build/compose_app.mjs+1
- static/app.js+32-108
- static/index.html+2
- static/reader-resources.js+1
- static/search/shell/application/controller.jsAdded+79
- static/search/shell/presentation/view.jsAdded+105
- static/sw.js+2
- tests/gates/test_static_contract.py+6-1
- tests/search/ui/shell_contracts.mjsAdded+170
- tests/search/ui/test_sidebar_directory.mjs+1
- tests/support/classic_search_modules.js+1
- typings/search-shell.d.tsAdded+44
96 unmodified lines97989910010110296 unmodified lines"../../typings/search-restoration.d.ts","../../typings/search-results.d.ts","../../typings/search-discovery.d.ts","../../typings/search-history-ambient.d.ts","../../static/search/history/infrastructure/store.js","../../static/search/history/application/controller.js",96 unmodified lines97989910010110210310410596 unmodified lines"../../typings/search-restoration.d.ts","../../typings/search-results.d.ts","../../typings/search-discovery.d.ts","../../typings/search-shell.d.ts","../../static/search/shell/presentation/view.js","../../static/search/shell/application/controller.js","../../typings/search-history-ambient.d.ts","../../static/search/history/infrastructure/store.js","../../static/search/history/application/controller.js",
19 unmodified lines20212223242519 unmodified lines"../../typings/search-restoration.d.ts","../../typings/search-results.d.ts","../../typings/search-discovery.d.ts","../../typings/search-history-ambient.d.ts","../../static/search/history/infrastructure/store.js","../../static/search/history/application/controller.js",19 unmodified lines20212223242526272819 unmodified lines"../../typings/search-restoration.d.ts","../../typings/search-results.d.ts","../../typings/search-discovery.d.ts","../../typings/search-shell.d.ts","../../static/search/shell/presentation/view.js","../../static/search/shell/application/controller.js","../../typings/search-history-ambient.d.ts","../../static/search/history/infrastructure/store.js","../../static/search/history/application/controller.js",
72 unmodified lines73747576777872 unmodified lines"../../typings/search-restoration.d.ts","../../typings/search-results.d.ts","../../typings/search-discovery.d.ts","../../typings/search-history-ambient.d.ts","../../static/search/history/infrastructure/store.js","../../static/search/history/application/controller.js",72 unmodified lines73747576777879808172 unmodified lines"../../typings/search-restoration.d.ts","../../typings/search-results.d.ts","../../typings/search-discovery.d.ts","../../typings/search-shell.d.ts","../../static/search/shell/presentation/view.js","../../static/search/shell/application/controller.js","../../typings/search-history-ambient.d.ts","../../static/search/history/infrastructure/store.js","../../static/search/history/application/controller.js",
19 unmodified lines2021222324252619 unmodified linesglobals: { ...globals.browser, ...globals.serviceworker, ...globals.worker,module: "readonly", require: "readonly",...Object.fromEntries(["VoiceOfMLReaderSecurity", "VoiceOfMLReader", "VoiceOfMLReaderResources","VoiceOfMLReaderStore", "VoiceOfMLReaderChapters", "VoiceOfMLReaderVirtual", "VoiceOfMLReaderNavigation", "VoiceOfMLReaderHost", "VoiceOfMLReaderReturn","VoiceOfMLDownloadController", "VoiceOfMLDownloadBatch", "VoiceOfMLDownloadQueue", "VoiceOfMLSearchSession", "VoiceOfMLPositionPolicy", "VoiceOfMLPositionStorage", "VoiceOfMLSearchPositions", "VoiceOfMLPositionRestoration", "VoiceOfMLResultRows", "VoiceOfMLResultInteractions", "VoiceOfMLRandomDiscovery", "VoiceOfMLSearchHistoryStore", "VoiceOfMLSearchHistory", "VoiceOfMLQuotation", "VoiceOfMLSnapshotPolicy", "VoiceOfMLResultSnapshots", "VoiceOfMLSearchWindowPage", "VoiceOfMLReaderRequests", "VoiceOfMLReaderRuntime","VoiceOfMLReaderAdapters", "VoiceOfMLReaderScroll", "VoiceOfMLSearchSidebarRender", "DOMPurify", "marked"].map(name => [name, "readonly"])) }19 unmodified lines2021222324252619 unmodified linesglobals: { ...globals.browser, ...globals.serviceworker, ...globals.worker,module: "readonly", require: "readonly",...Object.fromEntries(["VoiceOfMLReaderSecurity", "VoiceOfMLReader", "VoiceOfMLReaderResources","VoiceOfMLShell", "VoiceOfMLShellView", "VoiceOfMLReaderStore", "VoiceOfMLReaderChapters", "VoiceOfMLReaderVirtual", "VoiceOfMLReaderNavigation", "VoiceOfMLReaderHost", "VoiceOfMLReaderReturn","VoiceOfMLDownloadController", "VoiceOfMLDownloadBatch", "VoiceOfMLDownloadQueue", "VoiceOfMLSearchSession", "VoiceOfMLPositionPolicy", "VoiceOfMLPositionStorage", "VoiceOfMLSearchPositions", "VoiceOfMLPositionRestoration", "VoiceOfMLResultRows", "VoiceOfMLResultInteractions", "VoiceOfMLRandomDiscovery", "VoiceOfMLSearchHistoryStore", "VoiceOfMLSearchHistory", "VoiceOfMLQuotation", "VoiceOfMLSnapshotPolicy", "VoiceOfMLResultSnapshots", "VoiceOfMLSearchWindowPage", "VoiceOfMLReaderRequests", "VoiceOfMLReaderRuntime","VoiceOfMLReaderAdapters", "VoiceOfMLReaderScroll", "VoiceOfMLSearchSidebarRender", "DOMPurify", "marked"].map(name => [name, "readonly"])) }
281 unmodified lines282283284285286287281 unmodified lines"search/positions/domain/policy.js", "search/positions/infrastructure/indexed-db.js", "search/positions/application/persistence.js", "search/positions/application/restoration.js","search/results/presentation/row.js", "search/results/application/interactions.js","search/discovery/application/random.js","search/history/infrastructure/store.js","search/history/application/controller.js","search/ambient/application/quotation.js",281 unmodified lines282283284285286287288281 unmodified lines"search/positions/domain/policy.js", "search/positions/infrastructure/indexed-db.js", "search/positions/application/persistence.js", "search/positions/application/restoration.js","search/results/presentation/row.js", "search/results/application/interactions.js","search/discovery/application/random.js","search/shell/presentation/view.js", "search/shell/application/controller.js","search/history/infrastructure/store.js","search/history/application/controller.js","search/ambient/application/quotation.js",
9 unmodified lines1011121314159 unmodified lines"search/positions/domain/policy.js", "search/positions/infrastructure/indexed-db.js", "search/positions/application/persistence.js", "search/positions/application/restoration.js","search/results/presentation/row.js", "search/results/application/interactions.js","search/discovery/application/random.js","search/history/infrastructure/store.js","search/history/application/controller.js","search/ambient/application/quotation.js",9 unmodified lines101112131415169 unmodified lines"search/positions/domain/policy.js", "search/positions/infrastructure/indexed-db.js", "search/positions/application/persistence.js", "search/positions/application/restoration.js","search/results/presentation/row.js", "search/results/application/interactions.js","search/discovery/application/random.js","search/shell/presentation/view.js", "search/shell/application/controller.js","search/history/infrastructure/store.js","search/history/application/controller.js","search/ambient/application/quotation.js",
2067 unmodified lines2068206920702071207220732074207520762077207820792080208120822083208420851597 unmodified lines368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537862067 unmodified linesnavigateSidebarParent();}function closeLeftSidebar() {STATE.leftSidebarOpen = false;setSidebarExpanded(false);updateSidebarVisibility();syncStateToURL(true);}function closeRightSidebar() {STATE.rightSidebarOpen = false;updateSidebarVisibility();syncStateToURL(true);}const ROUTER = {parse() {const path = window.location.pathname.replace(/\/+$/, "");1597 unmodified linesdocument.addEventListener("visibilitychange", () => { if (document.hidden) cancelQuickScroll(); });}function toggleTheme() {const btn = DOM.themeBtn;const rect = btn.getBoundingClientRect();const ripple = document.createElement("div");ripple.className = "theme-ripple";ripple.style.left = (rect.left + rect.width / 2) + "px";ripple.style.top = (rect.top + rect.height / 2) + "px";ripple.style.background = STATE.isDark ? "#fff" : "#1a1c1e";document.body.appendChild(ripple);document.body.classList.add("theme-transitioning");STATE.isDark = !STATE.isDark;applyTheme();writeSearchPreference("theme", STATE.isDark ? "dark" : "light");ripple.addEventListener("animationend", () => {ripple.remove();document.body.classList.remove("theme-transitioning");});}function applyTheme() {DOM.themeBtn.setAttribute('aria-label', STATE.isDark ? '切换到浅色主题' : '切换到深色主题');if (STATE.isDark) { document.body.classList.remove("light"); DOM.themeIconLight.style.display = "none"; DOM.themeIconDark.style.display = ""; }else { document.body.classList.add("light"); DOM.themeIconLight.style.display = ""; DOM.themeIconDark.style.display = "none"; }if (readerHost.overlay && readerHost.overlay.contentWindow) readerHost.overlay.contentWindow.postMessage({ type: "voice-reader-theme-state", theme: STATE.isDark ? "dark" : "light" }, location.origin);}function toggleMobile() { STATE.isMobile = !STATE.isMobile; applyMobileMode(); writeSearchPreference("mobileMode", STATE.isMobile ? "mobile" : "desktop"); }function applyMobileMode() {DOM.mobileToggleBtn.setAttribute('aria-label', STATE.isMobile ? '切换到电脑版' : '切换到手机版');if (STATE.isMobile) {document.body.classList.add("mobile"); document.body.classList.remove("force-desktop");DOM.mobileIconPhone.style.display = ""; DOM.mobileIconDesktop.style.display = "none";STATE.leftSidebarOpen = false; STATE.rightSidebarOpen = false;} else {document.body.classList.remove("mobile"); document.body.classList.add("force-desktop");DOM.mobileIconPhone.style.display = "none"; DOM.mobileIconDesktop.style.display = "";STATE.leftSidebarOpen = true; STATE.rightSidebarOpen = false;}updateSidebarVisibility();document.documentElement.classList.remove("mobile-boot");updateSidebarHeader();updateSelectionUI();requestAnimationFrame(updateScrollTrack);}function autoDetectMobile() { return window.innerWidth <= 768; }function focusSearchSidebar(sidebar) {const control = Array.from(sidebar.querySelectorAll('button,a[href],input,select,[tabindex="0"]')).find(node => !node.disabled && node.getClientRects().length && !node.closest('[hidden],[inert]'));(control || sidebar).focus({ preventScroll: true });}function toggleLeftSidebar(event) {if (STATE.leftSidebarOpen) {closeLeftSidebar();return;}STATE.leftSidebarOpen = true;if (STATE.isMobile && STATE.leftSidebarOpen && STATE.rightSidebarOpen) STATE.rightSidebarOpen = false;updateSidebarVisibility();updateSidebarHeader();syncStateToURL(true);if (event?.detail === 0 || event?.type === 'keydown') focusSearchSidebar(DOM.leftSidebar);}function toggleRightSidebar(event) {if (STATE.rightSidebarOpen) return closeRightSidebar();STATE.rightSidebarOpen = true;if (STATE.rightSidebarOpen && STATE.leftSidebarOpen && STATE.isMobile) STATE.leftSidebarOpen = false;updateSidebarVisibility();syncStateToURL(true);if (event?.detail === 0 || event?.type === 'keydown') focusSearchSidebar(DOM.rightSidebar);}function updateSidebarVisibility() {const main = /** @type {HTMLElement} */ (document.querySelector('#main-content'));const covered = STATE.isMobile && (STATE.leftSidebarOpen || STATE.rightSidebarOpen);const focusCovered = covered && main.contains(document.activeElement);main.inert = covered;for (const [sidebar, button, open] of [[DOM.leftSidebar, DOM.hamburgerBtn, STATE.leftSidebarOpen],[DOM.rightSidebar, DOM.settingsBtn, STATE.rightSidebarOpen]]) {const restoreFocus = !open && sidebar.contains(document.activeElement);sidebar.inert = !open;button.setAttribute('aria-expanded', String(open));button.setAttribute('aria-label', sidebar === DOM.leftSidebar ? (open ? '关闭仓库导航' : '打开仓库导航') :(open ? '关闭搜索过滤' : '打开搜索过滤'));if (restoreFocus) button.focus({ preventScroll: true });}DOM.leftSidebar.classList.toggle("collapsed", !STATE.leftSidebarOpen);DOM.leftSidebar.classList.toggle("open", STATE.leftSidebarOpen);DOM.rightSidebar.classList.toggle("collapsed", !STATE.rightSidebarOpen);DOM.rightSidebar.classList.toggle("open", STATE.rightSidebarOpen);DOM.overlay.style.display = "";DOM.overlay.classList.toggle("open", STATE.isMobile && (STATE.leftSidebarOpen || STATE.rightSidebarOpen));if (focusCovered) focusSearchSidebar(STATE.rightSidebarOpen ? DOM.rightSidebar : DOM.leftSidebar);}let keyboardResultIndex = -1;function focusKeyboardResult(index) {2067 unmodified lines206820692070207120722073207420751597 unmodified lines367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937102067 unmodified linesnavigateSidebarParent();}function closeLeftSidebar() { getShell().closeLeftSidebar(); }function closeRightSidebar() { getShell().closeRightSidebar(); }const ROUTER = {parse() {const path = window.location.pathname.replace(/\/+$/, "");1597 unmodified linesdocument.addEventListener("visibilitychange", () => { if (document.hidden) cancelQuickScroll(); });}let shell = null;function getShell() {return shell || (shell = VoiceOfMLShell.create({readState: () => ({ isDark: STATE.isDark, isMobile: STATE.isMobile,leftSidebarOpen: STATE.leftSidebarOpen, rightSidebarOpen: STATE.rightSidebarOpen }),setDark: value => { STATE.isDark = value; },setMobile: value => { STATE.isMobile = value; },setSidebars: (left, right) => { STATE.leftSidebarOpen = left; STATE.rightSidebarOpen = right; },writePreference: writeSearchPreference,notifyReaderTheme: isDark => {if (readerHost.overlay && readerHost.overlay.contentWindow) readerHost.overlay.contentWindow.postMessage({ type: "voice-reader-theme-state", theme: isDark ? "dark" : "light" }, location.origin);},updateSidebarHeader, updateSelectionUI,scheduleScrollTrack: () => requestAnimationFrame(updateScrollTrack),syncURL: () => syncStateToURL(true),collapseLeftSidebar: () => setSidebarExpanded(false),view: VoiceOfMLShellView.create(() => ({ themeBtn: DOM.themeBtn, themeIconLight: DOM.themeIconLight,themeIconDark: DOM.themeIconDark, mobileToggleBtn: DOM.mobileToggleBtn,mobileIconPhone: DOM.mobileIconPhone, mobileIconDesktop: DOM.mobileIconDesktop,leftSidebar: DOM.leftSidebar, rightSidebar: DOM.rightSidebar, hamburgerBtn: DOM.hamburgerBtn,settingsBtn: DOM.settingsBtn, overlay: DOM.overlay }), false)}));}function toggleTheme() { getShell().toggleTheme(); }function applyTheme() { getShell().applyTheme(); }function toggleMobile() { getShell().toggleMobile(); }function applyMobileMode() { getShell().applyMobileMode(); }function autoDetectMobile() { return window.innerWidth <= 768; }function focusSearchSidebar(sidebar) { VoiceOfMLShellView.focusSearchSidebar(sidebar); }function toggleLeftSidebar(event) { getShell().toggleLeftSidebar(event); }function toggleRightSidebar(event) { getShell().toggleRightSidebar(event); }function updateSidebarVisibility() { getShell().updateSidebarVisibility(); }let keyboardResultIndex = -1;function focusKeyboardResult(index) {
346 unmodified lines347348349350351352346 unmodified lines<script src="/static/search/results/presentation/row.js"></script><script src="/static/search/results/application/interactions.js"></script><script src="/static/search/discovery/application/random.js"></script><script src="/static/search/history/infrastructure/store.js"></script><script src="/static/search/history/application/controller.js"></script><script src="/static/search/ambient/application/quotation.js"></script>346 unmodified lines347348349350351352353354346 unmodified lines<script src="/static/search/results/presentation/row.js"></script><script src="/static/search/results/application/interactions.js"></script><script src="/static/search/discovery/application/random.js"></script><script src="/static/search/shell/presentation/view.js"></script><script src="/static/search/shell/application/controller.js"></script><script src="/static/search/history/infrastructure/store.js"></script><script src="/static/search/history/application/controller.js"></script><script src="/static/search/ambient/application/quotation.js"></script>
130 unmodified lines131132133134135136130 unmodified lines"search/positions/domain/policy.js", "search/positions/infrastructure/indexed-db.js", "search/positions/application/persistence.js", "search/positions/application/restoration.js","search/results/presentation/row.js", "search/results/application/interactions.js","search/discovery/application/random.js","search/history/infrastructure/store.js","search/history/application/controller.js","search/ambient/application/quotation.js",130 unmodified lines131132133134135136137130 unmodified lines"search/positions/domain/policy.js", "search/positions/infrastructure/indexed-db.js", "search/positions/application/persistence.js", "search/positions/application/restoration.js","search/results/presentation/row.js", "search/results/application/interactions.js","search/discovery/application/random.js","search/shell/presentation/view.js", "search/shell/application/controller.js","search/history/infrastructure/store.js","search/history/application/controller.js","search/ambient/application/quotation.js",
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879// Shell transitions use a bounded state snapshot; the app owns preferences and routing.(function () { /** @param {VoiceOfMLShellDependencies} deps */ function create(deps) { const view = deps.view;
function applyTheme() { const { isDark } = deps.readState(); view.applyTheme(isDark); deps.notifyReaderTheme(isDark); }
function toggleTheme() { const ripple = view.startThemeTransition(deps.readState().isDark); deps.setDark(!deps.readState().isDark); applyTheme(); deps.writePreference("theme", deps.readState().isDark ? "dark" : "light"); view.finishThemeTransition(ripple); }
function updateSidebarVisibility() { view.updateSidebarVisibility(deps.readState()); }
function applyMobileMode() { const { isMobile } = deps.readState(); view.applyMobileMode(isMobile); deps.setSidebars(!isMobile, false); updateSidebarVisibility(); view.finishMobileMode(); deps.updateSidebarHeader(); deps.updateSelectionUI(); deps.scheduleScrollTrack(); }
function toggleMobile() { deps.setMobile(!deps.readState().isMobile); applyMobileMode(); deps.writePreference("mobileMode", deps.readState().isMobile ? "mobile" : "desktop"); }
function closeLeftSidebar() { deps.setSidebars(false, deps.readState().rightSidebarOpen); if (deps.collapseLeftSidebar) deps.collapseLeftSidebar(); else view.collapseLeftSidebar(); updateSidebarVisibility(); deps.syncURL(); }
function closeRightSidebar() { deps.setSidebars(deps.readState().leftSidebarOpen, false); updateSidebarVisibility(); deps.syncURL(); }
/** @param {VoiceOfMLShellEvent} [event] */ function toggleLeftSidebar(event) { const state = deps.readState(); if (state.leftSidebarOpen) return closeLeftSidebar(); deps.setSidebars(true, state.isMobile ? false : state.rightSidebarOpen); updateSidebarVisibility(); deps.updateSidebarHeader(); deps.syncURL(); if (event?.detail === 0 || event?.type === 'keydown') view.focusSidebar('left'); }
/** @param {VoiceOfMLShellEvent} [event] */ function toggleRightSidebar(event) { const state = deps.readState(); if (state.rightSidebarOpen) return closeRightSidebar(); deps.setSidebars(state.isMobile ? false : state.leftSidebarOpen, true); updateSidebarVisibility(); deps.syncURL(); if (event?.detail === 0 || event?.type === 'keydown') view.focusSidebar('right'); }
return { toggleTheme, applyTheme, toggleMobile, applyMobileMode, toggleLeftSidebar, toggleRightSidebar, closeLeftSidebar, closeRightSidebar, updateSidebarVisibility }; } globalThis.VoiceOfMLShell = { create };})();123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105// Theme, mobile chrome and sidebar accessibility share one bounded element set.(function () { /** @param {HTMLElement} sidebar */ function focusSearchSidebar(sidebar) { const control = Array.from(sidebar.querySelectorAll('button,a[href],input,select,[tabindex="0"]')) .find(node => !(/** @type {HTMLButtonElement} */ (node)).disabled && node.getClientRects().length && !node.closest('[hidden],[inert]')); (/** @type {HTMLElement} */ (control) || sidebar).focus({ preventScroll: true }); }
/** @param {() => VoiceOfMLShellElements} elements @param {boolean} zeroRippleMargins */ function create(elements, zeroRippleMargins) { /** @param {boolean} isDark */ function startThemeTransition(isDark) { const rect = elements().themeBtn.getBoundingClientRect(); const ripple = document.createElement("div"); ripple.className = "theme-ripple"; ripple.style.left = (rect.left + rect.width / 2) + "px"; ripple.style.top = (rect.top + rect.height / 2) + "px"; ripple.style.background = isDark ? "#fff" : "#1a1c1e"; if (zeroRippleMargins) { ripple.style.marginLeft = "-0px"; ripple.style.marginTop = "-0px"; } document.body.appendChild(ripple); document.body.classList.add("theme-transitioning"); return ripple; }
/** @param {HTMLElement} ripple */ function finishThemeTransition(ripple) { ripple.addEventListener("animationend", () => { ripple.remove(); document.body.classList.remove("theme-transitioning"); }); }
/** @param {boolean} isDark */ function applyTheme(isDark) { const el = elements(); el.themeBtn.setAttribute('aria-label', isDark ? '切换到浅色主题' : '切换到深色主题'); if (isDark) { document.body.classList.remove("light"); el.themeIconLight.style.display = "none"; el.themeIconDark.style.display = ""; } else { document.body.classList.add("light"); el.themeIconLight.style.display = ""; el.themeIconDark.style.display = "none"; } }
/** @param {boolean} isMobile */ function applyMobileMode(isMobile) { const el = elements(); el.mobileToggleBtn.setAttribute('aria-label', isMobile ? '切换到电脑版' : '切换到手机版'); if (isMobile) { document.body.classList.add("mobile"); document.body.classList.remove("force-desktop"); el.mobileIconPhone.style.display = ""; el.mobileIconDesktop.style.display = "none"; } else { document.body.classList.remove("mobile"); document.body.classList.add("force-desktop"); el.mobileIconPhone.style.display = "none"; el.mobileIconDesktop.style.display = ""; } }
function finishMobileMode() { document.documentElement.classList.remove("mobile-boot"); } function collapseLeftSidebar() { elements().leftSidebar.classList.remove("expanded-wide"); } /** @param {'left' | 'right'} side */ function focusSidebar(side) { focusSearchSidebar(side === 'left' ? elements().leftSidebar : elements().rightSidebar); }
/** @param {VoiceOfMLShellState} state */ function updateSidebarVisibility(state) { const el = elements(); const main = /** @type {HTMLElement} */ (document.querySelector('#main-content')); const covered = state.isMobile && (state.leftSidebarOpen || state.rightSidebarOpen); const focusCovered = covered && main.contains(document.activeElement); main.inert = covered; /** @type {Array<[HTMLElement, HTMLElement, boolean]>} */ const sidebars = [[el.leftSidebar, el.hamburgerBtn, state.leftSidebarOpen], [el.rightSidebar, el.settingsBtn, state.rightSidebarOpen]]; for (const [sidebar, button, open] of sidebars) { const restoreFocus = !open && sidebar.contains(document.activeElement); sidebar.inert = !open; button.setAttribute('aria-expanded', String(open)); button.setAttribute('aria-label', sidebar === el.leftSidebar ? (open ? '关闭仓库导航' : '打开仓库导航') : (open ? '关闭搜索过滤' : '打开搜索过滤')); if (restoreFocus) button.focus({ preventScroll: true }); } el.leftSidebar.classList.toggle("collapsed", !state.leftSidebarOpen); el.leftSidebar.classList.toggle("open", state.leftSidebarOpen); el.rightSidebar.classList.toggle("collapsed", !state.rightSidebarOpen); el.rightSidebar.classList.toggle("open", state.rightSidebarOpen); el.overlay.style.display = ""; el.overlay.classList.toggle("open", state.isMobile && (state.leftSidebarOpen || state.rightSidebarOpen)); if (focusCovered) focusSearchSidebar(state.rightSidebarOpen ? el.rightSidebar : el.leftSidebar); }
return { startThemeTransition, finishThemeTransition, applyTheme, applyMobileMode, finishMobileMode, collapseLeftSidebar, focusSidebar, updateSidebarVisibility }; } globalThis.VoiceOfMLShellView = { create, focusSearchSidebar };})();102 unmodified lines103104105106107108102 unmodified lines"/static/search/results/presentation/row.js","/static/search/results/application/interactions.js","/static/search/discovery/application/random.js","/static/search/history/infrastructure/store.js","/static/search/history/application/controller.js","/static/search/ambient/application/quotation.js",102 unmodified lines103104105106107108109110102 unmodified lines"/static/search/results/presentation/row.js","/static/search/results/application/interactions.js","/static/search/discovery/application/random.js","/static/search/shell/presentation/view.js","/static/search/shell/application/controller.js","/static/search/history/infrastructure/store.js","/static/search/history/application/controller.js","/static/search/ambient/application/quotation.js",
187 unmodified lines188189190191192193475 unmodified lines669670671672673674675187 unmodified linescls.result_rows = (ROOT / "static/search/results/presentation/row.js").read_text(encoding="utf-8")cls.result_interactions = (ROOT / "static/search/results/application/interactions.js").read_text(encoding="utf-8")cls.random_discovery = (ROOT / "static/search/discovery/application/random.js").read_text(encoding="utf-8")cls.css = (ROOT / "static/style.css").read_text(encoding="utf-8")cls.sw = (ROOT / "static/sw.js").read_text(encoding="utf-8")475 unmodified linesdef test_mobile_shell_hides_sidebars_before_application_startup(self):self.assertIn('document.documentElement.classList.add("mobile-boot")', self.html)self.assertIn('html.mobile-boot .left-sidebar', self.css)self.assertIn('document.documentElement.classList.remove("mobile-boot")', self.js)def test_js_has_warm_connection_keepalive(self):for value in ('function warmConnection', '/api/ping', 'keepalivePending', 'KEEPALIVE_INTERVAL_MS', 'KEEPALIVE_MIN_GAP_MS', 'document.hidden || !navigator.onLine', 'warmConnection(true)'):187 unmodified lines188189190191192193194475 unmodified lines670671672673674675676677678679680187 unmodified linescls.result_rows = (ROOT / "static/search/results/presentation/row.js").read_text(encoding="utf-8")cls.result_interactions = (ROOT / "static/search/results/application/interactions.js").read_text(encoding="utf-8")cls.random_discovery = (ROOT / "static/search/discovery/application/random.js").read_text(encoding="utf-8")cls.shell_view = (ROOT / "static/search/shell/presentation/view.js").read_text(encoding="utf-8")cls.css = (ROOT / "static/style.css").read_text(encoding="utf-8")cls.sw = (ROOT / "static/sw.js").read_text(encoding="utf-8")475 unmodified linesdef test_mobile_shell_hides_sidebars_before_application_startup(self):self.assertIn('document.documentElement.classList.add("mobile-boot")', self.html)self.assertIn('html.mobile-boot .left-sidebar', self.css)self.assertIn('document.documentElement.classList.remove("mobile-boot")', self.shell_view)self.assertIn('function applyMobileMode() { getShell().applyMobileMode(); }', self.js)self.assertIn('view: VoiceOfMLShellView.create(', self.js)shell_controller = (ROOT / "static/search/shell/application/controller.js").read_text(encoding="utf-8")self.assertRegex(shell_controller, r'updateSidebarVisibility\(\);\s*view\.finishMobileMode\(\);\s*deps\.updateSidebarHeader\(\);\s*deps\.updateSelectionUI\(\);\s*deps\.scheduleScrollTrack\(\);')def test_js_has_warm_connection_keepalive(self):for value in ('function warmConnection', '/api/ping', 'keepalivePending', 'KEEPALIVE_INTERVAL_MS', 'KEEPALIVE_MIN_GAP_MS', 'document.hidden || !navigator.onLine', 'warmConnection(true)'):
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170import assert from 'node:assert/strict';import fs from 'node:fs';import vm from 'node:vm';import test from 'node:test';import {createRequire} from 'node:module';import {APP_DEPENDENCIES, stripBundledScripts} from '../../../scripts/build/compose_app.mjs';
const names = ['search/shell/presentation/view.js', 'search/shell/application/controller.js'];const app = fs.readFileSync('static/app.js', 'utf8');const space = fs.existsSync('pyrightconfig.json');const functions = ['getShell', 'toggleTheme', 'applyTheme', 'toggleMobile', 'applyMobileMode', 'focusSearchSidebar', 'toggleLeftSidebar', 'toggleRightSidebar', 'closeLeftSidebar', 'closeRightSidebar', 'updateSidebarVisibility'];
function fixture() { const events = [], nodes = [], frames = [], messages = []; const document = {activeElement: null}; function element(name, parent = null) { const classes = new Set(), attributes = {}, listeners = {}; const node = {name, parent, classes, attributes, listeners, disabled: false, hidden: false, inert: false, style: {}, controls: [], classList: {add: value => classes.add(value), remove: value => classes.delete(value), toggle(value, on) { if (on) classes.add(value); else classes.delete(value); }}, setAttribute(key, value) { attributes[key] = value; events.push([name, key, value]); }, contains(other) { return !!other && (other === node || node.contains(other.parent)); }, focus(options) { assert.equal(options.preventScroll, true); document.activeElement = node; events.push(['focus', name]); }, querySelectorAll() { return node.controls; }, getClientRects() { return node.hidden ? [] : [{}]; }, closest() { for (let el = node; el; el = el.parent) if (el.hidden || el.inert) return el; return null; }, getBoundingClientRect() { return {left: 10, top: 20, width: 40, height: 60}; }, appendChild(child) { child.parent = node; nodes.push(child); events.push(['append', child.className]); }, addEventListener(type, fn) { listeners[type] = fn; events.push(['listen', type]); }, remove() { node.removed = true; } }; return node; } const DOM = Object.fromEntries(['themeBtn', 'themeIconLight', 'themeIconDark', 'mobileToggleBtn', 'mobileIconPhone', 'mobileIconDesktop', 'leftSidebar', 'rightSidebar', 'hamburgerBtn', 'settingsBtn', 'overlay'] .map(name => [name, element(name)])); const main = element('main'), mainControl = element('mainControl', main); const leftControl = element('leftControl', DOM.leftSidebar), rightControl = element('rightControl', DOM.rightSidebar); DOM.leftSidebar.controls = [leftControl]; DOM.rightSidebar.controls = [rightControl]; document.body = element('body'); document.documentElement = element('html'); document.querySelector = selector => { assert.equal(selector, '#main-content'); return main; }; document.createElement = () => element('ripple'); const c = vm.createContext({shell: null, DOM, document, STATE: {isDark: true, isMobile: false, leftSidebarOpen: true, rightSidebarOpen: false}, readerHost: {overlay: {contentWindow: {postMessage(message, origin) { messages.push(JSON.parse(JSON.stringify([message, origin]))); events.push(['reader', message.theme]); }}}}, location: {origin: 'https://shell.test'}, writeSearchPreference: (...args) => events.push(['preference', ...args]), syncStateToURL: (...args) => events.push(['url', ...args]), updateSidebarHeader: () => events.push(['header']), updateSelectionUI: () => events.push(['selection']), updateScrollTrack: () => events.push(['scroll']), requestAnimationFrame: fn => { frames.push(fn); events.push(['frame']); }, setSidebarExpanded: value => { DOM.leftSidebar.classList.toggle('expanded-wide', value); events.push(['header']); } }); for (const name of names) vm.runInContext(fs.readFileSync('static/' + name, 'utf8'), c); for (const name of functions) { const match = app.match(new RegExp('^function ' + name + '\\([^]*?^}', 'm')); // Single-line forwarding functions must not consume the following function. const single = app.match(new RegExp('^function ' + name + '\\([^\\n]*\\}\\s*$', 'm')); assert.ok(single || match, name); vm.runInContext((single || match)[0], c); } return {c, events, nodes, frames, messages, document, DOM, main, mainControl, leftControl, rightControl, element};}
test('shell modules are registered once in source, bundle, offline, resources and strict projects', () => { const html = fs.readFileSync('static/index.html', 'utf8'); const resources = createRequire(import.meta.url)('../../../static/reader-resources.js'); for (const name of names) { assert.equal(APP_DEPENDENCIES.filter(item => item === name).length, 1); assert.equal(html.split(name).length - 1, 1); assert.ok(html.indexOf(name) < html.lastIndexOf('app.js')); assert.ok(!stripBundledScripts(html).includes(name)); for (const project of ['github', 'hf']) assert.equal(resources.buildFiles(project).filter(item => item === name).length, 1); assert.equal(fs.readFileSync('static/sw.js', 'utf8').split('/static/' + name).length - 1, 1); for (const project of ['check', 'strict', 'search-exact']) { const files = JSON.parse(fs.readFileSync(`config/types/jsconfig.${project}.json`, 'utf8')).files; assert.ok(files.includes('../../static/' + name)); assert.ok(files.includes('../../typings/search-shell.d.ts')); } assert.doesNotMatch(fs.readFileSync('static/' + name, 'utf8'), /\bSTATE\b|\bDOM\b|\bVSCROLL\b|syncStateToURL|localStorage/); }});
test('theme ripple, reader message, persistence and animation cleanup retain ordering', () => { const f = fixture(); f.c.toggleTheme(); assert.equal(f.c.STATE.isDark, false); assert.equal(f.document.body.classes.has('light'), true); assert.equal(f.DOM.themeIconDark.style.display, 'none'); assert.deepEqual(f.messages, [[{type: 'voice-reader-theme-state', theme: 'light'}, 'https://shell.test']]); assert.deepEqual(f.events.slice(-3), [['reader', 'light'], ['preference', 'theme', 'light'], ['listen', 'animationend']]); const ripple = f.nodes[0]; assert.equal(ripple.style.left, '30px'); assert.equal(ripple.style.top, '50px'); assert.equal(ripple.style.background, '#fff'); assert.equal(ripple.style.marginLeft, space ? undefined : '-0px'); ripple.listeners.animationend(); assert.equal(ripple.removed, true); assert.equal(f.document.body.classes.has('theme-transitioning'), false); f.c.readerHost.overlay = null; f.events.length = 0; f.c.applyTheme(); assert.ok(!f.events.some(event => event[0] === 'preference')); f.c.toggleTheme(); assert.equal(f.c.STATE.isDark, true); assert.equal(f.nodes[1].style.background, '#1a1c1e'); assert.equal(f.document.body.classes.has('light'), false);});
test('mobile defaults apply before header, selection, scheduled scroll and preference write', () => { const f = fixture(); f.document.documentElement.classes.add('mobile-boot'); f.c.STATE.rightSidebarOpen = true; f.c.toggleMobile(); assert.equal(f.c.STATE.isMobile, true); assert.equal(f.c.STATE.leftSidebarOpen, false); assert.equal(f.c.STATE.rightSidebarOpen, false); assert.equal(f.document.body.classes.has('mobile'), true); assert.equal(f.document.documentElement.classes.has('mobile-boot'), false); assert.deepEqual(f.events.slice(-4), [['header'], ['selection'], ['frame'], ['preference', 'mobileMode', 'mobile']]); assert.equal(f.frames[0], f.c.updateScrollTrack); assert.ok(!f.events.some(event => event[0] === 'scroll' || event[0] === 'url')); f.c.toggleMobile(); assert.equal(f.c.STATE.leftSidebarOpen, true); assert.equal(f.c.STATE.rightSidebarOpen, false); assert.equal(f.document.body.classes.has('force-desktop'), true); f.events.length = 0; f.c.applyMobileMode(); assert.ok(!f.events.some(event => event[0] === 'preference'));});
test('sidebar event detail and keyboard rules focus only after URL sync, preserving site route mode', () => { for (const side of ['Left', 'Right']) for (const event of [undefined, {detail: 1}, {detail: 0}, {type: 'keydown'}]) { const f = fixture(); f.c.STATE.leftSidebarOpen = false; f.c['toggle' + side + 'Sidebar'](event); const keyboard = event?.detail === 0 || event?.type === 'keydown'; const tail = [...(side === 'Left' ? [['header']] : []), ['url', ...(space ? [true] : [])], ...(keyboard ? [['focus', side === 'Left' ? 'leftControl' : 'rightControl']] : [])]; assert.deepEqual(f.events.slice(-tail.length), tail); assert.equal(f.document.activeElement, keyboard ? (side === 'Left' ? f.leftControl : f.rightControl) : null); }});
test('mobile coverage, mutually exclusive sidebars, inert, ARIA and focus restoration stay synchronized', () => { const f = fixture(); f.c.STATE.isMobile = true; f.c.applyMobileMode(); f.document.activeElement = f.mainControl; f.c.toggleLeftSidebar({detail: 1}); assert.equal(f.main.inert, true); assert.equal(f.document.activeElement, f.leftControl); assert.equal(f.DOM.leftSidebar.inert, false); assert.equal(f.DOM.rightSidebar.inert, true); assert.equal(f.DOM.hamburgerBtn.attributes['aria-expanded'], 'true'); assert.equal(f.DOM.hamburgerBtn.attributes['aria-label'], '关闭仓库导航'); assert.equal(f.DOM.overlay.classes.has('open'), true); f.c.toggleRightSidebar({detail: 0}); assert.equal(f.c.STATE.leftSidebarOpen, false); assert.equal(f.document.activeElement, f.rightControl); assert.equal(f.DOM.leftSidebar.inert, true); assert.equal(f.DOM.settingsBtn.attributes['aria-expanded'], 'true'); f.c.closeRightSidebar(); assert.equal(f.document.activeElement, f.DOM.settingsBtn); assert.equal(f.main.inert, false); assert.equal(f.DOM.overlay.classes.has('open'), false); assert.equal(f.DOM.settingsBtn.attributes['aria-label'], '打开搜索过滤'); f.c.toggleLeftSidebar({type: 'keydown'}); f.DOM.leftSidebar.classes.add('expanded-wide'); f.events.length = 0; f.c.toggleLeftSidebar(); assert.equal(f.DOM.leftSidebar.classes.has('expanded-wide'), false); assert.equal(f.document.activeElement, f.DOM.hamburgerBtn); assert.deepEqual(f.events.at(-1), ['url', ...(space ? [true] : [])]); assert.equal(f.events.filter(event => event[0] === 'header').length, space ? 1 : 0); if (space) assert.deepEqual(f.events[0], ['header']);});
test('sidebar focus skips disabled, invisible and hidden or inert controls and falls back to the sidebar', () => { const f = fixture(), sidebar = f.DOM.leftSidebar; const disabled = f.element('disabled', sidebar); disabled.disabled = true; const invisible = f.element('invisible', sidebar); invisible.hidden = true; const inertParent = f.element('inertParent', sidebar); inertParent.inert = true; const inert = f.element('inert', inertParent); sidebar.controls = [disabled, invisible, inert, f.leftControl]; f.c.focusSearchSidebar(sidebar); assert.equal(f.document.activeElement, f.leftControl); sidebar.controls.pop(); f.c.focusSearchSidebar(sidebar); assert.equal(f.document.activeElement, sidebar);});123456import './result_interactions_contracts.mjs';import './random_discovery_contracts.mjs';import './history_quotation_contracts.mjs';import assert from 'node:assert/strict';import fs from 'node:fs';import vm from 'node:vm';1234567import './result_interactions_contracts.mjs';import './random_discovery_contracts.mjs';import './history_quotation_contracts.mjs';import './shell_contracts.mjs';import assert from 'node:assert/strict';import fs from 'node:fs';import vm from 'node:vm';
8 unmodified lines910111213148 unmodified lines"static/search/positions/domain/policy.js", "static/search/positions/infrastructure/indexed-db.js", "static/search/positions/application/persistence.js", "static/search/positions/application/restoration.js","static/search/results/presentation/row.js", "static/search/results/application/interactions.js","static/search/discovery/application/random.js","static/search/history/infrastructure/store.js", "static/search/history/application/controller.js","static/search/ambient/application/quotation.js","static/search/results/snapshots/domain/copy-budget.js", "static/search/results/snapshots/application/store.js",8 unmodified lines91011121314158 unmodified lines"static/search/positions/domain/policy.js", "static/search/positions/infrastructure/indexed-db.js", "static/search/positions/application/persistence.js", "static/search/positions/application/restoration.js","static/search/results/presentation/row.js", "static/search/results/application/interactions.js","static/search/discovery/application/random.js","static/search/shell/presentation/view.js", "static/search/shell/application/controller.js","static/search/history/infrastructure/store.js", "static/search/history/application/controller.js","static/search/ambient/application/quotation.js","static/search/results/snapshots/domain/copy-budget.js", "static/search/results/snapshots/application/store.js",
1234567891011121314151617181920212223242526272829303132333435363738394041424344interface VoiceOfMLShellState { isDark: boolean; isMobile: boolean; leftSidebarOpen: boolean; rightSidebarOpen: boolean;}interface VoiceOfMLShellEvent { detail?: number; type?: string }interface VoiceOfMLShellElements { themeBtn: HTMLElement; themeIconLight: HTMLElement; themeIconDark: HTMLElement; mobileToggleBtn: HTMLElement; mobileIconPhone: HTMLElement; mobileIconDesktop: HTMLElement; leftSidebar: HTMLElement; rightSidebar: HTMLElement; hamburgerBtn: HTMLElement; settingsBtn: HTMLElement; overlay: HTMLElement;}interface VoiceOfMLShellViewInstance { startThemeTransition(isDark: boolean): HTMLElement; finishThemeTransition(ripple: HTMLElement): void; applyTheme(isDark: boolean): void; applyMobileMode(isMobile: boolean): void; finishMobileMode(): void; collapseLeftSidebar(): void; focusSidebar(side: 'left' | 'right'): void; updateSidebarVisibility(state: VoiceOfMLShellState): void;}interface VoiceOfMLShellDependencies { view: VoiceOfMLShellViewInstance; readState(): VoiceOfMLShellState; setDark(value: boolean): void; setMobile(value: boolean): void; setSidebars(left: boolean, right: boolean): void; writePreference(key: 'theme' | 'mobileMode', value: string): void; notifyReaderTheme(isDark: boolean): void; updateSidebarHeader(): void; updateSelectionUI(): void; scheduleScrollTrack(): void; syncURL(): void; collapseLeftSidebar?(): void;}declare var VoiceOfMLShellView: { create(elements: () => VoiceOfMLShellElements, zeroRippleMargins: boolean): VoiceOfMLShellViewInstance; focusSearchSidebar(sidebar: HTMLElement): void;};declare var VoiceOfMLShell: { create(deps: VoiceOfMLShellDependencies): { toggleTheme(): void; applyTheme(): void; toggleMobile(): void; applyMobileMode(): void; toggleLeftSidebar(event?: VoiceOfMLShellEvent): void; toggleRightSidebar(event?: VoiceOfMLShellEvent): void; closeLeftSidebar(): void; closeRightSidebar(): void; updateSidebarVisibility(): void;}};config/types/jsconfig.check.json+3
config/types/jsconfig.search-exact.json+3
config/types/jsconfig.strict.json+3
eslint.config.mjs+1-1
scripts/build/check_reader_release.mjs+1
scripts/build/compose_app.mjs+1
static/app.js+32-108
static/index.html+2
static/reader-resources.js+1
static/search/shell/application/controller.jsAdded+79
static/search/shell/presentation/view.jsAdded+105
static/sw.js+2
tests/gates/test_static_contract.py+6-1
tests/search/ui/shell_contracts.mjsAdded+170
tests/search/ui/test_sidebar_directory.mjs+1
tests/support/classic_search_modules.js+1
typings/search-shell.d.tsAdded+44