Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(facade): add lifecycle and cellChange event api #3626

Merged
merged 3 commits into from
Oct 12, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 35 additions & 23 deletions packages/facade/src/apis/facade.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,29 +14,6 @@
* limitations under the License.
*/

import {
BorderStyleTypes,
debounce,
ICommandService,
Inject,
Injector,
IUniverInstanceService,
Quantity,
RedoCommand,
toDisposable,
UndoCommand,
Univer,
UniverInstanceType,
WrapStrategy,
} from '@univerjs/core';
import { SetFormulaCalculationStartMutation } from '@univerjs/engine-formula';
import { IRenderManagerService } from '@univerjs/engine-render';
import { ISocketService, WebSocketService } from '@univerjs/network';
import { DisableCrosshairHighlightOperation, EnableCrosshairHighlightOperation, SetCrosshairHighlightColorOperation } from '@univerjs/sheets-crosshair-highlight';
import { IRegisterFunctionService, RegisterFunctionService } from '@univerjs/sheets-formula';

import { SHEET_VIEW_KEY } from '@univerjs/sheets-ui';
import { CopyCommand, PasteCommand } from '@univerjs/ui';
import type {
CommandListener,
Dependency,
Expand All @@ -45,6 +22,7 @@ import type {
IDocumentData,
IExecutionOptions,
IWorkbookData,
LifecycleStages,
Nullable,
Workbook,
} from '@univerjs/core';
Expand All @@ -59,6 +37,30 @@ import type {
import type { ISocket } from '@univerjs/network';
import type { ISetCrosshairHighlightColorOperationParams } from '@univerjs/sheets-crosshair-highlight';
import type { IRegisterFunctionParams } from '@univerjs/sheets-formula';
import {
BorderStyleTypes,
debounce,
ICommandService,
Inject,
Injector,
IUniverInstanceService,
LifecycleService,
Quantity,
RedoCommand,
toDisposable,
UndoCommand,
Univer,
UniverInstanceType,
WrapStrategy,
} from '@univerjs/core';

import { SetFormulaCalculationStartMutation } from '@univerjs/engine-formula';
import { IRenderManagerService } from '@univerjs/engine-render';
import { ISocketService, WebSocketService } from '@univerjs/network';
import { DisableCrosshairHighlightOperation, EnableCrosshairHighlightOperation, SetCrosshairHighlightColorOperation } from '@univerjs/sheets-crosshair-highlight';
import { IRegisterFunctionService, RegisterFunctionService } from '@univerjs/sheets-formula';
import { SHEET_VIEW_KEY } from '@univerjs/sheets-ui';
import { CopyCommand, PasteCommand } from '@univerjs/ui';
import { FDocument } from './docs/f-document';
import { FHooks } from './f-hooks';
import { FDataValidationBuilder } from './sheets/f-data-validation-builder';
Expand Down Expand Up @@ -313,6 +315,16 @@ export class FUniver {
return this._injector.createInstance(FFormula);
}

/**
* Get the current lifecycle stage.
*
* @returns {LifecycleStages} - The current lifecycle stage.
*/
getCurrentLifecycleStage(): LifecycleStages {
const lifecycleService = this._injector.get(LifecycleService);
return lifecycleService.stage;
}

// #region

/**
Expand Down
51 changes: 45 additions & 6 deletions packages/facade/src/apis/sheets/f-worksheet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,26 +14,27 @@
* limitations under the License.
*/

import type { ICellData, IDisposable, IFreeze, IObjectMatrixPrimitiveType, IRange, Nullable, ObjectMatrix, Workbook, Worksheet } from '@univerjs/core';
import type { ISetRangeValuesMutationParams } from '@univerjs/sheets';
import type { IDataValidationResCache } from '@univerjs/sheets-data-validation';

import type { FilterModel } from '@univerjs/sheets-filter';
import type { FWorkbook, IFICanvasFloatDom } from './f-workbook';
import { Direction, ICommandService, Inject, Injector, RANGE_TYPE } from '@univerjs/core';
import { deserializeRangeWithSheet } from '@univerjs/engine-formula';
import { copyRangeStyles, InsertColCommand, InsertRowCommand, MoveColsCommand, MoveRowsCommand, RemoveColCommand, RemoveRowCommand, SetColHiddenCommand, SetColWidthCommand, SetFrozenCommand, SetRowHeightCommand, SetRowHiddenCommand, SetSpecificColsVisibleCommand, SetSpecificRowsVisibleCommand, SetWorksheetRowIsAutoHeightCommand, SheetsSelectionsService } from '@univerjs/sheets';

import { copyRangeStyles, InsertColCommand, InsertRowCommand, MoveColsCommand, MoveRowsCommand, RemoveColCommand, RemoveRowCommand, SetColHiddenCommand, SetColWidthCommand, SetFrozenCommand, SetRangeValuesMutation, SetRowHeightCommand, SetRowHiddenCommand, SetSpecificColsVisibleCommand, SetSpecificRowsVisibleCommand, SetWorksheetRowIsAutoHeightCommand, SheetsSelectionsService } from '@univerjs/sheets';
import { DataValidationModel, SheetsDataValidationValidatorService } from '@univerjs/sheets-data-validation';
import { SheetCanvasFloatDomManagerService } from '@univerjs/sheets-drawing-ui';
import { SheetsFilterService } from '@univerjs/sheets-filter';
import { SheetsThreadCommentModel } from '@univerjs/sheets-thread-comment';
import { CancelFrozenCommand } from '@univerjs/sheets-ui';
import { ComponentManager } from '@univerjs/ui';
import type { IFreeze, IRange, Nullable, ObjectMatrix, Workbook, Worksheet } from '@univerjs/core';
import type { IDataValidationResCache } from '@univerjs/sheets-data-validation';
import type { FilterModel } from '@univerjs/sheets-filter';
import { FDataValidation } from './f-data-validation';
import { FFilter } from './f-filter';
import { FRange } from './f-range';
import { FSelection } from './f-selection';
import { FThreadComment } from './f-thread-comment';
import { covertToColRange, covertToRowRange, transformComponentKey } from './utils';
import type { FWorkbook, IFICanvasFloatDom } from './f-workbook';

export class FWorksheet {
constructor(
Expand Down Expand Up @@ -1034,4 +1035,42 @@ export class FWorksheet {
}
return freeze.startRow;
}

/**
* Subscribe to the cell data change event.
* @param callback - The callback function to be executed when the cell data changes.
* @returns - A disposable object to unsubscribe from the event.
*/
onCellDataChange(callback: (cellValue: IObjectMatrixPrimitiveType<Nullable<ICellData>>) => void): IDisposable {
const commandService = this._injector.get(ICommandService);
return commandService.onCommandExecuted((command) => {
if (command.id === SetRangeValuesMutation.id) {
const params = command.params as ISetRangeValuesMutationParams;
if (
params.unitId === this._workbook.getUnitId() &&
params.subUnitId === this._worksheet.getSheetId() &&
params.cellValue
) {
callback(params.cellValue);
}
}
});
}

/**
* Subscribe to the cell data change event.
* @param callback - The callback function to be executed before the cell data changes.
* @returns - A disposable object to unsubscribe from the event.
*/
onBeforeCellDataChange(callback: (cellValue: IObjectMatrixPrimitiveType<Nullable<ICellData>>) => void): IDisposable {
const commandService = this._injector.get(ICommandService);
return commandService.beforeCommandExecuted((command) => {
if (command.id === SetRangeValuesMutation.id) {
const params = command.params as ISetRangeValuesMutationParams;
if (params.unitId === this._workbook.getUnitId() && params.subUnitId === this._worksheet.getSheetId() && params.cellValue) {
callback(params.cellValue);
}
}
});
}
}
Loading