Skip to content

Instantly share code, notes, and snippets.

@jchen1
Created August 1, 2020 22:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jchen1/bf8e6ed2e411e2ef6f19074f7a301ab4 to your computer and use it in GitHub Desktop.
Save jchen1/bf8e6ed2e411e2ef6f19074f7a301ab4 to your computer and use it in GitHub Desktop.
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