debian-mirror-gitlab/app/assets/javascripts/vue_shared/components/markdown/header.vue

197 lines
5.8 KiB
Vue
Raw Normal View History

2017-09-10 17:25:29 +05:30
<script>
2018-12-13 13:39:08 +05:30
import $ from 'jquery';
2019-09-04 21:01:54 +05:30
import { GlPopover, GlButton, GlTooltipDirective } from '@gitlab/ui';
2018-12-13 13:39:08 +05:30
import ToolbarButton from './toolbar_button.vue';
import Icon from '../icon.vue';
2017-09-10 17:25:29 +05:30
2018-12-13 13:39:08 +05:30
export default {
components: {
ToolbarButton,
Icon,
2019-09-04 21:01:54 +05:30
GlPopover,
GlButton,
2018-12-13 13:39:08 +05:30
},
directives: {
GlTooltip: GlTooltipDirective,
},
props: {
previewMarkdown: {
type: Boolean,
required: true,
2017-09-10 17:25:29 +05:30
},
2019-02-15 15:39:39 +05:30
lineContent: {
type: String,
required: false,
default: '',
},
canSuggest: {
type: Boolean,
required: false,
default: true,
},
2019-09-04 21:01:54 +05:30
showSuggestPopover: {
type: Boolean,
required: false,
default: false,
},
2018-12-13 13:39:08 +05:30
},
computed: {
mdTable() {
return [
2019-10-12 21:52:04 +05:30
// False positive i18n lint: https://gitlab.com/gitlab-org/frontend/eslint-plugin-i18n/issues/26
'| header | header |', // eslint-disable-line @gitlab/i18n/no-non-i18n-strings
2018-12-13 13:39:08 +05:30
'| ------ | ------ |',
2019-10-12 21:52:04 +05:30
'| cell | cell |', // eslint-disable-line @gitlab/i18n/no-non-i18n-strings
'| cell | cell |', // eslint-disable-line @gitlab/i18n/no-non-i18n-strings
2018-12-13 13:39:08 +05:30
].join('\n');
2017-09-10 17:25:29 +05:30
},
2019-02-15 15:39:39 +05:30
mdSuggestion() {
2019-07-07 11:18:12 +05:30
return ['```suggestion:-0+0', `{text}`, '```'].join('\n');
2019-02-15 15:39:39 +05:30
},
2018-12-13 13:39:08 +05:30
},
mounted() {
$(document).on('markdown-preview:show.vue', this.previewMarkdownTab);
$(document).on('markdown-preview:hide.vue', this.writeMarkdownTab);
},
beforeDestroy() {
$(document).off('markdown-preview:show.vue', this.previewMarkdownTab);
$(document).off('markdown-preview:hide.vue', this.writeMarkdownTab);
},
methods: {
isValid(form) {
return (
!form ||
(form.find('.js-vue-markdown-field').length && $(this.$el).closest('form')[0] === form[0])
);
2017-09-10 17:25:29 +05:30
},
2018-03-17 18:26:18 +05:30
2018-12-13 13:39:08 +05:30
previewMarkdownTab(event, form) {
if (event.target.blur) event.target.blur();
if (!this.isValid(form)) return;
2018-03-17 18:26:18 +05:30
2018-12-13 13:39:08 +05:30
this.$emit('preview-markdown');
},
2018-03-17 18:26:18 +05:30
2018-12-13 13:39:08 +05:30
writeMarkdownTab(event, form) {
if (event.target.blur) event.target.blur();
if (!this.isValid(form)) return;
2018-03-17 18:26:18 +05:30
2018-12-13 13:39:08 +05:30
this.$emit('write-markdown');
2017-09-10 17:25:29 +05:30
},
2019-09-04 21:01:54 +05:30
handleSuggestDismissed() {
this.$emit('handleSuggestDismissed');
},
2018-12-13 13:39:08 +05:30
},
};
2017-09-10 17:25:29 +05:30
</script>
<template>
<div class="md-header">
<ul class="nav-links clearfix">
2019-02-15 15:39:39 +05:30
<li :class="{ active: !previewMarkdown }" class="md-header-tab">
2019-03-02 22:35:43 +05:30
<button class="js-write-link" tabindex="-1" type="button" @click="writeMarkdownTab($event)">
2019-07-07 11:18:12 +05:30
{{ __('Write') }}
2018-12-13 13:39:08 +05:30
</button>
2017-09-10 17:25:29 +05:30
</li>
2019-02-15 15:39:39 +05:30
<li :class="{ active: previewMarkdown }" class="md-header-tab">
2018-12-13 13:39:08 +05:30
<button
2018-11-08 19:23:39 +05:30
class="js-preview-link js-md-preview-button"
2017-09-10 17:25:29 +05:30
tabindex="-1"
2018-12-13 13:39:08 +05:30
type="button"
2019-03-02 22:35:43 +05:30
@click="previewMarkdownTab($event)"
2018-03-17 18:26:18 +05:30
>
2019-07-07 11:18:12 +05:30
{{ __('Preview') }}
2018-12-13 13:39:08 +05:30
</button>
2017-09-10 17:25:29 +05:30
</li>
2019-02-15 15:39:39 +05:30
<li :class="{ active: !previewMarkdown }" class="md-header-toolbar">
2019-09-04 21:01:54 +05:30
<div class="d-inline-block">
<toolbar-button tag="**" :button-title="__('Add bold text')" icon="bold" />
<toolbar-button tag="*" :button-title="__('Add italic text')" icon="italic" />
<toolbar-button
:prepend="true"
tag="> "
:button-title="__('Insert a quote')"
icon="quote"
/>
</div>
<div class="d-inline-block ml-md-2 ml-0">
<template v-if="canSuggest">
<toolbar-button
ref="suggestButton"
:tag="mdSuggestion"
:prepend="true"
:button-title="__('Insert suggestion')"
:cursor-offset="4"
:tag-content="lineContent"
icon="doc-code"
2019-10-12 21:52:04 +05:30
class="qa-suggestion-btn js-suggestion-btn"
2019-09-04 21:01:54 +05:30
@click="handleSuggestDismissed"
/>
<gl-popover
v-if="showSuggestPopover"
:target="() => $refs.suggestButton"
:css-classes="['diff-suggest-popover']"
placement="bottom"
:show="showSuggestPopover"
>
<strong>{{ __('New! Suggest changes directly') }}</strong>
<p class="mb-2">
{{ __('Suggest code changes which are immediately applied. Try it out!') }}
</p>
<gl-button variant="primary" size="sm" @click="handleSuggestDismissed">
{{ __('Got it') }}
</gl-button>
</gl-popover>
</template>
<toolbar-button tag="`" tag-block="```" :button-title="__('Insert code')" icon="code" />
<toolbar-button
tag="[{text}](url)"
tag-select="url"
:button-title="__('Add a link')"
icon="link"
/>
</div>
<div class="d-inline-block ml-md-2 ml-0">
<toolbar-button
:prepend="true"
tag="* "
:button-title="__('Add a bullet list')"
icon="list-bulleted"
/>
<toolbar-button
:prepend="true"
tag="1. "
:button-title="__('Add a numbered list')"
icon="list-numbered"
/>
<toolbar-button
:prepend="true"
tag="* [ ] "
:button-title="__('Add a task list')"
icon="task-done"
/>
<toolbar-button
:tag="mdTable"
:prepend="true"
:button-title="__('Add a table')"
icon="table"
/>
</div>
<div class="d-inline-block ml-md-2 ml-0">
<button
v-gl-tooltip
:aria-label="__('Go full screen')"
class="toolbar-btn toolbar-fullscreen-btn js-zen-enter"
data-container="body"
tabindex="-1"
:title="__('Go full screen')"
type="button"
>
<icon name="screen-full" />
</button>
</div>
2017-09-10 17:25:29 +05:30
</li>
</ul>
</div>
</template>