123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166 |
- <template lang="html">
- <div class="editor">
- <div ref="toolbar" class="toolbar">
- </div>
- <div ref="editor" class="text">
- </div>
- </div>
- </template>
- <script>
- import E from 'wangeditor'
- import { createNamespacedHelpers } from 'vuex'
- const { mapActions } = createNamespacedHelpers('files')
- const token = sessionStorage.getItem('token')
- export default {
- name: 'editoritem',
- data () {
- return {
-
- editor: null,
- info_: null
- }
- },
- model: {
- prop: 'value',
- event: 'change'
- },
- props: {
- value: {
- type: String,
- default: ''
- },
- isClear: {
- type: Boolean,
- default: false
- }
- },
- watch: {
- isClear (val) {
-
- if (val) {
- this.editor.txt.clear()
- this.info_ = null
- }
- },
- value: function (value) {
- if (value !== this.editor.txt.html()) {
- this.editor.txt.html(this.value)
- }
- }
-
- },
- mounted () {
- this.seteditor()
- this.editor.txt.html(this.value)
- },
- methods: {
- ...mapActions(['filesupload']),
- seteditor () {
- const _this = this
-
- this.editor = new E(this.$refs.toolbar, this.$refs.editor)
- this.editor.config.uploadImgParams = { type: 'resource' }
- this.editor.config.uploadImgShowBase64 = false
- this.editor.config.uploadImgServer = '/api/files/upload'
- this.editor.config.uploadImgHeaders = { Authorization: `Bearer ${token}`, a: 100 }
- this.editor.config.uploadFileName = 'file'
- this.editor.config.uploadImgMaxSize = 2 * 1024 * 1024
- this.editor.config.uploadImgMaxLength = 6
- this.editor.config.uploadImgTimeout = 3 * 60 * 1000
- this.editor.config.showLinkImg = false
-
- this.editor.config.menus = [
- 'head',
- 'bold',
- 'fontSize',
- 'fontName',
- 'italic',
- 'underline',
- 'strikeThrough',
- 'foreColor',
- 'backColor',
- 'link',
- 'list',
- 'justify',
- 'quote',
- 'emoticon',
- 'image',
- 'table',
- 'video',
- 'code',
- 'undo',
- 'redo',
- 'fullscreen'
- ]
- this.editor.config.customUploadImg = async function (resultFiles, insertImgFn) {
-
- var data = new FormData()
- data.append('type', 'resource')
- data.append('file', resultFiles[0])
- const res = await _this.filesupload(data)
-
- const url = res.data.path
-
- insertImgFn(url)
- }
- this.editor.config.uploadImgHooks = {
- fail: (xhr, editor, result) => {
-
- console.log('插入图片失败回调')
- console.log(xhr, editor, result)
- },
- success: (xhr, editor, result) => {
-
- console.log('图片上传成功回调')
- console.log(xhr, editor, result)
- },
- timeout: (xhr, editor) => {
-
- },
- error: (xhr, editor) => {
-
- _this.$message.error('上传失败')
- },
- customInsert: (insertImg, result, editor) => {
- console.log('图片上传成功,插入图片的回调')
- console.log(result, editor)
-
-
-
-
-
-
-
- const url = result.data.path
- insertImg(url)
-
- }
- }
- this.editor.config.onchange = (html) => {
- this.info_ = html
- this.$emit('change', this.info_)
- }
-
- this.editor.create()
- }
- }
- }
- </script>
- <style lang="css">
- .editor {
- width: 100%;
- margin: 0 auto;
- position: relative;
- z-index: 0;
- }
- .toolbar {
- border: 1px solid #ccc;
- }
- .text {
- border: 1px solid #ccc;
- min-height: 300px;
- }
- </style>
|