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(permission): optimize formula bar permission & resize header permission (#2603)

ybzkyde05ff8a045c
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 4da1ae0588..86fa2a3243 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
@@ -127,10 +127,11 @@ export class SheetPermissionInitController extends Disposable {
} else {
const ruleList = this._rangeProtectionRuleModel.getSubunitRuleList(info.unitId, info.subUnitId);
if (ruleList.length === 0) {
+ // because this rule is attached to other protection, if other protection is deleted, this rule should be deleted.
this._worksheetProtectionPointRuleModel.deleteRule(info.unitId, info.subUnitId);
[...getAllWorksheetPermissionPointByPointPanel()].forEach((F) => {
const instance = new F(info.unitId, info.subUnitId);
- this._permissionService.addPermissionPoint(instance);
+ this._permissionService.updatePermissionPoint(instance.id, instance.value);
});
}
}
diff --git a/packages/sheets-ui/src/controllers/permission/sheet-permission-interceptor-base.controller.ts b/packages/sheets-ui/src/controllers/permission/sheet-permission-interceptor-base.controller.ts
index 26e98c80d7..5247b19ff9 100644
--- a/packages/sheets-ui/src/controllers/permission/sheet-permission-interceptor-base.controller.ts
+++ b/packages/sheets-ui/src/controllers/permission/sheet-permission-interceptor-base.controller.ts
@@ -45,6 +45,8 @@ export const SHEET_PERMISSION_PASTE_PLUGIN = 'SHEET_PERMISSION_PASTE_PLUGIN';
export class SheetPermissionInterceptorBaseController extends Disposable {
disposableCollection = new DisposableCollection();
+ private _showPermissionDialog = true;
+
constructor(
@ICommandService private readonly _commandService: ICommandService,
@IUniverInstanceService private readonly _univerInstanceService: IUniverInstanceService,
@@ -63,6 +65,10 @@ export class SheetPermissionInterceptorBaseController extends Disposable {
this._initialize();
}
+ setShowPermissionDialog(value: boolean) {
+ this._showPermissionDialog = value;
+ }
+
public haveNotPermissionHandle(errorMsg: string) {
const dialogProps = {
id: UNIVER_SHEET_PERMISSION_ALERT_DIALOG_ID,
@@ -76,7 +82,9 @@ export class SheetPermissionInterceptorBaseController extends Disposable {
onClose: () => this._dialogService.close(UNIVER_SHEET_PERMISSION_ALERT_DIALOG_ID),
className: 'sheet-permission-user-dialog',
};
- this._dialogService.open(dialogProps);
+ if (this._showPermissionDialog) {
+ this._dialogService.open(dialogProps);
+ }
throw new Error('have not permission');
}
diff --git a/packages/sheets-ui/src/controllers/permission/sheet-permission-interceptor-canvas-render.controller.ts b/packages/sheets-ui/src/controllers/permission/sheet-permission-interceptor-canvas-render.controller.ts
index bd315adad3..f0092bfb16 100644
--- a/packages/sheets-ui/src/controllers/permission/sheet-permission-interceptor-canvas-render.controller.ts
+++ b/packages/sheets-ui/src/controllers/permission/sheet-permission-interceptor-canvas-render.controller.ts
@@ -15,9 +15,9 @@
*/
import type { ICellDataForSheetInterceptor, IRange, Nullable, Workbook } from '@univerjs/core';
-import { DisposableCollection, IPermissionService, IUniverInstanceService, LifecycleStages, OnLifecycle, Rectangle, RxDisposable, UniverInstanceType } from '@univerjs/core';
import { getSheetCommandTarget, RangeProtectionRuleModel, SheetsSelectionsService, WorkbookEditablePermission, WorksheetEditPermission, WorksheetSetColumnStylePermission, WorksheetSetRowStylePermission } from '@univerjs/sheets';
import { Inject, Optional } from '@wendellhu/redi';
+import { DisposableCollection, IPermissionService, IUniverInstanceService, LifecycleStages, OnLifecycle, RANGE_TYPE, Rectangle, RxDisposable, UniverInstanceType } from '@univerjs/core';
import type { IRenderContext, IRenderModule, Scene, SpreadsheetSkeleton } from '@univerjs/engine-render';
import { UnitAction } from '@univerjs/protocol';
@@ -157,7 +157,60 @@ export class SheetPermissionInterceptorCanvasRenderController extends RxDisposab
return Rectangle.intersects(range, selectionRange);
});
- const haveNotPermission = protectionLapRange.some((range) => {
+ let haveNotPermission = protectionLapRange.some((range) => {
+ const { startRow, startColumn, endRow, endColumn } = range;
+ for (let row = startRow; row <= endRow; row++) {
+ for (let col = startColumn; col <= endColumn; col++) {
+ const permission = (worksheet.getCell(row, col) as (ICellDataForSheetInterceptor & { selectionProtection: ICellPermission[] }))?.selectionProtection?.[0];
+ if (permission?.[UnitAction.Edit] === false) {
+ return true;
+ }
+ }
+ }
+ return false;
+ });
+
+ if (haveNotPermission) {
+ return false;
+ }
+
+ const resizeType = rangeParams.row !== undefined ? RANGE_TYPE.ROW : RANGE_TYPE.COLUMN;
+ const selectionRowColRanges: IRange[] = [];
+ const selectionRanges = this._selectionManagerService.getCurrentSelections();
+ let isResizeTargetInSelectionRanges = false;
+ if (resizeType === RANGE_TYPE.ROW) {
+ selectionRanges?.forEach((selection) => {
+ if (selection?.range?.rangeType === RANGE_TYPE.ROW || selection?.range?.rangeType === RANGE_TYPE.ALL) {
+ selectionRowColRanges.push(selection.range);
+ if (selection.range.startRow <= rangeParams.row! && selection.range.endRow >= rangeParams.row!) {
+ isResizeTargetInSelectionRanges = true;
+ }
+ }
+ });
+ } else {
+ selectionRanges?.forEach((selection) => {
+ if (selection?.range?.rangeType === RANGE_TYPE.COLUMN || selection?.range?.rangeType === RANGE_TYPE.ALL) {
+ selectionRowColRanges.push(selection.range);
+ if (selection.range.startColumn <= rangeParams.col! && selection.range.endColumn >= rangeParams.col!) {
+ isResizeTargetInSelectionRanges = true;
+ }
+ }
+ });
+ }
+
+ if (!isResizeTargetInSelectionRanges) {
+ return true;
+ }
+
+ const protectionLapWithSelectionRanges = this._rangeProtectionRuleModel.getSubunitRuleList(unitId, subUnitId).reduce((p, c) => {
+ return [...p, ...c.ranges];
+ }, [] as IRange[]).filter((range) => {
+ return selectionRowColRanges.some((selectionRange) => {
+ return Rectangle.intersects(range, selectionRange);
+ });
+ });
+
+ haveNotPermission = protectionLapWithSelectionRanges.some((range) => {
const { startRow, startColumn, endRow, endColumn } = range;
for (let row = startRow; row <= endRow; row++) {
for (let col = startColumn; col <= endColumn; col++) {
diff --git a/packages/sheets-ui/src/views/formula-bar/FormulaBar.tsx b/packages/sheets-ui/src/views/formula-bar/FormulaBar.tsx
index b7ed71953b..a1d74b60d4 100644
--- a/packages/sheets-ui/src/views/formula-bar/FormulaBar.tsx
+++ b/packages/sheets-ui/src/views/formula-bar/FormulaBar.tsx
@@ -24,7 +24,7 @@ import clsx from 'clsx';
import React, { useEffect, useLayoutEffect, useState } from 'react';
import { RangeProtectionPermissionEditPoint, RangeProtectionRuleModel, SheetsSelectionsService, WorkbookEditablePermission, WorksheetEditPermission, WorksheetProtectionRuleModel, WorksheetSetCellValuePermission } from '@univerjs/sheets';
-import { merge } from 'rxjs';
+import { EMPTY, merge, switchMap } from 'rxjs';
import { IFormulaEditorManagerService } from '../../services/editor/formula-editor-manager.service';
import { IEditorBridgeService } from '../../services/editor-bridge.service';
import { DefinedName } from '../defined-name/DefinedName';
@@ -51,52 +51,53 @@ export function FormulaBar() {
const rangeProtectionRuleModel = useDependency(RangeProtectionRuleModel);
const permissionService = useDependency(IPermissionService);
+ function getPermissionIds(unitId: string, subUnitId: string): string[] {
+ return [
+ new WorkbookEditablePermission(unitId).id,
+ new WorksheetSetCellValuePermission(unitId, subUnitId).id,
+ new WorksheetEditPermission(unitId, subUnitId).id,
+ ];
+ }
+
useLayoutEffect(() => {
const workbook = univerInstanceService.getCurrentUnitForType<Workbook>(UniverInstanceType.UNIVER_SHEET)!;
- merge(
+ const subscription = merge(
worksheetProtectionRuleModel.ruleChange$,
rangeProtectionRuleModel.ruleChange$,
- selectionManager.selectionMoveEnd$
- ).subscribe(() => {
- const unitId = workbook.getUnitId();
- const worksheet = workbook.getActiveSheet();
- if (!worksheet) return;
- const subUnitId = worksheet.getSheetId();
- const range = selectionManager.getCurrentLastSelection()?.range;
- if (!range) return;
- const workbookEditPermission = permissionService.getPermissionPoint(new WorkbookEditablePermission(unitId).id)?.value;
- const worksheetSetCellValuePermission = permissionService.getPermissionPoint(new WorksheetSetCellValuePermission(unitId, subUnitId).id)?.value;
- const worksheetEditPermission = permissionService.getPermissionPoint(new WorksheetEditPermission(unitId, subUnitId).id)?.value;
-
- if (!workbookEditPermission || !worksheetSetCellValuePermission || !worksheetEditPermission) {
- setDisable(true);
- return;
- }
-
- const selectionRanges = selectionManager.getCurrentSelections()?.map((selection) => selection.range);
- const permissionList = rangeProtectionRuleModel.getSubunitRuleList(unitId, subUnitId).filter((rule) => {
- return rule.ranges.some((r) => {
- return selectionRanges?.some((selectionRange) => {
- return Rectangle.intersects(r, selectionRange);
- });
+ selectionManager.selectionMoveEnd$,
+ workbook.activeSheet$
+ ).pipe(
+ switchMap(() => {
+ const unitId = workbook.getUnitId();
+ const worksheet = workbook.getActiveSheet();
+ if (!worksheet) return EMPTY;
+
+ const subUnitId = worksheet.getSheetId();
+ const range = selectionManager.getCurrentLastSelection()?.range;
+ if (!range) return EMPTY;
+
+ const permissionIds = getPermissionIds(unitId, subUnitId);
+
+ const selectionRanges = selectionManager.getCurrentSelections()?.map((selection) => selection.range);
+ const permissionList = rangeProtectionRuleModel.getSubunitRuleList(unitId, subUnitId).filter((rule) => {
+ return rule.ranges.some((r) => selectionRanges?.some((selectionRange) => Rectangle.intersects(r, selectionRange)));
});
- });
- const permissionEditIds: string[] = [];
- permissionList.forEach((p) => {
- permissionEditIds.push(new RangeProtectionPermissionEditPoint(unitId, subUnitId, p.permissionId).id);
- });
-
- const rangeEditPermission = permissionService.composePermission(permissionEditIds).every((p) => p.value);
+ permissionList.forEach((p) => {
+ permissionIds.push(new RangeProtectionPermissionEditPoint(unitId, subUnitId, p.permissionId).id);
+ });
- if (rangeEditPermission) {
- setDisable(false);
- } else {
- setDisable(true);
+ return permissionService.composePermission$(permissionIds);
+ })
+ ).subscribe((permissions) => {
+ if (permissions) {
+ setDisable(!permissions.every((p) => p.value));
}
- }
- );
- // eslint-disable-next-line react-hooks/exhaustive-deps
+ });
+
+ return () => {
+ subscription.unsubscribe();
+ };
}, []);
const INITIAL_SNAPSHOT = {