Skip to content

Commit 3399aea

Browse files
committed
fix: don't create provisional notes/todos while using templates
1 parent 7dfcdf2 commit 3399aea

5 files changed

Lines changed: 54 additions & 16 deletions

File tree

.eslintrc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
"plugin:@typescript-eslint/recommended"
99
],
1010
"rules": {
11-
"indent": ["error", 4],
11+
"indent": ["error", 4, { "SwitchCase": 1 }],
1212
"quotes": ["error", "double"],
1313
"eol-last": ["error", "always"]
1414
}

src/actions.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import joplin from "api";
2+
import { getSelectedFolder } from "./utils/folders";
3+
4+
export enum TemplateAction {
5+
NewNote = "newNote",
6+
NewTodo = "newTodo",
7+
InsertText = "insertText"
8+
}
9+
10+
const performInsertTextAction = async (template: string) => {
11+
await joplin.commands.execute("insertText", template);
12+
}
13+
14+
const performNewNoteAction = async (template: string) => {
15+
const currentFolder = await getSelectedFolder();
16+
const note = await joplin.data.post(["notes"], null, { body: template, parent_id: currentFolder });
17+
await joplin.commands.execute("openNote", note.id);
18+
}
19+
20+
const performNewTodoAction = async (template: string) => {
21+
const currentFolder = await getSelectedFolder();
22+
const note = await joplin.data.post(["notes"], null, { body: template, parent_id: currentFolder, is_todo: 1 });
23+
await joplin.commands.execute("openNote", note.id);
24+
}
25+
26+
export const performAction = async (action: TemplateAction, template: string): Promise<void> => {
27+
switch (action) {
28+
case TemplateAction.InsertText:
29+
await performInsertTextAction(template);
30+
break;
31+
case TemplateAction.NewNote:
32+
await performNewNoteAction(template);
33+
break;
34+
case TemplateAction.NewTodo:
35+
await performNewTodoAction(template);
36+
break;
37+
}
38+
}

src/index.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { Parser } from "./parser";
44
import { DateAndTimeUtils } from "./utils/dateAndTime";
55
import { getTemplateFromId, getUserTemplateSelection, Note } from "./utils/templates";
66
import { setDefaultTemplatesView } from "./views/defaultTemplates";
7-
import { JoplinCommand } from "./types";
7+
import { TemplateAction, performAction } from "./actions";
88
import { loadLegacyTemplates } from "./legacyTemplates";
99
import * as open from "open";
1010

@@ -44,16 +44,16 @@ joplin.plugins.register({
4444

4545

4646
// Utility Functions
47-
const executeCommandWithParsedTemplate = async (command: JoplinCommand, template: Note | null) => {
47+
const performActionWithParsedTemplate = async (action: TemplateAction, template: Note | null) => {
4848
const parsedTemplate = await parser.parseTemplate(template);
4949
if (parsedTemplate) {
50-
await joplin.commands.execute(command, parsedTemplate);
50+
await performAction(action, parsedTemplate);
5151
}
5252
}
5353

54-
const getTemplateAndExecuteCommand = async (command: JoplinCommand) => {
54+
const getTemplateAndPerformAction = async (action: TemplateAction) => {
5555
const template: Note = JSON.parse(await getUserTemplateSelection());
56-
await executeCommandWithParsedTemplate(command, template);
56+
await performActionWithParsedTemplate(action, template);
5757
}
5858

5959

@@ -62,23 +62,23 @@ joplin.plugins.register({
6262
name: "createNoteFromTemplate",
6363
label: "Create note from template",
6464
execute: async () => {
65-
await getTemplateAndExecuteCommand(JoplinCommand.NewNote);
65+
await getTemplateAndPerformAction(TemplateAction.NewNote);
6666
}
6767
});
6868

6969
await joplin.commands.register({
7070
name: "createTodoFromTemplate",
7171
label: "Create to-do from template",
7272
execute: async () => {
73-
await getTemplateAndExecuteCommand(JoplinCommand.NewTodo);
73+
await getTemplateAndPerformAction(TemplateAction.NewTodo);
7474
}
7575
});
7676

7777
await joplin.commands.register({
7878
name: "insertTemplate",
7979
label: "Insert template",
8080
execute: async () => {
81-
await getTemplateAndExecuteCommand(JoplinCommand.InsertText);
81+
await getTemplateAndPerformAction(TemplateAction.InsertText);
8282
}
8383
});
8484

@@ -127,7 +127,7 @@ joplin.plugins.register({
127127
execute: async () => {
128128
const template = await getTemplateFromId(await joplin.settings.value("defaultNoteTemplateId"));
129129
if (template) {
130-
return await executeCommandWithParsedTemplate(JoplinCommand.NewNote, template);
130+
return await performActionWithParsedTemplate(TemplateAction.NewNote, template);
131131
}
132132
await joplin.views.dialogs.showMessageBox("No default note template is set.");
133133
}
@@ -139,7 +139,7 @@ joplin.plugins.register({
139139
execute: async () => {
140140
const template = await getTemplateFromId(await joplin.settings.value("defaultTodoTemplateId"));
141141
if (template) {
142-
return await executeCommandWithParsedTemplate(JoplinCommand.NewTodo, template);
142+
return await performActionWithParsedTemplate(TemplateAction.NewTodo, template);
143143
}
144144
await joplin.views.dialogs.showMessageBox("No default to-do template is set.");
145145
}

src/types.ts

Lines changed: 0 additions & 5 deletions
This file was deleted.

src/utils/folders.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
import joplin from "api";
22

3+
export const getSelectedFolder = async (): Promise<string> => {
4+
const folder = await joplin.workspace.selectedFolder();
5+
return folder.id;
6+
}
7+
38
export const createFolder = async (title: string): Promise<string> => {
49
const folder = await joplin.data.post(["folders"], null, { title: title });
510
return folder.id;

0 commit comments

Comments
 (0)