Skip to content

Commit

Permalink
feat(sheet): exclusive range serivce (#3278)
Browse files Browse the repository at this point in the history
  • Loading branch information
VicKun4937 authored Sep 3, 2024
1 parent 183b3e0 commit 093ee31
Show file tree
Hide file tree
Showing 13 changed files with 225 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

import type { IWorkbookData } from '@univerjs/core';
import { AuthzIoLocalService, DisposableCollection, IAuthzIoService, ICommandService, Inject, Injector, LocaleType, Plugin, Univer, UniverInstanceType } from '@univerjs/core';
import { RangeProtectionRuleModel, RefRangeService, SetWorksheetActiveOperation, SheetInterceptorService, SheetsSelectionsService, WorkbookPermissionService, WorksheetPermissionService, WorksheetProtectionPointModel, WorksheetProtectionRuleModel } from '@univerjs/sheets';
import { ExclusiveRangeService, IExclusiveRangeService, RangeProtectionRuleModel, RefRangeService, SetWorksheetActiveOperation, SheetInterceptorService, SheetsSelectionsService, WorkbookPermissionService, WorksheetPermissionService, WorksheetProtectionPointModel, WorksheetProtectionRuleModel } from '@univerjs/sheets';
import type { ISetSheetsFilterCriteriaMutationParams, ISetSheetsFilterRangeMutationParams } from '@univerjs/sheets-filter';
import { RemoveSheetsFilterMutation, SetSheetsFilterCriteriaMutation, SetSheetsFilterRangeMutation, UniverSheetsFilterPlugin } from '@univerjs/sheets-filter';
import { IMenuService, IPlatformService, IShortcutService, MenuService, PlatformService, ShortcutService } from '@univerjs/ui';
Expand Down Expand Up @@ -76,6 +76,7 @@ function createSheetsFilterMenuTestBed() {
injector.add([WorksheetProtectionRuleModel]);
injector.add([SheetInterceptorService]);
injector.add([RangeProtectionRuleModel]);
injector.add([IExclusiveRangeService, { useClass: ExclusiveRangeService, deps: [SheetsSelectionsService] }]);

const commandService = injector.get(ICommandService);
[
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import { SheetsFilterService } from '@univerjs/sheets-filter';
import { UniverInstanceType } from '@univerjs/core';

import { map, of, switchMap } from 'rxjs';
import { getCurrentRangeDisable$ } from '@univerjs/sheets-ui';
import { getCurrentRangeDisable$, getObservableWithExclusiveRange$ } from '@univerjs/sheets-ui';
import { RangeProtectionPermissionViewPoint, WorksheetFilterPermission, WorksheetViewPermission } from '@univerjs/sheets';
import { ClearSheetsFilterCriteriaCommand, ReCalcSheetsFilterCommand, SmartToggleSheetsFilterCommand } from '../commands/commands/sheets-filter.command';

Expand All @@ -37,7 +37,7 @@ export function SmartToggleFilterMenuItemFactory(accessor: IAccessor): IMenuSele
positions: [MenuPosition.TOOLBAR_START],
hidden$: getMenuHiddenObservable(accessor, UniverInstanceType.UNIVER_SHEET),
activated$: sheetsFilterService.activeFilterModel$.pipe(map((model) => !!model)),
disabled$: getCurrentRangeDisable$(accessor, { worksheetTypes: [WorksheetFilterPermission, WorksheetViewPermission], rangeTypes: [RangeProtectionPermissionViewPoint] }),
disabled$: getObservableWithExclusiveRange$(accessor, getCurrentRangeDisable$(accessor, { worksheetTypes: [WorksheetFilterPermission, WorksheetViewPermission], rangeTypes: [RangeProtectionPermissionViewPoint] })),
};
}

Expand Down
6 changes: 3 additions & 3 deletions packages/sheets-hyper-link-ui/src/controllers/menu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import { UniverInstanceType } from '@univerjs/core';
import type { IMenuItem, IShortcutItem } from '@univerjs/ui';
import { getMenuHiddenObservable, KeyCode, MenuGroup, MenuItemType, MenuPosition, MetaKeys } from '@univerjs/ui';
import type { IAccessor } from '@univerjs/core';
import { getCurrentRangeDisable$, whenSheetEditorFocused } from '@univerjs/sheets-ui';
import { getCurrentRangeDisable$, getObservableWithExclusiveRange$, whenSheetEditorFocused } from '@univerjs/sheets-ui';
import { RangeProtectionPermissionEditPoint, WorkbookEditablePermission, WorksheetEditPermission, WorksheetInsertHyperlinkPermission, WorksheetSetCellValuePermission } from '@univerjs/sheets';
import { InsertHyperLinkOperation, InsertHyperLinkToolbarOperation } from '../commands/operations/sidebar.operations';

Expand All @@ -30,7 +30,7 @@ export const insertLinkMenuFactory = (accessor: IAccessor) => {
title: 'hyperLink.menu.add',
hidden$: getMenuHiddenObservable(accessor, UniverInstanceType.UNIVER_SHEET),
icon: 'LinkSingle',
disabled$: getCurrentRangeDisable$(accessor, { workbookTypes: [WorkbookEditablePermission], worksheetTypes: [WorksheetEditPermission, WorksheetSetCellValuePermission, WorksheetInsertHyperlinkPermission], rangeTypes: [RangeProtectionPermissionEditPoint] }),
disabled$: getObservableWithExclusiveRange$(accessor, getCurrentRangeDisable$(accessor, { workbookTypes: [WorkbookEditablePermission], worksheetTypes: [WorksheetEditPermission, WorksheetSetCellValuePermission, WorksheetInsertHyperlinkPermission], rangeTypes: [RangeProtectionPermissionEditPoint] })),
} as IMenuItem;
};

