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

fix(permission): fix permission render bugs #3300

Merged
merged 6 commits into from
Sep 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ import {
import type { DocumentViewModel, RenderComponentType } from '@univerjs/engine-render';
import { DeviceInputEventType, IRenderManagerService, ScrollBar } from '@univerjs/engine-render';
import type { IMoveRangeMutationParams, ISetRangeValuesMutationParams } from '@univerjs/sheets';
import { MoveRangeMutation, SetRangeValuesMutation } from '@univerjs/sheets';
import { MoveRangeMutation, RangeProtectionRuleModel, SetRangeValuesMutation, WorksheetProtectionRuleModel } from '@univerjs/sheets';
import { takeUntil } from 'rxjs';

import { SetEditorResizeOperation } from '@univerjs/ui';
Expand All @@ -66,7 +66,9 @@ export class FormulaEditorController extends RxDisposable {
@IContextService private readonly _contextService: IContextService,
@IFormulaEditorManagerService private readonly _formulaEditorManagerService: IFormulaEditorManagerService,
@IUndoRedoService private readonly _undoRedoService: IUndoRedoService,
@Inject(TextSelectionManagerService) private readonly _textSelectionManagerService: TextSelectionManagerService
@Inject(TextSelectionManagerService) private readonly _textSelectionManagerService: TextSelectionManagerService,
@Inject(RangeProtectionRuleModel) private readonly _rangeProtectionRuleModel: RangeProtectionRuleModel,
@Inject(WorksheetProtectionRuleModel) private readonly _worksheetProtectionRuleModel: WorksheetProtectionRuleModel
) {
super();

Expand Down Expand Up @@ -245,7 +247,13 @@ export class FormulaEditorController extends RxDisposable {
// Sync cell content to formula editor bar when sheet selection changed.
private _syncFormulaEditorContent() {
this._editorBridgeService.currentEditCellState$.pipe(takeUntil(this.dispose$)).subscribe((editCellState) => {
if (editCellState == null || this._editorBridgeService.isForceKeepVisible()) {
if (
editCellState == null
|| this._editorBridgeService.isForceKeepVisible()
// If permissions are not initialized, data synchronization will not be performed.
|| !this._rangeProtectionRuleModel.getRangeRuleInitState()
|| !this._worksheetProtectionRuleModel.getSheetRuleInitState()
) {
return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@
*/

import type { Workbook } from '@univerjs/core';
import { Disposable, IAuthzIoService, Inject, IPermissionService, IUndoRedoService, IUniverInstanceService, LifecycleStages, OnLifecycle, UniverInstanceType, UserManagerService } from '@univerjs/core';
import { Disposable, IAuthzIoService, ICommandService, Inject, IPermissionService, IUndoRedoService, IUniverInstanceService, LifecycleStages, OnLifecycle, UniverInstanceType, UserManagerService } from '@univerjs/core';

import type { IRangeProtectionRule, IWorksheetProtectionRenderCellData } from '@univerjs/sheets';
import { defaultWorkbookPermissionPoints, defaultWorksheetPermissionPoint, getAllRangePermissionPoint, getAllWorkbookPermissionPoint, getAllWorksheetPermissionPoint, getAllWorksheetPermissionPointByPointPanel, INTERCEPTOR_POINT, RangeProtectionRenderModel, RangeProtectionRuleModel, SheetInterceptorService, WorksheetEditPermission, WorksheetProtectionPointModel, WorksheetProtectionRuleModel, WorksheetViewPermission } from '@univerjs/sheets';
import type { IAddRangeProtectionMutationParams, IAddWorksheetProtectionParams, IRangeProtectionRule, ISetWorksheetPermissionPointsMutationParams, IWorksheetProtectionRenderCellData } from '@univerjs/sheets';
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 { UnitAction, UnitObject } from '@univerjs/protocol';
Expand All @@ -39,7 +39,8 @@ export class SheetPermissionInitController extends Disposable {
@Inject(WorksheetProtectionPointModel) private _worksheetProtectionPointRuleModel: WorksheetProtectionPointModel,
@Inject(SheetInterceptorService) private _sheetInterceptorService: SheetInterceptorService,
@Inject(RangeProtectionRenderModel) private _selectionProtectionRenderModel: RangeProtectionRenderModel,
@Inject(IUndoRedoService) private _undoRedoService: IUndoRedoService
@Inject(IUndoRedoService) private _undoRedoService: IUndoRedoService,
@Inject(ICommandService) private _commandService: ICommandService
) {
super();
this._initRangePermissionFromSnapshot();
Expand All @@ -51,10 +52,11 @@ export class SheetPermissionInitController extends Disposable {
this._initUserChange();
this._initViewModelByRangeInterceptor();
this._initViewModelBySheetInterceptor();
this._refreshPermissionByCollaCreate();
}

private _initRangePermissionFromSnapshot() {
const initRangePermissionFunc = (workbook: Workbook) => {
private async _initRangePermissionFromSnapshot() {
const initRangePermissionFunc = async (workbook: Workbook) => {
const allAllowedParams: {
objectID: string;
unitID: string;
Expand All @@ -78,6 +80,7 @@ export class SheetPermissionInitController extends Disposable {
});

if (!allAllowedParams.length) {
this._rangeProtectionRuleModel.changeRuleInitState(true);
return;
}

Expand All @@ -95,12 +98,12 @@ export class SheetPermissionInitController extends Disposable {
});
}
});
this._rangeProtectionRuleModel.changeRuleInitState(true);
});
};

this._univerInstanceService.getAllUnitsForType<Workbook>(UniverInstanceType.UNIVER_SHEET).forEach((workbook) => {
initRangePermissionFunc(workbook);
});
await Promise.all(this._univerInstanceService.getAllUnitsForType<Workbook>(UniverInstanceType.UNIVER_SHEET).map((workbook) => initRangePermissionFunc(workbook)));
this._rangeProtectionRuleModel.changeRuleInitState(true);
}

private _initRangePermissionChange() {
Expand Down Expand Up @@ -144,7 +147,7 @@ export class SheetPermissionInitController extends Disposable {
);
}

public initWorkbookPermissionChange(_unitId?: string) {
public async initWorkbookPermissionChange(_unitId?: string) {
const unitId = _unitId || this._univerInstanceService.getCurrentUnitForType<Workbook>(UniverInstanceType.UNIVER_SHEET)?.getUnitId();
if (!unitId) {
return;
Expand All @@ -166,10 +169,8 @@ export class SheetPermissionInitController extends Disposable {
});
}

private _initWorkbookPermissionFromSnapshot() {
this._univerInstanceService.getAllUnitsForType<Workbook>(UniverInstanceType.UNIVER_SHEET).forEach((workbook) => {
this.initWorkbookPermissionChange(workbook.getUnitId());
});
private async _initWorkbookPermissionFromSnapshot() {
await Promise.all(this._univerInstanceService.getAllUnitsForType<Workbook>(UniverInstanceType.UNIVER_SHEET).map((workbook) => this.initWorkbookPermissionChange(workbook.getUnitId())));
}

private _initWorksheetPermissionChange() {
Expand Down Expand Up @@ -224,8 +225,8 @@ export class SheetPermissionInitController extends Disposable {
);
}

private _initWorksheetPermissionFromSnapshot() {
const initSheetPermissionFunc = (workbook: Workbook) => {
private async _initWorksheetPermissionFromSnapshot() {
const initSheetPermissionFunc = async (workbook: Workbook) => {
const allAllowedParams: {
objectID: string;
unitID: string;
Expand Down Expand Up @@ -261,6 +262,7 @@ export class SheetPermissionInitController extends Disposable {
});

if (!allAllowedParams.length) {
this._worksheetProtectionRuleModel.changeRuleInitState(true);
return;
}

Expand All @@ -278,12 +280,12 @@ export class SheetPermissionInitController extends Disposable {
});
}
});
this._worksheetProtectionRuleModel.changeRuleInitState(true);
});
};

this._univerInstanceService.getAllUnitsForType<Workbook>(UniverInstanceType.UNIVER_SHEET).forEach((workbook) => {
initSheetPermissionFunc(workbook);
});
await Promise.all(this._univerInstanceService.getAllUnitsForType<Workbook>(UniverInstanceType.UNIVER_SHEET).map((workbook) => initSheetPermissionFunc(workbook)));
this._worksheetProtectionRuleModel.changeRuleInitState(true);
}

private _initUserChange() {
Expand Down Expand Up @@ -488,4 +490,21 @@ export class SheetPermissionInitController extends Disposable {
});
}
}

private _refreshPermissionByCollaCreate() {
this.disposeWithMe(
this._commandService.onCommandExecuted((command, options) => {
if (options?.fromCollab) {
if (
command.id === AddRangeProtectionMutation.id
|| command.id === AddWorksheetProtectionMutation.id
|| command.id === SetWorksheetPermissionPointsMutation.id
) {
const params = command.params as IAddRangeProtectionMutationParams | IAddWorksheetProtectionParams | ISetWorksheetPermissionPointsMutationParams;
this._undoRedoService.clearUndoRedo(params.unitId);
}
}
})
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ type ICheckPermissionCommandParams = IEditorBridgeServiceVisibleParam | IMoveRow

export const SHEET_PERMISSION_PASTE_PLUGIN = 'SHEET_PERMISSION_PASTE_PLUGIN';

@OnLifecycle(LifecycleStages.Steady, SheetPermissionInterceptorBaseController)
@OnLifecycle(LifecycleStages.Ready, SheetPermissionInterceptorBaseController)
export class SheetPermissionInterceptorBaseController extends Disposable {
disposableCollection = new DisposableCollection();

Expand Down Expand Up @@ -170,7 +170,6 @@ export class SheetPermissionInterceptorBaseController extends Disposable {
errorMsg = this._localeService.t('permission.dialog.setStyleErr');
break;
case SheetCopyCommand.id:
case SheetCutCommand.id:
permission = this.permissionCheckWithRanges({
workbookTypes: [WorkbookCopyPermission],
rangeTypes: [RangeProtectionPermissionViewPoint],
Expand All @@ -181,6 +180,17 @@ export class SheetPermissionInterceptorBaseController extends Disposable {
errorMsg = this._localeService.t('permission.dialog.workbookCopyErr');
}
break;
case SheetCutCommand.id:
permission = this.permissionCheckWithRanges({
workbookTypes: [WorkbookCopyPermission, WorkbookEditablePermission],
rangeTypes: [RangeProtectionPermissionViewPoint, RangeProtectionPermissionEditPoint],
worksheetTypes: [WorksheetCopyPermission, WorksheetEditPermission],
});
errorMsg = this._localeService.t('permission.dialog.copyErr');
if (!this._permissionService.getPermissionPoint(new WorkbookCopyPermission(this._univerInstanceService.getCurrentUnitForType<Workbook>(UniverInstanceType.UNIVER_SHEET)!.getUnitId()).id)?.value) {
errorMsg = this._localeService.t('permission.dialog.workbookCopyErr');
}
break;
case DeltaColumnWidthCommand.id:
case SetColWidthCommand.id:
permission = this.permissionCheckWithoutRange({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*/

import type { ICellDataForSheetInterceptor, IRange, Nullable, Workbook } from '@univerjs/core';
import { getSheetCommandTarget, RangeProtectionRuleModel, SheetsSelectionsService, WorkbookEditablePermission, WorksheetEditPermission, WorksheetSetColumnStylePermission, WorksheetSetRowStylePermission } from '@univerjs/sheets';
import { getSheetCommandTarget, RangeProtectionRuleModel, SheetsSelectionsService, WorkbookEditablePermission, WorksheetEditPermission, WorksheetSetCellStylePermission, WorksheetSetCellValuePermission, WorksheetSetColumnStylePermission, WorksheetSetRowStylePermission } from '@univerjs/sheets';
import { DisposableCollection, Inject, IPermissionService, IUniverInstanceService, LifecycleStages, OnLifecycle, Optional, RANGE_TYPE, Rectangle, RxDisposable, UniverInstanceType } from '@univerjs/core';
import type { IRenderContext, IRenderModule, Scene, SpreadsheetSkeleton } from '@univerjs/engine-render';

Expand Down Expand Up @@ -238,7 +238,12 @@ export class SheetPermissionInterceptorCanvasRenderController extends RxDisposab
}
const { worksheet, unitId, subUnitId } = target;

const worksheetEditPermission = this._permissionService.composePermission([new WorkbookEditablePermission(unitId).id, new WorksheetEditPermission(unitId, subUnitId).id]).every((permission) => permission.value);
const worksheetEditPermission = this._permissionService.composePermission([
new WorkbookEditablePermission(unitId).id,
new WorksheetEditPermission(unitId, subUnitId).id,
new WorksheetSetCellValuePermission(unitId, subUnitId).id,
new WorksheetSetCellStylePermission(unitId, subUnitId).id,
]).every((permission) => permission.value);
if (!worksheetEditPermission) {
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,21 @@
* limitations under the License.
*/

import { Disposable, Inject, IPermissionService } from '@univerjs/core';
import { Disposable, Inject, IPermissionService, IUniverInstanceService, UniverInstanceType } from '@univerjs/core';
import type { MenuConfig } from '@univerjs/ui';
import { ComponentManager } from '@univerjs/ui';
import { CheckMarkSingle, DeleteSingle, LockSingle, ProtectSingle, WriteSingle } from '@univerjs/icons';
import { RangeProtectionRuleModel } from '@univerjs/sheets';
import { RangeProtectionRuleModel, WorksheetProtectionRuleModel } from '@univerjs/sheets';
import type { IRenderContext, IRenderModule, Spreadsheet } from '@univerjs/engine-render';
import { IRenderManagerService } from '@univerjs/engine-render';
import { merge, throttleTime } from 'rxjs';
import { permissionCheckIconKey, permissionDeleteIconKey, permissionEditIconKey, permissionLockIconKey, permissionMenuIconKey, UNIVER_SHEET_PERMISSION_DIALOG, UNIVER_SHEET_PERMISSION_PANEL, UNIVER_SHEET_PERMISSION_PANEL_FOOTER, UNIVER_SHEET_PERMISSION_USER_DIALOG } from '../../consts/permission';
import { SheetPermissionDialog, SheetPermissionPanel, SheetPermissionPanelFooter, SheetPermissionUserDialog } from '../../views/permission';
import { UNIVER_SHEET_PERMISSION_ALERT_DIALOG } from '../../views/permission/error-msg-dialog/interface';
import { AlertDialog } from '../../views/permission/error-msg-dialog';
import { SheetSkeletonManagerService } from '../../services/sheet-skeleton-manager.service';
import { RANGE_PROTECTION_CAN_NOT_VIEW_RENDER_EXTENSION_KEY, RANGE_PROTECTION_CAN_VIEW_RENDER_EXTENSION_KEY, RangeProtectionCanNotViewRenderExtension, RangeProtectionCanViewRenderExtension } from '../../views/permission/extensions/range-protection.render';
import { worksheetProtectionKey, WorksheetProtectionRenderExtension } from '../../views/permission/extensions/worksheet-permission.render';

export interface IUniverSheetsPermissionMenuConfig {
menu: MenuConfig;
Expand Down Expand Up @@ -108,7 +110,54 @@ export class SheetPermissionRenderController extends Disposable implements IRend

this.disposeWithMe(merge(
this._permissionService.permissionPointUpdate$.pipe(throttleTime(300, undefined, { trailing: true })),
this._rangeProtectionRuleModel.rangeRuleInitStateChange$,
this._rangeProtectionRuleModel.ruleChange$
).pipe().subscribe(markDirtySkeleton));
}
}

export class WorksheetProtectionRenderController extends Disposable implements IRenderModule {
private _worksheetProtectionRenderExtension = new WorksheetProtectionRenderExtension();
constructor(
private readonly _context: IRenderContext,
@Inject(IRenderManagerService) private _renderManagerService: IRenderManagerService,
@Inject(IUniverInstanceService) private _univerInstanceService: IUniverInstanceService,
@Inject(SheetSkeletonManagerService) private _sheetSkeletonManagerService: SheetSkeletonManagerService,
@Inject(WorksheetProtectionRuleModel) private _worksheetProtectionRuleModel: WorksheetProtectionRuleModel

) {
super();
this._initRender();
this._initSkeleton();
}

private _initRender() {
const register = (renderId: string) => {
const render = renderId && this._renderManagerService.getRenderById(renderId);
const spreadsheetRender = render && render.mainComponent as Spreadsheet;
if (spreadsheetRender) {
if (!spreadsheetRender.getExtensionByKey(worksheetProtectionKey)) {
spreadsheetRender.register(this._worksheetProtectionRenderExtension);
}
}
};
this.disposeWithMe(this._renderManagerService.currentRender$.subscribe((renderId) => {
renderId && register(renderId);
}));
const workbook = this._univerInstanceService.getCurrentUnitForType(UniverInstanceType.UNIVER_SHEET)!;
if (workbook) {
register(workbook.getUnitId());
}
}

private _initSkeleton(): void {
const markDirtySkeleton = () => {
this._sheetSkeletonManagerService.reCalculate();
this._context.mainComponent?.makeDirty();
};

this.disposeWithMe(merge(
this._worksheetProtectionRuleModel.worksheetRuleInitStateChange$
).pipe().subscribe(markDirtySkeleton));
}
}
Loading
Loading