diff --git a/client/homebrew/editor/editor.jsx b/client/homebrew/editor/editor.jsx index de3a53a36..07946c132 100644 --- a/client/homebrew/editor/editor.jsx +++ b/client/homebrew/editor/editor.jsx @@ -19,11 +19,6 @@ const DEFAULT_STYLE_TEXT = dedent` color: black; }`; -const splice = function(str, index, inject){ - return str.slice(0, index) + inject + str.slice(index); -}; - - const Editor = createClass({ displayName : 'Editor', @@ -80,19 +75,7 @@ const Editor = createClass({ }, handleInject : function(injectText){ - let text; - if(this.isText()) text = this.props.brew.text; - if(this.isStyle()) text = this.props.brew.style ?? DEFAULT_STYLE_TEXT; - - const lines = text.split('\n'); - const cursorPos = this.refs.codeEditor.getCursorPosition(); - lines[cursorPos.line] = splice(lines[cursorPos.line], cursorPos.ch, injectText); - - const injectLines = injectText.split('\n'); - this.refs.codeEditor.setCursorPosition(cursorPos.line + injectLines.length, cursorPos.ch + injectLines[injectLines.length - 1].length); - - if(this.isText()) this.props.onTextChange(lines.join('\n')); - if(this.isStyle()) this.props.onStyleChange(lines.join('\n')); + this.refs.codeEditor?.injectText(injectText, false); }, handleViewChange : function(newView){ diff --git a/shared/naturalcrit/codeEditor/codeEditor.jsx b/shared/naturalcrit/codeEditor/codeEditor.jsx index da1390511..245317910 100644 --- a/shared/naturalcrit/codeEditor/codeEditor.jsx +++ b/shared/naturalcrit/codeEditor/codeEditor.jsx @@ -229,6 +229,15 @@ const CodeEditor = createClass({ this.codeMirror.replaceSelection('\n\\page\n\n', 'end'); }, + injectText : function(injectText, overwrite=true) { + const cm = this.codeMirror; + if(!overwrite) { + cm.setCursor(cm.getCursor('from')); + } + cm.replaceSelection(injectText, 'end'); + cm.focus(); + }, + makeUnderline : function() { const selection = this.codeMirror.getSelection(), t = selection.slice(0, 3) === '' && selection.slice(-4) === ''; this.codeMirror.replaceSelection(t ? selection.slice(3, -4) : `${selection}`, 'around');