BigW Consortium Gitlab

Commit 78e7efae by Douwe Maan

Copy as GFM even when parts of other elements are selected

parent c013d23d
...@@ -330,11 +330,27 @@ class CopyAsGFM { ...@@ -330,11 +330,27 @@ class CopyAsGFM {
} }
static transformGFMSelection(documentFragment) { static transformGFMSelection(documentFragment) {
// If the documentFragment contains more than just Markdown, don't copy as GFM. const gfmEls = documentFragment.querySelectorAll('.md, .wiki');
if (documentFragment.querySelector('.md, .wiki')) return null; switch (gfmEls.length) {
case 0: {
return documentFragment; return documentFragment;
} }
case 1: {
return gfmEls[0];
}
default: {
const allGfmEl = document.createElement('div');
for (let i = 0; i < gfmEls.length; i += 1) {
const lineEl = gfmEls[i];
allGfmEl.appendChild(lineEl);
allGfmEl.appendChild(document.createTextNode('\n\n'));
}
return allGfmEl;
}
}
}
static transformCodeSelection(documentFragment) { static transformCodeSelection(documentFragment) {
const lineEls = documentFragment.querySelectorAll('.line'); const lineEls = documentFragment.querySelectorAll('.line');
......
...@@ -38,7 +38,7 @@ import './shortcuts_navigation'; ...@@ -38,7 +38,7 @@ import './shortcuts_navigation';
} }
ShortcutsIssuable.prototype.replyWithSelectedText = function() { ShortcutsIssuable.prototype.replyWithSelectedText = function() {
var quote, documentFragment, selected, separator; var quote, documentFragment, el, selected, separator;
var replyField = $('.js-main-target-form #note_note'); var replyField = $('.js-main-target-form #note_note');
documentFragment = window.gl.utils.getSelectedFragment(); documentFragment = window.gl.utils.getSelectedFragment();
...@@ -47,10 +47,8 @@ import './shortcuts_navigation'; ...@@ -47,10 +47,8 @@ import './shortcuts_navigation';
return; return;
} }
// If the documentFragment contains more than just Markdown, don't copy as GFM. el = window.gl.CopyAsGFM.transformGFMSelection(documentFragment.cloneNode(true));
if (documentFragment.querySelector('.md, .wiki')) return; selected = window.gl.CopyAsGFM.nodeToGFM(el);
selected = window.gl.CopyAsGFM.nodeToGFM(documentFragment);
if (selected.trim() === "") { if (selected.trim() === "") {
return; return;
......
---
title: Copy as GFM even when parts of other elements are selected
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