Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
88 changes: 50 additions & 38 deletions src/features/jumpNav.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -230,40 +230,51 @@ const toggleReferenceParents = () =>
.forEach((element) => {
element.click();
});
const expandReferenceChildren = () =>
document
.querySelectorAll<HTMLElement>(".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<HTMLElement>(".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,
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The fix is adding button: 2. The rest is refactor.

});

element.dispatchEvent(event);
};

const expandReferenceChildren = () => {
const expandButtons = document.querySelectorAll<HTMLElement>(
".rm-reference-item .block-expand"
);

expandButtons.forEach((button) => {
triggerContextMenu(button);
const expandOption = findContextMenuOption("Expand all");
expandOption?.click();
});
};

const collapseReferenceChildren = () => {
const expandButtons = document.querySelectorAll<HTMLElement>(
".rm-reference-item .block-expand"
);

expandButtons.forEach((button) => {
triggerContextMenu(button);
const collapseOption = findContextMenuOption("Collapse all");
collapseOption?.click();
});
};
const copyBlockRef = () => {
const uid = window.roamAlphaAPI.ui.getFocusedBlock()?.["block-uid"];
if (uid) {
Expand Down Expand Up @@ -541,7 +552,7 @@ const replaceLastReferenceWithTextAndAlias = () => {
}).then(() =>
setTimeout(() => {
window.roamAlphaAPI.ui.setBlockFocusAndSelection({
location,
location: location || undefined,
selection: { start: prefix.length },
});
}, 200)
Expand Down Expand Up @@ -602,13 +613,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 = [
Expand Down