Skip to content

Hackape/codemirror #45

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Dec 20, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions app/components/CodeMirrorEditor/addons.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// mode detect
import 'codemirror/mode/meta.js'
import 'codemirror/addon/mode/loadmode.js'
// diaglog
import 'codemirror/addon/dialog/dialog.js'
import 'codemirror/addon/dialog/dialog.css'
// search
import 'codemirror/addon/search/search.js'

// comment
import 'codemirror/addon/comment/comment.js'

// bracket
import 'codemirror/addon/edit/matchbrackets.js'
import 'codemirror/addon/edit/closebrackets.js'

// code fold
import 'codemirror/addon/fold/foldcode.js'
import 'codemirror/addon/fold/foldgutter.js'

// hint
import 'codemirror/addon/hint/show-hint.js'
1 change: 1 addition & 0 deletions app/components/CodeMirrorEditor/getMode.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

86 changes: 86 additions & 0 deletions app/components/CodeMirrorEditor/index.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/*@flow weak*/
import React, {Component} from 'react';
import CodeMirror from 'codemirror';
import './addons';
import {connect} from 'react-redux';
import * as TabActions from '../Tab/actions';

class CodeMirrorEditor extends Component {
static defaultProps = {
theme: 'monokai',
height: '100%',
width: '100%',
};


constructor(props) {
super(props);
this.state = {};
}

componentDidMount() {
const {theme, tab, width, height} = this.props;
const editor = this.editor = CodeMirror(this.editorDOM, {
theme: theme,
autofocus: true,
lineNumbers: true,
matchBrackets: true,
autoCloseBrackets: true,
});

// 1. resize
editor.setSize(width, height);

if (tab.content) {
const {body, path} = tab.content;
const modeInfo = this.getMode(path);
if (body) editor.setValue(body);
if (modeInfo) {
let mode = modeInfo.mode;
require([`codemirror/mode/${mode}/${mode}.js`], () => {
editor.setOption('mode', mode);
});
}
}
editor.focus();
editor.isFocused = editor.hasFocus; // little hack to make codemirror work with legacy interface
tab.editor = editor;
editor.on('change', this.onChange);
editor.on('focus', () => this.props.dispatch(TabActions.activateTab(tab.id)))
}

getMode(path) {
const m = /.+\.([^.]+)$/.exec(path);
if (m) {
const info = CodeMirror.findModeByExtension(m[1]);
if (info) {
return info
}
}
}

onChange = (e) => {
const {tab, dispatch} = this.props;
if (!tab.flags.modified) {
dispatch(TabActions.updateTabFlags(tab.id, 'modified', true))
}
};


render() {
const {width, height} = this.props;
const name = this.state.name;
const divStyle = {width, height};
return (
<div ref={ c => this.editorDOM = c }
id={name}
style={divStyle}
></div>
);
}

}

CodeMirrorEditor = connect(state => ({setting: state.SettingState.views.tabs.EDITOR}), null)(CodeMirrorEditor);

export default CodeMirrorEditor;
4 changes: 3 additions & 1 deletion app/components/EditorWrapper/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ import React, { PropTypes } from 'react'
import AceEditor from '../AceEditor'
import MarkdownEditor from '../MarkdownEditor'
import PictureEditor from '../PictureEditor'
import CodeMirrorEditor from '../CodeMirrorEditor'

const editors = {
CodeMirrorEditor,
AceEditor,
MarkdownEditor,
PictureEditor
Expand All @@ -16,7 +18,7 @@ const getEditorByName = ({
path
}) => {
if (type === 'default') {
return React.createElement(editors.AceEditor, { tab })
return React.createElement(editors.CodeMirrorEditor, { tab });
} else if (type === 'editorWithPreview') {
return React.createElement(editors.MarkdownEditor, { content: body, tab })
} else if (type === 'pictureEditor') {
Expand Down
Loading