Build your own AI office with Univer Office SDK. Bring editing, calculation, collaboration, and file processing into your product for people and AI agents. Built on an extensible open-source core.
TypeScript boardcollaborationdocdocxexceloffce365officepdfpptpptxpresentationsdksheetslidesspreadsheetuniverwordwordprocessorworkspacexlsx
fix(base): fix empty base snapshot build (#7098)
wpxp123456b7b77b6de716
diff --git a/packages/core/src/bases/base-data-model.ts b/packages/core/src/bases/base-data-model.tsindex a346ea2ca8..b96cb507ee 100644--- a/packages/core/src/bases/base-data-model.ts+++ b/packages/core/src/bases/base-data-model.ts@@ -20,6 +20,7 @@ import { BehaviorSubject } from 'rxjs';import { UnitModel, UniverInstanceType } from '../common/unit';import { Tools } from '../shared/tools';import { CellValueType } from '../types/enum';+import { getEmptyBaseSnapshot } from './empty-snapshot';const BASE_LIST_VALUE_SEPARATOR = ', ';const BASE_ATTACHMENT_RESOURCE_KEY_SEPARATOR = '\u001F';@@ -36,16 +37,21 @@ export class BaseDataModel extends UnitModel<IBaseSnapshot, UniverInstanceType.Usuper();const now = Date.now();- this._snapshot = normalizeBaseSnapshot(Tools.commonExtend({- id: '',- name: '',- schemaVersion: 1,- tables: {},- tableOrder: [],- createdAt: now,- updatedAt: now,- rev: 1,- } as IBaseSnapshot, snapshot));++ const defaultSnapshot = Tools.isEmptyObject(snapshot)+ ? getEmptyBaseSnapshot()+ : {+ id: '',+ name: '',+ schemaVersion: 1,+ tables: {},+ tableOrder: [],+ createdAt: now,+ updatedAt: now,+ rev: 1,+ };++ this._snapshot = normalizeBaseSnapshot(Tools.commonExtend(defaultSnapshot, snapshot));if (!this._snapshot.id) {this._snapshot.id = `base-${Math.random().toString(36).slice(2, 10)}`;diff --git a/packages/core/src/bases/empty-snapshot.ts b/packages/core/src/bases/empty-snapshot.tsnew file mode 100644index 0000000000..753a425747--- /dev/null+++ b/packages/core/src/bases/empty-snapshot.ts@@ -0,0 +1,117 @@+/**+ * Copyright 2023-present DreamNum Co., Ltd.+ *+ * 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 type { IBaseSnapshot, IFieldSnapshot, IRecordSnapshot, ITableSnapshot, IViewSnapshot } from './typedef';++export interface ICreateDefaultBaseTableSnapshotOptions {+ id: string;+ name: string;+ primaryFieldId?: string;+ primaryFieldName?: string;+ gridViewId?: string;+ recordCount?: number;+ now?: number;+}++export function createDefaultBaseTableSnapshot(options: ICreateDefaultBaseTableSnapshotOptions): ITableSnapshot {+ const now = options.now ?? Date.now();+ const primaryFieldId = options.primaryFieldId ?? `${options.id}-field-name`;+ const gridViewId = options.gridViewId ?? `${options.id}-view-grid`;+ const recordCount = options.recordCount ?? 5;+ const primaryField: IFieldSnapshot = {+ id: primaryFieldId,+ name: options.primaryFieldName ?? 'Name',+ type: 'text',+ config: { placeholder: 'bases.fieldConfig.textPlaceholder' },+ };+ const records: Record<string, IRecordSnapshot> = {};+ const recordOrder: string[] = [];+ const rowIndex: Record<string, number> = {};+ const rowId: Record<number, string> = {};+ const cellData: NonNullable<ITableSnapshot['cellData']> = {};++ for (let index = 0; index < recordCount; index++) {+ const recordId = `${options.id}-record-${index + 1}`;+ records[recordId] = {+ id: recordId,+ values: {},+ orderKey: String(index + 1).padStart(4, '0'),+ createdAt: now,+ updatedAt: now,+ };+ recordOrder.push(recordId);+ rowIndex[recordId] = index;+ rowId[index] = recordId;+ cellData[index] = {};+ }++ const gridView: IViewSnapshot = {+ id: gridViewId,+ tableId: options.id,+ name: 'Grid',+ type: 'grid',+ fieldOrder: [primaryFieldId],+ fieldSettings: {},+ config: { frozenFieldCount: 1 },+ };++ return {+ id: options.id,+ name: options.name,+ primaryFieldId,+ fieldOrder: [primaryFieldId],+ fields: { [primaryFieldId]: primaryField },+ records,+ recordOrder,+ rowIndex,+ rowId,+ colIndex: { [primaryFieldId]: 0 },+ colId: { 0: primaryFieldId },+ cellData,+ resources: {+ attachmentSets: {},+ attachments: {},+ },+ views: { [gridViewId]: gridView },+ viewOrder: [gridViewId],+ };+}++export function getEmptyBaseSnapshot(+ unitID = '',+ name = ''+): IBaseSnapshot {+ const now = Date.now();+ const tableId = 'table-1';++ return {+ id: unitID,+ name,+ schemaVersion: 1,+ tableOrder: [tableId],+ tables: {+ [tableId]: createDefaultBaseTableSnapshot({+ id: tableId,+ name: 'Table 1',+ now,+ recordCount: 5,+ }),+ },+ createdAt: now,+ updatedAt: now,+ rev: 1,+ };+}diff --git a/packages/core/src/bases/index.ts b/packages/core/src/bases/index.tsindex 4320d663e2..6eea0fdadb 100644--- a/packages/core/src/bases/index.ts+++ b/packages/core/src/bases/index.ts@@ -15,6 +15,7 @@*/export { BaseDataModel } from './base-data-model';+export { createDefaultBaseTableSnapshot, getEmptyBaseSnapshot, type ICreateDefaultBaseTableSnapshotOptions } from './empty-snapshot';export type {BaseCellData,BaseCellMatrix,