|
1 | 1 | import joplin from "api"; |
2 | 2 | import { doesFolderExist, getAllNotesInFolder, Note } from "./folders"; |
3 | 3 | import { getAllNotesWithTag, getTagWithTitle } from "./tags"; |
| 4 | +import { setTemplatesFolderView } from "../views/templatesFolder"; |
4 | 5 |
|
5 | 6 | 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. |
6 | 9 |
|
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> => { |
8 | 16 | const templatesFolderId = await joplin.settings.value("templatesFolderId"); |
9 | 17 |
|
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; |
14 | 34 | } |
15 | 35 |
|
16 | 36 | return templatesFolderId; |
|
0 commit comments