diff --git a/browser/components/CodeEditor.js b/browser/components/CodeEditor.js index dc6e0cd4..843d644a 100644 --- a/browser/components/CodeEditor.js +++ b/browser/components/CodeEditor.js @@ -191,8 +191,8 @@ CodeEditor.propTypes = { key: PropTypes.string }), className: PropTypes.string, - onChange: PropTypes.func, onBlur: PropTypes.func, + onChange: PropTypes.func, readOnly: PropTypes.bool } diff --git a/browser/main/HomePage/ArticleDetail/ArticleEditor.js b/browser/main/HomePage/ArticleDetail/ArticleEditor.js index 7673424e..378133d6 100644 --- a/browser/main/HomePage/ArticleDetail/ArticleEditor.js +++ b/browser/main/HomePage/ArticleDetail/ArticleEditor.js @@ -3,21 +3,42 @@ import ReactDOM from 'react-dom' import MarkdownPreview from 'browser/components/MarkdownPreview' import CodeEditor from 'browser/components/CodeEditor' import activityRecord from 'browser/lib/activityRecord' +import fetchConfig from 'browser/lib/fetchConfig' + +const electron = require('electron') +const ipc = electron.ipcRenderer export const PREVIEW_MODE = 'PREVIEW_MODE' export const EDIT_MODE = 'EDIT_MODE' +let config = fetchConfig() +ipc.on('config-apply', function (e, newConfig) { + config = newConfig +}) + export default class ArticleEditor extends React.Component { constructor (props) { super(props) + + this.configApplyHandler = (e, config) => this.handleConfigApply(e, config) this.isMouseDown = false this.state = { status: PREVIEW_MODE, cursorPosition: null, - firstVisibleRow: null + firstVisibleRow: null, + switchPreview: config['switch-preview'] } } + componentDidMount () { + console.log(this.state.switchPreview) + ipc.on('config-apply', this.configApplyHandler) + } + + componentWillUnmount () { + ipc.removeListener('config-apply', this.configApplyHandler) + } + componentWillReceiveProps (nextProps) { if (nextProps.article.key !== this.props.article.key) { this.setState({ @@ -26,6 +47,12 @@ export default class ArticleEditor extends React.Component { } } + handleConfigApply (e, newConfig) { + this.setState({ + switchPreview: newConfig['switch-preview'] + }) + } + resetCursorPosition () { this.setState({ cursorPosition: null, @@ -70,14 +97,15 @@ export default class ArticleEditor extends React.Component { } handlePreviewMouseDown (e) { - if (e.button === 2) return true - this.isDrag = false - this.isMouseDown = true - this.moveCount = 0 + if (this.state.switchPreview === 'blur' && e.button === 0) { + this.isDrag = false + this.isMouseDown = true + this.moveCount = 0 + } } handlePreviewMouseMove () { - if (this.isMouseDown) { + if (this.state.switchPreview === 'blur' && this.isMouseDown) { this.moveCount++ if (this.moveCount > 5) { this.isDrag = true @@ -86,17 +114,32 @@ export default class ArticleEditor extends React.Component { } handlePreviewMouseUp (e) { - if (e.button === 2) return true - this.isMouseDown = false - this.moveCount = 0 - if (!this.isDrag) { - this.switchEditMode() + if (this.state.switchPreview === 'blur' && e.button === 0) { + this.isMouseDown = false + this.moveCount = 0 + if (!this.isDrag) { + this.switchEditMode() + } } } + handleRightClickPreview (e) { + let { article } = this.props + if (this.state.switchPreview !== 'rightclick' || article.mode !== 'markdown') return true + + this.switchEditMode() + } + + handleRightClickCodeEditor (e) { + let { article } = this.props + if (this.state.switchPreview !== 'rightclick' || article.mode !== 'markdown') return true + + this.switchPreviewMode() + } + handleBlurCodeEditor (e) { let isFocusingToThis = e.relatedTarget === ReactDOM.findDOMNode(this) - if (isFocusingToThis) { + if (isFocusingToThis || this.state.switchPreview !== 'blur') { return } @@ -115,7 +158,7 @@ export default class ArticleEditor extends React.Component { let showPreview = article.mode === 'markdown' && this.state.status === PREVIEW_MODE if (showPreview) { return ( -