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(doc): editor doc

DR-Univere9b28e75ad17
diff --git a/packages/core/src/Command/ActionBase.ts b/packages/core/src/Command/ActionBase.ts
index a61c331b35..b951e8ae0e 100644
--- a/packages/core/src/Command/ActionBase.ts
+++ b/packages/core/src/Command/ActionBase.ts
@@ -1,4 +1,4 @@
-import { ActionObservers } from './index';
+import { ActionObservers, CommonParameter } from './index';
/**
* Format of action data param
@@ -86,11 +86,11 @@ export abstract class ActionBase<
this._operation &= ~operation;
}
- abstract redo(): void;
+ abstract redo(commonParameter?: CommonParameter): void;
- abstract undo(): void;
+ abstract undo(commonParameter?: CommonParameter): void;
- abstract do(): R;
+ abstract do(commonParameter?: CommonParameter): R;
abstract validate(): boolean;
}
diff --git a/packages/core/src/Command/Command.ts b/packages/core/src/Command/Command.ts
index 2e9a5a3f0b..ae7c200ca5 100644
--- a/packages/core/src/Command/Command.ts
+++ b/packages/core/src/Command/Command.ts
@@ -7,6 +7,7 @@ import {
CommandInjector,
CommandManager,
ActionOperation,
+ CommonParameter,
} from './index';
import { DocumentModel } from '../Docs/Domain/DocumentModel';
@@ -28,6 +29,8 @@ export class Command {
_actionList: Array<ActionBase<IActionData>>;
+ private _commonParameter = new CommonParameter();
+
constructor(commandUnit: CommandUnit, ...list: IActionData[]) {
this._unit = commandUnit;
this._actionDataList = list;
@@ -67,7 +70,7 @@ export class Command {
redo(): void {
this._actionList.forEach((action) => {
if (ActionOperation.hasUndo(action.getDoActionData())) {
- action.redo();
+ action.redo(this._commonParameter.reset());
}
});
CommandManager.getCommandObservers().notifyObservers({
@@ -81,9 +84,9 @@ export class Command {
// 1. removeColumn C:E 2.insertColumnData A,
// when undo, it should be
// 1. removeColumn A, 2. insertColumnData C:E
- this._actionList.reverse().forEach((action) => {
+ this._actionList.forEach((action) => {
if (ActionOperation.hasUndo(action.getOldActionData())) {
- action.undo();
+ action.undo(this._commonParameter.reset());
}
});
CommandManager.getCommandObservers().notifyObservers({
@@ -93,6 +96,20 @@ export class Command {
}
invoke(): void {
+ this._actionDataList.forEach((data) => {
+ const ActionClass = CommandManager.getAction(data.actionName);
+ if (!ActionClass) return;
+ const observers = CommandManager.getActionObservers();
+ const action = new ActionClass(
+ data,
+ this._unit,
+ observers,
+ this._commonParameter.reset()
+ );
+
+ this._actionList.push(action);
+ });
+
CommandManager.getCommandInjectorObservers().notifyObservers(
this.getInjector()
);
diff --git a/packages/core/src/Command/CommandManager.ts b/packages/core/src/Command/CommandManager.ts
index 2047bc9dfb..f0db2f495e 100644
--- a/packages/core/src/Command/CommandManager.ts
+++ b/packages/core/src/Command/CommandManager.ts
@@ -81,19 +81,11 @@ export class CommandManager {
invoke(command: Command): void {
const { _undoManager } = this;
- const { _actionDataList, _unit, _actionList } = command;
+ const { _actionDataList } = command;
// Action may be added or reissued
this._actionExtensionManager.handle(_actionDataList);
- _actionDataList.forEach((data) => {
- const ActionClass = CommandManager.getAction(data.actionName);
- if (!ActionClass) return;
- const observers = CommandManager.getActionObservers();
- const action = new ActionClass(data, _unit, observers);
-
- _actionList.push(action);
- });
command.invoke();
_undoManager.push(command);
diff --git a/packages/core/src/Command/CommonParameter.ts b/packages/core/src/Command/CommonParameter.ts
new file mode 100644
index 0000000000..686fcd4371
--- /dev/null
+++ b/packages/core/src/Command/CommonParameter.ts
@@ -0,0 +1,33 @@
+import {
+ ITextRun,
+ IParagraph,
+ ISectionBreak,
+ ICustomBlock,
+ ITable,
+ ICustomRange,
+} from '../Interfaces';
+import { Nullable } from '../Shared';
+
+type CommonParameterAttribute = Nullable<
+ ITextRun | IParagraph | ISectionBreak | ICustomBlock | ITable | ICustomRange
+>;
+
+export class CommonParameter {
+ cursor: number;
+
+ attributes: CommonParameterAttribute[] = [];
+
+ reset() {
+ this.cursor = 0;
+ this.attributes = [];
+ return this;
+ }
+
+ resetAttribute() {
+ this.attributes = [];
+ }
+
+ addAttribute(attr: CommonParameterAttribute) {
+ this.attributes.push(attr);
+ }
+}
diff --git a/packages/core/src/Command/index.ts b/packages/core/src/Command/index.ts
index e0fe53b910..13a266eb84 100644
--- a/packages/core/src/Command/index.ts
+++ b/packages/core/src/Command/index.ts
@@ -1,4 +1,5 @@
// @index(['./*.ts','./Action/index.ts'], f => `export * from '${f.path}'`);
+export * from './CommonParameter';
export * from './ActionBase';
export * from './ActionObservers';
export * from './CommandInjectorObservers';
diff --git a/packages/core/src/Docs/Action/DeleteAction.ts b/packages/core/src/Docs/Action/DeleteAction.ts
new file mode 100644
index 0000000000..f82a854595
--- /dev/null
+++ b/packages/core/src/Docs/Action/DeleteAction.ts
@@ -0,0 +1,70 @@
+import { DocActionBase, IDocActionData } from '../../Command/DocActionBase';
+import { ActionObservers, CommandManager, CommandUnit } from '../../Command';
+import { IInsertTextActionData } from './InsertAction';
+
+export interface IDeleteTextActionData extends IDocActionData {
+ len: number;
+ segmentId?: string;
+}
+
+export class DeleteTextAction extends DocActionBase<
+ IDeleteTextActionData,
+ IInsertTextActionData
+> {
+ static Name = 'DeleteTextAction';
+
+ constructor(
+ actionData: IDeleteTextActionData,
+ commandUnit: CommandUnit,
+ observers: ActionObservers
+ ) {
+ super(actionData, commandUnit, observers);
+ this._doActionData = { ...actionData };
+ this.do();
+ const { len, segmentId } = actionData;
+ // this._oldActionData = {
+ // actionName: DOC_ACTION_NAMES.INSERT_TEXT_ACTION_NAME,
+ // text,
+ // textLength,
+ // cursorStart,
+ // isStartBack,
+ // segmentId,
+ // };
+ }
+
+ redo(): void {
+ this.do();
+ }
+
+ do(): void {
+ const actionData = this.getDoActionData();
+ const document = this.getDocument();
+ const { len, segmentId } = actionData;
+ // DeleteTextApply(document, {
+ // cursorStart,
+ // cursorEnd,
+ // isStartBack,
+ // isEndBack,
+ // isCollapse,
+ // segmentId,
+ // });
+ }
+
+ undo(): void {
+ // TODO ...
+ const actionData = this.getOldActionData();
+ const document = this.getDocument();
+ const { text, len, segmentId } = actionData;
+ // InsertTextApply(document, text, textLength, {
+ // cursorStart,
+ // isStartBack,
+ // segmentId,
+ // });
+ }
+
+ validate(): boolean {
+ return false;
+ }
+}
+
+CommandManager.register(DeleteTextAction.Name, DeleteTextAction);
diff --git a/packages/core/src/Docs/Action/DeleteTextAction.ts b/packages/core/src/Docs/Action/DeleteTextAction.ts
deleted file mode 100644
index f3922c98a5..0000000000
--- a/packages/core/src/Docs/Action/DeleteTextAction.ts
+++ /dev/null
@@ -1,84 +0,0 @@
-import { DocActionBase, IDocActionData } from '../../Command/DocActionBase';
-import { ActionObservers, CommandManager, CommandUnit } from '../../Command';
-import { DeleteTextApply } from '../Apply/DeleteTextApply';
-import { IInsertTextActionData } from './InsertTextAction';
-import { InsertTextApply } from '../Apply/InsertTextApply';
-import { ITextSelectionRangeParam } from '../../Interfaces/ISelectionData';
-import { DOC_ACTION_NAMES } from '../../Const/DOC_ACTION_NAMES';
-import { IElement } from '../../Interfaces/IDocumentData';
-
-export interface IDeleteTextActionData
- extends IDocActionData,
- ITextSelectionRangeParam {
- text: string | IElement[];
- textLength: number;
-}
-
-export class DeleteTextAction extends DocActionBase<
- IDeleteTextActionData,
- IInsertTextActionData
-> {
- static Name = 'DeleteTextAction';
-
- constructor(
- actionData: IDeleteTextActionData,
- commandUnit: CommandUnit,
- observers: ActionObservers
- ) {
- super(actionData, commandUnit, observers);
- this._doActionData = { ...actionData };
- this.do();
- const { text, cursorStart, isStartBack, textLength, segmentId } = actionData;
- this._oldActionData = {
- actionName: DOC_ACTION_NAMES.INSERT_TEXT_ACTION_NAME,
- text,
- textLength,
- cursorStart,
- isStartBack,
- segmentId,
- };
- }
-
- redo(): void {
- this.do();
- }
-
- do(): void {
- const actionData = this.getDoActionData();
- const document = this.getDocument();
- const {
- cursorStart,
- cursorEnd,
- isStartBack,
- isEndBack,
- isCollapse,
- segmentId,
- } = actionData;
- DeleteTextApply(document, {
- cursorStart,
- cursorEnd,
- isStartBack,
- isEndBack,
- isCollapse,
- segmentId,
- });
- }
-
- undo(): void {
- // TODO ...
- const actionData = this.getOldActionData();
- const document = this.getDocument();
- const { text, textLength, cursorStart, isStartBack, segmentId } = actionData;
- InsertTextApply(document, text, textLength, {
- cursorStart,
- isStartBack,
- segmentId,
- });
- }
-
- validate(): boolean {
- return false;
- }
-}
-
-CommandManager.register(DeleteTextAction.Name, DeleteTextAction);
diff --git a/packages/core/src/Docs/Action/InsertTextAction.ts b/packages/core/src/Docs/Action/InsertAction.ts
similarity index 55%
rename from packages/core/src/Docs/Action/InsertTextAction.ts
rename to packages/core/src/Docs/Action/InsertAction.ts
index b527a5fc6f..c5631143fe 100644
--- a/packages/core/src/Docs/Action/InsertTextAction.ts
+++ b/packages/core/src/Docs/Action/InsertAction.ts
@@ -4,18 +4,13 @@ import {
ActionType,
CommandManager,
CommandUnit,
+ CommonParameter,
} from '../../Command';
-import { IElement } from '../../Interfaces/IDocumentData';
-import { InsertTextApply } from '../Apply/InsertTextApply';
-import { IDeleteTextActionData } from './DeleteTextAction';
-import { DeleteTextApply } from '../Apply/DeleteTextApply';
-import { DOC_ACTION_NAMES } from '../../Const/DOC_ACTION_NAMES';
+import { IDeleteTextActionData } from './DeleteAction';
export interface IInsertTextActionData extends IDocActionData {
- text: string | IElement[];
- textLength: number;
- cursorStart: number;
- isStartBack: boolean;
+ text: string;
+ len: number;
segmentId?: string; //The ID of the header, footer or footnote the location is in. An empty segment ID signifies the document's body.
}
@@ -28,34 +23,35 @@ export class InsertTextAction extends DocActionBase<
constructor(
actionData: IInsertTextActionData,
commandUnit: CommandUnit,
- observers: ActionObservers
+ observers: ActionObservers,
+ commonParameter: CommonParameter
) {
super(actionData, commandUnit, observers);
this._doActionData = { ...actionData };
- this.do();
- const { cursorStart, isStartBack, text, textLength } = actionData;
- this._oldActionData = {
- ...actionData,
- actionName: DOC_ACTION_NAMES.DELETE_TEXT_ACTION_NAME,
- cursorEnd: cursorStart + textLength,
- isEndBack: isStartBack,
- isCollapse: true,
- };
+ this.do(commonParameter);
+ const { segmentId, text, len } = actionData;
+ // this._oldActionData = {
+ // ...actionData,
+ // actionName: DOC_ACTION_NAMES.DELETE_TEXT_ACTION_NAME,
+ // cursorEnd: cursorStart + textLength,
+ // isEndBack: isStartBack,
+ // isCollapse: true,
+ // };
}
- redo(): void {
- this.do();
+ redo(commonParameter: CommonParameter): void {
+ this.do(commonParameter);
}
- do(): void {
+ do(commonParameter: CommonParameter): void {
const actionData = this.getDoActionData();
const document = this.getDocument();
- const { text, cursorStart, isStartBack, textLength, segmentId } = actionData;
- InsertTextApply(document, text, textLength, {
- cursorStart,
- isStartBack,
- segmentId,
- });
+ const { text, len, segmentId } = actionData;
+ // InsertTextApply(document, text, textLength, {
+ // cursorStart,
+ // isStartBack,
+ // segmentId,
+ // });
this._observers.notifyObservers({
type: ActionType.REDO,
@@ -64,11 +60,11 @@ export class InsertTextAction extends DocActionBase<
});
}
- undo(): void {
+ undo(commonParameter: CommonParameter): void {
const actionData = this.getOldActionData();
const document = this.getDocument();
// const { length, start, segmentId } = actionData;
- DeleteTextApply(document, { ...actionData });
+ // DeleteTextApply(document, { ...actionData });
this._observers.notifyObservers({
type: ActionType.UNDO,
diff --git a/packages/core/src/Docs/Action/UpdateParagraphAction.ts b/packages/core/src/Docs/Action/UpdateAttributeAction.ts
similarity index 100%
rename from packages/core/src/Docs/Action/UpdateParagraphAction.ts
rename to packages/core/src/Docs/Action/UpdateAttributeAction.ts
diff --git a/packages/core/src/Docs/Action/index.ts b/packages/core/src/Docs/Action/index.ts
index 2979aaa368..a0433700f7 100644
--- a/packages/core/src/Docs/Action/index.ts
+++ b/packages/core/src/Docs/Action/index.ts
@@ -1,4 +1,4 @@
-export * from './InsertTextAction';
-export * from './DeleteTextAction';
+export * from './InsertAction';
+export * from './DeleteAction';
export * from './UpdateDocumentAction';
-export * from './UpdateParagraphAction';
+export * from './UpdateAttributeAction';