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.
TypeScript boardcollaborationdocdocxexceloffce365officepdfpptpptxpresentationsdksheetslidesspreadsheetuniverwordwordprocessorworkspacexlsx
perf(sheet): skeleton cache (#591)
Univer07c850f603cf
diff --git a/packages/base-render/src/components/sheets/column-header.ts b/packages/base-render/src/components/sheets/column-header.tsindex b2a62325de..deae1b024a 100644--- a/packages/base-render/src/components/sheets/column-header.ts+++ b/packages/base-render/src/components/sheets/column-header.ts@@ -1,10 +1,10 @@import { getTranslateInSpreadContextWithPixelRatio } from '../../basics/draw';import { fixLineWidthByScale, getScale } from '../../basics/tools';-import { IBoundRect, Vector2 } from '../../basics/vector2';+import type { IBoundRect, Vector2 } from '../../basics/vector2';import { SheetColumnHeaderExtensionRegistry } from '../extension';-import { ColumnHeaderLayout } from './extensions/column-header-layout';+import type { ColumnHeaderLayout } from './extensions/column-header-layout';import { SpreadsheetHeader } from './sheet-component';-import { SpreadsheetSkeleton } from './sheet-skeleton';+import type { SpreadsheetSkeleton } from './sheet-skeleton';export class SpreadsheetColumnHeader extends SpreadsheetHeader {private _columnHeaderLayoutExtension!: ColumnHeaderLayout;@@ -28,7 +28,7 @@ export class SpreadsheetColumnHeader extends SpreadsheetHeader {}const parentScale = this.getParentScale();- spreadsheetSkeleton.calculate(bounds);+ spreadsheetSkeleton.calculateSegment(bounds);const segment = spreadsheetSkeleton.rowColumnSegment;diff --git a/packages/base-render/src/components/sheets/row-header.ts b/packages/base-render/src/components/sheets/row-header.tsindex 05fa81dfe4..0c484d08a5 100644--- a/packages/base-render/src/components/sheets/row-header.ts+++ b/packages/base-render/src/components/sheets/row-header.ts@@ -1,10 +1,10 @@import { getTranslateInSpreadContextWithPixelRatio } from '../../basics/draw';import { fixLineWidthByScale, getScale } from '../../basics/tools';-import { IBoundRect, Vector2 } from '../../basics/vector2';+import type { IBoundRect, Vector2 } from '../../basics/vector2';import { SheetRowHeaderExtensionRegistry } from '../extension';-import { RowHeaderLayout } from './extensions/row-header-layout';+import type { RowHeaderLayout } from './extensions/row-header-layout';import { SpreadsheetHeader } from './sheet-component';-import { SpreadsheetSkeleton } from './sheet-skeleton';+import type { SpreadsheetSkeleton } from './sheet-skeleton';export class SpreadsheetRowHeader extends SpreadsheetHeader {private _rowHeaderLayoutExtension!: RowHeaderLayout;@@ -29,7 +29,7 @@ export class SpreadsheetRowHeader extends SpreadsheetHeader {}const parentScale = this.getParentScale();- spreadsheetSkeleton.calculate(bounds);+ spreadsheetSkeleton.calculateSegment(bounds);const segment = spreadsheetSkeleton.rowColumnSegment;diff --git a/packages/base-render/src/components/sheets/sheet-skeleton.ts b/packages/base-render/src/components/sheets/sheet-skeleton.tsindex c3260fc178..08ac3445ff 100644--- a/packages/base-render/src/components/sheets/sheet-skeleton.ts+++ b/packages/base-render/src/components/sheets/sheet-skeleton.ts@@ -1,9 +1,4 @@-import {- BooleanNumber,- DEFAULT_EMPTY_DOCUMENT_VALUE,- DocumentDataModel,- getColorStyle,- HorizontalAlign,+import type {IBorderStyleData,ICellData,IColumnData,@@ -13,7 +8,6 @@ import {IRange,IRowData,ISelectionCellWithCoord,- isEmptyCell,IStyleBase,IStyleData,ITextRotation,@@ -22,19 +16,27 @@ import {LocaleService,Nullable,ObjectArrayType,- ObjectMatrix,- searchArray,Styles,TextDirection,+ Worksheet,+} from '@univerjs/core';+import {+ BooleanNumber,+ DEFAULT_EMPTY_DOCUMENT_VALUE,+ DocumentDataModel,+ getColorStyle,+ HorizontalAlign,+ isEmptyCell,+ ObjectMatrix,+ searchArray,Tools,VerticalAlign,- Worksheet,WrapStrategy,} from '@univerjs/core';import { BORDER_TYPE, COLOR_BLACK_RGB, MAXIMUM_ROW_HEIGHT, ORIENTATION_TYPE } from '../../basics/const';import { getRotateOffsetAndFarthestHypotenuse, getRotateOrientation } from '../../basics/draw';-import { IDocumentSkeletonColumn } from '../../basics/i-document-skeleton-cached';+import type { IDocumentSkeletonColumn } from '../../basics/i-document-skeleton-cached';import {degToRad,fixLineWidthByScale,@@ -46,12 +48,12 @@ import {isRectIntersect,mergeInfoOffset,} from '../../basics/tools';-import { IBoundRect } from '../../basics/vector2';+import type { IBoundRect } from '../../basics/vector2';import { columnIterator } from '../docs/common/tools';import { DocumentSkeleton } from '../docs/doc-skeleton';import { DocumentViewModel } from '../docs/view-model/document-view-model';import { Skeleton } from '../skeleton';-import { BorderCache, IStylesCache } from './interfaces';+import type { BorderCache, IStylesCache } from './interfaces';/*** Obtain the height and width of a cell's text, taking into account scenarios with rotated text.@@ -207,6 +209,8 @@ export class SpreadsheetSkeleton extends Skeleton {border: new ObjectMatrix<BorderCache>(),};+ private _renderedCellCache = new ObjectMatrix<boolean>();+private _showGridlines: BooleanNumber = BooleanNumber.TRUE;private _marginTop: number = 0;@@ -340,7 +344,7 @@ export class SpreadsheetSkeleton extends Skeleton {this._marginTop = top;}- calculate(bounds?: IBoundRect) {+ calculateSegment(bounds?: IBoundRect) {if (!this._config) {return;}@@ -351,13 +355,32 @@ export class SpreadsheetSkeleton extends Skeleton {return;}- const { mergeData } = this._config;if (bounds != null) {this._rowColumnSegment = this.getRowColumnSegment(bounds);}++ return true;+ }++ calculateWithoutClearingCache(bounds?: IBoundRect) {+ if (!this.calculateSegment(bounds)) {+ return;+ }++ const { mergeData } = this._config;+this._dataMergeCache = mergeData && this._getMergeCells(mergeData, this._rowColumnSegment);+this._calculateStylesCache();- // this._overflowCache = this._calculateOverflowCache();++ return this;+ }++ calculate(bounds?: IBoundRect) {+ this._resetCache();++ this.calculateWithoutClearingCache(bounds);+return this;}@@ -1391,8 +1414,6 @@ export class SpreadsheetSkeleton extends Skeleton {// eslint-disable-next-line max-lines-per-functionprivate _calculateStylesCache() {- this._resetCache();-const dataMergeCache = this._dataMergeCache;const rowColumnSegment = this._rowColumnSegment;const columnWidthAccumulation = this.columnWidthAccumulation;@@ -1440,9 +1461,19 @@ export class SpreadsheetSkeleton extends Skeleton {font: {},border: new ObjectMatrix<BorderCache>(),};++ this._renderedCellCache = new ObjectMatrix<boolean>();}private _setCellCache(r: number, c: number, skipBackgroundAndBorder: boolean) {+ const needsRendering = this._renderedCellCache.getValue(r, c);++ if (needsRendering || r === -1 || c === -1) {+ return true;+ }++ this._renderedCellCache.setValue(r, c, true);+if (!this._worksheet) {return true;}diff --git a/packages/base-render/src/components/sheets/spreadsheet.ts b/packages/base-render/src/components/sheets/spreadsheet.tsindex fbeebdad50..81fb13d7da 100644--- a/packages/base-render/src/components/sheets/spreadsheet.ts+++ b/packages/base-render/src/components/sheets/spreadsheet.ts@@ -1,32 +1,27 @@-import {- CellValueType,- HorizontalAlign,- IRange,- Nullable,- ObjectMatrix,- sortRules,- WrapStrategy,-} from '@univerjs/core';--import { BaseObject } from '../../base-object';+import type { IRange, Nullable } from '@univerjs/core';+import { CellValueType, HorizontalAlign, ObjectMatrix, sortRules, WrapStrategy } from '@univerjs/core';++import type { BaseObject } from '../../base-object';import { RENDER_CLASS_TYPE } from '../../basics/const';import { getTranslateInSpreadContextWithPixelRatio } from '../../basics/draw';-import { ITransformChangeState } from '../../basics/interfaces';+import type { ITransformChangeState } from '../../basics/interfaces';import { fixLineWidthByScale, getCellByIndex, getCellPositionByIndex, getScale } from '../../basics/tools';-import { IBoundRect, Vector2 } from '../../basics/vector2';+import type { IBoundRect } from '../../basics/vector2';+import { Vector2 } from '../../basics/vector2';import { Canvas } from '../../canvas';-import { Engine } from '../../engine';-import { Scene } from '../../scene';-import { SceneViewer } from '../../scene-viewer';-import { Viewport } from '../../viewport';+import type { Engine } from '../../engine';+import type { Scene } from '../../scene';+import type { SceneViewer } from '../../scene-viewer';+import type { Viewport } from '../../viewport';import { Documents } from '../docs/document';import { SpreadsheetExtensionRegistry } from '../extension';-import { Background } from './extensions/background';-import { Border } from './extensions/border';-import { BorderAuxiliary } from './extensions/border-auxiliary';-import { Font } from './extensions/font';+import type { Background } from './extensions/background';+import type { Border } from './extensions/border';+import type { BorderAuxiliary } from './extensions/border-auxiliary';+import type { Font } from './extensions/font';import { SheetComponent } from './sheet-component';-import { getDocsSkeletonPageSize, SpreadsheetSkeleton } from './sheet-skeleton';+import type { SpreadsheetSkeleton } from './sheet-skeleton';+import { getDocsSkeletonPageSize } from './sheet-skeleton';const OBJECT_KEY = '__SHEET_EXTENSION_FONT_DOCUMENT_INSTANCE__';@@ -108,7 +103,7 @@ export class Spreadsheet extends SheetComponent {}const parentScale = this.getParentScale();- spreadsheetSkeleton.calculate(bounds);+ spreadsheetSkeleton.calculateWithoutClearingCache(bounds);const segment = spreadsheetSkeleton.rowColumnSegment;diff --git a/packages/base-sheets/src/basics/const/command-listener-const.ts b/packages/base-sheets/src/basics/const/command-listener-const.tsindex 5849927e6e..093de7b3d9 100644--- a/packages/base-sheets/src/basics/const/command-listener-const.ts+++ b/packages/base-sheets/src/basics/const/command-listener-const.ts@@ -1,5 +1,6 @@import { InsertColMutation, InsertRowMutation } from '../../commands/mutations/insert-row-col.mutation';-import { MoveRowsMutation } from '../../commands/mutations/move-rows-cols.mutation';+import { MoveRangeMutation } from '../../commands/mutations/move-range.mutation';+import { MoveColsMutation, MoveRowsMutation } from '../../commands/mutations/move-rows-cols.mutation';import { RemoveColMutation, RemoveRowMutation } from '../../commands/mutations/remove-row-col.mutation';import { SetColHiddenMutation, SetColVisibleMutation } from '../../commands/mutations/set-col-visible.mutation';import { SetRangeValuesMutation } from '../../commands/mutations/set-range-values.mutation';@@ -23,10 +24,11 @@ export const COMMAND_LISTENER_SKELETON_CHANGE = [InsertColMutation.id,RemoveColMutation.id,MoveRowsMutation.id,+ MoveColsMutation.id,SetColHiddenMutation.id,SetColVisibleMutation.id,SetRowHiddenMutation.id,SetRowVisibleMutation.id,];-export const COMMAND_LISTENER_VALUE_CHANGE = [SetRangeValuesMutation.id];+export const COMMAND_LISTENER_VALUE_CHANGE = [SetRangeValuesMutation.id, MoveRangeMutation.id];diff --git a/packages/base-sheets/src/index.ts b/packages/base-sheets/src/index.tsindex 88f32defec..f96ae4640c 100644--- a/packages/base-sheets/src/index.ts+++ b/packages/base-sheets/src/index.ts@@ -1,7 +1,7 @@export * from './base-sheets-plugin';// #region services-export { COMMAND_LISTENER_SKELETON_CHANGE } from './basics/const/command-listener-const';+export { COMMAND_LISTENER_SKELETON_CHANGE, COMMAND_LISTENER_VALUE_CHANGE } from './basics/const/command-listener-const';export {type IAddWorksheetMergeMutationParams,type IDeleteRangeMutationParams,diff --git a/packages/sheets-plugin-numfmt/src/controllers/numfmt.controller.ts b/packages/sheets-plugin-numfmt/src/controllers/numfmt.controller.tsindex 35428e8114..3a9137e2ea 100644--- a/packages/sheets-plugin-numfmt/src/controllers/numfmt.controller.ts+++ b/packages/sheets-plugin-numfmt/src/controllers/numfmt.controller.ts@@ -1,9 +1,9 @@import numfmt from '@univerjs/base-numfmt-engine';import { IRenderManagerService } from '@univerjs/base-render';+import type { EffectRefRangeParams } from '@univerjs/base-sheets';import {ClearSelectionAllCommand,ClearSelectionFormatCommand,- EffectRefRangeParams,EffectRefRangId,handleDeleteRangeMoveLeft,handleDeleteRangeMoveUp,@@ -20,19 +20,16 @@ import {SetRangeValuesCommand,} from '@univerjs/base-sheets';import { ComponentManager, IMenuService, ISidebarService } from '@univerjs/base-ui';+import type { ICellData, IMutationInfo, IRange, Nullable } from '@univerjs/core';import {CellValueType,Disposable,DisposableCollection,- ICellData,ICommandService,- IMutationInfo,INTERCEPTOR_POINT,- IRange,IUniverInstanceService,LocaleService,LocaleType,- Nullable,ObjectMatrix,Range,Rectangle,@@ -40,44 +37,40 @@ import {ThemeService,toDisposable,} from '@univerjs/core';-import {- APPLY_TYPE,- getRepeatRange,- IAutoFillHook,- IAutoFillService,- SheetSkeletonManagerService,-} from '@univerjs/ui-plugin-sheets';-import { IDisposable, Inject, Injector } from '@wendellhu/redi';+import type { IAutoFillHook } from '@univerjs/ui-plugin-sheets';+import { APPLY_TYPE, getRepeatRange, IAutoFillService, SheetSkeletonManagerService } from '@univerjs/ui-plugin-sheets';+import type { IDisposable } from '@wendellhu/redi';+import { Inject, Injector } from '@wendellhu/redi';import { combineLatest, Observable } from 'rxjs';import { bufferTime, distinctUntilChanged, filter, map, switchMap, tap } from 'rxjs/operators';import { SHEET_NUMFMT_PLUGIN } from '../base/const/PLUGIN_NAME';import { AddDecimalCommand } from '../commands/commands/add.decimal.command';import { SetCurrencyCommand } from '../commands/commands/set.currency.command';-import { SetNumfmtCommand, SetNumfmtCommandParams } from '../commands/commands/set.numfmt.command';+import type { SetNumfmtCommandParams } from '../commands/commands/set.numfmt.command';+import { SetNumfmtCommand } from '../commands/commands/set.numfmt.command';import { SubtractDecimalCommand } from '../commands/commands/subtract.decimal.command';-import {- factorySetNumfmtUndoMutation,- SetNumfmtMutation,- SetNumfmtMutationParams,-} from '../commands/mutations/set.numfmt.mutation';+import type { SetNumfmtMutationParams } from '../commands/mutations/set.numfmt.mutation';+import { factorySetNumfmtUndoMutation, SetNumfmtMutation } from '../commands/mutations/set.numfmt.mutation';import { CloseNumfmtPanelOperator } from '../commands/operators/close.numfmt.panel.operator';import { OpenNumfmtPanelOperator } from '../commands/operators/open.numfmt.panel.operator';-import { SheetNumfmtPanel, SheetNumfmtPanelProps } from '../components/index';+import type { SheetNumfmtPanelProps } from '../components/index';+import { SheetNumfmtPanel } from '../components/index';import { zhCn } from '../locale/zh-CN';import { AddDecimalMenuItem, CurrencyMenuItem, FactoryOtherMenuItem, SubtractDecimalMenuItem } from '../menu/menu';-import { INumfmtService, NumfmtItemWithCache } from '../service/type';+import type { NumfmtItemWithCache } from '../service/type';+import { INumfmtService } from '../service/type';import { getPatternPreview, getPatternType } from '../utils/pattern';import type { INumfmtController } from './type';const createCollectEffectMutation = () => {- type Config = {+ interface Config {workbookId: string;worksheetId: string;row: number;col: number;value: Nullable<NumfmtItemWithCache>;- };+ }let list: Config[] = [];const add = (workbookId: string,@@ -129,6 +122,7 @@ export class NumfmtController extends Disposable implements INumfmtController {this._initPanel();this._initCommands();this._initAutoFill();+ this._commandExecutedListener();}openPanel = () => {@@ -166,6 +160,7 @@ export class NumfmtController extends Disposable implements INumfmtController {onChange: (config) => {if (config.type === 'change') {this._previewPattern = config.value;+ this._sheetSkeletonManagerService.reCalculate();this._renderManagerService.getRenderById(workbook.getUnitId())?.mainComponent?.makeDirty();} else if (config.type === 'confirm') {const selections = selectionManagerService.getSelectionRanges() || [];@@ -712,6 +707,18 @@ export class NumfmtController extends Disposable implements INumfmtController {);}+ private _commandExecutedListener() {+ this.disposeWithMe(+ this._commandService.onCommandExecuted((command) => {+ if (SetNumfmtCommand.id === command.id) {+ this._sheetSkeletonManagerService.reCalculate();+ // TODO: @Gggpound The command requires the parameters unitId and sheetId to be passed in.+ // this._renderManagerService.getRenderById(unitId)?.mainComponent?.makeDirty();+ }+ })+ );+ }+private _registerRefRange() {this.disposeWithMe(toDisposable(diff --git a/packages/ui-plugin-sheets/src/controllers/sheet-render.controller.ts b/packages/ui-plugin-sheets/src/controllers/sheet-render.controller.tsindex fd2779070e..96181dfcfd 100644--- a/packages/ui-plugin-sheets/src/controllers/sheet-render.controller.ts+++ b/packages/ui-plugin-sheets/src/controllers/sheet-render.controller.ts@@ -1,18 +1,14 @@-import {- IRenderManagerService,- Rect,- Spreadsheet,- SpreadsheetColumnHeader,- SpreadsheetRowHeader,-} from '@univerjs/base-render';+import type { Rect, Spreadsheet, SpreadsheetColumnHeader, SpreadsheetRowHeader } from '@univerjs/base-render';+import { IRenderManagerService } from '@univerjs/base-render';import {COMMAND_LISTENER_SKELETON_CHANGE,+ COMMAND_LISTENER_VALUE_CHANGE,SelectionManagerService,SetWorksheetActivateMutation,} from '@univerjs/base-sheets';+import type { ICommandInfo } from '@univerjs/core';import {Disposable,- ICommandInfo,ICommandService,IUniverInstanceService,LifecycleStages,@@ -131,10 +127,11 @@ export class SheetRenderController extends Disposable {sheetId,commandId: command.id,});+ } else if (COMMAND_LISTENER_VALUE_CHANGE.includes(command.id)) {+ this._sheetSkeletonManagerService.reCalculate();}- // else if (COMMAND_LISTENER_VALUE_CHANGE.includes(command.id)) {- // this._sheetSkeletonManagerService.reCalculate();- // }++ // this._sheetSkeletonManagerService.reCalculate();this._renderManagerService.getRenderById(unitId)?.mainComponent?.makeDirty(); // refresh spreadsheet})