Skip to content

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.

feat: create empty doc by pass empty object `{}` (#1606)

Ran Luoff54fa01e428
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 9d3b21b080..002c3e180e 100644
--- a/packages/core/src/docs/data-model/document-data-model.ts
+++ b/packages/core/src/docs/data-model/document-data-model.ts
@@ -31,6 +31,7 @@ import { updateAttributeByInsert } from './apply-utils/insert-apply';
import { updateAttribute } from './apply-utils/update-apply';
import { type TextXAction, TextXActionType } from './action-types';
import { getBodySlice } from './text-x/utils';
+import { getEmptySnapshot } from './empty-snapshot';
export const DEFAULT_DOC = {
id: 'default_doc',
@@ -206,6 +207,9 @@ export class DocumentDataModel extends DocumentDataModelSimple {
footerModelMap: Map<string, DocumentDataModel> = new Map();
constructor(snapshot: Partial<IDocumentData>) {
+ if (Tools.isEmptyObject(snapshot)) {
+ snapshot = getEmptySnapshot();
+ }
super(snapshot);
const UNIT_ID_LENGTH = 6;
diff --git a/packages/core/src/docs/data-model/empty-snapshot.ts b/packages/core/src/docs/data-model/empty-snapshot.ts
new file mode 100644
index 0000000000..b24eadcb6e
--- /dev/null
+++ b/packages/core/src/docs/data-model/empty-snapshot.ts
@@ -0,0 +1,68 @@
+/**
+ * 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 { Tools } from '../../shared/tools';
+import { LocaleType } from '../../types/enum/locale-type';
+import type { IDocumentData } from '../../types/interfaces';
+
+export function getEmptySnapshot(
+ unitID = Tools.generateRandomId(6),
+ locale = LocaleType.EN_US,
+ title = ''
+): IDocumentData {
+ const EMPTY_DOCUMENT_DATA: IDocumentData = {
+ id: unitID,
+ locale,
+ title, // title should get from request.
+ body: {
+ dataStream: '\r\n',
+ textRuns: [],
+ paragraphs: [
+ {
+ startIndex: 0,
+ paragraphStyle: {
+ spaceAbove: 5,
+ lineSpacing: 1,
+ spaceBelow: 0,
+ },
+ },
+ ],
+ sectionBreaks: [
+ {
+ startIndex: 1,
+ },
+ ],
+ },
+ documentStyle: {
+ pageSize: {
+ width: 595 / 0.75,
+ height: 842 / 0.75,
+ },
+ marginTop: 50,
+ marginBottom: 50,
+ marginRight: 40,
+ marginLeft: 40,
+ renderConfig: {
+ vertexAngle: 0,
+ centerAngle: 0,
+ },
+ },
+ settings: {},
+
+ };
+
+ return EMPTY_DOCUMENT_DATA;
+}
diff --git a/packages/core/src/shared/tools.ts b/packages/core/src/shared/tools.ts
index 319380e4f6..2fe9ada525 100644
--- a/packages/core/src/shared/tools.ts
+++ b/packages/core/src/shared/tools.ts
@@ -405,7 +405,7 @@ export class Tools {
static isEmptyObject(value?: any): boolean {
// eslint-disable-next-line no-unreachable-loop
- for (const key in value) {
+ for (const _key in value) {
return false;
}
return true;