Просмотр исходного кода

语音矩阵删除后,再添加时富文本输入框有时加载失败

dsy 1 месяц назад
Родитель
Сommit
ee90c7b817

+ 1 - 1
.env

@@ -11,4 +11,4 @@ VUE_APP_BookWebSI = '/GCLSBookWebSI/ServiceInterface'
 VUE_APP_EepServer = '/EEPServer/SI'
 
 #version
-VUE_APP_VERSION = '2026.10.22'
+VUE_APP_VERSION = '2026.10.23'

+ 1 - 1
packages/package.json

@@ -1,6 +1,6 @@
 {
   "name": "eep-ui",
-  "version": "0.0.62",
+  "version": "0.0.63",
   "main": "eep/eep-ui.umd.js",
   "style": "eep/eep-ui.css",
   "exports": {

+ 12 - 10
src/components/PinyinText.vue

@@ -25,7 +25,7 @@
           <span
             v-for="(word, wIndex) in block.word_list"
             :key="wIndex"
-            :class="['pinyin-text', { spacing: isShowPinyin }]"
+            class="pinyin-text"
             :style="[block.styleObj, getBlockSpacingStyle(block)]"
           >
             <span
@@ -142,7 +142,7 @@
           class="pinyin-sentence"
           :style="{ 'align-items': pinyinPosition === 'top' ? 'flex-end' : 'flex-start' }"
         >
-          <span v-for="(word, wIndex) in sentence" :key="wIndex" :class="['pinyin-text', { spacing: isShowPinyin }]">
+          <span v-for="(word, wIndex) in sentence" :key="wIndex" class="pinyin-text">
             <span
               :title="isPreview ? '' : '点击校对'"
               :style="{
@@ -901,12 +901,18 @@ export default {
         return '\u200B';
       }
       // 优先使用新的 pinyin_list 字段(与字符一一对应)
+      let value = '';
       if (word.pinyin_list && Array.isArray(word.pinyin_list)) {
-        return word.pinyin_list[charIndex] || '\u200B';
+        value = word.pinyin_list[charIndex] || '';
+      } else {
+        // 兼容旧数据:使用 pinyin 字段
+        const pinyinList = word.pinyin ? word.pinyin.trim().split(/\s+/) : [];
+        value = pinyinList[charIndex] || '';
       }
-      // 兼容旧数据:使用 pinyin 字段
-      const pinyinList = word.pinyin ? word.pinyin.trim().split(/\s+/) : [];
-      return pinyinList[charIndex] || '\u200B';
+      // 标点/空白处用全角空格占位,只用于占住拼音位,不能撑宽字符单元,
+      // 否则启用拼音后文本会偏离未启用时的排布(如用空格对齐的填空题选项)
+      const text = String(value).replace(/[\u3000\u00a0\s\u200B]+/g, '');
+      return text || '\u200B';
     },
     // 如果汉字有字间距,拼音与汉字左对齐,如果没有则居中对齐
     getWordAlignItems(word, block) {
@@ -1189,10 +1195,6 @@ export default {
     text-wrap: pretty;
     hanging-punctuation: allow-end;
 
-    &.spacing {
-      padding: 0 2px;
-    }
-
     > span {
       display: inline-flex;
     }

+ 4 - 0
src/components/RichText.vue

@@ -1519,6 +1519,10 @@ export default {
       // 更新 v-model
       this.$emit('input', text);
     },
+    syncContent() {
+      if (!this.editor) return;
+      this.$emit('input', this.editor.getContent());
+    },
     // 设置填空
     setFill() {
       this.setContent();

+ 21 - 11
src/views/book/courseware/create/components/CreateCanvas.vue

@@ -132,12 +132,7 @@
 <script>
 import { getRandomNumber } from '@/utils/index';
 import { componentList } from '../../data/bookType';
-import {
-  ContentSaveCoursewareContent,
-  ContentGetCoursewareContent,
-  GetBookUnifiedAttrib,
-  ContentSaveCoursewareComponentContent,
-} from '@/api/book';
+import { ContentSaveCoursewareContent, ContentGetCoursewareContent, GetBookUnifiedAttrib } from '@/api/book';
 import _ from 'lodash';
 import { unified_attrib } from '@/common/data';
 import { waitLayoutStable } from '@/utils/common';
@@ -472,6 +467,11 @@ export default {
         activeElement &&
         (activeElement.tagName === 'INPUT' || activeElement.tagName === 'TEXTAREA' || activeElement.isContentEditable);
       if (isInputting) return;
+
+      // 存在选中文本时不拦截,交给浏览器执行原生文本复制
+      const selection = window.getSelection();
+      if (selection && !selection.isCollapsed && String(selection).length > 0) return;
+
       if (!this.curComponentId) return;
 
       const isCtrlC =
@@ -1334,8 +1334,11 @@ export default {
 
       const oldID = this.curParams?.id;
       const component = this.findChildComponentByKey(`grid-${oldID}`);
-      // 获取组件内容,保存后再插入到新的位置
-      const requestData = component?.getComponentRequestContent();
+      if (!component || !component.property?.isGetContent) {
+        this.$message.warning('组件内容尚未加载完成,已取消移动');
+        this.resetMoveData();
+        return;
+      }
 
       const data = component?.data;
       this.curType = data?.type || 'select';
@@ -1352,10 +1355,17 @@ export default {
         oldId,
       };
 
-      // 根据 oldID 改变 data.row_list 中,将对应ID 改为新ID,并返回新ID,在删除时使用
-      const deleteID = this.replaceGridIdByOldId(oldID);
+      try {
+        await component.saveCoursewareComponentContent();
+      } catch (error) {
+        console.error('移动组件前保存内容失败', error);
+        this.$message.error('组件内容保存失败,已取消移动');
+        this.resetMoveData();
+        return;
+      }
 
-      await ContentSaveCoursewareComponentContent(requestData);
+      // 保存成功后再调整原组件 id,避免保存失败时留下不完整的页面结构
+      const deleteID = this.replaceGridIdByOldId(oldID);
 
       if (this.curRow >= -1 && this.curCol <= -1) {
         this.calculateRowInsertedObject();

+ 61 - 2
src/views/book/courseware/create/components/common/ModuleMixin.js

@@ -12,6 +12,41 @@ import {
   PinyinBuild_OldFormat,
 } from '@/api/book';
 
+const isPlainObject = (value) => Object.prototype.toString.call(value) === '[object Object]';
+
+/**
+ * 将已保存的组件内容合并到组件默认内容上
+ * 组件内容可能是空对象(如 "{}")、旧版本结构或部分字段缺失,直接覆盖会让模板读取 undefined 报错,
+ * 表现为整个组件渲染失败、变成空白,因此缺失(或为 null)的字段都回退到默认值
+ * @param {object} defaults 组件默认内容
+ * @param {object} data 已保存的组件内容
+ * @returns {object} 合并后的内容
+ */
+function mergeComponentData(defaults, data) {
+  if (!isPlainObject(defaults)) return data;
+  if (!isPlainObject(data)) return defaults;
+
+  const result = { ...defaults };
+
+  Object.keys(data).forEach((key) => {
+    const defaultValue = defaults[key];
+    const value = data[key];
+
+    // 空值回退到默认值,避免覆盖掉模板依赖的字段
+    if (value === undefined || value === null) return;
+
+    if (Array.isArray(defaultValue)) {
+      result[key] = Array.isArray(value) ? value : defaultValue;
+      return;
+    }
+
+    const isObjectValue = isPlainObject(defaultValue) && isPlainObject(value);
+    result[key] = isObjectValue ? mergeComponentData(defaultValue, value) : value;
+  });
+
+  return result;
+}
+
 const mixin = {
   data() {
     return {
@@ -124,7 +159,13 @@ const mixin = {
       ContentGetCoursewareComponentContent({ courseware_id: coursewareID, component_id })
         .then(({ content }) => {
           if (content) {
-            let oldData = JSON.parse(content);
+            let oldData = {};
+            try {
+              oldData = JSON.parse(content) ?? {};
+            } catch (error) {
+              // 内容不是合法 JSON 时不能让组件停留在空数据上
+              console.error('组件内容解析失败,已使用默认内容', error);
+            }
             if (oldData.type === 'audio') {
               let p = oldData.property || {};
               if (!p.file_name_display_mode) p.file_name_display_mode = 'true';
@@ -138,7 +179,7 @@ const mixin = {
                 p.style_mode = 'small';
               }
             }
-            this.data = oldData;
+            this.data = mergeComponentData(this.data, oldData);
           } else {
             const bookUnifiedAttr = this.getBookUnifiedAttr();
 
@@ -171,6 +212,10 @@ const mixin = {
             { deep: true },
           );
         })
+        .catch((error) => {
+          // 请求失败时保持未加载状态(避免用空内容覆盖已保存内容),但把错误暴露出来便于排查
+          console.error('组件内容获取失败', component_id, error);
+        })
         .finally(() => {
           this.$nextTick(() => {
             this.property.isSilentPropertyWatch = false;
@@ -264,6 +309,7 @@ const mixin = {
      * @returns {Promise} 返回 Promise,便于父组件 await
      */
     saveCoursewareComponentContent() {
+      this.syncRichTextContent();
       return ContentSaveCoursewareComponentContent({
         courseware_id: this.courseware_id,
         component_id: this.id,
@@ -272,16 +318,29 @@ const mixin = {
         background: this.background,
       });
     },
+    syncRichTextContent() {
+      const syncEditors = (children) => {
+        children.forEach((child) => {
+          if (child.$options.name === 'RichText' && typeof child.syncContent === 'function') {
+            child.syncContent();
+          }
+          syncEditors(child.$children || []);
+        });
+      };
+      syncEditors(this.$children || []);
+    },
     /**
      * @description 获取组件请求内容
       @returns {Object} 组件请求内容
      */
     getComponentRequestContent() {
+      this.syncRichTextContent();
       return {
         courseware_id: this.courseware_id,
         component_id: this.id,
         component_type: this.data.type,
         content: JSON.stringify(this.data),
+        background: this.background,
       };
     },
     findComponentWithRefAndMethod(children, refName, methodName) {

+ 6 - 6
src/views/book/courseware/create/components/question/voice_matrix/VoiceMatrix.vue

@@ -59,7 +59,7 @@
         </div>
 
         <SelectUpload label="矩阵音频" type="audio" @uploadSuccess="uploadAudioSuccess" />
-        <div v-if="data.audio_data.url.length > 0" class="upload-file">
+        <div v-if="data.audio_data?.url?.length > 0" class="upload-file">
           <div class="file-name">
             <span>
               <SvgIcon icon-class="note" size="12" />
@@ -92,7 +92,7 @@
           </template>
         </div>
 
-        <div v-if="data.lrc_data.url.length > 0" class="upload-file">
+        <div v-if="data.lrc_data?.url?.length > 0" class="upload-file">
           <div class="file-name">
             <span>
               <SvgIcon icon-class="note" size="12" />
@@ -106,7 +106,7 @@
 
       <CheckSubtitles
         :visible.sync="visible"
-        :audio-id="data.audio_data.file_id"
+        :audio-id="data.audio_data?.file_id"
         :option-list="data.option_list"
         @saveSubtitles="saveSubtitles"
       />
@@ -204,7 +204,7 @@ export default {
      * 解析lrc文件
      */
     parseLrcFile() {
-      if (this.data.lrc_data.file_id.length === 0) {
+      if (!this.data.lrc_data?.file_id) {
         return this.$message.warning('请先上传lrc文件');
       }
       const loading = this.$loading({ text: '解析lrc文件中' });
@@ -320,7 +320,7 @@ export default {
      * 打开校对字幕
      */
     openCheckSubtitles() {
-      if (this.data.audio_data.url.length === 0) {
+      if (!this.data.audio_data?.url?.length) {
         return this.$message.warning('请先上传音频文件');
       }
       this.visible = true;
@@ -356,7 +356,7 @@ export default {
      * 自动生成字幕节点
      */
     autoWordTime() {
-      if (this.data.audio_data.url.length === 0) {
+      if (!this.data.audio_data?.url?.length) {
         return this.$message.warning('请先上传音频文件');
       }
       const text = this.data.option_list