Skip to content

Instantly share code, notes, and snippets.

@yohm
Created November 18, 2019 06:09
Show Gist options
  • Save yohm/6368ba453996ed8c11e6cff4b9b1b51d to your computer and use it in GitHub Desktop.
Save yohm/6368ba453996ed8c11e6cff4b9b1b51d to your computer and use it in GitHub Desktop.
(async () => {
const db = inkdrop.main.dataStore.getLocalDB()
inkdrop.commands.add(document.body, "custom:new-note", async () => {
const { queryContext } = inkdrop.store.getState()
if( queryContext.mode === "book" ) {
const template_tag = (await db.tags.all()).find(t => t.name === "template")
const template_notes = await db.notes.findWithTag(template_tag._id)
const template = template_notes.docs.find(d => queryContext.bookId === d.bookId)
if(!template) {
inkdrop.commands.dispatch(document.body, "core:new-note")
return
}
const d = new Date()
const dayOfWeekStr = ["Sun","Mon","Tue","Wed","Thu","Fri","Sat"][d.getDay()]
const note = {
...template,
_id: db.notes.createId(),
_rev: undefined,
title: template.title.replace('%{year}', d.getFullYear()).replace('%{month}', d.getMonth()+1).replace('%{day}', d.getDate()).replace('%{week}', dayOfWeekStr),
tags: template.tags.filter(t => t !== template_tag._id),
createdAt: +new Date(),
updatedAt: +new Date(),
}
try {
await db.notes.put(note)
inkdrop.commands.dispatch(document.body, "core:open-note", {
noteId: note._id,
})
inkdrop.commands.dispatch(document.body, "editor:focus-mde")
} catch (e) {
console.error(e)
}
} else {
inkdrop.commands.dispatch(document.body, "core:new-note")
}
})
inkdrop.commands.add(document.body, "custom:todays-journal", async () => {
const recent_notes = await db.notes.all({"sort": [{createdAt: 'desc'}]})
const d = new Date()
const dayOfWeekStr = ["Sun","Mon","Tue","Wed","Thu","Fri","Sat"][d.getDay()]
const todays_title = `${d.getFullYear()}/${d.getMonth()+1}/${d.getDate()}(${dayOfWeekStr})`
const found = recent_notes.docs.find(d => d.title === todays_title)
if(found) {
try {
inkdrop.commands.dispatch(document.body, "core:open-note", { noteId: found._id })
inkdrop.commands.dispatch(document.body, "editor:focus-mde")
} catch (e) {
console.error(e)
}
} else {
console.log("no journal found")
}
})
})()
inkdrop.menu.add([
{
label: "File",
submenu: [
{
label: "Templates",
submenu: [
{
label: "Create a journal",
command: "custom:new-note",
},
{
label: "Today's journal",
command: "custom:todays-journal",
},
],
},
],
},
])
'.CodeMirror.vim-mode:not(.insert-mode):not(.key-buffering) textarea':
'0': 'vim-mode:move-to-beginning-of-line'
"body":
"cmd-n": "custom:new-note"
'cmd-j': 'custom:todays-journal'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment