|
@@ -10,7 +10,7 @@
|
|
|
@update:content="handleContentUpdate"
|
|
|
@ready="handleEditorReady"
|
|
|
@error="handleEditorError"
|
|
|
- contentType="delta"
|
|
|
+ contentType="html"
|
|
|
/>
|
|
|
</div>
|
|
|
<div v-else class="placeholder-container">
|
|
@@ -24,7 +24,7 @@
|
|
|
|
|
|
<script setup>
|
|
|
/* eslint-disable no-undef */
|
|
|
-import { ref, computed, onUnmounted } from 'vue';
|
|
|
+import { ref, computed, onUnmounted, watch } from 'vue';
|
|
|
import { QuillEditor } from '@vueup/vue-quill';
|
|
|
import '@vueup/vue-quill/dist/vue-quill.snow.css';
|
|
|
import { Document } from '@element-plus/icons-vue';
|
|
@@ -48,16 +48,7 @@ const emit = defineEmits(['update:content']);
|
|
|
|
|
|
const editorKey = ref(0);
|
|
|
const editorInstance = ref(null);
|
|
|
-
|
|
|
-// 计算编辑器内容
|
|
|
-const editorContent = computed({
|
|
|
- get() {
|
|
|
- return props.content ?? '';
|
|
|
- },
|
|
|
- set(value) {
|
|
|
- emit('update:content', value);
|
|
|
- }
|
|
|
-});
|
|
|
+const internalContent = ref('');
|
|
|
|
|
|
const editorOptions = {
|
|
|
theme: 'snow',
|
|
@@ -72,6 +63,52 @@ const editorOptions = {
|
|
|
placeholder: '请输入章节内容...'
|
|
|
};
|
|
|
|
|
|
+// 计算编辑器内容
|
|
|
+const editorContent = computed({
|
|
|
+ get() {
|
|
|
+ return internalContent.value;
|
|
|
+ },
|
|
|
+ set(value) {
|
|
|
+ internalContent.value = value;
|
|
|
+ emit('update:content', value);
|
|
|
+ }
|
|
|
+});
|
|
|
+
|
|
|
+// 将Delta格式转换为HTML
|
|
|
+function deltaToHtml(delta) {
|
|
|
+ if (!delta || !delta.ops) {
|
|
|
+ return '';
|
|
|
+ }
|
|
|
+
|
|
|
+ let html = '';
|
|
|
+ for (const op of delta.ops) {
|
|
|
+ if (op.insert) {
|
|
|
+ if (typeof op.insert === 'string') {
|
|
|
+ html += op.insert;
|
|
|
+ } else if (op.insert.image) {
|
|
|
+ html += `<img src="${op.insert.image}" alt="image">`;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return html;
|
|
|
+}
|
|
|
+
|
|
|
+// 监听props.content变化
|
|
|
+watch(() => props.content, (newContent) => {
|
|
|
+ if (newContent) {
|
|
|
+ // 如果是Delta格式,转换为HTML
|
|
|
+ if (typeof newContent === 'object' && newContent.ops) {
|
|
|
+ internalContent.value = deltaToHtml(newContent);
|
|
|
+ } else if (typeof newContent === 'string') {
|
|
|
+ internalContent.value = newContent;
|
|
|
+ } else {
|
|
|
+ internalContent.value = '';
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ internalContent.value = '';
|
|
|
+ }
|
|
|
+}, { immediate: true });
|
|
|
+
|
|
|
// 处理编辑器准备就绪
|
|
|
function handleEditorReady(quill) {
|
|
|
editorInstance.value = quill;
|