summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/static_site_editor/components/edit_area.vue
blob: e9efef406323582a15286e731fe085cc765a96b0 (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
<script>
import RichContentEditor from '~/vue_shared/components/rich_content_editor/rich_content_editor.vue';
import PublishToolbar from './publish_toolbar.vue';
import EditHeader from './edit_header.vue';
import UnsavedChangesConfirmDialog from './unsaved_changes_confirm_dialog.vue';
import parseSourceFile from '~/static_site_editor/services/parse_source_file';
import { EDITOR_TYPES } from '~/vue_shared/components/rich_content_editor/constants';

export default {
  components: {
    RichContentEditor,
    PublishToolbar,
    EditHeader,
    UnsavedChangesConfirmDialog,
  },
  props: {
    title: {
      type: String,
      required: true,
    },
    content: {
      type: String,
      required: true,
    },
    savingChanges: {
      type: Boolean,
      required: true,
    },
    returnUrl: {
      type: String,
      required: false,
      default: '',
    },
  },
  data() {
    return {
      saveable: false,
      parsedSource: parseSourceFile(this.content),
      editorMode: EDITOR_TYPES.wysiwyg,
    };
  },
  computed: {
    editableContent() {
      return this.parsedSource.editable;
    },
    editableKey() {
      return this.isWysiwygMode ? 'body' : 'raw';
    },
    isWysiwygMode() {
      return this.editorMode === EDITOR_TYPES.wysiwyg;
    },
    modified() {
      return this.isWysiwygMode
        ? this.parsedSource.isModifiedBody()
        : this.parsedSource.isModifiedRaw();
    },
  },
  methods: {
    syncSource() {
      if (this.isWysiwygMode) {
        this.parsedSource.syncBody();
        return;
      }

      this.parsedSource.syncRaw();
    },
    onModeChange(mode) {
      this.editorMode = mode;
      this.syncSource();
    },
    onSubmit() {
      this.syncSource();
      this.$emit('submit', { content: this.editableContent.raw });
    },
  },
};
</script>
<template>
  <div class="d-flex flex-grow-1 flex-column h-100">
    <edit-header class="py-2" :title="title" />
    <rich-content-editor
      v-model="editableContent[editableKey]"
      :initial-edit-type="editorMode"
      class="mb-9 h-100"
      @modeChange="onModeChange"
    />
    <unsaved-changes-confirm-dialog :modified="modified" />
    <publish-toolbar
      class="gl-fixed gl-left-0 gl-bottom-0 gl-w-full"
      :return-url="returnUrl"
      :saveable="modified"
      :saving-changes="savingChanges"
      @submit="onSubmit"
    />
  </div>
</template>