Created
December 11, 2024 01:07
-
-
Save Stvad/0f6e888416f81fc1e4e17fbb8e48cc98 to your computer and use it in GitHub Desktop.
Roam Research shortcut to create a new block on a daily note page and open it in sidebar with focus on it
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function () { | |
// Define the keyboard shortcut | |
const shortcut = { | |
key: "N", | |
modifiers: ["ctrlKey", "shiftKey"], | |
}; | |
// Function to create a new block, open it in the sidebar, and set focus | |
async function createBlockAndFocus() { | |
const todayDate = new Date(); | |
const dailyNoteUid = window.roamAlphaAPI.util.dateToPageUid(todayDate); | |
// Generate a unique UID for the new block | |
const blockUid = window.roamAlphaAPI.util.generateUID(); | |
// Create a new block in the daily note page | |
await window.roamAlphaAPI.data.block.create({ | |
location: { | |
"parent-uid": dailyNoteUid, | |
order: "last", | |
}, | |
block: { | |
uid: blockUid, | |
string: "", // New block content is empty | |
}, | |
}); | |
// Open the block in the right sidebar | |
await window.roamAlphaAPI.ui.rightSidebar.addWindow({ | |
window: { | |
type: "block", | |
"block-uid": blockUid, | |
}, | |
}); | |
// Retrieve the ID of the newly created window | |
const windows = await window.roamAlphaAPI.ui.rightSidebar.getWindows(); | |
const newWindow = windows.find( | |
(win) => win["type"] === "block" && win["block-uid"] === blockUid | |
); | |
if (newWindow) { | |
// Focus the new block in the sidebar | |
window.roamAlphaAPI.ui.setBlockFocusAndSelection({ | |
location: { | |
"block-uid": blockUid, | |
"window-id": newWindow["window-id"], | |
}, | |
}); | |
console.log("Block created, opened, and focused in the sidebar:", blockUid); | |
} else { | |
console.error("Failed to find the window for the new block."); | |
} | |
} | |
// Listen for the keyboard shortcut | |
document.addEventListener("keydown", (event) => { | |
if ( | |
event.key.toUpperCase() === shortcut.key && | |
shortcut.modifiers.every((mod) => event[mod]) | |
) { | |
event.preventDefault(); | |
createBlockAndFocus(); | |
} | |
}); | |
console.log("Roam Research extension loaded: Shortcut Ctrl+Shift+N to create, open, and focus block."); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment