Commit 7e0a4c5e authored by Douwe Maan's avatar Douwe Maan

Don't wrap pasted code when it's already inside code tags

parent 36ede008
...@@ -299,12 +299,29 @@ class CopyAsGFM { ...@@ -299,12 +299,29 @@ class CopyAsGFM {
const clipboardData = e.originalEvent.clipboardData; const clipboardData = e.originalEvent.clipboardData;
if (!clipboardData) return; if (!clipboardData) return;
const text = clipboardData.getData('text/plain');
const gfm = clipboardData.getData('text/x-gfm'); const gfm = clipboardData.getData('text/x-gfm');
if (!gfm) return; if (!gfm) return;
e.preventDefault(); 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) { static transformGFMSelection(documentFragment) {
......
...@@ -198,10 +198,12 @@ ...@@ -198,10 +198,12 @@
const textBefore = value.substring(0, selectionStart); const textBefore = value.substring(0, selectionStart);
const textAfter = value.substring(selectionEnd, value.length); 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.value = newText;
target.selectionStart = target.selectionEnd = selectionStart + text.length; target.selectionStart = target.selectionEnd = selectionStart + insertedText.length;
// Trigger autosave // Trigger autosave
$(target).trigger('input'); $(target).trigger('input');
......
---
title: Don't wrap pasted code when it's already inside code tags
merge_request:
author:
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment