├── LICENSE ├── README.md ├── clear-sidebar-plugin.js └── random-page-plugin.js /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2020 Oliver Schmid 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Roam Research Plugins 2 | 3 | ### Clear Sidebar Plugin 4 | 5 | Adds a button to the right sidebar to clear all open blocks/pages. Handy when you have a lot open in the sidebar and want to move on to a new task. 6 | 7 | Also adds a keyboard shortcut (Alt/Opt + C) to clear all blocks/pages from sidebar. 8 | 9 | ### Left Expander Plugin 10 | 11 | Moved to Roam Depot. Source [here](https://github.com/oschmid/roam-left-expander/) 12 | 13 | ### Random Page Plugin 14 | 15 | Adds a button to the left sidebar to go to a randomly chosen note. Handy for finding old pages to cleanup, reminding yourself of old ideas, and connecting those old ideas to those you're thinking of right now. 16 | 17 | Also adds a keyboard shortcut (Alt/Opt + R) to go to a random note and (Alt/Opt + Shift + R) to open a random note in the right sidebar. 18 | 19 | When used on the 'All Pages' page, use the search filter to chose from only a subset of pages. 20 | 21 | ## Installation 22 | 23 | 1. Create a block containing `{{[[roam/js]]}}` 24 | 2. Indented beneath that, create a code block `````` 25 | 3. Choose javascript and copy the contents of the `*-plugin.js` file 26 | -------------------------------------------------------------------------------- /clear-sidebar-plugin.js: -------------------------------------------------------------------------------- 1 | function clearSidebarPlugin() { 2 | // settings 3 | const shortcut = 67; // Alt+C 4 | 5 | function addButton() { 6 | // cleanup old versions of the button 7 | var clearButton = document.querySelector('#clear-button'); 8 | if (clearButton != null) { 9 | clearButton.parentNode.removeChild(clearButton); 10 | } 11 | // create button 12 | var template = document.createElement('template'); 13 | template.innerHTML = ''; 14 | template.content.firstChild.onclick = clearSidebar; 15 | clearButton = template.content.firstChild; 16 | 17 | // insert button into sidebar 18 | const sidebarMenu = document.querySelector('#right-sidebar .flex-h-box'); 19 | sidebarMenu.appendChild(clearButton); 20 | } 21 | 22 | function addKeyboardShortcut() { 23 | document.onkeyup = async function(e) { 24 | var key = e.which || e.keyCode; 25 | if (e.altKey && key === shortcut) { 26 | await clearSidebar(); 27 | } 28 | } 29 | } 30 | 31 | async function clearSidebar() { 32 | var x; 33 | while ((x = document.querySelector('.bp3-icon-cross:not(#clear-button)')) !== null) { 34 | x.click(); 35 | await new Promise(r => setTimeout(r, 0)); // let Roam re-render before deleting next block 36 | } 37 | } 38 | 39 | addButton(); 40 | addKeyboardShortcut(); 41 | } 42 | setTimeout(clearSidebarPlugin, 0); 43 | -------------------------------------------------------------------------------- /random-page-plugin.js: -------------------------------------------------------------------------------- 1 | function randomPagePlugin() { 2 | function isMac() { 3 | return window.navigator.platform.startsWith('Mac'); 4 | } 5 | 6 | // settings 7 | const title = 'Go to random page'; 8 | const icon = 'bp3-button bp3-minimal bp3-icon-random pointer bp3-small'; 9 | const shortcut = isMac() ? {ctrlKey: true, key: "r"} : {altKey: true, key: "r"}; 10 | 11 | function addButton() { 12 | // cleanup old versions of the button 13 | var randomButton = document.querySelector('#random-button'); 14 | if (randomButton != null) { 15 | randomButton.parentNode.removeChild(randomButton); 16 | } 17 | // create button 18 | var template = document.createElement('template'); 19 | template.innerHTML = ''; 20 | template.content.firstChild.onclick = goToRandomPage; 21 | randomButton = template.content.firstChild; 22 | 23 | // create spacer 24 | var spacer = document.querySelector('#random-spacer'); 25 | if (spacer != null) { 26 | spacer.parentNode.removeChild(spacer); 27 | } 28 | template.innerHTML = '
'; 29 | spacer = template.content.firstChild; 30 | 31 | // insert button into topbar 32 | const search = document.querySelector('.rm-topbar .rm-find-or-create-wrapper'); 33 | search.insertAdjacentElement('afterend', randomButton); 34 | search.insertAdjacentElement('afterend', spacer); 35 | } 36 | 37 | function addKeyboardShortcut() { 38 | document.onkeydown = function(e) { 39 | if (shortcut.ctrlKey && !e.ctrlKey) return; 40 | if (shortcut.shiftKey && !e.shiftKey) return; 41 | if (shortcut.altKey && !e.altKey) return; 42 | if (shortcut.key === e.key.toLowerCase()) { 43 | e.preventDefault(); 44 | goToRandomPage(e); 45 | } 46 | } 47 | } 48 | 49 | function goToRandomPage(e) { 50 | const allPages = roamAlphaAPI.q('[ :find (pull ?e [:block/uid]) :where [?e :node/title]]'); 51 | const page = getRandomElement(allPages); 52 | const uid = page[0].uid; 53 | const db = location.hash.split('/')[2]; 54 | if (e.shiftKey) { 55 | roamAlphaAPI.ui.rightSidebar.addWindow({window:{type:'block','block-uid':uid}}); 56 | } else { 57 | setTimeout(function(){location.assign('/#/app/' + db + '/page/' + uid);}, 0); 58 | } 59 | } 60 | 61 | function getRandomElement(array) { 62 | return array[Math.floor(Math.random() * array.length)]; 63 | } 64 | 65 | addButton(); 66 | addKeyboardShortcut(); 67 | } 68 | setTimeout(randomPagePlugin, 0); --------------------------------------------------------------------------------