update src/components/Editor/index.vue.

更新编辑器wangeditor 4.7.15 至 5.1.1
This commit is contained in:
‭‭胖就胖呗 2022-06-21 02:14:59 +00:00 committed by 不做码农
parent 2a3916571d
commit 0e89db757c

View File

@ -1,74 +1,135 @@
<template>
<div class="editor-container">
<div :id="id"></div>
</div>
<div style="border: 1px solid #ccc">
<Toolbar style="border-bottom: 1px solid #ccc" :editor="editorRef" :defaultConfig="toolbarConfig" :mode="mode" />
<Editor
style="height: 500px; overflow-y: hidden"
v-model="valueHtml"
:defaultConfig="editorConfig"
:mode="mode"
@onCreated="handleCreated"
@onChange="handleChange" />
</div>
</template>
<script>
import { toRefs, reactive, onMounted, watch, defineComponent } from 'vue';
import wangeditor from 'wangeditor';
import '@wangeditor/editor/dist/css/style.css' // css
import { onBeforeUnmount, ref, shallowRef } from 'vue'
import { Editor, Toolbar } from '@wangeditor/editor-for-vue'
import { getToken } from '@/utils/auth'
import useUserStore from '@/store/modules/user'
export default {
components: { Editor, Toolbar },
props: {
placeholder: {
type: String,
default: () => '请输入内容',
},
modelValue: String,
},
setup(props, { emit }) {
const editorRef = shallowRef()
const valueHtml = ref(props.modelValue)
const toolbarConfig = {}
const editorConfig = {
MENU_CONF: {},
placeholder: props.placeholder,
}
//
editorConfig.MENU_CONF['uploadImage'] = {
server: import.meta.env.VITE_APP_BASE_API + '/common/UploadFile',
// form-data fieldName 'wangeditor-uploaded-image'
fieldName: 'file',
// 2M
maxFileSize: 1 * 1024 * 1024, // 1M
// 100
maxNumberOfFiles: 10,
// ['image/*'] []
allowedFileTypes: ['image/*'],
// meta url false
metaWithUrl: false,
// http header
headers: {
Authorization: 'Bearer ' + getToken(),
userid: useUserStore().userId,
},
// cookie false
withCredentials: true,
// 10
timeout: 5 * 1000, // 5
//
customInsert(res, insertFn) {
;-(
// res url alt href
insertFn(res.data.url)
)
},
}
//
editorConfig.MENU_CONF['uploadVideo'] = {
server: import.meta.env.VITE_APP_BASE_API + '/common/UploadFile',
// form-data fieldName 'wangeditor-uploaded-video'
fieldName: 'file',
export default defineComponent({
name: 'wngEditor',
props: {
// id
id: {
type: String,
default: () => 'wangeditor',
},
//
isDisable: {
type: Boolean,
default: () => false,
},
// placeholder
placeholder: {
type: String,
default: () => '请输入内容',
},
//
//
// https://v3.cn.vuejs.org/guide/migration/v-model.html#%E8%BF%81%E7%A7%BB%E7%AD%96%E7%95%A5
modelValue: String,
},
setup(props, { emit }) {
const state = reactive({
editor: null,
});
//
// https://doc.wangeditor.com/
const initWangeditor = () => {
state.editor = new wangeditor(`#${props.id}`);
state.editor.config.zIndex = 1;
state.editor.config.placeholder = props.placeholder;
state.editor.config.uploadImgShowBase64 = true;
state.editor.config.showLinkImg = false;
onWangeditorChange();
state.editor.create();
state.editor.txt.html(props.modelValue);
props.isDisable ? state.editor.disable() : state.editor.enable();
};
//
const onWangeditorChange = () => {
state.editor.config.onchange = (html) => {
emit('update:modelValue', html);
};
};
//
onMounted(() => {
initWangeditor();
});
//
// https://gitee.com/lyt-top/vue-next-admin/issues/I4LM7I
watch(
() => props.modelValue,
(value) => {
state.editor.txt.html(value);
}
);
return {
...toRefs(state),
};
},
});
</script>
// 10M
maxFileSize: 5 * 1024 * 1024, // 5M
// 5
maxNumberOfFiles: 3,
// ['video/*'] []
allowedFileTypes: ['video/*'],
// meta url false
metaWithUrl: false,
// http header
headers: {
Authorization: 'Bearer ' + getToken(),
userid: useUserStore().userId,
},
// cookie false
withCredentials: true,
// 30
timeout: 15 * 1000, // 15
//
customInsert(res, insertFn) {
;-(
// res url alt href
insertFn(res.data.url)
)
},
}
onBeforeUnmount(() => {
const editor = editorRef.value
if (editor == null) return
editor.destroy()
})
const handleCreated = (editor) => {
editorRef.value = editor
}
const handleChange = (editor) => {
emit('update:modelValue', editor.getHtml())
}
watch(
() => props.modelValue,
(value) => {
const editor = editorRef.value
if (value == undefined) {
editor.clear()
return
}
valueHtml.value = value;
},
)
return {
editorRef,
valueHtml,
mode: 'default',
toolbarConfig,
editorConfig,
handleCreated,
handleChange,
}
},
}
</script>