Skip to content

Commit 5612f36

Browse files
committed
feat: display template name in variable input dialog
1 parent 1a8e0f4 commit 5612f36

4 files changed

Lines changed: 23 additions & 14 deletions

File tree

src/index.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import joplin from "api";
22
import { SettingItemType } from "api/types";
33
import { Parser } from "./parser";
44
import { DateAndTimeUtils } from "./utils/dateAndTime";
5-
import { doesFolderExist } from "./utils/folders";
5+
import { doesFolderExist, Note } from "./utils/folders";
66
import { getTemplateFromId, getUserTemplateSelection } from "./utils/templates";
77
import { setDefaultTemplatesView } from "./views/defaultTemplates";
88
import { JoplinCommand } from "./types";
@@ -50,15 +50,15 @@ joplin.plugins.register({
5050

5151

5252
// Utility Functions
53-
const executeCommandWithParsedTemplate = async (command: JoplinCommand, template: string | null) => {
53+
const executeCommandWithParsedTemplate = async (command: JoplinCommand, template: Note | null) => {
5454
const parsedTemplate = await parser.parseTemplate(template);
5555
if (parsedTemplate) {
5656
await joplin.commands.execute(command, parsedTemplate);
5757
}
5858
}
5959

6060
const getTemplateAndExecuteCommand = async (command: JoplinCommand) => {
61-
const template = await getUserTemplateSelection(templatesFolderId);
61+
const template: Note = JSON.parse(await getUserTemplateSelection(templatesFolderId));
6262
await executeCommandWithParsedTemplate(command, template);
6363
}
6464

@@ -133,7 +133,7 @@ joplin.plugins.register({
133133
execute: async () => {
134134
const template = await getTemplateFromId(await joplin.settings.value("defaultNoteTemplateId"));
135135
if (template) {
136-
return await executeCommandWithParsedTemplate(JoplinCommand.NewNote, template.body);
136+
return await executeCommandWithParsedTemplate(JoplinCommand.NewNote, template);
137137
}
138138
await joplin.views.dialogs.showMessageBox("No default note template is set.");
139139
}
@@ -145,7 +145,7 @@ joplin.plugins.register({
145145
execute: async () => {
146146
const template = await getTemplateFromId(await joplin.settings.value("defaultTodoTemplateId"));
147147
if (template) {
148-
return await executeCommandWithParsedTemplate(JoplinCommand.NewTodo, template.body);
148+
return await executeCommandWithParsedTemplate(JoplinCommand.NewTodo, template);
149149
}
150150
await joplin.views.dialogs.showMessageBox("No default to-do template is set.");
151151
}

src/parser.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import joplin from "api";
22
import * as Handlebars from "handlebars/dist/handlebars";
33
import { DateAndTimeUtils } from "./utils/dateAndTime";
4+
import { Note } from "./utils/folders";
45
import { setTemplateVariablesView } from "./views/templateVariables";
56

67
// Can't use import for this library because the types in the library
@@ -55,12 +56,12 @@ export class Parser {
5556
return variableValues;
5657
}
5758

58-
private async getVariableInputs(variables: Record<string, string>) {
59+
private async getVariableInputs(title: string, variables: Record<string, string>) {
5960
if (Object.keys(variables).length == 0) {
6061
return {};
6162
}
6263

63-
await setTemplateVariablesView(this.dialog, variables);
64+
await setTemplateVariablesView(this.dialog, title, variables);
6465
const dialogResponse = (await joplin.views.dialogs.open(this.dialog));
6566

6667
if (dialogResponse.id === "cancel") {
@@ -71,16 +72,16 @@ export class Parser {
7172
return this.mapUserResponseToVariables(variables, userResponse);
7273
}
7374

74-
public async parseTemplate(template: string | null): Promise<string | null> {
75+
public async parseTemplate(template: Note | null): Promise<string | null> {
7576
if (!template) {
7677
return null;
7778
}
7879

7980
try {
80-
const processedTemplate = frontmatter(template);
81+
const processedTemplate = frontmatter(template.body);
8182
const templateVariables = processedTemplate.attributes;
8283

83-
const variableInputs = await this.getVariableInputs(templateVariables);
84+
const variableInputs = await this.getVariableInputs(template.title, templateVariables);
8485
if (variableInputs === null) {
8586
return null;
8687
}

src/utils/templates.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,20 @@ import { getAllNotesInFolder, Note } from "./folders";
33

44
type NoteProperty = "body" | "id" | "title";
55

6-
export const getUserTemplateSelection = async (templatesFolderId: string, property: NoteProperty = "body"): Promise<string | null> => {
6+
export const getUserTemplateSelection = async (templatesFolderId: string, property?: NoteProperty): Promise<string | null> => {
77
const templates = await getAllNotesInFolder(templatesFolderId);
88
const templateOptions = templates.map(note => {
9+
let optionValue;
10+
11+
if (!property) {
12+
optionValue = JSON.stringify(note);
13+
} else {
14+
optionValue = note[property];
15+
}
16+
917
return {
1018
label: note.title,
11-
value: note[property]
19+
value: optionValue
1220
};
1321
});
1422

src/views/templateVariables.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ const getVariableHtml = (variable: string, type: string): string => {
7676
return `<div class="invalidVariable"><i>${encode(variable)} has an invalid type.</i></div>`;
7777
}
7878

79-
export const setTemplateVariablesView = async (viewHandle: string, variables: Record<string, string>): Promise<void> => {
79+
export const setTemplateVariablesView = async (viewHandle: string, title: string, variables: Record<string, string>): Promise<void> => {
8080
await joplin.views.dialogs.addScript(viewHandle, "./views/webview.css");
8181

8282
const variablesFormInputHtml = Object.keys(variables).map(variable => {
@@ -86,7 +86,7 @@ export const setTemplateVariablesView = async (viewHandle: string, variables: Re
8686
await joplin.views.dialogs.setHtml(
8787
viewHandle,
8888
`
89-
<h2> Template variables </h2>
89+
<h2> ${encode(title)} </h2>
9090
<form name="variables">
9191
${variablesFormInputHtml.join("")}
9292
</form>

0 commit comments

Comments
 (0)