From 6309ec0bfafcf5fd0c18b72a2fc9b9b8a073e9df Mon Sep 17 00:00:00 2001 From: Rodrigo Kuerten Date: Sat, 24 Oct 2020 16:59:26 -0300 Subject: [PATCH] Updated makeBold and makeItalic functions to center cursor when selection is empty --- shared/naturalcrit/codeEditor/codeEditor.jsx | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/shared/naturalcrit/codeEditor/codeEditor.jsx b/shared/naturalcrit/codeEditor/codeEditor.jsx index 26ec17be5..e29c561bd 100644 --- a/shared/naturalcrit/codeEditor/codeEditor.jsx +++ b/shared/naturalcrit/codeEditor/codeEditor.jsx @@ -45,12 +45,24 @@ const CodeEditor = createClass({ makeBold : function() { const selection = this.codeMirror.getSelection(); - this.codeMirror.replaceSelection(`**${selection}**`, 'around'); + if (selection.length === 0){ + this.codeMirror.replaceSelection(`**${selection}**`, 'around'); + let cursor = this.codeMirror.getCursor(); + this.codeMirror.setCursor({ line: cursor.line, ch: cursor.ch - 2 }); + } else { + this.codeMirror.replaceSelection(`**${selection}**`, 'around'); + } }, makeItalic : function() { const selection = this.codeMirror.getSelection(); - this.codeMirror.replaceSelection(`*${selection}*`, 'around'); + if (selection.length === 0){ + this.codeMirror.replaceSelection(`*${selection}*`, 'around'); + let cursor = this.codeMirror.getCursor(); + this.codeMirror.setCursor({ line: cursor.line, ch: cursor.ch - 1 }); + } else { + this.codeMirror.replaceSelection(`*${selection}*`, 'around'); + } }, componentWillReceiveProps : function(nextProps){