Files
klp-oa/klp-ui/src/components/VditorEditor.vue

80 lines
1.6 KiB
Vue
Raw Normal View History

2025-07-24 17:44:00 +08:00
<template>
<div id="vditor" style="min-height: 192px;"></div>
</template>
<script>
import Vditor from 'vditor'
import 'vditor/dist/index.css'
export default {
name: 'VditorEditor',
props: {
value: {
type: String,
default: ''
2025-07-26 10:14:40 +08:00
},
readonly: {
type: Boolean,
default: false
2025-07-24 17:44:00 +08:00
}
},
data() {
return {
vditor: null
}
},
mounted() {
2025-07-26 10:14:40 +08:00
const config = {
2025-07-24 17:44:00 +08:00
value: this.value,
height: 360,
2025-07-26 10:14:40 +08:00
cache: { enable: false },
2025-09-02 15:39:51 +08:00
theme: 'dark',
preview: {
theme: {
current: 'dark'
}
},
2025-07-24 17:44:00 +08:00
after: () => {
this.vditor.setValue(this.value || '')
},
input: (val) => {
this.$emit('input', val)
}
2025-07-26 10:14:40 +08:00
};
if (this.readonly) {
config.toolbar = []; // 不显示工具栏
config.editable = false;
} else {
config.toolbarConfig = { pin: true };
config.editable = true;
}
this.vditor = new Vditor('vditor', config);
if (this.readonly) {
this.vditor.vditor.disabled()
}
2025-07-24 17:44:00 +08:00
},
watch: {
value(val) {
if (this.vditor && val !== this.vditor.getValue()) {
this.vditor.setValue(val || '')
2025-07-26 10:14:40 +08:00
// 只读模式下内容变化时重新渲染mermaid
if (this.readonly && window.VditorPreview && typeof window.VditorPreview.mermaidRender === 'function') {
this.$nextTick(() => {
window.VditorPreview.mermaidRender(document);
});
}
2025-07-24 17:44:00 +08:00
}
}
}
}
</script>
<style scoped>
#vditor {
border: 1px solid #e4e7ed;
2025-09-24 17:23:06 +08:00
color: black;
background-color: white;
2025-07-24 17:44:00 +08:00
border-radius: 4px;
}
</style>