|
|
@@ -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) {
|