From 4ca2ed50be184a36c7f09819d6128ba1d210498f Mon Sep 17 00:00:00 2001 From: ybzky Date: Mon, 6 Jan 2025 16:45:04 +0800 Subject: [PATCH] chore: move permission controller to sheets pacakge (#4295) --- .../clipboard/html-to-udm/converter.ts | 2 +- .../controllers/cf.permission.controller.ts | 9 +- .../controllers/dv-permission.controller.ts | 13 +- .../views/components/list-dropdown/index.tsx | 10 +- .../sheets-filter-permission.controller.ts | 19 +- .../hyper-link-permission.controller.ts | 9 +- .../src/controllers/popup.controller.ts | 9 +- .../sheets-thread-comment-hover.controller.ts | 8 +- .../sheets-thread-comment-popup.controller.ts | 8 +- .../sheet-permission-check-ui.controller.ts | 253 ++++++++++++++++++ ...on-interceptor-canvas-render.controller.ts | 2 - ...ission-interceptor-clipboard.controller.ts | 7 +- ...n-interceptor-formula-render.controller.ts | 2 - packages/sheets-ui/src/index.ts | 24 +- packages/sheets-ui/src/mobile-plugin.ts | 38 ++- packages/sheets-ui/src/plugin.ts | 12 +- .../sheet-permission-check.controller.ts} | 227 +++------------- .../sheet-permission-init.controller.ts | 15 +- .../sheet-permission-view-model.controller.ts | 8 +- packages/sheets/src/index.ts | 2 + packages/sheets/src/sheets-plugin.ts | 10 + 21 files changed, 400 insertions(+), 287 deletions(-) create mode 100644 packages/sheets-ui/src/controllers/permission/sheet-permission-check-ui.controller.ts rename packages/{sheets-ui/src/controllers/permission/sheet-permission-interceptor-base.controller.ts => sheets/src/controllers/permission/sheet-permission-check.controller.ts} (68%) rename packages/{sheets-ui => sheets}/src/controllers/permission/sheet-permission-init.controller.ts (93%) rename packages/{sheets-ui => sheets}/src/controllers/permission/sheet-permission-view-model.controller.ts (87%) diff --git a/packages/docs-ui/src/services/clipboard/html-to-udm/converter.ts b/packages/docs-ui/src/services/clipboard/html-to-udm/converter.ts index 19b65a5aac3..07b0073c548 100644 --- a/packages/docs-ui/src/services/clipboard/html-to-udm/converter.ts +++ b/packages/docs-ui/src/services/clipboard/html-to-udm/converter.ts @@ -38,7 +38,7 @@ function matchFilter(node: HTMLElement, filter: IStyleRule['filter']) { } // TODO: get from page width. -const DEFAULT_TABLE_WIDTH = 600; +const DEFAULT_TABLE_WIDTH = 660; interface ITableCache { table: ITable; diff --git a/packages/sheets-conditional-formatting-ui/src/controllers/cf.permission.controller.ts b/packages/sheets-conditional-formatting-ui/src/controllers/cf.permission.controller.ts index 9f75c5367b0..bf65c97b584 100644 --- a/packages/sheets-conditional-formatting-ui/src/controllers/cf.permission.controller.ts +++ b/packages/sheets-conditional-formatting-ui/src/controllers/cf.permission.controller.ts @@ -17,15 +17,14 @@ import type { ICommandInfo } from '@univerjs/core'; import type { IAddCfCommandParams } from '../commands/commands/add-cf.command'; import { Disposable, ICommandService, Inject, LocaleService } from '@univerjs/core'; -import { RangeProtectionPermissionEditPoint, WorkbookEditablePermission, WorksheetEditPermission, WorksheetSetCellStylePermission } from '@univerjs/sheets'; -import { SheetPermissionInterceptorBaseController } from '@univerjs/sheets-ui'; +import { RangeProtectionPermissionEditPoint, SheetPermissionCheckController, WorkbookEditablePermission, WorksheetEditPermission, WorksheetSetCellStylePermission } from '@univerjs/sheets'; import { AddCfCommand } from '../commands/commands/add-cf.command'; export class ConditionalFormattingPermissionController extends Disposable { constructor( @Inject(LocaleService) private _localeService: LocaleService, @ICommandService private readonly _commandService: ICommandService, - @Inject(SheetPermissionInterceptorBaseController) private readonly _sheetPermissionInterceptorBaseController: SheetPermissionInterceptorBaseController + @Inject(SheetPermissionCheckController) private readonly _sheetPermissionCheckController: SheetPermissionCheckController ) { super(); @@ -37,13 +36,13 @@ export class ConditionalFormattingPermissionController extends Disposable { this.disposeWithMe( this._commandService.beforeCommandExecuted((command: ICommandInfo) => { if (command.id === AddCfCommand.id) { - const permission = this._sheetPermissionInterceptorBaseController.permissionCheckWithRanges({ + const permission = this._sheetPermissionCheckController.permissionCheckWithRanges({ workbookTypes: [WorkbookEditablePermission], rangeTypes: [RangeProtectionPermissionEditPoint], worksheetTypes: [WorksheetEditPermission, WorksheetSetCellStylePermission], }, (command.params as IAddCfCommandParams).rule.ranges); if (!permission) { - this._sheetPermissionInterceptorBaseController.haveNotPermissionHandle(this._localeService.t('permission.dialog.setStyleErr')); + this._sheetPermissionCheckController.blockExecuteWithoutPermission(this._localeService.t('permission.dialog.setStyleErr')); } } }) diff --git a/packages/sheets-data-validation-ui/src/controllers/dv-permission.controller.ts b/packages/sheets-data-validation-ui/src/controllers/dv-permission.controller.ts index d53cb9fde61..2530e1a2aae 100644 --- a/packages/sheets-data-validation-ui/src/controllers/dv-permission.controller.ts +++ b/packages/sheets-data-validation-ui/src/controllers/dv-permission.controller.ts @@ -17,15 +17,14 @@ import type { ICommandInfo } from '@univerjs/core'; import type { IUpdateSheetDataValidationRangeCommandParams } from '@univerjs/sheets-data-validation'; import { Disposable, ICommandService, Inject, LocaleService } from '@univerjs/core'; -import { RangeProtectionPermissionEditPoint, WorkbookEditablePermission, WorksheetEditPermission, WorksheetSetCellStylePermission } from '@univerjs/sheets'; +import { RangeProtectionPermissionEditPoint, SheetPermissionCheckController, WorkbookEditablePermission, WorksheetEditPermission, WorksheetSetCellStylePermission } from '@univerjs/sheets'; import { AddSheetDataValidationCommand, UpdateSheetDataValidationRangeCommand } from '@univerjs/sheets-data-validation'; -import { SheetPermissionInterceptorBaseController } from '@univerjs/sheets-ui'; export class DataValidationPermissionController extends Disposable { constructor( @Inject(LocaleService) private _localeService: LocaleService, @ICommandService private readonly _commandService: ICommandService, - @Inject(SheetPermissionInterceptorBaseController) private readonly _sheetPermissionInterceptorBaseController: SheetPermissionInterceptorBaseController + @Inject(SheetPermissionCheckController) private readonly _sheetPermissionCheckController: SheetPermissionCheckController ) { super(); this._commandExecutedListener(); @@ -35,23 +34,23 @@ export class DataValidationPermissionController extends Disposable { this.disposeWithMe( this._commandService.beforeCommandExecuted((command: ICommandInfo) => { if (command.id === AddSheetDataValidationCommand.id) { - const permission = this._sheetPermissionInterceptorBaseController.permissionCheckWithRanges({ + const permission = this._sheetPermissionCheckController.permissionCheckWithRanges({ workbookTypes: [WorkbookEditablePermission], rangeTypes: [RangeProtectionPermissionEditPoint], worksheetTypes: [WorksheetEditPermission, WorksheetSetCellStylePermission], }); if (!permission) { - this._sheetPermissionInterceptorBaseController.haveNotPermissionHandle(this._localeService.t('permission.dialog.setStyleErr')); + this._sheetPermissionCheckController.blockExecuteWithoutPermission(this._localeService.t('permission.dialog.setStyleErr')); } } if (command.id === UpdateSheetDataValidationRangeCommand.id) { - const permission = this._sheetPermissionInterceptorBaseController.permissionCheckWithRanges({ + const permission = this._sheetPermissionCheckController.permissionCheckWithRanges({ workbookTypes: [WorkbookEditablePermission], rangeTypes: [RangeProtectionPermissionEditPoint], worksheetTypes: [WorksheetEditPermission, WorksheetSetCellStylePermission], }, (command.params as IUpdateSheetDataValidationRangeCommandParams).ranges); if (!permission) { - this._sheetPermissionInterceptorBaseController.haveNotPermissionHandle(this._localeService.t('permission.dialog.setStyleErr')); + this._sheetPermissionCheckController.blockExecuteWithoutPermission(this._localeService.t('permission.dialog.setStyleErr')); } } }) diff --git a/packages/sheets-data-validation-ui/src/views/components/list-dropdown/index.tsx b/packages/sheets-data-validation-ui/src/views/components/list-dropdown/index.tsx index 8c6ddfe2ec9..e56b1a6728f 100644 --- a/packages/sheets-data-validation-ui/src/views/components/list-dropdown/index.tsx +++ b/packages/sheets-data-validation-ui/src/views/components/list-dropdown/index.tsx @@ -27,9 +27,9 @@ import { Scrollbar } from '@univerjs/design'; import { RichTextEditingMutation } from '@univerjs/docs'; import { DeviceInputEventType } from '@univerjs/engine-render'; import { CheckMarkSingle } from '@univerjs/icons'; -import { RangeProtectionPermissionEditPoint, SetRangeValuesCommand, WorkbookEditablePermission, WorksheetEditPermission } from '@univerjs/sheets'; +import { RangeProtectionPermissionEditPoint, SetRangeValuesCommand, SheetPermissionCheckController, WorkbookEditablePermission, WorksheetEditPermission } from '@univerjs/sheets'; import { deserializeListOptions, getDataValidationCellValue, serializeListOptions, SheetDataValidationModel } from '@univerjs/sheets-data-validation'; -import { IEditorBridgeService, SetCellEditVisibleOperation, SheetPermissionInterceptorBaseController } from '@univerjs/sheets-ui'; +import { IEditorBridgeService, SetCellEditVisibleOperation } from '@univerjs/sheets-ui'; import { KeyCode, RectPopup, useObservable } from '@univerjs/ui'; import React, { useEffect, useMemo, useState } from 'react'; import { debounceTime } from 'rxjs'; @@ -58,8 +58,8 @@ const SelectList = (props: ISelectListProps) => { const { row, col, unitId, subUnitId } = location; const filteredOptions = options.filter((item) => lowerFilter ? item.label.toLowerCase().includes(lowerFilter) : true); const showEditOnDropdown = configService.getConfig(SHEETS_DATA_VALIDATION_UI_PLUGIN_CONFIG_KEY)?.showEditOnDropdown ?? true; - const sheetPermissionInterceptorBaseController = useDependency(SheetPermissionInterceptorBaseController); - const hasPermission = useMemo(() => sheetPermissionInterceptorBaseController.permissionCheckWithRanges( + const sheetPermissionCheckController = useDependency(SheetPermissionCheckController); + const hasPermission = useMemo(() => sheetPermissionCheckController.permissionCheckWithRanges( { workbookTypes: [WorkbookEditablePermission], rangeTypes: [RangeProtectionPermissionEditPoint], @@ -68,7 +68,7 @@ const SelectList = (props: ISelectListProps) => { [{ startColumn: col, startRow: row, endColumn: col, endRow: row }], unitId, subUnitId - ), [sheetPermissionInterceptorBaseController, col, row, unitId, subUnitId]); + ), [sheetPermissionCheckController, col, row, unitId, subUnitId]); return (
diff --git a/packages/sheets-filter-ui/src/controllers/sheets-filter-permission.controller.ts b/packages/sheets-filter-ui/src/controllers/sheets-filter-permission.controller.ts index ef6771575fa..0299763bb1a 100644 --- a/packages/sheets-filter-ui/src/controllers/sheets-filter-permission.controller.ts +++ b/packages/sheets-filter-ui/src/controllers/sheets-filter-permission.controller.ts @@ -19,9 +19,8 @@ import type { MenuConfig } from '@univerjs/ui'; import { Disposable, ICommandService, Inject, Injector, IUniverInstanceService, LocaleService, Tools } from '@univerjs/core'; -import { expandToContinuousRange, getSheetCommandTarget, RangeProtectionPermissionViewPoint, SheetsSelectionsService, WorksheetFilterPermission, WorksheetViewPermission } from '@univerjs/sheets'; +import { expandToContinuousRange, getSheetCommandTarget, RangeProtectionPermissionViewPoint, SheetPermissionCheckController, SheetsSelectionsService, WorksheetFilterPermission, WorksheetViewPermission } from '@univerjs/sheets'; import { SheetsFilterService, SmartToggleSheetsFilterCommand } from '@univerjs/sheets-filter'; -import { SheetPermissionInterceptorBaseController } from '@univerjs/sheets-ui'; import { type IOpenFilterPanelOperationParams, OpenFilterPanelOperation } from '../commands/operations/sheets-filter.operation'; export interface IUniverSheetsFilterUIConfig { @@ -40,8 +39,8 @@ export class SheetsFilterPermissionController extends Disposable { @Inject(SheetsFilterService) private _sheetsFilterService: SheetsFilterService, @Inject(LocaleService) private _localeService: LocaleService, @ICommandService private readonly _commandService: ICommandService, - @Inject(SheetPermissionInterceptorBaseController) - private readonly _sheetPermissionInterceptorBaseController: SheetPermissionInterceptorBaseController, + @Inject(SheetPermissionCheckController) + private readonly _sheetPermissionCheckPermission: SheetPermissionCheckController, @Inject(Injector) private _injector: Injector, @Inject(SheetsSelectionsService) private _sheetsSelectionService: SheetsSelectionsService ) { @@ -61,7 +60,7 @@ export class SheetsFilterPermissionController extends Disposable { const filterRange = this._sheetsFilterService.getFilterModel(unitId, subUnitId)?.getRange(); let permission; if (filterRange) { - permission = this._sheetPermissionInterceptorBaseController.permissionCheckWithRanges({ + permission = this._sheetPermissionCheckPermission.permissionCheckWithRanges({ rangeTypes: [RangeProtectionPermissionViewPoint], worksheetTypes: [WorksheetFilterPermission, WorksheetViewPermission], }, [filterRange]); @@ -71,12 +70,12 @@ export class SheetsFilterPermissionController extends Disposable { let newRange = { ...range }; const isCellRange = range.startColumn === range.endColumn && range.startRow === range.endRow; newRange = isCellRange ? expandToContinuousRange(newRange, { left: true, right: true, up: true, down: true }, worksheet) : newRange; - permission = this._sheetPermissionInterceptorBaseController.permissionCheckWithRanges({ + permission = this._sheetPermissionCheckPermission.permissionCheckWithRanges({ rangeTypes: [RangeProtectionPermissionViewPoint], worksheetTypes: [WorksheetViewPermission, WorksheetFilterPermission], }, [newRange], unitId, subUnitId); } else { - permission = this._sheetPermissionInterceptorBaseController.permissionCheckWithoutRange({ + permission = this._sheetPermissionCheckPermission.permissionCheckWithoutRange({ rangeTypes: [RangeProtectionPermissionViewPoint], worksheetTypes: [WorksheetViewPermission, WorksheetFilterPermission], }); @@ -84,7 +83,7 @@ export class SheetsFilterPermissionController extends Disposable { } if (!permission) { - this._sheetPermissionInterceptorBaseController.haveNotPermissionHandle(this._localeService.t('permission.dialog.filterErr')); + this._sheetPermissionCheckPermission.blockExecuteWithoutPermission(this._localeService.t('permission.dialog.filterErr')); } } if (command.id === OpenFilterPanelOperation.id) { @@ -95,12 +94,12 @@ export class SheetsFilterPermissionController extends Disposable { if (colRange) { colRange.startColumn = params.col; colRange.endColumn = params.col; - const permission = this._sheetPermissionInterceptorBaseController.permissionCheckWithRanges({ + const permission = this._sheetPermissionCheckPermission.permissionCheckWithRanges({ rangeTypes: [RangeProtectionPermissionViewPoint], worksheetTypes: [WorksheetFilterPermission, WorksheetViewPermission], }, [colRange]); if (!permission) { - this._sheetPermissionInterceptorBaseController.haveNotPermissionHandle(this._localeService.t('permission.dialog.filterErr')); + this._sheetPermissionCheckPermission.blockExecuteWithoutPermission(this._localeService.t('permission.dialog.filterErr')); } } } diff --git a/packages/sheets-hyper-link-ui/src/controllers/hyper-link-permission.controller.ts b/packages/sheets-hyper-link-ui/src/controllers/hyper-link-permission.controller.ts index bd4cc09b21d..8033abb1a90 100644 --- a/packages/sheets-hyper-link-ui/src/controllers/hyper-link-permission.controller.ts +++ b/packages/sheets-hyper-link-ui/src/controllers/hyper-link-permission.controller.ts @@ -16,15 +16,14 @@ import type { ICommandInfo } from '@univerjs/core'; import { Disposable, ICommandService, Inject, LocaleService } from '@univerjs/core'; -import { RangeProtectionPermissionEditPoint, WorkbookEditablePermission, WorksheetEditPermission, WorksheetInsertHyperlinkPermission, WorksheetSetCellValuePermission } from '@univerjs/sheets'; -import { SheetPermissionInterceptorBaseController } from '@univerjs/sheets-ui'; +import { RangeProtectionPermissionEditPoint, SheetPermissionCheckController, WorkbookEditablePermission, WorksheetEditPermission, WorksheetInsertHyperlinkPermission, WorksheetSetCellValuePermission } from '@univerjs/sheets'; import { InsertLinkShortcut } from './menu'; export class SheetsHyperLinkPermissionController extends Disposable { constructor( @Inject(LocaleService) private _localeService: LocaleService, @ICommandService private readonly _commandService: ICommandService, - @Inject(SheetPermissionInterceptorBaseController) private readonly _sheetPermissionInterceptorBaseController: SheetPermissionInterceptorBaseController + @Inject(SheetPermissionCheckController) private readonly _sheetPermissionCheckController: SheetPermissionCheckController ) { super(); @@ -35,13 +34,13 @@ export class SheetsHyperLinkPermissionController extends Disposable { this.disposeWithMe( this._commandService.beforeCommandExecuted((command: ICommandInfo) => { if (command.id === InsertLinkShortcut.id) { - const permission = this._sheetPermissionInterceptorBaseController.permissionCheckWithRanges({ + const permission = this._sheetPermissionCheckController.permissionCheckWithRanges({ workbookTypes: [WorkbookEditablePermission], rangeTypes: [RangeProtectionPermissionEditPoint], worksheetTypes: [WorksheetEditPermission, WorksheetSetCellValuePermission, WorksheetInsertHyperlinkPermission], }); if (!permission) { - this._sheetPermissionInterceptorBaseController.haveNotPermissionHandle(this._localeService.t('permission.dialog.hyperLinkErr')); + this._sheetPermissionCheckController.blockExecuteWithoutPermission(this._localeService.t('permission.dialog.hyperLinkErr')); } } }) diff --git a/packages/sheets-hyper-link-ui/src/controllers/popup.controller.ts b/packages/sheets-hyper-link-ui/src/controllers/popup.controller.ts index 54385867d95..e3b4aac22ff 100644 --- a/packages/sheets-hyper-link-ui/src/controllers/popup.controller.ts +++ b/packages/sheets-hyper-link-ui/src/controllers/popup.controller.ts @@ -27,6 +27,7 @@ import { ClearSelectionFormatCommand, RangeProtectionPermissionEditPoint, RangeProtectionPermissionViewPoint, + SheetPermissionCheckController, WorkbookCopyPermission, WorkbookEditablePermission, WorkbookViewPermission, @@ -35,7 +36,7 @@ import { WorksheetInsertHyperlinkPermission, WorksheetViewPermission, } from '@univerjs/sheets'; -import { HoverManagerService, HoverRenderController, IEditorBridgeService, SheetPermissionInterceptorBaseController, SheetSkeletonManagerService } from '@univerjs/sheets-ui'; +import { HoverManagerService, HoverRenderController, IEditorBridgeService, SheetSkeletonManagerService } from '@univerjs/sheets-ui'; import { IZenZoneService } from '@univerjs/ui'; import { debounceTime, map, Observable, switchMap } from 'rxjs'; import { SheetsHyperLinkPopupService } from '../services/popup.service'; @@ -47,7 +48,7 @@ export class SheetsHyperLinkPopupController extends Disposable { @Inject(SheetsHyperLinkPopupService) private readonly _sheetsHyperLinkPopupService: SheetsHyperLinkPopupService, @Inject(IRenderManagerService) private readonly _renderManagerService: IRenderManagerService, @Inject(IPermissionService) private readonly _permissionService: IPermissionService, - @Inject(SheetPermissionInterceptorBaseController) private readonly _sheetPermissionInterceptorBaseController: SheetPermissionInterceptorBaseController, + @Inject(SheetPermissionCheckController) private readonly _sheetPermissionCheckController: SheetPermissionCheckController, @ICommandService private readonly _commandService: ICommandService, @IEditorBridgeService private readonly _editorBridgeService: IEditorBridgeService, @Inject(DocSelectionManagerService) private readonly _textSelectionManagerService: DocSelectionManagerService, @@ -75,13 +76,13 @@ export class SheetsHyperLinkPopupController extends Disposable { }; } - const viewPermission = this._sheetPermissionInterceptorBaseController.permissionCheckWithRanges({ + const viewPermission = this._sheetPermissionCheckController.permissionCheckWithRanges({ workbookTypes: [WorkbookViewPermission], worksheetTypes: [WorksheetViewPermission], rangeTypes: [RangeProtectionPermissionViewPoint], }, [{ startRow: currentRow, startColumn: currentCol, endRow: currentRow, endColumn: currentCol }]); - const editPermission = this._sheetPermissionInterceptorBaseController.permissionCheckWithRanges({ + const editPermission = this._sheetPermissionCheckController.permissionCheckWithRanges({ workbookTypes: [WorkbookEditablePermission], worksheetTypes: [WorksheetEditPermission, WorksheetInsertHyperlinkPermission], rangeTypes: [RangeProtectionPermissionEditPoint], diff --git a/packages/sheets-thread-comment-ui/src/controllers/sheets-thread-comment-hover.controller.ts b/packages/sheets-thread-comment-ui/src/controllers/sheets-thread-comment-hover.controller.ts index daabcbc8e54..d819f102a03 100644 --- a/packages/sheets-thread-comment-ui/src/controllers/sheets-thread-comment-hover.controller.ts +++ b/packages/sheets-thread-comment-ui/src/controllers/sheets-thread-comment-hover.controller.ts @@ -15,9 +15,9 @@ */ import { Disposable, Inject } from '@univerjs/core'; -import { RangeProtectionPermissionViewPoint, WorkbookCommentPermission, WorksheetViewPermission } from '@univerjs/sheets'; +import { RangeProtectionPermissionViewPoint, SheetPermissionCheckController, WorkbookCommentPermission, WorksheetViewPermission } from '@univerjs/sheets'; import { SheetsThreadCommentModel } from '@univerjs/sheets-thread-comment'; -import { HoverManagerService, SheetPermissionInterceptorBaseController } from '@univerjs/sheets-ui'; +import { HoverManagerService } from '@univerjs/sheets-ui'; import { debounceTime } from 'rxjs'; import { SheetsThreadCommentPopupService } from '../services/sheets-thread-comment-popup.service'; @@ -26,7 +26,7 @@ export class SheetsThreadCommentHoverController extends Disposable { @Inject(HoverManagerService) private readonly _hoverManagerService: HoverManagerService, @Inject(SheetsThreadCommentPopupService) private readonly _sheetsThreadCommentPopupService: SheetsThreadCommentPopupService, @Inject(SheetsThreadCommentModel) private readonly _sheetsThreadCommentModel: SheetsThreadCommentModel, - @Inject(SheetPermissionInterceptorBaseController) private readonly _sheetPermissionInterceptorBaseController: SheetPermissionInterceptorBaseController + @Inject(SheetPermissionCheckController) private readonly _sheetPermissionCheckController: SheetPermissionCheckController ) { super(); this._initHoverEvent(); @@ -43,7 +43,7 @@ export class SheetsThreadCommentHoverController extends Disposable { const commentId = this._sheetsThreadCommentModel.getByLocation(unitId, subUnitId, row, col); if (commentId) { - const commentPermission = this._sheetPermissionInterceptorBaseController.permissionCheckWithRanges({ + const commentPermission = this._sheetPermissionCheckController.permissionCheckWithRanges({ workbookTypes: [WorkbookCommentPermission], worksheetTypes: [WorksheetViewPermission], rangeTypes: [RangeProtectionPermissionViewPoint], diff --git a/packages/sheets-thread-comment-ui/src/controllers/sheets-thread-comment-popup.controller.ts b/packages/sheets-thread-comment-ui/src/controllers/sheets-thread-comment-popup.controller.ts index 8ef34a29a5d..9beff43e9d7 100644 --- a/packages/sheets-thread-comment-ui/src/controllers/sheets-thread-comment-popup.controller.ts +++ b/packages/sheets-thread-comment-ui/src/controllers/sheets-thread-comment-popup.controller.ts @@ -20,9 +20,9 @@ import type { IDeleteCommentMutationParams } from '@univerjs/thread-comment'; import { Disposable, ICommandService, Inject, IUniverInstanceService, RANGE_TYPE, Rectangle, UniverInstanceType } from '@univerjs/core'; import { singleReferenceToGrid } from '@univerjs/engine-formula'; import { IRenderManagerService } from '@univerjs/engine-render'; -import { RangeProtectionPermissionViewPoint, SetWorksheetActiveOperation, SheetsSelectionsService, WorkbookCommentPermission, WorksheetViewPermission } from '@univerjs/sheets'; +import { RangeProtectionPermissionViewPoint, SetWorksheetActiveOperation, SheetPermissionCheckController, SheetsSelectionsService, WorkbookCommentPermission, WorksheetViewPermission } from '@univerjs/sheets'; import { SheetsThreadCommentModel } from '@univerjs/sheets-thread-comment'; -import { IEditorBridgeService, IMarkSelectionService, ScrollToRangeOperation, SheetPermissionInterceptorBaseController, SheetSkeletonManagerService } from '@univerjs/sheets-ui'; +import { IEditorBridgeService, IMarkSelectionService, ScrollToRangeOperation, SheetSkeletonManagerService } from '@univerjs/sheets-ui'; import { DeleteCommentMutation } from '@univerjs/thread-comment'; import { SetActiveCommentOperation, ThreadCommentPanelService } from '@univerjs/thread-comment-ui'; import { debounceTime } from 'rxjs'; @@ -45,7 +45,7 @@ export class SheetsThreadCommentPopupController extends Disposable { @Inject(SheetsThreadCommentModel) private readonly _sheetsThreadCommentModel: SheetsThreadCommentModel, @Inject(ThreadCommentPanelService) private readonly _threadCommentPanelService: ThreadCommentPanelService, @IUniverInstanceService private readonly _univerInstanceService: IUniverInstanceService, - @Inject(SheetPermissionInterceptorBaseController) private readonly _sheetPermissionInterceptorBaseController: SheetPermissionInterceptorBaseController, + @Inject(SheetPermissionCheckController) private readonly _sheetPermissionCheckController: SheetPermissionCheckController, @IMarkSelectionService private readonly _markSelectionService: IMarkSelectionService, @Inject(SheetsSelectionsService) private readonly _sheetSelectionService: SheetsSelectionsService, @IEditorBridgeService private readonly _editorBridgeService: IEditorBridgeService, @@ -171,7 +171,7 @@ export class SheetsThreadCommentPopupController extends Disposable { const location = singleReferenceToGrid(comment.ref); const { row, column: col } = location; - const commentPermission = this._sheetPermissionInterceptorBaseController.permissionCheckWithRanges({ + const commentPermission = this._sheetPermissionCheckController.permissionCheckWithRanges({ workbookTypes: [WorkbookCommentPermission], worksheetTypes: [WorksheetViewPermission], rangeTypes: [RangeProtectionPermissionViewPoint], diff --git a/packages/sheets-ui/src/controllers/permission/sheet-permission-check-ui.controller.ts b/packages/sheets-ui/src/controllers/permission/sheet-permission-check-ui.controller.ts new file mode 100644 index 00000000000..cce52ee80c2 --- /dev/null +++ b/packages/sheets-ui/src/controllers/permission/sheet-permission-check-ui.controller.ts @@ -0,0 +1,253 @@ +/** + * 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. + */ + +/* eslint-disable complexity */ +/* eslint-disable max-lines-per-function */ + +import type { ICellDataForSheetInterceptor, ICommandInfo, IRange, Workbook } from '@univerjs/core'; +import type { IMoveColsCommandParams, IMoveRangeCommandParams, IMoveRowsCommandParams, ISetRangeValuesCommandParams, ISetSpecificRowsVisibleCommandParams } from '@univerjs/sheets'; +import type { IAutoFillCommandParams } from '../../commands/commands/auto-fill.command'; +import type { ISheetPasteParams } from '../../commands/commands/clipboard.command'; +import type { IEditorBridgeServiceVisibleParam } from '../../services/editor-bridge.service'; +import { Disposable, DisposableCollection, FOCUSING_EDITOR_STANDALONE, ICommandService, IContextService, Inject, IPermissionService, IUniverInstanceService, LocaleService, Rectangle, UniverInstanceType } from '@univerjs/core'; +import { IMEInputCommand, InsertCommand } from '@univerjs/docs-ui'; +import { UnitAction } from '@univerjs/protocol'; +import { getSheetCommandTarget, RangeProtectionPermissionEditPoint, RangeProtectionPermissionViewPoint, RangeProtectionRuleModel, SetBackgroundColorCommand, SheetPermissionCheckController, WorkbookCopyPermission, WorkbookEditablePermission, WorksheetCopyPermission, WorksheetEditPermission, WorksheetSetCellStylePermission, WorksheetSetCellValuePermission, WorksheetSetColumnStylePermission } from '@univerjs/sheets'; +import { IDialogService } from '@univerjs/ui'; +import { AutoFillCommand } from '../../commands/commands/auto-fill.command'; +import { SheetCopyCommand, SheetCutCommand, SheetPasteColWidthCommand, SheetPasteShortKeyCommand } from '../../commands/commands/clipboard.command'; +import { SetRangeBoldCommand, SetRangeItalicCommand, SetRangeStrickThroughCommand, SetRangeUnderlineCommand } from '../../commands/commands/inline-format.command'; +import { ApplyFormatPainterCommand } from '../../commands/commands/set-format-painter.command'; +import { SetCellEditVisibleOperation } from '../../commands/operations/cell-edit.operation'; +import { IAutoFillService } from '../../services/auto-fill/auto-fill.service'; +import { PREDEFINED_HOOK_NAME } from '../../services/clipboard/clipboard.service'; +import { UNIVER_SHEET_PERMISSION_ALERT_DIALOG, UNIVER_SHEET_PERMISSION_ALERT_DIALOG_ID } from '../../views/permission/error-msg-dialog/interface'; + +type ICellPermission = Record & { ruleId?: string; ranges?: IRange[] }; +type ICheckPermissionCommandParams = IEditorBridgeServiceVisibleParam | IMoveRowsCommandParams | IMoveColsCommandParams | IMoveRangeCommandParams | ISetRangeValuesCommandParams | ISheetPasteParams | ISetSpecificRowsVisibleCommandParams | IAutoFillCommandParams; + +export class SheetPermissionCheckUIController extends Disposable { + disposableCollection = new DisposableCollection(); + + constructor( + @ICommandService private readonly _commandService: ICommandService, + @IUniverInstanceService private readonly _univerInstanceService: IUniverInstanceService, + @IPermissionService private readonly _permissionService: IPermissionService, + @IDialogService private readonly _dialogService: IDialogService, + @Inject(RangeProtectionRuleModel) private _rangeProtectionRuleModel: RangeProtectionRuleModel, + @IAutoFillService private _autoFillService: IAutoFillService, + @Inject(LocaleService) private readonly _localeService: LocaleService, + @IContextService private readonly _contextService: IContextService, + + @Inject(SheetPermissionCheckController) private readonly _sheetPermissionCheckController: SheetPermissionCheckController + ) { + super(); + this._initialize(); + + this._initUIEvent(); + } + + private _initUIEvent() { + this.disposeWithMe( + this._sheetPermissionCheckController.triggerPermissionUIEvent$.subscribe((errorMsg) => { + this._haveNotPermissionHandle(errorMsg); + }) + ); + } + + private _haveNotPermissionHandle(errorMsg: string) { + const dialogProps = { + id: UNIVER_SHEET_PERMISSION_ALERT_DIALOG_ID, + title: { title: '' }, + children: { + label: UNIVER_SHEET_PERMISSION_ALERT_DIALOG, + errorMsg, + }, + width: 320, + destroyOnClose: true, + onClose: () => this._dialogService.close(UNIVER_SHEET_PERMISSION_ALERT_DIALOG_ID), + className: 'sheet-permission-user-dialog', + }; + if (this._permissionService.getShowComponents()) { + this._dialogService.open(dialogProps); + } + } + + private _getPermissionCheck(id: string, params: ICheckPermissionCommandParams) { + let permission = true; + let errorMsg = ''; + + switch (id) { + case InsertCommand.id: + case IMEInputCommand.id: + if (this._contextService.getContextValue(FOCUSING_EDITOR_STANDALONE) === true) { + break; + } + permission = this._sheetPermissionCheckController.permissionCheckWithoutRange({ + workbookTypes: [WorkbookEditablePermission], + rangeTypes: [RangeProtectionPermissionEditPoint], + worksheetTypes: [WorksheetSetCellValuePermission, WorksheetEditPermission], + }); + errorMsg = this._localeService.t('permission.dialog.editErr'); + break; + case SetCellEditVisibleOperation.id: + if ((params as IEditorBridgeServiceVisibleParam).visible === false) { + break; + } + permission = this._sheetPermissionCheckController.permissionCheckWithoutRange({ + workbookTypes: [WorkbookEditablePermission], + rangeTypes: [RangeProtectionPermissionEditPoint], + worksheetTypes: [WorksheetSetCellValuePermission, WorksheetEditPermission], + }); + errorMsg = this._localeService.t('permission.dialog.editErr'); + break; + case SheetPasteColWidthCommand.id: + permission = this._sheetPermissionCheckController.permissionCheckWithoutRange({ + workbookTypes: [WorkbookEditablePermission], + rangeTypes: [RangeProtectionPermissionEditPoint], + worksheetTypes: [WorksheetEditPermission, WorksheetSetColumnStylePermission], + }); + errorMsg = this._localeService.t('permission.dialog.pasteErr'); + break; + case SheetPasteShortKeyCommand.id: + permission = this._permissionCheckByPaste(params as ISheetPasteParams); + errorMsg = this._localeService.t('permission.dialog.pasteErr'); + break; + case ApplyFormatPainterCommand.id: + permission = this._sheetPermissionCheckController.permissionCheckWithRanges({ + workbookTypes: [WorkbookEditablePermission], + rangeTypes: [RangeProtectionPermissionEditPoint], + worksheetTypes: [WorksheetEditPermission, WorksheetSetCellValuePermission, WorksheetSetCellStylePermission], + }); + errorMsg = this._localeService.t('permission.dialog.commonErr'); + break; + case SetBackgroundColorCommand.id: + case SetRangeBoldCommand.id: + case SetRangeItalicCommand.id: + case SetRangeUnderlineCommand.id: + case SetRangeStrickThroughCommand.id: + permission = this._sheetPermissionCheckController.permissionCheckWithRanges({ + workbookTypes: [WorkbookEditablePermission], + rangeTypes: [RangeProtectionPermissionEditPoint], + worksheetTypes: [WorksheetSetCellStylePermission, WorksheetEditPermission], + }); + errorMsg = this._localeService.t('permission.dialog.setStyleErr'); + break; + case SheetCopyCommand.id: + permission = this._sheetPermissionCheckController.permissionCheckWithRanges({ + workbookTypes: [WorkbookCopyPermission], + rangeTypes: [RangeProtectionPermissionViewPoint], + worksheetTypes: [WorksheetCopyPermission], + }); + errorMsg = this._localeService.t('permission.dialog.copyErr'); + if (!this._permissionService.getPermissionPoint(new WorkbookCopyPermission(this._univerInstanceService.getCurrentUnitForType(UniverInstanceType.UNIVER_SHEET)!.getUnitId()).id)?.value) { + errorMsg = this._localeService.t('permission.dialog.workbookCopyErr'); + } + break; + case SheetCutCommand.id: + permission = this._sheetPermissionCheckController.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(UniverInstanceType.UNIVER_SHEET)!.getUnitId()).id)?.value) { + errorMsg = this._localeService.t('permission.dialog.workbookCopyErr'); + } + break; + + case AutoFillCommand.id: + permission = this._permissionCheckByAutoFillCommand(params as IAutoFillCommandParams); + errorMsg = this._localeService.t('permission.dialog.autoFillErr'); + break; + + default: + break; + } + + if (!permission) { + this._sheetPermissionCheckController.blockExecuteWithoutPermission(errorMsg); + } + }; + + private _initialize(): void { + this._commandExecutedListener(); + } + + private _commandExecutedListener() { + this.disposeWithMe( + this._commandService.beforeCommandExecuted((command: ICommandInfo) => { + this._getPermissionCheck(command.id, command?.params as ICheckPermissionCommandParams); + }) + ); + } + + private _permissionCheckByPaste(params: ISheetPasteParams) { + if (params.value === PREDEFINED_HOOK_NAME.SPECIAL_PASTE_VALUE || params.value === PREDEFINED_HOOK_NAME.SPECIAL_PASTE_FORMULA) { + return this._sheetPermissionCheckController.permissionCheckWithRanges({ + workbookTypes: [WorkbookEditablePermission], + rangeTypes: [RangeProtectionPermissionEditPoint], + worksheetTypes: [WorksheetSetCellStylePermission, WorksheetEditPermission], + }); + } else if (params.value === PREDEFINED_HOOK_NAME.SPECIAL_PASTE_FORMAT) { + return this._sheetPermissionCheckController.permissionCheckWithRanges({ + workbookTypes: [WorkbookEditablePermission], + rangeTypes: [RangeProtectionPermissionEditPoint], + worksheetTypes: [WorksheetSetCellStylePermission, WorksheetEditPermission], + }); + } else { + return this._sheetPermissionCheckController.permissionCheckWithRanges({ + workbookTypes: [WorkbookEditablePermission], + rangeTypes: [RangeProtectionPermissionEditPoint], + worksheetTypes: [WorksheetSetCellValuePermission, WorksheetSetCellStylePermission, WorksheetEditPermission], + }); + } + } + + private _permissionCheckByAutoFillCommand(params?: IAutoFillCommandParams) { + if (!params) { + return false; + } + + const { targetRange } = params; + + const target = getSheetCommandTarget(this._univerInstanceService); + if (!target) { + return false; + } + const { worksheet, unitId, subUnitId } = target; + + const permissionLapRanges = this._rangeProtectionRuleModel.getSubunitRuleList(unitId, subUnitId).reduce((p, c) => { + return [...p, ...c.ranges]; + }, [] as IRange[]).filter((range) => { + return Rectangle.intersects(range, targetRange); + }); + + const hasNotPermission = permissionLapRanges.some((range) => { + for (let row = range.startRow; row <= range.endRow; row++) { + for (let col = range.startColumn; col <= range.endColumn; col++) { + const permission = (worksheet.getCell(row, col) as (ICellDataForSheetInterceptor & { selectionProtection: ICellPermission[] }))?.selectionProtection?.[0]; + if (permission?.[UnitAction.Edit] === false) { + return true; + } + } + } + return false; + }); + + return !hasNotPermission; + } +} 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 969b651a4da..76e06c15589 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 @@ -28,8 +28,6 @@ import { getTransformCoord } from '../utils/component-tools'; type ICellPermission = Record & { ruleId?: string; ranges?: IRange[] }; -export const SHEET_PERMISSION_PASTE_PLUGIN = 'SHEET_PERMISSION_PASTE_PLUGIN'; - export class SheetPermissionInterceptorCanvasRenderController extends RxDisposable implements IRenderModule { disposableCollection = new DisposableCollection(); diff --git a/packages/sheets-ui/src/controllers/permission/sheet-permission-interceptor-clipboard.controller.ts b/packages/sheets-ui/src/controllers/permission/sheet-permission-interceptor-clipboard.controller.ts index e5fc5b4fc06..f0dca3b8cb6 100644 --- a/packages/sheets-ui/src/controllers/permission/sheet-permission-interceptor-clipboard.controller.ts +++ b/packages/sheets-ui/src/controllers/permission/sheet-permission-interceptor-clipboard.controller.ts @@ -17,10 +17,9 @@ import type { ICellDataForSheetInterceptor, IRange, Workbook } from '@univerjs/core'; import { Disposable, DisposableCollection, Inject, IUniverInstanceService, LocaleService, UniverInstanceType } from '@univerjs/core'; import { UnitAction } from '@univerjs/protocol'; -import { SheetsSelectionsService } from '@univerjs/sheets'; +import { SheetPermissionCheckController, SheetsSelectionsService } from '@univerjs/sheets'; import { ISheetClipboardService } from '../../services/clipboard/clipboard.service'; import { virtualizeDiscreteRanges } from '../utils/range-tools'; -import { SheetPermissionInterceptorBaseController } from './sheet-permission-interceptor-base.controller'; type ICellPermission = Record & { ruleId?: string; ranges?: IRange[] }; export const SHEET_PERMISSION_PASTE_PLUGIN = 'SHEET_PERMISSION_PASTE_PLUGIN'; @@ -33,7 +32,7 @@ export class SheetPermissionInterceptorClipboardController extends Disposable { @Inject(SheetsSelectionsService) private readonly _selectionManagerService: SheetsSelectionsService, @Inject(LocaleService) private readonly _localService: LocaleService, @Inject(ISheetClipboardService) private _sheetClipboardService: ISheetClipboardService, - @Inject(SheetPermissionInterceptorBaseController) private readonly _sheetPermissionInterceptorBaseController: SheetPermissionInterceptorBaseController + @Inject(SheetPermissionCheckController) private readonly _sheetPermissionCheckController: SheetPermissionCheckController ) { super(); this._initClipboardHook(); @@ -77,7 +76,7 @@ export class SheetPermissionInterceptorClipboardController extends Disposable { } if (!hasPermission) { - this._sheetPermissionInterceptorBaseController.haveNotPermissionHandle(this._localService.t('permission.dialog.pasteErr')); + this._sheetPermissionCheckController.blockExecuteWithoutPermission(this._localService.t('permission.dialog.pasteErr')); } return hasPermission; 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 b4ecacb8d2c..c46eb1b8d9c 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 @@ -22,8 +22,6 @@ import { UnitAction } from '@univerjs/protocol'; import { getSheetCommandTarget, RangeProtectionCache, WorksheetViewPermission } from '@univerjs/sheets'; import { StatusBarController } from '../status-bar.controller'; -export const SHEET_PERMISSION_PASTE_PLUGIN = 'SHEET_PERMISSION_PASTE_PLUGIN'; - export class SheetPermissionInterceptorFormulaRenderController extends RxDisposable implements IRenderModule { disposableCollection = new DisposableCollection(); diff --git a/packages/sheets-ui/src/index.ts b/packages/sheets-ui/src/index.ts index 0c9ce39c526..adefe61ef59 100644 --- a/packages/sheets-ui/src/index.ts +++ b/packages/sheets-ui/src/index.ts @@ -17,23 +17,14 @@ import './global.css'; export { getEditorObject } from './basics/editor/get-editor-object'; -export { EMBEDDING_FORMULA_EDITOR_COMPONENT_KEY, RANGE_SELECTOR_COMPONENT_KEY, SHEET_VIEW_KEY } from './common/keys'; export { useActiveWorkbook, useActiveWorksheet, useWorkbooks } from './components/hook'; export { SHEET_UI_PLUGIN_NAME } from './consts/plugin-name'; export { SheetsUIPart } from './consts/ui-name'; export { AutoFillController } from './controllers/auto-fill.controller'; -export { AFFECT_LAYOUT_STYLES, AutoHeightController } from './controllers/auto-height.controller'; -export { AutoWidthController } from './controllers/auto-width.controller'; export { CellCustomRenderController } from './controllers/cell-custom-render.controller'; -export { type IUniverSheetsUIConfig } from './controllers/config.schema'; -export { DragRenderController } from './controllers/drag-render.controller'; export { EditingRenderController } from './controllers/editor/editing.render-controller'; -export { FormulaEditorController } from './controllers/editor/formula-editor.controller'; -export { HoverRenderController } from './controllers/hover-render.controller'; export { PASTE_SPECIAL_MENU_ID, SheetMenuPosition } from './controllers/menu/menu'; export { deriveStateFromActiveSheet$, getCurrentExclusiveRangeInterest$, getCurrentRangeDisable$, getObservableWithExclusiveRange$ } from './controllers/menu/menu-util'; -export { SheetPermissionInitController } from './controllers/permission/sheet-permission-init.controller'; -export { SheetPermissionInterceptorBaseController } from './controllers/permission/sheet-permission-interceptor-base.controller'; export { HeaderFreezeRenderController } from './controllers/render-controllers/freeze.render-controller'; export { HeaderMoveRenderController } from './controllers/render-controllers/header-move.render-controller'; export { HeaderResizeRenderController } from './controllers/render-controllers/header-resize.render-controller'; @@ -41,15 +32,12 @@ export { SheetsScrollRenderController } from './controllers/render-controllers/s export { SheetRenderController } from './controllers/render-controllers/sheet.render-controller'; export { SheetUIController } from './controllers/sheet-ui.controller'; export { whenFormulaEditorActivated, whenSheetEditorFocused } from './controllers/shortcuts/utils'; -export { StatusBarController } from './controllers/status-bar.controller'; export { getCoordByCell, getCoordByOffset, getSheetObject, getTransformCoord, } from './controllers/utils/component-tools'; -export { discreteRangeToRange, rangeToDiscreteRange, virtualizeDiscreteRanges } from './controllers/utils/range-tools'; -export type { IDiscreteRange } from './controllers/utils/range-tools'; export { matchedSelectionByRowColIndex as checkInHeaderRanges } from './controllers/utils/selections-tools'; export { useHighlightRange } from './hooks/useHighlightRange'; export { UniverSheetsMobileUIPlugin } from './mobile-plugin'; @@ -81,6 +69,16 @@ export { IFormulaEditorManagerService } from './services/editor/formula-editor-m export { HoverManagerService } from './services/hover-manager.service'; export type { ICellPosWithEvent, IHoverCellPosition, IHoverRichTextInfo, IHoverRichTextPosition } from './services/hover-manager.service'; export { IMarkSelectionService, MarkSelectionService } from './services/mark-selection/mark-selection.service'; +export { HoverRenderController } from './controllers/hover-render.controller'; +export { DragRenderController } from './controllers/drag-render.controller'; +export { EMBEDDING_FORMULA_EDITOR_COMPONENT_KEY, RANGE_SELECTOR_COMPONENT_KEY, SHEET_VIEW_KEY } from './common/keys'; +export type { IDiscreteRange } from './controllers/utils/range-tools'; +export { discreteRangeToRange, rangeToDiscreteRange, virtualizeDiscreteRanges } from './controllers/utils/range-tools'; +export { AFFECT_LAYOUT_STYLES, AutoHeightController } from './controllers/auto-height.controller'; +export { AutoWidthController } from './controllers/auto-width.controller'; +export { FormulaEditorController } from './controllers/editor/formula-editor.controller'; +export { StatusBarController } from './controllers/status-bar.controller'; +export { SheetPermissionCheckUIController } from './controllers/permission/sheet-permission-check-ui.controller'; export { SheetPermissionUserManagerService } from './services/permission/sheet-permission-user-list.service'; export { SheetPrintInterceptorService } from './services/print-interceptor.service'; export { SheetScrollManagerService } from './services/scroll-manager.service'; @@ -98,6 +96,8 @@ export { MobileSheetBar } from './views/mobile/sheet-bar/MobileSheetBar'; export type { IRangeProtectionRenderCellData } from './views/permission/extensions/range-protection.render'; export { SheetBar } from './views/sheet-bar/SheetBar'; export { RenderSheetContent, RenderSheetFooter, RenderSheetHeader } from './views/sheet-container/SheetContainer'; +export { SheetPermissionInitController } from '@univerjs/sheets/controllers/permission/sheet-permission-init.controller.js'; +export { type IUniverSheetsUIConfig } from './controllers/config.schema'; export { getCellRealRange } from './common/utils'; export { FONT_FAMILY_COMPONENT, FONT_FAMILY_ITEM_COMPONENT } from './components/font-family/interface'; diff --git a/packages/sheets-ui/src/mobile-plugin.ts b/packages/sheets-ui/src/mobile-plugin.ts index 18ba371ca65..3a180b3268a 100644 --- a/packages/sheets-ui/src/mobile-plugin.ts +++ b/packages/sheets-ui/src/mobile-plugin.ts @@ -29,47 +29,46 @@ import { CellAlertRenderController } from './controllers/cell-alert.controller'; import { CellCustomRenderController } from './controllers/cell-custom-render.controller'; import { SheetClipboardController } from './controllers/clipboard/clipboard.controller'; import { defaultPluginConfig } from './controllers/config.schema'; +// import { SheetContextMenuRenderController } from './controllers/render-controllers/contextmenu.render-controller'; +import { DragRenderController } from './controllers/drag-render.controller'; import { ForceStringAlertRenderController } from './controllers/force-string-alert-render.controller'; import { ForceStringRenderController } from './controllers/force-string-render.controller'; import { HoverRenderController } from './controllers/hover-render.controller'; import { MarkSelectionRenderController } from './controllers/mark-selection.controller'; +import { SheetUIMobileController } from './controllers/mobile/mobile-sheet-ui.controller'; +import { SheetPermissionCheckUIController } from './controllers/permission/sheet-permission-check-ui.controller'; +import { SheetPermissionInterceptorCanvasRenderController } from './controllers/permission/sheet-permission-interceptor-canvas-render.controller'; +import { SheetPermissionInterceptorClipboardController } from './controllers/permission/sheet-permission-interceptor-clipboard.controller'; +import { SheetPermissionInterceptorFormulaRenderController } from './controllers/permission/sheet-permission-interceptor-formula-render.controller'; +import { SheetPermissionRenderController, SheetPermissionRenderManagerController, WorksheetProtectionRenderController } from './controllers/permission/sheet-permission-render.controller'; import { FormatPainterRenderController } from './controllers/render-controllers/format-painter.render-controller'; import { HeaderFreezeRenderController } from './controllers/render-controllers/freeze.render-controller'; import { HeaderMoveRenderController } from './controllers/render-controllers/header-move.render-controller'; +import { SheetContextMenuMobileRenderController } from './controllers/render-controllers/mobile/mobile-contextmenu.render-controller'; import { MobileSheetsScrollRenderController } from './controllers/render-controllers/mobile/mobile-scroll.render-controller'; +import { SheetRenderController } from './controllers/render-controllers/sheet.render-controller'; import { SheetsZoomRenderController } from './controllers/render-controllers/zoom.render-controller'; import { StatusBarController } from './controllers/status-bar.controller'; import { AutoFillService, IAutoFillService } from './services/auto-fill/auto-fill.service'; import { SheetCanvasPopManagerService } from './services/canvas-pop-manager.service'; import { CellAlertManagerService } from './services/cell-alert-manager.service'; import { ISheetClipboardService, SheetClipboardService } from './services/clipboard/clipboard.service'; +import { DragManagerService } from './services/drag-manager.service'; import { FormatPainterService, IFormatPainterService } from './services/format-painter/format-painter.service'; import { HoverManagerService } from './services/hover-manager.service'; import { IMarkSelectionService, MarkSelectionService } from './services/mark-selection/mark-selection.service'; -import { SheetScrollManagerService } from './services/scroll-manager.service'; -import { ISheetBarService, SheetBarService } from './services/sheet-bar/sheet-bar.service'; -import { SheetSkeletonManagerService } from './services/sheet-skeleton-manager.service'; -import { SheetsRenderService } from './services/sheets-render.service'; -import { ShortcutExperienceService } from './services/shortcut-experience.service'; -import { IStatusBarService, StatusBarService } from './services/status-bar.service'; -// import { SheetContextMenuRenderController } from './controllers/render-controllers/contextmenu.render-controller'; -import { DragRenderController } from './controllers/drag-render.controller'; -import { SheetUIMobileController } from './controllers/mobile/mobile-sheet-ui.controller'; -import { SheetPermissionInitController } from './controllers/permission/sheet-permission-init.controller'; -import { SheetPermissionInterceptorBaseController } from './controllers/permission/sheet-permission-interceptor-base.controller'; -import { SheetPermissionInterceptorCanvasRenderController } from './controllers/permission/sheet-permission-interceptor-canvas-render.controller'; -import { SheetPermissionInterceptorClipboardController } from './controllers/permission/sheet-permission-interceptor-clipboard.controller'; -import { SheetPermissionInterceptorFormulaRenderController } from './controllers/permission/sheet-permission-interceptor-formula-render.controller'; -import { SheetPermissionRenderController, SheetPermissionRenderManagerController, WorksheetProtectionRenderController } from './controllers/permission/sheet-permission-render.controller'; -import { SheetContextMenuMobileRenderController } from './controllers/render-controllers/mobile/mobile-contextmenu.render-controller'; -import { SheetRenderController } from './controllers/render-controllers/sheet.render-controller'; -import { DragManagerService } from './services/drag-manager.service'; import { SheetPermissionPanelModel } from './services/permission/sheet-permission-panel.model'; import { SheetPermissionUserManagerService } from './services/permission/sheet-permission-user-list.service'; import { SheetPrintInterceptorService } from './services/print-interceptor.service'; +import { SheetScrollManagerService } from './services/scroll-manager.service'; import { SelectAllService } from './services/select-all/select-all.service'; import { ISheetSelectionRenderService } from './services/selection/base-selection-render.service'; import { MobileSheetsSelectionRenderService } from './services/selection/mobile-selection-render.service'; +import { ISheetBarService, SheetBarService } from './services/sheet-bar/sheet-bar.service'; +import { SheetSkeletonManagerService } from './services/sheet-skeleton-manager.service'; +import { SheetsRenderService } from './services/sheets-render.service'; +import { ShortcutExperienceService } from './services/shortcut-experience.service'; +import { IStatusBarService, StatusBarService } from './services/status-bar.service'; /** * @ignore @@ -118,8 +117,7 @@ export class UniverSheetsMobileUIPlugin extends Plugin { [SheetPermissionUserManagerService], [WorksheetProtectionRenderController], [SheetPermissionInterceptorClipboardController], - [SheetPermissionInterceptorBaseController], - [SheetPermissionInitController], + [SheetPermissionCheckUIController], [SheetPermissionRenderManagerController], ] as Dependency[] ).forEach((d) => this._injector.add(d)); diff --git a/packages/sheets-ui/src/plugin.ts b/packages/sheets-ui/src/plugin.ts index fd0bf34b628..0b92202e1de 100644 --- a/packages/sheets-ui/src/plugin.ts +++ b/packages/sheets-ui/src/plugin.ts @@ -42,13 +42,11 @@ import { FormatPainterController } from './controllers/format-painter/format-pai import { HoverRenderController } from './controllers/hover-render.controller'; import { MarkSelectionRenderController } from './controllers/mark-selection.controller'; import { MoveRangeRenderController } from './controllers/move-range.controller'; -import { SheetPermissionInitController } from './controllers/permission/sheet-permission-init.controller'; -import { SheetPermissionInterceptorBaseController } from './controllers/permission/sheet-permission-interceptor-base.controller'; +import { SheetPermissionCheckUIController } from './controllers/permission/sheet-permission-check-ui.controller'; import { SheetPermissionInterceptorCanvasRenderController } from './controllers/permission/sheet-permission-interceptor-canvas-render.controller'; import { SheetPermissionInterceptorClipboardController } from './controllers/permission/sheet-permission-interceptor-clipboard.controller'; import { SheetPermissionInterceptorFormulaRenderController } from './controllers/permission/sheet-permission-interceptor-formula-render.controller'; import { SheetPermissionRenderController, SheetPermissionRenderManagerController, WorksheetProtectionRenderController } from './controllers/permission/sheet-permission-render.controller'; -import { SheetPermissionViewModelController } from './controllers/permission/sheet-permission-view-model.controller'; import { SheetContextMenuRenderController } from './controllers/render-controllers/contextmenu.render-controller'; import { EditorBridgeRenderController } from './controllers/render-controllers/editor-bridge.render-controller'; import { FormatPainterRenderController } from './controllers/render-controllers/format-painter.render-controller'; @@ -156,14 +154,11 @@ export class UniverSheetsUIPlugin extends Plugin { [SheetPermissionPanelModel], [SheetPermissionUserManagerService], [SheetPermissionInterceptorClipboardController], - [SheetPermissionInterceptorBaseController], - [SheetPermissionInitController], - [SheetPermissionViewModelController], + [SheetPermissionCheckUIController], [SheetPermissionRenderManagerController], ] as Dependency[], this._config.override)); touchDependencies(this._injector, [ - [SheetPermissionViewModelController], [SheetPermissionPanelModel], ]); } @@ -183,7 +178,7 @@ export class UniverSheetsUIPlugin extends Plugin { [SheetUIController], [SheetsRenderService], [ActiveWorksheetController], - [SheetPermissionInterceptorBaseController], + [SheetPermissionCheckUIController], ]); } @@ -191,7 +186,6 @@ export class UniverSheetsUIPlugin extends Plugin { this._registerRenderModules(); touchDependencies(this._injector, [ - [SheetPermissionInitController], [SheetPermissionRenderManagerController], [SheetClipboardController], [FormulaEditorController], diff --git a/packages/sheets-ui/src/controllers/permission/sheet-permission-interceptor-base.controller.ts b/packages/sheets/src/controllers/permission/sheet-permission-check.controller.ts similarity index 68% rename from packages/sheets-ui/src/controllers/permission/sheet-permission-interceptor-base.controller.ts rename to packages/sheets/src/controllers/permission/sheet-permission-check.controller.ts index 8206670f57e..1b98e796d2d 100644 --- a/packages/sheets-ui/src/controllers/permission/sheet-permission-interceptor-base.controller.ts +++ b/packages/sheets/src/controllers/permission/sheet-permission-check.controller.ts @@ -14,46 +14,55 @@ * limitations under the License. */ -/* eslint-disable complexity */ -/* eslint-disable max-lines-per-function */ - import type { ICellData, ICellDataForSheetInterceptor, ICommandInfo, IObjectMatrixPrimitiveType, IPermissionTypes, IRange, Nullable, Workbook, WorkbookPermissionPointConstructor, Worksheet } from '@univerjs/core'; -import type { IMoveColsCommandParams, IMoveRangeCommandParams, IMoveRowsCommandParams, ISetRangeValuesCommandParams, ISetSpecificColsVisibleCommandParams, ISetSpecificRowsVisibleCommandParams, ISetWorksheetNameMutationParams, ISetWorksheetShowCommandParams } from '@univerjs/sheets'; -import type { IAutoFillCommandParams } from '../../commands/commands/auto-fill.command'; -import type { ISheetPasteParams } from '../../commands/commands/clipboard.command'; -import type { IEditorBridgeServiceVisibleParam } from '../../services/editor-bridge.service'; -import { CustomCommandExecutionError, Disposable, DisposableCollection, FOCUSING_EDITOR_STANDALONE, ICommandService, IContextService, Inject, IPermissionService, isICellData, IUniverInstanceService, LocaleService, ObjectMatrix, Rectangle, Tools, UniverInstanceType } from '@univerjs/core'; -import { IMEInputCommand, InsertCommand } from '@univerjs/docs-ui'; +import type { ISetSpecificColsVisibleCommandParams } from '../../commands/commands/set-col-visible.command'; +import type { ISetWorksheetShowCommandParams } from '../../commands/commands/set-worksheet-show.command'; +import type { ISetWorksheetNameMutationParams } from '../../commands/mutations/set-worksheet-name.mutation'; +import { CustomCommandExecutionError, Disposable, DisposableCollection, ICommandService, IContextService, Inject, IPermissionService, isICellData, IUniverInstanceService, LocaleService, ObjectMatrix, Rectangle, Tools, UniverInstanceType } from '@univerjs/core'; import { deserializeRangeWithSheet, deserializeRangeWithSheetWithCache, IDefinedNamesService, LexerTreeBuilder, operatorToken, sequenceNodeType } from '@univerjs/engine-formula'; import { UnitAction } from '@univerjs/protocol'; -import { ClearSelectionContentCommand, DeleteRangeMoveLeftCommand, DeleteRangeMoveUpCommand, DeltaColumnWidthCommand, DeltaRowHeightCommand, getSheetCommandTarget, InsertRangeMoveDownCommand, InsertRangeMoveRightCommand, MoveColsCommand, MoveRangeCommand, MoveRowsCommand, RangeProtectionPermissionEditPoint, RangeProtectionPermissionViewPoint, RangeProtectionRuleModel, SetBackgroundColorCommand, SetColWidthCommand, SetRangeValuesCommand, SetRowHeightCommand, SetSelectedColsVisibleCommand, SetSelectedRowsVisibleCommand, SetSpecificColsVisibleCommand, SetSpecificRowsVisibleCommand, SetWorksheetNameCommand, SetWorksheetNameMutation, SetWorksheetOrderCommand, SetWorksheetRowIsAutoHeightCommand, SetWorksheetShowCommand, SheetsSelectionsService, WorkbookCopyPermission, WorkbookEditablePermission, WorkbookHideSheetPermission, WorkbookManageCollaboratorPermission, WorkbookMoveSheetPermission, WorkbookRenameSheetPermission, WorksheetCopyPermission, WorksheetEditPermission, WorksheetProtectionRuleModel, WorksheetSetCellStylePermission, WorksheetSetCellValuePermission, WorksheetSetColumnStylePermission, WorksheetSetRowStylePermission, WorksheetViewPermission } from '@univerjs/sheets'; -import { IDialogService } from '@univerjs/ui'; -import { AutoFillCommand } from '../../commands/commands/auto-fill.command'; -import { SheetCopyCommand, SheetCutCommand, SheetPasteColWidthCommand, SheetPasteShortKeyCommand } from '../../commands/commands/clipboard.command'; -import { SetRangeBoldCommand, SetRangeItalicCommand, SetRangeStrickThroughCommand, SetRangeUnderlineCommand } from '../../commands/commands/inline-format.command'; -import { ApplyFormatPainterCommand } from '../../commands/commands/set-format-painter.command'; -import { SetCellEditVisibleOperation } from '../../commands/operations/cell-edit.operation'; -import { IAutoFillService } from '../../services/auto-fill/auto-fill.service'; -import { PREDEFINED_HOOK_NAME } from '../../services/clipboard/clipboard.service'; -import { UNIVER_SHEET_PERMISSION_ALERT_DIALOG, UNIVER_SHEET_PERMISSION_ALERT_DIALOG_ID } from '../../views/permission/error-msg-dialog/interface'; +import { Subject } from 'rxjs'; +import { ClearSelectionContentCommand } from '../../commands/commands/clear-selection-content.command'; +import { DeleteRangeMoveLeftCommand } from '../../commands/commands/delete-range-move-left.command'; +import { DeleteRangeMoveUpCommand } from '../../commands/commands/delete-range-move-up.command'; +import { InsertRangeMoveDownCommand } from '../../commands/commands/insert-range-move-down.command'; +import { InsertRangeMoveRightCommand } from '../../commands/commands/insert-range-move-right.command'; +import { type IMoveRangeCommandParams, MoveRangeCommand } from '../../commands/commands/move-range.command'; +import { type IMoveColsCommandParams, type IMoveRowsCommandParams, MoveColsCommand, MoveRowsCommand } from '../../commands/commands/move-rows-cols.command'; +import { SetSelectedColsVisibleCommand, SetSpecificColsVisibleCommand } from '../../commands/commands/set-col-visible.command'; +import { type ISetRangeValuesCommandParams, SetRangeValuesCommand } from '../../commands/commands/set-range-values.command'; +import { type ISetSpecificRowsVisibleCommandParams, SetSelectedRowsVisibleCommand, SetSpecificRowsVisibleCommand } from '../../commands/commands/set-row-visible.command'; +import { DeltaColumnWidthCommand, SetColWidthCommand } from '../../commands/commands/set-worksheet-col-width.command'; +import { SetWorksheetNameCommand } from '../../commands/commands/set-worksheet-name.command'; +import { SetWorksheetOrderCommand } from '../../commands/commands/set-worksheet-order.command'; +import { DeltaRowHeightCommand, SetRowHeightCommand, SetWorksheetRowIsAutoHeightCommand } from '../../commands/commands/set-worksheet-row-height.command'; +import { SetWorksheetShowCommand } from '../../commands/commands/set-worksheet-show.command'; +import { getSheetCommandTarget } from '../../commands/commands/utils/target-util'; +import { SetWorksheetNameMutation } from '../../commands/mutations/set-worksheet-name.mutation'; +import { RangeProtectionRuleModel } from '../../model/range-protection-rule.model'; +import { RangeProtectionPermissionEditPoint, WorkbookEditablePermission, WorkbookHideSheetPermission, WorkbookManageCollaboratorPermission, WorkbookMoveSheetPermission, WorkbookRenameSheetPermission, WorksheetEditPermission, WorksheetSetCellValuePermission, WorksheetSetColumnStylePermission, WorksheetSetRowStylePermission, WorksheetViewPermission } from '../../services/permission/permission-point'; +import { WorksheetProtectionRuleModel } from '../../services/permission/worksheet-permission'; +import { SheetsSelectionsService } from '../../services/selections'; -type ICellPermission = Record & { ruleId?: string; ranges?: IRange[] }; -type ICheckPermissionCommandParams = IEditorBridgeServiceVisibleParam | IMoveRowsCommandParams | IMoveColsCommandParams | IMoveRangeCommandParams | ISetRangeValuesCommandParams | ISheetPasteParams | ISetSpecificRowsVisibleCommandParams | IAutoFillCommandParams; +/* eslint-disable complexity */ +/* eslint-disable max-lines-per-function */ -export const SHEET_PERMISSION_PASTE_PLUGIN = 'SHEET_PERMISSION_PASTE_PLUGIN'; +type ICellPermission = Record & { ruleId?: string; ranges?: IRange[] }; +type ICheckPermissionCommandParams = IMoveRowsCommandParams | IMoveColsCommandParams | IMoveRangeCommandParams | ISetRangeValuesCommandParams | ISetSpecificRowsVisibleCommandParams; -export class SheetPermissionInterceptorBaseController extends Disposable { +export class SheetPermissionCheckController extends Disposable { disposableCollection = new DisposableCollection(); + private _triggerPermissionUIEvent$ = new Subject(); + public triggerPermissionUIEvent$ = this._triggerPermissionUIEvent$.asObservable(); + constructor( @ICommandService private readonly _commandService: ICommandService, @IUniverInstanceService private readonly _univerInstanceService: IUniverInstanceService, @IPermissionService private readonly _permissionService: IPermissionService, @Inject(SheetsSelectionsService) private readonly _selectionManagerService: SheetsSelectionsService, - @IDialogService private readonly _dialogService: IDialogService, @Inject(RangeProtectionRuleModel) private _rangeProtectionRuleModel: RangeProtectionRuleModel, @Inject(WorksheetProtectionRuleModel) private _worksheetProtectionRuleModel: WorksheetProtectionRuleModel, - @IAutoFillService private _autoFillService: IAutoFillService, @Inject(LocaleService) private readonly _localeService: LocaleService, @Inject(LexerTreeBuilder) private readonly _lexerTreeBuilder: LexerTreeBuilder, @IContextService private readonly _contextService: IContextService, @@ -63,23 +72,9 @@ export class SheetPermissionInterceptorBaseController extends Disposable { this._initialize(); } - public haveNotPermissionHandle(errorMsg: string) { - const dialogProps = { - id: UNIVER_SHEET_PERMISSION_ALERT_DIALOG_ID, - title: { title: '' }, - children: { - label: UNIVER_SHEET_PERMISSION_ALERT_DIALOG, - errorMsg, - }, - width: 320, - destroyOnClose: true, - onClose: () => this._dialogService.close(UNIVER_SHEET_PERMISSION_ALERT_DIALOG_ID), - className: 'sheet-permission-user-dialog', - }; - if (this._permissionService.getShowComponents()) { - this._dialogService.open(dialogProps); - } - throw new CustomCommandExecutionError('have not permission'); + public blockExecuteWithoutPermission(errorMsg: string) { + this._triggerPermissionUIEvent$.next(errorMsg); + throw new CustomCommandExecutionError('have no permission'); } private _getPermissionCheck(id: string, params: ICheckPermissionCommandParams) { @@ -87,29 +82,6 @@ export class SheetPermissionInterceptorBaseController extends Disposable { let errorMsg = ''; switch (id) { - case InsertCommand.id: - case IMEInputCommand.id: - if (this._contextService.getContextValue(FOCUSING_EDITOR_STANDALONE) === true) { - break; - } - permission = this.permissionCheckWithoutRange({ - workbookTypes: [WorkbookEditablePermission], - rangeTypes: [RangeProtectionPermissionEditPoint], - worksheetTypes: [WorksheetSetCellValuePermission, WorksheetEditPermission], - }); - errorMsg = this._localeService.t('permission.dialog.editErr'); - break; - case SetCellEditVisibleOperation.id: - if ((params as IEditorBridgeServiceVisibleParam).visible === false) { - break; - } - permission = this.permissionCheckWithoutRange({ - workbookTypes: [WorkbookEditablePermission], - rangeTypes: [RangeProtectionPermissionEditPoint], - worksheetTypes: [WorksheetSetCellValuePermission, WorksheetEditPermission], - }); - errorMsg = this._localeService.t('permission.dialog.editErr'); - break; case SetRangeValuesCommand.id: if (isICellData((params as ISetRangeValuesCommandParams).value) && ((params as ISetRangeValuesCommandParams).value as ICellData).f) { permission = this._permissionCheckWithFormula((params as ISetRangeValuesCommandParams)); @@ -131,60 +103,6 @@ export class SheetPermissionInterceptorBaseController extends Disposable { }); errorMsg = this._localeService.t('permission.dialog.editErr'); break; - case SheetPasteColWidthCommand.id: - permission = this.permissionCheckWithoutRange({ - workbookTypes: [WorkbookEditablePermission], - rangeTypes: [RangeProtectionPermissionEditPoint], - worksheetTypes: [WorksheetEditPermission, WorksheetSetColumnStylePermission], - }); - errorMsg = this._localeService.t('permission.dialog.pasteErr'); - break; - case SheetPasteShortKeyCommand.id: - permission = this._permissionCheckByPaste(params as ISheetPasteParams); - errorMsg = this._localeService.t('permission.dialog.pasteErr'); - break; - case ApplyFormatPainterCommand.id: - permission = this.permissionCheckWithRanges({ - workbookTypes: [WorkbookEditablePermission], - rangeTypes: [RangeProtectionPermissionEditPoint], - worksheetTypes: [WorksheetEditPermission, WorksheetSetCellValuePermission, WorksheetSetCellStylePermission], - }); - errorMsg = this._localeService.t('permission.dialog.commonErr'); - break; - case SetBackgroundColorCommand.id: - case SetRangeBoldCommand.id: - case SetRangeItalicCommand.id: - case SetRangeUnderlineCommand.id: - case SetRangeStrickThroughCommand.id: - permission = this.permissionCheckWithRanges({ - workbookTypes: [WorkbookEditablePermission], - rangeTypes: [RangeProtectionPermissionEditPoint], - worksheetTypes: [WorksheetSetCellStylePermission, WorksheetEditPermission], - }); - errorMsg = this._localeService.t('permission.dialog.setStyleErr'); - break; - case SheetCopyCommand.id: - permission = this.permissionCheckWithRanges({ - workbookTypes: [WorkbookCopyPermission], - rangeTypes: [RangeProtectionPermissionViewPoint], - worksheetTypes: [WorksheetCopyPermission], - }); - errorMsg = this._localeService.t('permission.dialog.copyErr'); - if (!this._permissionService.getPermissionPoint(new WorkbookCopyPermission(this._univerInstanceService.getCurrentUnitForType(UniverInstanceType.UNIVER_SHEET)!.getUnitId()).id)?.value) { - 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(UniverInstanceType.UNIVER_SHEET)!.getUnitId()).id)?.value) { - errorMsg = this._localeService.t('permission.dialog.workbookCopyErr'); - } - break; case DeltaColumnWidthCommand.id: case SetColWidthCommand.id: permission = this.permissionCheckWithoutRange({ @@ -212,11 +130,6 @@ export class SheetPermissionInterceptorBaseController extends Disposable { errorMsg = this._localeService.t('permission.dialog.moveRangeErr'); break; - case AutoFillCommand.id: - permission = this._permissionCheckByAutoFillCommand(params as IAutoFillCommandParams); - errorMsg = this._localeService.t('permission.dialog.autoFillErr'); - break; - case SetWorksheetOrderCommand.id: permission = this._permissionCheckByWorksheetCommand([WorkbookEditablePermission, WorkbookMoveSheetPermission]); errorMsg = this._localeService.t('permission.dialog.operatorSheetErr'); @@ -297,7 +210,7 @@ export class SheetPermissionInterceptorBaseController extends Disposable { } if (!permission) { - this.haveNotPermissionHandle(errorMsg); + this.blockExecuteWithoutPermission(errorMsg); } }; @@ -306,7 +219,6 @@ export class SheetPermissionInterceptorBaseController extends Disposable { } private _commandExecutedListener() { - // @ybzky todo If encounter problems with multiple instances later, you need to increase the unitId in command params. this.disposeWithMe( this._commandService.beforeCommandExecuted((command: ICommandInfo) => { this._getPermissionCheck(command.id, command?.params as ICheckPermissionCommandParams); @@ -344,17 +256,10 @@ export class SheetPermissionInterceptorBaseController extends Disposable { return false; }; if (direction === 'top' || direction === 'bottom') { - // selectionRange.startRow = 0; selectionRange.endRow = worksheet.getRowCount() - 1; } else if (direction === 'left' || direction === 'right') { - // selectionRange.startColumn = 0; selectionRange.endColumn = worksheet.getColumnCount() - 1; } - // } else if (direction === 'right') { - // selectionRange.endColumn = worksheet.getColumnCount() - 1; - // } else if (direction === 'bottom') { - // selectionRange.endRow = worksheet.getRowCount() - 1; - // } const selectionRuleRanges = this._rangeProtectionRuleModel.getSubunitRuleList(unitId, subUnitId).map((rule) => rule.ranges).flat(); const hasLap = selectionRuleRanges.some((range) => { @@ -483,28 +388,6 @@ export class SheetPermissionInterceptorBaseController extends Disposable { return true; } - private _permissionCheckByPaste(params: ISheetPasteParams) { - if (params.value === PREDEFINED_HOOK_NAME.SPECIAL_PASTE_VALUE || params.value === PREDEFINED_HOOK_NAME.SPECIAL_PASTE_FORMULA) { - return this.permissionCheckWithRanges({ - workbookTypes: [WorkbookEditablePermission], - rangeTypes: [RangeProtectionPermissionEditPoint], - worksheetTypes: [WorksheetSetCellStylePermission, WorksheetEditPermission], - }); - } else if (params.value === PREDEFINED_HOOK_NAME.SPECIAL_PASTE_FORMAT) { - return this.permissionCheckWithRanges({ - workbookTypes: [WorkbookEditablePermission], - rangeTypes: [RangeProtectionPermissionEditPoint], - worksheetTypes: [WorksheetSetCellStylePermission, WorksheetEditPermission], - }); - } else { - return this.permissionCheckWithRanges({ - workbookTypes: [WorkbookEditablePermission], - rangeTypes: [RangeProtectionPermissionEditPoint], - worksheetTypes: [WorksheetSetCellValuePermission, WorksheetSetCellStylePermission, WorksheetEditPermission], - }); - } - } - private _permissionCheckByMoveCommand(params: IMoveRowsCommandParams | IMoveColsCommandParams) { const target = getSheetCommandTarget(this._univerInstanceService); if (!target) { @@ -539,40 +422,6 @@ export class SheetPermissionInterceptorBaseController extends Disposable { return true; } - private _permissionCheckByAutoFillCommand(params?: { targetRange: IRange }) { - if (!params) { - return false; - } - - const { targetRange } = params; - - const target = getSheetCommandTarget(this._univerInstanceService); - if (!target) { - return false; - } - const { worksheet, unitId, subUnitId } = target; - - const permissionLapRanges = this._rangeProtectionRuleModel.getSubunitRuleList(unitId, subUnitId).reduce((p, c) => { - return [...p, ...c.ranges]; - }, [] as IRange[]).filter((range) => { - return Rectangle.intersects(range, targetRange); - }); - - const hasNotPermission = permissionLapRanges.some((range) => { - for (let row = range.startRow; row <= range.endRow; row++) { - for (let col = range.startColumn; col <= range.endColumn; col++) { - const permission = (worksheet.getCell(row, col) as (ICellDataForSheetInterceptor & { selectionProtection: ICellPermission[] }))?.selectionProtection?.[0]; - if (permission?.[UnitAction.Edit] === false) { - return true; - } - } - } - return false; - }); - - return !hasNotPermission; - } - private _permissionCheckByMoveRangeCommand(params: IMoveRangeCommandParams) { const target = getSheetCommandTarget(this._univerInstanceService); if (!target) { diff --git a/packages/sheets-ui/src/controllers/permission/sheet-permission-init.controller.ts b/packages/sheets/src/controllers/permission/sheet-permission-init.controller.ts similarity index 93% rename from packages/sheets-ui/src/controllers/permission/sheet-permission-init.controller.ts rename to packages/sheets/src/controllers/permission/sheet-permission-init.controller.ts index ccc40fb8548..13207292c96 100644 --- a/packages/sheets-ui/src/controllers/permission/sheet-permission-init.controller.ts +++ b/packages/sheets/src/controllers/permission/sheet-permission-init.controller.ts @@ -16,11 +16,22 @@ import type { Workbook } from '@univerjs/core'; import type { UnitAction } from '@univerjs/protocol'; -import type { IAddRangeProtectionMutationParams, IAddWorksheetProtectionParams, ISetWorksheetPermissionPointsMutationParams } from '@univerjs/sheets'; +import type { IAddRangeProtectionMutationParams } from '../../commands/mutations/add-range-protection.mutation'; +import type { IAddWorksheetProtectionParams } from '../../commands/mutations/add-worksheet-protection.mutation'; +import type { ISetWorksheetPermissionPointsMutationParams } from '../../commands/mutations/set-worksheet-permission-points.mutation'; import { Disposable, IAuthzIoService, ICommandService, Inject, IPermissionService, IUndoRedoService, IUniverInstanceService, UniverInstanceType, UserManagerService } from '@univerjs/core'; import { UnitObject } from '@univerjs/protocol'; -import { AddRangeProtectionMutation, AddWorksheetProtectionMutation, baseProtectionActions, defaultWorkbookPermissionPoints, defaultWorksheetPermissionPoint, getAllRangePermissionPoint, getAllWorkbookPermissionPoint, getAllWorksheetPermissionPoint, getAllWorksheetPermissionPointByPointPanel, RangeProtectionCache, RangeProtectionRuleModel, SetWorksheetPermissionPointsMutation, SheetInterceptorService, WorksheetProtectionPointModel, WorksheetProtectionRuleModel } from '@univerjs/sheets'; import { skip } from 'rxjs'; +import { AddRangeProtectionMutation } from '../../commands/mutations/add-range-protection.mutation'; +import { AddWorksheetProtectionMutation } from '../../commands/mutations/add-worksheet-protection.mutation'; +import { SetWorksheetPermissionPointsMutation } from '../../commands/mutations/set-worksheet-permission-points.mutation'; +import { RangeProtectionRuleModel } from '../../model/range-protection-rule.model'; +import { RangeProtectionCache } from '../../model/range-protection.cache'; +import { defaultWorksheetPermissionPoint, getAllWorksheetPermissionPoint, getAllWorksheetPermissionPointByPointPanel } from '../../services/permission'; +import { baseProtectionActions, getAllRangePermissionPoint } from '../../services/permission/range-permission/util'; +import { defaultWorkbookPermissionPoints, getAllWorkbookPermissionPoint } from '../../services/permission/workbook-permission'; +import { WorksheetProtectionPointModel, WorksheetProtectionRuleModel } from '../../services/permission/worksheet-permission'; +import { SheetInterceptorService } from '../../services/sheet-interceptor/sheet-interceptor.service'; export class SheetPermissionInitController extends Disposable { constructor( diff --git a/packages/sheets-ui/src/controllers/permission/sheet-permission-view-model.controller.ts b/packages/sheets/src/controllers/permission/sheet-permission-view-model.controller.ts similarity index 87% rename from packages/sheets-ui/src/controllers/permission/sheet-permission-view-model.controller.ts rename to packages/sheets/src/controllers/permission/sheet-permission-view-model.controller.ts index ee44db7eaf8..f603c08e84d 100644 --- a/packages/sheets-ui/src/controllers/permission/sheet-permission-view-model.controller.ts +++ b/packages/sheets/src/controllers/permission/sheet-permission-view-model.controller.ts @@ -14,10 +14,14 @@ * limitations under the License. */ -import type { IWorksheetProtectionRenderCellData } from '@univerjs/sheets'; +import type { IWorksheetProtectionRenderCellData } from '../../services/permission/worksheet-permission/type'; import { Disposable, Inject, InterceptorEffectEnum, IPermissionService } from '@univerjs/core'; import { UnitAction } from '@univerjs/protocol'; -import { INTERCEPTOR_POINT, RangeProtectionCache, SheetInterceptorService, WorksheetEditPermission, WorksheetProtectionRuleModel, WorksheetViewPermission } from '@univerjs/sheets'; +import { RangeProtectionCache } from '../../model/range-protection.cache'; +import { WorksheetEditPermission, WorksheetViewPermission } from '../../services/permission/permission-point'; +import { WorksheetProtectionRuleModel } from '../../services/permission/worksheet-permission'; +import { INTERCEPTOR_POINT } from '../../services/sheet-interceptor/interceptor-const'; +import { SheetInterceptorService } from '../../services/sheet-interceptor/sheet-interceptor.service'; export class SheetPermissionViewModelController extends Disposable { constructor( diff --git a/packages/sheets/src/index.ts b/packages/sheets/src/index.ts index 40852622ccc..4b517076aca 100644 --- a/packages/sheets/src/index.ts +++ b/packages/sheets/src/index.ts @@ -101,6 +101,8 @@ export { ExclusiveRangeService, IExclusiveRangeService } from './services/exclus // permission export { defaultWorksheetPermissionPoint, getAllWorksheetPermissionPoint, getAllWorksheetPermissionPointByPointPanel } from './services/permission'; export type { IWorksheetProtectionRule } from './services/permission/type'; +export { SheetPermissionCheckController } from './controllers/permission/sheet-permission-check.controller'; +export { SheetPermissionInitController } from './controllers/permission/sheet-permission-init.controller'; export { WorksheetProtectionPointModel, WorksheetProtectionRuleModel } from './services/permission/worksheet-permission'; export { defaultWorkbookPermissionPoints, getAllWorkbookPermissionPoint } from './services/permission/workbook-permission'; export { diff --git a/packages/sheets/src/sheets-plugin.ts b/packages/sheets/src/sheets-plugin.ts index c403ccc88c2..47981e1a079 100644 --- a/packages/sheets/src/sheets-plugin.ts +++ b/packages/sheets/src/sheets-plugin.ts @@ -25,7 +25,11 @@ import { defaultPluginConfig, SHEETS_PLUGIN_CONFIG_KEY } from './controllers/con import { DefinedNameDataController } from './controllers/defined-name-data.controller'; import { MergeCellController } from './controllers/merge-cell.controller'; import { NumberCellDisplayController } from './controllers/number-cell.controller'; +import { SheetPermissionCheckController } from './controllers/permission/sheet-permission-check.controller'; +import { SheetPermissionInitController } from './controllers/permission/sheet-permission-init.controller'; +import { SheetPermissionViewModelController } from './controllers/permission/sheet-permission-view-model.controller'; import { RangeProtectionRenderModel } from './model/range-protection-render.model'; + import { RangeProtectionRuleModel } from './model/range-protection-rule.model'; import { RangeProtectionCache } from './model/range-protection.cache'; import { SheetRangeThemeModel } from './model/range-theme-model'; @@ -99,6 +103,9 @@ export class UniverSheetsPlugin extends Plugin { [WorksheetPermissionService], [WorksheetProtectionRuleModel], [WorksheetProtectionPointModel], + [SheetPermissionViewModelController], + [SheetPermissionInitController], + [SheetPermissionCheckController], // range theme [SheetRangeThemeModel], @@ -134,12 +141,14 @@ export class UniverSheetsPlugin extends Plugin { [MergeCellController], [WorkbookPermissionService], [WorksheetPermissionService], + [SheetPermissionViewModelController], ]); } override onRendered(): void { touchDependencies(this._injector, [ [INumfmtService], + [SheetPermissionInitController], ]); } @@ -152,6 +161,7 @@ export class UniverSheetsPlugin extends Plugin { [RangeProtectionRenderModel], [RangeProtectionRefRangeService], [RefRangeService], + [SheetPermissionCheckController], ]); } }