summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/vue_shared/components/markdown/field.vue
blob: e6977681e96e052179f6f34ef35f9bfdd6ab3b99 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
<script>
  /* global Flash */
  import markdownHeader from './header.vue';
  import markdownToolbar from './toolbar.vue';

  export default {
    props: {
      markdownPreviewUrl: {
        type: String,
        required: false,
        default: '',
      },
      markdownDocs: {
        type: String,
        required: true,
      },
    },
    data() {
      return {
        markdownPreview: '',
        markdownPreviewLoading: false,
        previewMarkdown: false,
      };
    },
    components: {
      markdownHeader,
      markdownToolbar,
    },
    methods: {
      toggleMarkdownPreview() {
        this.previewMarkdown = !this.previewMarkdown;

        if (!this.previewMarkdown) {
          this.markdownPreview = '';
        } else {
          this.markdownPreviewLoading = true;
          this.$http.post(
            this.markdownPreviewUrl,
            {
              /*
                Can't use `$refs` as the component is technically in the parent component
                so we access the VNode & then get the element
              */
              text: this.$slots.textarea[0].elm.value,
            },
          )
          .then((res) => {
            const data = res.json();

            this.markdownPreviewLoading = false;
            this.markdownPreview = data.body;

            this.$nextTick(() => {
              $(this.$refs['markdown-preview']).renderGFM();
            });
          })
          .catch(() => new Flash('Error loading markdown preview'));
        }
      },
    },
    mounted() {
      /*
        GLForm class handles all the toolbar buttons
      */
      return new gl.GLForm($(this.$refs['gl-form']), true);
    },
  };
</script>

<template>
  <div
    class="md-area prepend-top-default append-bottom-default js-vue-markdown-field"
    ref="gl-form">
    <markdown-header
      :preview-markdown="previewMarkdown"
      @toggle-markdown="toggleMarkdownPreview" />
    <div
      class="md-write-holder"
      v-show="!previewMarkdown">
      <div class="zen-backdrop">
        <slot name="textarea"></slot>
        <a
          class="zen-control zen-control-leave js-zen-leave"
          href="#"
          aria-label="Enter zen mode">
          <i
            class="fa fa-compress"
            aria-hidden="true">
          </i>
        </a>
        <markdown-toolbar
          :markdown-docs="markdownDocs" />
      </div>
    </div>
    <div
      class="md md-preview-holder md-preview"
      v-show="previewMarkdown">
      <div
        ref="markdown-preview"
        v-html="markdownPreview">
      </div>
      <span v-if="markdownPreviewLoading">
        Loading...
      </span>
    </div>
  </div>
</template>