Skip to content

Commit 08dd04a

Browse files
authored
Hotkeys Expand/Collapse Reference children fix (#498)
* Refactor jumpNav functions for context menu interactions - Introduced `findContextMenuOption` to streamline option retrieval. - Created `triggerContextMenu` for dispatching context menu events. - Updated `expandReferenceChildren` and `collapseReferenceChildren` to use the new functions with error handling. - Improved readability and maintainability by reducing nested calls and using early returns. * typefix * Remove local try/catch
1 parent 2477fe7 commit 08dd04a

1 file changed

Lines changed: 50 additions & 38 deletions

File tree

src/features/jumpNav.tsx

Lines changed: 50 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -230,40 +230,51 @@ const toggleReferenceParents = () =>
230230
.forEach((element) => {
231231
element.click();
232232
});
233-
const expandReferenceChildren = () =>
234-
document
235-
.querySelectorAll<HTMLElement>(".rm-reference-item .block-expand")
236-
.forEach((element) => {
237-
element.dispatchEvent(
238-
new MouseEvent("contextmenu", {
239-
bubbles: true,
240-
})
241-
);
242-
const li = Array.from(
243-
document.querySelector(
244-
'.bp3-transition-container:not([style*="display: none;"]) .bp3-popover-content > div > ul'
245-
)?.children || []
246-
).find((e: Element) => (e as HTMLLinkElement).innerText === "Expand all");
247-
(li?.childNodes[0] as HTMLElement)?.click();
248-
});
249-
const collapseReferenceChildren = () =>
250-
document
251-
.querySelectorAll<HTMLElement>(".rm-reference-item .block-expand")
252-
.forEach((element) => {
253-
element.dispatchEvent(
254-
new MouseEvent("contextmenu", {
255-
bubbles: true,
256-
})
257-
);
258-
const li = Array.from(
259-
document.querySelector(
260-
'.bp3-transition-container:not([style*="display: none;"]) .bp3-popover-content > div > ul'
261-
)?.children || []
262-
).find(
263-
(e: Element) => (e as HTMLLinkElement).innerText === "Collapse all"
264-
);
265-
(li?.childNodes[0] as HTMLElement).click();
266-
});
233+
const findContextMenuOption = (optionText: string): HTMLElement | null => {
234+
const contextMenu = document.querySelector(
235+
'.bp3-transition-container:not([style*="display: none;"]) .bp3-popover-content > div > ul'
236+
);
237+
if (!contextMenu) return null;
238+
239+
const options = Array.from(contextMenu.children);
240+
const targetOption = options.find(
241+
(e: Element) => (e as HTMLLinkElement).innerText === optionText
242+
);
243+
return (targetOption?.childNodes[0] as HTMLElement) || null;
244+
};
245+
246+
const triggerContextMenu = (element: HTMLElement) => {
247+
const event = new MouseEvent("contextmenu", {
248+
bubbles: true,
249+
button: 2,
250+
});
251+
252+
element.dispatchEvent(event);
253+
};
254+
255+
const expandReferenceChildren = () => {
256+
const expandButtons = document.querySelectorAll<HTMLElement>(
257+
".rm-reference-item .block-expand"
258+
);
259+
260+
expandButtons.forEach((button) => {
261+
triggerContextMenu(button);
262+
const expandOption = findContextMenuOption("Expand all");
263+
expandOption?.click();
264+
});
265+
};
266+
267+
const collapseReferenceChildren = () => {
268+
const expandButtons = document.querySelectorAll<HTMLElement>(
269+
".rm-reference-item .block-expand"
270+
);
271+
272+
expandButtons.forEach((button) => {
273+
triggerContextMenu(button);
274+
const collapseOption = findContextMenuOption("Collapse all");
275+
collapseOption?.click();
276+
});
277+
};
267278
const copyBlockRef = () => {
268279
const uid = window.roamAlphaAPI.ui.getFocusedBlock()?.["block-uid"];
269280
if (uid) {
@@ -541,7 +552,7 @@ const replaceLastReferenceWithTextAndAlias = () => {
541552
}).then(() =>
542553
setTimeout(() => {
543554
window.roamAlphaAPI.ui.setBlockFocusAndSelection({
544-
location,
555+
location: location || undefined,
545556
selection: { start: prefix.length },
546557
});
547558
}, 200)
@@ -602,13 +613,14 @@ const expandCollapseBlockTree = () => {
602613
Promise.resolve(
603614
window.roamAlphaAPI.ui.getFocusedBlock()?.["block-uid"] ||
604615
window.roamAlphaAPI.ui.mainWindow.getOpenPageOrBlockUid()
605-
).then((blockUid) =>
616+
).then((blockUid) => {
617+
if (!blockUid) return;
606618
renderOverlay({
607619
id: "exp-col-dialog",
608620
Overlay: ExpColDialog,
609621
props: { blockUid },
610-
})
611-
);
622+
});
623+
});
612624
};
613625

614626
const commands = [

0 commit comments

Comments
 (0)