Skip to content

Commit

Permalink
feat(#51): add support for clearing defaults for a notebook and misc …
Browse files Browse the repository at this point in the history
…improvements
  • Loading branch information
nishantwrp committed Sep 15, 2024
1 parent f021ffc commit d11b2c4
Show file tree
Hide file tree
Showing 3 changed files with 129 additions and 101 deletions.
41 changes: 28 additions & 13 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ import { MenuItemLocation, SettingItemType } from "api/types";
import { Parser } from "./parser";
import { DateAndTimeUtils } from "./utils/dateAndTime";
import { getFolderFromId, getSelectedFolder, getUserFolderSelection, Folder } from "./utils/folders";
import { getTemplateFromId, getUserDefaultTemplateTypeSelection, getUserTemplateSelection, setDefaultTemplate, Note, DefaultTemplatesConfigSetting } from "./utils/templates";
import { clearDefaultTemplates, getNotebookDefaultTemplatesConfig, getUserDefaultTemplateTypeSelection, setDefaultTemplate, DefaultTemplatesConfigSetting } from "./utils/defaultTemplates";
import { getTemplateFromId, getUserTemplateSelection, Note } from "./utils/templates";
import { setDefaultTemplatesView, DefaultTemplatesDisplayData, NotebookDefaultTemplatesDisplayData } from "./views/defaultTemplates";
import { TemplateAction, performAction } from "./actions";
import { loadLegacyTemplates } from "./legacyTemplates";
Expand Down Expand Up @@ -109,8 +110,11 @@ joplin.plugins.register({
promiseGroup.set("todoTemplate", getTemplateFromId(defaultTemplateTodoId));
const { notebook, noteTemplate, todoTemplate } = await promiseGroup.groupAll();

// TODO: We can remove the deleted notebooks from settings storage.
if (notebook === null) return null;
if (notebook === null || (noteTemplate === null && todoTemplate === null)) {
// Async remove of the obsolete config
clearDefaultTemplates(notebookId);
return null;
}
return {
notebookTitle: notebook.title,
defaultNoteTemplateTitle: noteTemplate ? noteTemplate.title : null,
Expand Down Expand Up @@ -160,9 +164,7 @@ joplin.plugins.register({
execute: async () => {
const noteTemplate = await getTemplateFromId(await joplin.settings.value("defaultNoteTemplateId"));
const todoTemplate = await getTemplateFromId(await joplin.settings.value("defaultTodoTemplateId"));

let defaultTemplatesConfig: DefaultTemplatesConfigSetting | null = await joplin.settings.value("defaultTemplatesConfig");
if (defaultTemplatesConfig === null) defaultTemplatesConfig = {};
const defaultTemplatesConfig = await getNotebookDefaultTemplatesConfig();

const globalDefaultTemplates: DefaultTemplatesDisplayData = {
defaultNoteTemplateTitle: noteTemplate ? noteTemplate.title : null,
Expand Down Expand Up @@ -208,16 +210,27 @@ joplin.plugins.register({
}
}));

joplinCommands.add(joplin.commands.register({
name: "clearDefaultTemplatesForNotebook",
label: "Clear default templates for notebook",
execute: async () => {
const folder: Folder | null = JSON.parse(await getUserFolderSelection());
if (folder === null) return;

await clearDefaultTemplates(folder.id);
await joplin.views.dialogs.showMessageBox(`Default templates for "${folder.title}" cleared successfully!`);
}
}));

joplinCommands.add(joplin.commands.register({
name: "createNoteFromDefaultTemplate",
label: "Create note from default template",
execute: async () => {
let defaultTemplate: Note | null = null;

let defaultTemplatesConfig: DefaultTemplatesConfigSetting | null = await joplin.settings.value("defaultTemplatesConfig");
if (defaultTemplatesConfig === null) defaultTemplatesConfig = {};

const defaultTemplatesConfig = await getNotebookDefaultTemplatesConfig();
const currentFolderId = await getSelectedFolder();

if (currentFolderId in defaultTemplatesConfig) {
defaultTemplate = await getTemplateFromId(defaultTemplatesConfig[currentFolderId].defaultNoteTemplateId);
}
Expand All @@ -239,10 +252,9 @@ joplin.plugins.register({
execute: async () => {
let defaultTemplate: Note | null = null;

let defaultTemplatesConfig: DefaultTemplatesConfigSetting | null = await joplin.settings.value("defaultTemplatesConfig");
if (defaultTemplatesConfig === null) defaultTemplatesConfig = {};

const defaultTemplatesConfig = await getNotebookDefaultTemplatesConfig();
const currentFolderId = await getSelectedFolder();

if (currentFolderId in defaultTemplatesConfig) {
defaultTemplate = await getTemplateFromId(defaultTemplatesConfig[currentFolderId].defaultTodoTemplateId);
}
Expand Down Expand Up @@ -314,7 +326,10 @@ joplin.plugins.register({
},
{
commandName: "setDefaultTemplateForNotebook"
}
},
{
commandName: "clearDefaultTemplatesForNotebook"
},
]
},
{
Expand Down
101 changes: 101 additions & 0 deletions src/utils/defaultTemplates.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
import joplin from "api";

export interface DefaultTemplatesConfigSetting {
[notebookId: string]: {
defaultNoteTemplateId: string | null;
defaultTodoTemplateId: string | null;
}
}

export enum DefaultTemplateType {
Both,
Note,
Todo,
}

export const getUserDefaultTemplateTypeSelection = async (promptLabel = "Applicable for:"): Promise<DefaultTemplateType | null> => {
const defaultTypeOptions = [
{
label: "Both notes & todos",
value: DefaultTemplateType.Both
},
{
label: "Notes",
value: DefaultTemplateType.Note
},
{
label: "Todos",
value: DefaultTemplateType.Todo
},
];

const { answer } = await joplin.commands.execute("showPrompt", {
label: promptLabel,
inputType: "dropdown",
value: defaultTypeOptions[0],
autocomplete: defaultTypeOptions
});

if (!answer) return null;
return answer.value;
}

export const setDefaultTemplate = async (notebookId: string | null, templateId: string, defaultType: DefaultTemplateType): Promise<void> => {
if (notebookId === null) {
// Global default
switch (defaultType) {
case DefaultTemplateType.Note:
await joplin.settings.setValue("defaultNoteTemplateId", templateId);
break;
case DefaultTemplateType.Todo:
await joplin.settings.setValue("defaultTodoTemplateId", templateId);
break;
case DefaultTemplateType.Both:
await joplin.settings.setValue("defaultNoteTemplateId", templateId);
await joplin.settings.setValue("defaultTodoTemplateId", templateId);
break;
default:
break;
}
} else {
// Notebook specific default
let defaultTemplatesConfig: DefaultTemplatesConfigSetting | null = await joplin.settings.value("defaultTemplatesConfig");
if (defaultTemplatesConfig === null) defaultTemplatesConfig = {};

if (!(notebookId in defaultTemplatesConfig)) {
defaultTemplatesConfig[notebookId] = {
defaultNoteTemplateId: null,
defaultTodoTemplateId: null
};
}

switch (defaultType) {
case DefaultTemplateType.Note:
defaultTemplatesConfig[notebookId].defaultNoteTemplateId = templateId;
break;
case DefaultTemplateType.Todo:
defaultTemplatesConfig[notebookId].defaultTodoTemplateId = templateId;
break;
case DefaultTemplateType.Both:
defaultTemplatesConfig[notebookId].defaultNoteTemplateId = templateId;
defaultTemplatesConfig[notebookId].defaultTodoTemplateId = templateId;
break;
default:
break;
}

await joplin.settings.setValue("defaultTemplatesConfig", defaultTemplatesConfig);
}
}

export const getNotebookDefaultTemplatesConfig = async (): Promise<DefaultTemplatesConfigSetting> => {
let defaultTemplatesConfig: DefaultTemplatesConfigSetting | null = await joplin.settings.value("defaultTemplatesConfig");
if (defaultTemplatesConfig === null) defaultTemplatesConfig = {};
return defaultTemplatesConfig;
}

export const clearDefaultTemplates = async (notebookId: string): Promise<void> => {
const defaultTemplatesConfig = await getNotebookDefaultTemplatesConfig();
delete defaultTemplatesConfig[notebookId];
await joplin.settings.setValue("defaultTemplatesConfig", defaultTemplatesConfig);
}
88 changes: 0 additions & 88 deletions src/utils/templates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,6 @@ export interface Note {
body: string;
}

export interface DefaultTemplatesConfigSetting {
[notebookId: string]: {
defaultNoteTemplateId: string | null;
defaultTodoTemplateId: string | null;
}
}

export enum DefaultTemplateType {
Both,
Note,
Todo,
}

type NoteProperty = "body" | "id" | "title";

const removeDuplicateTemplates = (templates: Note[]) => {
Expand Down Expand Up @@ -112,78 +99,3 @@ export const getTemplateFromId = async (templateId: string | null): Promise<Note
return null;
}
}

export const getUserDefaultTemplateTypeSelection = async (promptLabel = "Applicable for:"): Promise<DefaultTemplateType | null> => {
const defaultTypeOptions = [
{
label: "Both notes & todos",
value: DefaultTemplateType.Both
},
{
label: "Notes",
value: DefaultTemplateType.Note
},
{
label: "Todos",
value: DefaultTemplateType.Todo
},
];

const { answer } = await joplin.commands.execute("showPrompt", {
label: promptLabel,
inputType: "dropdown",
value: defaultTypeOptions[0],
autocomplete: defaultTypeOptions
});

if (!answer) return null;
return answer.value;
}

export const setDefaultTemplate = async (notebookId: string | null, templateId: string, defaultType: DefaultTemplateType): Promise<void> => {
if (notebookId === null) {
// Global default
switch (defaultType) {
case DefaultTemplateType.Note:
await joplin.settings.setValue("defaultNoteTemplateId", templateId);
break;
case DefaultTemplateType.Todo:
await joplin.settings.setValue("defaultTodoTemplateId", templateId);
break;
case DefaultTemplateType.Both:
await joplin.settings.setValue("defaultNoteTemplateId", templateId);
await joplin.settings.setValue("defaultTodoTemplateId", templateId);
break;
default:
break;
}
} else {
// Notebook specific default
let defaultTemplatesConfig: DefaultTemplatesConfigSetting | null = await joplin.settings.value("defaultTemplatesConfig");
if (defaultTemplatesConfig === null) defaultTemplatesConfig = {};

if (!(notebookId in defaultTemplatesConfig)) {
defaultTemplatesConfig[notebookId] = {
defaultNoteTemplateId: null,
defaultTodoTemplateId: null
};
}

switch (defaultType) {
case DefaultTemplateType.Note:
defaultTemplatesConfig[notebookId].defaultNoteTemplateId = templateId;
break;
case DefaultTemplateType.Todo:
defaultTemplatesConfig[notebookId].defaultTodoTemplateId = templateId;
break;
case DefaultTemplateType.Both:
defaultTemplatesConfig[notebookId].defaultNoteTemplateId = templateId;
defaultTemplatesConfig[notebookId].defaultTodoTemplateId = templateId;
break;
default:
break;
}

await joplin.settings.setValue("defaultTemplatesConfig", defaultTemplatesConfig);
}
}

0 comments on commit d11b2c4

Please sign in to comment.