From 83d910c328e91b196491fc3459de2744d3948b90 Mon Sep 17 00:00:00 2001 From: karl <50100424+karlsbeard@users.noreply.github.com> Date: Fri, 29 Mar 2024 18:34:04 +0800 Subject: [PATCH] fix(sheets): fix shallow copy bugs of the sheet snapshot (#1742) * fix(sheets): fix shallow copy bugs of the sheet snapshot * fix: fllow the doc case to rebuild the sheet snapshot --- .../docs/data-model/document-data-model.ts | 5 +-- packages/core/src/sheets/empty-snapshot.ts | 38 +++++++++++++++++++ packages/core/src/sheets/workbook.ts | 10 ++++- packages/core/src/types/const/const.ts | 17 --------- 4 files changed, 47 insertions(+), 23 deletions(-) create mode 100644 packages/core/src/sheets/empty-snapshot.ts diff --git a/packages/core/src/docs/data-model/document-data-model.ts b/packages/core/src/docs/data-model/document-data-model.ts index 002c3e180e7..6dac3f67095 100644 --- a/packages/core/src/docs/data-model/document-data-model.ts +++ b/packages/core/src/docs/data-model/document-data-model.ts @@ -207,10 +207,7 @@ export class DocumentDataModel extends DocumentDataModelSimple { footerModelMap: Map = new Map(); constructor(snapshot: Partial) { - if (Tools.isEmptyObject(snapshot)) { - snapshot = getEmptySnapshot(); - } - super(snapshot); + super(Tools.isEmptyObject(snapshot) ? getEmptySnapshot() : snapshot); const UNIT_ID_LENGTH = 6; diff --git a/packages/core/src/sheets/empty-snapshot.ts b/packages/core/src/sheets/empty-snapshot.ts new file mode 100644 index 00000000000..64b80db6866 --- /dev/null +++ b/packages/core/src/sheets/empty-snapshot.ts @@ -0,0 +1,38 @@ +/** + * Copyright 2023-present DreamNum Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { LocaleType } from '../types/enum/locale-type'; +import type { IWorkbookData } from '../types/interfaces'; +import { version } from '../../package.json'; + +export function getEmptySnapshot( + unitID = '', + locale = LocaleType.ZH_CN, + name = '' +): IWorkbookData { + const DEFAULT_WORKBOOK_DATA: IWorkbookData = { + id: unitID, + sheetOrder: [], + name, + appVersion: version, + locale, + styles: {}, + sheets: {}, + resources: [], + }; + + return DEFAULT_WORKBOOK_DATA; +} diff --git a/packages/core/src/sheets/workbook.ts b/packages/core/src/sheets/workbook.ts index d5cf25e5bf7..51ab7a90ef3 100644 --- a/packages/core/src/sheets/workbook.ts +++ b/packages/core/src/sheets/workbook.ts @@ -20,7 +20,7 @@ import { ILogService } from '../services/log/log.service'; import type { Nullable } from '../shared'; import { Tools } from '../shared'; import { Disposable } from '../shared/lifecycle'; -import { DEFAULT_RANGE_ARRAY, DEFAULT_WORKBOOK } from '../types/const'; +import { DEFAULT_RANGE_ARRAY } from '../types/const'; import { BooleanNumber } from '../types/enum'; import type { IColumnStartEndData, @@ -34,6 +34,7 @@ import type { } from '../types/interfaces'; import { Styles } from './styles'; import { Worksheet } from './worksheet'; +import { getEmptySnapshot } from './empty-snapshot'; export function getWorksheetUID(workbook: Workbook, worksheet: Worksheet): string { return `${workbook.getUnitId()}|${worksheet.getSheetId()}`; @@ -87,7 +88,12 @@ export class Workbook extends Disposable { ) { super(); - this._snapshot = Tools.commonExtend(DEFAULT_WORKBOOK, workbookData); + const DEFAULT_WORKBOOK = getEmptySnapshot(); + if (Tools.isEmptyObject(workbookData)) { + this._snapshot = DEFAULT_WORKBOOK; + } else { + this._snapshot = Tools.commonExtend(DEFAULT_WORKBOOK, workbookData); + } const { styles } = this._snapshot; if (this._snapshot.id == null || this._snapshot.id.length === 0) { diff --git a/packages/core/src/types/const/const.ts b/packages/core/src/types/const/const.ts index 55afc5e5027..3d20c10b6f1 100644 --- a/packages/core/src/types/const/const.ts +++ b/packages/core/src/types/const/const.ts @@ -17,13 +17,10 @@ import { BooleanNumber, HorizontalAlign, - LocaleType, TextDirection, VerticalAlign, WrapStrategy, } from '../enum'; -import type { IWorkbookData } from '../interfaces'; -import { version } from '../../../package.json'; /** * Used as an illegal range array return value @@ -65,20 +62,6 @@ export const DEFAULT_CELL = { column: 0, }; -/** - * Used as an init workbook return value - */ -export const DEFAULT_WORKBOOK: IWorkbookData = { - id: '', - sheetOrder: [], - name: '', - appVersion: version, - locale: LocaleType.ZH_CN, - styles: {}, - sheets: {}, - resources: [], -}; - /** * Default styles. */