From edca8f60b6026241acbacd3e3a46a9b046b37a40 Mon Sep 17 00:00:00 2001 From: Ezinwa Okpoechi Date: Thu, 9 Mar 2017 14:19:06 +0100 Subject: [PATCH 01/16] Add settings support for plugins --- app/lib/plugins/index.js | 1 + app/lib/plugins/settings.js | 48 +++++++++++++++++++++++++++++ app/main/plugins/externalPlugins.js | 3 +- 3 files changed, 51 insertions(+), 1 deletion(-) create mode 100644 app/lib/plugins/settings.js diff --git a/app/lib/plugins/index.js b/app/lib/plugins/index.js index 4cc42f2c..571f4713 100644 --- a/app/lib/plugins/index.js +++ b/app/lib/plugins/index.js @@ -32,3 +32,4 @@ export const ensureFiles = () => { } export const client = npm(pluginsPath) +export { default as addSettings } from './settings' diff --git a/app/lib/plugins/settings.js b/app/lib/plugins/settings.js new file mode 100644 index 00000000..d80b326e --- /dev/null +++ b/app/lib/plugins/settings.js @@ -0,0 +1,48 @@ +import config from 'lib/config' +import { isEqual } from 'lodash' + +const VALID_TYPES = new Set([ + 'array', + 'string', + 'number', + 'bool', + 'option', +]) + +const isValidSetting = setting => { + const { type, options } = setting + if (!type || !VALID_TYPES.has(type)) { + return false + } + + if (type == 'option') { + return Array.isArray(options) && options.length + } + + return true +} + +export default ({ settings }, base) => { + if (! settings) return + + const externalSettings = config.get('external') || {} + const pluginSettings = externalSettings[base] || {} + + Object.keys(settings).forEach(key => { + const setting = settings[key] + if (! isValidSetting(setting)) { + console.log(`Invalid setting '${key}'`) + return + } + + const { value, ...pluginSetting } = pluginSettings[key] || {} + + if (!isEqual(setting, pluginSetting)) { + const { description, type, defaultValue, multi, options } = setting + pluginSettings[key] = { description, type, defaultValue, multi, options } + } + }) + + externalSettings[base] = pluginSettings + config.set('external', externalSettings) +} diff --git a/app/main/plugins/externalPlugins.js b/app/main/plugins/externalPlugins.js index 4beaf87f..0fed9095 100644 --- a/app/main/plugins/externalPlugins.js +++ b/app/main/plugins/externalPlugins.js @@ -1,7 +1,7 @@ import debounce from 'lodash/debounce' import chokidar from 'chokidar' import path from 'path' -import { modulesDirectory, ensureFiles } from 'lib/plugins' +import { modulesDirectory, ensureFiles, addSettings } from 'lib/plugins' const requirePlugin = (pluginPath) => { try { @@ -55,6 +55,7 @@ pluginsWatcher.on('addDir', (pluginPath) => { console.groupEnd() return } + addSettings(plugin, base); console.log('Loaded.') const requirePath = window.require.resolve(pluginPath) const watcher = chokidar.watch(requirePath, { depth: 0 }) From c2baf0bedbca94a0855678291d6ca5655dde1d2b Mon Sep 17 00:00:00 2001 From: Ezinwa Okpoechi Date: Fri, 10 Mar 2017 10:48:09 +0100 Subject: [PATCH 02/16] Render plugin settings --- .../settings/Settings/components/checkbox.js | 18 +++++ .../settings/Settings/components/index.js | 4 + .../settings/Settings/components/input.js | 19 +++++ .../settings/Settings/components/option.js | 19 +++++ .../settings/Settings/components/select.js | 18 +++++ .../settings/Settings/external-settings.js | 81 +++++++++++++++++++ .../core/cerebro/settings/Settings/index.js | 10 ++- .../core/cerebro/settings/Settings/styles.css | 13 +++ 8 files changed, 180 insertions(+), 2 deletions(-) create mode 100644 app/main/plugins/core/cerebro/settings/Settings/components/checkbox.js create mode 100644 app/main/plugins/core/cerebro/settings/Settings/components/index.js create mode 100644 app/main/plugins/core/cerebro/settings/Settings/components/input.js create mode 100644 app/main/plugins/core/cerebro/settings/Settings/components/option.js create mode 100644 app/main/plugins/core/cerebro/settings/Settings/components/select.js create mode 100644 app/main/plugins/core/cerebro/settings/Settings/external-settings.js diff --git a/app/main/plugins/core/cerebro/settings/Settings/components/checkbox.js b/app/main/plugins/core/cerebro/settings/Settings/components/checkbox.js new file mode 100644 index 00000000..b598db5f --- /dev/null +++ b/app/main/plugins/core/cerebro/settings/Settings/components/checkbox.js @@ -0,0 +1,18 @@ +import React from 'react' +import styles from '../styles.css' + +export default (props) => ( +
+
+ +
+
+) diff --git a/app/main/plugins/core/cerebro/settings/Settings/components/index.js b/app/main/plugins/core/cerebro/settings/Settings/components/index.js new file mode 100644 index 00000000..fe718154 --- /dev/null +++ b/app/main/plugins/core/cerebro/settings/Settings/components/index.js @@ -0,0 +1,4 @@ +export { default as Checkbox } from './checkbox' +export { default as Input } from './input' +export { default as Select } from './select' +export { default as Option } from './option' diff --git a/app/main/plugins/core/cerebro/settings/Settings/components/input.js b/app/main/plugins/core/cerebro/settings/Settings/components/input.js new file mode 100644 index 00000000..9732cef9 --- /dev/null +++ b/app/main/plugins/core/cerebro/settings/Settings/components/input.js @@ -0,0 +1,19 @@ +import React from 'react' +import styles from '../styles.css' +import hotkeyStyles from '../Hotkey/styles.css' + +export default (props) => ( +
+ +
+ + {props.description + ?
{props.description}
+ : ''} +
+
+) diff --git a/app/main/plugins/core/cerebro/settings/Settings/components/option.js b/app/main/plugins/core/cerebro/settings/Settings/components/option.js new file mode 100644 index 00000000..9e76284f --- /dev/null +++ b/app/main/plugins/core/cerebro/settings/Settings/components/option.js @@ -0,0 +1,19 @@ +import React from 'react' +import Select, { Creatable } from 'react-select' +import styles from '../styles.css' + +export default (props) => ( +
+
+ + {props.description + ?
{props.description}
+ : ''} +
+
+) diff --git a/app/main/plugins/core/cerebro/settings/Settings/components/select.js b/app/main/plugins/core/cerebro/settings/Settings/components/select.js new file mode 100644 index 00000000..d3c5f03a --- /dev/null +++ b/app/main/plugins/core/cerebro/settings/Settings/components/select.js @@ -0,0 +1,18 @@ +import React from 'react' +import Select, { Creatable } from 'react-select' +import styles from '../styles.css' + +export default (props) => ( +
+
+ + {props.description + ?
{props.description}
+ : ''} +
+
+) diff --git a/app/main/plugins/core/cerebro/settings/Settings/external-settings.js b/app/main/plugins/core/cerebro/settings/Settings/external-settings.js new file mode 100644 index 00000000..0d04dfb8 --- /dev/null +++ b/app/main/plugins/core/cerebro/settings/Settings/external-settings.js @@ -0,0 +1,81 @@ +import React, { PropTypes, Component } from 'react' +import loadThemes from 'lib/loadThemes' +import styles from './styles.css' +import hotkeyStyles from './Hotkey/styles.css' +import { Checkbox, Input, Select, Option } from './components' + +export default class ExternalSettings extends Component { + static propTypes = { + settings: PropTypes.object.isRequired, + } + + renderSetting(label, setting, plugin) { + const { type, value, description } = setting + + if (type == 'bool') { + return ( + + ) + } + + if (type == 'array') { + return ( + + ) + } + + render() { + const { settings } = this.props + + return ( +
+ {Object.keys(settings).map(plugin => { + return ( +
+ + + {Object.keys(settings[plugin]).map(label => { + const setting = settings[plugin][label] + return this.renderSetting(label, setting, plugin) + })} +
+ ) + })} +
+ ) + } +} diff --git a/app/main/plugins/core/cerebro/settings/Settings/index.js b/app/main/plugins/core/cerebro/settings/Settings/index.js index 1df849af..1a67bcbb 100644 --- a/app/main/plugins/core/cerebro/settings/Settings/index.js +++ b/app/main/plugins/core/cerebro/settings/Settings/index.js @@ -4,6 +4,7 @@ import CountrySelect from './CountrySelect' import Select from 'react-select' import loadThemes from 'lib/loadThemes' import styles from './styles.css' +import ExternalSettings from './external-settings' class Settings extends Component { constructor(props) { @@ -15,7 +16,8 @@ class Settings extends Component { country: get('country'), theme: get('theme'), developerMode: get('developerMode'), - cleanOnHide: get('cleanOnHide') + cleanOnHide: get('cleanOnHide'), + externalSettings: get('external'), } this.changeConfig = this.changeConfig.bind(this) } @@ -27,7 +29,7 @@ class Settings extends Component { } render() { const { - hotkey, showInTray, country, theme, developerMode, cleanOnHide + hotkey, showInTray, country, theme, developerMode, cleanOnHide, externalSettings } = this.state return (
@@ -110,6 +112,10 @@ class Settings extends Component {
+ + ) } diff --git a/app/main/plugins/core/cerebro/settings/Settings/styles.css b/app/main/plugins/core/cerebro/settings/Settings/styles.css index 04b9741b..a1d01ce9 100644 --- a/app/main/plugins/core/cerebro/settings/Settings/styles.css +++ b/app/main/plugins/core/cerebro/settings/Settings/styles.css @@ -41,3 +41,16 @@ .checkbox { margin-right: 5px; } + +.settingItem { + padding: 20px; + box-sizing: border-box; + width: 100%; + border-color: #d9d9d9 #ccc #b3b3b3; + border-top: 1px solid #ccc; + margin-top: 16px; +} + +.header { + font-weight: bold; +} From 715831c60c0b167a6b63f87c89fb27a1bd5055fa Mon Sep 17 00:00:00 2001 From: Ezinwa Okpoechi Date: Fri, 10 Mar 2017 12:02:57 +0100 Subject: [PATCH 03/16] Save changed external settings --- .../settings/Settings/components/checkbox.js | 2 +- .../settings/Settings/components/index.js | 2 +- .../settings/Settings/components/input.js | 1 + .../components/{option.js => options.js} | 1 + .../settings/Settings/components/select.js | 1 + .../settings/Settings/external-settings.js | 41 ++++++++++++------- .../core/cerebro/settings/Settings/index.js | 7 ++-- 7 files changed, 36 insertions(+), 19 deletions(-) rename app/main/plugins/core/cerebro/settings/Settings/components/{option.js => options.js} (93%) diff --git a/app/main/plugins/core/cerebro/settings/Settings/components/checkbox.js b/app/main/plugins/core/cerebro/settings/Settings/components/checkbox.js index b598db5f..ed2d0658 100644 --- a/app/main/plugins/core/cerebro/settings/Settings/components/checkbox.js +++ b/app/main/plugins/core/cerebro/settings/Settings/components/checkbox.js @@ -7,7 +7,7 @@ export default (props) => (