Skip to content

Commit 1a8e0f4

Browse files
committed
refactor: create utility functions for redundant tasks
1 parent a56206a commit 1a8e0f4

3 files changed

Lines changed: 30 additions & 32 deletions

File tree

src/index.ts

Lines changed: 20 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { DateAndTimeUtils } from "./utils/dateAndTime";
55
import { doesFolderExist } from "./utils/folders";
66
import { getTemplateFromId, getUserTemplateSelection } from "./utils/templates";
77
import { setDefaultTemplatesView } from "./views/defaultTemplates";
8+
import { JoplinCommand } from "./types";
89

910
joplin.plugins.register({
1011
onStart: async function() {
@@ -48,46 +49,42 @@ joplin.plugins.register({
4849
const parser = new Parser(dateAndTimeUtils, dialogViewHandle);
4950

5051

52+
// Utility Functions
53+
const executeCommandWithParsedTemplate = async (command: JoplinCommand, template: string | null) => {
54+
const parsedTemplate = await parser.parseTemplate(template);
55+
if (parsedTemplate) {
56+
await joplin.commands.execute(command, parsedTemplate);
57+
}
58+
}
59+
60+
const getTemplateAndExecuteCommand = async (command: JoplinCommand) => {
61+
const template = await getUserTemplateSelection(templatesFolderId);
62+
await executeCommandWithParsedTemplate(command, template);
63+
}
64+
65+
5166
// Register all commands
5267
await joplin.commands.register({
5368
name: "createNoteFromTemplate",
5469
label: "Create note from template",
5570
execute: async () => {
56-
const template = await getUserTemplateSelection(templatesFolderId);
57-
if (template) {
58-
const parsedTemplate = await parser.parseTemplate(template);
59-
if (parsedTemplate) {
60-
await joplin.commands.execute("newNote", parsedTemplate);
61-
}
62-
}
71+
await getTemplateAndExecuteCommand(JoplinCommand.NewNote);
6372
}
6473
});
6574

6675
await joplin.commands.register({
6776
name: "createTodoFromTemplate",
6877
label: "Create to-do from template",
6978
execute: async () => {
70-
const template = await getUserTemplateSelection(templatesFolderId);
71-
if (template) {
72-
const parsedTemplate = await parser.parseTemplate(template);
73-
if (parsedTemplate) {
74-
await joplin.commands.execute("newTodo", parsedTemplate);
75-
}
76-
}
79+
await getTemplateAndExecuteCommand(JoplinCommand.NewTodo);
7780
}
7881
});
7982

8083
await joplin.commands.register({
8184
name: "insertTemplate",
8285
label: "Insert template",
8386
execute: async () => {
84-
const template = await getUserTemplateSelection(templatesFolderId);
85-
if (template) {
86-
const parsedTemplate = await parser.parseTemplate(template);
87-
if (parsedTemplate) {
88-
await joplin.commands.execute("insertText", parsedTemplate);
89-
}
90-
}
87+
await getTemplateAndExecuteCommand(JoplinCommand.InsertText);
9188
}
9289
});
9390

@@ -136,11 +133,7 @@ joplin.plugins.register({
136133
execute: async () => {
137134
const template = await getTemplateFromId(await joplin.settings.value("defaultNoteTemplateId"));
138135
if (template) {
139-
const parsedTemplate = await parser.parseTemplate(template.body);
140-
if (parsedTemplate) {
141-
await joplin.commands.execute("newNote", parsedTemplate);
142-
}
143-
return;
136+
return await executeCommandWithParsedTemplate(JoplinCommand.NewNote, template.body);
144137
}
145138
await joplin.views.dialogs.showMessageBox("No default note template is set.");
146139
}
@@ -152,11 +145,7 @@ joplin.plugins.register({
152145
execute: async () => {
153146
const template = await getTemplateFromId(await joplin.settings.value("defaultTodoTemplateId"));
154147
if (template) {
155-
const parsedTemplate = await parser.parseTemplate(template.body);
156-
if (parsedTemplate) {
157-
await joplin.commands.execute("newTodo", parsedTemplate);
158-
}
159-
return;
148+
return await executeCommandWithParsedTemplate(JoplinCommand.NewTodo, template.body);
160149
}
161150
await joplin.views.dialogs.showMessageBox("No default to-do template is set.");
162151
}

src/parser.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,11 @@ export class Parser {
7171
return this.mapUserResponseToVariables(variables, userResponse);
7272
}
7373

74-
public async parseTemplate(template: string): Promise<string> {
74+
public async parseTemplate(template: string | null): Promise<string | null> {
75+
if (!template) {
76+
return null;
77+
}
78+
7579
try {
7680
const processedTemplate = frontmatter(template);
7781
const templateVariables = processedTemplate.attributes;

src/types.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export enum JoplinCommand {
2+
NewNote = "newNote",
3+
NewTodo = "newTodo",
4+
InsertText = "insertText"
5+
}

0 commit comments

Comments
 (0)