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(dev): update doc devleop

liuyang.0809272c75a9ac77
diff --git a/packages/core/src/Docs/Action/ActionDataInterface.ts b/packages/core/src/Docs/Action/ActionDataInterface.ts
index d3b99e31c6..2a5df054b3 100644
--- a/packages/core/src/Docs/Action/ActionDataInterface.ts
+++ b/packages/core/src/Docs/Action/ActionDataInterface.ts
@@ -1,9 +1,15 @@
import { IDocActionData } from '../../Command/DocActionBase';
-import {
- CommonParameterAttribute,
- IDocumentBody,
-} from '../../Interfaces/IDocumentData';
+import { UpdateAttributeValueType, IDocumentBody } from '../../Interfaces/IDocumentData';
+import { UpdateDocsAttributeType } from '../../Shared/CommandEnum';
+export enum UpdateAttributeType {
+ TEXT_RUN,
+ PARAGRAPH,
+ SECTION,
+ CUSTOM_BLOCK,
+ TABLE,
+ CUSTOM_RANGE,
+}
export interface IDeleteActionData extends IDocActionData {
len: number;
line: number;
@@ -18,8 +24,10 @@ export interface IInsertActionData extends IDocActionData {
}
export interface IRetainActionData extends IDocActionData {
- attribute?: CommonParameterAttribute;
+ attributes?: UpdateAttributeValueType[];
+ attributeType?: UpdateAttributeType;
len: number;
+ coverType: UpdateDocsAttributeType;
segmentId?: string;
}
diff --git a/packages/core/src/Docs/Action/RetainAction.ts b/packages/core/src/Docs/Action/RetainAction.ts
index c87ab10e7e..172f0f1fe6 100644
--- a/packages/core/src/Docs/Action/RetainAction.ts
+++ b/packages/core/src/Docs/Action/RetainAction.ts
@@ -1,50 +1,51 @@
import { DocActionBase } from '../../Command/DocActionBase';
-import {
- ActionObservers,
- CommandManager,
- CommandUnit,
- CommonParameter,
-} from '../../Command';
-import { CommonParameterAttribute } from '../../Interfaces/IDocumentData';
+import { ActionObservers, CommandManager, CommandUnit, CommonParameter } from '../../Command';
+import { UpdateAttributeValueType } from '../../Interfaces/IDocumentData';
import { IRetainActionData } from './ActionDataInterface';
+import { UpdateDocsAttributeType } from '../../Shared/CommandEnum';
+import { UpdateAttributeApply } from '../Apply/UpdateAttributeApply';
-export class RetainAction extends DocActionBase<
- IRetainActionData,
- IRetainActionData
-> {
+export class RetainAction extends DocActionBase<IRetainActionData, IRetainActionData> {
static NAME = 'RetainAction';
- constructor(
- actionData: IRetainActionData,
- commandUnit: CommandUnit,
- observers: ActionObservers,
- commonParameter: CommonParameter
- ) {
+ constructor(actionData: IRetainActionData, commandUnit: CommandUnit, observers: ActionObservers, commonParameter: CommonParameter) {
super(actionData, commandUnit, observers);
this._doActionData = { ...actionData };
- this._oldActionData = { ...actionData, attribute: this.do(commonParameter) };
+ const undoData = { ...actionData, coverType: UpdateDocsAttributeType.REPLACE };
+
+ const attributes = this.do(commonParameter);
+ if (attributes.length > 0) {
+ undoData.attributes = attributes;
+ }
+ this._oldActionData = undoData;
}
redo(commonParameter: CommonParameter): void {
this.do(commonParameter);
}
- do(commonParameter: CommonParameter): CommonParameterAttribute {
+ do(commonParameter: CommonParameter): UpdateAttributeValueType[] {
const actionData = this.getDoActionData();
const document = this.getDocument();
- const { len, segmentId, attribute } = actionData;
+ const { len, segmentId, attributes, coverType, attributeType } = actionData;
+
+ let results: UpdateAttributeValueType[] = [];
+ if (attributes && attributeType) {
+ results = UpdateAttributeApply(document, len, commonParameter.cursor, attributes, attributeType, coverType, segmentId);
+ }
+
commonParameter.moveCursor(len);
- // InsertTextApply(document, actionData.text);
- return {
- ed: 0,
- st: 0,
- };
+
+ return results;
}
undo(commonParameter: CommonParameter): void {
const actionData = this.getOldActionData();
const document = this.getDocument();
- const { len, segmentId, attribute } = actionData;
+ const { len, segmentId, attributes, coverType, attributeType } = actionData;
+
+ if (attributes && attributeType) UpdateAttributeApply(document, len, commonParameter.cursor, attributes, attributeType, coverType, segmentId);
+
commonParameter.moveCursor(len);
}
diff --git a/packages/core/src/Docs/Apply/UpdateAttribute.ts b/packages/core/src/Docs/Apply/UpdateAttribute.ts
deleted file mode 100644
index e69de29bb2..0000000000
diff --git a/packages/core/src/Docs/Apply/UpdateAttributeApply.ts b/packages/core/src/Docs/Apply/UpdateAttributeApply.ts
new file mode 100644
index 0000000000..2b38c36b93
--- /dev/null
+++ b/packages/core/src/Docs/Apply/UpdateAttributeApply.ts
@@ -0,0 +1,107 @@
+import { UpdateAttributeValueType, IDocumentBody, ITextRun } from '../../Interfaces/IDocumentData';
+import { DocumentModel } from '../Domain/DocumentModel';
+import { getDocsUpdateBody, horizontalLineSegmentsSubtraction } from '../../Shared/Common';
+import { UpdateDocsAttributeType } from '../../Shared/CommandEnum';
+import { UpdateAttributeType } from '../Action/ActionDataInterface';
+
+export function UpdateAttributeApply(
+ document: DocumentModel,
+ textLength: number,
+ currentIndex: number,
+ attributes: UpdateAttributeValueType[],
+ attributeType: UpdateAttributeType,
+ coverType: UpdateDocsAttributeType,
+ segmentId?: string
+) {
+ const doc = document.snapshot;
+
+ const body = getDocsUpdateBody(doc, segmentId);
+
+ if (textLength === 0) {
+ return [];
+ }
+
+ if (body == null) {
+ throw new Error('no body has changed');
+ }
+
+ return updateAttribute(body, textLength, currentIndex, attributes, attributeType, coverType);
+}
+
+function updateAttribute(
+ body: IDocumentBody,
+ textLength: number,
+ currentIndex: number,
+ attributes: UpdateAttributeValueType[],
+ attributeType: UpdateAttributeType,
+ coverType: UpdateDocsAttributeType
+) {
+ if (attributeType === UpdateAttributeType.TEXT_RUN) {
+ return processTextRuns(body, textLength, currentIndex, attributes as ITextRun[], coverType);
+ }
+
+ return [];
+}
+
+function processTextRuns(body: IDocumentBody, textLength: number, currentIndex: number, updateTextRuns: ITextRun[], coverType: UpdateDocsAttributeType) {
+ const { textRuns } = body;
+
+ if (textRuns == null) {
+ return [];
+ }
+
+ const startIndex = currentIndex;
+
+ const endIndex = currentIndex + textLength - 1;
+
+ const removeTextRuns = [];
+ for (let i = 0, len = textRuns.length; i < len; i++) {
+ const textRun = textRuns[i];
+ const { st, ed } = textRun;
+
+ if (startIndex <= st && endIndex >= ed) {
+ removeTextRuns.push({
+ ...textRun,
+ st: st - currentIndex,
+ ed: ed - currentIndex,
+ });
+
+ for (let updateTextRun of updateTextRuns) {
+ const { st: updateSt, ed: updateEd } = updateTextRun;
+ }
+
+ continue;
+ } else if (st <= startIndex && ed >= endIndex) {
+ const segments = horizontalLineSegmentsSubtraction(st, ed, startIndex, endIndex);
+
+ if (segments.length > 2) {
+ const seg1 = segments[0];
+ const seg2 = segments[1];
+ textRun.st = seg1[0];
+ textRun.ed = seg1[1] + seg2[1] - seg2[0] + 1;
+ } else {
+ textRun.st = segments[0][0];
+ textRun.ed = segments[0][1];
+ }
+ } else if (startIndex >= st && startIndex <= ed) {
+ removeTextRuns.push({
+ ...textRun,
+ st: startIndex - currentIndex,
+ ed: ed - currentIndex,
+ });
+ textRun.ed = startIndex - 1;
+ } else if (endIndex >= st && endIndex <= ed) {
+ removeTextRuns.push({
+ ...textRun,
+ st: st - currentIndex,
+ ed: endIndex - currentIndex,
+ });
+ textRun.st = endIndex + 1;
+ } else if (st > endIndex) {
+ textRun.st -= textLength;
+ textRun.ed -= textLength;
+ }
+ }
+
+ return removeTextRuns;
+}
diff --git a/packages/core/src/Docs/Apply/UpdateDocument.ts b/packages/core/src/Docs/Apply/UpdateDocument.ts
deleted file mode 100644
index e69de29bb2..0000000000
diff --git a/packages/core/src/Docs/Apply/UpdateDocumentApply.ts b/packages/core/src/Docs/Apply/UpdateDocumentApply.ts
new file mode 100644
index 0000000000..dfe383d995
--- /dev/null
+++ b/packages/core/src/Docs/Apply/UpdateDocumentApply.ts
@@ -0,0 +1,18 @@
+import { DocumentModel } from '../Domain/DocumentModel';
+import { getDocsUpdateBody } from '../../Shared/Common';
+
+export function UpdateDocumentApply(document: DocumentModel, textLength: number, currentIndex: number, segmentId?: string) {
+ const doc = document.snapshot;
+
+ const bodyModel = document.getBodyModel(segmentId);
+
+ const body = getDocsUpdateBody(doc, segmentId);
+
+ if (textLength === 0) {
+ return;
+ }
+
+ if (body == null) {
+ throw new Error('no body has changed');
+ }
+}
diff --git a/packages/core/src/Docs/Apply/index.ts b/packages/core/src/Docs/Apply/index.ts
index d7f3d2ee66..66e86d4ad7 100644
--- a/packages/core/src/Docs/Apply/index.ts
+++ b/packages/core/src/Docs/Apply/index.ts
@@ -1,4 +1,4 @@
export * from './InsertApply';
export * from './DeleteApply';
-export * from './UpdateAttribute';
-export * from './UpdateDocument';
+export * from './UpdateAttributeApply';
+export * from './UpdateDocumentApply';
diff --git a/packages/core/src/Interfaces/IDocumentData.ts b/packages/core/src/Interfaces/IDocumentData.ts
index 7ea3d087f1..17f0f04320 100644
--- a/packages/core/src/Interfaces/IDocumentData.ts
+++ b/packages/core/src/Interfaces/IDocumentData.ts
@@ -65,7 +65,7 @@ export interface ISizeData {
height?: number;
}
-export type CommonParameterAttribute = ITextRun | IParagraph | ISectionBreak | ICustomBlock | ITable | ICustomRange;
+export type UpdateAttributeValueType = ITextRun | IParagraph | ISectionBreak | ICustomBlock | ITable | ICustomRange;
/**
* Properties of document body
diff --git a/packages/core/src/Shared/CommandEnum.ts b/packages/core/src/Shared/CommandEnum.ts
new file mode 100644
index 0000000000..d69b845bdd
--- /dev/null
+++ b/packages/core/src/Shared/CommandEnum.ts
@@ -0,0 +1,5 @@
+export enum UpdateDocsAttributeType {
+ COVER, // Default, if not present, add; if present, overwrite, while retaining the original properties.
+ REPLACE, // Replace the original properties entirely.
+ INTERSECTION, // Retain only the intersecting properties.
+}