Skip to content

Commit

Permalink
feat(dev): refactor settings
Browse files Browse the repository at this point in the history
  • Loading branch information
nishantwrp committed Sep 15, 2024
1 parent 7a643ff commit 9f53803
Show file tree
Hide file tree
Showing 6 changed files with 100 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/settings/applyTagsWhileInserting.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { SettingItemType } from "api/types";
import { createSimpleSetting } from "./base";

export const ApplyTagsWhileInsertingSetting = createSimpleSetting<boolean>("applyTagsWhileInserting", {
public: true,
type: SettingItemType.Bool,
value: true,
label: "Apply tags while inserting template",
description: "Apply tags using 'template_tags' variable while inserting template to notes/to-dos.",
section: "templatesPlugin"
});
25 changes: 25 additions & 0 deletions src/settings/base.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import joplin from "api";
import { SettingItem } from "api/types";

export interface PluginSetting<T> {
id: string;
manifest: SettingItem;

get(): Promise<T>;
set(newValue: T): Promise<void>;
}

export const createSimpleSetting = <T>(id: string, manifest: SettingItem): PluginSetting<T> => {
return class {
static id = id;
static manifest = manifest;

static async get(): Promise<T> {
return await joplin.settings.value(id);
}

static async set(newValue: T): Promise<void> {
await joplin.settings.setValue(id, newValue);
}
}
}
9 changes: 9 additions & 0 deletions src/settings/defaultNoteTemplateId.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { SettingItemType } from "api/types";
import { createSimpleSetting } from "./base";

export const DefaultNoteTemplateIdSetting = createSimpleSetting<string | null>("defaultNoteTemplateId", {
public: false,
type: SettingItemType.String,
value: null,
label: "Default note template ID"
});
30 changes: 30 additions & 0 deletions src/settings/defaultTemplatesConfig.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import joplin from "api";
import { SettingItemType } from "api/types";
import { PluginSetting } from "./base";

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

export const DefaultTemplatesConfigSetting: PluginSetting<DefaultTemplatesConfig> = class {
static id = "defaultTemplatesConfig";

static manifest = {
public: false,
type: SettingItemType.Object,
value: null,
label: "Default templates config"
};

static async get(): Promise<DefaultTemplatesConfig> {
const defaultTemplatesConfig: DefaultTemplatesConfig | null = await joplin.settings.value(DefaultTemplatesConfigSetting.id);
return defaultTemplatesConfig ? defaultTemplatesConfig : {};
}

static async set(newValue: DefaultTemplatesConfig): Promise<void> {
await joplin.settings.setValue(DefaultTemplatesConfigSetting.id, newValue);
}
}
9 changes: 9 additions & 0 deletions src/settings/defaultTodoTemplateId.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { SettingItemType } from "api/types";
import { createSimpleSetting } from "./base";

export const DefaultTodoTemplateIdSetting = createSimpleSetting<string | null>("defaultTodoTemplateId", {
public: false,
type: SettingItemType.String,
value: null,
label: "Default to-do template ID"
});
16 changes: 16 additions & 0 deletions src/settings/templatesSource.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { SettingItemType } from "api/types";
import { createSimpleSetting } from "./base";

export const TemplatesSourceSetting = createSimpleSetting<string>("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"
});

0 comments on commit 9f53803

Please sign in to comment.