diff --git a/browser/main/lib/ConfigManager.js b/browser/main/lib/ConfigManager.js index 4cbe80a7..b662a892 100644 --- a/browser/main/lib/ConfigManager.js +++ b/browser/main/lib/ConfigManager.js @@ -80,6 +80,10 @@ export const DEFAULT_CONFIG = { token: '', username: '', password: '' + }, + export: { + action: 'DONT_EXPORT', // 'DONT_EXPORT', 'MERGE_HEADER', 'MERGE_VARIABLE' + variable: 'metadata' } } diff --git a/browser/main/modals/PreferencesModal/ExportTab.js b/browser/main/modals/PreferencesModal/ExportTab.js new file mode 100644 index 00000000..b2036646 --- /dev/null +++ b/browser/main/modals/PreferencesModal/ExportTab.js @@ -0,0 +1,161 @@ +import PropTypes from 'prop-types' +import React from 'react' +import CSSModules from 'browser/lib/CSSModules' +import styles from './ConfigTab.styl' +import ConfigManager from 'browser/main/lib/ConfigManager' +import store from 'browser/main/store' +import _ from 'lodash' +import i18n from 'browser/lib/i18n' + +const electron = require('electron') +const ipc = electron.ipcRenderer + +class ExportTab extends React.Component { + constructor (props) { + super(props) + + this.state = { + config: props.config + } + } + + clearMessage () { + _.debounce(() => { + this.setState({ + ExportAlert: null + }) + }, 2000)() + } + + componentDidMount () { + this.handleSettingDone = () => { + this.setState({ + ExportAlert: { + type: 'success', + message: i18n.__('Successfully applied!') + } + } + ) + } + this.handleSettingError = (err) => { + this.setState({ + ExportAlert: { + type: 'error', + message: err.message != null ? err.message : i18n.__('An error occurred!') + } + } + ) + } + + this.oldExport = this.state.config.export + + ipc.addListener('APP_SETTING_DONE', this.handleSettingDone) + ipc.addListener('APP_SETTING_ERROR', this.handleSettingError) + } + + componentWillUnmount () { + ipc.removeListener('APP_SETTING_DONE', this.handleSettingDone) + ipc.removeListener('APP_SETTING_ERROR', this.handleSettingError) + } + + handleSaveButtonClick (e) { + const newConfig = { + export: this.state.config.export + } + + ConfigManager.set(newConfig) + + store.dispatch({ + type: 'SET_UI', + config: newConfig + }) + + this.clearMessage() + this.props.haveToSave() + } + + handleExportChange (e) { + const { config } = this.state + + config.export = { + action: this.refs.action.value, + variable: !_.isNil(this.refs.variable) ? this.refs.variable.value : config.export.variable + } + + this.setState({ + config + }) + + if (_.isEqual(this.oldExport, config.export)) { + this.props.haveToSave() + } else { + this.props.haveToSave({ + tab: 'Export', + type: 'warning', + message: i18n.__('Unsaved Changes!') + }) + } + } + + render () { + const { config, ExportAlert } = this.state + console.log(config.export) + + const ExportAlertElement = ExportAlert != null + ?

+ {ExportAlert.message} +

+ : null + + return ( +
+
+
{i18n.__('Export')}
+ +
+
+ {i18n.__('Action')} +
+
+ +
+
+ + { config.export.action === 'MERGE_VARIABLE' && +
+
{i18n.__('Variable Name')}
+
+ this.handleExportChange(e)} + ref='variable' + value={config.export.variable} + type='text' /> +
+
+ } + +
+ + {ExportAlertElement} +
+
+
+ ) + } +} + +ExportTab.propTypes = { + dispatch: PropTypes.func, + haveToSave: PropTypes.func +} + +export default CSSModules(ExportTab, styles) diff --git a/browser/main/modals/PreferencesModal/index.js b/browser/main/modals/PreferencesModal/index.js index f3fc3751..257d50c7 100644 --- a/browser/main/modals/PreferencesModal/index.js +++ b/browser/main/modals/PreferencesModal/index.js @@ -6,6 +6,7 @@ import UiTab from './UiTab' import InfoTab from './InfoTab' import Crowdfunding from './Crowdfunding' import StoragesTab from './StoragesTab' +import ExportTab from './ExportTab' import SnippetTab from './SnippetTab' import Blog from './Blog' import ModalEscButton from 'browser/components/ModalEscButton' @@ -23,7 +24,8 @@ class Preferences extends React.Component { currentTab: 'STORAGES', UIAlert: '', HotkeyAlert: '', - BlogAlert: '' + BlogAlert: '', + ExportAlert: '' } } @@ -87,6 +89,15 @@ class Preferences extends React.Component { haveToSave={alert => this.setState({BlogAlert: alert})} /> ) + case 'EXPORT': + return ( + this.setState({ExportAlert: alert})} + /> + ) case 'SNIPPET': return (