Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added ablity to create a note if required. #5

Merged
merged 3 commits into from
Apr 25, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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', title: 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(response.allowNewNotes && 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
42 changes: 38 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,23 @@ import { ContentScriptType, SettingItem, SettingItemType } from 'api/types';
const NUM_RESULTS = 21;
const FOLDERS_REFRESH_INTERVAL = 6000;
const SETTING_SHOW_FOLDERS = 'showFolders';
const SETTING_ALLOW_NEW_NOTES = 'allowNewNotes';

let showFolders = false;
let allowNewNotes = false;
let folders = {};

async function onShowFolderSettingChanged() {
showFolders = await joplin.settings.value(SETTING_SHOW_FOLDERS);
if (showFolders) {
refreshFolderList();
await refreshFolderList();
}
}

async function onAllowNewNotesSettingChanged() {
allowNewNotes = await joplin.settings.value(SETTING_ALLOW_NEW_NOTES);
}

async function refreshFolderList() {
folders = await getFolders();
setTimeout(() => {
Expand Down Expand Up @@ -72,13 +78,28 @@ async function initSettings() {
label: 'Show Notebooks',
} as SettingItem);

await joplin.settings.registerSetting(SETTING_ALLOW_NEW_NOTES, {
public: true,
section: SECTION,
type: SettingItemType.Bool,
value: allowNewNotes,
label: 'Allow new Notes',
} as SettingItem);

await onShowFolderSettingChanged();

await onAllowNewNotesSettingChanged();

await joplin.settings.onChange(change => {
const idx = change.keys.indexOf(SETTING_SHOW_FOLDERS);
if (idx >= 0) {
const showFoldersIdx = change.keys.indexOf(SETTING_SHOW_FOLDERS);
if (showFoldersIdx >= 0) {
onShowFolderSettingChanged();
}

const allowNewNotesIdx = change.keys.indexOf(SETTING_ALLOW_NEW_NOTES);
if (allowNewNotesIdx >= 0) {
onAllowNewNotesSettingChanged();
}
});
}

Expand All @@ -105,7 +126,20 @@ joplin.plugins.register({
folder: folders[n.parent_id],
};
});
return { notes: res, showFolders: showFolders };
return { notes: res, showFolders: showFolders, allowNewNotes: allowNewNotes};
}
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};
}
});
}
Expand Down