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

215 lines
5.5 KiB
Vue
Raw Normal View History

2017-09-10 17:25:29 +05:30
<script>
2018-05-09 12:01:36 +05:30
import $ from 'jquery';
2018-11-08 19:23:39 +05:30
import { s__ } from '~/locale';
2018-03-17 18:26:18 +05:30
import Flash from '../../../flash';
import GLForm from '../../../gl_form';
2017-09-10 17:25:29 +05:30
import markdownHeader from './header.vue';
import markdownToolbar from './toolbar.vue';
2018-03-17 18:26:18 +05:30
import icon from '../icon.vue';
2017-09-10 17:25:29 +05:30
export default {
2018-03-17 18:26:18 +05:30
components: {
markdownHeader,
markdownToolbar,
icon,
},
2017-09-10 17:25:29 +05:30
props: {
2018-03-17 18:26:18 +05:30
markdownPreviewPath: {
2017-09-10 17:25:29 +05:30
type: String,
required: false,
default: '',
},
2018-03-17 18:26:18 +05:30
markdownDocsPath: {
2017-09-10 17:25:29 +05:30
type: String,
required: true,
},
2018-11-08 19:23:39 +05:30
markdownVersion: {
type: Number,
required: false,
default: 0,
},
2018-03-17 18:26:18 +05:30
addSpacingClasses: {
type: Boolean,
required: false,
default: true,
},
quickActionsDocsPath: {
type: String,
required: false,
default: '',
},
canAttachFile: {
type: Boolean,
required: false,
default: true,
},
enableAutocomplete: {
type: Boolean,
required: false,
default: true,
},
2017-09-10 17:25:29 +05:30
},
data() {
return {
markdownPreview: '',
2018-03-17 18:26:18 +05:30
referencedCommands: '',
referencedUsers: '',
2017-09-10 17:25:29 +05:30
markdownPreviewLoading: false,
previewMarkdown: false,
};
},
2018-03-17 18:26:18 +05:30
computed: {
shouldShowReferencedUsers() {
const referencedUsersThreshold = 10;
return this.referencedUsers.length >= referencedUsersThreshold;
2017-09-10 17:25:29 +05:30
},
},
mounted() {
/*
GLForm class handles all the toolbar buttons
*/
2018-11-08 19:23:39 +05:30
return new GLForm($(this.$refs['gl-form']), {
emojis: this.enableAutocomplete,
members: this.enableAutocomplete,
issues: this.enableAutocomplete,
mergeRequests: this.enableAutocomplete,
epics: this.enableAutocomplete,
milestones: this.enableAutocomplete,
labels: this.enableAutocomplete,
2018-12-05 23:21:45 +05:30
snippets: this.enableAutocomplete,
2018-11-08 19:23:39 +05:30
});
2017-09-10 17:25:29 +05:30
},
beforeDestroy() {
2018-03-27 19:54:05 +05:30
const glForm = $(this.$refs['gl-form']).data('glForm');
2017-09-10 17:25:29 +05:30
if (glForm) {
glForm.destroy();
}
},
2018-03-17 18:26:18 +05:30
methods: {
showPreviewTab() {
if (this.previewMarkdown) return;
this.previewMarkdown = true;
/*
Can't use `$refs` as the component is technically in the parent component
so we access the VNode & then get the element
*/
const text = this.$slots.textarea[0].elm.value;
if (text) {
this.markdownPreviewLoading = true;
2018-11-08 19:23:39 +05:30
this.$http
.post(this.versionedPreviewPath(), { text })
.then(resp => resp.json())
.then(data => this.renderMarkdown(data))
.catch(() => new Flash(s__('Error loading markdown preview')));
2018-03-17 18:26:18 +05:30
} else {
this.renderMarkdown();
}
},
showWriteTab() {
this.markdownPreview = '';
this.previewMarkdown = false;
},
renderMarkdown(data = {}) {
this.markdownPreviewLoading = false;
this.markdownPreview = data.body || 'Nothing to preview.';
if (data.references) {
this.referencedCommands = data.references.commands;
this.referencedUsers = data.references.users;
}
this.$nextTick(() => {
$(this.$refs['markdown-preview']).renderGFM();
});
},
2018-11-08 19:23:39 +05:30
versionedPreviewPath() {
const { markdownPreviewPath, markdownVersion } = this;
return `${markdownPreviewPath}${
markdownPreviewPath.indexOf('?') === -1 ? '?' : '&'
}markdown_version=${markdownVersion}`;
},
2018-03-17 18:26:18 +05:30
},
2017-09-10 17:25:29 +05:30
};
</script>
<template>
<div
2018-11-08 19:23:39 +05:30
ref="gl-form"
2018-03-17 18:26:18 +05:30
:class="{ 'prepend-top-default append-bottom-default': addSpacingClasses }"
2018-11-08 19:23:39 +05:30
class="md-area js-vue-markdown-field">
2017-09-10 17:25:29 +05:30
<markdown-header
:preview-markdown="previewMarkdown"
2018-03-17 18:26:18 +05:30
@preview-markdown="showPreviewTab"
@write-markdown="showWriteTab"
/>
2017-09-10 17:25:29 +05:30
<div
2018-03-17 18:26:18 +05:30
v-show="!previewMarkdown"
2018-11-08 19:23:39 +05:30
class="md-write-holder"
2018-03-17 18:26:18 +05:30
>
2017-09-10 17:25:29 +05:30
<div class="zen-backdrop">
<slot name="textarea"></slot>
<a
class="zen-control zen-control-leave js-zen-leave"
href="#"
2018-03-17 18:26:18 +05:30
aria-label="Enter zen mode"
>
<icon
:size="32"
2018-11-08 19:23:39 +05:30
name="screen-normal"
2018-03-17 18:26:18 +05:30
/>
2017-09-10 17:25:29 +05:30
</a>
<markdown-toolbar
2018-03-17 18:26:18 +05:30
:markdown-docs-path="markdownDocsPath"
:quick-actions-docs-path="quickActionsDocsPath"
:can-attach-file="canAttachFile"
/>
2017-09-10 17:25:29 +05:30
</div>
</div>
<div
2018-03-17 18:26:18 +05:30
v-show="previewMarkdown"
2018-11-08 19:23:39 +05:30
class="md md-preview-holder md-preview js-vue-md-preview"
2018-03-17 18:26:18 +05:30
>
2017-09-10 17:25:29 +05:30
<div
ref="markdown-preview"
2018-03-17 18:26:18 +05:30
v-html="markdownPreview"
>
2017-09-10 17:25:29 +05:30
</div>
<span v-if="markdownPreviewLoading">
Loading...
</span>
</div>
2018-03-17 18:26:18 +05:30
<template v-if="previewMarkdown && !markdownPreviewLoading">
<div
v-if="referencedCommands"
class="referenced-commands"
2018-11-08 19:23:39 +05:30
v-html="referencedCommands"
2018-03-17 18:26:18 +05:30
>
</div>
<div
v-if="shouldShowReferencedUsers"
class="referenced-users"
>
<span>
<i
class="fa fa-exclamation-triangle"
aria-hidden="true"
>
</i>
You are about to add
<strong>
<span class="js-referenced-users-count">
{{ referencedUsers.length }}
</span>
</strong> people to the discussion. Proceed with caution.
</span>
</div>
</template>
2017-09-10 17:25:29 +05:30
</div>
</template>