/roam-copy-daily-template.js Secret
Created
August 1, 2020 22:25
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
const templateName = "Daily Template"; | |
function deepPullNode(eid) { | |
return roamAlphaAPI.pull(`[:node/title {:block/children [:db/id :block/order :block/string { :block/children ...}]}]`, eid); | |
} | |
function nodeByTitle(title) { | |
return roamAlphaAPI.q(`[:find ?e :where [?e :node/title "${title}"]]`); | |
} | |
function nodeToString(deepEntity, level = 0) { | |
const indent = (level + 1) * 4; | |
const children = (deepEntity[":block/children"] || []) | |
.sort((a, b) => a[':block/order'] - b[':block/order']) | |
.map(child => { | |
const childStr = nodeToString(child, level + 1); | |
return `${" ".repeat(indent)}- ${childStr}`; | |
}); | |
return `${deepEntity[":block/string"] || ""} | |
${children.join("\n")}`; | |
} | |
function dailyTemplateStr() { | |
const eid = nodeByTitle(templateName)[0][0]; | |
const entity = deepPullNode(eid); | |
return nodeToString(entity); | |
} | |
function onHashChange(evt, repeat) { | |
const hash = window.location.href.split("#")[1]; | |
if (hash === `/app/${graph}`) { | |
const today = moment().format("MMMM Do, YYYY"); | |
const todayQuery = nodeByTitle(today); | |
if (todayQuery.length > 0) { | |
const todayEnt = deepPullNode(todayQuery[0][0]); | |
if (!(todayEnt[":block/children"] || []).some(el => (el[":block/string"] || "").length > 0)) { | |
const str = dailyTemplateStr(); | |
// click the first node | |
document.querySelector(".rm-block-text").dispatchEvent(new MouseEvent("mousedown", { | |
view: window, | |
buttons: 1, | |
bubbles: true, | |
cancelable: true | |
})); | |
// pause to let roam update the active textarea | |
window.setTimeout(function() { | |
const textarea = document.querySelector("textarea:first-of-type"); | |
if (textarea.value.length === 0) { | |
textarea.value = str; | |
const data = new DataTransfer(); | |
data.setData("text", str); | |
textarea.dispatchEvent( | |
new ClipboardEvent("paste", { clipboardData: data, data: str, view: window, bubbles: true, cancelable: true }) | |
); | |
} | |
}, 100); | |
} | |
} | |
} | |
if (repeat) { | |
setTimeout(() => onHashChange(null, true), 1000); | |
} | |
} | |
function loadJS(url) { | |
if (document.querySelector(`script[src='${url}']`) !== null) { | |
return Promise.resolve(); | |
} | |
return new Promise((resolve, reject) => { | |
const tag = document.createElement("script"); | |
tag.src = url; | |
tag.onload = () => resolve(); | |
tag.onerror = () => reject(); | |
document.head.appendChild(tag); | |
}); | |
} | |
async function init() { | |
await loadJS("https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.27.0/moment.min.js"); | |
return { graph: localStorage.getItem("lastUsedGraph") }; | |
} | |
init().then(data => { | |
window.removeEventListener("hashchange", onHashChange); | |
window.addEventListener("hashchange", onHashChange); | |
setTimeout(() => onHashChange(null, true), 1000); | |
}); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment