Skip to content

Commit 388e40e

Browse files
committed
feat: add a dialog for showing default templates
1 parent 7b64870 commit 388e40e

6 files changed

Lines changed: 56 additions & 18 deletions

File tree

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"dist": "webpack --joplin-plugin-config buildMain && webpack --joplin-plugin-config buildExtraScripts && webpack --joplin-plugin-config createArchive",
66
"prepare": "npm run dist",
77
"update": "npm install -g generator-joplin && yo joplin --update",
8-
"lint": "eslint 'src/**/*'"
8+
"lint": "eslint 'src/**/*.{ts,json}'"
99
},
1010
"license": "MIT",
1111
"keywords": [

src/index.ts

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { SettingItemType } from "api/types";
33
import { parseTemplate } from "./parser";
44
import { doesFolderExist } from "./utils/folders";
55
import { getTemplateFromId, getUserTempateSelection } from "./utils/templates";
6+
import { setDefaultTemplatesView } from "./views/defaultTemplates";
67

78
joplin.plugins.register({
89
onStart: async function() {
@@ -42,6 +43,10 @@ joplin.plugins.register({
4243
}
4344

4445

46+
// Register a dialog box
47+
const dialogViewHandle = await joplin.views.dialogs.create("dialog");
48+
49+
4550
// Register all commands
4651
await joplin.commands.register({
4752
name: "createNoteFromTemplate",
@@ -80,7 +85,14 @@ joplin.plugins.register({
8085
name: "showDefaultTemplates",
8186
label: "Show default templates",
8287
execute: async () => {
83-
console.log("Command");
88+
const noteTemplate = await getTemplateFromId(await joplin.settings.value("defaultNoteTemplateId"));
89+
const todoTemplate = await getTemplateFromId(await joplin.settings.value("defaultTodoTemplateId"));
90+
91+
const noteTemplateTitle = noteTemplate ? noteTemplate.title : null;
92+
const todoTemplateTitle = todoTemplate ? todoTemplate.title : null;
93+
94+
await setDefaultTemplatesView(dialogViewHandle, noteTemplateTitle, todoTemplateTitle);
95+
await joplin.views.dialogs.open(dialogViewHandle);
8496
}
8597
});
8698

@@ -112,13 +124,10 @@ joplin.plugins.register({
112124
name: "createNoteFromDefaultTemplate",
113125
label: "Create note from default template",
114126
execute: async () => {
115-
const templateId = await joplin.settings.value("defaultNoteTemplateId");
116-
if (templateId) {
117-
const template = await getTemplateFromId(templateId)
118-
if (template) {
119-
await joplin.commands.execute("newNote", await parseTemplate(template.body));
120-
return;
121-
}
127+
const template = await getTemplateFromId(await joplin.settings.value("defaultNoteTemplateId"));
128+
if (template) {
129+
await joplin.commands.execute("newNote", await parseTemplate(template.body));
130+
return;
122131
}
123132
await joplin.views.dialogs.showMessageBox("No default note template is set.");
124133
}
@@ -128,13 +137,10 @@ joplin.plugins.register({
128137
name: "createTodoFromDefaultTemplate",
129138
label: "Create to-do from default template",
130139
execute: async () => {
131-
const templateId = await joplin.settings.value("defaultTodoTemplateId");
132-
if (templateId) {
133-
const template = await getTemplateFromId(templateId)
134-
if (template) {
135-
await joplin.commands.execute("newTodo", await parseTemplate(template.body));
136-
return;
137-
}
140+
const template = await getTemplateFromId(await joplin.settings.value("defaultTodoTemplateId"));
141+
if (template) {
142+
await joplin.commands.execute("newTodo", await parseTemplate(template.body));
143+
return;
138144
}
139145
await joplin.views.dialogs.showMessageBox("No default to-do template is set.");
140146
}

src/utils/folders.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ export interface Note {
1515
body: string;
1616
}
1717

18-
export const getAllNotesInFolder = async (folderId: string): Promise <Note[]> => {
18+
export const getAllNotesInFolder = async (folderId: string): Promise<Note[]> => {
1919
let pageNum = 1;
2020
let response;
2121
let notes = [];

src/utils/templates.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,11 @@ export const getUserTempateSelection = async (templatesFolderId: string, propert
3131
return null;
3232
}
3333

34-
export const getTemplateFromId = async (templateId: string): Promise<Note | null> => {
34+
export const getTemplateFromId = async (templateId: string | null): Promise<Note | null> => {
35+
if (!templateId) {
36+
return null;
37+
}
38+
3539
try {
3640
return await joplin.data.get([ "notes", templateId ], { fields: ["id", "title", "body"] });
3741
} catch (error) {

src/views/defaultTemplates.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import joplin from "api";
2+
3+
export const setDefaultTemplatesView = async (viewHandle: string, noteTemplate: string | null, todoTemplate: string | null): Promise<void> => {
4+
await joplin.views.dialogs.addScript(viewHandle, "./views/webview.css");
5+
6+
await joplin.views.dialogs.setHtml(
7+
viewHandle,
8+
`
9+
<h2> Default Templates </h2>
10+
<table>
11+
<tr>
12+
<td><u> Note </u></td>
13+
<td>${noteTemplate ? noteTemplate : "<i>Not set</i>"}</td>
14+
</tr>
15+
<tr>
16+
<td><u> To-do </u></td>
17+
<td>${todoTemplate ? todoTemplate : "<i>Not set</i>"}</td>
18+
</tr>
19+
</table>
20+
`
21+
);
22+
23+
await joplin.views.dialogs.setButtons(viewHandle, [{ id: "ok" }]);
24+
}

src/views/webview.css

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
body {
2+
text-align: center;
3+
font-size: 14px;
4+
}

0 commit comments

Comments
 (0)