Expand All @@ -43,7 +43,7 @@ export const insertLinkMenuToolbarFactory = (accessor: IAccessor) => {
type: MenuItemType.BUTTON,
icon: 'LinkSingle',
hidden$: getMenuHiddenObservable(accessor, UniverInstanceType.UNIVER_SHEET),
disabled$: getCurrentRangeDisable$(accessor, { workbookTypes: [WorkbookEditablePermission], worksheetTypes: [WorksheetEditPermission, WorksheetSetCellValuePermission, WorksheetInsertHyperlinkPermission], rangeTypes: [RangeProtectionPermissionEditPoint] }),
disabled$: getObservableWithExclusiveRange$(accessor, getCurrentRangeDisable$(accessor, { workbookTypes: [WorkbookEditablePermission], worksheetTypes: [WorksheetEditPermission, WorksheetSetCellValuePermission, WorksheetInsertHyperlinkPermission], rangeTypes: [RangeProtectionPermissionEditPoint] })),

};
};
Expand Down
6 changes: 3 additions & 3 deletions packages/sheets-ui/src/controllers/menu/clear.menu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import { getMenuHiddenObservable, MenuGroup, MenuItemType, MenuPosition } from '
import type { IAccessor } from '@univerjs/core';
import { UniverInstanceType } from '@univerjs/core';
import { SheetMenuPosition } from './menu';
import { getCurrentRangeDisable$ } from './menu-util';
import { getCurrentRangeDisable$, getObservableWithExclusiveRange$ } from './menu-util';

