mirror of
https://github.com/naturalcrit/homebrewery.git
synced 2026-01-06 16:32:40 +00:00
Finish Merge with Master
This commit is contained in:
@@ -13,6 +13,13 @@ if(typeof navigator !== 'undefined'){
|
|||||||
require('codemirror/mode/gfm/gfm.js'); //Github flavoured markdown
|
require('codemirror/mode/gfm/gfm.js'); //Github flavoured markdown
|
||||||
require('codemirror/mode/css/css.js');
|
require('codemirror/mode/css/css.js');
|
||||||
require('codemirror/mode/javascript/javascript.js');
|
require('codemirror/mode/javascript/javascript.js');
|
||||||
|
|
||||||
|
//Addons
|
||||||
|
require('codemirror/addon/fold/foldcode.js');
|
||||||
|
require('codemirror/addon/fold/foldgutter.js');
|
||||||
|
|
||||||
|
const foldCode = require('./fold-code');
|
||||||
|
foldCode.registerHomebreweryHelper(CodeMirror);
|
||||||
}
|
}
|
||||||
|
|
||||||
const CodeEditor = createClass({
|
const CodeEditor = createClass({
|
||||||
@@ -25,28 +32,48 @@ const CodeEditor = createClass({
|
|||||||
};
|
};
|
||||||
},
|
},
|
||||||
|
|
||||||
|
getInitialState : function() {
|
||||||
|
return {
|
||||||
|
docs : {}
|
||||||
|
};
|
||||||
|
},
|
||||||
|
|
||||||
componentDidMount : function() {
|
componentDidMount : function() {
|
||||||
this.buildEditor();
|
this.buildEditor();
|
||||||
|
const newDoc = CodeMirror.Doc(this.props.value, this.props.language);
|
||||||
|
this.codeMirror.swapDoc(newDoc);
|
||||||
},
|
},
|
||||||
|
|
||||||
componentDidUpdate : function(prevProps) {
|
componentDidUpdate : function(prevProps) {
|
||||||
if(prevProps.language !== this.props.language){ //rebuild editor when switching tabs
|
if(prevProps.view !== this.props.view){ //view changed; swap documents
|
||||||
this.buildEditor();
|
let newDoc;
|
||||||
}
|
|
||||||
if(this.codeMirror && this.codeMirror.getValue() != this.props.value) { //update editor contents if brew.text is changed from outside
|
if(!this.state.docs[this.props.view]) {
|
||||||
|
newDoc = CodeMirror.Doc(this.props.value, this.props.language);
|
||||||
|
} else {
|
||||||
|
newDoc = this.state.docs[this.props.view];
|
||||||
|
}
|
||||||
|
|
||||||
|
const oldDoc = { [prevProps.view]: this.codeMirror.swapDoc(newDoc) };
|
||||||
|
|
||||||
|
this.setState((prevState)=>({
|
||||||
|
docs : _.merge({}, prevState.docs, oldDoc)
|
||||||
|
}));
|
||||||
|
|
||||||
|
this.props.rerenderParent();
|
||||||
|
} else if(this.codeMirror?.getValue() != this.props.value) { //update editor contents if brew.text is changed from outside
|
||||||
this.codeMirror.setValue(this.props.value);
|
this.codeMirror.setValue(this.props.value);
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
buildEditor : function() {
|
buildEditor : function() {
|
||||||
this.codeMirror = CodeMirror(this.refs.editor, {
|
this.codeMirror = CodeMirror(this.refs.editor, {
|
||||||
value : this.props.value,
|
lineNumbers : true,
|
||||||
lineNumbers : true,
|
lineWrapping : this.props.wrap,
|
||||||
lineWrapping : this.props.wrap,
|
indentWithTabs : true,
|
||||||
mode : this.props.language, //TODO: CSS MODE DOESN'T SEEM TO LOAD PROPERLY
|
tabSize : 2,
|
||||||
indentWithTabs : true,
|
historyEventDelay : 250,
|
||||||
tabSize : 2,
|
extraKeys : {
|
||||||
extraKeys : {
|
|
||||||
'Ctrl-B' : this.makeBold,
|
'Ctrl-B' : this.makeBold,
|
||||||
'Cmd-B' : this.makeBold,
|
'Cmd-B' : this.makeBold,
|
||||||
'Ctrl-I' : this.makeItalic,
|
'Ctrl-I' : this.makeItalic,
|
||||||
@@ -59,17 +86,46 @@ const CodeEditor = createClass({
|
|||||||
'Shift-Cmd-.' : this.makeSpace,
|
'Shift-Cmd-.' : this.makeSpace,
|
||||||
'Shift-Ctrl-,' : this.removeSpace,
|
'Shift-Ctrl-,' : this.removeSpace,
|
||||||
'Shift-Cmd-,' : this.removeSpace,
|
'Shift-Cmd-,' : this.removeSpace,
|
||||||
'Shift-Ctrl-Enter' : this.newColumn,
|
|
||||||
'Shift-Cmd-Enter' : this.newColumn,
|
|
||||||
'Ctrl-Enter' : this.newPage,
|
|
||||||
'Cmd-Enter' : this.newPage,
|
|
||||||
'Ctrl-M' : this.makeSpan,
|
'Ctrl-M' : this.makeSpan,
|
||||||
'Cmd-M' : this.makeSpan,
|
'Cmd-M' : this.makeSpan,
|
||||||
'Shift-Ctrl-M' : this.makeDiv,
|
'Shift-Ctrl-M' : this.makeDiv,
|
||||||
'Shift-Cmd-M' : this.makeDiv,
|
'Shift-Cmd-M' : this.makeDiv,
|
||||||
'Ctrl-/' : this.makeComment,
|
'Ctrl-/' : this.makeComment,
|
||||||
'Cmd-/' : this.makeComment
|
'Cmd-/' : this.makeComment,
|
||||||
}
|
'Ctrl-K' : this.makeLink,
|
||||||
|
'Cmd-K' : this.makeLink,
|
||||||
|
'Shift-Ctrl-Enter' : this.newColumn,
|
||||||
|
'Shift-Cmd-Enter' : this.newColumn,
|
||||||
|
'Ctrl-Enter' : this.newPage,
|
||||||
|
'Cmd-Enter' : this.newPage,
|
||||||
|
'Ctrl-[' : this.foldAllCode,
|
||||||
|
'Cmd-[' : this.foldAllCode,
|
||||||
|
'Ctrl-]' : this.unfoldAllCode,
|
||||||
|
'Cmd-]' : this.unfoldAllCode
|
||||||
|
},
|
||||||
|
foldGutter : true,
|
||||||
|
foldOptions : {
|
||||||
|
scanUp : true,
|
||||||
|
rangeFinder : CodeMirror.fold.homebrewery,
|
||||||
|
widget : (from, to)=>{
|
||||||
|
let text = '';
|
||||||
|
let currentLine = from.line;
|
||||||
|
const maxLength = 50;
|
||||||
|
while (currentLine <= to.line && text.length <= maxLength) {
|
||||||
|
text += this.codeMirror.getLine(currentLine);
|
||||||
|
if(currentLine < to.line)
|
||||||
|
text += ' ';
|
||||||
|
currentLine += 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
text = text.trim();
|
||||||
|
if(text.length > maxLength)
|
||||||
|
text = `${text.substr(0, maxLength)}...`;
|
||||||
|
|
||||||
|
return `\u21A4 ${text} \u21A6`;
|
||||||
|
}
|
||||||
|
},
|
||||||
|
gutters : ['CodeMirror-linenumbers', 'CodeMirror-foldgutter']
|
||||||
});
|
});
|
||||||
|
|
||||||
// Note: codeMirror passes a copy of itself in this callback. cm === this.codeMirror. Either one works.
|
// Note: codeMirror passes a copy of itself in this callback. cm === this.codeMirror. Either one works.
|
||||||
@@ -175,6 +231,31 @@ const CodeEditor = createClass({
|
|||||||
};
|
};
|
||||||
},
|
},
|
||||||
|
|
||||||
|
makeLink : function() {
|
||||||
|
const isLink = /^\[(.*)\]\((.*)\)$/;
|
||||||
|
const selection = this.codeMirror.getSelection().trim();
|
||||||
|
let match;
|
||||||
|
if(match = isLink.exec(selection)){
|
||||||
|
const altText = match[1];
|
||||||
|
const url = match[2];
|
||||||
|
this.codeMirror.replaceSelection(`${altText} ${url}`);
|
||||||
|
const cursor = this.codeMirror.getCursor();
|
||||||
|
this.codeMirror.setSelection({ line: cursor.line, ch: cursor.ch - url.length }, { line: cursor.line, ch: cursor.ch });
|
||||||
|
} else {
|
||||||
|
this.codeMirror.replaceSelection(`[${selection || 'alt text'}](url)`);
|
||||||
|
const cursor = this.codeMirror.getCursor();
|
||||||
|
this.codeMirror.setSelection({ line: cursor.line, ch: cursor.ch - 4 }, { line: cursor.line, ch: cursor.ch - 1 });
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
|
foldAllCode : function() {
|
||||||
|
this.codeMirror.execCommand('foldAll');
|
||||||
|
},
|
||||||
|
|
||||||
|
unfoldAllCode : function() {
|
||||||
|
this.codeMirror.execCommand('unfoldAll');
|
||||||
|
},
|
||||||
|
|
||||||
//=-- Externally used -==//
|
//=-- Externally used -==//
|
||||||
setCursorPosition : function(line, char){
|
setCursorPosition : function(line, char){
|
||||||
setTimeout(()=>{
|
setTimeout(()=>{
|
||||||
@@ -188,10 +269,19 @@ const CodeEditor = createClass({
|
|||||||
updateSize : function(){
|
updateSize : function(){
|
||||||
this.codeMirror.refresh();
|
this.codeMirror.refresh();
|
||||||
},
|
},
|
||||||
|
redo : function(){
|
||||||
|
return this.codeMirror.redo();
|
||||||
|
},
|
||||||
|
undo : function(){
|
||||||
|
return this.codeMirror.undo();
|
||||||
|
},
|
||||||
|
historySize : function(){
|
||||||
|
return this.codeMirror.doc.historySize();
|
||||||
|
},
|
||||||
//----------------------//
|
//----------------------//
|
||||||
|
|
||||||
render : function(){
|
render : function(){
|
||||||
return <div className='codeEditor' ref='editor' />;
|
return <div className='codeEditor' ref='editor' style={this.props.style}/>;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user