Blame view

ant-design-vue-jeecg/src/components/jeecg/JMarkdownEditor/index.vue 6.38 KB
肖超群 authored
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
<template>
  <div>
    <div class="j-markdown-editor" :id="id"/>
    <div v-if="isShow">
      <j-modal
        title="图片上传"
        :visible.sync="dialogVisible"
        width="30%"
        :before-close="handleClose"
        @ok="handleOk">
        <a-tabs default-active-key="1" @change="handleChange">
          <a-tab-pane tab="本地图片上传" key="1" :forceRender="true">
            <j-upload v-model="fileList" :number="1"></j-upload>
            <div style="margin-top: 20px">
              <a-input v-model="remark" placeholder="请填写备注"></a-input>
            </div>
          </a-tab-pane>
          <a-tab-pane tab="网络图片地址" key="2" :forceRender="true">
            <a-input v-model="networkPic" placeholder="请填写网络图片地址"></a-input>
            <a-input style="margin-top: 20px" v-model="remark" placeholder="请填写备注"></a-input>
          </a-tab-pane>
        </a-tabs>
      </j-modal>
    </div>
  </div>
</template>

<script>
import 'codemirror/lib/codemirror.css'
import '@toast-ui/editor/dist/toastui-editor.css';
import '@toast-ui/editor/dist/i18n/zh-cn';

import Editor from '@toast-ui/editor';
import defaultOptions from './default-options'
import JUpload from '@/components/jeecg/JUpload'
肖超群 authored
36
import {getFileAccessHttpUrl} from '@/api/manage'
肖超群 authored
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

export default {
  name: 'JMarkdownEditor',
  components: {
    JUpload,
  },
  props: {
    value: {
      type: String,
      default: ''
    },
    id: {
      type: String,
      required: false,
      default() {
        return 'markdown-editor-' + +new Date() + ((Math.random() * 1000).toFixed(0) + '')
      }
    },
    options: {
      type: Object,
      default() {
        return defaultOptions
      }
    },
    mode: {
      type: String,
      default: 'markdown'
    },
    height: {
      type: String,
      required: false,
      default: '300px'
    },
    language: {
      type: String,
      required: false,
      default: 'zh-CN'
    }
  },
  data() {
    return {
      editor: null,
肖超群 authored
79
80
81
82
83
84
85
86
87
      isShow: false,
      activeIndex: "1",
      dialogVisible: false,
      index: "1",
      fileList: [],
      remark: "",
      imageName: "",
      imageUrl: "",
      networkPic: ""
肖超群 authored
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
153
154
155
156
    }
  },
  computed: {
    editorOptions() {
      const options = Object.assign({}, defaultOptions, this.options)
      options.initialEditType = this.mode
      options.height = this.height
      options.language = this.language
      return options
    }
  },
  watch: {
    value(newValue, preValue) {
      if (newValue !== preValue && newValue !== this.editor.getMarkdown()) {
        this.editor.setMarkdown(newValue)
      }
    },
    language(val) {
      this.destroyEditor()
      this.initEditor()
    },
    height(newValue) {
      this.editor.height(newValue)
    },
    mode(newValue) {
      this.editor.changeMode(newValue)
    }
  },
  mounted() {
    this.initEditor()
  },
  destroyed() {
    this.destroyEditor()
  },
  methods: {
    initEditor() {
      this.editor = new Editor({
        el: document.getElementById(this.id),
        ...this.editorOptions
      })
      if (this.value) {
        this.editor.setMarkdown(this.value)
      }
      this.editor.on('change', () => {
        this.$emit('change', this.editor.getMarkdown())
      })
      //--begin 添加自定义上传按钮
      /*
       * 添加自定义按钮
       */
      //获取编辑器上的功能条
      let toolbar = this.editor.getUI().getToolbar();
      let fileDom = this.$refs.files;
      //添加图片点击事件
      this.editor.eventManager.addEventType('isShowClickEvent');
      this.editor.eventManager.listen('isShowClickEvent', () => {
        this.isShow = true
        this.dialogVisible = true
      });
      //addImageBlobHook图片上传、剪切、拖拽都会走此方法
      // 删除默认监听事件
      this.editor.eventManager.removeEventHandler('addImageBlobHook')
      // 添加自定义监听事件
      this.editor.eventManager.listen('addImageBlobHook', (blob, callback) => {
        this.upload(blob, url => {
          callback(url)
        })
      })
      // 添加自定义按钮 第二个参数代表位置,不传默认放在最后
肖超群 authored
157
      toolbar.insertItem(15, {
肖超群 authored
158
        type: 'button',
肖超群 authored
159
        options: {
肖超群 authored
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
          name: 'customize',
          className: 'tui-image tui-toolbar-icons',
          event: 'isShowClickEvent',
          tooltip: '上传图片',
        }
        //
      });
      //--end 添加自定义上传按钮
    },
    destroyEditor() {
      if (!this.editor) return
      this.editor.off('change')
      this.editor.remove()
    },
    setMarkdown(value) {
      this.editor.setMarkdown(value)
    },
    getMarkdown() {
      return this.editor.getMarkdown()
    },
    setHtml(value) {
      this.editor.setHtml(value)
    },
    getHtml() {
      return this.editor.getHtml()
    },
肖超群 authored
186
187
    handleOk() {
      if (this.index == '1') {
肖超群 authored
188
        this.imageUrl = getFileAccessHttpUrl(this.fileList)
肖超群 authored
189
190
191
192
        if (this.remark) {
          this.addImgToMd(this.imageUrl, this.remark)
        } else {
          this.addImgToMd(this.imageUrl, "")
肖超群 authored
193
        }
肖超群 authored
194
195
196
197
198
      } else {
        if (this.remark) {
          this.addImgToMd(this.networkPic, this.remark)
        } else {
          this.addImgToMd(this.networkPic, "")
肖超群 authored
199
200
        }
      }
肖超群 authored
201
202
203
204
205
206
207
208
      this.index = "1"
      this.fileList = []
      this.imageName = "";
      this.imageUrl = "";
      this.remark = ""
      this.networkPic = ""
      this.dialogVisible = false
      this.isShow = false;
肖超群 authored
209
210
211
212
    },
    handleClose(done) {
      done();
    },
肖超群 authored
213
214
215
216
217
218
219
    handleChange(val) {
      this.fileList = []
      this.remark = ""
      this.imageName = ""
      this.imageUrl = ""
      this.networkPic = ""
      this.index = val
肖超群 authored
220
221
    },
    //添加图片到markdown
肖超群 authored
222
    addImgToMd(data, name) {
肖超群 authored
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
      let editor = this.editor.getCodeMirror();
      let editorHtml = this.editor.getCurrentModeEditor();
      let isMarkdownMode = this.editor.isMarkdownMode();
      if (isMarkdownMode) {
        editor.replaceSelection(`![${name}](${data})`);
      } else {
        let range = editorHtml.getRange();
        let img = document.createElement('img');
        img.src = `${data}`;
        img.alt = name;
        range.insertNode(img);
      }
    },
  },
  model: {
    prop: 'value',
    event: 'change'
  }
}
</script>
<style scoped lang="less">
肖超群 authored
245
246
247
248
249
.j-markdown-editor {
  /deep/ .tui-editor-defaultUI {
    .te-mode-switch,
    .tui-scrollsync {
      line-height: 1.5;
肖超群 authored
250
251
    }
  }
肖超群 authored
252
}
肖超群 authored
253
254

</style>