Skip to content

Instantly share code, notes, and snippets.

@kiprasmel
Last active September 17, 2023 18:02
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kiprasmel/3fb1578e1f428dd1dd8e88683ca8c400 to your computer and use it in GitHub Desktop.
Save kiprasmel/3fb1578e1f428dd1dd8e88683ca8c400 to your computer and use it in GitHub Desktop.
roam: Expand All blocks of linked refs
/**
* https://gist.github.com/kiprasmel/3fb1578e1f428dd1dd8e88683ca8c400
*
* put in a javascript code block under a block containing {{[[roam/js]]}} text, like so:
* - {{[[roam/js]]}}
* - this code
*/
// https://roamresearch.com/#/app/developer-documentation/page/JTLUegLiI
window.roamAlphaAPI.ui
.commandPalette
.addCommand({label: 'Expand All blocks of linked refs (custom)',
callback: expandAll,
"disable-hotkey": false,
// this is the default hotkey, and can be customized by the user.
// in most cases, you DO NOT want to be setting a default hotkey
"default-hotkey": "shift-cmd-e"})
// alt instead of super on win prolly
function expandAll() {
// find all top-level blocks of linked references and expand all
//const elements = Array.from(document.querySelectorAll(".rm-mentions.refs-by-page-view .rm-ref-page-view .rm-bullet"))
const elements = Array.from(document.querySelectorAll(".rm-mentions.refs-by-page-view .rm-ref-page-view .rm-reference-item > * > .roam-block-container > .rm-block-main.rm-block__self .rm-bullet"))
for (const el of elements) {
// open context menu
// https://stackoverflow.com/a/52878847/9285308
const event = new MouseEvent("contextmenu", {
bubbles: true,
cancelable: false,
view: window,
button: 2,
buttons: 0,
clientX: el.getBoundingClientRect().x,
clientY: el.getBoundingClientRect().y
});
el.dispatchEvent(event);
// find the "Expand all" button and click it
Array.from(document.querySelectorAll('.bp3-menu-item.bp3-popover-dismiss')).find(x => x.textContent === "Expand all").click()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment