Commit eb2bd98b authored by Phil Hughes's avatar Phil Hughes

Merge branch 'master' into 'master'

Add basic function to transform selected text to markdown and insert it as quote

See merge request gitlab-org/gitlab!36753
parents 0aa28970 d920ae02
......@@ -180,6 +180,10 @@ export class CopyAsGFM {
})
.catch(() => {});
}
static quoted(markdown) {
return `> ${markdown.split('\n').join('\n> ')}`;
}
}
// Export CopyAsGFM as a global for rspec to access
......
......@@ -308,9 +308,11 @@ export function addMarkdownListeners(form) {
.off('click')
.on('click', function() {
const $this = $(this);
const tag = this.dataset.mdTag;
return updateText({
textArea: $this.closest('.md-area').find('textarea'),
tag: $this.data('mdTag'),
tag,
cursorOffset: $this.data('mdCursorOffset'),
blockTag: $this.data('mdBlock'),
wrap: !$this.data('mdPrepend'),
......
<script>
import $ from 'jquery';
import { GlPopover, GlButton, GlTooltipDirective } from '@gitlab/ui';
import { getSelectedFragment } from '~/lib/utils/common_utils';
import { CopyAsGFM } from '../../../behaviors/markdown/copy_as_gfm';
import ToolbarButton from './toolbar_button.vue';
import Icon from '../icon.vue';
......@@ -35,6 +37,11 @@ export default {
default: false,
},
},
data() {
return {
tag: '> ',
};
},
computed: {
mdTable() {
return [
......@@ -81,6 +88,24 @@ export default {
handleSuggestDismissed() {
this.$emit('handleSuggestDismissed');
},
handleQuote() {
const documentFragment = getSelectedFragment();
if (!documentFragment || !documentFragment.textContent) {
this.tag = '> ';
return;
}
this.tag = '';
const transformed = CopyAsGFM.transformGFMSelection(documentFragment);
const area = this.$el.parentNode.querySelector('textarea');
CopyAsGFM.nodeToGFM(transformed)
.then(gfm => {
CopyAsGFM.insertPastedText(area, documentFragment.textContent, CopyAsGFM.quoted(gfm));
})
.catch(() => {});
},
},
};
</script>
......@@ -108,9 +133,10 @@ export default {
<toolbar-button tag="*" :button-title="__('Add italic text')" icon="italic" />
<toolbar-button
:prepend="true"
tag="> "
:tag="tag"
:button-title="__('Insert a quote')"
icon="quote"
@click="handleQuote"
/>
</div>
<div class="d-inline-block ml-md-2 ml-0">
......
......@@ -123,4 +123,14 @@ describe('CopyAsGFM', () => {
});
});
});
describe('CopyAsGFM.quoted', () => {
const sampleGFM = '* List 1\n* List 2\n\n`Some code`';
it('adds quote char `> ` to each line', done => {
const expectedQuotedGFM = '> * List 1\n> * List 2\n> \n> `Some code`';
expect(CopyAsGFM.quoted(sampleGFM)).toEqual(expectedQuotedGFM);
done();
});
});
});
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