From 56fde5a448f4c3539f5b489a97d1149a9c66c5f4 Mon Sep 17 00:00:00 2001 From: "G.Ambatte" Date: Sat, 24 Sep 2022 13:34:26 +1200 Subject: [PATCH 1/5] Use cm.replaceSelection instead of split/join text --- client/homebrew/editor/editor.jsx | 14 +------------- shared/naturalcrit/codeEditor/codeEditor.jsx | 4 ++++ 2 files changed, 5 insertions(+), 13 deletions(-) diff --git a/client/homebrew/editor/editor.jsx b/client/homebrew/editor/editor.jsx index de3a53a36..63d7a2d97 100644 --- a/client/homebrew/editor/editor.jsx +++ b/client/homebrew/editor/editor.jsx @@ -80,19 +80,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); }, handleViewChange : function(newView){ diff --git a/shared/naturalcrit/codeEditor/codeEditor.jsx b/shared/naturalcrit/codeEditor/codeEditor.jsx index 6340a58fe..9f342d6d2 100644 --- a/shared/naturalcrit/codeEditor/codeEditor.jsx +++ b/shared/naturalcrit/codeEditor/codeEditor.jsx @@ -229,6 +229,10 @@ const CodeEditor = createClass({ this.codeMirror.replaceSelection('\n\\page\n\n', 'end'); }, + injectText : function(injectText) { + this.codeMirror.replaceSelection(injectText, 'around'); + }, + 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'); From 22896470e39dc958e9024f0fff10f8ffc8ea76a2 Mon Sep 17 00:00:00 2001 From: "G.Ambatte" Date: Sat, 24 Sep 2022 13:36:14 +1200 Subject: [PATCH 2/5] Remove unused `splice` function --- client/homebrew/editor/editor.jsx | 5 ----- 1 file changed, 5 deletions(-) diff --git a/client/homebrew/editor/editor.jsx b/client/homebrew/editor/editor.jsx index 63d7a2d97..e1dc9fd64 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', From db174c9655d795000b5f1f8923d6e7a2d2234560 Mon Sep 17 00:00:00 2001 From: "G.Ambatte" Date: Sat, 24 Sep 2022 13:50:26 +1200 Subject: [PATCH 3/5] Add optional overwrite protection --- client/homebrew/editor/editor.jsx | 2 +- shared/naturalcrit/codeEditor/codeEditor.jsx | 8 ++++++-- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/client/homebrew/editor/editor.jsx b/client/homebrew/editor/editor.jsx index e1dc9fd64..07946c132 100644 --- a/client/homebrew/editor/editor.jsx +++ b/client/homebrew/editor/editor.jsx @@ -75,7 +75,7 @@ const Editor = createClass({ }, handleInject : function(injectText){ - this.refs.codeEditor?.injectText(injectText); + this.refs.codeEditor?.injectText(injectText, false); }, handleViewChange : function(newView){ diff --git a/shared/naturalcrit/codeEditor/codeEditor.jsx b/shared/naturalcrit/codeEditor/codeEditor.jsx index 9f342d6d2..562891d15 100644 --- a/shared/naturalcrit/codeEditor/codeEditor.jsx +++ b/shared/naturalcrit/codeEditor/codeEditor.jsx @@ -229,8 +229,12 @@ const CodeEditor = createClass({ this.codeMirror.replaceSelection('\n\\page\n\n', 'end'); }, - injectText : function(injectText) { - this.codeMirror.replaceSelection(injectText, 'around'); + injectText : function(injectText, overwrite=true) { + const cm = this.codeMirror; + if(!overwrite) { + cm.setCursor(cm.getCursor('from')); + } + cm.replaceSelection(injectText, 'around'); }, makeUnderline : function() { From de86a77dfc894122c232e48cfb6f083ef4ce37aa Mon Sep 17 00:00:00 2001 From: "G.Ambatte" Date: Tue, 27 Sep 2022 17:06:48 +1300 Subject: [PATCH 4/5] Change post-insertion selection option --- shared/naturalcrit/codeEditor/codeEditor.jsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/shared/naturalcrit/codeEditor/codeEditor.jsx b/shared/naturalcrit/codeEditor/codeEditor.jsx index fe63fdd15..3ca0e61e4 100644 --- a/shared/naturalcrit/codeEditor/codeEditor.jsx +++ b/shared/naturalcrit/codeEditor/codeEditor.jsx @@ -234,7 +234,7 @@ const CodeEditor = createClass({ if(!overwrite) { cm.setCursor(cm.getCursor('from')); } - cm.replaceSelection(injectText, 'around'); + cm.replaceSelection(injectText, 'end'); }, makeUnderline : function() { From 3327712253d65529d7cddf46a89a9a5904bb430d Mon Sep 17 00:00:00 2001 From: Trevor Buckner Date: Tue, 18 Oct 2022 13:52:22 -0400 Subject: [PATCH 5/5] Focus on editor after snippet --- shared/naturalcrit/codeEditor/codeEditor.jsx | 1 + 1 file changed, 1 insertion(+) diff --git a/shared/naturalcrit/codeEditor/codeEditor.jsx b/shared/naturalcrit/codeEditor/codeEditor.jsx index 3ca0e61e4..245317910 100644 --- a/shared/naturalcrit/codeEditor/codeEditor.jsx +++ b/shared/naturalcrit/codeEditor/codeEditor.jsx @@ -235,6 +235,7 @@ const CodeEditor = createClass({ cm.setCursor(cm.getCursor('from')); } cm.replaceSelection(injectText, 'end'); + cm.focus(); }, makeUnderline : function() {