Skip to content

Commit 790e0e8

Browse files
authored
refactor(wiki): move keydown events to wiki component (#562)
1 parent a613c2c commit 790e0e8

3 files changed

Lines changed: 50 additions & 33 deletions

File tree

public/components/wiki/wiki.js

Lines changed: 44 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@ export class Wiki {
1313
const { header, navigation } = documentationUI.render(
1414
this.documentationRenderContainer, { preCacheAllFlags: true }
1515
);
16+
/** @type {documentationUI.Header} */
1617
this.header = header;
18+
/** @type {Record<string, documentationUI.Navigation>} */
1719
this.navigation = navigation;
1820

1921
const packageInfoDomElement = document.getElementById(PackageInfo.DOMElementName);
@@ -31,18 +33,51 @@ export class Wiki {
3133
});
3234

3335
document.addEventListener("keydown", (event) => {
34-
const isTargetInput = event.target.tagName === "INPUT";
35-
const isTargetPopup = event.target.id === "popup--background";
36-
if (isTargetInput || isTargetPopup) {
37-
return;
36+
this.#keydownHotkeys(event);
37+
if (this.isOpen) {
38+
this.#keydownArrows(event);
3839
}
40+
});
41+
}
42+
43+
/**
44+
* @param {KeyboardEvent} event
45+
*/
46+
#keydownHotkeys(event) {
47+
const isTargetInput = event.target.tagName === "INPUT";
48+
const isTargetPopup = event.target.id === "popup--background";
49+
if (isTargetInput || isTargetPopup) {
50+
return;
51+
}
3952

40-
const hotkeys = JSON.parse(localStorage.getItem("hotkeys"));
53+
const hotkeys = JSON.parse(localStorage.getItem("hotkeys"));
4154

42-
if (event.key.toUpperCase() === hotkeys.wiki) {
43-
this[this.isOpen ? "close" : "open"]();
44-
}
45-
});
55+
if (event.key.toUpperCase() === hotkeys.wiki) {
56+
this[this.isOpen ? "close" : "open"]();
57+
}
58+
}
59+
60+
/**
61+
* @param {KeyboardEvent} event
62+
*/
63+
#keydownArrows(event) {
64+
/** @type {documentationUI.Navigation} */
65+
const activeNav = this.navigation[this.header.active.getAttribute("data-menu")];
66+
67+
switch (event.key) {
68+
case "ArrowLeft":
69+
case "ArrowRight":
70+
this.header.switchActiveView();
71+
break;
72+
case "ArrowUp":
73+
activeNav.previous();
74+
break;
75+
case "ArrowDown":
76+
activeNav.next();
77+
break;
78+
default:
79+
break;
80+
}
4681
}
4782

4883
get isOpen() {

workspaces/documentation-ui/index.d.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ declare class Header {
44
defaultName: string | null;
55

66
setNewActiveView(name: string): void;
7+
switchActiveView(): void;
78
}
89

910
declare class Navigation {
@@ -14,6 +15,9 @@ declare class Navigation {
1415
fetchCallback: (name: string, menu: HTMLElement) => any;
1516

1617
setNewActiveMenu(name: string): void;
18+
generateFromIterable(items: Iterable<HTMLElement>): DocumentFragment;
19+
next(): void;
20+
previous(): void;
1721
}
1822

1923
export interface RenderDocumentationUIOptions {

workspaces/documentation-ui/index.js

Lines changed: 2 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,9 @@ const kHeaderMenus = Object.entries(kWikiMenus)
5454
export function render(rootElement, options = {}) {
5555
const { prefetch = false } = options;
5656

57+
/** @type {Record<string, Navigation>} */
5758
const navigation = {};
59+
/** @type {HTMLElement[]} */
5860
const containers = [];
5961
for (const [name, properties] of Object.entries(kWikiMenus)) {
6062
const nav = new Navigation({
@@ -91,30 +93,6 @@ export function render(rootElement, options = {}) {
9193
}
9294
rootElement.appendChild(mainContainer);
9395

94-
document.addEventListener("keydown", (event) => {
95-
const isWikiOpen = document.getElementById("documentation-root-element").classList.contains("slide-in");
96-
if (!isWikiOpen) {
97-
return;
98-
}
99-
100-
const activeNav = navigation[header.active.getAttribute("data-menu")];
101-
102-
switch (event.key) {
103-
case "ArrowLeft":
104-
case "ArrowRight":
105-
header.switchActiveView();
106-
break;
107-
case "ArrowUp":
108-
activeNav.previous();
109-
break;
110-
case "ArrowDown":
111-
activeNav.next();
112-
break;
113-
default:
114-
break;
115-
}
116-
});
117-
11896
return {
11997
header,
12098
navigation

0 commit comments

Comments
 (0)