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.

fix: fix an issue where disposeUnit would clear all event listeners (#6508)

wpxp123456e0b514c811a2
diff --git a/examples/package.json b/examples/package.json
index 1735f6913c..5b80d093aa 100644
--- a/examples/package.json
+++ b/examples/package.json
@@ -81,7 +81,8 @@
"monaco-editor": "0.55.1",
"react": "19.2.3",
"react-dom": "19.2.3",
- "react-mosaic-component": "^6.1.1"
+ "react-mosaic-component": "^6.1.1",
+ "rxjs": "^7.8.2"
},
"devDependencies": {
"@types/fs-extra": "^11.0.4",
diff --git a/examples/src/sheets/custom/custom-register-event.ts b/examples/src/sheets/custom/custom-register-event.ts
index 38e222ae3f..f51928133c 100644
--- a/examples/src/sheets/custom/custom-register-event.ts
+++ b/examples/src/sheets/custom/custom-register-event.ts
@@ -14,15 +14,17 @@
* limitations under the License.
*/
-import type { Univer } from '@univerjs/core';
+import type { Nullable, Univer } from '@univerjs/core';
import type { FUniver, IEventBase } from '@univerjs/core/facade';
+import type { IRender } from '@univerjs/engine-render';
import type { IRemoveColByRangeCommandParams } from '@univerjs/sheets';
import type { FWorkbook, FWorksheet } from '@univerjs/sheets/facade';
-import { CanceledError, ICommandService } from '@univerjs/core';
+import { CanceledError, DisposableCollection, ICommandService, LifecycleService, LifecycleStages, UniverInstanceType } from '@univerjs/core';
import { IRenderManagerService } from '@univerjs/engine-render';
import { RemoveColByRangeCommand } from '@univerjs/sheets';
import { SHEET_VIEW_KEY } from '@univerjs/sheets-ui';
import { IContextMenuService } from '@univerjs/ui';
+import { combineLatest } from 'rxjs';
interface IMainRightClickEventParams extends IEventBase {
event: MouseEvent;
@@ -51,9 +53,10 @@ interface ICustomEventParamConfig {
}
export function customRegisterEvent(univer: Univer, univerAPI: FUniver) {
+ registerMainRightClickEvent(univer, univerAPI);
+
univerAPI.addEvent(univerAPI.Event.LifeCycleChanged, ({ stage }) => {
if (stage === univerAPI.Enum.LifecycleStages.Steady) {
- registerMainRightClickEvent(univer, univerAPI);
registerRemoveColumnEvent(univer, univerAPI);
registerBeforeRemoveColumnEvent(univer, univerAPI);
@@ -91,68 +94,90 @@ export function customRegisterEvent(univer: Univer, univerAPI: FUniver) {
}
function registerMainRightClickEvent(univer: Univer, univerAPI: FUniver) {
- const fWorkbook = univerAPI.getActiveWorkbook();
- if (!fWorkbook) return;
-
- const fWorksheet = fWorkbook.getActiveSheet();
- if (!fWorksheet) return;
-
const injector = univer.__getInjector();
const renderManagerService = injector.get(IRenderManagerService);
- const render = renderManagerService.getRenderById(fWorkbook.getId());
- if (!render) return;
-
- const { components } = render;
- const mainComponent = components.get(SHEET_VIEW_KEY.MAIN);
- if (!mainComponent) return;
-
+ const lifeCycleService = injector.get(LifecycleService);
const contextMenuService = injector.get(IContextMenuService);
- univerAPI.registerEventHandler(
- 'MainRightClickEvent',
- () => mainComponent.onPointerDown$.subscribeEvent((event) => {
- if (event.button !== 2) return;
-
- const activeRange = fWorksheet.getActiveRange();
- const eventParams: IMainRightClickEventParams = {
- event,
- row: activeRange?.getRow() ?? 0,
- column: activeRange?.getColumn() ?? 0,
- };
-
- univerAPI.fireEvent('MainRightClickEvent', eventParams);
-
- if (eventParams.cancel) {
- requestAnimationFrame(() => {
- contextMenuService.hideContextMenu();
- });
- }
- })
- );
+ let sheetRenderUnit: Nullable<IRender>;
+ const combined$ = combineLatest([
+ renderManagerService.created$,
+ lifeCycleService.lifecycle$,
+ ]);
+ const disposable = new DisposableCollection();
+
+ univerAPI.disposeWithMe(combined$.subscribe(([created, lifecycle]) => {
+ if (created.type === UniverInstanceType.UNIVER_SHEET) {
+ sheetRenderUnit = created;
+ }
+ if (lifecycle <= LifecycleStages.Rendered) return;
+ if (!sheetRenderUnit) return;
+
+ const { components } = sheetRenderUnit;
+ const mainComponent = components.get(SHEET_VIEW_KEY.MAIN);
+ if (!mainComponent) return;
+
+ const fWorkbook = univerAPI.getWorkbook(sheetRenderUnit.unitId);
+ if (!fWorkbook) return;
+
+ const fWorksheet = fWorkbook.getActiveSheet();
+ if (!fWorksheet) return;
+
+ disposable.dispose();
+
+ disposable.add(
+ univerAPI.registerEventHandler(
+ 'MainRightClickEvent',
+ () => mainComponent.onPointerDown$.subscribeEvent((event) => {
+ if (event.button !== 2) return;
+
+ const activeRange = fWorksheet.getActiveRange();
+ const eventParams: IMainRightClickEventParams = {
+ event,
+ row: activeRange?.getRow() ?? 0,
+ column: activeRange?.getColumn() ?? 0,
+ };
+
+ univerAPI.fireEvent('MainRightClickEvent', eventParams);
+
+ // If the event is canceled, do not show the context menu
+ if (eventParams.cancel) {
+ requestAnimationFrame(() => {
+ contextMenuService.hideContextMenu();
+ });
+ }
+ })
+ )
+ );
+
+ univerAPI.disposeWithMe(disposable);
+ }));
}
function registerRemoveColumnEvent(univer: Univer, univerAPI: FUniver) {
const injector = univer.__getInjector();
const commandService = injector.get(ICommandService);
- univerAPI.registerEventHandler(
- 'RemoveColumnEvent',
- () => commandService.onCommandExecuted((commandInfo) => {
- if (commandInfo.id !== RemoveColByRangeCommand.id) return;
-
- const target = univerAPI.getCommandSheetTarget(commandInfo);
- if (!target) return;
-
- const { range } = commandInfo.params as IRemoveColByRangeCommandParams;
- const eventParams: IRemoveColumnEventParams = {
- workbook: target.workbook,
- worksheet: target.worksheet,
- startColumn: range.startColumn,
- endColumn: range.endColumn,
- };
-
- univerAPI.fireEvent('RemoveColumnEvent', eventParams);
- })
+ univerAPI.disposeWithMe(
+ univerAPI.registerEventHandler(
+ 'RemoveColumnEvent',
+ () => commandService.onCommandExecuted((commandInfo) => {
+ if (commandInfo.id !== RemoveColByRangeCommand.id) return;
+
+ const target = univerAPI.getCommandSheetTarget(commandInfo);
+ if (!target) return;
+
+ const { range } = commandInfo.params as IRemoveColByRangeCommandParams;
+ const eventParams: IRemoveColumnEventParams = {
+ workbook: target.workbook,
+ worksheet: target.worksheet,
+ startColumn: range.startColumn,
+ endColumn: range.endColumn,
+ };
+
+ univerAPI.fireEvent('RemoveColumnEvent', eventParams);
+ })
+ )
);
}
@@ -160,28 +185,30 @@ function registerBeforeRemoveColumnEvent(univer: Univer, univerAPI: FUniver) {
const injector = univer.__getInjector();
const commandService = injector.get(ICommandService);
- univerAPI.registerEventHandler(
- 'BeforeRemoveColumnEvent',
- () => commandService.beforeCommandExecuted((commandInfo) => {
- if (commandInfo.id !== RemoveColByRangeCommand.id) return;
+ univerAPI.disposeWithMe(
+ univerAPI.registerEventHandler(
+ 'BeforeRemoveColumnEvent',
+ () => commandService.beforeCommandExecuted((commandInfo) => {
+ if (commandInfo.id !== RemoveColByRangeCommand.id) return;
- const target = univerAPI.getCommandSheetTarget(commandInfo);
- if (!target) return;
+ const target = univerAPI.getCommandSheetTarget(commandInfo);
+ if (!target) return;
- const { range } = commandInfo.params as IRemoveColByRangeCommandParams;
- const eventParams: IBeforeRemoveColumnEventParams = {
- workbook: target.workbook,
- worksheet: target.worksheet,
- startColumn: range.startColumn,
- endColumn: range.endColumn,
- };
+ const { range } = commandInfo.params as IRemoveColByRangeCommandParams;
+ const eventParams: IBeforeRemoveColumnEventParams = {
+ workbook: target.workbook,
+ worksheet: target.worksheet,
+ startColumn: range.startColumn,
+ endColumn: range.endColumn,
+ };
- univerAPI.fireEvent('BeforeRemoveColumnEvent', eventParams);
+ univerAPI.fireEvent('BeforeRemoveColumnEvent', eventParams);
- if (eventParams.cancel) {
- throw new CanceledError();
- }
- })
+ if (eventParams.cancel) {
+ throw new CanceledError();
+ }
+ })
+ )
);
}
diff --git a/packages/core/src/facade/f-event-registry.ts b/packages/core/src/facade/f-event-registry.ts
index 1c69b6de82..d8f3212bee 100644
--- a/packages/core/src/facade/f-event-registry.ts
+++ b/packages/core/src/facade/f-event-registry.ts
@@ -32,17 +32,6 @@ export class FEventRegistry {
return this._eventRegistry.get(event)!;
}
- dispose(): void {
- this._eventRegistry.clear();
- this._eventHandlerMap.clear();
- this._eventHandlerRegisted.forEach((map) => {
- map.forEach((disposable) => disposable.dispose());
- map.clear();
- });
-
- this._eventHandlerRegisted.clear();
- }
-
registerEventHandler(event: string, handler: () => IDisposable | Subscription): IDisposable {
const current = this._eventHandlerMap.get(event);
if (current) {
@@ -78,7 +67,7 @@ export class FEventRegistry {
const handlers = this._eventHandlerMap.get(event);
if (!handlers) return;
- if (!current) {
+ if (!current || current.size === 0) {
current = new Map();
this._eventHandlerRegisted.set(event, current);
handlers?.forEach((handler) => {
diff --git a/packages/core/src/facade/f-univer.ts b/packages/core/src/facade/f-univer.ts
index 9c965ae5b4..dec2244c88 100644
--- a/packages/core/src/facade/f-univer.ts
+++ b/packages/core/src/facade/f-univer.ts
@@ -108,14 +108,16 @@ export class FUniver extends Disposable {
) {
super();
- this.registerEventHandler(
- this.Event.LifeCycleChanged,
- () =>
- toDisposable(
- this._lifecycleService.lifecycle$.subscribe((stage) => {
- this.fireEvent(this.Event.LifeCycleChanged, { stage });
- })
- )
+ this.disposeWithMe(
+ this.registerEventHandler(
+ this.Event.LifeCycleChanged,
+ () =>
+ toDisposable(
+ this._lifecycleService.lifecycle$.subscribe((stage) => {
+ this.fireEvent(this.Event.LifeCycleChanged, { stage });
+ })
+ )
+ )
);
this._initUnitEvent(this._injector);
@@ -136,125 +138,142 @@ export class FUniver extends Disposable {
private _initCommandEvent(injector: Injector): void {
const commandService = injector.get(ICommandService);
- this.registerEventHandler(
- this.Event.Redo,
- () => commandService.onCommandExecuted((commandInfo) => {
- const { id, type: propType, params } = commandInfo;
- if (commandInfo.id === RedoCommand.id) {
- const type = propType!;
- const eventParams: ICommandEvent = { id, type, params };
- this.fireEvent(this.Event.Redo, eventParams);
- }
- })
+
+ this.disposeWithMe(
+ this.registerEventHandler(
+ this.Event.Redo,
+ () => commandService.onCommandExecuted((commandInfo) => {
+ const { id, type: propType, params } = commandInfo;
+ if (commandInfo.id === RedoCommand.id) {
+ const type = propType!;
+ const eventParams: ICommandEvent = { id, type, params };
+ this.fireEvent(this.Event.Redo, eventParams);
+ }
+ })
+ )
);
- this.registerEventHandler(
- this.Event.Undo,
- () => commandService.onCommandExecuted((commandInfo) => {
- const { id, type: propType, params } = commandInfo;
- if (commandInfo.id === UndoCommand.id) {
- const type = propType!;
- const eventParams: ICommandEvent = { id, type, params };
- this.fireEvent(this.Event.Undo, eventParams);
- }
- })
+ this.disposeWithMe(
+ this.registerEventHandler(
+ this.Event.Undo,
+ () => commandService.onCommandExecuted((commandInfo) => {
+ const { id, type: propType, params } = commandInfo;
+ if (commandInfo.id === UndoCommand.id) {
+ const type = propType!;
+ const eventParams: ICommandEvent = { id, type, params };
+ this.fireEvent(this.Event.Undo, eventParams);
+ }
+ })
+ )
);
- this.registerEventHandler(
- this.Event.CommandExecuted,
- () => commandService.onCommandExecuted((commandInfo, options) => {
- const { id, type: propType, params } = commandInfo;
- if (commandInfo.id !== RedoCommand.id && commandInfo.id !== UndoCommand.id) {
- const type = propType!;
- const eventParams: ICommandEvent = { id, type, params, options };
- this.fireEvent(this.Event.CommandExecuted, eventParams);
- }
- })
+ this.disposeWithMe(
+ this.registerEventHandler(
+ this.Event.CommandExecuted,
+ () => commandService.onCommandExecuted((commandInfo, options) => {
+ const { id, type: propType, params } = commandInfo;
+ if (commandInfo.id !== RedoCommand.id && commandInfo.id !== UndoCommand.id) {
+ const type = propType!;
+ const eventParams: ICommandEvent = { id, type, params, options };
+ this.fireEvent(this.Event.CommandExecuted, eventParams);
+ }
+ })
+ )
);
}
private _initBeforeCommandEvent(injector: Injector): void {
const commandService = injector.get(ICommandService);
- this.registerEventHandler(
- this.Event.BeforeRedo,
- () => commandService.beforeCommandExecuted((commandInfo) => {
- const { id, type: propType, params } = commandInfo;
- if (commandInfo.id === RedoCommand.id) {
- const type = propType!;
- const eventParams: ICommandEvent = { id, type, params };
- this.fireEvent(this.Event.BeforeRedo, eventParams);
-
- if (eventParams.cancel) {
- throw new CanceledError();
+ this.disposeWithMe(
+ this.registerEventHandler(
+ this.Event.BeforeRedo,
+ () => commandService.beforeCommandExecuted((commandInfo) => {
+ const { id, type: propType, params } = commandInfo;
+ if (commandInfo.id === RedoCommand.id) {
+ const type = propType!;
+ const eventParams: ICommandEvent = { id, type, params };
+ this.fireEvent(this.Event.BeforeRedo, eventParams);
+
+ if (eventParams.cancel) {
+ throw new CanceledError();
+ }
}
- }
- })
+ })
+ )
);
- this.registerEventHandler(
- this.Event.BeforeUndo,
- () => commandService.beforeCommandExecuted((commandInfo) => {
- const { id, type: propType, params } = commandInfo;
- if (commandInfo.id === UndoCommand.id) {
- const type = propType!;
- const eventParams: ICommandEvent = { id, type, params };
- this.fireEvent(this.Event.BeforeUndo, eventParams);
-
- if (eventParams.cancel) {
- throw new CanceledError();
+ this.disposeWithMe(
+ this.registerEventHandler(
+ this.Event.BeforeUndo,
+ () => commandService.beforeCommandExecuted((commandInfo) => {
+ const { id, type: propType, params } = commandInfo;
+ if (commandInfo.id === UndoCommand.id) {
+ const type = propType!;
+ const eventParams: ICommandEvent = { id, type, params };
+ this.fireEvent(this.Event.BeforeUndo, eventParams);
+
+ if (eventParams.cancel) {
+ throw new CanceledError();
+ }
}
- }
- })
+ })
+ )
);
- this.registerEventHandler(
- this.Event.BeforeCommandExecute,
- () => commandService.beforeCommandExecuted((commandInfo, options) => {
- const { id, type: propType, params } = commandInfo;
- if (commandInfo.id !== RedoCommand.id && commandInfo.id !== UndoCommand.id) {
- const type = propType!;
- const eventParams: ICommandEvent = { id, type, params, options };
- this.fireEvent(this.Event.BeforeCommandExecute, eventParams);
-
- if (eventParams.cancel) {
- throw new CanceledError();
+ this.disposeWithMe(
+ this.registerEventHandler(
+ this.Event.BeforeCommandExecute,
+ () => commandService.beforeCommandExecuted((commandInfo, options) => {
+ const { id, type: propType, params } = commandInfo;
+ if (commandInfo.id !== RedoCommand.id && commandInfo.id !== UndoCommand.id) {
+ const type = propType!;
+ const eventParams: ICommandEvent = { id, type, params, options };
+ this.fireEvent(this.Event.BeforeCommandExecute, eventParams);
+
+ if (eventParams.cancel) {
+ throw new CanceledError();
+ }
}
- }
- })
+ })
+ )
);
}
private _initUnitEvent(injector: Injector): void {
const univerInstanceService = injector.get(IUniverInstanceService);
- this.registerEventHandler(
- this.Event.DocDisposed,
- () => univerInstanceService.unitDisposed$.subscribe((unit) => {
- if (unit.type === UniverInstanceType.UNIVER_DOC) {
- this.fireEvent(this.Event.DocDisposed, {
- unitId: unit.getUnitId(),
- unitType: unit.type,
- snapshot: unit.getSnapshot() as IDocumentData,
- });
- }
- })
+ this.disposeWithMe(
+ this.registerEventHandler(
+ this.Event.DocDisposed,
+ () => univerInstanceService.unitDisposed$.subscribe((unit) => {
+ if (unit.type === UniverInstanceType.UNIVER_DOC) {
+ this.fireEvent(this.Event.DocDisposed, {
+ unitId: unit.getUnitId(),
+ unitType: unit.type,
+ snapshot: unit.getSnapshot() as IDocumentData,
+ });
+ }
+ })
+ )
);
- this.registerEventHandler(
- this.Event.DocCreated,
- () => univerInstanceService.unitAdded$.subscribe((unit) => {
- if (unit.type === UniverInstanceType.UNIVER_DOC) {
- const doc = unit as DocumentDataModel;
- const docUnit = injector.createInstance(FDoc, doc);
- this.fireEvent(this.Event.DocCreated, {
- unitId: unit.getUnitId(),
- type: unit.type,
- doc: docUnit,
- unit: docUnit,
- });
- }
- })
+ this.disposeWithMe(
+ this.registerEventHandler(
+ this.Event.DocCreated,
+ () => univerInstanceService.unitAdded$.subscribe((unit) => {
+ if (unit.type === UniverInstanceType.UNIVER_DOC) {
+ const doc = unit as DocumentDataModel;
+ const docUnit = injector.createInstance(FDoc, doc);
+ this.fireEvent(this.Event.DocCreated, {
+ unitId: unit.getUnitId(),
+ type: unit.type,
+ doc: docUnit,
+ unit: docUnit,
+ });
+ }
+ })
+ )
);
}
@@ -274,9 +293,7 @@ export class FUniver extends Disposable {
* ```
*/
disposeUnit(unitId: string): boolean {
- const rs = this._univerInstanceService.disposeUnit(unitId);
- this._eventRegistry.dispose();
- return rs;
+ return this._univerInstanceService.disposeUnit(unitId);
}
/**
diff --git a/packages/docs-ui/src/views/count-bar/ZoomSlider.tsx b/packages/docs-ui/src/views/count-bar/ZoomSlider.tsx
index 6a624e729c..e929f4fa01 100644
--- a/packages/docs-ui/src/views/count-bar/ZoomSlider.tsx
+++ b/packages/docs-ui/src/views/count-bar/ZoomSlider.tsx
@@ -16,8 +16,8 @@
import type { DocumentDataModel } from '@univerjs/core';
import { ICommandService, IUniverInstanceService, UniverInstanceType } from '@univerjs/core';
-import { Slider, useDependency, useObservable } from '@univerjs/ui';
-import { useCallback, useEffect, useMemo, useState } from 'react';
+import { Slider, useDependency } from '@univerjs/ui';
+import { useCallback, useEffect, useState } from 'react';
import { SetDocZoomRatioOperation } from '../../commands/operations/set-doc-zoom-ratio.operation';
const ZOOM_MAP = [50, 80, 100, 130, 150, 170, 200, 400];
@@ -26,8 +26,8 @@ const DOC_ZOOM_RANGE = [10, 400];
export function ZoomSlider() {
const commandService = useDependency(ICommandService);
const univerInstanceService = useDependency(IUniverInstanceService);
- const currentDoc$ = useMemo(() => univerInstanceService.getCurrentTypeOfUnit$<DocumentDataModel>(UniverInstanceType.UNIVER_DOC), []);
- const documentDataModel = useObservable(currentDoc$);
+ const [documentDataModel, setDocumentDataModel] = useState<DocumentDataModel | null>(null);
+ const [zoom, setZoom] = useState<number>(100);
const getCurrentZoom = useCallback(() => {
if (!documentDataModel) return 100;
@@ -36,11 +36,22 @@ export function ZoomSlider() {
return Math.round(currentZoom);
}, [documentDataModel]);
- const [zoom, setZoom] = useState<number>(100);
-
useEffect(() => {
- setZoom(getCurrentZoom());
+ const currentDoc$ = univerInstanceService.getCurrentTypeOfUnit$<DocumentDataModel>(UniverInstanceType.UNIVER_DOC);
+
+ const subscription = currentDoc$.subscribe((doc) => {
+ if (doc) {
+ setDocumentDataModel(doc);
+ setZoom(getCurrentZoom());
+ }
+ });
+ return () => {
+ subscription.unsubscribe();
+ };
+ }, [univerInstanceService, getCurrentZoom]);
+
+ useEffect(() => {
const disposable = commandService.onCommandExecuted((commandInfo) => {
if (commandInfo.id === SetDocZoomRatioOperation.id) {
const currentZoom = getCurrentZoom();
diff --git a/packages/sheets-crosshair-highlight/src/facade/f-univer.ts b/packages/sheets-crosshair-highlight/src/facade/f-univer.ts
index 0e05e8292d..9dca266075 100644
--- a/packages/sheets-crosshair-highlight/src/facade/f-univer.ts
+++ b/packages/sheets-crosshair-highlight/src/facade/f-univer.ts
@@ -172,36 +172,40 @@ export class FUniverCrosshairHighlightMixin extends FUniver implements IFUniverC
override _initialize(injector: Injector): void {
const commandService = injector.get(ICommandService);
- this.registerEventHandler(
- this.Event.CrosshairHighlightEnabledChanged,
- () => commandService.onCommandExecuted((commandInfo) => {
- if (
- commandInfo.id === EnableCrosshairHighlightOperation.id ||
+ this.disposeWithMe(
+ this.registerEventHandler(
+ this.Event.CrosshairHighlightEnabledChanged,
+ () => commandService.onCommandExecuted((commandInfo) => {
+ if (
+ commandInfo.id === EnableCrosshairHighlightOperation.id ||
commandInfo.id === DisableCrosshairHighlightOperation.id ||
commandInfo.id === ToggleCrosshairHighlightOperation.id
- ) {
- const activeSheet = this.getActiveSheet();
- if (!activeSheet) return;
- this.fireEvent(this.Event.CrosshairHighlightEnabledChanged, {
- enabled: this.getCrosshairHighlightEnabled(),
- ...activeSheet,
- });
- }
- })
+ ) {
+ const activeSheet = this.getActiveSheet();
+ if (!activeSheet) return;
+ this.fireEvent(this.Event.CrosshairHighlightEnabledChanged, {
+ enabled: this.getCrosshairHighlightEnabled(),
+ ...activeSheet,
+ });
+ }
+ })
+ )
);
- this.registerEventHandler(
- this.Event.CrosshairHighlightColorChanged,
- () => commandService.onCommandExecuted((commandInfo) => {
- if (commandInfo.id === SetCrosshairHighlightColorOperation.id) {
- const activeSheet = this.getActiveSheet();
- if (!activeSheet) return;
- this.fireEvent(this.Event.CrosshairHighlightColorChanged, {
- color: this.getCrosshairHighlightColor(),
- ...activeSheet,
- });
- }
- })
+ this.disposeWithMe(
+ this.registerEventHandler(
+ this.Event.CrosshairHighlightColorChanged,
+ () => commandService.onCommandExecuted((commandInfo) => {
+ if (commandInfo.id === SetCrosshairHighlightColorOperation.id) {
+ const activeSheet = this.getActiveSheet();
+ if (!activeSheet) return;
+ this.fireEvent(this.Event.CrosshairHighlightColorChanged, {
+ color: this.getCrosshairHighlightColor(),
+ ...activeSheet,
+ });
+ }
+ })
+ )
);
}
diff --git a/packages/sheets-data-validation/src/facade/f-univer.ts b/packages/sheets-data-validation/src/facade/f-univer.ts
index 53e496cb82..2c3266dd21 100644
--- a/packages/sheets-data-validation/src/facade/f-univer.ts
+++ b/packages/sheets-data-validation/src/facade/f-univer.ts
@@ -86,223 +86,239 @@ export class FUnvierDataValidationMixin extends FUniver implements IFUnvierDataV
override _initialize(injector: Injector): void {
const commandService = injector.get(ICommandService);
- this.registerEventHandler(
- this.Event.SheetDataValidationChanged,
- () => {
- if (!injector.has(SheetDataValidationModel)) return { dispose: () => {} } as IDisposable;
- const sheetDataValidationModel = injector.get(SheetDataValidationModel);
+ this.disposeWithMe(
+ this.registerEventHandler(
+ this.Event.SheetDataValidationChanged,
+ () => {
+ if (!injector.has(SheetDataValidationModel)) return { dispose: () => {} } as IDisposable;
+ const sheetDataValidationModel = injector.get(SheetDataValidationModel);
- return sheetDataValidationModel.ruleChange$.subscribe((ruleChange) => {
- const { unitId, subUnitId, rule, oldRule, type } = ruleChange;
- const target = this.getSheetTarget(unitId, subUnitId);
- if (!target) {
- return;
- }
- const { workbook, worksheet } = target;
+ return sheetDataValidationModel.ruleChange$.subscribe((ruleChange) => {
+ const { unitId, subUnitId, rule, oldRule, type } = ruleChange;
+ const target = this.getSheetTarget(unitId, subUnitId);
+ if (!target) {
+ return;
+ }
+ const { workbook, worksheet } = target;
- const fRule = new FDataValidation(rule, worksheet.getSheet(), this._injector);
- this.fireEvent(this.Event.SheetDataValidationChanged, {
- origin: ruleChange,
- worksheet,
- workbook,
- changeType: type,
- oldRule,
- rule: fRule,
+ const fRule = new FDataValidation(rule, worksheet.getSheet(), this._injector);
+ this.fireEvent(this.Event.SheetDataValidationChanged, {
+ origin: ruleChange,
+ worksheet,
+ workbook,
+ changeType: type,
+ oldRule,
+ rule: fRule,
+ });
});
- });
- }
+ }
+ )
);
- this.registerEventHandler(
- this.Event.SheetDataValidatorStatusChanged,
- () => {
- if (!injector.has(SheetDataValidationModel)) return { dispose: () => {} } as IDisposable;
- const sheetDataValidationModel = injector.get(SheetDataValidationModel);
+ this.disposeWithMe(
+ this.registerEventHandler(
+ this.Event.SheetDataValidatorStatusChanged,
+ () => {
+ if (!injector.has(SheetDataValidationModel)) return { dispose: () => {} } as IDisposable;
+ const sheetDataValidationModel = injector.get(SheetDataValidationModel);
- return sheetDataValidationModel.validStatusChange$.subscribe((statusChange) => {
- const { unitId, subUnitId, ruleId, status, row, col } = statusChange;
- const target = this.getSheetTarget(unitId, subUnitId);
- if (!target) {
- return;
- }
- const { workbook, worksheet } = target;
- const rule = worksheet.getDataValidation(ruleId);
- if (!rule) {
- return;
- }
- this.fireEvent(this.Event.SheetDataValidatorStatusChanged, {
- workbook,
- worksheet,
- row,
- column: col,
- rule,
- status,
+ return sheetDataValidationModel.validStatusChange$.subscribe((statusChange) => {
+ const { unitId, subUnitId, ruleId, status, row, col } = statusChange;
+ const target = this.getSheetTarget(unitId, subUnitId);
+ if (!target) {
+ return;
+ }
+ const { workbook, worksheet } = target;
+ const rule = worksheet.getDataValidation(ruleId);
+ if (!rule) {
+ return;
+ }
+ this.fireEvent(this.Event.SheetDataValidatorStatusChanged, {
+ workbook,
+ worksheet,
+ row,
+ column: col,
+ rule,
+ status,
+ });
});
- });
- }
+ }
+ )
);
// Register handlers for before command events
- this.registerEventHandler(
- this.Event.BeforeSheetDataValidationAdd,
- () => commandService.beforeCommandExecuted((commandInfo) => {
- if (commandInfo.id === AddSheetDataValidationCommand.id) {
- const params = commandInfo.params as IAddSheetDataValidationCommandParams;
- const target = this.getSheetTarget(params.unitId, params.subUnitId);
- if (!target) {
- return;
- }
- const { workbook, worksheet } = target;
- const eventParams: IBeforeSheetDataValidationAddEvent = {
- worksheet,
- workbook,
- rule: params.rule,
- };
- this.fireEvent(this.Event.BeforeSheetDataValidationAdd, eventParams);
- if (eventParams.cancel) {
- throw new CanceledError();
+ this.disposeWithMe(
+ this.registerEventHandler(
+ this.Event.BeforeSheetDataValidationAdd,
+ () => commandService.beforeCommandExecuted((commandInfo) => {
+ if (commandInfo.id === AddSheetDataValidationCommand.id) {
+ const params = commandInfo.params as IAddSheetDataValidationCommandParams;
+ const target = this.getSheetTarget(params.unitId, params.subUnitId);
+ if (!target) {
+ return;
+ }
+ const { workbook, worksheet } = target;
+ const eventParams: IBeforeSheetDataValidationAddEvent = {
+ worksheet,
+ workbook,
+ rule: params.rule,
+ };
+ this.fireEvent(this.Event.BeforeSheetDataValidationAdd, eventParams);
+ if (eventParams.cancel) {
+ throw new CanceledError();
+ }
}
- }
- })
+ })
+ )
);
- this.registerEventHandler(
- this.Event.BeforeSheetDataValidationCriteriaUpdate,
- () => commandService.beforeCommandExecuted((commandInfo) => {
- if (commandInfo.id === UpdateSheetDataValidationSettingCommand.id) {
- const params = commandInfo.params as IUpdateSheetDataValidationSettingCommandParams;
- const target = this.getSheetTarget(params.unitId, params.subUnitId);
- if (!target) {
- return;
- }
- const { workbook, worksheet } = target;
- const rule = worksheet.getDataValidation(params.ruleId);
- if (!rule) {
- return;
- }
- const eventParams: IBeforeSheetDataValidationCriteriaUpdateEvent = {
- worksheet,
- workbook,
- rule,
- ruleId: params.ruleId,
- newCriteria: params.setting,
- };
+ this.disposeWithMe(
+ this.registerEventHandler(
+ this.Event.BeforeSheetDataValidationCriteriaUpdate,
+ () => commandService.beforeCommandExecuted((commandInfo) => {
+ if (commandInfo.id === UpdateSheetDataValidationSettingCommand.id) {
+ const params = commandInfo.params as IUpdateSheetDataValidationSettingCommandParams;
+ const target = this.getSheetTarget(params.unitId, params.subUnitId);
+ if (!target) {
+ return;
+ }
+ const { workbook, worksheet } = target;
+ const rule = worksheet.getDataValidation(params.ruleId);
+ if (!rule) {
+ return;
+ }
+ const eventParams: IBeforeSheetDataValidationCriteriaUpdateEvent = {
+ worksheet,
+ workbook,
+ rule,
+ ruleId: params.ruleId,
+ newCriteria: params.setting,
+ };
- this.fireEvent(this.Event.BeforeSheetDataValidationCriteriaUpdate, eventParams);
- if (eventParams.cancel) {
- throw new CanceledError();
+ this.fireEvent(this.Event.BeforeSheetDataValidationCriteriaUpdate, eventParams);
+ if (eventParams.cancel) {
+ throw new CanceledError();
+ }
}
- }
- })
+ })
+ )
);
- this.registerEventHandler(
- this.Event.BeforeSheetDataValidationRangeUpdate,
- () => commandService.beforeCommandExecuted((commandInfo) => {
- if (commandInfo.id === UpdateSheetDataValidationRangeCommand.id) {
- const params = commandInfo.params as IUpdateSheetDataValidationRangeCommandParams;
- const target = this.getSheetTarget(params.unitId, params.subUnitId);
- if (!target) {
- return;
- }
- const { workbook, worksheet } = target;
- const rule = worksheet.getDataValidation(params.ruleId);
- if (!rule) {
- return;
+ this.disposeWithMe(
+ this.registerEventHandler(
+ this.Event.BeforeSheetDataValidationRangeUpdate,
+ () => commandService.beforeCommandExecuted((commandInfo) => {
+ if (commandInfo.id === UpdateSheetDataValidationRangeCommand.id) {
+ const params = commandInfo.params as IUpdateSheetDataValidationRangeCommandParams;
+ const target = this.getSheetTarget(params.unitId, params.subUnitId);
+ if (!target) {
+ return;
+ }
+ const { workbook, worksheet } = target;
+ const rule = worksheet.getDataValidation(params.ruleId);
+ if (!rule) {
+ return;
+ }
+ const eventParams: IBeforeSheetDataValidationRangeUpdateEvent = {
+ worksheet,
+ workbook,
+ rule,
+ ruleId: params.ruleId,
+ newRanges: params.ranges,
+ };
+ this.fireEvent(this.Event.BeforeSheetDataValidationRangeUpdate, eventParams);
+ if (eventParams.cancel) {
+ throw new CanceledError();
+ }
}
- const eventParams: IBeforeSheetDataValidationRangeUpdateEvent = {
- worksheet,
- workbook,
- rule,
- ruleId: params.ruleId,
- newRanges: params.ranges,
- };
- this.fireEvent(this.Event.BeforeSheetDataValidationRangeUpdate, eventParams);
- if (eventParams.cancel) {
- throw new CanceledError();
- }
- }
- })
+ })
+ )
);
- this.registerEventHandler(
- this.Event.BeforeSheetDataValidationOptionsUpdate,
- () => commandService.beforeCommandExecuted((commandInfo) => {
- if (commandInfo.id === UpdateSheetDataValidationOptionsCommand.id) {
- const params = commandInfo.params as IUpdateSheetDataValidationOptionsCommandParams;
- const target = this.getSheetTarget(params.unitId, params.subUnitId);
- if (!target) {
- return;
- }
- const { workbook, worksheet } = target;
- const rule = worksheet.getDataValidation(params.ruleId);
- if (!rule) {
- return;
+ this.disposeWithMe(
+ this.registerEventHandler(
+ this.Event.BeforeSheetDataValidationOptionsUpdate,
+ () => commandService.beforeCommandExecuted((commandInfo) => {
+ if (commandInfo.id === UpdateSheetDataValidationOptionsCommand.id) {
+ const params = commandInfo.params as IUpdateSheetDataValidationOptionsCommandParams;
+ const target = this.getSheetTarget(params.unitId, params.subUnitId);
+ if (!target) {
+ return;
+ }
+ const { workbook, worksheet } = target;
+ const rule = worksheet.getDataValidation(params.ruleId);
+ if (!rule) {
+ return;
+ }
+ const eventParams: IBeforeSheetDataValidationOptionsUpdateEvent = {
+ worksheet,
+ workbook,
+ rule,
+ ruleId: params.ruleId,
+ newOptions: params.options,
+ };
+ this.fireEvent(this.Event.BeforeSheetDataValidationOptionsUpdate, eventParams);
+ if (eventParams.cancel) {
+ throw new CanceledError();
+ }
}
- const eventParams: IBeforeSheetDataValidationOptionsUpdateEvent = {
- worksheet,
- workbook,
- rule,
- ruleId: params.ruleId,
- newOptions: params.options,
- };
- this.fireEvent(this.Event.BeforeSheetDataValidationOptionsUpdate, eventParams);
- if (eventParams.cancel) {
- throw new CanceledError();
- }
- }
- })
+ })
+ )
);
- this.registerEventHandler(
- this.Event.BeforeSheetDataValidationDelete,
- () => commandService.beforeCommandExecuted((commandInfo) => {
- if (commandInfo.id === RemoveSheetDataValidationCommand.id) {
- const params = commandInfo.params as IRemoveSheetDataValidationCommandParams;
- const target = this.getSheetTarget(params.unitId, params.subUnitId);
- if (!target) {
- return;
+ this.disposeWithMe(
+ this.registerEventHandler(
+ this.Event.BeforeSheetDataValidationDelete,
+ () => commandService.beforeCommandExecuted((commandInfo) => {
+ if (commandInfo.id === RemoveSheetDataValidationCommand.id) {
+ const params = commandInfo.params as IRemoveSheetDataValidationCommandParams;
+ const target = this.getSheetTarget(params.unitId, params.subUnitId);
+ if (!target) {
+ return;
+ }
+ const { workbook, worksheet } = target;
+ const rule = worksheet.getDataValidation(params.ruleId);
+ if (!rule) {
+ return;
+ }
+ const eventParams: IBeforeSheetDataValidationDeleteEvent = {
+ worksheet,
+ workbook,
+ rule,
+ ruleId: params.ruleId,
+ };
+ this.fireEvent(this.Event.BeforeSheetDataValidationDelete, eventParams);
+ if (eventParams.cancel) {
+ throw new CanceledError();
+ }
}
- const { workbook, worksheet } = target;
- const rule = worksheet.getDataValidation(params.ruleId);
- if (!rule) {
- return;
- }
- const eventParams: IBeforeSheetDataValidationDeleteEvent = {
- worksheet,
- workbook,
- rule,
- ruleId: params.ruleId,
- };
- this.fireEvent(this.Event.BeforeSheetDataValidationDelete, eventParams);
- if (eventParams.cancel) {
- throw new CanceledError();
- }
- }
- })
+ })
+ )
);
- this.registerEventHandler(
- this.Event.BeforeSheetDataValidationDeleteAll,
- () => commandService.beforeCommandExecuted((commandInfo) => {
- if (commandInfo.id === RemoveSheetAllDataValidationCommand.id) {
- const params = commandInfo.params as IRemoveSheetAllDataValidationCommandParams;
- const target = this.getSheetTarget(params.unitId, params.subUnitId);
- if (!target) {
- return;
+ this.disposeWithMe(
+ this.registerEventHandler(
+ this.Event.BeforeSheetDataValidationDeleteAll,
+ () => commandService.beforeCommandExecuted((commandInfo) => {
+ if (commandInfo.id === RemoveSheetAllDataValidationCommand.id) {
+ const params = commandInfo.params as IRemoveSheetAllDataValidationCommandParams;
+ const target = this.getSheetTarget(params.unitId, params.subUnitId);
+ if (!target) {
+ return;
+ }
+ const { workbook, worksheet } = target;
+ const eventParams: IBeforeSheetDataValidationDeleteAllEvent = {
+ worksheet,
+ workbook,
+ rules: worksheet.getDataValidations(),
+ };
+ this.fireEvent(this.Event.BeforeSheetDataValidationDeleteAll, eventParams);
+ if (eventParams.cancel) {
+ throw new CanceledError();
+ }
}
- const { workbook, worksheet } = target;
- const eventParams: IBeforeSheetDataValidationDeleteAllEvent = {
- worksheet,
- workbook,
- rules: worksheet.getDataValidations(),
- };
- this.fireEvent(this.Event.BeforeSheetDataValidationDeleteAll, eventParams);
- if (eventParams.cancel) {
- throw new CanceledError();
- }
- }
- })
+ })
+ )
);
}
}
diff --git a/packages/sheets-drawing-ui/src/facade/f-univer.ts b/packages/sheets-drawing-ui/src/facade/f-univer.ts
index f25d9e2dc5..bb7f28acdb 100644
--- a/packages/sheets-drawing-ui/src/facade/f-univer.ts
+++ b/packages/sheets-drawing-ui/src/facade/f-univer.ts
@@ -86,410 +86,439 @@ export class FUniverDrawingUIMixin extends FUniver implements IFUniverDrawingUIM
const commandService = injector.get(ICommandService);
// Float DOM Add Events
- this.registerEventHandler(
- this.Event.BeforeFloatDomAdd,
- () => commandService.beforeCommandExecuted((commandInfo) => {
- if (commandInfo.id !== InsertSheetDrawingCommand.id) return;
-
- const params = commandInfo.params as IInsertDrawingCommandParams;
- const workbook = this.getActiveWorkbook();
- if (workbook == null || params == null) {
- return;
- }
-
- const { drawings } = params;
- const floatDomDrawings = drawings.filter(
- (drawing) => drawing.drawingType === DrawingTypeEnum.DRAWING_DOM
- ) as ISheetFloatDom[];
-
- if (floatDomDrawings.length === 0) {
- return;
- }
-
- const eventParams: IBeforeFloatDomAddParam = {
- workbook,
- drawings: floatDomDrawings,
- };
-
- this.fireEvent(this.Event.BeforeFloatDomAdd, eventParams);
-
- if (eventParams.cancel) {
- throw new CanceledError();
- }
- })
- );
+ this.disposeWithMe(
+ this.registerEventHandler(
+ this.Event.BeforeFloatDomAdd,
+ () => commandService.beforeCommandExecuted((commandInfo) => {
+ if (commandInfo.id !== InsertSheetDrawingCommand.id) return;
+
+ const params = commandInfo.params as IInsertDrawingCommandParams;
+ const workbook = this.getActiveWorkbook();
+ if (workbook == null || params == null) {
+ return;
+ }
- this.registerEventHandler(
- this.Event.FloatDomAdded,
- () => commandService.onCommandExecuted((commandInfo) => {
- if (commandInfo.id !== InsertSheetDrawingCommand.id) return;
-
- const params = commandInfo.params as IInsertDrawingCommandParams;
- const workbook = this.getActiveWorkbook();
- if (workbook == null || params == null) {
- return;
- }
-
- const { drawings } = params;
- const floatDomDrawings = drawings.filter(
- (drawing) => drawing.drawingType === DrawingTypeEnum.DRAWING_DOM
- ) as ISheetFloatDom[];
-
- if (floatDomDrawings.length === 0) {
- return;
- }
-
- this.fireEvent(this.Event.FloatDomAdded, {
- workbook,
- drawings: floatDomDrawings,
- });
- })
- );
+ const { drawings } = params;
+ const floatDomDrawings = drawings.filter(
+ (drawing) => drawing.drawingType === DrawingTypeEnum.DRAWING_DOM
+ ) as ISheetFloatDom[];
+
+ if (floatDomDrawings.length === 0) {
+ return;
+ }
+
+ const eventParams: IBeforeFloatDomAddParam = {
+ workbook,
+ drawings: floatDomDrawings,
+ };
+
+ this.fireEvent(this.Event.BeforeFloatDomAdd, eventParams);
- this.registerEventHandler(
- this.Event.BeforeOverGridImageInsert,
- () => commandService.beforeCommandExecuted((commandInfo) => {
- if (commandInfo.id !== InsertSheetDrawingCommand.id) return;
-
- const params = commandInfo.params as IInsertDrawingCommandParams;
- const workbook = this.getActiveWorkbook();
- if (workbook == null || params == null) {
- return;
- }
-
- const { drawings } = params;
- const eventParams: IBeforeOverGridImageInsertParam = {
- workbook,
- insertImageParams: drawings as ISheetImage[],
- };
-
- this.fireEvent(this.Event.BeforeOverGridImageInsert, eventParams);
-
- if (eventParams.cancel) {
- throw new CanceledError();
- }
- })
+ if (eventParams.cancel) {
+ throw new CanceledError();
+ }
+ })
+ )
);
- this.registerEventHandler(
- this.Event.BeforeOverGridImageRemove,
- () => commandService.beforeCommandExecuted((commandInfo) => {
- if (commandInfo.id !== RemoveSheetDrawingCommand.id) return;
+ this.disposeWithMe(
+ this.registerEventHandler(
+ this.Event.FloatDomAdded,
+ () => commandService.onCommandExecuted((commandInfo) => {
+ if (commandInfo.id !== InsertSheetDrawingCommand.id) return;
+
+ const params = commandInfo.params as IInsertDrawingCommandParams;
+ const workbook = this.getActiveWorkbook();
+ if (workbook == null || params == null) {
+ return;
+ }
+
+ const { drawings } = params;
+ const floatDomDrawings = drawings.filter(
+ (drawing) => drawing.drawingType === DrawingTypeEnum.DRAWING_DOM
+ ) as ISheetFloatDom[];
+
+ if (floatDomDrawings.length === 0) {
+ return;
+ }
- const params = commandInfo.params as IDeleteDrawingCommandParams;
- const workbook = this.getActiveWorkbook();
- if (workbook == null || params == null) {
- return;
- }
+ this.fireEvent(this.Event.FloatDomAdded, {
+ workbook,
+ drawings: floatDomDrawings,
+ });
+ })
+ )
+ );
- const drawingManagerService = injector.get(IDrawingManagerService);
+ this.disposeWithMe(
+ this.registerEventHandler(
+ this.Event.BeforeOverGridImageInsert,
+ () => commandService.beforeCommandExecuted((commandInfo) => {
+ if (commandInfo.id !== InsertSheetDrawingCommand.id) return;
- const { drawings } = params;
- const willRemoveDrawings = drawings.map((drawing) => {
- return drawingManagerService.getDrawingByParam(drawing);
- }) as ISheetImage[];
+ const params = commandInfo.params as IInsertDrawingCommandParams;
+ const workbook = this.getActiveWorkbook();
+ if (workbook == null || params == null) {
+ return;
+ }
- const eventParams: IBeforeOverGridImageRemoveParam = {
- workbook,
- images: this._createFOverGridImage(willRemoveDrawings),
- };
+ const { drawings } = params;
+ const eventParams: IBeforeOverGridImageInsertParam = {
+ workbook,
+ insertImageParams: drawings as ISheetImage[],
+ };
- this.fireEvent(this.Event.BeforeOverGridImageRemove, eventParams);
+ this.fireEvent(this.Event.BeforeOverGridImageInsert, eventParams);
- if (eventParams.cancel) {
- throw new CanceledError();
- }
- })
+ if (eventParams.cancel) {
+ throw new CanceledError();
+ }
+ })
+ )
);
- this.registerEventHandler(
- this.Event.BeforeOverGridImageChange,
- () => commandService.beforeCommandExecuted((commandInfo) => {
- if (commandInfo.id !== SetSheetDrawingCommand.id) return;
+ this.disposeWithMe(
+ this.registerEventHandler(
+ this.Event.BeforeOverGridImageRemove,
+ () => commandService.beforeCommandExecuted((commandInfo) => {
+ if (commandInfo.id !== RemoveSheetDrawingCommand.id) return;
+
+ const params = commandInfo.params as IDeleteDrawingCommandParams;
+ const workbook = this.getActiveWorkbook();
+ if (workbook == null || params == null) {
+ return;
+ }
+
+ const drawingManagerService = injector.get(IDrawingManagerService);
+
+ const { drawings } = params;
+ const willRemoveDrawings = drawings.map((drawing) => {
+ return drawingManagerService.getDrawingByParam(drawing);
+ }) as ISheetImage[];
- const params = commandInfo.params as ISetDrawingCommandParams;
- const workbook = this.getActiveWorkbook();
- if (workbook == null || params == null) {
- return;
- }
+ const eventParams: IBeforeOverGridImageRemoveParam = {
+ workbook,
+ images: this._createFOverGridImage(willRemoveDrawings),
+ };
+
+ this.fireEvent(this.Event.BeforeOverGridImageRemove, eventParams);
+
+ if (eventParams.cancel) {
+ throw new CanceledError();
+ }
+ })
+ )
+ );
- const { drawings } = params;
- const drawingManagerService = injector.get(IDrawingManagerService);
+ this.disposeWithMe(
+ this.registerEventHandler(
+ this.Event.BeforeOverGridImageChange,
+ () => commandService.beforeCommandExecuted((commandInfo) => {
+ if (commandInfo.id !== SetSheetDrawingCommand.id) return;
- const images: IBeforeOverGridImageChangeParamObject[] = [];
- drawings.forEach((drawing) => {
- const image = drawingManagerService.getDrawingByParam(drawing as IDrawingSearch) as ISheetImage;
- if (image == null) {
+ const params = commandInfo.params as ISetDrawingCommandParams;
+ const workbook = this.getActiveWorkbook();
+ if (workbook == null || params == null) {
return;
}
- images.push({
- changeParam: drawing,
- image: this._injector.createInstance(FOverGridImage, image),
+ const { drawings } = params;
+ const drawingManagerService = injector.get(IDrawingManagerService);
+
+ const images: IBeforeOverGridImageChangeParamObject[] = [];
+ drawings.forEach((drawing) => {
+ const image = drawingManagerService.getDrawingByParam(drawing as IDrawingSearch) as ISheetImage;
+ if (image == null) {
+ return;
+ }
+
+ images.push({
+ changeParam: drawing,
+ image: this._injector.createInstance(FOverGridImage, image),
+ });
});
- });
- const eventParams: IBeforeOverGridImageChangeParam = {
- workbook,
- images,
- };
+ const eventParams: IBeforeOverGridImageChangeParam = {
+ workbook,
+ images,
+ };
- this.fireEvent(this.Event.BeforeOverGridImageChange, eventParams);
+ this.fireEvent(this.Event.BeforeOverGridImageChange, eventParams);
- if (eventParams.cancel) {
- drawingManagerService.updateNotification(drawings as IDrawingSearch[]);
- throw new CanceledError();
- }
- })
+ if (eventParams.cancel) {
+ drawingManagerService.updateNotification(drawings as IDrawingSearch[]);
+ throw new CanceledError();
+ }
+ })
+ )
);
// Float DOM Update Events
- this.registerEventHandler(
- this.Event.BeforeFloatDomUpdate,
- () => commandService.beforeCommandExecuted((commandInfo) => {
- if (commandInfo.id !== SetSheetDrawingCommand.id) return;
-
- const params = commandInfo.params as ISetDrawingCommandParams;
- const workbook = this.getActiveWorkbook();
- if (workbook == null || params == null) {
- return;
- }
-
- const { drawings } = params;
- const drawingManagerService = injector.get(IDrawingManagerService);
-
- const floatDomDrawings: ISheetFloatDom[] = [];
- drawings.forEach((drawing) => {
- const dom = drawingManagerService.getDrawingByParam(drawing as IDrawingSearch) as ISheetFloatDom;
- if (dom?.drawingType === DrawingTypeEnum.DRAWING_DOM) {
- floatDomDrawings.push(dom);
+ this.disposeWithMe(
+ this.registerEventHandler(
+ this.Event.BeforeFloatDomUpdate,
+ () => commandService.beforeCommandExecuted((commandInfo) => {
+ if (commandInfo.id !== SetSheetDrawingCommand.id) return;
+
+ const params = commandInfo.params as ISetDrawingCommandParams;
+ const workbook = this.getActiveWorkbook();
+ if (workbook == null || params == null) {
+ return;
}
- });
- if (floatDomDrawings.length === 0) {
- return;
- }
+ const { drawings } = params;
+ const drawingManagerService = injector.get(IDrawingManagerService);
+
+ const floatDomDrawings: ISheetFloatDom[] = [];
+ drawings.forEach((drawing) => {
+ const dom = drawingManagerService.getDrawingByParam(drawing as IDrawingSearch) as ISheetFloatDom;
+ if (dom?.drawingType === DrawingTypeEnum.DRAWING_DOM) {
+ floatDomDrawings.push(dom);
+ }
+ });
+
+ if (floatDomDrawings.length === 0) {
+ return;
+ }
- const eventParams: IBeforeFloatDomUpdateParam = {
- workbook,
- drawings: floatDomDrawings,
- };
+ const eventParams: IBeforeFloatDomUpdateParam = {
+ workbook,
+ drawings: floatDomDrawings,
+ };
- this.fireEvent(this.Event.BeforeFloatDomUpdate, eventParams);
+ this.fireEvent(this.Event.BeforeFloatDomUpdate, eventParams);
- if (eventParams.cancel) {
- drawingManagerService.updateNotification(drawings as IDrawingSearch[]);
- throw new CanceledError();
- }
- })
+ if (eventParams.cancel) {
+ drawingManagerService.updateNotification(drawings as IDrawingSearch[]);
+ throw new CanceledError();
+ }
+ })
+ )
);
- this.registerEventHandler(
- this.Event.FloatDomUpdated,
- () => commandService.onCommandExecuted((commandInfo) => {
- if (commandInfo.id !== SetSheetDrawingCommand.id) return;
-
- const params = commandInfo.params as ISetDrawingCommandParams;
- const workbook = this.getActiveWorkbook();
- if (workbook == null || params == null) {
- return;
- }
-
- const { drawings } = params;
- const drawingManagerService = injector.get(IDrawingManagerService);
-
- const floatDomDrawings: ISheetFloatDom[] = [];
- drawings.forEach((drawing) => {
- const dom = drawingManagerService.getDrawingByParam(drawing as IDrawingSearch) as ISheetFloatDom;
- if (dom?.drawingType === DrawingTypeEnum.DRAWING_DOM) {
- floatDomDrawings.push(dom);
+ // Float DOM Update Events
+ this.disposeWithMe(
+ this.registerEventHandler(
+ this.Event.FloatDomUpdated,
+ () => commandService.onCommandExecuted((commandInfo) => {
+ if (commandInfo.id !== SetSheetDrawingCommand.id) return;
+
+ const params = commandInfo.params as ISetDrawingCommandParams;
+ const workbook = this.getActiveWorkbook();
+ if (workbook == null || params == null) {
+ return;
}
- });
- if (floatDomDrawings.length === 0) {
- return;
- }
+ const { drawings } = params;
+ const drawingManagerService = injector.get(IDrawingManagerService);
+
+ const floatDomDrawings: ISheetFloatDom[] = [];
+ drawings.forEach((drawing) => {
+ const dom = drawingManagerService.getDrawingByParam(drawing as IDrawingSearch) as ISheetFloatDom;
+ if (dom?.drawingType === DrawingTypeEnum.DRAWING_DOM) {
+ floatDomDrawings.push(dom);
+ }
+ });
+
+ if (floatDomDrawings.length === 0) {
+ return;
+ }
- this.fireEvent(this.Event.FloatDomUpdated, {
- workbook,
- drawings: floatDomDrawings,
- });
- })
+ this.fireEvent(this.Event.FloatDomUpdated, {
+ workbook,
+ drawings: floatDomDrawings,
+ });
+ })
+ )
);
// Float DOM Delete Events
- this.registerEventHandler(
- this.Event.BeforeFloatDomDelete,
- () => commandService.beforeCommandExecuted((commandInfo) => {
- if (commandInfo.id !== RemoveSheetDrawingCommand.id) return;
-
- const params = commandInfo.params as IDeleteDrawingCommandParams;
- const workbook = this.getActiveWorkbook();
- if (workbook == null || params == null) {
- return;
- }
-
- const drawingManagerService = injector.get(IDrawingManagerService);
-
- const { drawings } = params;
- const floatDomDrawings = drawings
- .map((drawing) => drawingManagerService.getDrawingByParam(drawing))
- .filter((drawing): drawing is ISheetFloatDom =>
- drawing?.drawingType === DrawingTypeEnum.DRAWING_DOM
- );
-
- if (floatDomDrawings.length === 0) {
- return;
- }
-
- const eventParams: IBeforeFloatDomDeleteParam = {
- workbook,
- drawings: floatDomDrawings,
- };
-
- this.fireEvent(this.Event.BeforeFloatDomDelete, eventParams);
-
- if (eventParams.cancel) {
- throw new CanceledError();
- }
- })
+ this.disposeWithMe(
+ this.registerEventHandler(
+ this.Event.BeforeFloatDomDelete,
+ () => commandService.beforeCommandExecuted((commandInfo) => {
+ if (commandInfo.id !== RemoveSheetDrawingCommand.id) return;
+
+ const params = commandInfo.params as IDeleteDrawingCommandParams;
+ const workbook = this.getActiveWorkbook();
+ if (workbook == null || params == null) {
+ return;
+ }
+
+ const drawingManagerService = injector.get(IDrawingManagerService);
+
+ const { drawings } = params;
+ const floatDomDrawings = drawings
+ .map((drawing) => drawingManagerService.getDrawingByParam(drawing))
+ .filter((drawing): drawing is ISheetFloatDom =>
+ drawing?.drawingType === DrawingTypeEnum.DRAWING_DOM
+ );
+
+ if (floatDomDrawings.length === 0) {
+ return;
+ }
+
+ const eventParams: IBeforeFloatDomDeleteParam = {
+ workbook,
+ drawings: floatDomDrawings,
+ };
+
+ this.fireEvent(this.Event.BeforeFloatDomDelete, eventParams);
+
+ if (eventParams.cancel) {
+ throw new CanceledError();
+ }
+ })
+ )
);
- this.registerEventHandler(
- this.Event.FloatDomDeleted,
- () => commandService.onCommandExecuted((commandInfo) => {
- if (commandInfo.id !== RemoveSheetDrawingCommand.id) return;
+ this.disposeWithMe(
+ this.registerEventHandler(
+ this.Event.FloatDomDeleted,
+ () => commandService.onCommandExecuted((commandInfo) => {
+ if (commandInfo.id !== RemoveSheetDrawingCommand.id) return;
- const params = commandInfo.params as IDeleteDrawingCommandParams;
- const workbook = this.getActiveWorkbook();
- if (workbook == null || params == null) {
- return;
- }
+ const params = commandInfo.params as IDeleteDrawingCommandParams;
+ const workbook = this.getActiveWorkbook();
+ if (workbook == null || params == null) {
+ return;
+ }
- const { drawings } = params;
+ const { drawings } = params;
- this.fireEvent(this.Event.FloatDomDeleted, {
- workbook,
- drawings: drawings.filter((i) => i.drawingType === DrawingTypeEnum.DRAWING_DOM).map((i) => i.drawingId),
- });
- })
+ this.fireEvent(this.Event.FloatDomDeleted, {
+ workbook,
+ drawings: drawings.filter((i) => i.drawingType === DrawingTypeEnum.DRAWING_DOM).map((i) => i.drawingId),
+ });
+ })
+ )
);
- this.registerEventHandler(
- this.Event.BeforeOverGridImageSelect,
- () => commandService.beforeCommandExecuted((commandInfo) => {
- if (commandInfo.id !== SetDrawingSelectedOperation.id) return;
-
- const drawings = commandInfo.params as IDrawingSearch[];
- const workbook = this.getActiveWorkbook();
- if (workbook == null) {
- return;
- }
- const drawingManagerService = injector.get(IDrawingManagerService);
-
- const oldSelectedDrawings = drawingManagerService.getFocusDrawings() as ISheetImage[];
- const selectedDrawings = drawings.map((drawing) => {
- return drawingManagerService.getDrawingByParam(drawing);
- }) as ISheetImage[];
-
- const eventParams: IBeforeOverGridImageSelectParam = {
- workbook,
- selectedImages: this._createFOverGridImage(selectedDrawings),
- oldSelectedImages: this._createFOverGridImage(oldSelectedDrawings),
- };
-
- this.fireEvent(this.Event.BeforeOverGridImageSelect, eventParams);
-
- if (eventParams.cancel) {
- throw new CanceledError();
- }
- })
+ this.disposeWithMe(
+ this.registerEventHandler(
+ this.Event.BeforeOverGridImageSelect,
+ () => commandService.beforeCommandExecuted((commandInfo) => {
+ if (commandInfo.id !== SetDrawingSelectedOperation.id) return;
+
+ const drawings = commandInfo.params as IDrawingSearch[];
+ const workbook = this.getActiveWorkbook();
+ if (workbook == null) {
+ return;
+ }
+ const drawingManagerService = injector.get(IDrawingManagerService);
+
+ const oldSelectedDrawings = drawingManagerService.getFocusDrawings() as ISheetImage[];
+ const selectedDrawings = drawings.map((drawing) => {
+ return drawingManagerService.getDrawingByParam(drawing);
+ }) as ISheetImage[];
+
+ const eventParams: IBeforeOverGridImageSelectParam = {
+ workbook,
+ selectedImages: this._createFOverGridImage(selectedDrawings),
+ oldSelectedImages: this._createFOverGridImage(oldSelectedDrawings),
+ };
+
+ this.fireEvent(this.Event.BeforeOverGridImageSelect, eventParams);
+
+ if (eventParams.cancel) {
+ throw new CanceledError();
+ }
+ })
+ )
);
- this.registerEventHandler(
- this.Event.OverGridImageInserted,
- () => commandService.onCommandExecuted((commandInfo) => {
- if (commandInfo.id !== InsertSheetDrawingCommand.id) return;
-
- const params = commandInfo.params as IInsertDrawingCommandParams;
- const workbook = this.getActiveWorkbook();
- if (workbook == null || params == null) {
- return;
- }
-
- const { drawings } = params;
- this.fireEvent(this.Event.OverGridImageInserted, {
- workbook,
- images: this._createFOverGridImage(drawings as ISheetImage[]),
- });
- })
+ this.disposeWithMe(
+ this.registerEventHandler(
+ this.Event.OverGridImageInserted,
+ () => commandService.onCommandExecuted((commandInfo) => {
+ if (commandInfo.id !== InsertSheetDrawingCommand.id) return;
+
+ const params = commandInfo.params as IInsertDrawingCommandParams;
+ const workbook = this.getActiveWorkbook();
+ if (workbook == null || params == null) {
+ return;
+ }
+
+ const { drawings } = params;
+ this.fireEvent(this.Event.OverGridImageInserted, {
+ workbook,
+ images: this._createFOverGridImage(drawings as ISheetImage[]),
+ });
+ })
+ )
);
- this.registerEventHandler(
- this.Event.OverGridImageRemoved,
- () => commandService.onCommandExecuted((commandInfo) => {
- if (commandInfo.id !== RemoveSheetDrawingCommand.id) return;
-
- const params = commandInfo.params as IDeleteDrawingCommandParams;
- const workbook = this.getActiveWorkbook();
- if (workbook == null || params == null) {
- return;
- }
-
- const { drawings } = params;
- this.fireEvent(this.Event.OverGridImageRemoved, {
- workbook,
- removeImageParams: drawings,
- });
- })
+ this.disposeWithMe(
+ this.registerEventHandler(
+ this.Event.OverGridImageRemoved,
+ () => commandService.onCommandExecuted((commandInfo) => {
+ if (commandInfo.id !== RemoveSheetDrawingCommand.id) return;
+
+ const params = commandInfo.params as IDeleteDrawingCommandParams;
+ const workbook = this.getActiveWorkbook();
+ if (workbook == null || params == null) {
+ return;
+ }
+
+ const { drawings } = params;
+ this.fireEvent(this.Event.OverGridImageRemoved, {
+ workbook,
+ removeImageParams: drawings,
+ });
+ })
+ )
);
- this.registerEventHandler(
- this.Event.OverGridImageChanged,
- () => commandService.onCommandExecuted((commandInfo) => {
- if (commandInfo.id !== SetSheetDrawingCommand.id) return;
-
- const params = commandInfo.params as ISetDrawingCommandParams;
- const workbook = this.getActiveWorkbook();
- if (workbook == null || params == null) {
- return;
- }
-
- const { drawings } = params;
- const drawingManagerService = injector.get(IDrawingManagerService);
-
- const images = drawings.map((drawing) => {
- return this._injector.createInstance(FOverGridImage, drawingManagerService.getDrawingByParam(drawing as IDrawingSearch) as ISheetImage);
- });
-
- this.fireEvent(this.Event.OverGridImageChanged, {
- workbook,
- images,
- });
- })
+ this.disposeWithMe(
+ this.registerEventHandler(
+ this.Event.OverGridImageChanged,
+ () => commandService.onCommandExecuted((commandInfo) => {
+ if (commandInfo.id !== SetSheetDrawingCommand.id) return;
+
+ const params = commandInfo.params as ISetDrawingCommandParams;
+ const workbook = this.getActiveWorkbook();
+ if (workbook == null || params == null) {
+ return;
+ }
+
+ const { drawings } = params;
+ const drawingManagerService = injector.get(IDrawingManagerService);
+
+ const images = drawings.map((drawing) => {
+ return this._injector.createInstance(FOverGridImage, drawingManagerService.getDrawingByParam(drawing as IDrawingSearch) as ISheetImage);
+ });
+
+ this.fireEvent(this.Event.OverGridImageChanged, {
+ workbook,
+ images,
+ });
+ })
+ )
);
- this.registerEventHandler(
- this.Event.OverGridImageSelected,
- () => commandService.onCommandExecuted((commandInfo) => {
- if (commandInfo.id !== SetDrawingSelectedOperation.id) return;
-
- const drawings = commandInfo.params as IDrawingSearch[];
- const workbook = this.getActiveWorkbook();
- if (workbook == null) {
- return;
- }
- const drawingManagerService = injector.get(IDrawingManagerService);
-
- const selectedDrawings = drawings.map((drawing) => {
- return drawingManagerService.getDrawingByParam(drawing);
- }) as ISheetImage[];
-
- this.fireEvent(this.Event.OverGridImageSelected, {
- workbook,
- selectedImages: this._createFOverGridImage(selectedDrawings as ISheetImage[]),
- });
- })
+ this.disposeWithMe(
+ this.registerEventHandler(
+ this.Event.OverGridImageSelected,
+ () => commandService.onCommandExecuted((commandInfo) => {
+ if (commandInfo.id !== SetDrawingSelectedOperation.id) return;
+
+ const drawings = commandInfo.params as IDrawingSearch[];
+ const workbook = this.getActiveWorkbook();
+ if (workbook == null) {
+ return;
+ }
+ const drawingManagerService = injector.get(IDrawingManagerService);
+
+ const selectedDrawings = drawings.map((drawing) => {
+ return drawingManagerService.getDrawingByParam(drawing);
+ }) as ISheetImage[];
+
+ this.fireEvent(this.Event.OverGridImageSelected, {
+ workbook,
+ selectedImages: this._createFOverGridImage(selectedDrawings as ISheetImage[]),
+ });
+ })
+ )
);
}
diff --git a/packages/sheets-filter/src/facade/f-event.ts b/packages/sheets-filter/src/facade/f-event.ts
index 0fc23770bc..637984f184 100644
--- a/packages/sheets-filter/src/facade/f-event.ts
+++ b/packages/sheets-filter/src/facade/f-event.ts
@@ -162,41 +162,49 @@ class FUniverSheetsFilterEventMixin extends FUniver {
const commandService = injector.get(ICommandService);
// Register filter criteria set event handlers
- this.registerEventHandler(
- this.Event.SheetBeforeRangeFilter,
- () => commandService.beforeCommandExecuted((commandInfo) => {
- if (commandInfo.id === SetSheetsFilterCriteriaCommand.id) {
- this._beforeRangeFilter(commandInfo as Readonly<ICommandInfo<ISetSheetsFilterCriteriaCommandParams>>);
- }
- })
+ this.disposeWithMe(
+ this.registerEventHandler(
+ this.Event.SheetBeforeRangeFilter,
+ () => commandService.beforeCommandExecuted((commandInfo) => {
+ if (commandInfo.id === SetSheetsFilterCriteriaCommand.id) {
+ this._beforeRangeFilter(commandInfo as Readonly<ICommandInfo<ISetSheetsFilterCriteriaCommandParams>>);
+ }
+ })
+ )
);
- this.registerEventHandler(
- this.Event.SheetBeforeRangeFilterClear,
- () => commandService.beforeCommandExecuted((commandInfo) => {
- if (commandInfo.id === ClearSheetsFilterCriteriaCommand.id) {
- this._beforeRangeFilterClear();
- }
- })
+ this.disposeWithMe(
+ this.registerEventHandler(
+ this.Event.SheetBeforeRangeFilterClear,
+ () => commandService.beforeCommandExecuted((commandInfo) => {
+ if (commandInfo.id === ClearSheetsFilterCriteriaCommand.id) {
+ this._beforeRangeFilterClear();
+ }
+ })
+ )
);
// Register filter criteria execution event handlers
- this.registerEventHandler(
- this.Event.SheetRangeFiltered,
- () => commandService.onCommandExecuted((commandInfo) => {
- if (commandInfo.id === SetSheetsFilterCriteriaCommand.id) {
- this._onRangeFiltered(commandInfo as Readonly<ICommandInfo<ISetSheetsFilterCriteriaCommandParams>>);
- }
- })
+ this.disposeWithMe(
+ this.registerEventHandler(
+ this.Event.SheetRangeFiltered,
+ () => commandService.onCommandExecuted((commandInfo) => {
+ if (commandInfo.id === SetSheetsFilterCriteriaCommand.id) {
+ this._onRangeFiltered(commandInfo as Readonly<ICommandInfo<ISetSheetsFilterCriteriaCommandParams>>);
+ }
+ })
+ )
);
- this.registerEventHandler(
- this.Event.SheetRangeFilterCleared,
- () => commandService.onCommandExecuted((commandInfo) => {
- if (commandInfo.id === ClearSheetsFilterCriteriaCommand.id) {
- this._onRangeFilterCleared();
- }
- })
+ this.disposeWithMe(
+ this.registerEventHandler(
+ this.Event.SheetRangeFilterCleared,
+ () => commandService.onCommandExecuted((commandInfo) => {
+ if (commandInfo.id === ClearSheetsFilterCriteriaCommand.id) {
+ this._onRangeFilterCleared();
+ }
+ })
+ )
);
}
diff --git a/packages/sheets-hyper-link/src/facade/f-univer.ts b/packages/sheets-hyper-link/src/facade/f-univer.ts
index 901d53edce..8af8802396 100644
--- a/packages/sheets-hyper-link/src/facade/f-univer.ts
+++ b/packages/sheets-hyper-link/src/facade/f-univer.ts
@@ -28,74 +28,80 @@ export class FSheetLinkUniver extends FUniver {
override _initialize(injector: Injector): void {
const commandService = injector.get(ICommandService);
- this.registerEventHandler(
- this.Event.BeforeSheetLinkAdd,
- () => commandService.beforeCommandExecuted((commandInfo) => {
- if (commandInfo.id !== AddHyperLinkCommand.id) return;
+ this.disposeWithMe(
+ this.registerEventHandler(
+ this.Event.BeforeSheetLinkAdd,
+ () => commandService.beforeCommandExecuted((commandInfo) => {
+ if (commandInfo.id !== AddHyperLinkCommand.id) return;
- const eventTarget = this.getCommandSheetTarget(commandInfo);
- if (!eventTarget) return;
+ const eventTarget = this.getCommandSheetTarget(commandInfo);
+ if (!eventTarget) return;
- const params = commandInfo.params as IAddHyperLinkCommandParams;
- const eventParams: IBeforeSheetLinkAddEvent = {
- workbook: eventTarget.workbook,
- worksheet: eventTarget.worksheet,
- row: params.link.row,
- col: params.link.column,
- link: params.link,
- };
- this.fireEvent(this.Event.BeforeSheetLinkAdd, eventParams);
- if (eventParams.cancel) {
- throw new CanceledError();
- }
- })
+ const params = commandInfo.params as IAddHyperLinkCommandParams;
+ const eventParams: IBeforeSheetLinkAddEvent = {
+ workbook: eventTarget.workbook,
+ worksheet: eventTarget.worksheet,
+ row: params.link.row,
+ col: params.link.column,
+ link: params.link,
+ };
+ this.fireEvent(this.Event.BeforeSheetLinkAdd, eventParams);
+ if (eventParams.cancel) {
+ throw new CanceledError();
+ }
+ })
+ )
);
- this.registerEventHandler(
- this.Event.BeforeSheetLinkUpdate,
- () => commandService.beforeCommandExecuted((commandInfo) => {
- if (commandInfo.id !== UpdateHyperLinkCommand.id) return;
+ this.disposeWithMe(
+ this.registerEventHandler(
+ this.Event.BeforeSheetLinkUpdate,
+ () => commandService.beforeCommandExecuted((commandInfo) => {
+ if (commandInfo.id !== UpdateHyperLinkCommand.id) return;
- const eventTarget = this.getCommandSheetTarget(commandInfo);
- if (!eventTarget) return;
+ const eventTarget = this.getCommandSheetTarget(commandInfo);
+ if (!eventTarget) return;
- const params = commandInfo.params as IUpdateHyperLinkCommandParams;
- const eventParams: IBeforeSheetLinkUpdateEvent = {
- workbook: eventTarget.workbook,
- worksheet: eventTarget.worksheet,
- row: params.row,
- column: params.column,
- id: params.id,
- payload: params.payload,
- };
- this.fireEvent(this.Event.BeforeSheetLinkUpdate, eventParams);
- if (eventParams.cancel) {
- throw new CanceledError();
- }
- })
+ const params = commandInfo.params as IUpdateHyperLinkCommandParams;
+ const eventParams: IBeforeSheetLinkUpdateEvent = {
+ workbook: eventTarget.workbook,
+ worksheet: eventTarget.worksheet,
+ row: params.row,
+ column: params.column,
+ id: params.id,
+ payload: params.payload,
+ };
+ this.fireEvent(this.Event.BeforeSheetLinkUpdate, eventParams);
+ if (eventParams.cancel) {
+ throw new CanceledError();
+ }
+ })
+ )
);
- this.registerEventHandler(
- this.Event.BeforeSheetLinkCancel,
- () => commandService.beforeCommandExecuted((commandInfo) => {
- if (commandInfo.id !== CancelHyperLinkCommand.id) return;
+ this.disposeWithMe(
+ this.registerEventHandler(
+ this.Event.BeforeSheetLinkCancel,
+ () => commandService.beforeCommandExecuted((commandInfo) => {
+ if (commandInfo.id !== CancelHyperLinkCommand.id) return;
- const eventTarget = this.getCommandSheetTarget(commandInfo);
- if (!eventTarget) return;
+ const eventTarget = this.getCommandSheetTarget(commandInfo);
+ if (!eventTarget) return;
- const params = commandInfo.params as ICancelHyperLinkCommandParams;
- const eventParams: IBeforeSheetLinkCancelEvent = {
- workbook: eventTarget.workbook,
- worksheet: eventTarget.worksheet,
- row: params.row,
- column: params.column,
- id: params.id,
- };
- this.fireEvent(this.Event.BeforeSheetLinkCancel, eventParams);
- if (eventParams.cancel) {
- throw new CanceledError();
- }
- })
+ const params = commandInfo.params as ICancelHyperLinkCommandParams;
+ const eventParams: IBeforeSheetLinkCancelEvent = {
+ workbook: eventTarget.workbook,
+ worksheet: eventTarget.worksheet,
+ row: params.row,
+ column: params.column,
+ id: params.id,
+ };
+ this.fireEvent(this.Event.BeforeSheetLinkCancel, eventParams);
+ if (eventParams.cancel) {
+ throw new CanceledError();
+ }
+ })
+ )
);
}
}
diff --git a/packages/sheets-note/src/facade/f-univer.ts b/packages/sheets-note/src/facade/f-univer.ts
index 0491eb0a79..e64894b5e4 100644
--- a/packages/sheets-note/src/facade/f-univer.ts
+++ b/packages/sheets-note/src/facade/f-univer.ts
@@ -30,135 +30,146 @@ export interface IFUniverSheetNoteMixin {
export class FUniverSheetNoteMixin extends FUniver implements IFUniverSheetNoteMixin {
// eslint-disable-next-line max-lines-per-function
override _initialize(injector: Injector): void {
- this.registerEventHandler(
- this.Event.SheetNoteAdd,
- () => {
- const model = injector.get(SheetsNoteModel);
- return model.change$.subscribe((change) => {
- if (change.type === 'update' && !change.oldNote && change.note) {
- const { unitId, sheetId, row, col, note, oldNote } = change;
- const target = this.getSheetTarget(unitId, sheetId);
- if (!target) {
- return;
- }
- const { workbook, worksheet } = target;
+ const commandService = injector.get(ICommandService);
- this.fireEvent(this.Event.SheetNoteAdd, {
- workbook,
- worksheet,
- row,
- col,
- note,
- });
- }
- });
- }
- );
+ this.disposeWithMe(
+ this.registerEventHandler(
+ this.Event.SheetNoteAdd,
+ () => {
+ const model = injector.get(SheetsNoteModel);
+ return model.change$.subscribe((change) => {
+ if (change.type === 'update' && !change.oldNote && change.note) {
+ const { unitId, sheetId, row, col, note } = change;
+ const target = this.getSheetTarget(unitId, sheetId);
+ if (!target) {
+ return;
+ }
+ const { workbook, worksheet } = target;
- this.registerEventHandler(
- this.Event.SheetNoteDelete,
- () => {
- const model = injector.get(SheetsNoteModel);
- return model.change$.subscribe((change) => {
- if (change.type === 'update' && change.oldNote && !change.note) {
- const { unitId, sheetId, row, col, note, oldNote } = change;
- const target = this.getSheetTarget(unitId, sheetId);
- if (!target) {
- return;
+ this.fireEvent(this.Event.SheetNoteAdd, {
+ workbook,
+ worksheet,
+ row,
+ col,
+ note,
+ });
}
- const { workbook, worksheet } = target;
-
- this.fireEvent(this.Event.SheetNoteDelete, {
- workbook,
- worksheet,
- row,
- col,
- oldNote,
- });
- }
- });
- }
+ });
+ }
+ )
);
- this.registerEventHandler(
- this.Event.SheetNoteUpdate,
- () => {
- const model = injector.get(SheetsNoteModel);
- return model.change$.subscribe((change) => {
- if (change.type === 'update' && change.oldNote && change.note) {
- const { unitId, sheetId, row, col, note, oldNote } = change;
- const target = this.getSheetTarget(unitId, sheetId);
- if (!target) {
- return;
- }
- const { workbook, worksheet } = target;
+ this.disposeWithMe(
+ this.registerEventHandler(
+ this.Event.SheetNoteDelete,
+ () => {
+ const model = injector.get(SheetsNoteModel);
+ return model.change$.subscribe((change) => {
+ if (change.type === 'update' && change.oldNote && !change.note) {
+ const { unitId, sheetId, row, col, oldNote } = change;
+ const target = this.getSheetTarget(unitId, sheetId);
+ if (!target) {
+ return;
+ }
+ const { workbook, worksheet } = target;
- this.fireEvent(this.Event.SheetNoteUpdate, {
- workbook,
- worksheet,
- row,
- col,
- note,
- oldNote,
- });
- }
- });
- }
+ this.fireEvent(this.Event.SheetNoteDelete, {
+ workbook,
+ worksheet,
+ row,
+ col,
+ oldNote,
+ });
+ }
+ });
+ }
+ )
);
- this.registerEventHandler(
- this.Event.SheetNoteShow,
- () => {
- const model = injector.get(SheetsNoteModel);
- return model.change$.subscribe((change) => {
- if (change.type === 'update' && change.oldNote && change.note && !change.oldNote.show && change.note.show) {
- const { unitId, sheetId, row, col } = change;
- const target = this.getSheetTarget(unitId, sheetId);
- if (!target) {
- return;
- }
- const { workbook, worksheet } = target;
+ this.disposeWithMe(
+ this.registerEventHandler(
+ this.Event.SheetNoteUpdate,
+ () => {
+ const model = injector.get(SheetsNoteModel);
+ return model.change$.subscribe((change) => {
+ if (change.type === 'update' && change.oldNote && change.note) {
+ const { unitId, sheetId, row, col, note, oldNote } = change;
+ const target = this.getSheetTarget(unitId, sheetId);
+ if (!target) {
+ return;
+ }
+ const { workbook, worksheet } = target;
- this.fireEvent(this.Event.SheetNoteShow, {
- workbook,
- worksheet,
- row,
- col,
- });
- }
- });
- }
+ this.fireEvent(this.Event.SheetNoteUpdate, {
+ workbook,
+ worksheet,
+ row,
+ col,
+ note,
+ oldNote,
+ });
+ }
+ });
+ }
+ )
);
- this.registerEventHandler(
- this.Event.SheetNoteHide,
- () => {
- const model = injector.get(SheetsNoteModel);
- return model.change$.subscribe((change) => {
- if (change.type === 'update' && change.oldNote && change.note && change.oldNote.show && !change.note.show) {
- const { unitId, sheetId, row, col } = change;
- const target = this.getSheetTarget(unitId, sheetId);
- if (!target) {
- return;
+ this.disposeWithMe(
+ this.registerEventHandler(
+ this.Event.SheetNoteShow,
+ () => {
+ const model = injector.get(SheetsNoteModel);
+ return model.change$.subscribe((change) => {
+ if (change.type === 'update' && change.oldNote && change.note && !change.oldNote.show && change.note.show) {
+ const { unitId, sheetId, row, col } = change;
+ const target = this.getSheetTarget(unitId, sheetId);
+ if (!target) {
+ return;
+ }
+ const { workbook, worksheet } = target;
+
+ this.fireEvent(this.Event.SheetNoteShow, {
+ workbook,
+ worksheet,
+ row,
+ col,
+ });
}
- const { workbook, worksheet } = target;
+ });
+ }
+ )
+ );
- this.fireEvent(this.Event.SheetNoteHide, {
- workbook,
- worksheet,
- row,
- col,
- });
- }
- });
- }
+ this.disposeWithMe(
+ this.registerEventHandler(
+ this.Event.SheetNoteHide,
+ () => {
+ const model = injector.get(SheetsNoteModel);
+ return model.change$.subscribe((change) => {
+ if (change.type === 'update' && change.oldNote && change.note && change.oldNote.show && !change.note.show) {
+ const { unitId, sheetId, row, col } = change;
+ const target = this.getSheetTarget(unitId, sheetId);
+ if (!target) {
+ return;
+ }
+ const { workbook, worksheet } = target;
+
+ this.fireEvent(this.Event.SheetNoteHide, {
+ workbook,
+ worksheet,
+ row,
+ col,
+ });
+ }
+ });
+ }
+ )
);
- this.registerEventHandler(
- this.Event.BeforeSheetNoteAdd,
- () => {
- const commandService = injector.get(ICommandService);
- return commandService.beforeCommandExecuted((command) => {
+ this.disposeWithMe(
+ this.registerEventHandler(
+ this.Event.BeforeSheetNoteAdd,
+ () => commandService.beforeCommandExecuted((command) => {
if (command.id === SheetUpdateNoteCommand.id) {
const model = injector.get(SheetsNoteModel);
const { unitId, sheetId, row, col, note } = command.params as IUpdateNoteMutationParams;
@@ -180,15 +191,14 @@ export class FUniverSheetNoteMixin extends FUniver implements IFUniverSheetNoteM
throw new CanceledError();
}
}
- });
- }
+ })
+ )
);
- this.registerEventHandler(
- this.Event.BeforeSheetNoteDelete,
- () => {
- const commandService = injector.get(ICommandService);
- return commandService.beforeCommandExecuted((command) => {
+ this.disposeWithMe(
+ this.registerEventHandler(
+ this.Event.BeforeSheetNoteDelete,
+ () => commandService.beforeCommandExecuted((command) => {
if (command.id === SheetDeleteNoteCommand.id) {
const model = injector.get(SheetsNoteModel);
const { unitId, sheetId, row, col } = command.params as IRemoveNoteMutationParams;
@@ -210,15 +220,14 @@ export class FUniverSheetNoteMixin extends FUniver implements IFUniverSheetNoteM
throw new CanceledError();
}
}
- });
- }
+ })
+ )
);
- this.registerEventHandler(
- this.Event.BeforeSheetNoteUpdate,
- () => {
- const commandService = injector.get(ICommandService);
- return commandService.beforeCommandExecuted((command) => {
+ this.disposeWithMe(
+ this.registerEventHandler(
+ this.Event.BeforeSheetNoteUpdate,
+ () => commandService.beforeCommandExecuted((command) => {
if (command.id === SheetUpdateNoteCommand.id) {
const model = injector.get(SheetsNoteModel);
const { unitId, sheetId, row, col, note } = command.params as IUpdateNoteMutationParams;
@@ -241,15 +250,14 @@ export class FUniverSheetNoteMixin extends FUniver implements IFUniverSheetNoteM
throw new CanceledError();
}
}
- });
- }
+ })
+ )
);
- this.registerEventHandler(
- this.Event.BeforeSheetNoteShow,
- () => {
- const commandService = injector.get(ICommandService);
- return commandService.beforeCommandExecuted((command) => {
+ this.disposeWithMe(
+ this.registerEventHandler(
+ this.Event.BeforeSheetNoteShow,
+ () => commandService.beforeCommandExecuted((command) => {
if (command.id === SheetToggleNotePopupCommand.id) {
const model = injector.get(SheetsNoteModel);
const { unitId, sheetId, row, col } = command.params as IUpdateNoteMutationParams;
@@ -270,15 +278,14 @@ export class FUniverSheetNoteMixin extends FUniver implements IFUniverSheetNoteM
throw new CanceledError();
}
}
- });
- }
+ })
+ )
);
- this.registerEventHandler(
- this.Event.BeforeSheetNoteHide,
- () => {
- const commandService = injector.get(ICommandService);
- return commandService.beforeCommandExecuted((command) => {
+ this.disposeWithMe(
+ this.registerEventHandler(
+ this.Event.BeforeSheetNoteHide,
+ () => commandService.beforeCommandExecuted((command) => {
if (command.id === SheetToggleNotePopupCommand.id) {
const model = injector.get(SheetsNoteModel);
const { unitId, sheetId, row, col } = command.params as IUpdateNoteMutationParams;
@@ -299,8 +306,8 @@ export class FUniverSheetNoteMixin extends FUniver implements IFUniverSheetNoteM
throw new CanceledError();
}
}
- });
- }
+ })
+ )
);
}
}
diff --git a/packages/sheets-sort/src/facade/f-event.ts b/packages/sheets-sort/src/facade/f-event.ts
index f7bbe22b2f..c072c382ee 100644
--- a/packages/sheets-sort/src/facade/f-event.ts
+++ b/packages/sheets-sort/src/facade/f-event.ts
@@ -95,20 +95,24 @@ class FUniverSheetsSortEventMixin extends FUniver {
override _initialize(injector: Injector): void {
const commandService = injector.get(ICommandService);
- this.registerEventHandler(
- this.Event.SheetBeforeRangeSort,
- () => commandService.beforeCommandExecuted((commandInfo) => {
- if (commandInfo.id !== SortRangeCommand.id) return;
- this._beforeRangeSort(commandInfo as Readonly<ICommandInfo<ISortRangeCommandParams>>);
- })
+ this.disposeWithMe(
+ this.registerEventHandler(
+ this.Event.SheetBeforeRangeSort,
+ () => commandService.beforeCommandExecuted((commandInfo) => {
+ if (commandInfo.id !== SortRangeCommand.id) return;
+ this._beforeRangeSort(commandInfo as Readonly<ICommandInfo<ISortRangeCommandParams>>);
+ })
+ )
);
- this.registerEventHandler(
- this.Event.SheetRangeSorted,
- () => commandService.onCommandExecuted((commandInfo) => {
- if (commandInfo.id !== SortRangeCommand.id) return;
- this._onRangeSorted(commandInfo as Readonly<ICommandInfo<ISortRangeCommandParams>>);
- })
+ this.disposeWithMe(
+ this.registerEventHandler(
+ this.Event.SheetRangeSorted,
+ () => commandService.onCommandExecuted((commandInfo) => {
+ if (commandInfo.id !== SortRangeCommand.id) return;
+ this._onRangeSorted(commandInfo as Readonly<ICommandInfo<ISortRangeCommandParams>>);
+ })
+ )
);
}
diff --git a/packages/sheets-thread-comment/src/facade/f-univer.ts b/packages/sheets-thread-comment/src/facade/f-univer.ts
index 6f923446ea..054df85eaa 100644
--- a/packages/sheets-thread-comment/src/facade/f-univer.ts
+++ b/packages/sheets-thread-comment/src/facade/f-univer.ts
@@ -15,10 +15,12 @@
*/
import type { IDisposable, Injector } from '@univerjs/core';
+import type { FWorkbook, FWorksheet } from '@univerjs/sheets/facade';
import type { IAddCommentCommandParams, IDeleteCommentCommandParams, IResolveCommentCommandParams, IThreadComment, IUpdateCommentCommandParams } from '@univerjs/thread-comment';
import type { IBeforeSheetCommentAddEvent, IBeforeSheetCommentDeleteEvent, IBeforeSheetCommentUpdateEvent, ISheetCommentAddEvent, ISheetCommentDeleteEvent, ISheetCommentResolveEvent, ISheetCommentUpdateEvent } from './f-event';
import { CanceledError, ICommandService, RichTextValue } from '@univerjs/core';
import { FUniver } from '@univerjs/core/facade';
+import { deserializeRangeWithSheet } from '@univerjs/engine-formula';
import { AddCommentCommand, DeleteCommentCommand, DeleteCommentTreeCommand, ResolveCommentCommand, UpdateCommentCommand } from '@univerjs/thread-comment';
import { FTheadCommentBuilder, FTheadCommentItem } from './f-thread-comment';
@@ -75,231 +77,246 @@ export interface IFUniverCommentMixin {
* @ignore
*/
export class FUniverCommentMixin extends FUniver implements IFUniverCommentMixin {
+ private _getTargetSheet(params: { unitId?: string; subUnitId?: string } = {}): {
+ workbook: FWorkbook;
+ worksheet: FWorksheet;
+ } | null {
+ const workbook = params.unitId ? this.getUniverSheet(params.unitId) : this.getActiveWorkbook?.();
+ if (!workbook) return null;
+ const worksheet = params.subUnitId ? workbook.getSheetBySheetId(params.subUnitId) : workbook.getActiveSheet();
+ if (!worksheet) return null;
+
+ return {
+ workbook,
+ worksheet,
+ };
+ }
+
// eslint-disable-next-line max-lines-per-function
override _initialize(injector: Injector): void {
const commandService = injector.get(ICommandService);
+
// After command events
- this.registerEventHandler(
- this.Event.CommentAdded,
- () => commandService.onCommandExecuted((commandInfo) => {
- if (commandInfo.id !== AddCommentCommand.id) return;
- const params = commandInfo.params as { unitId: string; subUnitId: string; sheetId: string };
- if (!params) return;
- const workbook = params.unitId ? this.getUniverSheet(params.unitId) : this.getActiveWorkbook?.();
- if (!workbook) return;
- const worksheet = workbook.getSheetBySheetId(params.subUnitId || params.sheetId) || workbook.getActiveSheet();
- if (!worksheet) return;
-
- const addParams = commandInfo.params as IAddCommentCommandParams;
- const { comment } = addParams;
- const threadComment = worksheet.getRange(comment.ref).getComment();
- if (threadComment) {
- this.fireEvent(this.Event.CommentAdded, {
- workbook,
- worksheet,
- row: threadComment.getRange()?.getRow() ?? 0,
- col: threadComment.getRange()?.getColumn() ?? 0,
- comment: threadComment,
- });
- }
- })
- );
+ this.disposeWithMe(
+ this.registerEventHandler(
+ this.Event.CommentAdded,
+ () => commandService.onCommandExecuted((commandInfo) => {
+ if (commandInfo.id !== AddCommentCommand.id) return;
- this.registerEventHandler(
- this.Event.CommentUpdated,
- () => commandService.onCommandExecuted((commandInfo) => {
- if (commandInfo.id !== UpdateCommentCommand.id) return;
- const params = commandInfo.params as { unitId: string; subUnitId: string; sheetId: string };
- if (!params) return;
- const workbook = params.unitId ? this.getUniverSheet(params.unitId) : this.getActiveWorkbook?.();
- if (!workbook) return;
- const worksheet = workbook.getSheetBySheetId(params.subUnitId || params.sheetId) || workbook.getActiveSheet();
- if (!worksheet) return;
-
- const updateParams = commandInfo.params as IUpdateCommentCommandParams;
- const { commentId } = updateParams.payload;
- const threadComment = worksheet.getCommentById(commentId);
- if (threadComment) {
- this.fireEvent(this.Event.CommentUpdated, {
- workbook,
- worksheet,
- row: threadComment.getRange()?.getRow() ?? 0,
- col: threadComment.getRange()?.getColumn() ?? 0,
- comment: threadComment,
- });
- }
- })
+ const target = this._getTargetSheet(commandInfo.params);
+ if (!target) return;
+
+ const { workbook, worksheet } = target;
+ const { comment } = commandInfo.params as IAddCommentCommandParams;
+ const threadComment = worksheet.getCommentById(comment.id);
+
+ if (threadComment) {
+ this.fireEvent(this.Event.CommentAdded, {
+ workbook,
+ worksheet,
+ row: threadComment.getRange()?.getRow() ?? 0,
+ col: threadComment.getRange()?.getColumn() ?? 0,
+ comment: threadComment,
+ });
+ }
+ })
+ )
);
- this.registerEventHandler(
- this.Event.CommentDeleted,
- () => commandService.onCommandExecuted((commandInfo) => {
- if (commandInfo.id !== DeleteCommentCommand.id && commandInfo.id !== DeleteCommentTreeCommand.id) return;
- const params = commandInfo.params as { unitId: string; subUnitId: string; sheetId: string };
- if (!params) return;
- const workbook = params.unitId ? this.getUniverSheet(params.unitId) : this.getActiveWorkbook?.();
- if (!workbook) return;
- const worksheet = workbook.getSheetBySheetId(params.subUnitId || params.sheetId) || workbook.getActiveSheet();
- if (!worksheet) return;
-
- const deleteParams = commandInfo.params as IDeleteCommentCommandParams;
- const { commentId } = deleteParams;
- this.fireEvent(this.Event.CommentDeleted, {
- workbook,
- worksheet,
- commentId,
- });
- })
+ this.disposeWithMe(
+ this.registerEventHandler(
+ this.Event.CommentUpdated,
+ () => commandService.onCommandExecuted((commandInfo) => {
+ if (commandInfo.id !== UpdateCommentCommand.id) return;
+
+ const target = this._getTargetSheet(commandInfo.params);
+ if (!target) return;
+
+ const { workbook, worksheet } = target;
+ const { payload } = commandInfo.params as IUpdateCommentCommandParams;
+ const threadComment = worksheet.getCommentById(payload.commentId);
+
+ if (threadComment) {
+ this.fireEvent(this.Event.CommentUpdated, {
+ workbook,
+ worksheet,
+ row: threadComment.getRange()?.getRow() ?? 0,
+ col: threadComment.getRange()?.getColumn() ?? 0,
+ comment: threadComment,
+ });
+ }
+ })
+ )
);
- this.registerEventHandler(
- this.Event.CommentResolved,
- () => commandService.onCommandExecuted((commandInfo) => {
- if (commandInfo.id !== ResolveCommentCommand.id) return;
- const params = commandInfo.params as { unitId: string; subUnitId: string; sheetId: string };
- if (!params) return;
- const workbook = params.unitId ? this.getUniverSheet(params.unitId) : this.getActiveWorkbook?.();
- if (!workbook) return;
- const worksheet = workbook.getSheetBySheetId(params.subUnitId || params.sheetId) || workbook.getActiveSheet();
- if (!worksheet) return;
-
- const resolveParams = commandInfo.params as IResolveCommentCommandParams;
- const { commentId, resolved } = resolveParams;
- const threadComment = worksheet.getComments().find((c) => c.getCommentData().id === commentId);
- if (threadComment) {
- this.fireEvent(this.Event.CommentResolved, {
+ this.disposeWithMe(
+ this.registerEventHandler(
+ this.Event.CommentDeleted,
+ () => commandService.onCommandExecuted((commandInfo) => {
+ if (commandInfo.id !== DeleteCommentCommand.id && commandInfo.id !== DeleteCommentTreeCommand.id) return;
+
+ const target = this._getTargetSheet(commandInfo.params);
+ if (!target) return;
+
+ const { workbook, worksheet } = target;
+ const { commentId } = commandInfo.params as IDeleteCommentCommandParams;
+
+ this.fireEvent(this.Event.CommentDeleted, {
workbook,
worksheet,
- row: threadComment.getRange()!.getRow() ?? 0,
- col: threadComment.getRange()!.getColumn() ?? 0,
- comment: threadComment,
- resolved,
+ commentId,
});
- }
- })
+ })
+ )
);
- // Before command events
- this.registerEventHandler(
- this.Event.BeforeCommentAdd,
- () => commandService.beforeCommandExecuted((commandInfo) => {
- if (commandInfo.id !== AddCommentCommand.id) return;
- const params = commandInfo.params as { unitId: string; subUnitId: string; sheetId: string };
- if (!params) return;
- const workbook = params.unitId ? this.getUniverSheet(params.unitId) : this.getActiveWorkbook?.();
- if (!workbook) return;
- const worksheet = workbook.getSheetBySheetId(params.subUnitId || params.sheetId) || workbook.getActiveSheet();
- if (!worksheet) return;
-
- const addParams = commandInfo.params as IAddCommentCommandParams;
- const { comment } = addParams;
- const activeRange = worksheet.getActiveRange();
- if (!activeRange) return;
- const eventParams: IBeforeSheetCommentAddEvent = {
- workbook,
- worksheet,
- row: activeRange.getRow() ?? 0,
- col: activeRange.getColumn() ?? 0,
- comment: FTheadCommentItem.create(comment),
- };
-
- this.fireEvent(this.Event.BeforeCommentAdd, eventParams);
- if (eventParams.cancel) {
- throw new CanceledError();
- }
- })
+ this.disposeWithMe(
+ this.registerEventHandler(
+ this.Event.CommentResolved,
+ () => commandService.onCommandExecuted((commandInfo) => {
+ if (commandInfo.id !== ResolveCommentCommand.id) return;
+
+ const target = this._getTargetSheet(commandInfo.params);
+ if (!target) return;
+
+ const { workbook, worksheet } = target;
+ const { commentId, resolved } = commandInfo.params as IResolveCommentCommandParams;
+ const threadComment = worksheet.getCommentById(commentId);
+
+ if (threadComment) {
+ this.fireEvent(this.Event.CommentResolved, {
+ workbook,
+ worksheet,
+ row: threadComment.getRange()?.getRow() ?? 0,
+ col: threadComment.getRange()?.getColumn() ?? 0,
+ comment: threadComment,
+ resolved,
+ });
+ }
+ })
+ )
);
- this.registerEventHandler(
- this.Event.BeforeCommentUpdate,
- () => commandService.beforeCommandExecuted((commandInfo) => {
- if (commandInfo.id !== UpdateCommentCommand.id) return;
- const params = commandInfo.params as { unitId: string; subUnitId: string; sheetId: string };
- if (!params) return;
- const workbook = params.unitId ? this.getUniverSheet(params.unitId) : this.getActiveWorkbook?.();
- if (!workbook) return;
- const worksheet = workbook.getSheetBySheetId(params.subUnitId || params.sheetId) || workbook.getActiveSheet();
- if (!worksheet) return;
-
- const updateParams = commandInfo.params as IUpdateCommentCommandParams;
- const { commentId, text } = updateParams.payload;
- const threadComment = worksheet.getCommentById(commentId);
- if (threadComment) {
- const eventParams: IBeforeSheetCommentUpdateEvent = {
+ // Before command events
+ this.disposeWithMe(
+ this.registerEventHandler(
+ this.Event.BeforeCommentAdd,
+ () => commandService.beforeCommandExecuted((commandInfo) => {
+ if (commandInfo.id !== AddCommentCommand.id) return;
+
+ const target = this._getTargetSheet(commandInfo.params);
+ if (!target) return;
+
+ const { workbook, worksheet } = target;
+ const { comment } = commandInfo.params as IAddCommentCommandParams;
+ const { range } = deserializeRangeWithSheet(comment.ref);
+
+ const eventParams: IBeforeSheetCommentAddEvent = {
workbook,
worksheet,
- row: threadComment.getRange()?.getRow() ?? 0,
- col: threadComment.getRange()?.getColumn() ?? 0,
- comment: threadComment,
- newContent: RichTextValue.createByBody(text),
+ row: range.startRow,
+ col: range.startColumn,
+ comment: FTheadCommentItem.create(comment),
};
- this.fireEvent(this.Event.BeforeCommentUpdate, eventParams);
+
+ this.fireEvent(this.Event.BeforeCommentAdd, eventParams);
if (eventParams.cancel) {
throw new CanceledError();
}
- }
- })
+ })
+ )
);
- this.registerEventHandler(
- this.Event.BeforeCommentDelete,
- () => commandService.beforeCommandExecuted((commandInfo) => {
- if (commandInfo.id !== DeleteCommentCommand.id && commandInfo.id !== DeleteCommentTreeCommand.id) return;
- const params = commandInfo.params as { unitId: string; subUnitId: string; sheetId: string };
- if (!params) return;
- const workbook = params.unitId ? this.getUniverSheet(params.unitId) : this.getActiveWorkbook?.();
- if (!workbook) return;
- const worksheet = workbook.getSheetBySheetId(params.subUnitId || params.sheetId) || workbook.getActiveSheet();
- if (!worksheet) return;
-
- const deleteParams = commandInfo.params as IDeleteCommentCommandParams;
- const { commentId } = deleteParams;
- const threadComment = worksheet.getCommentById(commentId);
- if (threadComment) {
- const eventParams: IBeforeSheetCommentDeleteEvent = {
- workbook,
- worksheet,
- row: threadComment.getRange()?.getRow() ?? 0,
- col: threadComment.getRange()?.getColumn() ?? 0,
- comment: threadComment,
- };
- this.fireEvent(this.Event.BeforeCommentDelete, eventParams);
- if (eventParams.cancel) {
- throw new CanceledError();
+ this.disposeWithMe(
+ this.registerEventHandler(
+ this.Event.BeforeCommentUpdate,
+ () => commandService.beforeCommandExecuted((commandInfo) => {
+ if (commandInfo.id !== UpdateCommentCommand.id) return;
+
+ const target = this._getTargetSheet(commandInfo.params);
+ if (!target) return;
+
+ const { workbook, worksheet } = target;
+ const { payload } = commandInfo.params as IUpdateCommentCommandParams;
+ const threadComment = worksheet.getCommentById(payload.commentId);
+
+ if (threadComment) {
+ const eventParams: IBeforeSheetCommentUpdateEvent = {
+ workbook,
+ worksheet,
+ row: threadComment.getRange()?.getRow() ?? 0,
+ col: threadComment.getRange()?.getColumn() ?? 0,
+ comment: threadComment,
+ newContent: RichTextValue.createByBody(payload.text),
+ };
+ this.fireEvent(this.Event.BeforeCommentUpdate, eventParams);
+ if (eventParams.cancel) {
+ throw new CanceledError();
+ }
}
- }
- })
+ })
+ )
);
- this.registerEventHandler(
- this.Event.BeforeCommentResolve,
- () => commandService.beforeCommandExecuted((commandInfo) => {
- if (commandInfo.id !== ResolveCommentCommand.id) return;
- const params = commandInfo.params as { unitId: string; subUnitId: string; sheetId: string };
- if (!params) return;
- const workbook = params.unitId ? this.getUniverSheet(params.unitId) : this.getActiveWorkbook?.();
- if (!workbook) return;
- const worksheet = workbook.getSheetBySheetId(params.subUnitId || params.sheetId) || workbook.getActiveSheet();
- if (!worksheet) return;
-
- const resolveParams = commandInfo.params as IResolveCommentCommandParams;
- const { commentId, resolved } = resolveParams;
- const threadComment = worksheet.getComments().find((c) => c.getCommentData().id === commentId);
- if (threadComment) {
- const eventParams: ISheetCommentResolveEvent = {
- workbook,
- worksheet,
- row: threadComment.getRange()!.getRow() ?? 0,
- col: threadComment.getRange()!.getColumn() ?? 0,
- comment: threadComment,
- resolved,
- };
- this.fireEvent(this.Event.BeforeCommentResolve, eventParams);
- if (eventParams.cancel) {
- throw new CanceledError();
+ this.disposeWithMe(
+ this.registerEventHandler(
+ this.Event.BeforeCommentDelete,
+ () => commandService.beforeCommandExecuted((commandInfo) => {
+ if (commandInfo.id !== DeleteCommentCommand.id && commandInfo.id !== DeleteCommentTreeCommand.id) return;
+
+ const target = this._getTargetSheet(commandInfo.params);
+ if (!target) return;
+
+ const { workbook, worksheet } = target;
+ const { commentId } = commandInfo.params as IDeleteCommentCommandParams;
+ const threadComment = worksheet.getCommentById(commentId);
+
+ if (threadComment) {
+ const eventParams: IBeforeSheetCommentDeleteEvent = {
+ workbook,
+ worksheet,
+ row: threadComment.getRange()?.getRow() ?? 0,
+ col: threadComment.getRange()?.getColumn() ?? 0,
+ comment: threadComment,
+ };
+ this.fireEvent(this.Event.BeforeCommentDelete, eventParams);
+ if (eventParams.cancel) {
+ throw new CanceledError();
+ }
+ }
+ })
+ )
+ );
+
+ this.disposeWithMe(
+ this.registerEventHandler(
+ this.Event.BeforeCommentResolve,
+ () => commandService.beforeCommandExecuted((commandInfo) => {
+ if (commandInfo.id !== ResolveCommentCommand.id) return;
+
+ const target = this._getTargetSheet(commandInfo.params);
+ if (!target) return;
+
+ const { workbook, worksheet } = target;
+ const { commentId, resolved } = commandInfo.params as IResolveCommentCommandParams;
+ const threadComment = worksheet.getCommentById(commentId);
+
+ if (threadComment) {
+ const eventParams: ISheetCommentResolveEvent = {
+ workbook,
+ worksheet,
+ row: threadComment.getRange()!.getRow() ?? 0,
+ col: threadComment.getRange()!.getColumn() ?? 0,
+ comment: threadComment,
+ resolved,
+ };
+ this.fireEvent(this.Event.BeforeCommentResolve, eventParams);
+ if (eventParams.cancel) {
+ throw new CanceledError();
+ }
}
- }
- })
+ })
+ )
);
}
diff --git a/packages/sheets-ui/src/facade/f-univer.ts b/packages/sheets-ui/src/facade/f-univer.ts
index 62e2713003..8d100bb4a7 100644
--- a/packages/sheets-ui/src/facade/f-univer.ts
+++ b/packages/sheets-ui/src/facade/f-univer.ts
@@ -177,454 +177,500 @@ export class FUniverSheetsUIMixin extends FUniver implements IFUniverSheetsUIMix
const commandService = injector.get(ICommandService);
// Edit events
- this.registerEventHandler(
- this.Event.BeforeSheetEditStart,
- () => commandService.beforeCommandExecuted((commandInfo) => {
- if (commandInfo.id !== SetCellEditVisibleOperation.id) return;
-
- const target = this.getActiveSheet();
- if (!target) return;
-
- const { workbook, worksheet } = target;
- const editorBridgeService = injector.get(IEditorBridgeService);
- const params = commandInfo.params as IEditorBridgeServiceVisibleParam;
- const { visible, keycode, eventType } = params;
- const loc = editorBridgeService.getEditLocation()!;
-
- if (visible) {
- const eventParams: IBeforeSheetEditStartEventParams = {
- row: loc.row,
- column: loc.column,
- eventType,
- keycode,
- workbook,
- worksheet,
- isZenEditor: false,
- };
- this.fireEvent(this.Event.BeforeSheetEditStart, eventParams);
- if (eventParams.cancel) {
- throw new CanceledError();
+ this.disposeWithMe(
+ this.registerEventHandler(
+ this.Event.BeforeSheetEditStart,
+ () => commandService.beforeCommandExecuted((commandInfo) => {
+ if (commandInfo.id !== SetCellEditVisibleOperation.id) return;
+
+ const target = this.getActiveSheet();
+ if (!target) return;
+
+ const { workbook, worksheet } = target;
+ const editorBridgeService = injector.get(IEditorBridgeService);
+ const params = commandInfo.params as IEditorBridgeServiceVisibleParam;
+ const { visible, keycode, eventType } = params;
+ const loc = editorBridgeService.getEditLocation()!;
+
+ if (visible) {
+ const eventParams: IBeforeSheetEditStartEventParams = {
+ row: loc.row,
+ column: loc.column,
+ eventType,
+ keycode,
+ workbook,
+ worksheet,
+ isZenEditor: false,
+ };
+ this.fireEvent(this.Event.BeforeSheetEditStart, eventParams);
+ if (eventParams.cancel) {
+ throw new CanceledError();
+ }
}
- }
- })
+ })
+ )
);
- this.registerEventHandler(
- this.Event.BeforeSheetEditEnd,
- () => commandService.beforeCommandExecuted((commandInfo) => {
- if (commandInfo.id !== SetCellEditVisibleOperation.id) return;
-
- const target = this.getActiveSheet();
- if (!target) return;
-
- const { workbook, worksheet } = target;
- const editorBridgeService = injector.get(IEditorBridgeService);
- const univerInstanceService = injector.get(IUniverInstanceService);
- const params = commandInfo.params as IEditorBridgeServiceVisibleParam;
- const { visible, keycode, eventType } = params;
- const loc = editorBridgeService.getEditLocation()!;
-
- if (!visible) {
- const eventParams: IBeforeSheetEditEndEventParams = {
- row: loc.row,
- column: loc.column,
- eventType,
- keycode,
- workbook,
- worksheet,
- isZenEditor: false,
- value: RichTextValue.create(univerInstanceService.getUnit<DocumentDataModel>(DOCS_NORMAL_EDITOR_UNIT_ID_KEY)!.getSnapshot()),
- isConfirm: keycode !== KeyCode.ESC,
- };
- this.fireEvent(this.Event.BeforeSheetEditEnd, eventParams);
- if (eventParams.cancel) {
- throw new CanceledError();
+ this.disposeWithMe(
+ this.registerEventHandler(
+ this.Event.BeforeSheetEditEnd,
+ () => commandService.beforeCommandExecuted((commandInfo) => {
+ if (commandInfo.id !== SetCellEditVisibleOperation.id) return;
+
+ const target = this.getActiveSheet();
+ if (!target) return;
+
+ const { workbook, worksheet } = target;
+ const editorBridgeService = injector.get(IEditorBridgeService);
+ const univerInstanceService = injector.get(IUniverInstanceService);
+ const params = commandInfo.params as IEditorBridgeServiceVisibleParam;
+ const { visible, keycode, eventType } = params;
+ const loc = editorBridgeService.getEditLocation()!;
+
+ if (!visible) {
+ const eventParams: IBeforeSheetEditEndEventParams = {
+ row: loc.row,
+ column: loc.column,
+ eventType,
+ keycode,
+ workbook,
+ worksheet,
+ isZenEditor: false,
+ value: RichTextValue.create(univerInstanceService.getUnit<DocumentDataModel>(DOCS_NORMAL_EDITOR_UNIT_ID_KEY)!.getSnapshot()),
+ isConfirm: keycode !== KeyCode.ESC,
+ };
+ this.fireEvent(this.Event.BeforeSheetEditEnd, eventParams);
+ if (eventParams.cancel) {
+ throw new CanceledError();
+ }
}
- }
- })
+ })
+ )
);
- this.registerEventHandler(
- this.Event.SheetEditStarted,
- () => commandService.onCommandExecuted((commandInfo) => {
- if (commandInfo.id !== SetCellEditVisibleOperation.id) return;
-
- const target = this.getCommandSheetTarget(commandInfo);
- if (!target) return;
-
- const { workbook, worksheet } = target;
- const editorBridgeService = injector.get(IEditorBridgeService);
- const params = commandInfo.params as IEditorBridgeServiceVisibleParam;
- const { visible, keycode, eventType } = params;
- const loc = editorBridgeService.getEditLocation()!;
-
- if (visible) {
- const eventParams: ISheetEditStartedEventParams = {
- row: loc.row,
- column: loc.column,
- eventType,
- keycode,
- workbook,
- worksheet,
- isZenEditor: false,
- };
- this.fireEvent(this.Event.SheetEditStarted, eventParams);
- }
- })
+ this.disposeWithMe(
+ this.registerEventHandler(
+ this.Event.SheetEditStarted,
+ () => commandService.onCommandExecuted((commandInfo) => {
+ if (commandInfo.id !== SetCellEditVisibleOperation.id) return;
+
+ const target = this.getCommandSheetTarget(commandInfo);
+ if (!target) return;
+
+ const { workbook, worksheet } = target;
+ const editorBridgeService = injector.get(IEditorBridgeService);
+ const params = commandInfo.params as IEditorBridgeServiceVisibleParam;
+ const { visible, keycode, eventType } = params;
+ const loc = editorBridgeService.getEditLocation()!;
+
+ if (visible) {
+ const eventParams: ISheetEditStartedEventParams = {
+ row: loc.row,
+ column: loc.column,
+ eventType,
+ keycode,
+ workbook,
+ worksheet,
+ isZenEditor: false,
+ };
+ this.fireEvent(this.Event.SheetEditStarted, eventParams);
+ }
+ })
+ )
);
- this.registerEventHandler(
- this.Event.SheetEditEnded,
- () => commandService.onCommandExecuted((commandInfo) => {
- if (commandInfo.id !== SetCellEditVisibleOperation.id) return;
-
- const target = this.getCommandSheetTarget(commandInfo);
- if (!target) return;
-
- const { workbook, worksheet } = target;
- const editorBridgeService = injector.get(IEditorBridgeService);
- const params = commandInfo.params as IEditorBridgeServiceVisibleParam;
- const { visible, keycode, eventType } = params;
- const loc = editorBridgeService.getEditLocation()!;
-
- if (!visible) {
- const eventParams: ISheetEditEndedEventParams = {
- row: loc.row,
- column: loc.column,
- eventType,
- keycode,
- workbook,
- worksheet,
- isZenEditor: false,
- isConfirm: keycode !== KeyCode.ESC,
- };
- this.fireEvent(this.Event.SheetEditEnded, eventParams);
- }
- })
+ this.disposeWithMe(
+ this.registerEventHandler(
+ this.Event.SheetEditEnded,
+ () => commandService.onCommandExecuted((commandInfo) => {
+ if (commandInfo.id !== SetCellEditVisibleOperation.id) return;
+
+ const target = this.getCommandSheetTarget(commandInfo);
+ if (!target) return;
+
+ const { workbook, worksheet } = target;
+ const editorBridgeService = injector.get(IEditorBridgeService);
+ const params = commandInfo.params as IEditorBridgeServiceVisibleParam;
+ const { visible, keycode, eventType } = params;
+ const loc = editorBridgeService.getEditLocation()!;
+
+ if (!visible) {
+ const eventParams: ISheetEditEndedEventParams = {
+ row: loc.row,
+ column: loc.column,
+ eventType,
+ keycode,
+ workbook,
+ worksheet,
+ isZenEditor: false,
+ isConfirm: keycode !== KeyCode.ESC,
+ };
+ this.fireEvent(this.Event.SheetEditEnded, eventParams);
+ }
+ })
+ )
);
- this.registerEventHandler(
- this.Event.SheetEditChanging,
- () => commandService.onCommandExecuted((commandInfo) => {
- if (commandInfo.id !== RichTextEditingMutation.id) return;
+ this.disposeWithMe(
+ this.registerEventHandler(
+ this.Event.SheetEditChanging,
+ () => commandService.onCommandExecuted((commandInfo) => {
+ if (commandInfo.id !== RichTextEditingMutation.id) return;
+
+ const target = this.getActiveSheet();
+ if (!target) return;
+
+ const { workbook, worksheet } = target;
+ const editorBridgeService = injector.get(IEditorBridgeService);
+ const univerInstanceService = injector.get(IUniverInstanceService);
+ const params = commandInfo.params as IRichTextEditingMutationParams;
+ if (!editorBridgeService.isVisible().visible) return;
+
+ const { unitId } = params;
+ if (unitId === DOCS_NORMAL_EDITOR_UNIT_ID_KEY) {
+ const { row, column } = editorBridgeService.getEditLocation()!;
+ const eventParams: ISheetEditChangingEventParams = {
+ workbook,
+ worksheet,
+ row,
+ column,
+ value: RichTextValue.create(univerInstanceService.getUnit<DocumentDataModel>(DOCS_NORMAL_EDITOR_UNIT_ID_KEY)!.getSnapshot()),
+ isZenEditor: false,
+ };
+ this.fireEvent(this.Event.SheetEditChanging, eventParams);
+ }
+ })
+ )
+ );
- const target = this.getActiveSheet();
- if (!target) return;
+ // Zoom events
+ this.disposeWithMe(
+ this.registerEventHandler(
+ this.Event.BeforeSheetZoomChange,
+ () => commandService.beforeCommandExecuted((commandInfo) => {
+ if (commandInfo.id !== SetZoomRatioCommand.id) return;
- const { workbook, worksheet } = target;
- const editorBridgeService = injector.get(IEditorBridgeService);
- const univerInstanceService = injector.get(IUniverInstanceService);
- const params = commandInfo.params as IRichTextEditingMutationParams;
- if (!editorBridgeService.isVisible().visible) return;
+ const target = this.getCommandSheetTarget(commandInfo);
+ if (!target) return;
- const { unitId } = params;
- if (unitId === DOCS_NORMAL_EDITOR_UNIT_ID_KEY) {
- const { row, column } = editorBridgeService.getEditLocation()!;
- const eventParams: ISheetEditChangingEventParams = {
+ const { workbook, worksheet } = target;
+ const eventParams: ISheetZoomEvent = {
+ zoom: (commandInfo.params as ISetZoomRatioCommandParams).zoomRatio,
workbook,
worksheet,
- row,
- column,
- value: RichTextValue.create(univerInstanceService.getUnit<DocumentDataModel>(DOCS_NORMAL_EDITOR_UNIT_ID_KEY)!.getSnapshot()),
- isZenEditor: false,
};
- this.fireEvent(this.Event.SheetEditChanging, eventParams);
- }
- })
+ this.fireEvent(this.Event.BeforeSheetZoomChange, eventParams);
+ if (eventParams.cancel) {
+ throw new CanceledError();
+ }
+ })
+ )
);
- // Zoom events
- this.registerEventHandler(
- this.Event.BeforeSheetZoomChange,
- () => commandService.beforeCommandExecuted((commandInfo) => {
- if (commandInfo.id !== SetZoomRatioCommand.id) return;
-
- const target = this.getCommandSheetTarget(commandInfo);
- if (!target) return;
-
- const { workbook, worksheet } = target;
- const eventParams: ISheetZoomEvent = {
- zoom: (commandInfo.params as ISetZoomRatioCommandParams).zoomRatio,
- workbook,
- worksheet,
- };
- this.fireEvent(this.Event.BeforeSheetZoomChange, eventParams);
- if (eventParams.cancel) {
- throw new CanceledError();
- }
- })
- );
+ this.disposeWithMe(
+ this.registerEventHandler(
+ this.Event.SheetZoomChanged,
+ () => commandService.onCommandExecuted((commandInfo) => {
+ if (commandInfo.id !== SetZoomRatioCommand.id) return;
+
+ const target = this.getCommandSheetTarget(commandInfo);
+ if (!target) return;
- this.registerEventHandler(
- this.Event.SheetZoomChanged,
- () => commandService.onCommandExecuted((commandInfo) => {
- if (commandInfo.id !== SetZoomRatioCommand.id) return;
-
- const target = this.getCommandSheetTarget(commandInfo);
- if (!target) return;
-
- const { workbook, worksheet } = target;
- this.fireEvent(this.Event.SheetZoomChanged, {
- zoom: worksheet.getZoom(),
- workbook,
- worksheet,
- });
- })
+ const { workbook, worksheet } = target;
+ this.fireEvent(this.Event.SheetZoomChanged, {
+ zoom: worksheet.getZoom(),
+ workbook,
+ worksheet,
+ });
+ })
+ )
);
}
// eslint-disable-next-line max-lines-per-function
private _initObserverListener(injector: Injector): void {
const renderManagerService = injector.get(IRenderManagerService);
-
const lifeCycleService = injector.get(LifecycleService);
- const disposable = new DisposableCollection();
+ const lifecycle$Disposable = new DisposableCollection();
// eslint-disable-next-line max-lines-per-function
this.disposeWithMe(lifeCycleService.lifecycle$.subscribe((lifecycle) => {
if (lifecycle !== LifecycleStages.Rendered) return;
- disposable.dispose();
const hoverManagerService = injector.get(HoverManagerService);
const dragManagerService = injector.get(DragManagerService);
if (!hoverManagerService) return;
+ // Prevent multiple registrations
+ lifecycle$Disposable.dispose();
+
// Cell events
- this.registerEventHandler(
- this.Event.CellClicked,
- () => hoverManagerService.currentClickedCell$
- ?.pipe(filter((cell) => !!cell))
- .subscribe((cell) => {
- const baseParams = this.getSheetTarget(cell.location.unitId, cell.location.subUnitId);
- if (!baseParams) return;
- this.fireEvent(this.Event.CellClicked, {
- ...baseParams,
- ...cell,
- row: cell.location.row,
- column: cell.location.col,
- });
- })
+ lifecycle$Disposable.add(
+ this.registerEventHandler(
+ this.Event.CellClicked,
+ () => hoverManagerService.currentClickedCell$
+ ?.pipe(filter((cell) => !!cell))
+ .subscribe((cell) => {
+ const baseParams = this.getSheetTarget(cell.location.unitId, cell.location.subUnitId);
+ if (!baseParams) return;
+ this.fireEvent(this.Event.CellClicked, {
+ ...baseParams,
+ ...cell,
+ row: cell.location.row,
+ column: cell.location.col,
+ });
+ })
+ )
);
- this.registerEventHandler(
- this.Event.CellHover,
- () => hoverManagerService.currentRichText$
- ?.pipe(filter((cell) => !!cell))
- .subscribe((cell) => {
- const baseParams = this.getSheetTarget(cell.unitId, cell.subUnitId);
- if (!baseParams) return;
- this.fireEvent(this.Event.CellHover, {
- ...baseParams,
- ...cell,
- row: cell.row,
- column: cell.col,
- });
- })
+ lifecycle$Disposable.add(
+ this.registerEventHandler(
+ this.Event.CellHover,
+ () => hoverManagerService.currentRichText$
+ ?.pipe(filter((cell) => !!cell))
+ .subscribe((cell) => {
+ const baseParams = this.getSheetTarget(cell.unitId, cell.subUnitId);
+ if (!baseParams) return;
+ this.fireEvent(this.Event.CellHover, {
+ ...baseParams,
+ ...cell,
+ row: cell.row,
+ column: cell.col,
+ });
+ })
+ )
);
- this.registerEventHandler(
- this.Event.CellPointerDown,
- () => hoverManagerService.currentPointerDownCell$
- ?.pipe(filter((cell) => !!cell))
- .subscribe((cell) => {
- const baseParams = this.getSheetTarget(cell.unitId, cell.subUnitId);
- if (!baseParams) return;
- this.fireEvent(this.Event.CellPointerDown, {
- ...baseParams,
- ...cell,
- row: cell.row,
- column: cell.col,
- });
- })
+ lifecycle$Disposable.add(
+ this.registerEventHandler(
+ this.Event.CellPointerDown,
+ () => hoverManagerService.currentPointerDownCell$
+ ?.pipe(filter((cell) => !!cell))
+ .subscribe((cell) => {
+ const baseParams = this.getSheetTarget(cell.unitId, cell.subUnitId);
+ if (!baseParams) return;
+ this.fireEvent(this.Event.CellPointerDown, {
+ ...baseParams,
+ ...cell,
+ row: cell.row,
+ column: cell.col,
+ });
+ })
+ )
);
- this.registerEventHandler(
- this.Event.CellPointerUp,
- () => hoverManagerService.currentPointerUpCell$
- ?.pipe(filter((cell) => !!cell))
- .subscribe((cell) => {
- const baseParams = this.getSheetTarget(cell.unitId, cell.subUnitId);
- if (!baseParams) return;
- this.fireEvent(this.Event.CellPointerUp, {
- ...baseParams,
- ...cell,
- row: cell.row,
- column: cell.col,
- });
- })
+ lifecycle$Disposable.add(
+ this.registerEventHandler(
+ this.Event.CellPointerUp,
+ () => hoverManagerService.currentPointerUpCell$
+ ?.pipe(filter((cell) => !!cell))
+ .subscribe((cell) => {
+ const baseParams = this.getSheetTarget(cell.unitId, cell.subUnitId);
+ if (!baseParams) return;
+ this.fireEvent(this.Event.CellPointerUp, {
+ ...baseParams,
+ ...cell,
+ row: cell.row,
+ column: cell.col,
+ });
+ })
+ )
);
- this.registerEventHandler(
- this.Event.CellPointerMove,
- () => hoverManagerService.currentCellPosWithEvent$
- ?.pipe(filter((cell) => !!cell))
- .subscribe((cell) => {
- const baseParams = this.getSheetTarget(cell.unitId, cell.subUnitId);
- if (!baseParams) return;
- this.fireEvent(this.Event.CellPointerMove, {
- ...baseParams,
- ...cell,
- row: cell.row,
- column: cell.col,
- });
- })
+ lifecycle$Disposable.add(
+ this.registerEventHandler(
+ this.Event.CellPointerMove,
+ () => hoverManagerService.currentCellPosWithEvent$
+ ?.pipe(filter((cell) => !!cell))
+ .subscribe((cell) => {
+ const baseParams = this.getSheetTarget(cell.unitId, cell.subUnitId);
+ if (!baseParams) return;
+ this.fireEvent(this.Event.CellPointerMove, {
+ ...baseParams,
+ ...cell,
+ row: cell.row,
+ column: cell.col,
+ });
+ })
+ )
);
// Drag events
- this.registerEventHandler(
- this.Event.DragOver,
- () => dragManagerService.currentCell$
- ?.pipe(filter((cell) => !!cell))
- .subscribe((cell) => {
- const baseParams = this.getSheetTarget(cell.location.unitId, cell.location.subUnitId);
- if (!baseParams) return;
- this.fireEvent(this.Event.DragOver, {
- ...baseParams,
- ...cell,
- row: cell.location.row,
- column: cell.location.col,
- });
- })
+ lifecycle$Disposable.add(
+ this.registerEventHandler(
+ this.Event.DragOver,
+ () => dragManagerService.currentCell$
+ ?.pipe(filter((cell) => !!cell))
+ .subscribe((cell) => {
+ const baseParams = this.getSheetTarget(cell.location.unitId, cell.location.subUnitId);
+ if (!baseParams) return;
+ this.fireEvent(this.Event.DragOver, {
+ ...baseParams,
+ ...cell,
+ row: cell.location.row,
+ column: cell.location.col,
+ });
+ })
+ )
);
- this.registerEventHandler(
- this.Event.Drop,
- () => dragManagerService.endCell$
- ?.pipe(filter((cell) => !!cell))
- .subscribe((cell) => {
- const baseParams = this.getSheetTarget(cell.location.unitId, cell.location.subUnitId);
- if (!baseParams) return;
- this.fireEvent(this.Event.Drop, {
- ...baseParams,
- ...cell,
- row: cell.location.row,
- column: cell.location.col,
- });
- })
+ lifecycle$Disposable.add(
+ this.registerEventHandler(
+ this.Event.Drop,
+ () => dragManagerService.endCell$
+ ?.pipe(filter((cell) => !!cell))
+ .subscribe((cell) => {
+ const baseParams = this.getSheetTarget(cell.location.unitId, cell.location.subUnitId);
+ if (!baseParams) return;
+ this.fireEvent(this.Event.Drop, {
+ ...baseParams,
+ ...cell,
+ row: cell.location.row,
+ column: cell.location.col,
+ });
+ })
+ )
);
// Row Header events
- this.registerEventHandler(
- this.Event.RowHeaderClick,
- () => hoverManagerService.currentRowHeaderClick$
- ?.pipe(filter((header) => !!header))
- .subscribe((header) => {
- const baseParams = this.getSheetTarget(header.unitId, header.subUnitId);
- if (!baseParams) return;
- this.fireEvent(this.Event.RowHeaderClick, {
- ...baseParams,
- row: header.index,
- });
- })
+ lifecycle$Disposable.add(
+ this.registerEventHandler(
+ this.Event.RowHeaderClick,
+ () => hoverManagerService.currentRowHeaderClick$
+ ?.pipe(filter((header) => !!header))
+ .subscribe((header) => {
+ const baseParams = this.getSheetTarget(header.unitId, header.subUnitId);
+ if (!baseParams) return;
+ this.fireEvent(this.Event.RowHeaderClick, {
+ ...baseParams,
+ row: header.index,
+ });
+ })
+ )
);
- this.registerEventHandler(
- this.Event.RowHeaderPointerDown,
- () => hoverManagerService.currentRowHeaderPointerDown$
- ?.pipe(filter((header) => !!header))
- .subscribe((header) => {
- const baseParams = this.getSheetTarget(header.unitId, header.subUnitId);
- if (!baseParams) return;
- this.fireEvent(this.Event.RowHeaderPointerDown, {
- ...baseParams,
- row: header.index,
- });
- })
+ lifecycle$Disposable.add(
+ this.registerEventHandler(
+ this.Event.RowHeaderPointerDown,
+ () => hoverManagerService.currentRowHeaderPointerDown$
+ ?.pipe(filter((header) => !!header))
+ .subscribe((header) => {
+ const baseParams = this.getSheetTarget(header.unitId, header.subUnitId);
+ if (!baseParams) return;
+ this.fireEvent(this.Event.RowHeaderPointerDown, {
+ ...baseParams,
+ row: header.index,
+ });
+ })
+ )
);
- this.registerEventHandler(
- this.Event.RowHeaderPointerUp,
- () => hoverManagerService.currentRowHeaderPointerUp$
- ?.pipe(filter((header) => !!header))
- .subscribe((header) => {
- const baseParams = this.getSheetTarget(header.unitId, header.subUnitId);
- if (!baseParams) return;
- this.fireEvent(this.Event.RowHeaderPointerUp, {
- ...baseParams,
- row: header.index,
- });
- })
+ lifecycle$Disposable.add(
+ this.registerEventHandler(
+ this.Event.RowHeaderPointerUp,
+ () => hoverManagerService.currentRowHeaderPointerUp$
+ ?.pipe(filter((header) => !!header))
+ .subscribe((header) => {
+ const baseParams = this.getSheetTarget(header.unitId, header.subUnitId);
+ if (!baseParams) return;
+ this.fireEvent(this.Event.RowHeaderPointerUp, {
+ ...baseParams,
+ row: header.index,
+ });
+ })
+ )
);
- this.registerEventHandler(
- this.Event.RowHeaderHover,
- () => hoverManagerService.currentHoveredRowHeader$
- ?.pipe(filter((header) => !!header))
- .subscribe((header) => {
- const baseParams = this.getSheetTarget(header.unitId, header.subUnitId);
- if (!baseParams) return;
- this.fireEvent(this.Event.RowHeaderHover, {
- ...baseParams,
- row: header.index,
- });
- })
+ lifecycle$Disposable.add(
+ this.registerEventHandler(
+ this.Event.RowHeaderHover,
+ () => hoverManagerService.currentHoveredRowHeader$
+ ?.pipe(filter((header) => !!header))
+ .subscribe((header) => {
+ const baseParams = this.getSheetTarget(header.unitId, header.subUnitId);
+ if (!baseParams) return;
+ this.fireEvent(this.Event.RowHeaderHover, {
+ ...baseParams,
+ row: header.index,
+ });
+ })
+ )
);
// Column Header events
- this.registerEventHandler(
- this.Event.ColumnHeaderClick,
- () => hoverManagerService.currentColHeaderClick$
- ?.pipe(filter((header) => !!header))
- .subscribe((header) => {
- const baseParams = this.getSheetTarget(header.unitId, header.subUnitId);
- if (!baseParams) return;
- this.fireEvent(this.Event.ColumnHeaderClick, {
- ...baseParams,
- column: header.index,
- });
- })
+ lifecycle$Disposable.add(
+ this.registerEventHandler(
+ this.Event.ColumnHeaderClick,
+ () => hoverManagerService.currentColHeaderClick$
+ ?.pipe(filter((header) => !!header))
+ .subscribe((header) => {
+ const baseParams = this.getSheetTarget(header.unitId, header.subUnitId);
+ if (!baseParams) return;
+ this.fireEvent(this.Event.ColumnHeaderClick, {
+ ...baseParams,
+ column: header.index,
+ });
+ })
+ )
);
- this.registerEventHandler(
- this.Event.ColumnHeaderPointerDown,
- () => hoverManagerService.currentColHeaderPointerDown$
- ?.pipe(filter((header) => !!header))
- .subscribe((header) => {
- const baseParams = this.getSheetTarget(header.unitId, header.subUnitId);
- if (!baseParams) return;
- this.fireEvent(this.Event.ColumnHeaderPointerDown, {
- ...baseParams,
- column: header.index,
- });
- })
+ lifecycle$Disposable.add(
+ this.registerEventHandler(
+ this.Event.ColumnHeaderPointerDown,
+ () => hoverManagerService.currentColHeaderPointerDown$
+ ?.pipe(filter((header) => !!header))
+ .subscribe((header) => {
+ const baseParams = this.getSheetTarget(header.unitId, header.subUnitId);
+ if (!baseParams) return;
+ this.fireEvent(this.Event.ColumnHeaderPointerDown, {
+ ...baseParams,
+ column: header.index,
+ });
+ })
+ )
);
- this.registerEventHandler(
- this.Event.ColumnHeaderPointerUp,
- () => hoverManagerService.currentColHeaderPointerUp$
- ?.pipe(filter((header) => !!header))
- .subscribe((header) => {
- const baseParams = this.getSheetTarget(header.unitId, header.subUnitId);
- if (!baseParams) return;
- this.fireEvent(this.Event.ColumnHeaderPointerUp, {
- ...baseParams,
- column: header.index,
- });
- })
+ lifecycle$Disposable.add(
+ this.registerEventHandler(
+ this.Event.ColumnHeaderPointerUp,
+ () => hoverManagerService.currentColHeaderPointerUp$
+ ?.pipe(filter((header) => !!header))
+ .subscribe((header) => {
+ const baseParams = this.getSheetTarget(header.unitId, header.subUnitId);
+ if (!baseParams) return;
+ this.fireEvent(this.Event.ColumnHeaderPointerUp, {
+ ...baseParams,
+ column: header.index,
+ });
+ })
+ )
);
- this.registerEventHandler(
- this.Event.ColumnHeaderHover,
- () => hoverManagerService.currentHoveredColHeader$
- ?.pipe(filter((header) => !!header))
- .subscribe((header) => {
- const baseParams = this.getSheetTarget(header.unitId, header.subUnitId);
- if (!baseParams) return;
- this.fireEvent(this.Event.ColumnHeaderHover, {
- ...baseParams,
- column: header.index,
- });
- })
+ lifecycle$Disposable.add(
+ this.registerEventHandler(
+ this.Event.ColumnHeaderHover,
+ () => hoverManagerService.currentHoveredColHeader$
+ ?.pipe(filter((header) => !!header))
+ .subscribe((header) => {
+ const baseParams = this.getSheetTarget(header.unitId, header.subUnitId);
+ if (!baseParams) return;
+ this.fireEvent(this.Event.ColumnHeaderHover, {
+ ...baseParams,
+ column: header.index,
+ });
+ })
+ )
);
+
+ this.disposeWithMe(lifecycle$Disposable);
}));
- this.disposeWithMe(disposable);
// UI Events in renderUnit
- const unitMap = new Map<string, IDisposable>();
let sheetRenderUnit: Nullable<IRender>;
const combined$ = combineLatest([
renderManagerService.created$,
lifeCycleService.lifecycle$,
]);
+ const combined$Disposable = new DisposableCollection();
// eslint-disable-next-line max-lines-per-function
this.disposeWithMe(combined$.subscribe(([created, lifecycle]) => {
// univer & univer-pro are not same in life cycle.
@@ -635,180 +681,186 @@ export class FUniverSheetsUIMixin extends FUniver implements IFUniverSheetsUIMix
// for univer
// type 2 1 1 1
// stage 1 1 1 2
-
if (created.type === UniverInstanceType.UNIVER_SHEET) {
sheetRenderUnit = created;
}
if (lifecycle <= LifecycleStages.Rendered) return;
if (!sheetRenderUnit) return;
- const disposable = new DisposableCollection();
const workbook = this.getWorkbook(sheetRenderUnit.unitId);
if (!workbook) return;
- if (unitMap.get(sheetRenderUnit.unitId)) {
- unitMap.get(sheetRenderUnit.unitId)?.dispose();
- }
- unitMap.set(sheetRenderUnit.unitId, disposable);
+ // Prevent multiple registrations
+ combined$Disposable.dispose();
+
const scrollManagerService = sheetRenderUnit.with(SheetScrollManagerService);
const selectionService = sheetRenderUnit.with(SheetsSelectionsService);
// Register scroll event handler
- disposable.add(this.registerEventHandler(
- this.Event.Scroll,
- () => scrollManagerService.validViewportScrollInfo$.subscribe((params: Nullable<IViewportScrollState>) => {
- if (!params) return;
- this.fireEvent(this.Event.Scroll, {
- workbook,
- worksheet: workbook.getActiveSheet(),
- ...params,
- });
- })
- ));
+ combined$Disposable.add(
+ this.registerEventHandler(
+ this.Event.Scroll,
+ () => scrollManagerService.validViewportScrollInfo$.subscribe((params: Nullable<IViewportScrollState>) => {
+ if (!params) return;
+ this.fireEvent(this.Event.Scroll, {
+ workbook,
+ worksheet: workbook.getActiveSheet(),
+ ...params,
+ });
+ })
+ )
+ );
// Register selection event handlers
- disposable.add(this.registerEventHandler(
- this.Event.SelectionMoveStart,
- () => selectionService.selectionMoveStart$.subscribe((selections) => {
- this.fireEvent(this.Event.SelectionMoveStart, {
- workbook,
- worksheet: workbook.getActiveSheet(),
- selections: selections?.map((s) => s.range) ?? [],
- });
- })
- ));
+ combined$Disposable.add(
+ this.registerEventHandler(
+ this.Event.SelectionMoveStart,
+ () => selectionService.selectionMoveStart$.subscribe((selections) => {
+ this.fireEvent(this.Event.SelectionMoveStart, {
+ workbook,
+ worksheet: workbook.getActiveSheet(),
+ selections: selections?.map((s) => s.range) ?? [],
+ });
+ })
+ )
+ );
- disposable.add(this.registerEventHandler(
- this.Event.SelectionMoving,
- () => selectionService.selectionMoving$.subscribe((selections) => {
- this.fireEvent(this.Event.SelectionMoving, {
- workbook,
- worksheet: workbook.getActiveSheet(),
- selections: selections?.map((s) => s.range) ?? [],
- });
- })
- ));
+ combined$Disposable.add(
+ this.registerEventHandler(
+ this.Event.SelectionMoving,
+ () => selectionService.selectionMoving$.subscribe((selections) => {
+ this.fireEvent(this.Event.SelectionMoving, {
+ workbook,
+ worksheet: workbook.getActiveSheet(),
+ selections: selections?.map((s) => s.range) ?? [],
+ });
+ })
+ )
+ );
- disposable.add(this.registerEventHandler(
- this.Event.SelectionMoveEnd,
- () => selectionService.selectionMoveEnd$.subscribe((selections) => {
- this.fireEvent(this.Event.SelectionMoveEnd, {
- workbook,
- worksheet: workbook.getActiveSheet(),
- selections: selections?.map((s) => s.range) ?? [],
- });
- })
- ));
+ combined$Disposable.add(
+ this.registerEventHandler(
+ this.Event.SelectionMoveEnd,
+ () => selectionService.selectionMoveEnd$.subscribe((selections) => {
+ this.fireEvent(this.Event.SelectionMoveEnd, {
+ workbook,
+ worksheet: workbook.getActiveSheet(),
+ selections: selections?.map((s) => s.range) ?? [],
+ });
+ })
+ )
+ );
- disposable.add(this.registerEventHandler(
- this.Event.SelectionChanged,
- () => selectionService.selectionChanged$.subscribe((selections) => {
- this.fireEvent(this.Event.SelectionChanged, {
- workbook,
- worksheet: workbook.getActiveSheet(),
- selections: selections?.map((s) => s.range) ?? [],
- });
- })
- ));
+ combined$Disposable.add(
+ this.registerEventHandler(
+ this.Event.SelectionChanged,
+ () => selectionService.selectionChanged$.subscribe((selections) => {
+ this.fireEvent(this.Event.SelectionChanged, {
+ workbook,
+ worksheet: workbook.getActiveSheet(),
+ selections: selections?.map((s) => s.range) ?? [],
+ });
+ })
+ )
+ );
// for pro, in pro, life cycle & created$ is not same as univer sdk
// if not clear sheetRenderUnit, that would cause event bind twice!
sheetRenderUnit = null;
- this.disposeWithMe(disposable);
+ this.disposeWithMe(combined$Disposable);
}));
-
- this.disposeWithMe(renderManagerService.disposed$.subscribe((unitId) => {
- unitMap.get(unitId)?.dispose();
- unitMap.delete(unitId);
- }));
-
- this.disposeWithMe(() => {
- unitMap.forEach((disposable) => {
- disposable.dispose();
- });
- });
}
/**
* @ignore
*/
-
+ // eslint-disable-next-line max-lines-per-function
override _initialize(injector: Injector): void {
this._initSheetUIEvent(injector);
this._initObserverListener(injector);
const commandService = injector.get(ICommandService);
- this.registerEventHandler(
- this.Event.BeforeClipboardChange,
- () => commandService.beforeCommandExecuted((commandInfo) => {
- switch (commandInfo.id) {
- case CopyCommand.id:
- case CutCommand.id:
- this._beforeClipboardChange();
- break;
- }
- })
+ this.disposeWithMe(
+ this.registerEventHandler(
+ this.Event.BeforeClipboardChange,
+ () => commandService.beforeCommandExecuted((commandInfo) => {
+ switch (commandInfo.id) {
+ case CopyCommand.id:
+ case CutCommand.id:
+ this._beforeClipboardChange();
+ break;
+ }
+ })
+ )
);
- this.registerEventHandler(
- this.Event.ClipboardChanged,
- () => commandService.onCommandExecuted((commandInfo) => {
- switch (commandInfo.id) {
- case CopyCommand.id:
- case CutCommand.id:
- this._clipboardChanged();
- break;
- }
- })
+ this.disposeWithMe(
+ this.registerEventHandler(
+ this.Event.ClipboardChanged,
+ () => commandService.onCommandExecuted((commandInfo) => {
+ switch (commandInfo.id) {
+ case CopyCommand.id:
+ case CutCommand.id:
+ this._clipboardChanged();
+ break;
+ }
+ })
+ )
);
- this.registerEventHandler(
- this.Event.BeforeClipboardPaste,
- () => commandService.beforeCommandExecuted((commandInfo) => {
- switch (commandInfo.id) {
- case SheetPasteShortKeyCommand.id:
- this._beforeClipboardPaste(commandInfo.params);
- break;
- case PasteCommand.id:
- this._beforeClipboardPasteAsync();
- break;
- }
- })
+ this.disposeWithMe(
+ this.registerEventHandler(
+ this.Event.BeforeClipboardPaste,
+ () => commandService.beforeCommandExecuted((commandInfo) => {
+ switch (commandInfo.id) {
+ case SheetPasteShortKeyCommand.id:
+ this._beforeClipboardPaste(commandInfo.params);
+ break;
+ case PasteCommand.id:
+ this._beforeClipboardPasteAsync();
+ break;
+ }
+ })
+ )
);
- this.registerEventHandler(
- this.Event.ClipboardPasted,
- () => commandService.onCommandExecuted((commandInfo) => {
- switch (commandInfo.id) {
- case SheetPasteShortKeyCommand.id:
- this._clipboardPaste(commandInfo.params);
- break;
- case PasteCommand.id:
- this._clipboardPasteAsync();
- break;
- }
- })
+ this.disposeWithMe(
+ this.registerEventHandler(
+ this.Event.ClipboardPasted,
+ () => commandService.onCommandExecuted((commandInfo) => {
+ switch (commandInfo.id) {
+ case SheetPasteShortKeyCommand.id:
+ this._clipboardPaste(commandInfo.params);
+ break;
+ case PasteCommand.id:
+ this._clipboardPasteAsync();
+ break;
+ }
+ })
+ )
);
- this.registerEventHandler(
- this.Event.SheetSkeletonChanged,
- () => commandService.onCommandExecuted((commandInfo) => {
- if (COMMAND_LISTENER_SKELETON_CHANGE.indexOf(commandInfo.id) > -1) {
- const sheet = this.getActiveSheet();
- if (!sheet) return;
- const ranges = getSkeletonChangedEffectedRange(commandInfo, sheet.worksheet.getMaxColumns())
- .map((range) => this.getWorkbook(range.unitId)?.getSheetBySheetId(range.subUnitId)?.getRange(range.range))
- .filter(Boolean) as FRange[];
- if (!ranges.length) return;
-
- this.fireEvent(this.Event.SheetSkeletonChanged, {
- workbook: sheet.workbook,
- worksheet: sheet.worksheet,
- payload: commandInfo as CommandListenerSkeletonChange,
- skeleton: sheet.worksheet.getSkeleton()!,
- effectedRanges: ranges,
- });
- }
- })
+ this.disposeWithMe(
+ this.registerEventHandler(
+ this.Event.SheetSkeletonChanged,
+ () => commandService.onCommandExecuted((commandInfo) => {
+ if (COMMAND_LISTENER_SKELETON_CHANGE.indexOf(commandInfo.id) > -1) {
+ const sheet = this.getActiveSheet();
+ if (!sheet) return;
+ const ranges = getSkeletonChangedEffectedRange(commandInfo, sheet.worksheet.getMaxColumns())
+ .map((range) => this.getWorkbook(range.unitId)?.getSheetBySheetId(range.subUnitId)?.getRange(range.range))
+ .filter(Boolean) as FRange[];
+ if (!ranges.length) return;
+
+ this.fireEvent(this.Event.SheetSkeletonChanged, {
+ workbook: sheet.workbook,
+ worksheet: sheet.worksheet,
+ payload: commandInfo as CommandListenerSkeletonChange,
+ skeleton: sheet.worksheet.getSkeleton()!,
+ effectedRanges: ranges,
+ });
+ }
+ })
+ )
);
}
diff --git a/packages/sheets-zen-editor/src/facade/f-univer.ts b/packages/sheets-zen-editor/src/facade/f-univer.ts
index e5edd8adae..c0b1e9d122 100644
--- a/packages/sheets-zen-editor/src/facade/f-univer.ts
+++ b/packages/sheets-zen-editor/src/facade/f-univer.ts
@@ -36,165 +36,175 @@ export class FUniverSheetsZenEditorMixin extends FUniver implements IFUniverShee
const commandService = injector.get(ICommandService);
// Register before command execution handlers
- this.registerEventHandler(
- this.Event.BeforeSheetEditStart,
- () => commandService.beforeCommandExecuted((commandInfo) => {
- if (commandInfo.id === OpenZenEditorCommand.id) {
- const target = this.getCommandSheetTarget(commandInfo);
- if (!target) {
- return;
- }
- const { workbook, worksheet } = target;
- const editorBridgeService = injector.get(IEditorBridgeService);
- const params = commandInfo.params as IEditorBridgeServiceVisibleParam;
- const { keycode, eventType } = params;
- const loc = editorBridgeService.getEditLocation()!;
-
- const eventParams: IBeforeSheetEditStartEventParams = {
- row: loc.row,
- column: loc.column,
- eventType,
- keycode,
- workbook,
- worksheet,
- isZenEditor: true,
- };
- this.fireEvent(this.Event.BeforeSheetEditStart, eventParams);
- if (eventParams.cancel) {
- throw new CanceledError();
+ this.disposeWithMe(
+ this.registerEventHandler(
+ this.Event.BeforeSheetEditStart,
+ () => commandService.beforeCommandExecuted((commandInfo) => {
+ if (commandInfo.id === OpenZenEditorCommand.id) {
+ const target = this.getCommandSheetTarget(commandInfo);
+ if (!target) {
+ return;
+ }
+ const { workbook, worksheet } = target;
+ const editorBridgeService = injector.get(IEditorBridgeService);
+ const params = commandInfo.params as IEditorBridgeServiceVisibleParam;
+ const { keycode, eventType } = params;
+ const loc = editorBridgeService.getEditLocation()!;
+
+ const eventParams: IBeforeSheetEditStartEventParams = {
+ row: loc.row,
+ column: loc.column,
+ eventType,
+ keycode,
+ workbook,
+ worksheet,
+ isZenEditor: true,
+ };
+ this.fireEvent(this.Event.BeforeSheetEditStart, eventParams);
+ if (eventParams.cancel) {
+ throw new CanceledError();
+ }
}
- }
- })
+ })
+ )
);
- this.registerEventHandler(
- this.Event.BeforeSheetEditEnd,
- () => commandService.beforeCommandExecuted((commandInfo) => {
- if (
- commandInfo.id === CancelZenEditCommand.id ||
+ this.disposeWithMe(
+ this.registerEventHandler(
+ this.Event.BeforeSheetEditEnd,
+ () => commandService.beforeCommandExecuted((commandInfo) => {
+ if (
+ commandInfo.id === CancelZenEditCommand.id ||
commandInfo.id === ConfirmZenEditCommand.id
- ) {
- const target = this.getCommandSheetTarget(commandInfo);
- if (!target) {
- return;
- }
- const { workbook, worksheet } = target;
- const editorBridgeService = injector.get(IEditorBridgeService);
- const univerInstanceService = injector.get(IUniverInstanceService);
- const params = commandInfo.params as IEditorBridgeServiceVisibleParam;
- const { keycode, eventType } = params;
- const loc = editorBridgeService.getEditLocation()!;
-
- const eventParams: IBeforeSheetEditEndEventParams = {
- row: loc.row,
- column: loc.column,
- eventType,
- keycode,
- workbook,
- worksheet,
- isZenEditor: true,
- value: RichTextValue.create(univerInstanceService.getUnit<DocumentDataModel>(DOCS_ZEN_EDITOR_UNIT_ID_KEY)!.getSnapshot()),
- isConfirm: commandInfo.id === ConfirmZenEditCommand.id,
- };
- this.fireEvent(this.Event.BeforeSheetEditEnd, eventParams);
- if (eventParams.cancel) {
- throw new CanceledError();
+ ) {
+ const target = this.getCommandSheetTarget(commandInfo);
+ if (!target) {
+ return;
+ }
+ const { workbook, worksheet } = target;
+ const editorBridgeService = injector.get(IEditorBridgeService);
+ const univerInstanceService = injector.get(IUniverInstanceService);
+ const params = commandInfo.params as IEditorBridgeServiceVisibleParam;
+ const { keycode, eventType } = params;
+ const loc = editorBridgeService.getEditLocation()!;
+
+ const eventParams: IBeforeSheetEditEndEventParams = {
+ row: loc.row,
+ column: loc.column,
+ eventType,
+ keycode,
+ workbook,
+ worksheet,
+ isZenEditor: true,
+ value: RichTextValue.create(univerInstanceService.getUnit<DocumentDataModel>(DOCS_ZEN_EDITOR_UNIT_ID_KEY)!.getSnapshot()),
+ isConfirm: commandInfo.id === ConfirmZenEditCommand.id,
+ };
+ this.fireEvent(this.Event.BeforeSheetEditEnd, eventParams);
+ if (eventParams.cancel) {
+ throw new CanceledError();
+ }
}
- }
- })
+ })
+ )
);
// Register command execution handlers
- this.registerEventHandler(
- this.Event.SheetEditStarted,
- () => commandService.onCommandExecuted((commandInfo) => {
- if (commandInfo.id === OpenZenEditorCommand.id) {
- const target = this.getCommandSheetTarget(commandInfo);
- if (!target) {
- return;
+ this.disposeWithMe(
+ this.registerEventHandler(
+ this.Event.SheetEditStarted,
+ () => commandService.onCommandExecuted((commandInfo) => {
+ if (commandInfo.id === OpenZenEditorCommand.id) {
+ const target = this.getCommandSheetTarget(commandInfo);
+ if (!target) {
+ return;
+ }
+ const { workbook, worksheet } = target;
+
+ const editorBridgeService = injector.get(IEditorBridgeService);
+ const params = commandInfo.params as IEditorBridgeServiceVisibleParam;
+ const { keycode, eventType } = params;
+ const loc = editorBridgeService.getEditLocation()!;
+ const eventParams: ISheetEditStartedEventParams = {
+ row: loc.row,
+ column: loc.column,
+ eventType,
+ keycode,
+ workbook,
+ worksheet,
+ isZenEditor: true,
+ };
+ this.fireEvent(this.Event.SheetEditStarted, eventParams);
}
- const { workbook, worksheet } = target;
-
- const editorBridgeService = injector.get(IEditorBridgeService);
- const params = commandInfo.params as IEditorBridgeServiceVisibleParam;
- const { keycode, eventType } = params;
- const loc = editorBridgeService.getEditLocation()!;
- const eventParams: ISheetEditStartedEventParams = {
- row: loc.row,
- column: loc.column,
- eventType,
- keycode,
- workbook,
- worksheet,
- isZenEditor: true,
- };
- this.fireEvent(this.Event.SheetEditStarted, eventParams);
- }
- })
+ })
+ )
);
- this.registerEventHandler(
- this.Event.SheetEditEnded,
- () => commandService.onCommandExecuted((commandInfo) => {
- if (
- commandInfo.id === CancelZenEditCommand.id ||
+ this.disposeWithMe(
+ this.registerEventHandler(
+ this.Event.SheetEditEnded,
+ () => commandService.onCommandExecuted((commandInfo) => {
+ if (
+ commandInfo.id === CancelZenEditCommand.id ||
commandInfo.id === ConfirmZenEditCommand.id
- ) {
- const target = this.getCommandSheetTarget(commandInfo);
- if (!target) {
- return;
- }
- const { workbook, worksheet } = target;
-
- const editorBridgeService = injector.get(IEditorBridgeService);
- const params = commandInfo.params as IEditorBridgeServiceVisibleParam;
- const { keycode, eventType } = params;
- const loc = editorBridgeService.getEditLocation()!;
-
- const eventParams: ISheetEditEndedEventParams = {
- row: loc.row,
- column: loc.column,
- eventType,
- keycode,
- workbook,
- worksheet,
- isZenEditor: true,
- isConfirm: commandInfo.id === ConfirmZenEditCommand.id,
- };
- this.fireEvent(this.Event.SheetEditEnded, eventParams);
- }
- })
- );
-
- // Register rich text editing mutation handler
- this.registerEventHandler(
- this.Event.SheetEditChanging,
- () => commandService.onCommandExecuted((commandInfo) => {
- if (commandInfo.id === RichTextEditingMutation.id) {
- const target = this.getActiveSheet();
- if (!target) {
- return;
- }
- const { workbook, worksheet } = target;
- const editorBridgeService = injector.get(IEditorBridgeService);
- const univerInstanceService = injector.get(IUniverInstanceService);
- const params = commandInfo.params as IRichTextEditingMutationParams;
- if (!editorBridgeService.isVisible().visible) return;
- const { unitId } = params;
- if (unitId === DOCS_ZEN_EDITOR_UNIT_ID_KEY) {
- const { row, column } = editorBridgeService.getEditLocation()!;
- const eventParams: ISheetEditChangingEventParams = {
+ ) {
+ const target = this.getCommandSheetTarget(commandInfo);
+ if (!target) {
+ return;
+ }
+ const { workbook, worksheet } = target;
+
+ const editorBridgeService = injector.get(IEditorBridgeService);
+ const params = commandInfo.params as IEditorBridgeServiceVisibleParam;
+ const { keycode, eventType } = params;
+ const loc = editorBridgeService.getEditLocation()!;
+
+ const eventParams: ISheetEditEndedEventParams = {
+ row: loc.row,
+ column: loc.column,
+ eventType,
+ keycode,
workbook,
worksheet,
- row,
- column,
- value: RichTextValue.create(univerInstanceService.getUnit<DocumentDataModel>(DOCS_ZEN_EDITOR_UNIT_ID_KEY)!.getSnapshot()),
isZenEditor: true,
+ isConfirm: commandInfo.id === ConfirmZenEditCommand.id,
};
- this.fireEvent(this.Event.SheetEditChanging, eventParams);
+ this.fireEvent(this.Event.SheetEditEnded, eventParams);
+ }
+ })
+ )
+ );
+
+ // Register rich text editing mutation handler
+ this.disposeWithMe(
+ this.registerEventHandler(
+ this.Event.SheetEditChanging,
+ () => commandService.onCommandExecuted((commandInfo) => {
+ if (commandInfo.id === RichTextEditingMutation.id) {
+ const target = this.getActiveSheet();
+ if (!target) {
+ return;
+ }
+ const { workbook, worksheet } = target;
+ const editorBridgeService = injector.get(IEditorBridgeService);
+ const univerInstanceService = injector.get(IUniverInstanceService);
+ const params = commandInfo.params as IRichTextEditingMutationParams;
+ if (!editorBridgeService.isVisible().visible) return;
+ const { unitId } = params;
+ if (unitId === DOCS_ZEN_EDITOR_UNIT_ID_KEY) {
+ const { row, column } = editorBridgeService.getEditLocation()!;
+ const eventParams: ISheetEditChangingEventParams = {
+ workbook,
+ worksheet,
+ row,
+ column,
+ value: RichTextValue.create(univerInstanceService.getUnit<DocumentDataModel>(DOCS_ZEN_EDITOR_UNIT_ID_KEY)!.getSnapshot()),
+ isZenEditor: true,
+ };
+ this.fireEvent(this.Event.SheetEditChanging, eventParams);
+ }
}
- }
- })
+ })
+ )
);
}
diff --git a/packages/sheets/src/facade/f-univer.ts b/packages/sheets/src/facade/f-univer.ts
index 11fcee4e5b..75380271fe 100644
--- a/packages/sheets/src/facade/f-univer.ts
+++ b/packages/sheets/src/facade/f-univer.ts
@@ -209,34 +209,38 @@ export class FUniverSheetsMixin extends FUniver implements IFUniverSheetsMixin {
const univerInstanceService = injector.get(IUniverInstanceService);
// Register workbook disposed event handler
- this.registerEventHandler(
- this.Event.WorkbookDisposed,
- () => univerInstanceService.unitDisposed$.subscribe((unit) => {
- if (unit.type === UniverInstanceType.UNIVER_SHEET) {
- this.fireEvent(this.Event.WorkbookDisposed, {
- unitId: unit.getUnitId(),
- unitType: unit.type,
- snapshot: unit.getSnapshot() as IWorkbookData,
- });
- }
- })
+ this.disposeWithMe(
+ this.registerEventHandler(
+ this.Event.WorkbookDisposed,
+ () => univerInstanceService.unitDisposed$.subscribe((unit) => {
+ if (unit.type === UniverInstanceType.UNIVER_SHEET) {
+ this.fireEvent(this.Event.WorkbookDisposed, {
+ unitId: unit.getUnitId(),
+ unitType: unit.type,
+ snapshot: unit.getSnapshot() as IWorkbookData,
+ });
+ }
+ })
+ )
);
// Register workbook created event handler
- this.registerEventHandler(
- this.Event.WorkbookCreated,
- () => univerInstanceService.unitAdded$.subscribe((unit) => {
- if (unit.type === UniverInstanceType.UNIVER_SHEET) {
- const workbook = unit as Workbook;
- const workbookUnit = injector.createInstance(FWorkbook, workbook);
- this.fireEvent(this.Event.WorkbookCreated, {
- unitId: unit.getUnitId(),
- type: unit.type,
- workbook: workbookUnit,
- unit: workbookUnit,
- });
- }
- })
+ this.disposeWithMe(
+ this.registerEventHandler(
+ this.Event.WorkbookCreated,
+ () => univerInstanceService.unitAdded$.subscribe((unit) => {
+ if (unit.type === UniverInstanceType.UNIVER_SHEET) {
+ const workbook = unit as Workbook;
+ const workbookUnit = injector.createInstance(FWorkbook, workbook);
+ this.fireEvent(this.Event.WorkbookCreated, {
+ unitId: unit.getUnitId(),
+ type: unit.type,
+ workbook: workbookUnit,
+ unit: workbookUnit,
+ });
+ }
+ })
+ )
);
}
@@ -247,332 +251,368 @@ export class FUniverSheetsMixin extends FUniver implements IFUniverSheetsMixin {
override _initialize(injector: Injector): void {
const commandService = injector.get(ICommandService);
- this.registerEventHandler(
- this.Event.BeforeSheetCreate,
- () => commandService.beforeCommandExecuted((commandInfo) => {
- if (commandInfo.id === InsertSheetCommand.id) {
- const params = (commandInfo.params) as IInsertSheetCommandParams;
- const { unitId, index, sheet } = params || {};
- const workbook = unitId ? this.getUniverSheet(unitId) : this.getActiveWorkbook?.();
- if (!workbook) {
- return;
- }
- const eventParams: IBeforeSheetCreateEventParams = {
- workbook,
- index,
- sheet,
- };
- this.fireEvent(this.Event.BeforeSheetCreate, eventParams);
- // cancel this command
- if (eventParams.cancel) {
- throw new CanceledError();
+ this.disposeWithMe(
+ this.registerEventHandler(
+ this.Event.BeforeSheetCreate,
+ () => commandService.beforeCommandExecuted((commandInfo) => {
+ if (commandInfo.id === InsertSheetCommand.id) {
+ const params = (commandInfo.params) as IInsertSheetCommandParams;
+ const { unitId, index, sheet } = params || {};
+ const workbook = unitId ? this.getUniverSheet(unitId) : this.getActiveWorkbook?.();
+ if (!workbook) {
+ return;
+ }
+ const eventParams: IBeforeSheetCreateEventParams = {
+ workbook,
+ index,
+ sheet,
+ };
+ this.fireEvent(this.Event.BeforeSheetCreate, eventParams);
+ // cancel this command
+ if (eventParams.cancel) {
+ throw new CanceledError();
+ }
}
- }
- })
+ })
+ )
);
- this.registerEventHandler(
- this.Event.BeforeActiveSheetChange,
- () => commandService.beforeCommandExecuted((commandInfo) => {
- if (commandInfo.id === SetWorksheetActiveOperation.id) {
- const { subUnitId: sheetId, unitId } = commandInfo.params as ISetWorksheetActivateCommandParams;
- const workbook = unitId ? this.getUniverSheet(unitId) : this.getActiveWorkbook?.();
- if (!workbook || !sheetId) return;
- const activeSheet = workbook.getSheetBySheetId(sheetId);
- const oldActiveSheet = workbook.getActiveSheet();
- if (!activeSheet || !oldActiveSheet) return;
- const eventParams: IBeforeActiveSheetChangeEvent = {
- workbook,
- activeSheet,
- oldActiveSheet,
- };
- this.fireEvent(this.Event.BeforeActiveSheetChange, eventParams);
- // cancel this command
- if (eventParams.cancel) {
- throw new CanceledError();
+ this.disposeWithMe(
+ this.registerEventHandler(
+ this.Event.BeforeActiveSheetChange,
+ () => commandService.beforeCommandExecuted((commandInfo) => {
+ if (commandInfo.id === SetWorksheetActiveOperation.id) {
+ const { subUnitId: sheetId, unitId } = commandInfo.params as ISetWorksheetActivateCommandParams;
+ const workbook = unitId ? this.getUniverSheet(unitId) : this.getActiveWorkbook?.();
+ if (!workbook || !sheetId) return;
+ const activeSheet = workbook.getSheetBySheetId(sheetId);
+ const oldActiveSheet = workbook.getActiveSheet();
+ if (!activeSheet || !oldActiveSheet) return;
+ const eventParams: IBeforeActiveSheetChangeEvent = {
+ workbook,
+ activeSheet,
+ oldActiveSheet,
+ };
+ this.fireEvent(this.Event.BeforeActiveSheetChange, eventParams);
+ // cancel this command
+ if (eventParams.cancel) {
+ throw new CanceledError();
+ }
}
- }
- })
+ })
+ )
);
- this.registerEventHandler(
- this.Event.BeforeSheetDelete,
- () => commandService.beforeCommandExecuted((commandInfo) => {
- if (commandInfo.id === RemoveSheetCommand.id) {
- const target = this.getCommandSheetTarget(commandInfo);
- if (!target) return;
- const { workbook, worksheet } = target;
- const eventParams: IBeforeSheetDeleteEvent = {
- workbook,
- worksheet,
- };
- this.fireEvent(this.Event.BeforeSheetDelete, eventParams);
- // cancel this command
- if (eventParams.cancel) {
- throw new CanceledError();
+ this.disposeWithMe(
+ this.registerEventHandler(
+ this.Event.BeforeSheetDelete,
+ () => commandService.beforeCommandExecuted((commandInfo) => {
+ if (commandInfo.id === RemoveSheetCommand.id) {
+ const target = this.getCommandSheetTarget(commandInfo);
+ if (!target) return;
+ const { workbook, worksheet } = target;
+ const eventParams: IBeforeSheetDeleteEvent = {
+ workbook,
+ worksheet,
+ };
+ this.fireEvent(this.Event.BeforeSheetDelete, eventParams);
+ // cancel this command
+ if (eventParams.cancel) {
+ throw new CanceledError();
+ }
}
- }
- })
+ })
+ )
);
- this.registerEventHandler(
- this.Event.BeforeSheetMove,
- () => commandService.beforeCommandExecuted((commandInfo) => {
- if (commandInfo.id === SetWorksheetOrderMutation.id) {
- const { fromOrder, toOrder } = commandInfo.params as ISetWorksheetOrderMutationParams;
- const target = this.getCommandSheetTarget(commandInfo);
- if (!target) return;
- const eventParams: IBeforeSheetMoveEvent = {
- workbook: target.workbook,
- worksheet: target.worksheet,
- newIndex: toOrder,
- oldIndex: fromOrder,
- };
- this.fireEvent(this.Event.BeforeSheetMove, eventParams);
- // cancel this command
- if (eventParams.cancel) {
- throw new CanceledError();
+ this.disposeWithMe(
+ this.registerEventHandler(
+ this.Event.BeforeSheetMove,
+ () => commandService.beforeCommandExecuted((commandInfo) => {
+ if (commandInfo.id === SetWorksheetOrderMutation.id) {
+ const { fromOrder, toOrder } = commandInfo.params as ISetWorksheetOrderMutationParams;
+ const target = this.getCommandSheetTarget(commandInfo);
+ if (!target) return;
+ const eventParams: IBeforeSheetMoveEvent = {
+ workbook: target.workbook,
+ worksheet: target.worksheet,
+ newIndex: toOrder,
+ oldIndex: fromOrder,
+ };
+ this.fireEvent(this.Event.BeforeSheetMove, eventParams);
+ // cancel this command
+ if (eventParams.cancel) {
+ throw new CanceledError();
+ }
}
- }
- })
+ })
+ )
);
- this.registerEventHandler(
- this.Event.BeforeSheetNameChange,
- () => commandService.beforeCommandExecuted((commandInfo) => {
- if (commandInfo.id === SetWorksheetNameCommand.id) {
- const { name } = commandInfo.params as ISetWorksheetNameCommandParams;
- const target = this.getCommandSheetTarget(commandInfo);
- if (!target) return;
- const eventParams: IBeforeSheetNameChangeEvent = {
- workbook: target.workbook,
- worksheet: target.worksheet,
- newName: name,
- oldName: target.worksheet.getSheetName(),
- };
- this.fireEvent(this.Event.BeforeSheetNameChange, eventParams);
- // cancel this command
- if (eventParams.cancel) {
- throw new CanceledError();
+ this.disposeWithMe(
+ this.registerEventHandler(
+ this.Event.BeforeSheetNameChange,
+ () => commandService.beforeCommandExecuted((commandInfo) => {
+ if (commandInfo.id === SetWorksheetNameCommand.id) {
+ const { name } = commandInfo.params as ISetWorksheetNameCommandParams;
+ const target = this.getCommandSheetTarget(commandInfo);
+ if (!target) return;
+ const eventParams: IBeforeSheetNameChangeEvent = {
+ workbook: target.workbook,
+ worksheet: target.worksheet,
+ newName: name,
+ oldName: target.worksheet.getSheetName(),
+ };
+ this.fireEvent(this.Event.BeforeSheetNameChange, eventParams);
+ // cancel this command
+ if (eventParams.cancel) {
+ throw new CanceledError();
+ }
}
- }
- })
+ })
+ )
);
- this.registerEventHandler(
- this.Event.BeforeSheetTabColorChange,
- () => commandService.beforeCommandExecuted((commandInfo) => {
- if (commandInfo.id === SetTabColorMutation.id) {
- const { color } = commandInfo.params as ISetTabColorMutationParams;
- const target = this.getCommandSheetTarget(commandInfo);
- if (!target) return;
- const eventParams: IBeforeSheetTabColorChangeEvent = {
- workbook: target.workbook,
- worksheet: target.worksheet,
- newColor: color,
- oldColor: target.worksheet.getTabColor(),
- };
- this.fireEvent(this.Event.BeforeSheetTabColorChange, eventParams);
- // cancel this command
- if (eventParams.cancel) {
- throw new CanceledError();
+ this.disposeWithMe(
+ this.registerEventHandler(
+ this.Event.BeforeSheetTabColorChange,
+ () => commandService.beforeCommandExecuted((commandInfo) => {
+ if (commandInfo.id === SetTabColorMutation.id) {
+ const { color } = commandInfo.params as ISetTabColorMutationParams;
+ const target = this.getCommandSheetTarget(commandInfo);
+ if (!target) return;
+ const eventParams: IBeforeSheetTabColorChangeEvent = {
+ workbook: target.workbook,
+ worksheet: target.worksheet,
+ newColor: color,
+ oldColor: target.worksheet.getTabColor(),
+ };
+ this.fireEvent(this.Event.BeforeSheetTabColorChange, eventParams);
+ // cancel this command
+ if (eventParams.cancel) {
+ throw new CanceledError();
+ }
}
- }
- })
+ })
+ )
);
- this.registerEventHandler(
- this.Event.BeforeSheetHideChange,
- () => commandService.beforeCommandExecuted((commandInfo) => {
- if (commandInfo.id === SetWorksheetHideMutation.id) {
- const { hidden } = commandInfo.params as ISetWorksheetHideMutationParams;
- const target = this.getCommandSheetTarget(commandInfo);
- if (!target) return;
- const eventParams: IBeforeSheetHideChangeEvent = {
- workbook: target.workbook,
- worksheet: target.worksheet,
- hidden: Boolean(hidden),
- };
- this.fireEvent(this.Event.BeforeSheetHideChange, eventParams);
- // cancel this command
- if (eventParams.cancel) {
- throw new CanceledError();
+ this.disposeWithMe(
+ this.registerEventHandler(
+ this.Event.BeforeSheetHideChange,
+ () => commandService.beforeCommandExecuted((commandInfo) => {
+ if (commandInfo.id === SetWorksheetHideMutation.id) {
+ const { hidden } = commandInfo.params as ISetWorksheetHideMutationParams;
+ const target = this.getCommandSheetTarget(commandInfo);
+ if (!target) return;
+ const eventParams: IBeforeSheetHideChangeEvent = {
+ workbook: target.workbook,
+ worksheet: target.worksheet,
+ hidden: Boolean(hidden),
+ };
+ this.fireEvent(this.Event.BeforeSheetHideChange, eventParams);
+ // cancel this command
+ if (eventParams.cancel) {
+ throw new CanceledError();
+ }
}
- }
- })
+ })
+ )
);
- this.registerEventHandler(
- this.Event.BeforeGridlineColorChange,
- () => commandService.beforeCommandExecuted((commandInfo) => {
- if (commandInfo.id === SetGridlinesColorCommand.id) {
- const target = this.getCommandSheetTarget(commandInfo);
- if (!target) return;
- const eventParams: IBeforeGridlineColorChanged = {
- ...target,
- color: (commandInfo.params as ISetGridlinesColorCommandParams)?.color,
- };
- this.fireEvent(this.Event.BeforeGridlineColorChange, eventParams);
- // cancel this command
- if (eventParams.cancel) {
- throw new CanceledError();
+ this.disposeWithMe(
+ this.registerEventHandler(
+ this.Event.BeforeGridlineColorChange,
+ () => commandService.beforeCommandExecuted((commandInfo) => {
+ if (commandInfo.id === SetGridlinesColorCommand.id) {
+ const target = this.getCommandSheetTarget(commandInfo);
+ if (!target) return;
+ const eventParams: IBeforeGridlineColorChanged = {
+ ...target,
+ color: (commandInfo.params as ISetGridlinesColorCommandParams)?.color,
+ };
+ this.fireEvent(this.Event.BeforeGridlineColorChange, eventParams);
+ // cancel this command
+ if (eventParams.cancel) {
+ throw new CanceledError();
+ }
}
- }
- })
+ })
+ )
);
- this.registerEventHandler(
- this.Event.BeforeGridlineEnableChange,
- () => commandService.beforeCommandExecuted((commandInfo) => {
- if (commandInfo.id === ToggleGridlinesCommand.id) {
- const target = this.getCommandSheetTarget(commandInfo);
- if (!target) return;
- const eventParams: IBeforeGridlineEnableChange = {
- ...target,
- enabled: Boolean((commandInfo.params as IToggleGridlinesCommandParams)?.showGridlines) ?? !target.worksheet.hasHiddenGridLines(),
- };
- this.fireEvent(this.Event.BeforeGridlineEnableChange, eventParams);
- // cancel this command
- if (eventParams.cancel) {
- throw new CanceledError();
+ this.disposeWithMe(
+ this.registerEventHandler(
+ this.Event.BeforeGridlineEnableChange,
+ () => commandService.beforeCommandExecuted((commandInfo) => {
+ if (commandInfo.id === ToggleGridlinesCommand.id) {
+ const target = this.getCommandSheetTarget(commandInfo);
+ if (!target) return;
+ const eventParams: IBeforeGridlineEnableChange = {
+ ...target,
+ enabled: Boolean((commandInfo.params as IToggleGridlinesCommandParams)?.showGridlines) ?? !target.worksheet.hasHiddenGridLines(),
+ };
+ this.fireEvent(this.Event.BeforeGridlineEnableChange, eventParams);
+ // cancel this command
+ if (eventParams.cancel) {
+ throw new CanceledError();
+ }
}
- }
- })
+ })
+ )
);
- this.registerEventHandler(
- this.Event.SheetValueChanged,
- () => commandService.onCommandExecuted((commandInfo) => {
- if (COMMAND_LISTENER_VALUE_CHANGE.indexOf(commandInfo.id) > -1) {
- const sheet = this.getActiveSheet();
- if (!sheet) return;
- const ranges = getValueChangedEffectedRange(commandInfo)
- .map(
- (range) => this.getWorkbook(range.unitId)
- ?.getSheetBySheetId(range.subUnitId)
- ?.getRange(range.range)
- )
- .filter(Boolean) as FRange[];
- if (!ranges.length) return;
- this.fireEvent(this.Event.SheetValueChanged, {
- payload: commandInfo as CommandListenerValueChange,
- effectedRanges: ranges,
- });
- }
- })
+ this.disposeWithMe(
+ this.registerEventHandler(
+ this.Event.SheetValueChanged,
+ () => commandService.onCommandExecuted((commandInfo) => {
+ if (COMMAND_LISTENER_VALUE_CHANGE.indexOf(commandInfo.id) > -1) {
+ const sheet = this.getActiveSheet();
+ if (!sheet) return;
+ const ranges = getValueChangedEffectedRange(commandInfo)
+ .map(
+ (range) => this.getWorkbook(range.unitId)
+ ?.getSheetBySheetId(range.subUnitId)
+ ?.getRange(range.range)
+ )
+ .filter(Boolean) as FRange[];
+ if (!ranges.length) return;
+ this.fireEvent(this.Event.SheetValueChanged, {
+ payload: commandInfo as CommandListenerValueChange,
+ effectedRanges: ranges,
+ });
+ }
+ })
+ )
);
- this.registerEventHandler(
- this.Event.SheetCreated,
- () => commandService.onCommandExecuted((commandInfo) => {
- if (commandInfo.id === InsertSheetCommand.id) {
- const params = commandInfo.params as IInsertSheetCommandParams;
- const { unitId } = params || {};
- const workbook = unitId ? this.getUniverSheet(unitId) : this.getActiveWorkbook?.();
- if (!workbook) {
- return;
- }
- const worksheet = workbook.getActiveSheet();
- if (!worksheet) {
- return;
+ this.disposeWithMe(
+ this.registerEventHandler(
+ this.Event.SheetCreated,
+ () => commandService.onCommandExecuted((commandInfo) => {
+ if (commandInfo.id === InsertSheetCommand.id) {
+ const params = commandInfo.params as IInsertSheetCommandParams;
+ const { unitId } = params || {};
+ const workbook = unitId ? this.getUniverSheet(unitId) : this.getActiveWorkbook?.();
+ if (!workbook) {
+ return;
+ }
+ const worksheet = workbook.getActiveSheet();
+ if (!worksheet) {
+ return;
+ }
+ const eventParams: ISheetCreatedEventParams = {
+ workbook,
+ worksheet,
+ };
+ this.fireEvent(
+ this.Event.SheetCreated,
+ eventParams
+ );
}
- const eventParams: ISheetCreatedEventParams = {
- workbook,
- worksheet,
- };
- this.fireEvent(
- this.Event.SheetCreated,
- eventParams
- );
- }
- })
+ })
+ )
);
- this.registerEventHandler(
- this.Event.ActiveSheetChanged,
- () => commandService.onCommandExecuted((commandInfo) => {
- if (commandInfo.id === SetWorksheetActiveOperation.id) {
- const target = this.getActiveSheet();
- if (!target) return;
- const { workbook, worksheet: activeSheet } = target;
- this._fireActiveSheetChanged(workbook, activeSheet);
- }
- })
+ this.disposeWithMe(
+ this.registerEventHandler(
+ this.Event.ActiveSheetChanged,
+ () => commandService.onCommandExecuted((commandInfo) => {
+ if (commandInfo.id === SetWorksheetActiveOperation.id) {
+ const target = this.getActiveSheet();
+ if (!target) return;
+ const { workbook, worksheet: activeSheet } = target;
+ this._fireActiveSheetChanged(workbook, activeSheet);
+ }
+ })
+ )
);
- this.registerEventHandler(
- this.Event.SheetDeleted,
- () => commandService.onCommandExecuted((commandInfo) => {
- if (commandInfo.id === RemoveSheetCommand.id) {
- const { subUnitId: sheetId, unitId } = commandInfo.params as IRemoveSheetCommandParams;
- const workbook = unitId ? this.getUniverSheet(unitId) : this.getActiveWorkbook?.();
- if (!workbook || !sheetId) return;
- this._fireSheetDeleted(workbook, sheetId);
- }
- })
+ this.disposeWithMe(
+ this.registerEventHandler(
+ this.Event.SheetDeleted,
+ () => commandService.onCommandExecuted((commandInfo) => {
+ if (commandInfo.id === RemoveSheetCommand.id) {
+ const { subUnitId: sheetId, unitId } = commandInfo.params as IRemoveSheetCommandParams;
+ const workbook = unitId ? this.getUniverSheet(unitId) : this.getActiveWorkbook?.();
+ if (!workbook || !sheetId) return;
+ this._fireSheetDeleted(workbook, sheetId);
+ }
+ })
+ )
);
- this.registerEventHandler(
- this.Event.SheetMoved,
- () => commandService.onCommandExecuted((commandInfo) => {
- if (commandInfo.id === SetWorksheetOrderMutation.id) {
- const { toOrder: toIndex } = commandInfo.params as ISetWorksheetOrderMutationParams;
- const target = this.getCommandSheetTarget(commandInfo);
- if (!target) return;
- this._fireSheetMoved(target.workbook, target.worksheet, toIndex);
- }
- })
+ this.disposeWithMe(
+ this.registerEventHandler(
+ this.Event.SheetMoved,
+ () => commandService.onCommandExecuted((commandInfo) => {
+ if (commandInfo.id === SetWorksheetOrderMutation.id) {
+ const { toOrder: toIndex } = commandInfo.params as ISetWorksheetOrderMutationParams;
+ const target = this.getCommandSheetTarget(commandInfo);
+ if (!target) return;
+ this._fireSheetMoved(target.workbook, target.worksheet, toIndex);
+ }
+ })
+ )
);
- this.registerEventHandler(
- this.Event.SheetNameChanged,
- () => commandService.onCommandExecuted((commandInfo) => {
- if (commandInfo.id === SetWorksheetNameCommand.id) {
- const { name } = commandInfo.params as ISetWorksheetNameCommandParams;
- const target = this.getCommandSheetTarget(commandInfo);
- if (!target) return;
- this._fireSheetNameChanged(target.workbook, target.worksheet, name);
- }
- })
+ this.disposeWithMe(
+ this.registerEventHandler(
+ this.Event.SheetNameChanged,
+ () => commandService.onCommandExecuted((commandInfo) => {
+ if (commandInfo.id === SetWorksheetNameCommand.id) {
+ const { name } = commandInfo.params as ISetWorksheetNameCommandParams;
+ const target = this.getCommandSheetTarget(commandInfo);
+ if (!target) return;
+ this._fireSheetNameChanged(target.workbook, target.worksheet, name);
+ }
+ })
+ )
);
- this.registerEventHandler(
- this.Event.SheetTabColorChanged,
- () => commandService.onCommandExecuted((commandInfo) => {
- if (commandInfo.id === SetTabColorMutation.id) {
- const { color } = commandInfo.params as ISetTabColorMutationParams;
- const target = this.getCommandSheetTarget(commandInfo);
- if (!target) return;
- this._fireSheetTabColorChanged(target.workbook, target.worksheet, color);
- }
- })
+ this.disposeWithMe(
+ this.registerEventHandler(
+ this.Event.SheetTabColorChanged,
+ () => commandService.onCommandExecuted((commandInfo) => {
+ if (commandInfo.id === SetTabColorMutation.id) {
+ const { color } = commandInfo.params as ISetTabColorMutationParams;
+ const target = this.getCommandSheetTarget(commandInfo);
+ if (!target) return;
+ this._fireSheetTabColorChanged(target.workbook, target.worksheet, color);
+ }
+ })
+ )
);
- this.registerEventHandler(
- this.Event.SheetHideChanged,
- () => commandService.onCommandExecuted((commandInfo) => {
- if (commandInfo.id === SetWorksheetHideMutation.id) {
- const { hidden } = commandInfo.params as ISetWorksheetHideMutationParams;
- const target = this.getCommandSheetTarget(commandInfo);
- if (!target) return;
- this._fireSheetHideChanged(target.workbook, target.worksheet, !!hidden);
- }
- })
+ this.disposeWithMe(
+ this.registerEventHandler(
+ this.Event.SheetHideChanged,
+ () => commandService.onCommandExecuted((commandInfo) => {
+ if (commandInfo.id === SetWorksheetHideMutation.id) {
+ const { hidden } = commandInfo.params as ISetWorksheetHideMutationParams;
+ const target = this.getCommandSheetTarget(commandInfo);
+ if (!target) return;
+ this._fireSheetHideChanged(target.workbook, target.worksheet, !!hidden);
+ }
+ })
+ )
);
- this.registerEventHandler(
- this.Event.GridlineChanged,
- () => commandService.onCommandExecuted((commandInfo) => {
- if (commandInfo.id === SetGridlinesColorCommand.id || commandInfo.id === ToggleGridlinesCommand.id) {
- const target = this.getCommandSheetTarget(commandInfo);
- if (!target) return;
- this.fireEvent(this.Event.GridlineChanged, {
- ...target,
- enabled: !target.worksheet.hasHiddenGridLines(),
- color: target.worksheet.getGridLinesColor(),
- });
- }
- })
+ this.disposeWithMe(
+ this.registerEventHandler(
+ this.Event.GridlineChanged,
+ () => commandService.onCommandExecuted((commandInfo) => {
+ if (commandInfo.id === SetGridlinesColorCommand.id || commandInfo.id === ToggleGridlinesCommand.id) {
+ const target = this.getCommandSheetTarget(commandInfo);
+ if (!target) return;
+ this.fireEvent(this.Event.GridlineChanged, {
+ ...target,
+ enabled: !target.worksheet.hasHiddenGridLines(),
+ color: target.worksheet.getGridLinesColor(),
+ });
+ }
+ })
+ )
);
this._initWorkbookEvent(injector);
diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml
index 20c9a3b140..09c76e4eb8 100644
--- a/pnpm-lock.yaml
+++ b/pnpm-lock.yaml
@@ -675,6 +675,9 @@ importers:
react-mosaic-component:
specifier: ^6.1.1
+ rxjs:
+ specifier: ^7.8.2
+ version: 7.8.2
devDependencies:
'@types/fs-extra':
specifier: ^11.0.4