const CLEAR_SELECTION_MENU_ID = 'sheet.menu.clear-selection';
export function ClearSelectionMenuItemFactory(accessor: IAccessor): IMenuSelectorItem<string> {
Expand All @@ -46,7 +46,7 @@ export function ClearSelectionContentMenuItemFactory(accessor: IAccessor): IMenu
type: MenuItemType.BUTTON,
title: 'rightClick.clearContent',
positions: [CLEAR_SELECTION_MENU_ID],
disabled$: getCurrentRangeDisable$(accessor, { workbookTypes: [WorkbookEditablePermission], worksheetTypes: [WorksheetEditPermission, WorksheetSetCellValuePermission], rangeTypes: [RangeProtectionPermissionEditPoint] }),
disabled$: getObservableWithExclusiveRange$(accessor, getCurrentRangeDisable$(accessor, { workbookTypes: [WorkbookEditablePermission], worksheetTypes: [WorksheetEditPermission, WorksheetSetCellValuePermission], rangeTypes: [RangeProtectionPermissionEditPoint] })),
hidden$: getMenuHiddenObservable(accessor, UniverInstanceType.UNIVER_SHEET),
};
}
Expand All @@ -66,7 +66,7 @@ export function ClearSelectionAllMenuItemFactory(accessor: IAccessor): IMenuButt
type: MenuItemType.BUTTON,
title: 'rightClick.clearAll',
positions: [CLEAR_SELECTION_MENU_ID],
disabled$: getCurrentRangeDisable$(accessor, { workbookTypes: [WorkbookEditablePermission], worksheetTypes: [WorksheetEditPermission, WorksheetSetCellValuePermission, WorksheetSetCellStylePermission], rangeTypes: [RangeProtectionPermissionEditPoint] }),
disabled$: getObservableWithExclusiveRange$(accessor, getCurrentRangeDisable$(accessor, { workbookTypes: [WorkbookEditablePermission], worksheetTypes: [WorksheetEditPermission, WorksheetSetCellValuePermission, WorksheetSetCellStylePermission], rangeTypes: [RangeProtectionPermissionEditPoint] })),
hidden$: getMenuHiddenObservable(accessor, UniverInstanceType.UNIVER_SHEET),
};
}
4 changes: 2 additions & 2 deletions packages/sheets-ui/src/controllers/menu/delete.menu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import {
RemoveRowConfirmCommand,
} from '../../commands/commands/remove-row-col-confirm.command';
import { SheetMenuPosition } from './menu';
import { getBaseRangeMenuHidden$, getCellMenuHidden$, getCurrentRangeDisable$, getDeleteMenuHidden$ } from './menu-util';
import { getBaseRangeMenuHidden$, getCellMenuHidden$, getCurrentRangeDisable$, getDeleteMenuHidden$, getObservableWithExclusiveRange$ } from './menu-util';

const DELETE_RANGE_MENU_ID = 'sheet.menu.delete';
export function DeleteRangeMenuItemFactory(accessor: IAccessor): IMenuSelectorItem<string> {
Expand All @@ -37,7 +37,7 @@ export function DeleteRangeMenuItemFactory(accessor: IAccessor): IMenuSelectorIt
title: 'rightClick.delete',
icon: 'Reduce',
positions: [MenuPosition.CONTEXT_MENU],
hidden$: getBaseRangeMenuHidden$(accessor),
hidden$: getObservableWithExclusiveRange$(accessor, getBaseRangeMenuHidden$(accessor)),
disabled$: getCurrentRangeDisable$(accessor, { workbookTypes: [WorkbookEditablePermission], worksheetTypes: [WorksheetEditPermission] }),
};
}
Expand Down
4 changes: 2 additions & 2 deletions packages/sheets-ui/src/controllers/menu/insert.menu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import type { IAccessor } from '@univerjs/core';
import { InsertRangeMoveDownConfirmCommand } from '../../commands/commands/insert-range-move-down-confirm.command';
import { InsertRangeMoveRightConfirmCommand } from '../../commands/commands/insert-range-move-right-confirm.command';
import { SheetMenuPosition } from './menu';
import { getBaseRangeMenuHidden$, getCellMenuHidden$, getCurrentRangeDisable$, getInsertAfterMenuHidden$, getInsertBeforeMenuHidden$ } from './menu-util';
import { getBaseRangeMenuHidden$, getCellMenuHidden$, getCurrentRangeDisable$, getInsertAfterMenuHidden$, getInsertBeforeMenuHidden$, getObservableWithExclusiveRange$ } from './menu-util';

