From 5258e9f0e6d7957706286e90ae052bca1aabe267 Mon Sep 17 00:00:00 2001 From: Gazook89 <58999374+Gazook89@users.noreply.github.com> Date: Thu, 12 Aug 2021 22:41:34 -0500 Subject: [PATCH 1/8] Add Hotkey Ctrl/Cmd + K to create Link --- shared/naturalcrit/codeEditor/codeEditor.jsx | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/shared/naturalcrit/codeEditor/codeEditor.jsx b/shared/naturalcrit/codeEditor/codeEditor.jsx index 9707bde56..b8ecda048 100644 --- a/shared/naturalcrit/codeEditor/codeEditor.jsx +++ b/shared/naturalcrit/codeEditor/codeEditor.jsx @@ -54,7 +54,9 @@ const CodeEditor = createClass({ 'Ctrl-M' : this.makeSpan, 'Cmd-M' : this.makeSpan, 'Ctrl-/' : this.makeComment, - 'Cmd-/' : this.makeComment + 'Cmd-/' : this.makeComment, + 'Ctrl-K' : this.makeLink, + 'Cmd-K' : this.makeLink } }); @@ -99,6 +101,15 @@ const CodeEditor = createClass({ } }, + makeLink : function() { + const selection = this.codeMirror.getSelection(), t = selection.slice(0, 1) === '[' && selection.slice(-3) === ']()'; + this.codeMirror.replaceSelection(t ? selection.slice(1, -3) : `[${selection}]()`, 'around'); + if(selection.length === 0){ + const cursor = this.codeMirror.getCursor(); + this.codeMirror.setCursor({ line: cursor.line, ch: cursor.ch - 3 }); + } + }, + //=-- Externally used -==// setCursorPosition : function(line, char){ setTimeout(()=>{ From b89c10a298f1867c12ce72c686bbf8b4e8984481 Mon Sep 17 00:00:00 2001 From: Gazook89 <58999374+Gazook89@users.noreply.github.com> Date: Thu, 12 Aug 2021 22:48:42 -0500 Subject: [PATCH 2/8] Change cursor finish position Change cursor finish position --- shared/naturalcrit/codeEditor/codeEditor.jsx | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/shared/naturalcrit/codeEditor/codeEditor.jsx b/shared/naturalcrit/codeEditor/codeEditor.jsx index b8ecda048..2b8879f7c 100644 --- a/shared/naturalcrit/codeEditor/codeEditor.jsx +++ b/shared/naturalcrit/codeEditor/codeEditor.jsx @@ -104,10 +104,8 @@ const CodeEditor = createClass({ makeLink : function() { const selection = this.codeMirror.getSelection(), t = selection.slice(0, 1) === '[' && selection.slice(-3) === ']()'; this.codeMirror.replaceSelection(t ? selection.slice(1, -3) : `[${selection}]()`, 'around'); - if(selection.length === 0){ - const cursor = this.codeMirror.getCursor(); - this.codeMirror.setCursor({ line: cursor.line, ch: cursor.ch - 3 }); - } + const cursor = this.codeMirror.getCursor(); + this.codeMirror.setCursor({ line: cursor.line, ch: cursor.ch - 1 }); }, //=-- Externally used -==// From 727a58f56d285a34bb3cf6cfb622499d34667dc3 Mon Sep 17 00:00:00 2001 From: Gazook89 <58999374+Gazook89@users.noreply.github.com> Date: Fri, 13 Aug 2021 08:28:02 -0500 Subject: [PATCH 3/8] Fixed cursor finish position - tested on single words and multi word strings - tested removal of hyperlink - tested with highlighting selection from left to right, and right to left. - tested on empty strings --- shared/naturalcrit/codeEditor/codeEditor.jsx | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/shared/naturalcrit/codeEditor/codeEditor.jsx b/shared/naturalcrit/codeEditor/codeEditor.jsx index 2b8879f7c..7f0c315fe 100644 --- a/shared/naturalcrit/codeEditor/codeEditor.jsx +++ b/shared/naturalcrit/codeEditor/codeEditor.jsx @@ -103,9 +103,12 @@ const CodeEditor = createClass({ makeLink : function() { const selection = this.codeMirror.getSelection(), t = selection.slice(0, 1) === '[' && selection.slice(-3) === ']()'; - this.codeMirror.replaceSelection(t ? selection.slice(1, -3) : `[${selection}]()`, 'around'); - const cursor = this.codeMirror.getCursor(); - this.codeMirror.setCursor({ line: cursor.line, ch: cursor.ch - 1 }); + this.codeMirror.replaceSelection(t ? selection.slice(1, -3) : `[${selection}]()`); + if ((selection.slice(0, 1) !== '[' || selection.slice(-3) !== ']()') || selection.length === 0){ + const cursor = this.codeMirror.getCursor(); + console.log('hello'); + this.codeMirror.setCursor({ line: cursor.line, ch: cursor.ch - 1 }); + } }, //=-- Externally used -==// From 8c03b453b2eeabb761829329cac6c18bc80b9e59 Mon Sep 17 00:00:00 2001 From: Gazook89 <58999374+Gazook89@users.noreply.github.com> Date: Fri, 13 Aug 2021 08:31:41 -0500 Subject: [PATCH 4/8] circleCI fix --- 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 7f0c315fe..85214b1d1 100644 --- a/shared/naturalcrit/codeEditor/codeEditor.jsx +++ b/shared/naturalcrit/codeEditor/codeEditor.jsx @@ -104,7 +104,7 @@ const CodeEditor = createClass({ makeLink : function() { const selection = this.codeMirror.getSelection(), t = selection.slice(0, 1) === '[' && selection.slice(-3) === ']()'; this.codeMirror.replaceSelection(t ? selection.slice(1, -3) : `[${selection}]()`); - if ((selection.slice(0, 1) !== '[' || selection.slice(-3) !== ']()') || selection.length === 0){ + if((selection.slice(0, 1) !== '[' || selection.slice(-3) !== ']()') || selection.length === 0){ const cursor = this.codeMirror.getCursor(); console.log('hello'); this.codeMirror.setCursor({ line: cursor.line, ch: cursor.ch - 1 }); From ae75eb07b70858bc98fbff1ee65f055d7d27b3b0 Mon Sep 17 00:00:00 2001 From: Gazook89 <58999374+Gazook89@users.noreply.github.com> Date: Sun, 5 Sep 2021 20:33:51 -0500 Subject: [PATCH 5/8] matched functionality of link hotkey in github --- shared/naturalcrit/codeEditor/codeEditor.jsx | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/shared/naturalcrit/codeEditor/codeEditor.jsx b/shared/naturalcrit/codeEditor/codeEditor.jsx index 85214b1d1..cf08de0b3 100644 --- a/shared/naturalcrit/codeEditor/codeEditor.jsx +++ b/shared/naturalcrit/codeEditor/codeEditor.jsx @@ -102,12 +102,11 @@ const CodeEditor = createClass({ }, makeLink : function() { - const selection = this.codeMirror.getSelection(), t = selection.slice(0, 1) === '[' && selection.slice(-3) === ']()'; - this.codeMirror.replaceSelection(t ? selection.slice(1, -3) : `[${selection}]()`); - if((selection.slice(0, 1) !== '[' || selection.slice(-3) !== ']()') || selection.length === 0){ + 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 cursor = this.codeMirror.getCursor(); - console.log('hello'); - this.codeMirror.setCursor({ line: cursor.line, ch: cursor.ch - 1 }); + this.codeMirror.setSelection({ line: cursor.line, ch: cursor.ch - 4 }, { line: cursor.line, ch: cursor.ch - 1 }); } }, 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 6/8] 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 }); } From b3e37dd2c10612c288e4c7d87ba9ea02a886cf6b Mon Sep 17 00:00:00 2001 From: Gazook89 <58999374+Gazook89@users.noreply.github.com> Date: Thu, 23 Sep 2021 01:01:02 -0500 Subject: [PATCH 7/8] add comment about trying capture groups in future. --- shared/naturalcrit/codeEditor/codeEditor.jsx | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/shared/naturalcrit/codeEditor/codeEditor.jsx b/shared/naturalcrit/codeEditor/codeEditor.jsx index f49cd29b1..c39c43da2 100644 --- a/shared/naturalcrit/codeEditor/codeEditor.jsx +++ b/shared/naturalcrit/codeEditor/codeEditor.jsx @@ -105,8 +105,8 @@ const CodeEditor = createClass({ 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); + const altText = selection.slice(1, selection.lastIndexOf('](')); // could likely be done better with capture groups + const url = selection.slice(selection.lastIndexOf('](') + 2, -1); // could likely be done better with capture groups 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 }); From 2586a871e1fd48e6bf34137d01b6fc936cdbeb9f Mon Sep 17 00:00:00 2001 From: Trevor Buckner Date: Thu, 2 Dec 2021 11:45:10 -0500 Subject: [PATCH 8/8] Use capture groups, add default "alt text" if none selected Also allow whitespace at end and start of selection, so you don't have to be perfect. --- shared/naturalcrit/codeEditor/codeEditor.jsx | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/shared/naturalcrit/codeEditor/codeEditor.jsx b/shared/naturalcrit/codeEditor/codeEditor.jsx index 495166c1b..b7bb8b435 100644 --- a/shared/naturalcrit/codeEditor/codeEditor.jsx +++ b/shared/naturalcrit/codeEditor/codeEditor.jsx @@ -156,16 +156,17 @@ const CodeEditor = createClass({ }, makeLink : function() { - const isLink = /^\[(.*)(\]\()(.*)\)$/g; - const selection = this.codeMirror.getSelection(); - if(isLink.test(selection) == true){ - const altText = selection.slice(1, selection.lastIndexOf('](')); // could likely be done better with capture groups - const url = selection.slice(selection.lastIndexOf('](') + 2, -1); // could likely be done better with capture groups + const isLink = /^\[(.*)\]\((.*)\)$/; + const selection = this.codeMirror.getSelection().trim(); + let match; + if(match = isLink.exec(selection)){ + const altText = match[1]; + const url = match[2]; 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)`); + this.codeMirror.replaceSelection(`[${selection || 'alt text'}](url)`); const cursor = this.codeMirror.getCursor(); this.codeMirror.setSelection({ line: cursor.line, ch: cursor.ch - 4 }, { line: cursor.line, ch: cursor.ch - 1 }); }