Skip to content

Commit 5396a4a

Browse files
committed
fix: allow users to set an existing notebook as the templates notebook
1 parent beb6d63 commit 5396a4a

5 files changed

Lines changed: 100 additions & 12 deletions

File tree

src/index.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ joplin.plugins.register({
3333

3434

3535
// Global variables
36-
const templatesFolderId = await getTemplatesFolderId();
3736
const dialogViewHandle = await joplin.views.dialogs.create("dialog");
37+
let templatesFolderId = await getTemplatesFolderId(dialogViewHandle);
3838

3939
const userLocale = await joplin.settings.globalValue("locale");
4040
const userDateFormat = await joplin.settings.globalValue("dateFormat");
@@ -145,6 +145,14 @@ joplin.plugins.register({
145145
}
146146
});
147147

148+
await joplin.commands.register({
149+
name: "changeTemplatesFolder",
150+
label: "Change templates notebook",
151+
execute: async () => {
152+
templatesFolderId = await getTemplatesFolderId(dialogViewHandle, true);
153+
}
154+
});
155+
148156

149157
// Create templates menu
150158
await joplin.views.menus.create("templates", "Templates", [
@@ -179,6 +187,9 @@ joplin.plugins.register({
179187
accelerator: "Alt+Shift+T"
180188
}
181189
]
190+
},
191+
{
192+
commandName: "changeTemplatesFolder"
182193
}
183194
]);
184195
},

src/utils/folders.ts

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

3+
export interface Note {
4+
id: string;
5+
title: string;
6+
body: string;
7+
}
8+
9+
export interface Folder {
10+
id: string;
11+
title: string;
12+
}
13+
314
export const doesFolderExist = async (folderId: string): Promise<boolean> => {
415
try {
516
await joplin.data.get([ "folders", folderId ], { fields: ["title"] });
@@ -9,12 +20,6 @@ export const doesFolderExist = async (folderId: string): Promise<boolean> => {
920
}
1021
};
1122

12-
export interface Note {
13-
id: string;
14-
title: string;
15-
body: string;
16-
}
17-
1823
export const getAllNotesInFolder = async (folderId: string): Promise<Note[]> => {
1924
let pageNum = 1;
2025
let response;
@@ -28,3 +33,17 @@ export const getAllNotesInFolder = async (folderId: string): Promise<Note[]> =>
2833

2934
return notes;
3035
}
36+
37+
export const getAllFolders = async (): Promise<Folder[]> => {
38+
let pageNum = 1;
39+
let response;
40+
let folders = [];
41+
42+
do {
43+
response = await joplin.data.get(["folders"], { fields: ["id", "title"], page: pageNum });
44+
folders = folders.concat(response.items);
45+
pageNum++;
46+
} while (response.has_more);
47+
48+
return folders;
49+
}

src/utils/templates.ts

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,36 @@
11
import joplin from "api";
22
import { doesFolderExist, getAllNotesInFolder, Note } from "./folders";
33
import { getAllNotesWithTag, getTagWithTitle } from "./tags";
4+
import { setTemplatesFolderView } from "../views/templatesFolder";
45

56
type NoteProperty = "body" | "id" | "title";
7+
const WELCOME_MESSAGE = (
8+
`Thanks for downloading the templates plugin. This plugin allows you to create and use templates in the Joplin desktop application. Please read the following instructions very carefully to get started.
69
7-
export const getTemplatesFolderId = async (): Promise<string> => {
10+
1. You can see the menu for this plugin under the tools category in the menu bar. To read the complete plugin documentation you can click on the help option in the menu itself.
11+
2. To use this plugin it is mandatory to have a templates notebook i.e. a notebook dedicated to storing templates. After you close this popup you'll be presented with another dialog to select or create a new templates notebook.
12+
3. If you used the native templates feature, your templates will be automatically loaded by this plugin into your templates notebook.`
13+
);
14+
15+
export const getTemplatesFolderId = async (dialogViewHandle: string, changeFolder = false): Promise<string> => {
816
const templatesFolderId = await joplin.settings.value("templatesFolderId");
917

10-
if (templatesFolderId == null || !(await doesFolderExist(templatesFolderId))) {
11-
const folder = await joplin.data.post(["folders"], null, { title: "Templates" });
12-
await joplin.settings.setValue("templatesFolderId", folder.id);
13-
return folder.id;
18+
if (templatesFolderId == null || !(await doesFolderExist(templatesFolderId)) || changeFolder) {
19+
if (!changeFolder) await joplin.views.dialogs.showMessageBox(WELCOME_MESSAGE);
20+
21+
await setTemplatesFolderView(dialogViewHandle);
22+
const dialogResponse = await joplin.views.dialogs.open(dialogViewHandle);
23+
24+
const newTemplatesFolderId = dialogResponse.formData.folders.folder;
25+
26+
if (newTemplatesFolderId === "new") {
27+
const folder = await joplin.data.post(["folders"], null, { title: "Templates" });
28+
await joplin.settings.setValue("templatesFolderId", folder.id);
29+
return folder.id;
30+
}
31+
32+
await joplin.settings.setValue("templatesFolderId", newTemplatesFolderId);
33+
return newTemplatesFolderId;
1434
}
1535

1636
return templatesFolderId;

src/views/templatesFolder.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import joplin from "api";
2+
import { encode } from "html-entities";
3+
import { getAllFolders } from "../utils/folders";
4+
5+
export const setTemplatesFolderView = async (viewHandle: string): Promise<void> => {
6+
await joplin.views.dialogs.addScript(viewHandle, "./views/webview.css");
7+
8+
const allFolders = await getAllFolders();
9+
const folderOptionsHtml = allFolders.map(folder => {
10+
return `<option value="${encode(folder.id)}">${encode(folder.title)}</option>`;
11+
}).join("");
12+
13+
await joplin.views.dialogs.setHtml(
14+
viewHandle,
15+
`
16+
<h2> Templates notebook </h2>
17+
<form name="folders">
18+
<div>
19+
<div class="formLabel">
20+
Choose an existing notebook or create a new one
21+
</div>
22+
<div>
23+
<select name="folder">
24+
<option value="new">Create a new notebook</option>
25+
${folderOptionsHtml}
26+
</select>
27+
</div>
28+
</div>
29+
</form>
30+
`
31+
);
32+
33+
await joplin.views.dialogs.setButtons(viewHandle, [{ id: "ok" }]);
34+
}

src/views/webview.css

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ body {
1616
padding: 2px;
1717
}
1818

19+
.formLabel {
20+
margin-bottom: 5px;
21+
}
22+
1923
input, select {
2024
height: 25px;
2125
font-size: 16px;

0 commit comments

Comments
 (0)