Welcome to mirror list, hosted at ThFree Co, Russian Federation.

edit_area.vue « components « static_site_editor « javascripts « assets « app - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: e602f26acdf670238b02a890ed8a7ffdbd21661f (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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
<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 EditDrawer from './edit_drawer.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';
import { DEFAULT_IMAGE_UPLOAD_PATH } from '../constants';
import imageRepository from '../image_repository';
import formatter from '../services/formatter';
import templater from '../services/templater';

export default {
  components: {
    RichContentEditor,
    PublishToolbar,
    EditHeader,
    EditDrawer,
    UnsavedChangesConfirmDialog,
  },
  props: {
    title: {
      type: String,
      required: true,
    },
    content: {
      type: String,
      required: true,
    },
    savingChanges: {
      type: Boolean,
      required: true,
    },
    returnUrl: {
      type: String,
      required: false,
      default: '',
    },
    imageRoot: {
      type: String,
      required: false,
      default: DEFAULT_IMAGE_UPLOAD_PATH,
      validator: prop => prop.endsWith('/'),
    },
  },
  data() {
    return {
      saveable: false,
      parsedSource: parseSourceFile(this.preProcess(true, this.content)),
      editorMode: EDITOR_TYPES.wysiwyg,
      isModified: false,
      hasMatter: false,
      isDrawerOpen: false,
    };
  },
  imageRepository: imageRepository(),
  computed: {
    editableContent() {
      return this.parsedSource.content(this.isWysiwygMode);
    },
    editableMatter() {
      return this.isDrawerOpen ? this.parsedSource.matter() : {};
    },
    hasSettings() {
      return this.hasMatter && this.isWysiwygMode;
    },
    isWysiwygMode() {
      return this.editorMode === EDITOR_TYPES.wysiwyg;
    },
  },
  created() {
    this.refreshEditHelpers();
  },
  methods: {
    preProcess(isWrap, value) {
      const formattedContent = formatter(value);
      const templatedContent = isWrap
        ? templater.wrap(formattedContent)
        : templater.unwrap(formattedContent);
      return templatedContent;
    },
    refreshEditHelpers() {
      this.isModified = this.parsedSource.isModified();
      this.hasMatter = this.parsedSource.hasMatter();
    },
    onDrawerOpen() {
      this.isDrawerOpen = true;
      this.refreshEditHelpers();
    },
    onDrawerClose() {
      this.isDrawerOpen = false;
      this.refreshEditHelpers();
    },
    onInputChange(newVal) {
      this.parsedSource.syncContent(newVal, this.isWysiwygMode);
      this.refreshEditHelpers();
    },
    onModeChange(mode) {
      this.editorMode = mode;

      const preProcessedContent = this.preProcess(this.isWysiwygMode, this.editableContent);
      this.$refs.editor.resetInitialValue(preProcessedContent);
    },
    onUpdateSettings(settings) {
      this.parsedSource.syncMatter(settings);
    },
    onUploadImage({ file, imageUrl }) {
      this.$options.imageRepository.add(file, imageUrl);
    },
    onSubmit() {
      const preProcessedContent = this.preProcess(false, this.parsedSource.content());
      this.$emit('submit', {
        content: preProcessedContent,
        images: this.$options.imageRepository.getAll(),
      });
    },
  },
};
</script>
<template>
  <div class="d-flex flex-grow-1 flex-column h-100">
    <edit-header class="py-2" :title="title" />
    <edit-drawer
      v-if="hasMatter"
      :is-open="isDrawerOpen"
      :settings="editableMatter"
      @close="onDrawerClose"
      @updateSettings="onUpdateSettings"
    />
    <rich-content-editor
      ref="editor"
      :content="editableContent"
      :initial-edit-type="editorMode"
      :image-root="imageRoot"
      class="mb-9 pb-6 h-100"
      @modeChange="onModeChange"
      @input="onInputChange"
      @uploadImage="onUploadImage"
    />
    <unsaved-changes-confirm-dialog :modified="isModified" />
    <publish-toolbar
      class="gl-fixed gl-left-0 gl-bottom-0 gl-w-full"
      :has-settings="hasSettings"
      :return-url="returnUrl"
      :saveable="isModified"
      :saving-changes="savingChanges"
      @editSettings="onDrawerOpen"
      @submit="onSubmit"
    />
  </div>
</template>