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 consts from 'browser/lib/consts' import ReactCodeMirror from 'react-codemirror' import CodeMirror from 'codemirror' import _ from 'lodash' const OSX = global.process.platform === 'darwin' const electron = require('electron') const ipc = electron.ipcRenderer class UiTab extends React.Component { constructor (props) { super(props) this.state = { config: props.config, codemirrorTheme: props.config.editor.theme } } componentDidMount () { this.handleSettingDone = () => { this.setState({UiAlert: { type: 'success', message: 'Successfully applied!' }}) } this.handleSettingError = (err) => { this.setState({UiAlert: { type: 'error', message: err.message != null ? err.message : 'Error occurs!' }}) } ipc.addListener('APP_SETTING_DONE', this.handleSettingDone) ipc.addListener('APP_SETTING_ERROR', this.handleSettingError) } componentWillMount () { CodeMirror.autoLoadMode(ReactCodeMirror, 'javascript') } componentWillUnmount () { ipc.removeListener('APP_SETTING_DONE', this.handleSettingDone) ipc.removeListener('APP_SETTING_ERROR', this.handleSettingError) } handleUIChange (e) { const { codemirrorTheme } = this.state let checkHighLight = document.getElementById('checkHighLight') if (checkHighLight === null) { checkHighLight = document.createElement('link') checkHighLight.setAttribute('id', 'checkHighLight') checkHighLight.setAttribute('rel', 'stylesheet') document.head.appendChild(checkHighLight) } const newConfig = { ui: { theme: this.refs.uiTheme.value, showCopyNotification: this.refs.showCopyNotification.checked, disableDirectWrite: this.refs.uiD2w != null ? this.refs.uiD2w.checked : false }, editor: { theme: this.refs.editorTheme.value, fontSize: this.refs.editorFontSize.value, fontFamily: this.refs.editorFontFamily.value, indentType: this.refs.editorIndentType.value, indentSize: this.refs.editorIndentSize.value, switchPreview: this.refs.editorSwitchPreview.value, keyMap: this.refs.editorKeyMap.value }, preview: { fontSize: this.refs.previewFontSize.value, fontFamily: this.refs.previewFontFamily.value, codeBlockTheme: this.refs.previewCodeBlockTheme.value, lineNumber: this.refs.previewLineNumber.checked } } const newCodemirrorTheme = this.refs.editorTheme.value if (newCodemirrorTheme !== codemirrorTheme) { checkHighLight.setAttribute('href', `../node_modules/codemirror/theme/${newCodemirrorTheme.split(' ')[0]}.css`) } this.setState({ config: newConfig, codemirrorTheme: newCodemirrorTheme }, () => { const {ui, editor, preview} = this.props.config this.currentConfig = {ui, editor, preview} if (_.isEqual(this.currentConfig, this.state.config)) { this.props.haveToSave() } else { this.props.haveToSave({ tab: 'UI', type: 'warning', message: 'You have to save!' }) } }) } handleSaveUIClick (e) { const newConfig = { ui: this.state.config.ui, editor: this.state.config.editor, preview: this.state.config.preview } ConfigManager.set(newConfig) store.dispatch({ type: 'SET_UI', config: newConfig }) this.clearMessage() this.props.haveToSave() } clearMessage () { _.debounce(() => { this.setState({ UiAlert: null }) }, 2000)() } render () { const UiAlert = this.state.UiAlert const UiAlertElement = UiAlert != null ?
{UiAlert.message}
: null const themes = consts.THEMES const { config, codemirrorTheme } = this.state const codemirrorSampleCode = 'function iamHappy (happy) {\n\tif (happy) {\n\t console.log("I am Happy!")\n\t} else {\n\t console.log("I am not Happy!")\n\t}\n};' return (⚠️ Please restart boostnote after you change the keymap