-
-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat(dev): refactor settings * fix: use the new settings framework for default templates & misc * fix: use the new settings framework for templates source * fix: remove unnecessary code * fix: generic * fix: bug fix * fix: failing unit test build
- Loading branch information
1 parent
4a0b645
commit b1c80fc
Showing
15 changed files
with
272 additions
and
109 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
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); | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* This considers that the original setting is of type `string`. On `set` if no original value | ||
* can be traced back, the setting is set to an empty string. | ||
*/ | ||
export const createMappedSetting = <T>(id: string, manifest: SettingItem, valueMap: Record<string, T>, defaultValue: T): PluginSetting<T> => { | ||
return class { | ||
static id = id; | ||
static manifest = manifest; | ||
|
||
static async get(): Promise<T> { | ||
const value: string = await joplin.settings.value(id); | ||
return value in valueMap ? valueMap[value] : defaultValue; | ||
} | ||
|
||
static async set(newValue: T): Promise<void> { | ||
const potentialValues = Object.entries(valueMap).filter((entry) => entry[1] === newValue); | ||
await joplin.settings.setValue(id, potentialValues.length ? potentialValues[0][0] : "" ); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import joplin from "api"; | ||
import { SettingItemType } from "api/types"; | ||
|
||
export interface DefaultTemplatesConfig { | ||
[notebookId: string]: { | ||
defaultNoteTemplateId: string | null; | ||
defaultTodoTemplateId: string | null; | ||
} | ||
} | ||
|
||
enum DefaultType { | ||
Both, | ||
Note, | ||
Todo, | ||
} | ||
|
||
export const DefaultTemplatesConfigSetting = class { | ||
static readonly DefaultType = DefaultType; | ||
|
||
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); | ||
} | ||
|
||
static async setDefaultTempalte(notebookId: string, templateId: string, defaultType: DefaultType): Promise<void> { | ||
const defaultTemplatesConfig = await this.get(); | ||
|
||
if (!(notebookId in defaultTemplatesConfig)) { | ||
defaultTemplatesConfig[notebookId] = { | ||
defaultNoteTemplateId: null, | ||
defaultTodoTemplateId: null | ||
}; | ||
} | ||
|
||
switch (defaultType) { | ||
case DefaultType.Note: | ||
defaultTemplatesConfig[notebookId].defaultNoteTemplateId = templateId; | ||
break; | ||
case DefaultType.Todo: | ||
defaultTemplatesConfig[notebookId].defaultTodoTemplateId = templateId; | ||
break; | ||
case DefaultType.Both: | ||
defaultTemplatesConfig[notebookId].defaultNoteTemplateId = templateId; | ||
defaultTemplatesConfig[notebookId].defaultTodoTemplateId = templateId; | ||
break; | ||
default: | ||
break; | ||
} | ||
|
||
await this.set(defaultTemplatesConfig); | ||
} | ||
|
||
static async clearDefaultTemplates(notebookId: string): Promise<void> { | ||
const defaultTemplatesConfig = await this.get(); | ||
delete defaultTemplatesConfig[notebookId]; | ||
await this.set(defaultTemplatesConfig); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import joplin from "api"; | ||
|
||
export interface GlobalSetting<T> { | ||
id: string; | ||
get(): Promise<T>; | ||
} | ||
|
||
export const createGlobalSetting = <T>(id: string): GlobalSetting<T> => { | ||
return class { | ||
static id = id; | ||
|
||
static async get(): Promise<T> { | ||
return await joplin.settings.globalValue(id); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { createGlobalSetting } from "./base"; | ||
|
||
export const LocaleGlobalSetting = createGlobalSetting<string>("locale"); | ||
|
||
export const DateFormatGlobalSetting = createGlobalSetting<string>("dateFormat"); | ||
|
||
export const TimeFormatGlobalSetting = createGlobalSetting<string>("timeFormat"); | ||
|
||
export const ProfileDirGlobalSetting = createGlobalSetting<string>("profileDir"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
// Export all individual settings | ||
export { ApplyTagsWhileInsertingSetting } from "./applyTagsWhileInserting"; | ||
export { DefaultNoteTemplateIdSetting } from "./defaultNoteTemplateId"; | ||
export { DefaultTemplatesConfigSetting } from "./defaultTemplatesConfig"; | ||
export { DefaultTodoTemplateIdSetting } from "./defaultTodoTemplateId"; | ||
export { TemplatesSourceSetting } from "./templatesSource"; | ||
|
||
// Export registry | ||
export { PluginSettingsRegistry } from "./registry"; |
Oops, something went wrong.