react-templates/playground/CodeMirrorEditor.js

174 lines
6.3 KiB
JavaScript
Raw Normal View History

2014-12-10 13:14:22 +01:00
define(['react', 'lodash', 'jquery', './libs/codemirror-4.8/lib/codemirror',
2014-12-30 09:42:31 +01:00
'./CMLint',
2014-12-09 14:31:25 +01:00
'./libs/codemirror-4.8/mode/javascript/javascript',
'./libs/codemirror-4.8/addon/hint/html-hint',
'./libs/codemirror-4.8/addon/hint/show-hint',
'./libs/codemirror-4.8/addon/hint/xml-hint',
'./libs/codemirror-4.8/addon/hint/html-hint',
2014-12-30 16:33:28 +01:00
//'./libs/codemirror-4.8/addon/display/panel',
2014-12-09 14:31:25 +01:00
'./libs/codemirror-4.8/mode/xml/xml',
2014-12-30 16:33:28 +01:00
//'./libs/codemirror-4.8/mode/css/css',
'./libs/codemirror-4.8/addon/runmode/runmode'
2014-12-09 14:31:25 +01:00
//'./libs/codemirror-4.8/addon/display/placeholder'
2014-12-30 09:42:31 +01:00
], function (React, _, $, CodeMirror, CMLint) {
2014-12-09 14:31:25 +01:00
'use strict';
//codeMirror: 'libs/codemirror-4.8/lib/codemirror',
//htmlmixed: 'libs/codemirror-4.8/mode/htmlmixed/htmlmixed',
//javascript: 'libs/codemirror-4.8/mode/javascript/javascript'
var rtSchema = {
div: {
attrs: {
'rt-props': null,
'rt-if': null,
'rt-repeat': null,
'rt-class': null,
'rt-scope': null,
valueLink: null,
2014-12-24 11:33:23 +01:00
key: null,
ref: null,
dangerouslySetInnerHTML: null
2014-12-09 14:31:25 +01:00
}
}
};
var tags = CodeMirror.htmlSchema;
Object.keys(CodeMirror.htmlSchema).forEach(function (i) {
tags[i].attrs = _.defaults(rtSchema.div.attrs, tags[i].attrs);
});
function completeAfter(cm, pred) {
2014-12-10 13:14:22 +01:00
//var cur = cm.getCursor();
2014-12-09 14:31:25 +01:00
if (!pred || pred()) {
setTimeout(function () {
if (!cm.state.completionActive) {
cm.showHint({completeSingle: false});
}
}, 100);
}
return CodeMirror.Pass;
}
function completeIfAfterLt(cm) {
return completeAfter(cm, function () {
var cur = cm.getCursor();
2014-12-10 13:14:22 +01:00
/*eslint new-cap:0*/
2014-12-09 14:31:25 +01:00
return cm.getRange(CodeMirror.Pos(cur.line, cur.ch - 1), cur) === '<';
});
}
function completeIfInTag(cm) {
return completeAfter(cm, function () {
var tok = cm.getTokenAt(cm.getCursor());
if (tok.type === 'string' && (!/['"]/.test(tok.string.charAt(tok.string.length - 1)) || tok.string.length === 1)) {
return false;
}
var inner = CodeMirror.innerMode(cm.getMode(), tok.state).state;
return inner.tagName;
});
}
var editor = React.createClass({
displayName: 'CodeMirrorEditor',
2014-12-30 16:33:28 +01:00
propTypes: {
id: React.PropTypes.string,
readOnly: React.PropTypes.bool,
runMode: React.PropTypes.bool,
mode: React.PropTypes.string
},
getDefaultProps: function () {
return {
readOnly: false,
mode: 'html'
};
},
2014-12-09 14:31:25 +01:00
getInitialState: function () {
return {
editorId: _.uniqueId()
};
},
2014-12-30 16:33:28 +01:00
//componentWillMount: function () {
//},
2014-12-09 14:31:25 +01:00
render: function () {
var props = _.omit(this.props, ['ref', 'key', 'value', 'valueLink', 'onChange']);
props.id = this.props.id || this.state.editorId;
var value = this.props.valueLink ? this.props.valueLink() : this.props.value;
2014-12-30 09:42:31 +01:00
return React.DOM.textarea(props, value);
2014-12-09 14:31:25 +01:00
},
componentWillUpdate: function (nextProps/*, nextState*/) {
var value = nextProps.valueLink ? nextProps.valueLink() : nextProps.value;
2014-12-10 13:14:22 +01:00
if (this.editor && this.editor.getValue() !== value) {
2014-12-09 14:31:25 +01:00
this.editor.setValue(value || '');
}
},
componentDidMount: function () {
var value = this.props.valueLink ? this.props.valueLink() : this.props.value;
var options = {
readOnly: this.props.readOnly,
lineWrapping: true,
2014-12-10 13:14:22 +01:00
smartIndent: true,
matchBrackets: true,
2014-12-09 14:31:25 +01:00
value: value,
lineNumbers: true,
mode: 'javascript',
2014-12-30 09:42:31 +01:00
gutters: ['CodeMirror-linenumbers', 'rt-annotations'],
2014-12-10 13:14:22 +01:00
theme: 'solarized' //solarized_light solarized-light
2014-12-09 14:31:25 +01:00
};
if (this.props.mode === 'html') {
options.mode = 'text/html';
options.extraKeys = {
"'<'": completeAfter,
"'/'": completeIfAfterLt,
"' '": completeIfInTag,
"'='": completeIfInTag,
'Ctrl-Space': 'autocomplete'
};
options.hintOptions = {schemaInfo: tags};
2014-12-30 09:42:31 +01:00
//options.gutters = ['CodeMirror-lint-markers'];
2014-12-30 16:33:28 +01:00
//options.lint = true;
2014-12-09 14:31:25 +01:00
} else {
options.mode = 'javascript';
2014-12-30 09:42:31 +01:00
//options.gutters = ['CodeMirror-lint-markers'];
2014-12-30 16:33:28 +01:00
//options.lint = true;
2014-12-09 14:31:25 +01:00
}
this.editor = CodeMirror.fromTextArea(this.getDOMNode(), options);
2014-12-09 14:31:25 +01:00
if (!this.props.readOnly) {
this.editor.on('change', function (/*e*/) {
if (this.props.valueLink) {
this.props.valueLink(this.editor.getValue());
} else if (this.props.onChange) {
this.props.onChange({target: {value: this.editor.getValue()}});
}
}.bind(this));
}
},
2014-12-30 16:33:28 +01:00
//showMessage: function (msg) {
// //var anOption = document.createElement('div');
// //anOption.innerText = msg;
// //anOption.setAttribute('class', 'error-panel');
// //if (this.panel) {
// // this.panel.clear();
// //}
// //this.panel = this.editor.addPanel(anOption, {height: 22}); // {position: 'bottom'}
//},
//clearMessage: function () {
// if (this.panel) {
// this.panel.clear();
// this.panel = null;
// }
//},
2014-12-30 09:42:31 +01:00
annotate: function (annot) {
CMLint.annotate(this.editor, annot);
},
clearAnnotations: function () {
CMLint.clearMarks(this.editor);
},
2014-12-09 14:31:25 +01:00
componentWillUnmount: function () {
this.editor.toTextArea();
2014-12-09 14:31:25 +01:00
}
});
return editor;
});