1
0
mirror of https://github.com/laurent22/joplin.git synced 2024-11-27 08:21:03 +02:00

Desktop: Resolves #4614: Allow registering multiple settings in one call (#4627)

This commit is contained in:
Jalaj 2021-03-23 14:31:02 +05:30 committed by GitHub
parent 9e58af7232
commit 0985340a7f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 80 additions and 35 deletions

View File

@ -18,22 +18,23 @@ describe('JoplinSettings', () => {
test('should listen to setting change event', async () => {
const service = new newPluginService() as PluginService;
const pluginScript = newPluginScript(`
const pluginScript = newPluginScript(`
joplin.plugins.register({
onStart: async function() {
await joplin.settings.registerSetting('myCustomSetting1', {
value: 1,
type: 1,
public: true,
label: 'My Custom Setting 1',
});
await joplin.settings.registerSetting('myCustomSetting2', {
value: 2,
type: 1,
public: true,
label: 'My Custom Setting 2',
});
await joplin.settings.registerSettings({
'myCustomSetting1': {
value: 1,
type: 1,
public: true,
label: 'My Custom Setting 1',
},
'myCustomSetting2': {
value: 2,
type: 1,
public: true,
label: 'My Custom Setting 2',
}
})
joplin.settings.onChange((event) => {
joplin.data.post(['folders'], null, { title: JSON.stringify(event.keys) });
@ -66,4 +67,35 @@ describe('JoplinSettings', () => {
await service.destroy();
});
test('should allow registering multiple settings', async () => {
const service = new newPluginService() as PluginService;
const pluginScript = newPluginScript(`
joplin.plugins.register({
onStart: async function() {
await joplin.settings.registerSettings({
'myCustomSetting1': {
value: 1,
type: 1,
public: true,
label: 'My Custom Setting 1',
},
'myCustomSetting2': {
value: 2,
type: 1,
public: true,
label: 'My Custom Setting 2',
}
})
},
});
`);
const plugin = await service.loadPluginFromJsBundle('', pluginScript);
await service.runPlugin(plugin);
expect(Setting.value('plugin-org.joplinapp.plugins.PluginTest.myCustomSetting1')).toBe(1);
expect(Setting.value('plugin-org.joplinapp.plugins.PluginTest.myCustomSetting2')).toBe(2);
await service.destroy();
});
});

View File

@ -39,32 +39,45 @@ export default class JoplinSettings {
}
/**
* Registers a new setting. Note that registering a setting item is dynamic and will be gone next time Joplin starts.
* Registers new settings.
* 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.
*/
public async registerSettings(settings: Record<string, SettingItem>) {
for (const [key, setting] of Object.entries(settings)) {
const internalSettingItem: InternalSettingItem = {
key: key,
value: setting.value,
type: setting.type,
public: setting.public,
label: () => setting.label,
description: (_appType: string) => setting.description,
};
if ('isEnum' in setting) internalSettingItem.isEnum = setting.isEnum;
if ('section' in setting) internalSettingItem.section = this.namespacedKey(setting.section);
if ('options' in setting) internalSettingItem.options = () => setting.options;
if ('appTypes' in setting) internalSettingItem.appTypes = setting.appTypes;
if ('secure' in setting) internalSettingItem.secure = setting.secure;
if ('advanced' in setting) internalSettingItem.advanced = setting.advanced;
if ('minimum' in setting) internalSettingItem.minimum = setting.minimum;
if ('maximum' in setting) internalSettingItem.maximum = setting.maximum;
if ('step' in setting) internalSettingItem.step = setting.step;
await Setting.registerSetting(this.namespacedKey(key), internalSettingItem);
}
}
/**
* @deprecated Use joplin.settings.registerSettings()
*
* Registers a new setting.
*/
public async registerSetting(key: string, settingItem: SettingItem) {
const internalSettingItem: InternalSettingItem = {
key: key,
value: settingItem.value,
type: settingItem.type,
public: settingItem.public,
label: () => settingItem.label,
description: (_appType: string) => settingItem.description,
};
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);
this.plugin_.deprecationNotice('1.8', 'joplin.settings.registerSetting() is deprecated in favour of joplin.settings.registerSettings()');
await this.registerSettings({ [key]: settingItem });
}
/**