Skip to content

Commit

Permalink
Added ability to create new notes and tasks
Browse files Browse the repository at this point in the history
  • Loading branch information
gravypower committed Apr 13, 2021
1 parent cd9b4f0 commit 380281f
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 0 deletions.
36 changes: 36 additions & 0 deletions src/QuickLinksPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,45 @@ interface Hint {
module.exports = {
default: function(context: any) {

function NewNoteHint(prefix: string, todo: boolean) {
let description = "New Note";

if(todo)
description = "New Task";

const newNoteHint: Hint = {
text: prefix,
hint: async (cm, data, completion) => {
const from = completion.from || data.from;
from.ch -= 2;

const response = await context.postMessage({command: 'createNote', prefix: prefix, todo: todo});
cm.replaceRange(`[${prefix}](:/${response.newNote.id})`, from, cm.getCursor(), "complete");
},
};

newNoteHint.render = (elem, _data, _completion) => {
const p = elem.ownerDocument.createElement('div');
p.setAttribute('style', 'width: 100%; display:table;');
elem.appendChild(p);
p.innerHTML = `
<div style="display:table-cell; padding-right: 5px">${prefix}</div>
<div style="display:table-cell; text-align: right;"><small><em>${description}</em></small></div>
`
};
return newNoteHint;
}

const buildHints = async (prefix: string) =>{
const response = await context.postMessage({ command: 'getNotes', prefix: prefix });

let hints: Hint[] = [];

if(prefix) {
hints.push(NewNoteHint(prefix, false));
hints.push(NewNoteHint(prefix, true));
}

const notes = response.notes;
for (let i = 0; i < notes.length; i++) {
const note = notes[i];
Expand Down Expand Up @@ -39,6 +74,7 @@ module.exports = {
}
hints.push(hint);
}

return hints;
}

Expand Down
13 changes: 13 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,19 @@ joplin.plugins.register({
});
return { notes: res, showFolders: showFolders };
}
else if(message.command === 'createNote')
{
const activeNote = await joplin.workspace.selectedNote();
const activeNotesFolder = await joplin.data.get(['folders', activeNote.parent_id]);
const newNote = await joplin.data.post(['notes'], null,
{
is_todo: message.todo,
title: message.title,
parent_id: activeNotesFolder.id
});

return {newNote: newNote};
}
});
}
});

0 comments on commit 380281f

Please sign in to comment.