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.

move command manager

jerryec7b3019bab9
diff --git a/packages/core/src/Basics/ContextBase.ts b/packages/core/src/Basics/ContextBase.ts
index cb464fe9b3..a22b14206a 100644
--- a/packages/core/src/Basics/ContextBase.ts
+++ b/packages/core/src/Basics/ContextBase.ts
@@ -1,10 +1,10 @@
import { CommandManager, UndoManager } from '../Command';
import { ObservableHooks, ObserverManager } from '../Observer';
import { HooksManager } from '../Observer/HooksManager';
+import { Univer } from './Univer';
import { PluginManager } from '../Plugin';
import { Nullable } from '../Shared';
import { Environment } from './Environment';
-import { Univer } from './Univer';
/**
* Core context, mount important instances, managers
@@ -12,29 +12,31 @@ import { Univer } from './Univer';
export abstract class ContextBase {
protected _commandManager: CommandManager;
+ protected _pluginManager: PluginManager;
+
protected _environment: Environment;
protected _hooksManager: HooksManager;
protected _undoManager: UndoManager;
- protected _pluginManager: PluginManager;
-
protected _observerManager: ObserverManager;
protected _univer: Univer;
constructor() {
- this._undoManager = new UndoManager();
- this._commandManager = new CommandManager(this);
- this._environment = new Environment();
this._hooksManager = new HooksManager();
this._pluginManager = new PluginManager(this);
+ this._environment = new Environment();
this._observerManager = new ObserverManager();
+ this._initialize();
}
onUniver(univer: Univer): void {
+ const glboalContext = univer.getGlobalContext();
this._univer = univer;
+ this._undoManager = glboalContext.getUndoManager();
+ this._commandManager = glboalContext.getCommandManager();
}
getHook<T>(path: string): Nullable<ObservableHooks<T>> {
@@ -65,5 +67,10 @@ export abstract class ContextBase {
return this._univer;
}
+ protected _initialize(): void {
+ this._undoManager = new UndoManager();
+ this._commandManager = new CommandManager(this);
+ }
+
protected abstract _setObserver(): void;
}
diff --git a/packages/core/src/Basics/SheetContext.ts b/packages/core/src/Basics/SheetContext.ts
index 49353fb2a8..18f9c6e44f 100644
--- a/packages/core/src/Basics/SheetContext.ts
+++ b/packages/core/src/Basics/SheetContext.ts
@@ -34,7 +34,10 @@ export class SheetContext extends ContextBase {
protected _setObserver(): void {
const manager = this.getObserverManager();
-
new WorkBookObserverImpl().install(manager);
}
+
+ protected _initialize(): void {
+ // EMPTY Context Initialize
+ }
}
diff --git a/packages/core/src/Sheets/Domain/Workbook.ts b/packages/core/src/Sheets/Domain/Workbook.ts
index 37943d7683..fb2595e181 100644
--- a/packages/core/src/Sheets/Domain/Workbook.ts
+++ b/packages/core/src/Sheets/Domain/Workbook.ts
@@ -65,8 +65,6 @@ export class Workbook {
private _context: SheetContext;
- private _commandManager: CommandManager;
-
private _namedRange: NamedRange;
constructor(workbookData: Partial<IWorkbookConfig> = {}, context: SheetContext) {
@@ -80,7 +78,6 @@ export class Workbook {
this._unitId = this._config.id;
this._styles = new Styles(styles);
this._worksheets = new Map<string, Worksheet>();
- this._commandManager = context.getCommandManager();
// this._formatManage = new FormatManager();
this._getDefaultWorkSheet();
@@ -150,10 +147,6 @@ export class Workbook {
this._context = context;
}
- setCommandManager(commandManager: CommandManager): void {
- this._commandManager = commandManager;
- }
-
insertSheet(): Nullable<string>;
insertSheet(index: number): Nullable<string>;
insertSheet(name: string): Nullable<string>;
@@ -163,8 +156,8 @@ export class Workbook {
insertSheet(index: number, data: Partial<IWorksheetConfig>): Nullable<string>;
insertSheet(index: number, sheet: Worksheet): Nullable<string>;
insertSheet(...argument: any[]): Nullable<string> {
- const { _context, _commandManager } = this;
-
+ const { _context } = this;
+ const commandManager = this.getCommandManager();
const before = _context.getContextObserver('onBeforeInsertSheetObservable');
const after = _context.getContextObserver('onAfterInsertSheetObservable');
@@ -180,7 +173,7 @@ export class Workbook {
index,
sheetId: worksheetConfig.id,
});
- _commandManager.invoke(
+ commandManager.invoke(
new Command(
{
WorkBookUnit: _context.getWorkBook(),
@@ -211,7 +204,7 @@ export class Workbook {
index,
sheetId: worksheetConfig.id,
});
- _commandManager.invoke(
+ commandManager.invoke(
new Command(
{
WorkBookUnit: _context.getWorkBook(),
@@ -243,7 +236,7 @@ export class Workbook {
index,
sheetId: worksheetConfig.id,
});
- _commandManager.invoke(
+ commandManager.invoke(
new Command(
{
WorkBookUnit: _context.getWorkBook(),
@@ -276,7 +269,7 @@ export class Workbook {
index,
sheetId: worksheetConfig.id,
});
- _commandManager.invoke(
+ commandManager.invoke(
new Command(
{
WorkBookUnit: _context.getWorkBook(),
@@ -304,7 +297,7 @@ export class Workbook {
index,
sheetId: worksheetConfig.id,
});
- _commandManager.invoke(
+ commandManager.invoke(
new Command(
{
WorkBookUnit: _context.getWorkBook(),
@@ -339,7 +332,7 @@ export class Workbook {
index,
sheetId: worksheetConfig.id,
});
- _commandManager.invoke(
+ commandManager.invoke(
new Command(
{
WorkBookUnit: _context.getWorkBook(),
@@ -369,7 +362,7 @@ export class Workbook {
index,
sheetId: worksheetConfig.id,
});
- _commandManager.invoke(
+ commandManager.invoke(
new Command(
{
WorkBookUnit: _context.getWorkBook(),
@@ -395,7 +388,7 @@ export class Workbook {
index,
sheetId: worksheetConfig.id,
});
- _commandManager.invoke(
+ commandManager.invoke(
new Command(
{
WorkBookUnit: _context.getWorkBook(),
@@ -582,7 +575,8 @@ export class Workbook {
// const exclude = _sheetOrder.filter((currentId) => currentId !== sheetId);
// exclude.splice(order, 0, sheetId);
// this._sheetOrder = exclude;
- const { _context, _commandManager } = this;
+ const { _context } = this;
+ const commandManager = this.getCommandManager();
const observer = _context.getContextObserver('onSheetOrderObservable');
const config: ISetSheetOrderActionData = {
actionName: SetSheetOrderAction.NAME,
@@ -595,7 +589,7 @@ export class Workbook {
},
config
);
- _commandManager.invoke(command);
+ commandManager.invoke(command);
observer.notifyObservers();
}
@@ -617,8 +611,9 @@ export class Workbook {
}
removeSheetBySheetId(sheetId: string): void {
- const { _commandManager, _config } = this;
+ const { _config } = this;
const { sheetOrder } = _config;
+ const commandManager = this.getCommandManager();
const sheet = this.getSheetBySheetId(sheetId);
if (sheetOrder.length > 1 && sheet != null) {
@@ -632,7 +627,7 @@ export class Workbook {
before.notifyObservers({
index,
});
- _commandManager.invoke(
+ commandManager.invoke(
new Command(
{
WorkBookUnit: this,
@@ -700,7 +695,7 @@ export class Workbook {
// }
getCommandManager(): CommandManager {
- return this._commandManager;
+ return this._context.getCommandManager();
}
getPluginMeta<T>(name: string): T {
diff --git a/packages/core/src/Sheets/Domain/Worksheet.ts b/packages/core/src/Sheets/Domain/Worksheet.ts
index 5a448f656a..9fef54fcbc 100644
--- a/packages/core/src/Sheets/Domain/Worksheet.ts
+++ b/packages/core/src/Sheets/Domain/Worksheet.ts
@@ -75,8 +75,6 @@ import { Command, CommandManager, ISheetActionData } from '../../Command';
* @beta
*/
export class Worksheet {
- protected _commandManager: CommandManager;
-
protected _selection: Selection;
protected _context: SheetContext;
@@ -165,7 +163,6 @@ export class Worksheet {
// this._borderStyles = new BorderStyles(this);
this._cellData = new ObjectMatrix<ICellData>(cellData);
// this._protection = new Protection();
- this._commandManager = this._context.getCommandManager();
// this._selection.setWorkSheet(this);
this._rowManager = new RowManager(this, rowData);
this._columnManager = new ColumnManager(this, columnData);
@@ -194,7 +191,8 @@ export class Worksheet {
if (this._config.status === BooleanNumber.TRUE) {
return this;
}
- const { _context, _commandManager } = this;
+ const { _context } = this;
+ const _commandManager = this.getCommandManager();
const before = _context.getContextObserver(
'onBeforeChangeActiveSheetObservable'
);
@@ -268,7 +266,8 @@ export class Worksheet {
* @alpha
*/
setName(name: string): Worksheet {
- const { _context, _commandManager, _sheetId } = this;
+ const { _context, _sheetId } = this;
+ const _commandManager = this.getCommandManager();
const before = _context.getContextObserver(
'onBeforeChangeSheetNameObservable'
);
@@ -343,7 +342,7 @@ export class Worksheet {
* @returns
*/
getCommandManager(): CommandManager {
- return this._commandManager;
+ return this._context.getCommandManager();
}
/**
@@ -461,7 +460,8 @@ export class Worksheet {
* @returns WorkSheet Instance
*/
setStatus(status: BooleanNumber): Worksheet {
- const { _context, _commandManager, _sheetId } = this;
+ const { _context, _sheetId } = this;
+ const _commandManager = this.getCommandManager();
const configure: ISetWorkSheetStatusActionData = {
actionName: SetWorkSheetStatusAction.NAME,
sheetId: _sheetId,
@@ -490,7 +490,8 @@ export class Worksheet {
* @param zoomRatio
*/
setZoomRatio(zoomRatio: number): void {
- const { _context, _sheetId, _commandManager } = this;
+ const { _context, _sheetId } = this;
+ const _commandManager = this.getCommandManager();
const zoomRation = {
actionName: SetZoomRatioAction.NAME,
zoom: zoomRatio,
@@ -579,7 +580,8 @@ export class Worksheet {
numRows = argument[1];
}
- const { _context, _commandManager, _sheetId } = this;
+ const { _context, _sheetId } = this;
+ const _commandManager = this.getCommandManager();
const insertRowData: IInsertRowDataActionData = {
actionName: InsertRowDataAction.NAME,
sheetId: _sheetId,
@@ -628,7 +630,8 @@ export class Worksheet {
numRows = argument[1];
}
- const { _context, _commandManager, _sheetId } = this;
+ const { _context, _sheetId } = this;
+ const _commandManager = this.getCommandManager();
const insertRowData = {
actionName: InsertRowDataAction.NAME,
sheetId: _sheetId,
@@ -682,7 +685,8 @@ export class Worksheet {
rowIndex = 0;
}
- const { _context, _commandManager, _sheetId } = this;
+ const { _context, _sheetId } = this;
+ const _commandManager = this.getCommandManager();
const insertRowData = {
actionName: InsertRowDataAction.NAME,
sheetId: _sheetId,
@@ -731,7 +735,8 @@ export class Worksheet {
numColumns = argument[1];
}
- const { _context, _commandManager, _sheetId } = this;
+ const { _context, _sheetId } = this;
+ const _commandManager = this.getCommandManager();
const columnData = new ObjectMatrix<ICellData>();
this._cellData.forEach((index) => {
for (let i = columnIndex; i < columnIndex + numColumns; i++) {
@@ -791,7 +796,8 @@ export class Worksheet {
columnIndex = 0;
}
- const { _context, _commandManager, _sheetId } = this;
+ const { _context, _sheetId } = this;
+ const _commandManager = this.getCommandManager();
const columnData = new ObjectMatrix<ICellData>();
this._cellData.forEach((index) => {
for (let i = columnIndex; i < columnIndex + numColumns; i++) {
@@ -847,7 +853,8 @@ export class Worksheet {
numColumns = argument[1];
}
- const { _context, _commandManager, _sheetId } = this;
+ const { _context, _sheetId } = this;
+ const _commandManager = this.getCommandManager();
const columnData = new ObjectMatrix<ICellData>();
this._cellData.forEach((index) => {
for (let i = columnIndex; i < columnIndex + numColumns; i++) {
@@ -890,8 +897,8 @@ export class Worksheet {
*/
clear(options: IOptionData): Worksheet;
clear(...argument: any): Worksheet {
- const { _context, _commandManager } = this;
-
+ const { _context } = this;
+ const _commandManager = this.getCommandManager();
// collect all cell as a Range
const _range = {
startRow: 0,
@@ -932,7 +939,8 @@ export class Worksheet {
* @returns WorkSheet This sheet, for chaining.
*/
setTabColor(color: Nullable<string>): Worksheet {
- const { _context, _commandManager } = this;
+ const { _context } = this;
+ const _commandManager = this.getCommandManager();
const observer = _context.getContextObserver(
'onSheetTabColorChangeObservable'
);
@@ -957,7 +965,8 @@ export class Worksheet {
* @returns Sheet — The current sheet.
*/
hideSheet(): Worksheet {
- const { _context, _commandManager } = this;
+ const { _context } = this;
+ const _commandManager = this.getCommandManager();
const _workbook = _context.getWorkBook();
if (!this._config.hidden) {
const observer = _context.getContextObserver('onHideSheetObservable');
@@ -974,7 +983,6 @@ export class Worksheet {
);
_commandManager.invoke(command);
observer.notifyObservers({ sheet: this });
-
const index = _workbook.getSheetIndex(this);
if (index) {
_workbook.activateSheetByIndex(index);
@@ -991,7 +999,8 @@ export class Worksheet {
if (!this._config.hidden) {
return this;
}
- const { _context, _commandManager } = this;
+ const { _context } = this;
+ const _commandManager = this.getCommandManager();
const setHidden: ISetWorkSheetHideActionData = {
hidden: BooleanNumber.FALSE,
sheetId: this._sheetId,
@@ -1010,16 +1019,6 @@ export class Worksheet {
return this;
}
- /**
- * Sets commandManager
- * @param commandManager Command Manager
- * @returns commandManager
- */
- setCommandManager(commandManager: CommandManager): Worksheet {
- this._commandManager = commandManager;
- return this;
- }
-
/**
* restore the active selection region for this sheet.
* @returns Range — the newly active range
@@ -1161,7 +1160,8 @@ export class Worksheet {
* @returns WorkSheet Instance
*/
deleteColumn(columnPosition: number): Worksheet {
- const { _context, _commandManager, _sheetId } = this;
+ const { _context, _sheetId } = this;
+ const _commandManager = this.getCommandManager();
const deleteColumnData: IRemoveColumnDataAction = {
actionName: RemoveColumnDataAction.NAME,
sheetId: _sheetId,
@@ -1193,7 +1193,8 @@ export class Worksheet {
* @returns WorkSheet Instance
*/
deleteColumns(columnPosition: number, howMany: number): Worksheet {
- const { _context, _commandManager, _sheetId } = this;
+ const { _context, _sheetId } = this;
+ const _commandManager = this.getCommandManager();
const deleteColumnData: IRemoveColumnDataAction = {
actionName: RemoveColumnDataAction.NAME,
sheetId: _sheetId,
@@ -1224,7 +1225,8 @@ export class Worksheet {
* @returns WorkSheet Instance
*/
deleteRow(rowPosition: number): Worksheet {
- const { _context, _commandManager, _sheetId } = this;
+ const { _context, _sheetId } = this;
+ const _commandManager = this.getCommandManager();
const dataRowDelete: IRemoveRowDataActionData = {
actionName: RemoveRowDataAction.NAME,
sheetId: _sheetId,
@@ -1257,7 +1259,8 @@ export class Worksheet {
* @returns WorkSheet Instance
*/
deleteRows(rowPosition: number, howMany: number): Worksheet {
- const { _context, _commandManager, _sheetId } = this;
+ const { _context, _sheetId } = this;
+ const _commandManager = this.getCommandManager();
const dataRowDelete: IRemoveRowDataActionData = {
actionName: RemoveRowDataAction.NAME,
sheetId: _sheetId,
@@ -1317,8 +1320,9 @@ export class Worksheet {
style: IBorderStyleData,
directions: Direction[]
) {
- const { _commandManager, _sheetId } = this;
+ const { _sheetId } = this;
+ const _commandManager = this.getCommandManager();
const context = this.getContext();
const matrix = this.getCellMatrix();
const workbook = context.getWorkBook();
@@ -1654,7 +1658,8 @@ export class Worksheet {
const range = row.getRangeData();
const index = range.startRow;
const count = range.endRow - range.startRow + 1;
- const { _context, _commandManager, _sheetId } = this;
+ const { _context, _sheetId } = this;
+ const _commandManager = this.getCommandManager();
const hideRow: ISetRowHideActionData = {
actionName: SetRowHideAction.NAME,
sheetId: _sheetId,
@@ -1690,7 +1695,8 @@ export class Worksheet {
if (argument[1]) {
count = argument[1];
}
- const { _context, _commandManager, _sheetId } = this;
+ const { _context, _sheetId } = this;
+ const _commandManager = this.getCommandManager();
const hideRow: ISetRowHideActionData = {
actionName: SetRowHideAction.NAME,
sheetId: _sheetId,
@@ -1716,7 +1722,8 @@ export class Worksheet {
const range = column.getRangeData();
const index = range.startColumn;
const count = range.endColumn - range.startColumn + 1;
- const { _context, _commandManager, _sheetId } = this;
+ const { _context, _sheetId } = this;
+ const _commandManager = this.getCommandManager();
const hideColumn: ISetColumnHideActionData = {
actionName: SetColumnHideAction.NAME,
sheetId: _sheetId,
@@ -1752,7 +1759,8 @@ export class Worksheet {
if (argument[1]) {
count = argument[1];
}
- const { _context, _commandManager, _sheetId } = this;
+ const { _context, _sheetId } = this;
+ const _commandManager = this.getCommandManager();
const hideColumn: ISetColumnHideActionData = {
actionName: SetColumnHideAction.NAME,
sheetId: _sheetId,
@@ -1778,7 +1786,8 @@ export class Worksheet {
const range = row.getRangeData();
const index = range.startRow;
const count = range.endRow - range.startRow + 1;
- const { _context, _commandManager, _sheetId } = this;
+ const { _context, _sheetId } = this;
+ const _commandManager = this.getCommandManager();
const unhideRow: ISetRowShowActionData = {
actionName: SetRowShowAction.NAME,
rowCount: count,
@@ -1804,7 +1813,8 @@ export class Worksheet {
const range = column.getRangeData();
const index = range.startColumn;
const count = range.endColumn - range.startColumn + 1;
- const { _context, _commandManager, _sheetId } = this;
+ const { _context, _sheetId } = this;
+ const _commandManager = this.getCommandManager();
const unhideColumn: ISetColumnShowActionData = {
actionName: SetColumnShowAction.NAME,
sheetId: _sheetId,
@@ -1840,7 +1850,8 @@ export class Worksheet {
if (argument[1]) {
count = argument[1];
}
- const { _context, _commandManager, _sheetId } = this;
+ const { _context, _sheetId } = this;
+ const _commandManager = this.getCommandManager();
const showColumn: ISetColumnShowActionData = {
actionName: SetColumnShowAction.NAME,
sheetId: _sheetId,
@@ -1876,7 +1887,8 @@ export class Worksheet {
if (argument[1]) {
count = argument[1];
}
- const { _context, _commandManager, _sheetId } = this;
+ const { _context, _sheetId } = this;
+ const _commandManager = this.getCommandManager();
const showRow: ISetRowShowActionData = {
actionName: SetRowShowAction.NAME,
sheetId: _sheetId,
@@ -2004,7 +2016,8 @@ export class Worksheet {
* @returns WorkSheet Instance
*/
setHiddenGridlines(hideGridlines: boolean): Worksheet {
- const { _context, _commandManager, _sheetId } = this;
+ const { _context, _sheetId } = this;
+ const _commandManager = this.getCommandManager();
const configure = {
actionName: SetHiddenGridlinesAction.NAME,
hideGridlines,
@@ -2056,7 +2069,8 @@ export class Worksheet {
* @returns WorkSheet Instance
*/
setRightToLeft(rightToLeft: BooleanNumber): Worksheet {
- const { _context, _commandManager, _sheetId } = this;
+ const { _context, _sheetId } = this;
+ const _commandManager = this.getCommandManager();
const configure = {
actionName: SetRightToLeftAction.NAME,
rightToLeft,
@@ -2069,7 +2083,6 @@ export class Worksheet {
configure
);
_commandManager.invoke(command);
-
return this;
}
diff --git a/packages/core/src/index.ts b/packages/core/src/index.ts
index f2a9486206..baffa42823 100644
--- a/packages/core/src/index.ts
+++ b/packages/core/src/index.ts
@@ -27,5 +27,3 @@ export * from './Observer';
export * from './Plugin';
export * from './Server';
export * from './Shared';
-
-