Last active
April 17, 2023 09:53
-
-
Save tombarys/42a6c25748a49184a23fb3018d5f19d3 to your computer and use it in GitHub Desktop.
Move the block to the next monday (context menu plugin for Roam) V1.2
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
;; Warning: this plugin adds a "⇨ Move to next Monday" item on both mobile and desktop Roam apps | |
;; to the context plugin menu and to command pallete (Mac: Cmd-P / Windows: Ctrl-P, then search for "Move" or "monday" etc.). | |
;; To activate Roam context menu on mobile, you need to install Victor Tabori's Long-tap script first: | |
;; https://gist.github.com/thesved/48cab2307cf0598fcc5cd37643d36cb4 | |
;; Installation: | |
;; 1) copy this block as children block `clojure` code block under parent containing {{[[roam/cljs]]}} anywhere | |
;; in [[roam/cljs]] page | |
;; 2) confirm "Yes, I know what I am doing" | |
;; 3) restart Roam | |
;; 4) desktop: right-click on any block context menu (on the bullet) / mobile: long-tap the bullet | |
;; 5) choose Plugins/Move to Monday (attention: moves block including it's children; but do not worry: Cmd/Ctrl-Z works well) | |
;; PS: if the Monday's Daily Page does not exist, it is created first | |
;; | |
;; Thanks to Josh Brown (RoamResearch.com) and Adam Kalisz (OrgPad.com) for answering my clojure-newbie questions. | |
;; My thanks to Josh Brown for helping me. | |
(ns my-custom-roam-151022 | |
(:require | |
[roam.block :as block] | |
[roam.page :as page] | |
[promesa.core :as p])) | |
(defn move-block [block-uid parent-uid] | |
(-> (block/move | |
{:location {:parent-uid parent-uid | |
:order 0} | |
:block {:uid block-uid}}) | |
(.then #(js/console.log "Block moved!")) | |
(.catch #(js/console.log "Moving failed" %)))) | |
(def ms-in-day "Millisenconds in a day of 86400 seconds." (* 86400 1000)) | |
(defn days-from-today [n] | |
(js/window.roamAlphaAPI.util.dateToPageUid (new js/Date (+ (.now js/Date) (* n ms-in-day))))) | |
(defn days-until-monday [] | |
(- 8 (.getDay (js/Date. (.now js/Date))))) | |
(defn days-from-today-title [n] | |
(js/window.roamAlphaAPI.util.dateToPageTitle (new js/Date (+ (.now js/Date) (* n ms-in-day))))) | |
(defn main [] | |
(let [i (days-until-monday)] | |
(js/window.roamAlphaAPI.ui.commandPalette.addCommand | |
(clj->js | |
{:label "Move to next Monday" | |
:callback | |
(fn [e] | |
(let [parent-uid (:block-uid (js->clj (js/window.roamAlphaAPI.ui.getFocusedBlock) :keywordize-keys true))] | |
(p/do | |
(-> | |
(roam.page/create | |
{:page {:title (days-from-today-title i)}}) | |
(.then #(js/console.log "Created new Daily Page!")) | |
(.catch (fn [error] | |
(js/console.log (.-message error))))) | |
(move-block | |
parent-uid (days-from-today i)))))})) | |
(js/window.roamAlphaAPI.ui.blockContextMenu.addCommand | |
(clj->js | |
{:label "⇨ Move to next Monday" | |
:callback | |
(fn [ctx] | |
(p/do | |
(-> (roam.page/create | |
{:page {:title (days-from-today-title i)}}) | |
(.then #(js/console.log "Created new Daily Page!")) | |
(.catch (fn [error] | |
(js/console.log (.-message error))))) | |
(move-block | |
(:block-uid (js->clj ctx :keywordize-keys true)) | |
(days-from-today i))))})))) | |
(main) |
Updated today to reflect some RoamAlphaAPI changes and make it work again. :)
Bugfix + updated to better handle errors.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Updated to properly work on Sundays :) Sorry.