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.

perf: fix permission perf (#3618)

ybzky56f248cc69e2
diff --git a/packages/sheets-ui/src/controllers/menu/__tests__/create-menu-test-bed.ts b/packages/sheets-ui/src/controllers/menu/__tests__/create-menu-test-bed.ts
index 3c6fdf284b..356de1129e 100644
--- a/packages/sheets-ui/src/controllers/menu/__tests__/create-menu-test-bed.ts
+++ b/packages/sheets-ui/src/controllers/menu/__tests__/create-menu-test-bed.ts
@@ -16,7 +16,8 @@
import type { IWorkbookData } from '@univerjs/core';
import { Inject, Injector, LocaleType, Plugin, Univer, UniverInstanceType } from '@univerjs/core';
-import { RangeProtectionRefRangeService, RangeProtectionRenderModel, RangeProtectionRuleModel, RangeProtectionService, RefRangeService, SheetInterceptorService,
+import { IRenderManagerService, RenderManagerService } from '@univerjs/engine-render';
+import { RangeProtectionCache, RangeProtectionRefRangeService, RangeProtectionRenderModel, RangeProtectionRuleModel, RangeProtectionService, RefRangeService, SheetInterceptorService,
SheetsSelectionsService,
WorkbookPermissionService,
WorksheetPermissionService,
@@ -24,7 +25,6 @@ import { RangeProtectionRefRangeService, RangeProtectionRenderModel, RangeProtec
WorksheetProtectionRuleModel,
} from '@univerjs/sheets';
import { IMenuManagerService, IPlatformService, IShortcutService, MenuManagerService, PlatformService, ShortcutService } from '@univerjs/ui';
-import { IRenderManagerService, RenderManagerService } from '@univerjs/engine-render';
const TEST_WORKBOOK_DATA_DEMO: IWorkbookData = {
id: 'test',
@@ -77,6 +77,7 @@ export function createMenuTestBed() {
injector.add([RangeProtectionRefRangeService]);
injector.add([RangeProtectionRenderModel]);
+ injector.add([RangeProtectionCache]);
injector.add([RangeProtectionRuleModel]);
injector.add([RangeProtectionService]);
}
diff --git a/packages/sheets-ui/src/controllers/permission/sheet-permission-init.controller.ts b/packages/sheets-ui/src/controllers/permission/sheet-permission-init.controller.ts
index ae626ba011..286adcf850 100644
--- a/packages/sheets-ui/src/controllers/permission/sheet-permission-init.controller.ts
+++ b/packages/sheets-ui/src/controllers/permission/sheet-permission-init.controller.ts
@@ -14,16 +14,15 @@
* limitations under the License.
*/
+import type { Workbook } from '@univerjs/core';
+import type { IAddRangeProtectionMutationParams, IAddWorksheetProtectionParams, ISetWorksheetPermissionPointsMutationParams, IWorksheetProtectionRenderCellData } from '@univerjs/sheets';
+
import { Disposable, IAuthzIoService, ICommandService, Inject, InterceptorEffectEnum, IPermissionService, IUndoRedoService, IUniverInstanceService, LifecycleStages, OnLifecycle, UniverInstanceType, UserManagerService } from '@univerjs/core';
import { UnitAction, UnitObject } from '@univerjs/protocol';
-import { AddRangeProtectionMutation, AddWorksheetProtectionMutation, defaultWorkbookPermissionPoints, defaultWorksheetPermissionPoint, getAllRangePermissionPoint, getAllWorkbookPermissionPoint, getAllWorksheetPermissionPoint, getAllWorksheetPermissionPointByPointPanel, INTERCEPTOR_POINT, RangeProtectionRenderModel, RangeProtectionRuleModel, SetWorksheetPermissionPointsMutation, SheetInterceptorService, WorksheetEditPermission, WorksheetProtectionPointModel, WorksheetProtectionRuleModel, WorksheetViewPermission } from '@univerjs/sheets';
-import { IDialogService } from '@univerjs/ui';
-import type { Workbook } from '@univerjs/core';
-
-import type { IAddRangeProtectionMutationParams, IAddWorksheetProtectionParams, IRangeProtectionRule, ISetWorksheetPermissionPointsMutationParams, IWorksheetProtectionRenderCellData } from '@univerjs/sheets';
+import { AddRangeProtectionMutation, AddWorksheetProtectionMutation, defaultWorkbookPermissionPoints, defaultWorksheetPermissionPoint, getAllRangePermissionPoint, getAllWorkbookPermissionPoint, getAllWorksheetPermissionPoint, getAllWorksheetPermissionPointByPointPanel, INTERCEPTOR_POINT, RangeProtectionCache, RangeProtectionRenderModel, RangeProtectionRuleModel, SetWorksheetPermissionPointsMutation, SheetInterceptorService, WorksheetEditPermission, WorksheetProtectionPointModel, WorksheetProtectionRuleModel, WorksheetViewPermission } from '@univerjs/sheets';
-import type { IRangeProtectionRenderCellData } from '../../views/permission/extensions/range-protection.render';
+import { IDialogService } from '@univerjs/ui';
@OnLifecycle(LifecycleStages.Rendered, SheetPermissionInitController)
export class SheetPermissionInitController extends Disposable {
@@ -39,7 +38,8 @@ export class SheetPermissionInitController extends Disposable {
@Inject(SheetInterceptorService) private _sheetInterceptorService: SheetInterceptorService,
@Inject(RangeProtectionRenderModel) private _selectionProtectionRenderModel: RangeProtectionRenderModel,
@Inject(IUndoRedoService) private _undoRedoService: IUndoRedoService,
- @Inject(ICommandService) private _commandService: ICommandService
+ @Inject(ICommandService) private _commandService: ICommandService,
+ @Inject(RangeProtectionCache) private _rangeProtectionCache: RangeProtectionCache
) {
super();
this._initRangePermissionFromSnapshot();
@@ -348,22 +348,11 @@ export class SheetPermissionInitController extends Disposable {
handler: (cell = {}, context, next) => {
const { unitId, subUnitId, row, col } = context;
- const length = this._rangeProtectionRuleModel.getSubunitRuleListLength(unitId, subUnitId);
- if (length === 0) {
- return next(cell);
- }
- const permissionList = this._selectionProtectionRenderModel.getCellInfo(unitId, subUnitId, row, col)
- .filter((p) => !!p.ruleId)
- .map((p) => {
- const rule = this._rangeProtectionRuleModel.getRule(unitId, subUnitId, p.ruleId!) || {} as IRangeProtectionRule;
- return {
- ...p, ranges: rule.ranges!,
- };
- })
- .filter((p) => !!p.ranges);
- if (permissionList.length) {
- const isSkipRender = permissionList.some((p) => !p?.[UnitAction.View]);
- const _cellData: IRangeProtectionRenderCellData = { ...cell, selectionProtection: permissionList };
+ const selectionProtection = this._rangeProtectionCache.getCellInfo(unitId, subUnitId, row, col);
+
+ if (selectionProtection) {
+ const isSkipRender = selectionProtection[UnitAction.View] === false;
+ const _cellData = { ...cell, selectionProtection: [selectionProtection] };
if (isSkipRender) {
delete _cellData.s;
delete _cellData.v;
diff --git a/packages/sheets-ui/src/controllers/permission/sheet-permission-interceptor-formula-render.controller.ts b/packages/sheets-ui/src/controllers/permission/sheet-permission-interceptor-formula-render.controller.ts
index f1105db322..d0cbc1d655 100644
--- a/packages/sheets-ui/src/controllers/permission/sheet-permission-interceptor-formula-render.controller.ts
+++ b/packages/sheets-ui/src/controllers/permission/sheet-permission-interceptor-formula-render.controller.ts
@@ -15,11 +15,11 @@
*/
import type { Workbook } from '@univerjs/core';
-import { DisposableCollection, Inject, Injector, IPermissionService, IUniverInstanceService, LifecycleStages, OnLifecycle, RxDisposable } from '@univerjs/core';
-import { getSheetCommandTarget, RangeProtectionRenderModel, WorksheetViewPermission } from '@univerjs/sheets';
import type { IRenderContext, IRenderModule } from '@univerjs/engine-render';
-import { UnitAction } from '@univerjs/protocol';
+import { DisposableCollection, Inject, Injector, IPermissionService, IUniverInstanceService, LifecycleStages, OnLifecycle, RxDisposable } from '@univerjs/core';
import { NullValueObject } from '@univerjs/engine-formula';
+import { UnitAction } from '@univerjs/protocol';
+import { getSheetCommandTarget, RangeProtectionCache, RangeProtectionRenderModel, WorksheetViewPermission } from '@univerjs/sheets';
import { StatusBarController } from '../status-bar.controller';
export const SHEET_PERMISSION_PASTE_PLUGIN = 'SHEET_PERMISSION_PASTE_PLUGIN';
@@ -34,7 +34,8 @@ export class SheetPermissionInterceptorFormulaRenderController extends RxDisposa
@IUniverInstanceService private readonly _univerInstanceService: IUniverInstanceService,
@IPermissionService private readonly _permissionService: IPermissionService,
@Inject(StatusBarController) private readonly _statusBarController: StatusBarController,
- @Inject(RangeProtectionRenderModel) private _rangeProtectionRenderModel: RangeProtectionRenderModel
+ @Inject(RangeProtectionRenderModel) private _rangeProtectionRenderModel: RangeProtectionRenderModel,
+ @Inject(RangeProtectionCache) private _rangeProtectionCache: RangeProtectionCache
) {
super();
this._initStatusBarPermissionInterceptor();
@@ -71,7 +72,7 @@ export class SheetPermissionInterceptorFormulaRenderController extends RxDisposa
if (cellValue === undefined) {
return;
}
- const permission = this._rangeProtectionRenderModel.getCellInfo(unitId, subUnitId, rowIndex + startRow, colIndex + startCol)?.[0];
+ const permission = this._rangeProtectionCache.getCellInfo(unitId, subUnitId, rowIndex + startRow, colIndex + startCol);
if (permission?.[UnitAction.View] === false) {
itemValue[rowIndex][colIndex] = NullValueObject.create();
}
diff --git a/packages/sheets/src/index.ts b/packages/sheets/src/index.ts
index 87ac0a5234..2710422553 100644
--- a/packages/sheets/src/index.ts
+++ b/packages/sheets/src/index.ts
@@ -148,6 +148,7 @@ export { checkRangesEditablePermission } from './services/permission/util';
// range-protection
export { type ICellPermission, RangeProtectionRenderModel } from './model/range-protection-render.model';
export { type IModel, type IObjectModel, type IRangeProtectionRule, RangeProtectionRuleModel } from './model/range-protection-rule.model';
+export { RangeProtectionCache } from './model/range-protection.cache';
export type { IWorksheetProtectionRenderCellData } from './services/permission/worksheet-permission/type';
export { RangeProtectionRefRangeService } from './services/permission/range-permission/range-protection.ref-range';
diff --git a/packages/sheets/src/model/range-protection-rule.model.ts b/packages/sheets/src/model/range-protection-rule.model.ts
index f6cd3db567..a75f4a95d9 100644
--- a/packages/sheets/src/model/range-protection-rule.model.ts
+++ b/packages/sheets/src/model/range-protection-rule.model.ts
@@ -14,11 +14,11 @@
* limitations under the License.
*/
-import { LifecycleStages, OnLifecycle, Tools } from '@univerjs/core';
-import { BehaviorSubject, Subject } from 'rxjs';
import type { IRange } from '@univerjs/core';
-
import type { UnitObject } from '@univerjs/protocol';
+import { LifecycleStages, OnLifecycle, Tools } from '@univerjs/core';
+
+import { BehaviorSubject, Subject } from 'rxjs';
export interface IRangeProtectionRule {
ranges: IRange[];
@@ -34,6 +34,14 @@ export type IObjectModel = Record<string, Record<string, IRangeProtectionRule[]>
export type IModel = Map<string, Map<string, Map<string, IRangeProtectionRule>>>;
+export interface IRuleChange {
+ unitId: string;
+ subUnitId: string;
+ rule: IRangeProtectionRule;
+ oldRule?: IRangeProtectionRule;
+ type: IRuleChangeType;
+}
+
type IRuleChangeType = 'add' | 'set' | 'delete';
@OnLifecycle(LifecycleStages.Starting, RangeProtectionRuleModel)
@@ -44,13 +52,7 @@ export class RangeProtectionRuleModel {
*/
private _model: IModel = new Map();
- private _ruleChange = new Subject<{
- unitId: string;
- subUnitId: string;
- rule: IRangeProtectionRule;
- oldRule?: IRangeProtectionRule;
- type: IRuleChangeType;
- }>();
+ private _ruleChange = new Subject<IRuleChange>();
ruleChange$ = this._ruleChange.asObservable();
diff --git a/packages/sheets/src/model/range-protection.cache.ts b/packages/sheets/src/model/range-protection.cache.ts
new file mode 100644
index 0000000000..81cf41ab53
--- /dev/null
+++ b/packages/sheets/src/model/range-protection.cache.ts
@@ -0,0 +1,237 @@
+/**
+ * Copyright 2023-present DreamNum Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+import type { IRange, Workbook } from '@univerjs/core';
+import type { IRangePermissionPoint } from '../services/permission/range-permission/util';
+import type { IRuleChange } from './range-protection-rule.model';
+import { Disposable, Inject, IPermissionService, IUniverInstanceService, Range, UniverInstanceType } from '@univerjs/core';
+import { UnitAction, UnitObject } from '@univerjs/protocol';
+import { filter, map } from 'rxjs';
+import { RangeProtectionPermissionEditPoint, RangeProtectionPermissionViewPoint } from '../services/permission/permission-point';
+import { RangeProtectionRuleModel } from './range-protection-rule.model';
+
+export class RangeProtectionCache extends Disposable {
+ private readonly _cellRuleCache: Map<string, Map<string, Map<string, string>>> = new Map();
+ private readonly _permissionIdCache: Map<string, string> = new Map();
+ private readonly _cellInfoCache: Map<string, Map<string, Map<string, Partial<Record<UnitAction, boolean>> & { ruleId?: string; ranges?: IRange[] }>>> = new Map();
+
+ constructor(
+ @Inject(RangeProtectionRuleModel) private readonly _ruleModel: RangeProtectionRuleModel,
+ @Inject(IPermissionService) private readonly _permissionService: IPermissionService,
+ @Inject(IUniverInstanceService) private readonly _univerInstanceService: IUniverInstanceService
+ ) {
+ super();
+ this._initUpdateCellRuleCache();
+ this._initUpdateCellInfoCache();
+ this._initCache();
+ }
+
+ private _initCache() {
+ this._univerInstanceService.getAllUnitsForType<Workbook>(UniverInstanceType.UNIVER_SHEET).forEach((workbook) => {
+ workbook.getSheets().forEach((sheet) => {
+ const unitId = workbook.getUnitId();
+ const subUnitId = sheet.getSheetId();
+ this.reBuildCache(unitId, subUnitId);
+ });
+ });
+ }
+
+ private _initUpdateCellInfoCache() {
+ this._permissionService.permissionPointUpdate$.pipe(
+ filter((permission) => permission.type === UnitObject.SelectRange),
+ map((permission) => permission as IRangePermissionPoint)
+ ).subscribe((permission) => {
+ const { subUnitId, unitId, permissionId } = permission;
+ const ruleId = this._permissionIdCache.get(permissionId);
+ if (!ruleId) {
+ return;
+ }
+ const ruleInstance = this._ruleModel.getRule(unitId, subUnitId, ruleId);
+ if (!ruleInstance) {
+ return;
+ }
+
+ const cellInfoMap = this._ensureCellInfoMap(unitId, subUnitId);
+ ruleInstance.ranges.forEach((range) => {
+ const { startRow, endRow, startColumn, endColumn } = range;
+ for (let i = startRow; i <= endRow; i++) {
+ for (let j = startColumn; j <= endColumn; j++) {
+ cellInfoMap.delete(`${i}-${j}`);
+ }
+ }
+ });
+ });
+
+ this._ruleModel.ruleChange$.subscribe((info) => {
+ const { unitId, subUnitId } = info;
+ const cellInfoMap = this._ensureCellInfoMap(unitId, subUnitId);
+ info.rule.ranges.forEach((range) => {
+ Range.foreach(range, (row, col) => {
+ cellInfoMap.delete(`${row}-${col}`);
+ });
+ });
+ if (info.type === 'set') {
+ info.oldRule?.ranges.forEach((range) => {
+ Range.foreach(range, (row, col) => {
+ this._cellInfoCache.delete(`${row}-${col}`);
+ });
+ });
+ }
+ });
+ }
+
+ private _initUpdateCellRuleCache() {
+ this._ruleModel.ruleChange$.subscribe((ruleChange) => {
+ const { type } = ruleChange;
+ if (type === 'add') {
+ this._addCellRuleCache(ruleChange);
+ } else if (type === 'delete') {
+ this._deleteCellRuleCache(ruleChange);
+ } else {
+ this._deleteCellRuleCache({ ...ruleChange, rule: ruleChange.oldRule! });
+ this._addCellRuleCache(ruleChange);
+ }
+ });
+ }
+
+ private _ensureRuleMap(unitId: string, subUnitId: string) {
+ let subUnitMap = this._cellRuleCache.get(unitId);
+ if (!subUnitMap) {
+ subUnitMap = new Map();
+ this._cellRuleCache.set(unitId, subUnitMap);
+ }
+ let cellMap = subUnitMap.get(subUnitId);
+
+ if (!cellMap) {
+ cellMap = new Map<string, string>();
+ subUnitMap.set(subUnitId, cellMap);
+ }
+ return cellMap;
+ }
+
+ private _ensureCellInfoMap(unitId: string, subUnitId: string) {
+ let subUnitMap = this._cellInfoCache.get(unitId);
+ if (!subUnitMap) {
+ subUnitMap = new Map();
+ this._cellInfoCache.set(unitId, subUnitMap);
+ }
+ let cellMap = subUnitMap.get(subUnitId);
+
+ if (!cellMap) {
+ cellMap = new Map<string, Partial<Record<UnitAction, boolean>> & { ruleId?: string; ranges?: IRange[] }>();
+ subUnitMap.set(subUnitId, cellMap);
+ }
+ return cellMap;
+ }
+
+ private _addCellRuleCache(ruleChange: IRuleChange) {
+ const { subUnitId, unitId, rule } = ruleChange;
+ const cellMap = this._ensureRuleMap(unitId, subUnitId);
+ rule.ranges.forEach((range) => {
+ const { startRow, endRow, startColumn, endColumn } = range;
+ for (let i = startRow; i <= endRow; i++) {
+ for (let j = startColumn; j <= endColumn; j++) {
+ cellMap.set(`${i}-${j}`, rule.id);
+ }
+ }
+ });
+ this._permissionIdCache.set(rule.permissionId, rule.id);
+ }
+
+ private _deleteCellRuleCache(ruleChange: IRuleChange) {
+ const { subUnitId, unitId, rule } = ruleChange;
+ const cellMap = this._ensureRuleMap(unitId, subUnitId);
+ const cellInfoMap = this._ensureCellInfoMap(unitId, subUnitId);
+ rule.ranges.forEach((range) => {
+ const { startRow, endRow, startColumn, endColumn } = range;
+ for (let i = startRow; i <= endRow; i++) {
+ for (let j = startColumn; j <= endColumn; j++) {
+ cellMap.delete(`${i}-${j}`);
+ cellInfoMap.delete(`${i}-${j}`);
+ }
+ }
+ });
+ this._permissionIdCache.delete(rule.permissionId);
+ }
+
+ public reBuildCache(unitId: string, subUnitId: string) {
+ const cellRuleMap = this._ensureRuleMap(unitId, subUnitId);
+ const cellInfoMap = this._ensureCellInfoMap(unitId, subUnitId);
+ cellRuleMap.clear();
+ cellInfoMap.clear();
+ this._ruleModel.getSubunitRuleList(unitId, subUnitId).forEach((rule) => {
+ const edit = this._permissionService.getPermissionPoint(new RangeProtectionPermissionEditPoint(unitId, subUnitId, rule.permissionId)?.id)?.value ?? true;
+ const view = this._permissionService.getPermissionPoint(new RangeProtectionPermissionViewPoint(unitId, subUnitId, rule.permissionId)?.id)?.value ?? true;
+ const selectionProtection = {
+ [UnitAction.Edit]: edit,
+ [UnitAction.View]: view,
+ ruleId: rule.id,
+ ranges: rule.ranges,
+ };
+ rule.ranges.forEach((range) => {
+ const { startRow, endRow, startColumn, endColumn } = range;
+ for (let i = startRow; i <= endRow; i++) {
+ for (let j = startColumn; j <= endColumn; j++) {
+ cellRuleMap.set(`${i}-${j}`, rule.id);
+ cellInfoMap.set(`${i}-${j}`, selectionProtection);
+ }
+ }
+ });
+ this._permissionIdCache.set(rule.permissionId, rule.id);
+ });
+ }
+
+ public getCellInfo(unitId: string, subUnitId: string, row: number, col: number) {
+ const cellMap = this._ensureCellInfoMap(unitId, subUnitId);
+ const cacheValue = cellMap.get(`${row}-${col}`);
+ if (cacheValue) {
+ return cacheValue;
+ }
+
+ let view = true;
+ let edit = true;
+ const ruleId = this._cellRuleCache.get(unitId)?.get(subUnitId)?.get(`${row}-${col}`);
+ if (!ruleId) {
+ return;
+ }
+
+ const rule = this._ruleModel.getRule(unitId, subUnitId, ruleId);
+ if (rule) {
+ view = this._permissionService.getPermissionPoint(new RangeProtectionPermissionViewPoint(unitId, subUnitId, rule.permissionId)?.id)?.value ?? true;
+ edit = this._permissionService.getPermissionPoint(new RangeProtectionPermissionEditPoint(unitId, subUnitId, rule.permissionId)?.id)?.value ?? true;
+ const selectionProtection = {
+ [UnitAction.Edit]: edit,
+ [UnitAction.View]: view,
+ ruleId,
+ ranges: rule.ranges,
+ };
+ cellMap.set(`${row}-${col}`, selectionProtection);
+ return selectionProtection;
+ }
+ }
+
+ public deleteUnit(unitId: string) {
+ this._cellRuleCache.delete(unitId);
+ this._cellInfoCache.delete(unitId);
+ const workbook = this._univerInstanceService.getUnit<Workbook>(unitId);
+ workbook?.getSheets().forEach((sheet) => {
+ const subUnitId = sheet.getSheetId();
+ this._ruleModel.getSubunitRuleList(unitId, subUnitId).forEach((rule) => {
+ this._permissionIdCache.delete(rule.permissionId);
+ });
+ });
+ }
+}
diff --git a/packages/sheets/src/services/permission/range-permission/range-protection.ref-range.ts b/packages/sheets/src/services/permission/range-permission/range-protection.ref-range.ts
index e70a732383..ff9be56c4a 100644
--- a/packages/sheets/src/services/permission/range-permission/range-protection.ref-range.ts
+++ b/packages/sheets/src/services/permission/range-permission/range-protection.ref-range.ts
@@ -14,41 +14,42 @@
* limitations under the License.
*/
-import { Disposable, DisposableCollection, ICommandService, Inject, IUniverInstanceService, LifecycleStages, OnLifecycle, Rectangle, Tools, UniverInstanceType } from '@univerjs/core';
-
import type { ICommandInfo, IMutationInfo, IRange, Workbook } from '@univerjs/core';
-import { RangeProtectionRuleModel } from '../../../model/range-protection-rule.model';
-import { RangeProtectionRenderModel } from '../../../model/range-protection-render.model';
-import type { ISetRangeProtectionMutationParams } from '../../../commands/mutations/set-range-protection.mutation';
-import { SetRangeProtectionMutation } from '../../../commands/mutations/set-range-protection.mutation';
-import { InsertColMutation, InsertRowMutation } from '../../../commands/mutations/insert-row-col.mutation';
-import { RemoveColMutation, RemoveRowMutation } from '../../../commands/mutations/remove-row-col.mutation';
-import { type IMoveRowsMutationParams, MoveColsMutation, MoveRowsMutation } from '../../../commands/mutations/move-rows-cols.mutation';
+
import type {
IInsertColMutationParams,
} from '../../../basics/interfaces/mutation-interface';
import type { IInsertColCommandParams, IInsertRowCommandParams } from '../../../commands/commands/insert-row-col.command';
-import { InsertColCommand, InsertRowCommand } from '../../../commands/commands/insert-row-col.command';
-import type { IRemoveRowColCommandParams } from '../../../commands/commands/remove-row-col.command';
-import type { EffectRefRangeParams } from '../../../services/ref-range/type';
import type {
IMoveColsCommandParams,
IMoveRowsCommandParams,
} from '../../../commands/commands/move-rows-cols.command';
+import type { IRemoveRowColCommandParams } from '../../../commands/commands/remove-row-col.command';
+import type { IAddRangeProtectionMutationParams } from '../../../commands/mutations/add-range-protection.mutation';
+import type { IDeleteSelectionProtectionMutationParams } from '../../../commands/mutations/delete-range-protection.mutation';
+import type { ISetRangeProtectionMutationParams } from '../../../commands/mutations/set-range-protection.mutation';
+import type { EffectRefRangeParams } from '../../../services/ref-range/type';
+import { Disposable, DisposableCollection, ICommandService, Inject, IUniverInstanceService, LifecycleStages, OnLifecycle, Rectangle, Tools, UniverInstanceType } from '@univerjs/core';
+import { InsertColCommand, InsertRowCommand } from '../../../commands/commands/insert-row-col.command';
import {
MoveColsCommand,
MoveRowsCommand,
} from '../../../commands/commands/move-rows-cols.command';
+import { RemoveColCommand, RemoveRowCommand } from '../../../commands/commands/remove-row-col.command';
import {
type ISetWorksheetActivateCommandParams,
SetWorksheetActivateCommand,
} from '../../../commands/commands/set-worksheet-activate.command';
-import { RemoveColCommand, RemoveRowCommand } from '../../../commands/commands/remove-row-col.command';
-import { RefRangeService } from '../../../services/ref-range/ref-range.service';
-import type { IDeleteSelectionProtectionMutationParams } from '../../../commands/mutations/delete-range-protection.mutation';
-import { DeleteRangeProtectionMutation } from '../../../commands/mutations/delete-range-protection.mutation';
-import type { IAddRangeProtectionMutationParams } from '../../../commands/mutations/add-range-protection.mutation';
import { AddRangeProtectionMutation } from '../../../commands/mutations/add-range-protection.mutation';
+import { DeleteRangeProtectionMutation } from '../../../commands/mutations/delete-range-protection.mutation';
+import { InsertColMutation, InsertRowMutation } from '../../../commands/mutations/insert-row-col.mutation';
+import { type IMoveRowsMutationParams, MoveColsMutation, MoveRowsMutation } from '../../../commands/mutations/move-rows-cols.mutation';
+import { RemoveColMutation, RemoveRowMutation } from '../../../commands/mutations/remove-row-col.mutation';
+import { SetRangeProtectionMutation } from '../../../commands/mutations/set-range-protection.mutation';
+import { RangeProtectionCache } from '../../../model/range-protection.cache';
+import { RangeProtectionRenderModel } from '../../../model/range-protection-render.model';
+import { RangeProtectionRuleModel } from '../../../model/range-protection-rule.model';
+import { RefRangeService } from '../../../services/ref-range/ref-range.service';
const mutationIdByRowCol = [InsertColMutation.id, InsertRowMutation.id, RemoveColMutation.id, RemoveRowMutation.id];
const mutationIdArrByMove = [MoveRowsMutation.id, MoveColsMutation.id];
@@ -64,12 +65,14 @@ export class RangeProtectionRefRangeService extends Disposable {
@Inject(IUniverInstanceService) private _univerInstanceService: IUniverInstanceService,
@ICommandService private readonly _commandService: ICommandService,
@Inject(RefRangeService) private readonly _refRangeService: RefRangeService,
- @Inject(RangeProtectionRenderModel) private readonly _selectionProtectionRenderModel: RangeProtectionRenderModel
+ @Inject(RangeProtectionRenderModel) private readonly _selectionProtectionRenderModel: RangeProtectionRenderModel,
+ @Inject(RangeProtectionCache) private readonly _rangeProtectionCache: RangeProtectionCache
) {
super();
this._onRefRangeChange();
this._correctPermissionRange();
+ this._initReBuildCache();
}
private _onRefRangeChange() {
@@ -538,5 +541,14 @@ export class RangeProtectionRefRangeService extends Disposable {
private _checkIsRightRange(range: IRange) {
return range.startRow <= range.endRow && range.startColumn <= range.endColumn;
}
+
+ private _initReBuildCache() {
+ this.disposeWithMe(this._commandService.onCommandExecuted((command: ICommandInfo) => {
+ if (mutationIdByRowCol.includes(command.id) || mutationIdArrByMove.includes(command.id)) {
+ const { unitId, subUnitId } = command.params as IMoveRowsMutationParams;
+ this._rangeProtectionCache.reBuildCache(unitId, subUnitId);
+ }
+ }));
+ }
}
diff --git a/packages/sheets/src/services/permission/range-permission/range-protection.service.ts b/packages/sheets/src/services/permission/range-permission/range-protection.service.ts
index 7082dbd6e7..bd226257bb 100644
--- a/packages/sheets/src/services/permission/range-permission/range-protection.service.ts
+++ b/packages/sheets/src/services/permission/range-permission/range-protection.service.ts
@@ -14,12 +14,13 @@
* limitations under the License.
*/
-import { Disposable, Inject, IPermissionService, IResourceManagerService } from '@univerjs/core';
+import type { IObjectModel } from '../../../model/range-protection-rule.model';
+import { Disposable, Inject, IPermissionService, IResourceManagerService } from '@univerjs/core';
import { UnitAction, UnitObject, UniverType } from '@univerjs/protocol';
-import type { IObjectModel } from '../../../model/range-protection-rule.model';
-import { RangeProtectionRuleModel } from '../../../model/range-protection-rule.model';
+import { RangeProtectionCache } from '../../../model/range-protection.cache';
+import { RangeProtectionRuleModel } from '../../../model/range-protection-rule.model';
import { getAllRangePermissionPoint } from './util';
const PLUGIN_NAME = 'SHEET_RANGE_PROTECTION_PLUGIN';
@@ -28,7 +29,8 @@ export class RangeProtectionService extends Disposable {
constructor(
@Inject(RangeProtectionRuleModel) private _selectionProtectionRuleModel: RangeProtectionRuleModel,
@Inject(IPermissionService) private _permissionService: IPermissionService,
- @Inject(IResourceManagerService) private _resourceManagerService: IResourceManagerService
+ @Inject(IResourceManagerService) private _resourceManagerService: IResourceManagerService,
+ @Inject(RangeProtectionCache) private _selectionProtectionCache: RangeProtectionCache
) {
super();
@@ -122,6 +124,7 @@ export class RangeProtectionService extends Disposable {
},
onUnLoad: (unitId: string) => {
this._selectionProtectionRuleModel.deleteUnitModel(unitId);
+ this._selectionProtectionCache.deleteUnit(unitId);
},
})
);
diff --git a/packages/sheets/src/sheets-plugin.ts b/packages/sheets/src/sheets-plugin.ts
index 3d4a687c9a..93f0277038 100644
--- a/packages/sheets/src/sheets-plugin.ts
+++ b/packages/sheets/src/sheets-plugin.ts
@@ -14,25 +14,27 @@
* limitations under the License.
*/
+import type { Dependency } from '@univerjs/core';
+import type { IUniverSheetsConfig } from './controllers/config.schema';
+
import { DependentOn, IConfigService, Inject, Injector, mergeOverrideWithDependencies, Plugin, UniverInstanceType } from '@univerjs/core';
import { UniverFormulaEnginePlugin } from '@univerjs/engine-formula';
-
-import type { Dependency } from '@univerjs/core';
import { BasicWorksheetController } from './controllers/basic-worksheet.controller';
import { CalculateResultApplyController } from './controllers/calculate-result-apply.controller';
import { ONLY_REGISTER_FORMULA_RELATED_MUTATIONS_KEY } from './controllers/config';
import { defaultPluginConfig, PLUGIN_CONFIG_KEY } from './controllers/config.schema';
import { DefinedNameDataController } from './controllers/defined-name-data.controller';
import { MergeCellController } from './controllers/merge-cell.controller';
-import { NumberCellDisplayController } from './controllers/number-cell.controller';
+import { NumberCellDisplayController } from './controllers/number-cell.controller';
+import { RangeProtectionCache } from './model/range-protection.cache';
import { RangeProtectionRenderModel } from './model/range-protection-render.model';
import { RangeProtectionRuleModel } from './model/range-protection-rule.model';
import { BorderStyleManagerService } from './services/border-style-manager.service';
import { ExclusiveRangeService, IExclusiveRangeService } from './services/exclusive-range/exclusive-range-service';
+
import { NumfmtService } from './services/numfmt/numfmt.service';
import { INumfmtService } from './services/numfmt/type';
-
import { RangeProtectionRefRangeService } from './services/permission/range-permission/range-protection.ref-range';
import { RangeProtectionService } from './services/permission/range-permission/range-protection.service';
import { WorkbookPermissionService } from './services/permission/workbook-permission/workbook-permission.service';
@@ -40,7 +42,6 @@ import { WorksheetPermissionService, WorksheetProtectionPointModel, WorksheetPro
import { RefRangeService } from './services/ref-range/ref-range.service';
import { SheetsSelectionsService } from './services/selections/selection-manager.service';
import { SheetInterceptorService } from './services/sheet-interceptor/sheet-interceptor.service';
-import type { IUniverSheetsConfig } from './controllers/config.schema';
const PLUGIN_NAME = 'SHEET_PLUGIN';
@@ -94,6 +95,7 @@ export class UniverSheetsPlugin extends Plugin {
// range protection
[RangeProtectionRenderModel],
[RangeProtectionRuleModel],
+ [RangeProtectionCache],
[RangeProtectionRefRangeService],
[RangeProtectionService],
[IExclusiveRangeService, {