kun-galgame-vue/src/components/WangEditor.vue

108 lines
2.7 KiB
Vue
Raw Normal View History

2023-05-10 16:46:20 +00:00
<!--
编辑器实例共用组件
-->
<script setup lang="ts">
2023-06-12 05:32:55 +00:00
import '@wangeditor/editor/dist/css/style.css'
import { IDomEditor } from '@wangeditor/editor'
2023-05-10 16:46:20 +00:00
import { onBeforeUnmount, ref, shallowRef, onMounted } from 'vue'
import { Editor, Toolbar } from '@wangeditor/editor-for-vue'
2023-06-12 05:32:55 +00:00
const editorRef = shallowRef<IDomEditor | undefined>(undefined)
2023-06-07 14:16:33 +00:00
2023-06-12 05:32:55 +00:00
const valueHtml = ref('<p>hello</p>')
2023-05-10 16:46:20 +00:00
2023-06-12 05:32:55 +00:00
// 编辑器相关配置
2023-05-10 16:46:20 +00:00
const editorConfig = {
placeholder: '请输入内容...',
2023-06-12 05:32:55 +00:00
readOnly: false,
2023-06-07 14:16:33 +00:00
MENU_CONF: {
uploadImage: {
2023-06-12 05:32:55 +00:00
server: 'http://127.0.0.1:10007/upload/img',
// server: '/api/upload-img-10s', // test timeout
// server: '/api/upload-img-failed', // test failed
// server: '/api/xxx', // test 404
timeout: 5 * 1000, // 5s
fieldName: 'custom-fileName',
meta: { token: 'xxx', a: 100 },
metaWithUrl: true, // join params to url
headers: { Accept: 'text/x-json' },
maxFileSize: 10 * 1024 * 1024, // 10M
base64LimitSize: 5 * 1024, // insert base64 format, if file's size less than 5kb
// onBeforeUpload(file) {
// console.log('onBeforeUpload', file)
// return file // will upload this file
// // return false // prevent upload
// },
// onProgress(progress) {
// console.log('onProgress', progress)
// },
// onSuccess(file, res) {
// console.log('onSuccess', file, res)
// },
// onFailed(file, res) {
// alert(res.message)
// console.log('onFailed', file, res)
// },
// onError(file, err, res) {
// alert(err.message)
// console.error('onError', file, err, res)
// },
2023-06-07 14:16:33 +00:00
},
},
2023-05-10 16:46:20 +00:00
}
2023-06-12 05:32:55 +00:00
const handleCreated = (editor: IDomEditor) => {
editorRef.value = editor
console.log(editor.getConfig())
2023-05-10 16:46:20 +00:00
}
2023-06-12 05:32:55 +00:00
onMounted(() => {
// 模拟 ajax 异步设置 value
setTimeout(() => {
valueHtml.value = '<p>hello world</p>' // 测试 v-model
}, 2000)
})
// 组件销毁时,也及时销毁编辑器
2023-05-10 16:46:20 +00:00
onBeforeUnmount(() => {
const editor = editorRef.value
if (editor == null) return
editor.destroy()
})
</script>
<template>
<!-- 编辑器 -->
<div class="editor—wrapper">
2023-06-07 14:16:33 +00:00
<Toolbar class="toolbar-container" :editor="editorRef" />
2023-05-10 16:46:20 +00:00
<Editor
2023-06-07 15:03:41 +00:00
style="height: 400px"
2023-05-10 16:46:20 +00:00
v-model="valueHtml"
:defaultConfig="editorConfig"
2023-05-25 13:43:42 +00:00
@onCreated="handleCreated"
2023-05-10 16:46:20 +00:00
/>
</div>
</template>
2023-05-25 07:55:30 +00:00
<style lang="scss" scoped>
2023-05-10 16:46:20 +00:00
/* 编辑器的样式 */
.editorwrapper {
/* 编辑器的 border */
2023-06-05 06:32:08 +00:00
border: 1px solid var(--kungalgame-blue-4);
2023-05-10 16:46:20 +00:00
box-sizing: border-box;
/* 编辑器的宽度 */
2023-06-07 15:03:41 +00:00
width: 100%;
2023-05-10 16:46:20 +00:00
margin: 0 auto;
z-index: 1008; /* 按需定义 */
}
.toolbar-container {
2023-06-05 06:32:08 +00:00
border-bottom: 1px solid var(--kungalgame-blue-4);
2023-05-10 16:46:20 +00:00
}
</style>