const COL_INSERT_MENU_ID = 'sheet.menu.col-insert';
export function ColInsertMenuItemFactory(accessor: IAccessor): IMenuSelectorItem<string> {
Expand Down Expand Up @@ -71,7 +71,7 @@ export function CellInsertMenuItemFactory(accessor: IAccessor): IMenuSelectorIte
title: 'rightClick.insert',
icon: 'Insert',
positions: [MenuPosition.CONTEXT_MENU],
hidden$: getBaseRangeMenuHidden$(accessor),
hidden$: getObservableWithExclusiveRange$(accessor, getBaseRangeMenuHidden$(accessor)),
disabled$: getCurrentRangeDisable$(accessor, { workbookTypes: [WorkbookEditablePermission], worksheetTypes: [WorksheetInsertColumnPermission, WorksheetEditPermission], rangeTypes: [RangeProtectionPermissionEditPoint] }),
};
}
Expand Down
55 changes: 54 additions & 1 deletion packages/sheets-ui/src/controllers/menu/menu-util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import { FOCUSING_COMMON_DRAWINGS, IContextService, IPermissionService, IUniverI
import { UnitAction } from '@univerjs/protocol';

import type { ICellPermission } from '@univerjs/sheets';
import { RangeProtectionRuleModel, SheetsSelectionsService, WorkbookEditablePermission, WorkbookManageCollaboratorPermission, WorksheetProtectionRuleModel } from '@univerjs/sheets';
import { IExclusiveRangeService, RangeProtectionRuleModel, SheetsSelectionsService, WorkbookEditablePermission, WorkbookManageCollaboratorPermission, WorksheetProtectionRuleModel } from '@univerjs/sheets';
import type { Observable } from 'rxjs';
import { combineLatest, map, of, startWith, switchMap } from 'rxjs';

Expand All @@ -45,6 +45,59 @@ export function deriveStateFromActiveSheet$<T>(univerInstanceService: IUniverIns
}));
}

/**
* @description Get the current exclusive range disable status
* @param accessor The accessor
* @param {Set<string>} [disableGroupSet] The disable group set, if provided, check if the interestGroupIds contains any of the disableGroupSet, otherwise check if the interestGroupIds is not empty
* @returns {Observable<boolean>} The current exclusive range disable status
*/
export function getCurrentExclusiveRangeInterest$(accessor: IAccessor, disableGroupSet?: Set<string>) {
const univerInstanceService = accessor.get(IUniverInstanceService);
const exclusiveRangeService = accessor.get(IExclusiveRangeService);
const selectionManagerService = accessor.get(SheetsSelectionsService);
const workbook$ = univerInstanceService.getCurrentTypeOfUnit$<Workbook>(UniverInstanceType.UNIVER_SHEET);

return workbook$.pipe(
switchMap((workbook) => {
if (!workbook) {
return of(false);
}
return combineLatest([selectionManagerService.selectionMoveEnd$, workbook.activeSheet$]).pipe(
switchMap(([selections, worksheet]) => {
if (!worksheet) {
return of(false);
}
if (selections.length === 0) {
return of(false);
}

const interestGroupIds = exclusiveRangeService.getInterestGroupId(selections);
// if disableGroupSet is provided, check if the interestGroupIds contains any of the disableGroupSet
if (disableGroupSet) {
const disableGroup = interestGroupIds.filter((groupId) => disableGroupSet.has(groupId));
return of(disableGroup.length > 0);
} else {
return of(interestGroupIds.length > 0);
}
})
);
})
);
}

/**
* Get the observable combine with exclusive range
* @param accessor The accessor
* @param {Observable<boolean>} observable$
* @param {Set<string>} [disableGroupSet] The disable group set, if provided, check if the interestGroupIds contains any of the disableGroupSet, otherwise check if the interestGroupIds is not empty
* @returns {Observable<boolean>} The observable combine with exclusive range
*/
export function getObservableWithExclusiveRange$(accessor: IAccessor, observable$: Observable<boolean>, disableGroupSet?: Set<string>): Observable<boolean> {
return combineLatest([observable$, getCurrentExclusiveRangeInterest$(accessor, disableGroupSet)]).pipe(
map(([observable, exclusiveRangeDisable]) => observable || exclusiveRangeDisable)
);
}

