Skip to content

Commit da07301

Browse files
committed
Refactor page grouping logic to combine parent and map retrieval, improving efficiency and readability. Update sidebar filter to handle navigation more robustly by ensuring the correct element is clicked.
1 parent 9c9de40 commit da07301

3 files changed

Lines changed: 43 additions & 18 deletions

File tree

src/backend/controllers/pages.ts

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -109,10 +109,9 @@ class Pages {
109109
public static async getParentSelectOptions(
110110
excludePageId?: EntityId
111111
): Promise<Array<{ page: Page; depth: number; indent: string }>> {
112-
const pages = excludePageId
113-
? await this.groupByParent(excludePageId)
114-
: await this.groupByParent();
115-
const pagesMap = await this.getPagesMap();
112+
const { pages, pagesMap } = excludePageId
113+
? await this.groupByParentWithMap(excludePageId)
114+
: await this.groupByParentWithMap('' as EntityId);
116115
const indentUnit = '\u00a0\u00a0';
117116

118117
return pages.map((page) => {
@@ -123,21 +122,20 @@ class Pages {
123122
}
124123

125124
/**
126-
* Group all pages by their parents
127-
* If the pageId is passed, it excludes passed page from result pages
128-
*
129-
* @param {string} pageId - pageId to exclude from result pages
130-
* @returns {Page[]}
125+
* Same as {@link groupByParent} but returns the pages map from the same load (no second getPagesMap).
131126
*/
132-
public static async groupByParent(pageId = '' as EntityId): Promise<Page[]> {
127+
private static async groupByParentWithMap(pageId = '' as EntityId): Promise<{
128+
pages: Page[];
129+
pagesMap: Map<string, Page>;
130+
}> {
133131
const rootPageOrder = await PagesOrder.getRootPageOrder(); // get order of the root pages
134132
const childPageOrder = await PagesOrder.getChildPageOrder(); // get order of the all other pages
135133

136134
/**
137135
* If there is no root and child page order, then it returns an empty array
138136
*/
139137
if (!rootPageOrder || (!rootPageOrder && childPageOrder.length <= 0)) {
140-
return [];
138+
return { pages: [], pagesMap: new Map() };
141139
}
142140

143141
const pagesMap = await this.getPagesMap();
@@ -176,16 +174,31 @@ class Pages {
176174
* Otherwise just returns result itself
177175
*/
178176
if (pageId) {
179-
return this.removeChildren(result, pageId).reduce((prev, curr) => {
177+
const pages = this.removeChildren(result, pageId).reduce((prev, curr) => {
180178
if (curr instanceof Page) {
181179
prev.push(curr);
182180
}
183181

184182
return prev;
185183
}, Array<Page>());
186-
} else {
187-
return result;
184+
185+
return { pages, pagesMap };
188186
}
187+
188+
return { pages: result, pagesMap };
189+
}
190+
191+
/**
192+
* Group all pages by their parents
193+
* If the pageId is passed, it excludes passed page from result pages
194+
*
195+
* @param {string} pageId - pageId to exclude from result pages
196+
* @returns {Page[]}
197+
*/
198+
public static async groupByParent(pageId = '' as EntityId): Promise<Page[]> {
199+
const { pages } = await this.groupByParentWithMap(pageId);
200+
201+
return pages;
189202
}
190203

191204
/**

src/frontend/js/classes/sidebar-filter.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,8 +96,10 @@ export default class SidebarFilter {
9696

9797
// handle enter key when item is focused.
9898
if (e.code === 'Enter' && this.selectedSearchResultIndex !== null) {
99-
// navigate to focused item.
100-
this.searchResults[this.selectedSearchResultIndex].element.click();
99+
const raw = this.searchResults[this.selectedSearchResultIndex].element;
100+
const navigateEl = raw.closest('a') || raw;
101+
102+
navigateEl.click();
101103
// prevent default action.
102104
e.preventDefault();
103105
e.stopPropagation();

src/frontend/js/modules/sidebar.js

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@ import SidebarFilter from '../classes/sidebar-filter';
77
const LOCAL_STORAGE_KEY = 'docs_sidebar_state';
88
const SIDEBAR_VISIBILITY_KEY = 'docs_sidebar_visibility';
99

10+
/**
11+
* Slack beyond scrollHeight + vertical borders for max-height (subpixels, hover radius).
12+
*/
13+
const SIDEBAR_LIST_MAX_HEIGHT_SLACK_PX = 4;
14+
1015
/**
1116
* Sidebar module
1217
*/
@@ -94,7 +99,7 @@ export default class Sidebar {
9499
this.nodes.rootSections,
95100
this.nodes.sidebarContent,
96101
this.nodes.search,
97-
this.setSectionCollapsed
102+
this.setSectionCollapsed.bind(this)
98103
);
99104

100105
this.ready();
@@ -150,7 +155,12 @@ export default class Sidebar {
150155
if (!list) {
151156
return;
152157
}
153-
list.style.maxHeight = `${list.scrollHeight + 4}px`;
158+
const cs = globalThis.getComputedStyle(list);
159+
const borderY =
160+
(Number.parseFloat(cs.borderTopWidth) || 0) +
161+
(Number.parseFloat(cs.borderBottomWidth) || 0);
162+
163+
list.style.maxHeight = `${list.scrollHeight + borderY + SIDEBAR_LIST_MAX_HEIGHT_SLACK_PX}px`;
154164
});
155165
});
156166
}

0 commit comments

Comments
 (0)