Skip to content

Commit

Permalink
fix(sheets): fix shallow copy bugs of the sheet snapshot (#1742)
Browse files Browse the repository at this point in the history
* fix(sheets): fix shallow copy bugs of the sheet snapshot

* fix: fllow the doc case to rebuild the sheet snapshot
  • Loading branch information
karlsbeard committed Mar 29, 2024
1 parent 82e5171 commit 83d910c
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 23 deletions.
5 changes: 1 addition & 4 deletions packages/core/src/docs/data-model/document-data-model.ts
Expand Up @@ -207,10 +207,7 @@ export class DocumentDataModel extends DocumentDataModelSimple {
footerModelMap: Map<string, DocumentDataModel> = new Map();

constructor(snapshot: Partial<IDocumentData>) {
if (Tools.isEmptyObject(snapshot)) {
snapshot = getEmptySnapshot();
}
super(snapshot);
super(Tools.isEmptyObject(snapshot) ? getEmptySnapshot() : snapshot);

const UNIT_ID_LENGTH = 6;

Expand Down
38 changes: 38 additions & 0 deletions 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;
}
10 changes: 8 additions & 2 deletions packages/core/src/sheets/workbook.ts
Expand Up @@ -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,
Expand All @@ -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()}`;
Expand Down Expand Up @@ -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) {
Expand Down
17 changes: 0 additions & 17 deletions packages/core/src/types/const/const.ts
Expand Up @@ -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
Expand Down Expand Up @@ -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.
*/
Expand Down

0 comments on commit 83d910c

Please sign in to comment.