Commit ed0423c7 authored by Jochen Roth's avatar Jochen Roth

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

parent 05225715
...@@ -180,6 +180,10 @@ export class CopyAsGFM { ...@@ -180,6 +180,10 @@ export class CopyAsGFM {
}) })
.catch(() => {}); .catch(() => {});
} }
static quoted(markdown) {
return `> ${markdown.split('\n').join('\n> ')}`;
}
} }
// Export CopyAsGFM as a global for rspec to access // Export CopyAsGFM as a global for rspec to access
......
...@@ -308,9 +308,11 @@ export function addMarkdownListeners(form) { ...@@ -308,9 +308,11 @@ export function addMarkdownListeners(form) {
.off('click') .off('click')
.on('click', function() { .on('click', function() {
const $this = $(this); const $this = $(this);
const tag = this.dataset.mdTag;
return updateText({ return updateText({
textArea: $this.closest('.md-area').find('textarea'), textArea: $this.closest('.md-area').find('textarea'),
tag: $this.data('mdTag'), tag,
cursorOffset: $this.data('mdCursorOffset'), cursorOffset: $this.data('mdCursorOffset'),
blockTag: $this.data('mdBlock'), blockTag: $this.data('mdBlock'),
wrap: !$this.data('mdPrepend'), wrap: !$this.data('mdPrepend'),
......
<script> <script>
import $ from 'jquery'; import $ from 'jquery';
import { GlPopover, GlButton, GlTooltipDirective } from '@gitlab/ui'; 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 ToolbarButton from './toolbar_button.vue';
import Icon from '../icon.vue'; import Icon from '../icon.vue';
...@@ -35,6 +37,11 @@ export default { ...@@ -35,6 +37,11 @@ export default {
default: false, default: false,
}, },
}, },
data() {
return {
tag: '> ',
};
},
computed: { computed: {
mdTable() { mdTable() {
return [ return [
...@@ -81,6 +88,24 @@ export default { ...@@ -81,6 +88,24 @@ export default {
handleSuggestDismissed() { handleSuggestDismissed() {
this.$emit('handleSuggestDismissed'); this.$emit('handleSuggestDismissed');
}, },
handleQuote() {
const documentFragment = getSelectedFragment();
const area = this.$parent.$slots.textarea[0].elm;
if (!documentFragment || !documentFragment.textContent) {
this.tag = '> ';
return;
}
this.tag = '';
const transformed = CopyAsGFM.transformGFMSelection(documentFragment);
CopyAsGFM.nodeToGFM(transformed)
.then(gfm => {
CopyAsGFM.insertPastedText(area, documentFragment.textContent, CopyAsGFM.quoted(gfm));
})
.catch(() => {});
},
}, },
}; };
</script> </script>
...@@ -108,9 +133,10 @@ export default { ...@@ -108,9 +133,10 @@ export default {
<toolbar-button tag="*" :button-title="__('Add italic text')" icon="italic" /> <toolbar-button tag="*" :button-title="__('Add italic text')" icon="italic" />
<toolbar-button <toolbar-button
:prepend="true" :prepend="true"
tag="> " :tag="tag"
:button-title="__('Insert a quote')" :button-title="__('Insert a quote')"
icon="quote" icon="quote"
@update-tag="handleQuote"
/> />
</div> </div>
<div class="d-inline-block ml-md-2 ml-0"> <div class="d-inline-block ml-md-2 ml-0">
......
...@@ -65,7 +65,7 @@ export default { ...@@ -65,7 +65,7 @@ export default {
type="button" type="button"
class="toolbar-btn js-md" class="toolbar-btn js-md"
data-container="body" data-container="body"
@click="() => $emit('click')" @click="$emit('update-tag'), () => $emit('click')"
> >
<icon :name="icon" /> <icon :name="icon" />
</button> </button>
......
...@@ -123,4 +123,14 @@ describe('CopyAsGFM', () => { ...@@ -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