From beb3c7ec89d10ffa2838bfc2602a018a0c032789 Mon Sep 17 00:00:00 2001 From: Gazook89 <58999374+Gazook89@users.noreply.github.com> Date: Thu, 23 Sep 2021 00:22:40 -0500 Subject: [PATCH] make undo behavior work regardless of url text --- shared/naturalcrit/codeEditor/codeEditor.jsx | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/shared/naturalcrit/codeEditor/codeEditor.jsx b/shared/naturalcrit/codeEditor/codeEditor.jsx index cf08de0b3..f49cd29b1 100644 --- a/shared/naturalcrit/codeEditor/codeEditor.jsx +++ b/shared/naturalcrit/codeEditor/codeEditor.jsx @@ -102,9 +102,16 @@ const CodeEditor = createClass({ }, makeLink : function() { - const selection = this.codeMirror.getSelection(), t = selection.slice(0, 1) === '[' && selection.slice(-6) === '](url)'; - this.codeMirror.replaceSelection(t ? selection.slice(1, -6) : `[${selection}](url)`); - if((selection.slice(0, 1) !== '[' || selection.slice(-6) !== '](url)') || selection.length === 0){ + const isLink = /^\[(.*)(\]\()(.*)\)$/g; + const selection = this.codeMirror.getSelection(); + if(isLink.test(selection) == true){ + const altText = selection.slice(1, selection.lastIndexOf('](')); + const url = selection.slice(selection.lastIndexOf('](') + 2, -1); + 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}](url)`); const cursor = this.codeMirror.getCursor(); this.codeMirror.setSelection({ line: cursor.line, ch: cursor.ch - 4 }, { line: cursor.line, ch: cursor.ch - 1 }); }