From f00b0a2d7fed3793f7e3fffbaa9f52c20a159ac5 Mon Sep 17 00:00:00 2001 From: Michael Gartner Date: Sun, 13 Apr 2025 16:26:07 -0600 Subject: [PATCH 1/3] 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. --- src/features/jumpNav.tsx | 87 ++++++++++++++++++++++++---------------- 1 file changed, 53 insertions(+), 34 deletions(-) diff --git a/src/features/jumpNav.tsx b/src/features/jumpNav.tsx index 018c15e..c11053c 100644 --- a/src/features/jumpNav.tsx +++ b/src/features/jumpNav.tsx @@ -230,40 +230,59 @@ const toggleReferenceParents = () => .forEach((element) => { element.click(); }); -const expandReferenceChildren = () => - document - .querySelectorAll(".rm-reference-item .block-expand") - .forEach((element) => { - element.dispatchEvent( - new MouseEvent("contextmenu", { - bubbles: true, - }) - ); - const li = Array.from( - document.querySelector( - '.bp3-transition-container:not([style*="display: none;"]) .bp3-popover-content > div > ul' - )?.children || [] - ).find((e: Element) => (e as HTMLLinkElement).innerText === "Expand all"); - (li?.childNodes[0] as HTMLElement)?.click(); - }); -const collapseReferenceChildren = () => - document - .querySelectorAll(".rm-reference-item .block-expand") - .forEach((element) => { - element.dispatchEvent( - new MouseEvent("contextmenu", { - bubbles: true, - }) - ); - const li = Array.from( - document.querySelector( - '.bp3-transition-container:not([style*="display: none;"]) .bp3-popover-content > div > ul' - )?.children || [] - ).find( - (e: Element) => (e as HTMLLinkElement).innerText === "Collapse all" - ); - (li?.childNodes[0] as HTMLElement).click(); - }); +const findContextMenuOption = (optionText: string): HTMLElement | null => { + const contextMenu = document.querySelector( + '.bp3-transition-container:not([style*="display: none;"]) .bp3-popover-content > div > ul' + ); + if (!contextMenu) return null; + + const options = Array.from(contextMenu.children); + const targetOption = options.find( + (e: Element) => (e as HTMLLinkElement).innerText === optionText + ); + return (targetOption?.childNodes[0] as HTMLElement) || null; +}; + +const triggerContextMenu = (element: HTMLElement) => { + const event = new MouseEvent("contextmenu", { + bubbles: true, + button: 2, + }); + + element.dispatchEvent(event); +}; + +const expandReferenceChildren = () => { + const expandButtons = document.querySelectorAll( + ".rm-reference-item .block-expand" + ); + + expandButtons.forEach((button) => { + try { + triggerContextMenu(button); + const expandOption = findContextMenuOption("Expand all"); + expandOption?.click(); + } catch (error) { + console.error("Failed to expand reference children:", error); + } + }); +}; + +const collapseReferenceChildren = () => { + const expandButtons = document.querySelectorAll( + ".rm-reference-item .block-expand" + ); + + expandButtons.forEach((button) => { + try { + triggerContextMenu(button); + const collapseOption = findContextMenuOption("Collapse all"); + collapseOption?.click(); + } catch (error) { + console.error("Failed to collapse reference children:", error); + } + }); +}; const copyBlockRef = () => { const uid = window.roamAlphaAPI.ui.getFocusedBlock()?.["block-uid"]; if (uid) { From f6a3ad77b560da2ecbb180cfd793b23dc63ddc0c Mon Sep 17 00:00:00 2001 From: Michael Gartner Date: Sun, 13 Apr 2025 16:26:22 -0600 Subject: [PATCH 2/3] typefix --- src/features/jumpNav.tsx | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/features/jumpNav.tsx b/src/features/jumpNav.tsx index c11053c..db13042 100644 --- a/src/features/jumpNav.tsx +++ b/src/features/jumpNav.tsx @@ -560,7 +560,7 @@ const replaceLastReferenceWithTextAndAlias = () => { }).then(() => setTimeout(() => { window.roamAlphaAPI.ui.setBlockFocusAndSelection({ - location, + location: location || undefined, selection: { start: prefix.length }, }); }, 200) @@ -621,13 +621,14 @@ const expandCollapseBlockTree = () => { Promise.resolve( window.roamAlphaAPI.ui.getFocusedBlock()?.["block-uid"] || window.roamAlphaAPI.ui.mainWindow.getOpenPageOrBlockUid() - ).then((blockUid) => + ).then((blockUid) => { + if (!blockUid) return; renderOverlay({ id: "exp-col-dialog", Overlay: ExpColDialog, props: { blockUid }, - }) - ); + }); + }); }; const commands = [ From d7b03b6ddaa206c5a023c054826bde5b17673aa3 Mon Sep 17 00:00:00 2001 From: Michael Gartner Date: Sun, 13 Apr 2025 16:34:19 -0600 Subject: [PATCH 3/3] Remove local try/catch --- src/features/jumpNav.tsx | 20 ++++++-------------- 1 file changed, 6 insertions(+), 14 deletions(-) diff --git a/src/features/jumpNav.tsx b/src/features/jumpNav.tsx index db13042..2802603 100644 --- a/src/features/jumpNav.tsx +++ b/src/features/jumpNav.tsx @@ -258,13 +258,9 @@ const expandReferenceChildren = () => { ); expandButtons.forEach((button) => { - try { - triggerContextMenu(button); - const expandOption = findContextMenuOption("Expand all"); - expandOption?.click(); - } catch (error) { - console.error("Failed to expand reference children:", error); - } + triggerContextMenu(button); + const expandOption = findContextMenuOption("Expand all"); + expandOption?.click(); }); }; @@ -274,13 +270,9 @@ const collapseReferenceChildren = () => { ); expandButtons.forEach((button) => { - try { - triggerContextMenu(button); - const collapseOption = findContextMenuOption("Collapse all"); - collapseOption?.click(); - } catch (error) { - console.error("Failed to collapse reference children:", error); - } + triggerContextMenu(button); + const collapseOption = findContextMenuOption("Collapse all"); + collapseOption?.click(); }); }; const copyBlockRef = () => {