2020-11-05 18:58:23 +02:00
|
|
|
import Setting, { SettingItem as InternalSettingItem } from '../../../models/Setting';
|
2020-10-09 19:35:46 +02:00
|
|
|
import Plugin from '../Plugin';
|
|
|
|
import { SettingItem, SettingSection } from './types';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This API allows registering new settings and setting sections, as well as getting and setting settings. Once a setting has been registered it will appear in the config screen and be editable by the user.
|
|
|
|
*
|
|
|
|
* Settings are essentially key/value pairs.
|
|
|
|
*
|
|
|
|
* Note: Currently this API does **not** provide access to Joplin's built-in settings. This is by design as plugins that modify user settings could give unexpected results
|
|
|
|
*
|
2020-11-05 18:58:23 +02:00
|
|
|
* [View the demo plugin](https://github.com/laurent22/joplin/tree/dev/packages/app-cli/tests/support/plugins/settings)
|
2020-10-09 19:35:46 +02:00
|
|
|
*/
|
|
|
|
export default class JoplinSettings {
|
2020-11-12 21:13:28 +02:00
|
|
|
private plugin_: Plugin = null;
|
2020-10-09 19:35:46 +02:00
|
|
|
|
|
|
|
constructor(plugin: Plugin) {
|
|
|
|
this.plugin_ = plugin;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensures that the plugin settings and sections are within their own namespace, to prevent them from
|
|
|
|
// overwriting other plugin settings or the default settings.
|
2020-11-12 21:13:28 +02:00
|
|
|
private namespacedKey(key: string): string {
|
2020-10-09 19:35:46 +02:00
|
|
|
return `plugin-${this.plugin_.id}.${key}`;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Registers a new setting. Note that registering a setting item is dynamic and will be gone next time Joplin starts.
|
|
|
|
* What it means is that you need to register the setting every time the plugin starts (for example in the onStart event).
|
|
|
|
* The setting value however will be preserved from one launch to the next so there is no risk that it will be lost even if for some
|
|
|
|
* reason the plugin fails to start at some point.
|
|
|
|
*/
|
2020-11-12 21:13:28 +02:00
|
|
|
async registerSetting(key: string, settingItem: SettingItem) {
|
|
|
|
const internalSettingItem: InternalSettingItem = {
|
2020-10-09 19:35:46 +02:00
|
|
|
key: key,
|
|
|
|
value: settingItem.value,
|
|
|
|
type: settingItem.type,
|
|
|
|
public: settingItem.public,
|
|
|
|
label: () => settingItem.label,
|
2020-11-12 21:13:28 +02:00
|
|
|
description: (_appType: string) => settingItem.description,
|
2020-10-09 19:35:46 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
if ('isEnum' in settingItem) internalSettingItem.isEnum = settingItem.isEnum;
|
|
|
|
if ('section' in settingItem) internalSettingItem.section = this.namespacedKey(settingItem.section);
|
|
|
|
if ('options' in settingItem) internalSettingItem.options = settingItem.options;
|
|
|
|
if ('appTypes' in settingItem) internalSettingItem.appTypes = settingItem.appTypes;
|
|
|
|
if ('secure' in settingItem) internalSettingItem.secure = settingItem.secure;
|
|
|
|
if ('advanced' in settingItem) internalSettingItem.advanced = settingItem.advanced;
|
|
|
|
if ('minimum' in settingItem) internalSettingItem.minimum = settingItem.minimum;
|
|
|
|
if ('maximum' in settingItem) internalSettingItem.maximum = settingItem.maximum;
|
|
|
|
if ('step' in settingItem) internalSettingItem.step = settingItem.step;
|
|
|
|
|
|
|
|
return Setting.registerSetting(this.namespacedKey(key), internalSettingItem);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Registers a new setting section. Like for registerSetting, it is dynamic and needs to be done every time the plugin starts.
|
|
|
|
*/
|
2020-11-12 21:13:28 +02:00
|
|
|
async registerSection(name: string, section: SettingSection) {
|
2020-10-09 19:35:46 +02:00
|
|
|
return Setting.registerSection(this.namespacedKey(name), section);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets a setting value (only applies to setting you registered from your plugin)
|
|
|
|
*/
|
2020-11-12 21:13:28 +02:00
|
|
|
async value(key: string): Promise<any> {
|
2020-10-09 19:35:46 +02:00
|
|
|
return Setting.value(this.namespacedKey(key));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets a setting value (only applies to setting you registered from your plugin)
|
|
|
|
*/
|
2020-11-12 21:13:28 +02:00
|
|
|
async setValue(key: string, value: any) {
|
2020-10-09 19:35:46 +02:00
|
|
|
return Setting.setValue(this.namespacedKey(key), value);
|
|
|
|
}
|
2020-10-13 10:59:53 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets a global setting value, including app-specific settings and those set by other plugins.
|
|
|
|
*
|
|
|
|
* The list of available settings is not documented yet, but can be found by looking at the source code:
|
|
|
|
*
|
2020-11-05 18:58:23 +02:00
|
|
|
* https://github.com/laurent22/joplin/blob/3539a452a359162c461d2849829d2d42973eab50/packages/app-mobile/lib/models/Setting.ts#L142
|
2020-10-13 10:59:53 +02:00
|
|
|
*/
|
2020-11-12 21:13:28 +02:00
|
|
|
async globalValue(key: string): Promise<any> {
|
2020-10-13 10:59:53 +02:00
|
|
|
return Setting.value(key);
|
|
|
|
}
|
2020-10-09 19:35:46 +02:00
|
|
|
}
|