summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDouwe Maan <douwe@selenight.nl>2017-05-18 22:17:59 -0500
committerDouwe Maan <douwe@selenight.nl>2017-05-19 07:26:28 -0500
commit7e0a4c5e577769dddc4aec1cfec1db3064a3ff42 (patch)
tree822ac0d55c6eeab802c144dbe86c60da9665c4a0
parent36ede00876aacd0005eac8e9b4779e3014519e0c (diff)
downloadgitlab-ce-7e0a4c5e577769dddc4aec1cfec1db3064a3ff42.tar.gz
Don't wrap pasted code when it's already inside code tags
-rw-r--r--app/assets/javascripts/copy_as_gfm.js19
-rw-r--r--app/assets/javascripts/lib/utils/common_utils.js6
-rw-r--r--changelogs/unreleased/dm-paste-code-inside-gfm-code.yml4
3 files changed, 26 insertions, 3 deletions
diff --git a/app/assets/javascripts/copy_as_gfm.js b/app/assets/javascripts/copy_as_gfm.js
index 570799c030e..9d559b15cd4 100644
--- a/app/assets/javascripts/copy_as_gfm.js
+++ b/app/assets/javascripts/copy_as_gfm.js
@@ -299,12 +299,29 @@ class CopyAsGFM {
const clipboardData = e.originalEvent.clipboardData;
if (!clipboardData) return;
+ const text = clipboardData.getData('text/plain');
const gfm = clipboardData.getData('text/x-gfm');
if (!gfm) return;
e.preventDefault();
- window.gl.utils.insertText(e.target, gfm);
+ window.gl.utils.insertText(e.target, (textBefore, textAfter) => {
+ // If the text before the cursor contains an odd number of backticks,
+ // we are either inside an inline code span that starts with 1 backtick
+ // or a code block that starts with 3 backticks.
+ // This logic still holds when there are one or more _closed_ code spans
+ // or blocks that will have 2 or 6 backticks.
+ // This will break down when the actual code block contains an uneven
+ // number of backticks, but this is a rare edge case.
+ const backtickMatch = textBefore.match(/`/g);
+ const insideCodeBlock = backtickMatch && (backtickMatch.length % 2) === 1;
+
+ if (insideCodeBlock) {
+ return text;
+ }
+
+ return gfm;
+ });
}
static transformGFMSelection(documentFragment) {
diff --git a/app/assets/javascripts/lib/utils/common_utils.js b/app/assets/javascripts/lib/utils/common_utils.js
index 7e62773ae6c..a537267643e 100644
--- a/app/assets/javascripts/lib/utils/common_utils.js
+++ b/app/assets/javascripts/lib/utils/common_utils.js
@@ -198,10 +198,12 @@
const textBefore = value.substring(0, selectionStart);
const textAfter = value.substring(selectionEnd, value.length);
- const newText = textBefore + text + textAfter;
+
+ const insertedText = text instanceof Function ? text(textBefore, textAfter) : text;
+ const newText = textBefore + insertedText + textAfter;
target.value = newText;
- target.selectionStart = target.selectionEnd = selectionStart + text.length;
+ target.selectionStart = target.selectionEnd = selectionStart + insertedText.length;
// Trigger autosave
$(target).trigger('input');
diff --git a/changelogs/unreleased/dm-paste-code-inside-gfm-code.yml b/changelogs/unreleased/dm-paste-code-inside-gfm-code.yml
new file mode 100644
index 00000000000..d078ca449a5
--- /dev/null
+++ b/changelogs/unreleased/dm-paste-code-inside-gfm-code.yml
@@ -0,0 +1,4 @@
+---
+title: Don't wrap pasted code when it's already inside code tags
+merge_request:
+author: