From f62487b0b0f28093ceb11160ea7b92522da1f92a Mon Sep 17 00:00:00 2001 From: fraxken Date: Tue, 12 Aug 2025 23:44:51 +0200 Subject: [PATCH] refactor(event): move keydown events to wiki component --- public/components/wiki/wiki.js | 53 +++++++++++++++++++++----- workspaces/documentation-ui/index.d.ts | 4 ++ workspaces/documentation-ui/index.js | 26 +------------ 3 files changed, 50 insertions(+), 33 deletions(-) diff --git a/public/components/wiki/wiki.js b/public/components/wiki/wiki.js index b9244570..bfc2efef 100644 --- a/public/components/wiki/wiki.js +++ b/public/components/wiki/wiki.js @@ -13,7 +13,9 @@ export class Wiki { const { header, navigation } = documentationUI.render( this.documentationRenderContainer, { preCacheAllFlags: true } ); + /** @type {documentationUI.Header} */ this.header = header; + /** @type {Record} */ this.navigation = navigation; const packageInfoDomElement = document.getElementById(PackageInfo.DOMElementName); @@ -31,18 +33,51 @@ export class Wiki { }); document.addEventListener("keydown", (event) => { - const isTargetInput = event.target.tagName === "INPUT"; - const isTargetPopup = event.target.id === "popup--background"; - if (isTargetInput || isTargetPopup) { - return; + this.#keydownHotkeys(event); + if (this.isOpen) { + this.#keydownArrows(event); } + }); + } + + /** + * @param {KeyboardEvent} event + */ + #keydownHotkeys(event) { + const isTargetInput = event.target.tagName === "INPUT"; + const isTargetPopup = event.target.id === "popup--background"; + if (isTargetInput || isTargetPopup) { + return; + } - const hotkeys = JSON.parse(localStorage.getItem("hotkeys")); + const hotkeys = JSON.parse(localStorage.getItem("hotkeys")); - if (event.key.toUpperCase() === hotkeys.wiki) { - this[this.isOpen ? "close" : "open"](); - } - }); + if (event.key.toUpperCase() === hotkeys.wiki) { + this[this.isOpen ? "close" : "open"](); + } + } + + /** + * @param {KeyboardEvent} event + */ + #keydownArrows(event) { + /** @type {documentationUI.Navigation} */ + const activeNav = this.navigation[this.header.active.getAttribute("data-menu")]; + + switch (event.key) { + case "ArrowLeft": + case "ArrowRight": + this.header.switchActiveView(); + break; + case "ArrowUp": + activeNav.previous(); + break; + case "ArrowDown": + activeNav.next(); + break; + default: + break; + } } get isOpen() { diff --git a/workspaces/documentation-ui/index.d.ts b/workspaces/documentation-ui/index.d.ts index 0dc65737..9a442a7c 100644 --- a/workspaces/documentation-ui/index.d.ts +++ b/workspaces/documentation-ui/index.d.ts @@ -4,6 +4,7 @@ declare class Header { defaultName: string | null; setNewActiveView(name: string): void; + switchActiveView(): void; } declare class Navigation { @@ -14,6 +15,9 @@ declare class Navigation { fetchCallback: (name: string, menu: HTMLElement) => any; setNewActiveMenu(name: string): void; + generateFromIterable(items: Iterable): DocumentFragment; + next(): void; + previous(): void; } export interface RenderDocumentationUIOptions { diff --git a/workspaces/documentation-ui/index.js b/workspaces/documentation-ui/index.js index c48f7406..e25ec969 100644 --- a/workspaces/documentation-ui/index.js +++ b/workspaces/documentation-ui/index.js @@ -54,7 +54,9 @@ const kHeaderMenus = Object.entries(kWikiMenus) export function render(rootElement, options = {}) { const { prefetch = false } = options; + /** @type {Record} */ const navigation = {}; + /** @type {HTMLElement[]} */ const containers = []; for (const [name, properties] of Object.entries(kWikiMenus)) { const nav = new Navigation({ @@ -91,30 +93,6 @@ export function render(rootElement, options = {}) { } rootElement.appendChild(mainContainer); - document.addEventListener("keydown", (event) => { - const isWikiOpen = document.getElementById("documentation-root-element").classList.contains("slide-in"); - if (!isWikiOpen) { - return; - } - - const activeNav = navigation[header.active.getAttribute("data-menu")]; - - switch (event.key) { - case "ArrowLeft": - case "ArrowRight": - header.switchActiveView(); - break; - case "ArrowUp": - activeNav.previous(); - break; - case "ArrowDown": - activeNav.next(); - break; - default: - break; - } - }); - return { header, navigation