Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ This plugin allows you to create templates in Joplin and use them to create new
- [Advanced Usage](#advanced-usage)
- [Special variables](#special-variables)
- [Default Templates](#default-templates)
- [Using Notebooks or Tags for Templates](#using-notebooks-or-tags-for-templates)
- [Changelog](#changelog)
- [Supporting](#supporting)
- [Contributing](#contributing)
Expand Down Expand Up @@ -174,6 +175,11 @@ This note contains the meeting minutes of the weekly meet held on {{ datetime }}
## Default Templates
You can define the templates you use the most as default templates. Currently you can have two default templates. One for `notes` and one for `to-dos`. You can also assign keyboard shortcuts to these defaults, so that you can quickly create a new note/to-do with the respective default template.

## Using Notebooks to store templates
Now, the plugin also supports using notebooks to store templates instead of tags. You can start using notebooks to store your templates by going to the plugin settings and selecting `Notebook` instead of `Tag`.

Now, any note or todo placed in a notebook titled "Templates" will be considered a template.

## Changelog
See [CHANGELOG.md](https://github.com/joplin/plugin-templates/blob/master/CHANGELOG.md).

Expand Down
15 changes: 14 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,20 @@ joplin.plugins.register({
label: "Apply tags while inserting template",
description: "Apply tags using 'template_tags' variable while inserting template to notes/to-dos.",
section: "templatesPlugin"
}
},
"templatesSource":{
public: true,
type: SettingItemType.String,
isEnum: true,
value: "tag",
options: {
"tag": "Tag",
"notebook": "Notebook"
},
label: "Are templates set with tags or stored in a notebook?",
description: "If set to 'Tag', any note/to-do with a 'template' tag is considered a template. If set to 'Notebook', any note/todo stored in a notebook titled 'Templates' is considered a template.",
section: "templatesPlugin"
},
});


Expand Down
6 changes: 6 additions & 0 deletions src/utils/folders.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import joplin from "api";
import { fetchAllItems } from "./dataApi";
import { Note } from "./templates";

export const getSelectedFolder = async (): Promise<string> => {
const folder = await joplin.workspace.selectedFolder();
Expand All @@ -10,6 +12,10 @@ export const createFolder = async (title: string): Promise<string> => {
return folder.id;
}

export const getAllNotesInFolder = async (title: string): Promise<Note[]> => {
return await fetchAllItems(["search"], { query: `notebook:${title}`, fields: ["id", "title", "body"]})
}

export const doesFolderExist = async (folderId: string): Promise<boolean> => {
try {
await joplin.data.get([ "folders", folderId ], { fields: ["title"] });
Expand Down
14 changes: 11 additions & 3 deletions src/utils/templates.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import joplin from "api";
import { getAllNotesInFolder } from "./folders";
import { getAllNotesWithTag, getAllTagsWithTitle } from "./tags";

export interface Note {
Expand All @@ -25,10 +26,17 @@ const removeDuplicateTemplates = (templates: Note[]) => {

const getAllTemplates = async () => {
let templates: Note[] = [];
const templateTags = await getAllTagsWithTitle("template");

for (const tag of templateTags) {
templates = templates.concat(await getAllNotesWithTag(tag.id));
const templatesSource = await joplin.settings.value("templatesSource");

if (templatesSource == "tag"){
const templateTags = await getAllTagsWithTitle("template");

for (const tag of templateTags) {
templates = templates.concat(await getAllNotesWithTag(tag.id));
}
} else {
templates = templates.concat(await getAllNotesInFolder("Templates"));
}

templates = removeDuplicateTemplates(templates);
Expand Down
3 changes: 2 additions & 1 deletion tests/mock-joplin-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ export default {
},
settings: {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
globalValue: async (setting: string): Promise<string> => { return ""; }
globalValue: async (setting: string): Promise<string> => { return ""; },
value: async (setting: string): Promise<string> => { return ""; }
},
require: (): unknown => { return ""; }
};
6 changes: 6 additions & 0 deletions tests/utils/templates.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@ describe("Get user template selection", () => {
}
});

jest.spyOn(joplin.settings, "value").mockImplementation(async (setting: string) => {
if (setting === "templatesSource") {
return "tag";
}
});

const expectTemplatesSelector = (templates: DropdownOption[], selectedValue: DropdownOption) => {
jest.spyOn(joplin.commands, "execute").mockImplementation(async (cmd: string, props: Record<string, unknown>) => {
expect(cmd).toEqual("showPrompt");
Expand Down