Skip to content

Commit 3ec3f98

Browse files
committed
feat: add util functions for user template selection
1 parent ddfd51a commit 3ec3f98

5 files changed

Lines changed: 89 additions & 11 deletions

File tree

src/index.ts

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
import joplin from "api";
22
import { SettingItemType } from "api/types";
3-
import { doesFolderExist } from './utils';
3+
// import { parseTempalte } from "./parser";
4+
import { doesFolderExist } from "./utils/folders";
5+
import { getUserTempateSelection } from "./utils/templates";
46

57
joplin.plugins.register({
68
onStart: async function() {
9+
// Register all settings
710
// TODO: The settings name i.e. "templatesFolderId" in this case should be stored in a variable.
811
await joplin.settings.registerSettings({
912
"templatesFolderId": {
@@ -19,5 +22,27 @@ joplin.plugins.register({
1922
const folder = await joplin.data.post(["folders"], null, { title: "Templates" });
2023
await joplin.settings.setValue("templatesFolderId", folder.id);
2124
}
25+
26+
// Register all commands
27+
await joplin.commands.register({
28+
name: "createNoteFromTemplate",
29+
label: "Create note from template",
30+
execute: async () => {
31+
/*
32+
1. Get all templates with ids and titles.
33+
2. Ask user to select a template.
34+
3. Parse the template to get markdown.
35+
4. Execute newNote command with that markdown.
36+
*/
37+
console.log(await getUserTempateSelection(templatesFolderId));
38+
}
39+
});
40+
41+
// Create templates menu
42+
await joplin.views.menus.create("templates", "Templates", [
43+
{
44+
commandName: "createNoteFromTemplate"
45+
}
46+
]);
2247
},
2348
});

src/parser.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export const parseTempalte = (template: string): string => {
2+
return template;
3+
}

src/utils.ts

Lines changed: 0 additions & 10 deletions
This file was deleted.

src/utils/folders.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import joplin from "api";
2+
3+
export const doesFolderExist = async (folderId: string): Promise<boolean> => {
4+
try {
5+
await joplin.data.get([ "folders", folderId ], { fields: ["title"] });
6+
return true;
7+
} catch (error) {
8+
return false;
9+
}
10+
};
11+
12+
interface Note {
13+
id: string;
14+
title: string;
15+
body: string;
16+
}
17+
18+
export const getAllNotesInFolder = async (folderId: string): Promise <Note[]> => {
19+
let pageNum = 1;
20+
let response;
21+
let notes = [];
22+
23+
do {
24+
response = await joplin.data.get(["folders", folderId, "notes"], { fields: ["id", "title", "body"], page: pageNum });
25+
notes = notes.concat(response.items);
26+
pageNum++;
27+
} while (response.has_more);
28+
29+
return notes;
30+
}

src/utils/templates.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import joplin from "api";
2+
import { getAllNotesInFolder } from "./folders";
3+
4+
export const getUserTempateSelection = async (templatesFolderId: string): Promise<string | null> => {
5+
const templates = await getAllNotesInFolder(templatesFolderId);
6+
const templateOptions = templates.map(note => {
7+
return {
8+
label: note.title,
9+
value: note.body
10+
};
11+
});
12+
13+
if (!templateOptions.length) {
14+
await joplin.views.dialogs.showMessageBox("No templates found! Please create a template and try again.");
15+
return null;
16+
}
17+
18+
const { answer } = await joplin.commands.execute("showPrompt", {
19+
label: "Template:",
20+
inputType: "dropdown",
21+
value: templateOptions[0],
22+
autocomplete: templateOptions
23+
});
24+
25+
if (answer) {
26+
return answer.value;
27+
}
28+
29+
return null;
30+
}

0 commit comments

Comments
 (0)