Skip to content

Build your own AI office with Univer Office SDK. Bring editing, calculation, collaboration, and file processing into your product for people and AI agents. Built on an extensible open-source core.

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

zhangw5fad25f6f390
diff --git a/packages/core/src/shared/__tests__/range.spec.ts b/packages/core/src/shared/__tests__/range.spec.ts
index 1bd6856cc7..d4ee236896 100644
--- a/packages/core/src/shared/__tests__/range.spec.ts
+++ b/packages/core/src/shared/__tests__/range.spec.ts
@@ -14,11 +14,11 @@
* limitations under the License.
*/
-import { describe, expect, it } from 'vitest';
+import type { IRange } from '../../sheets/typedef';
+import { describe, expect, it } from 'vitest';
import { AbsoluteRefType, RANGE_TYPE } from '../../sheets/typedef';
import { getIntersectRange, mergeHorizontalRanges, mergeRanges, mergeVerticalRanges, moveRangeByOffset, splitIntoGrid } from '../range';
-import type { IRange } from '../../sheets/typedef';
const stringifyRanges = (ranges: IRange[]) => {
return ranges.sort((a, b) => a.startRow - b.startRow || a.startColumn - b.startColumn).map((range) => `${range.startRow},${range.startColumn},${range.endRow},${range.endColumn}`);
diff --git a/packages/core/src/shared/range.ts b/packages/core/src/shared/range.ts
index 4ac7a7bd27..e725142b45 100644
--- a/packages/core/src/shared/range.ts
+++ b/packages/core/src/shared/range.ts
@@ -14,10 +14,10 @@
* limitations under the License.
*/
-import { AbsoluteRefType, RANGE_TYPE } from '../sheets/typedef';
-import { Rectangle } from './rectangle';
import type { IRange } from '../sheets/typedef';
import type { Nullable } from './types';
+import { AbsoluteRefType, RANGE_TYPE } from '../sheets/typedef';
+import { Rectangle } from './rectangle';
export function moveRangeByOffset(range: IRange, refOffsetX: number, refOffsetY: number, ignoreAbsolute = false): IRange {
let newRange = { ...range };
diff --git a/packages/core/src/shared/rectangle.ts b/packages/core/src/shared/rectangle.ts
index d0f7ae34e9..9f8b4d4862 100644
--- a/packages/core/src/shared/rectangle.ts
+++ b/packages/core/src/shared/rectangle.ts
@@ -14,9 +14,9 @@
* limitations under the License.
*/
+import type { Nullable } from './types';
import { AbsoluteRefType, type IRange, type IRectLTRB, RANGE_TYPE } from '../sheets/typedef';
import { mergeRanges, multiSubtractSingleRange } from './range';
-import type { Nullable } from './types';
/**
* This class provides a set of methods to calculate `IRange`.
diff --git a/packages/engine-formula/src/commands/mutations/set-defined-name.mutation.ts b/packages/engine-formula/src/commands/mutations/set-defined-name.mutation.ts
index 434cbcac4a..fa20a3051a 100644
--- a/packages/engine-formula/src/commands/mutations/set-defined-name.mutation.ts
+++ b/packages/engine-formula/src/commands/mutations/set-defined-name.mutation.ts
@@ -14,8 +14,8 @@
* limitations under the License.
*/
-import { CommandType } from '@univerjs/core';
import type { IAccessor, IMutation } from '@univerjs/core';
+import { CommandType } from '@univerjs/core';
import { IDefinedNamesService } from '../../services/defined-names.service';
export interface ISetDefinedNameMutationSearchParam {
diff --git a/packages/engine-formula/src/engine/utils/check-variant-error.ts b/packages/engine-formula/src/engine/utils/check-variant-error.ts
index 45ead3a35e..f0bb05d322 100644
--- a/packages/engine-formula/src/engine/utils/check-variant-error.ts
+++ b/packages/engine-formula/src/engine/utils/check-variant-error.ts
@@ -14,8 +14,8 @@
* limitations under the License.
*/
-import { ErrorType } from '../../basics/error-type';
import type { ArrayValueObject } from '../value-object/array-value-object';
+import { ErrorType } from '../../basics/error-type';
import { type BaseValueObject, ErrorValueObject } from '../value-object/base-value-object';
export function checkVariantErrorIsArray(variant: BaseValueObject): BaseValueObject {
diff --git a/packages/engine-formula/src/functions/text/regexmatch/index.ts b/packages/engine-formula/src/functions/text/regexmatch/index.ts
index b0d588a878..9c00512f7e 100644
--- a/packages/engine-formula/src/functions/text/regexmatch/index.ts
+++ b/packages/engine-formula/src/functions/text/regexmatch/index.ts
@@ -14,10 +14,10 @@
* limitations under the License.
*/
+import type { BaseValueObject } from '../../../engine/value-object/base-value-object';
import { ErrorType } from '../../../basics/error-type';
import { checkVariantsErrorIsArray } from '../../../engine/utils/check-variant-error';
import { handleRegExp } from '../../../engine/utils/regexp-check';
-import type { BaseValueObject } from '../../../engine/value-object/base-value-object';
import { ErrorValueObject } from '../../../engine/value-object/base-value-object';
import { BooleanValueObject } from '../../../engine/value-object/primitive-object';
import { BaseFunction } from '../../base-function';
diff --git a/packages/engine-formula/src/functions/text/regexreplace/index.ts b/packages/engine-formula/src/functions/text/regexreplace/index.ts
index dfeeb6e0da..9713da6b53 100644
--- a/packages/engine-formula/src/functions/text/regexreplace/index.ts
+++ b/packages/engine-formula/src/functions/text/regexreplace/index.ts
@@ -14,10 +14,10 @@
* limitations under the License.
*/
+import type { BaseValueObject } from '../../../engine/value-object/base-value-object';
import { ErrorType } from '../../../basics/error-type';
import { checkVariantsErrorIsArray } from '../../../engine/utils/check-variant-error';
import { handleRegExp } from '../../../engine/utils/regexp-check';
-import type { BaseValueObject } from '../../../engine/value-object/base-value-object';
import { ErrorValueObject } from '../../../engine/value-object/base-value-object';
import { StringValueObject } from '../../../engine/value-object/primitive-object';
import { BaseFunction } from '../../base-function';
diff --git a/packages/engine-formula/src/services/defined-names.service.ts b/packages/engine-formula/src/services/defined-names.service.ts
index 8baf4ab877..3c0829c9eb 100644
--- a/packages/engine-formula/src/services/defined-names.service.ts
+++ b/packages/engine-formula/src/services/defined-names.service.ts
@@ -14,10 +14,10 @@
* limitations under the License.
*/
-import { createIdentifier, Disposable, IUniverInstanceService } from '@univerjs/core';
-import { Subject } from 'rxjs';
import type { IUnitRange, Nullable, Workbook, Worksheet } from '@univerjs/core';
import type { Observable } from 'rxjs';
+import { createIdentifier, Disposable, IUniverInstanceService } from '@univerjs/core';
+import { Subject } from 'rxjs';
import { handleRefStringInfo, serializeRange } from '../engine/utils/reference';
export interface IDefinedNamesServiceParam {
diff --git a/packages/engine-render/src/basics/tools.ts b/packages/engine-render/src/basics/tools.ts
index dec5e513ee..907414f160 100644
--- a/packages/engine-render/src/basics/tools.ts
+++ b/packages/engine-render/src/basics/tools.ts
@@ -14,8 +14,6 @@
* limitations under the License.
*/
-import { BaselineOffset, ColorKit, DEFAULT_STYLES, FontStyleType, getCellInfoInMergeData, Rectangle, Tools } from '@univerjs/core';
-import * as cjk from 'cjk-regex';
import type {
IRange,
IRangeWithCoord,
@@ -26,11 +24,13 @@ import type {
LocaleService,
Nullable,
} from '@univerjs/core';
+import type { IDocumentSkeletonFontStyle } from './i-document-skeleton-cached';
+import type { IBoundRectNoAngle } from './vector2';
+import { BaselineOffset, ColorKit, DEFAULT_STYLES, FontStyleType, getCellInfoInMergeData, Rectangle, Tools } from '@univerjs/core';
+import * as cjk from 'cjk-regex';
import { FontCache } from '../components/docs/layout/shaping-engine/font-cache';
import { DEFAULT_FONTFACE_PLANE } from './const';
-import type { IDocumentSkeletonFontStyle } from './i-document-skeleton-cached';
-import type { IBoundRectNoAngle } from './vector2';
const DEG180 = 180;
diff --git a/packages/facade/src/apis/facade.ts b/packages/facade/src/apis/facade.ts
index f39f8a72b1..b17e2d5ee8 100644
--- a/packages/facade/src/apis/facade.ts
+++ b/packages/facade/src/apis/facade.ts
@@ -22,6 +22,7 @@ import type {
IDocumentData,
IExecutionOptions,
IWorkbookData,
+ LifecycleStages,
Nullable,
Workbook,
} from '@univerjs/core';
@@ -43,6 +44,7 @@ import {
Inject,
Injector,
IUniverInstanceService,
+ LifecycleService,
Quantity,
RedoCommand,
toDisposable,
@@ -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
/**
diff --git a/packages/facade/src/apis/sheets/f-worksheet.ts b/packages/facade/src/apis/sheets/f-worksheet.ts
index d8d8a30a35..d3cb516d19 100644
--- a/packages/facade/src/apis/sheets/f-worksheet.ts
+++ b/packages/facade/src/apis/sheets/f-worksheet.ts
@@ -14,14 +14,15 @@
* limitations under the License.
*/
-import type { IFreeze, IRange, Nullable, ObjectMatrix, Workbook, Worksheet } from '@univerjs/core';
+import type { ICellData, IDisposable, IFreeze, IRange, Nullable, 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 { Direction, ICommandService, Inject, Injector, ObjectMatrix, 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';
@@ -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: ObjectMatrix<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(new ObjectMatrix(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: ObjectMatrix<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(new ObjectMatrix(params.cellValue));
+ }
+ }
+ });
+ }
}
diff --git a/packages/sheets-conditional-formatting/src/services/calculate-unit/highlight-cell.ts b/packages/sheets-conditional-formatting/src/services/calculate-unit/highlight-cell.ts
index 91d10a9249..828f2fb114 100644
--- a/packages/sheets-conditional-formatting/src/services/calculate-unit/highlight-cell.ts
+++ b/packages/sheets-conditional-formatting/src/services/calculate-unit/highlight-cell.ts
@@ -16,16 +16,16 @@
/* eslint-disable max-lines-per-function */
+import type { IStyleBase } from '@univerjs/core';
+import type { IAverageHighlightCell, IConditionFormattingRule, IFormulaHighlightCell, IHighlightCell, INumberHighlightCell, IRankHighlightCell, ITextHighlightCell, ITimePeriodHighlightCell } from '../../models/type';
+import type { ICalculateUnit } from './type';
import { CellValueType, ObjectMatrix, Range, Rectangle, Tools } from '@univerjs/core';
import { deserializeRangeWithSheet, ERROR_TYPE_SET, generateStringWithSequence, LexerTreeBuilder, sequenceNodeType, serializeRange } from '@univerjs/engine-formula';
import dayjs from 'dayjs';
-import type { IStyleBase } from '@univerjs/core';
import { CFNumberOperator, CFRuleType, CFSubRuleType, CFTextOperator, CFTimePeriodOperator } from '../../base/const';
import { ConditionalFormattingFormulaService, FormulaResultStatus } from '../conditional-formatting-formula.service';
import { EMPTY_STYLE } from './type';
import { compareWithNumber, filterRange, getCellValue, isFloatsEqual, isNullable, serialTimeToTimestamp } from './utils';
-import type { IAverageHighlightCell, IConditionFormattingRule, IFormulaHighlightCell, IHighlightCell, INumberHighlightCell, IRankHighlightCell, ITextHighlightCell, ITimePeriodHighlightCell } from '../../models/type';
-import type { ICalculateUnit } from './type';
export const highlightCellCalculateUnit: ICalculateUnit = {
type: CFRuleType.highlightCell,
diff --git a/packages/sheets-ui/src/controllers/utils/char-tools.ts b/packages/sheets-ui/src/controllers/utils/char-tools.ts
index 4e02b7fdee..dfcbcf5c2d 100644
--- a/packages/sheets-ui/src/controllers/utils/char-tools.ts
+++ b/packages/sheets-ui/src/controllers/utils/char-tools.ts
@@ -14,9 +14,9 @@
* limitations under the License.
*/
+import type { IFunctionService, ISequenceNode, LexerTreeBuilder } from '@univerjs/engine-formula';
import { LocaleType, numfmt } from '@univerjs/core';
import { matchToken, sequenceNodeType } from '@univerjs/engine-formula';
-import type { IFunctionService, ISequenceNode, LexerTreeBuilder } from '@univerjs/engine-formula';
const fullWidthToHalfWidthMap: { [key: string]: string } = {
'0': '0', '1': '1', '2': '2', '3': '3', '4': '4',
diff --git a/packages/sheets-ui/src/views/defined-name/DefinedNameContainer.tsx b/packages/sheets-ui/src/views/defined-name/DefinedNameContainer.tsx
index 0892de9b05..32c24526dc 100644
--- a/packages/sheets-ui/src/views/defined-name/DefinedNameContainer.tsx
+++ b/packages/sheets-ui/src/views/defined-name/DefinedNameContainer.tsx
@@ -14,16 +14,16 @@
* limitations under the License.
*/
-import { ICommandService, IUniverInstanceService, LocaleService, Tools, UniverInstanceType, useDependency } from '@univerjs/core';
+import type { Nullable, Workbook } from '@univerjs/core';
+import type { IDefinedNamesServiceParam, ISetDefinedNameMutationParam } from '@univerjs/engine-formula';
+import { ICommandService, IUniverInstanceService, LocaleService, Tools, UniverInstanceType, useDependency } from '@univerjs/core';
import { Confirm, Tooltip } from '@univerjs/design';
import { IDefinedNamesService, serializeRangeWithSheet } from '@univerjs/engine-formula';
import { CheckMarkSingle, DeleteSingle, IncreaseSingle } from '@univerjs/icons';
import { InsertDefinedNameCommand, RemoveDefinedNameCommand, SetDefinedNameCommand, SetWorksheetShowCommand, SheetsSelectionsService } from '@univerjs/sheets';
import clsx from 'clsx';
import React, { useEffect, useState } from 'react';
-import type { Nullable, Workbook } from '@univerjs/core';
-import type { IDefinedNamesServiceParam, ISetDefinedNameMutationParam } from '@univerjs/engine-formula';
import { SCOPE_WORKBOOK_VALUE } from './component-name';
import { DefinedNameInput } from './DefinedNameInput';
import styles from './index.module.less';
diff --git a/packages/sheets-ui/src/views/defined-name/DefinedNameOverlay.tsx b/packages/sheets-ui/src/views/defined-name/DefinedNameOverlay.tsx
index 9ee2c4a6fb..f4e86fd7db 100644
--- a/packages/sheets-ui/src/views/defined-name/DefinedNameOverlay.tsx
+++ b/packages/sheets-ui/src/views/defined-name/DefinedNameOverlay.tsx
@@ -14,13 +14,13 @@
* limitations under the License.
*/
-import { ICommandService, IUniverInstanceService, LocaleService, UniverInstanceType, useDependency } from '@univerjs/core';
+import type { Workbook } from '@univerjs/core';
+import type { IDefinedNamesServiceParam } from '@univerjs/engine-formula';
+import { ICommandService, IUniverInstanceService, LocaleService, UniverInstanceType, useDependency } from '@univerjs/core';
import { IDefinedNamesService } from '@univerjs/engine-formula';
import { SetWorksheetShowCommand } from '@univerjs/sheets';
import React, { useEffect, useState } from 'react';
-import type { Workbook } from '@univerjs/core';
-import type { IDefinedNamesServiceParam } from '@univerjs/engine-formula';
import { SidebarDefinedNameOperation } from '../../commands/operations/sidebar-defined-name.operation';
import styles from './index.module.less';