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
| <template> <div style="border: 1px solid #ccc"> <Toolbar style="border-bottom: 1px solid #ccc" :editor="editorRef" :defaultConfig="toolbarConfig" mode="default" /> <Editor v-model="editHtml" :defaultConfig="editorConfig" @onChange="valueChange" mode="default" @onCreated="handleCreated" /> </div> </template>
<script setup> import '@wangeditor/editor/dist/css/style.css' import { onBeforeUnmount, ref, shallowRef, onMounted, watch } from 'vue' import { Editor, Toolbar } from '@wangeditor/editor-for-vue'
import { apiFileUpload } from '@/assets/api/api_common'
const htmlModel = defineModel() const txtModel = defineModel('txt')
const props = defineProps({ savefolder: { type: String, default: 'common' } })
const editHtml = ref('')
onMounted(() => { if (htmlModel.value) { setTimeout(() => { editHtml.value = htmlModel.value }, 0) } })
watch(htmlModel, () => { if (htmlModel.value) { setTimeout(() => { editHtml.value = htmlModel.value }) } })
const editorRef = shallowRef()
const toolbarConfig = { excludeKeys: [ 'emotion', 'group-video', 'codeBlock', 'todo', 'insertLink', 'code', 'insertTable', 'fullScreen' ] } const editorConfig = { placeholder: '请输入内容...', MENU_CONF: {} }
function valueChange(editor) { txtModel.value = editor.getText() htmlModel.value = editor.getHtml() }
let baseURL = window.baseURL
editorConfig.MENU_CONF['uploadImage'] = { async customUpload(file, insertFn) { let result = await apiFileUpload(file, props.savefolder) if (result && result.code === 0) { let urlAll = baseURL + result.obj insertFn(urlAll, '', urlAll) } } }
onBeforeUnmount(() => { const editor = editorRef.value if (editor == null) return editor.destroy() })
const handleCreated = (editor) => { editorRef.value = editor } </script>
<style lang="less"> .w-e-text-container { height: 420px !important; }
.w-e-text-container .w-e-scroll { height: 500px !important; -webkit-overflow-scrolling: touch; }
</style>
|