From 3bf5d7a2dbe44846a194ee99ae85bfcca1e29832 Mon Sep 17 00:00:00 2001 From: Gazook89 <58999374+Gazook89@users.noreply.github.com> Date: Thu, 30 Sep 2021 17:59:45 -0500 Subject: [PATCH] change makeComment function to adapt to gfm or css editor --- shared/naturalcrit/codeEditor/codeEditor.jsx | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/shared/naturalcrit/codeEditor/codeEditor.jsx b/shared/naturalcrit/codeEditor/codeEditor.jsx index a810bee3a..ee318ee1d 100644 --- a/shared/naturalcrit/codeEditor/codeEditor.jsx +++ b/shared/naturalcrit/codeEditor/codeEditor.jsx @@ -155,12 +155,24 @@ const CodeEditor = createClass({ }, makeComment : function() { - const selection = this.codeMirror.getSelection(), t = selection.slice(0, 4) === ''; - this.codeMirror.replaceSelection(t ? selection.slice(4, -3) : ``, 'around'); + let regex; + let cursorPos; + let newComment; + const selection = this.codeMirror.getSelection(); + if(this.props.language === 'gfm'){ + regex = /^\s*()\s*$/gs; + cursorPos = 4; + newComment = ``; + } else { + regex = /^\s*(\/\*\s?)(.*?)(\s?\*\/)\s*$/gs; + cursorPos = 3; + newComment = `/* ${selection} */`; + } + this.codeMirror.replaceSelection(regex.test(selection) == true ? selection.replace(regex, '$2') : newComment, 'around'); if(selection.length === 0){ const cursor = this.codeMirror.getCursor(); - this.codeMirror.setCursor({ line: cursor.line, ch: cursor.ch - 4 }); - } + this.codeMirror.setCursor({ line: cursor.line, ch: cursor.ch - cursorPos }); + }; }, //=-- Externally used -==//