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

ci: disposing unit should not emit error #2529

Merged
merged 13 commits into from
Jun 15, 2024
Prev Previous commit
Next Next commit
fix: clear sheet permission when dispose unit
  • Loading branch information
ybzky committed Jun 15, 2024
commit f5ee3ad2605f53d5a1018c3da21e7c254ab8735e
93 changes: 52 additions & 41 deletions packages/sheets-ui/src/controllers/menu/menu-util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,17 +48,12 @@ export function deriveStateFromActiveSheet$<T>(univerInstanceService: IUniverIns

export function getCurrentRangeDisable$(accessor: IAccessor, permissionTypes: IPermissionTypes = {}) {
const univerInstanceService = accessor.get(IUniverInstanceService);
const selectionManagerService = accessor.get(SelectionManagerService);
const rangeProtectionRuleModel = accessor.get(RangeProtectionRuleModel);
const worksheetRuleModel = accessor.get(WorksheetProtectionRuleModel);
const workbook$ = univerInstanceService.getCurrentTypeOfUnit$<Workbook>(UniverInstanceType.UNIVER_SHEET);
const userManagerService = accessor.get(UserManagerService);
const contextService = accessor.get(IContextService);
const focusedOnDrawing$ = contextService.subscribeContextValue$(FOCUSING_COMMON_DRAWINGS).pipe(startWith(false));

return combineLatest([userManagerService.currentUser$, workbook$, selectionManagerService.selectionMoveEnd$, focusedOnDrawing$]).pipe(
switchMap(([_, workbook, selection, focusOnDrawings]) => {
if (focusOnDrawings || !workbook) {
return combineLatest([userManagerService.currentUser$, workbook$]).pipe(
switchMap(([_, workbook]) => {
if (!workbook) {
return of(true);
}

Expand All @@ -67,47 +62,63 @@ export function getCurrentRangeDisable$(accessor: IAccessor, permissionTypes: IP
if (!worksheet) {
return of(true);
}
const unitId = workbook.getUnitId();
const subUnitId = worksheet.getSheetId();

const permissionService = accessor.get(IPermissionService);
const selectionManagerService = accessor.get(SelectionManagerService);
const rangeProtectionRuleModel = accessor.get(RangeProtectionRuleModel);
const worksheetRuleModel = accessor.get(WorksheetProtectionRuleModel);

const { workbookTypes = [WorkbookEditablePermission], worksheetTypes, rangeTypes } = permissionTypes;
const contextService = accessor.get(IContextService);
const focusedOnDrawing$ = contextService.subscribeContextValue$(FOCUSING_COMMON_DRAWINGS).pipe(startWith(false));

const permissionIds: string[] = [];
return combineLatest([selectionManagerService.selectionMoveEnd$, focusedOnDrawing$]).pipe(
switchMap(([selection, focusOnDrawings]) => {
if (focusOnDrawings) {
return of(true);
}

workbookTypes?.forEach((F) => {
permissionIds.push(new F(unitId).id);
});
const unitId = workbook.getUnitId();
const subUnitId = worksheet.getSheetId();

worksheetTypes?.forEach((F) => {
permissionIds.push(new F(unitId, subUnitId).id);
});
const permissionService = accessor.get(IPermissionService);

const worksheetRule = worksheetRuleModel.getRule(unitId, subUnitId);
const { workbookTypes = [WorkbookEditablePermission], worksheetTypes, rangeTypes } = permissionTypes;

if (worksheetRule) {
return permissionService.composePermission$(permissionIds).pipe(map((list) => {
return list.some((item) => item.value === false);
}));
}
const permissionIds: string[] = [];

const selectionRanges = selection?.map((selection) => selection.range);
const rules = rangeProtectionRuleModel.getSubunitRuleList(unitId, subUnitId).filter((rule) => {
return selectionRanges?.some((range) => {
return rule.ranges.some((ruleRange) => Rectangle.intersects(range, ruleRange));
});
});

rangeTypes?.forEach((F) => {
rules.forEach((rule) => {
permissionIds.push(new F(unitId, subUnitId, rule.permissionId).id);
});
});

return permissionService.composePermission$(permissionIds).pipe(map((list) => {
return list.some((item) => item.value === false);
}));
workbookTypes?.forEach((F) => {
permissionIds.push(new F(unitId).id);
});

worksheetTypes?.forEach((F) => {
permissionIds.push(new F(unitId, subUnitId).id);
});

const worksheetRule = worksheetRuleModel.getRule(unitId, subUnitId);

if (worksheetRule) {
return permissionService.composePermission$(permissionIds).pipe(map((list) => {
return list.some((item) => item.value === false);
}));
}

const selectionRanges = selection?.map((selection) => selection.range);
const rules = rangeProtectionRuleModel.getSubunitRuleList(unitId, subUnitId).filter((rule) => {
return selectionRanges?.some((range) => {
return rule.ranges.some((ruleRange) => Rectangle.intersects(range, ruleRange));
});
});

rangeTypes?.forEach((F) => {
rules.forEach((rule) => {
permissionIds.push(new F(unitId, subUnitId, rule.permissionId).id);
});
});

return permissionService.composePermission$(permissionIds).pipe(map((list) => {
return list.some((item) => item.value === false);
}));
})
);
})
);
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import { Inject } from '@wendellhu/redi';
import type { Workbook } from '@univerjs/core';
import { Disposable, IPermissionService, IUniverInstanceService, LifecycleStages, OnLifecycle, UniverInstanceType } from '@univerjs/core';

import { getAllWorksheetPermissionPoint, getAllWorksheetPermissionPointByPointPanel } from '../worksheet-permission/utils';
import { getAllWorkbookPermissionPoint } from './util';

@OnLifecycle(LifecycleStages.Starting, WorkbookPermissionService)
Expand Down Expand Up @@ -50,6 +51,14 @@ export class WorkbookPermissionService extends Disposable {

this.disposeWithMe(this._univerInstanceService.getTypeOfUnitDisposed$<Workbook>(UniverInstanceType.UNIVER_SHEET).subscribe((workbook) => {
const unitId = workbook.getUnitId();
workbook.getSheets().forEach((worksheet) => {
const subUnitId = worksheet.getSheetId();

[...getAllWorksheetPermissionPoint(), ...getAllWorksheetPermissionPointByPointPanel()].forEach((F) => {
const instance = new F(unitId, subUnitId);
this._permissionService.deletePermissionPoint(instance.id);
});
});
getAllWorkbookPermissionPoint().forEach((F) => {
const instance = new F(unitId);
this._permissionService.deletePermissionPoint(instance.id);
Expand Down