export function getCurrentRangeDisable$(accessor: IAccessor, permissionTypes: IPermissionTypes = {}) {
const univerInstanceService = accessor.get(IUniverInstanceService);
const workbook$ = univerInstanceService.getCurrentTypeOfUnit$<Workbook>(UniverInstanceType.UNIVER_SHEET);
Expand Down
4 changes: 2 additions & 2 deletions packages/sheets-ui/src/controllers/menu/menu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ import { FONT_FAMILY_COMPONENT, FONT_FAMILY_ITEM_COMPONENT } from '../../compone
import { FONT_SIZE_COMPONENT } from '../../components/font-size';
import { MENU_ITEM_INPUT_COMPONENT } from '../../components/menu-item-input';
import { FormatPainterStatus, IFormatPainterService } from '../../services/format-painter/format-painter.service';
import { deriveStateFromActiveSheet$, getCurrentRangeDisable$ } from './menu-util';
import { deriveStateFromActiveSheet$, getCurrentRangeDisable$, getObservableWithExclusiveRange$ } from './menu-util';

export enum SheetMenuPosition {
ROW_HEADER_CONTEXT_MENU = 'ROW_HEADER_CONTEXT_MENU',
Expand Down Expand Up @@ -970,7 +970,7 @@ export function PasteSpacialMenuItemFactory(accessor: IAccessor): IMenuSelectorI
SheetMenuPosition.COL_HEADER_CONTEXT_MENU,
SheetMenuPosition.ROW_HEADER_CONTEXT_MENU,
],
hidden$: getMenuHiddenObservable(accessor, UniverInstanceType.UNIVER_SHEET),
hidden$: getObservableWithExclusiveRange$(accessor, getMenuHiddenObservable(accessor, UniverInstanceType.UNIVER_SHEET)),
};
}

Expand Down
2 changes: 1 addition & 1 deletion packages/sheets-ui/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export { SheetsUIPart } from './consts/ui-name';
export { SHEET_UI_PLUGIN_NAME } from './consts/plugin-name';
export { getEditorObject } from './basics/editor/get-editor-object';
export { SheetsScrollRenderController } from './controllers/render-controllers/scroll.render-controller';
export { deriveStateFromActiveSheet$, getCurrentRangeDisable$ } from './controllers/menu/menu-util';
export { deriveStateFromActiveSheet$, getCurrentRangeDisable$, getObservableWithExclusiveRange$, getCurrentExclusiveRangeInterest$ } from './controllers/menu/menu-util';
export { SheetsRenderService } from './services/sheets-render.service';

export { SHEET_VIEWPORT_KEY as VIEWPORT_KEY } from './common/keys';
Expand Down
3 changes: 2 additions & 1 deletion packages/sheets-zen-editor/src/views/menu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

import { type IMenuButtonItem, MenuGroup, MenuItemType, MenuPosition } from '@univerjs/ui';

import { getCurrentRangeDisable$ } from '@univerjs/sheets-ui';
import { getCurrentExclusiveRangeInterest$, getCurrentRangeDisable$ } from '@univerjs/sheets-ui';
import type { IAccessor } from '@univerjs/core';
import { RangeProtectionPermissionEditPoint, WorkbookEditablePermission, WorksheetEditPermission, WorksheetSetCellStylePermission, WorksheetSetCellValuePermission } from '@univerjs/sheets';
import { OpenZenEditorOperation } from '../commands/operations/zen-editor.operation';
Expand All @@ -29,6 +29,7 @@ export function ZenEditorMenuItemFactory(accessor: IAccessor): IMenuButtonItem {
title: 'rightClick.zenEditor',
icon: 'AmplifySingle',
positions: [MenuPosition.CONTEXT_MENU],
hidden$: getCurrentExclusiveRangeInterest$(accessor),
disabled$: getCurrentRangeDisable$(accessor, { workbookTypes: [WorkbookEditablePermission], worksheetTypes: [WorksheetEditPermission, WorksheetSetCellValuePermission, WorksheetSetCellStylePermission], rangeTypes: [RangeProtectionPermissionEditPoint] }),
};
}
2 changes: 2 additions & 0 deletions packages/sheets/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,8 @@ export { AddMergeRedoSelectionsOperationFactory, AddMergeUndoSelectionsOperation
export type { FormatType } from './services/numfmt/type';
export { expandToContinuousRange } from './basics/expand-range';

export { ExclusiveRangeService, IExclusiveRangeService } from './services/exclusive-range/exclusive-range-service';

// permission
export { defaultWorksheetPermissionPoint, getAllWorksheetPermissionPoint, getAllWorksheetPermissionPointByPointPanel } from './services/permission';
export type { IWorksheetProtectionRule } from './services/permission/type';
Expand Down
Loading

0 comments on commit 093ee31

Please sign in to comment.