1
0
mirror of https://github.com/BoostIo/Boostnote synced 2025-12-13 17:56:25 +00:00

refactor CodeEditor

This commit is contained in:
Rokt33r
2016-01-02 13:14:46 +09:00
parent c36a46cad6
commit f8ad2eddf3

View File

@@ -1,4 +1,4 @@
import React from 'react' import React, { PropTypes } from 'react'
import ReactDOM from 'react-dom' import ReactDOM from 'react-dom'
import modes from '../lib/modes' import modes from '../lib/modes'
import _ from 'lodash' import _ from 'lodash'
@@ -6,26 +6,15 @@ import _ from 'lodash'
const remote = require('electron').remote const remote = require('electron').remote
const ace = window.ace const ace = window.ace
module.exports = React.createClass({ export default class CodeEditor extends React.Component {
propTypes: { componentWillReceiveProps (nextProps) {
code: React.PropTypes.string,
mode: React.PropTypes.string,
className: React.PropTypes.string,
onChange: React.PropTypes.func,
readOnly: React.PropTypes.bool
},
getDefaultProps: function () {
return {
readOnly: false
}
},
componentWillReceiveProps: function (nextProps) {
if (nextProps.readOnly !== this.props.readOnly) { if (nextProps.readOnly !== this.props.readOnly) {
this.editor.setReadOnly(!!nextProps.readOnly) this.editor.setReadOnly(!!nextProps.readOnly)
} }
}, }
componentDidMount: function () {
var el = ReactDOM.findDOMNode(this.refs.target) componentDidMount () {
var el = ReactDOM.findDOMNode(this)
var editor = this.editor = ace.edit(el) var editor = this.editor = ace.edit(el)
editor.$blockScrolling = Infinity editor.$blockScrolling = Infinity
editor.setValue(this.props.code) editor.setValue(this.props.code)
@@ -33,6 +22,8 @@ module.exports = React.createClass({
editor.setTheme('ace/theme/xcode') editor.setTheme('ace/theme/xcode')
editor.clearSelection() editor.clearSelection()
editor.moveCursorTo(0, 0) editor.moveCursorTo(0, 0)
editor.setReadOnly(!!this.props.readOnly)
editor.commands.addCommand({ editor.commands.addCommand({
name: 'Emacs cursor up', name: 'Emacs cursor up',
bindKey: {mac: 'Ctrl-P'}, bindKey: {mac: 'Ctrl-P'},
@@ -46,13 +37,14 @@ module.exports = React.createClass({
name: 'Focus title', name: 'Focus title',
bindKey: {win: 'Esc', mac: 'Esc'}, bindKey: {win: 'Esc', mac: 'Esc'},
exec: function (editor, e) { exec: function (editor, e) {
console.log(e)
remote.getCurrentWebContents().send('detail-edit') remote.getCurrentWebContents().send('detail-edit')
}, },
readOnly: true readOnly: true
}) })
editor.setReadOnly(!!this.props.readOnly) editor.on('blur', () => {
if (this.props.onBlur) this.props.onBlur()
})
var session = editor.getSession() var session = editor.getSession()
let mode = _.findWhere(modes, {name: this.props.mode}) let mode = _.findWhere(modes, {name: this.props.mode})
@@ -65,14 +57,15 @@ module.exports = React.createClass({
session.setOption('useWorker', false) session.setOption('useWorker', false)
session.setUseWrapMode(true) session.setUseWrapMode(true)
session.on('change', function (e) { session.on('change', e => {
if (this.props.onChange != null) { if (this.props.onChange != null) {
var value = editor.getValue() var value = editor.getValue()
this.props.onChange(e, value) this.props.onChange(e, value)
} }
}.bind(this)) })
}, }
componentDidUpdate: function (prevProps) {
componentDidUpdate (prevProps) {
if (this.editor.getValue() !== this.props.code) { if (this.editor.getValue() !== this.props.code) {
this.editor.setValue(this.props.code) this.editor.setValue(this.props.code)
this.editor.clearSelection() this.editor.clearSelection()
@@ -85,22 +78,42 @@ module.exports = React.createClass({
: 'text' : 'text'
session.setMode('ace/mode/' + syntaxMode) session.setMode('ace/mode/' + syntaxMode)
} }
}, }
getFirstVisibleRow: function () {
getFirstVisibleRow () {
return this.editor.getFirstVisibleRow() return this.editor.getFirstVisibleRow()
}, }
getCursorPosition: function () {
getCursorPosition () {
return this.editor.getCursorPosition() return this.editor.getCursorPosition()
}, }
moveCursorTo: function (row, col) {
moveCursorTo (row, col) {
this.editor.moveCursorTo(row, col) this.editor.moveCursorTo(row, col)
}, }
scrollToLine: function (num) {
scrollToLine (num) {
this.editor.scrollToLine(num, false, false) this.editor.scrollToLine(num, false, false)
}, }
render: function () {
render () {
return ( return (
<div ref='target' className={this.props.className == null ? 'CodeEditor' : 'CodeEditor ' + this.props.className}></div> <div className={this.props.className == null ? 'CodeEditor' : 'CodeEditor ' + this.props.className}></div>
) )
} }
}) }
CodeEditor.propTypes = {
code: PropTypes.string,
mode: PropTypes.string,
className: PropTypes.string,
onChange: PropTypes.func,
onBlur: PropTypes.func,
readOnly: PropTypes.bool
}
CodeEditor.defaultProps = {
readOnly: false
}
export default CodeEditor