Skip to content

Build your own AI office with Univer Office SDK. Bring editing, calculation, collaboration, and file processing into your product for people and AI agents. Built on an extensible open-source core.

feat(sheet): exclusive range serivce (#3278)

VicKun093ee318884d
diff --git a/packages/sheets-filter-ui/src/controllers/__tests__/sheets-filter.menu.spec.ts b/packages/sheets-filter-ui/src/controllers/__tests__/sheets-filter.menu.spec.ts
index 4d77b8b389..6d1968266b 100644
--- a/packages/sheets-filter-ui/src/controllers/__tests__/sheets-filter.menu.spec.ts
+++ b/packages/sheets-filter-ui/src/controllers/__tests__/sheets-filter.menu.spec.ts
@@ -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';
@@ -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);
[
diff --git a/packages/sheets-filter-ui/src/controllers/sheets-filter.menu.ts b/packages/sheets-filter-ui/src/controllers/sheets-filter.menu.ts
index 65342e6c87..5f9a2345a8 100644
--- a/packages/sheets-filter-ui/src/controllers/sheets-filter.menu.ts
+++ b/packages/sheets-filter-ui/src/controllers/sheets-filter.menu.ts
@@ -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';
@@ -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] })),
};
}
diff --git a/packages/sheets-hyper-link-ui/src/controllers/menu.ts b/packages/sheets-hyper-link-ui/src/controllers/menu.ts
index 124067d6a1..71159897d1 100644
--- a/packages/sheets-hyper-link-ui/src/controllers/menu.ts
+++ b/packages/sheets-hyper-link-ui/src/controllers/menu.ts
@@ -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';
@@ -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;
};
@@ -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] })),
};
};
diff --git a/packages/sheets-ui/src/controllers/menu/clear.menu.ts b/packages/sheets-ui/src/controllers/menu/clear.menu.ts
index a87b3bc42b..b90f7c83bb 100644
--- a/packages/sheets-ui/src/controllers/menu/clear.menu.ts
+++ b/packages/sheets-ui/src/controllers/menu/clear.menu.ts
@@ -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> {
@@ -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),
};
}
@@ -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),
};
}
diff --git a/packages/sheets-ui/src/controllers/menu/delete.menu.ts b/packages/sheets-ui/src/controllers/menu/delete.menu.ts
index c135396b79..d0a71f7470 100644
--- a/packages/sheets-ui/src/controllers/menu/delete.menu.ts
+++ b/packages/sheets-ui/src/controllers/menu/delete.menu.ts
@@ -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> {
@@ -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] }),
};
}
diff --git a/packages/sheets-ui/src/controllers/menu/insert.menu.ts b/packages/sheets-ui/src/controllers/menu/insert.menu.ts
index ecceb30c88..827cb68aad 100644
--- a/packages/sheets-ui/src/controllers/menu/insert.menu.ts
+++ b/packages/sheets-ui/src/controllers/menu/insert.menu.ts
@@ -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> {
@@ -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] }),
};
}
diff --git a/packages/sheets-ui/src/controllers/menu/menu-util.ts b/packages/sheets-ui/src/controllers/menu/menu-util.ts
index 92d14d2a72..57c0101316 100644
--- a/packages/sheets-ui/src/controllers/menu/menu-util.ts
+++ b/packages/sheets-ui/src/controllers/menu/menu-util.ts
@@ -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';
@@ -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);
diff --git a/packages/sheets-ui/src/controllers/menu/menu.ts b/packages/sheets-ui/src/controllers/menu/menu.ts
index 39bbaa5625..c19b0abacd 100644
--- a/packages/sheets-ui/src/controllers/menu/menu.ts
+++ b/packages/sheets-ui/src/controllers/menu/menu.ts
@@ -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',
@@ -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)),
};
}
diff --git a/packages/sheets-ui/src/index.ts b/packages/sheets-ui/src/index.ts
index 62dab92b72..db1acf8764 100644
--- a/packages/sheets-ui/src/index.ts
+++ b/packages/sheets-ui/src/index.ts
@@ -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';
diff --git a/packages/sheets-zen-editor/src/views/menu.ts b/packages/sheets-zen-editor/src/views/menu.ts
index f30497b001..aa064ce2ee 100644
--- a/packages/sheets-zen-editor/src/views/menu.ts
+++ b/packages/sheets-zen-editor/src/views/menu.ts
@@ -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';
@@ -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] }),
};
}
diff --git a/packages/sheets/src/index.ts b/packages/sheets/src/index.ts
index 33e1b3e950..6780c7284e 100644
--- a/packages/sheets/src/index.ts
+++ b/packages/sheets/src/index.ts
@@ -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';
diff --git a/packages/sheets/src/services/exclusive-range/exclusive-range-service.ts b/packages/sheets/src/services/exclusive-range/exclusive-range-service.ts
new file mode 100644
index 0000000000..c64459e0cc
--- /dev/null
+++ b/packages/sheets/src/services/exclusive-range/exclusive-range-service.ts
@@ -0,0 +1,144 @@
+/**
+ * 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.
+ */
+
+import type { IRange } from '@univerjs/core';
+import { createIdentifier, Disposable, Rectangle } from '@univerjs/core';
+import type { ISelectionWithStyle } from '../../basics/selection';
+
+interface IFeatureRange {
+ groupId: string;
+ range: IRange;
+}
+
+export interface IExclusiveRangeService {
+ /**
+ * @description Add an exclusive range to the service
+ * @param {string} unitId The unitId of the exclusive range
+ * @param {string} sheetId The sheetId of the exclusive range
+ * @param {string} feature The feature of the exclusive range
+ * @param {IFeatureRange} range The exclusive range
+ */
+ addExclusiveRange(unitId: string, sheetId: string, feature: string, ranges: IFeatureRange[]): void;
+ /**
+ * @description Get the exclusive ranges
+ * @param {string} unitId The unitId of the exclusive range
+ * @param {string} sheetId The sheetId of the exclusive range
+ * @param {string} feature The feature of the exclusive range
+ * @returns {undefined | IFeatureRange[]} The exclusive ranges
+ */
+ getExclusiveRanges(unitId: string, sheetId: string, feature: string): undefined | IFeatureRange[];
+ /**
+ * @description Clear the exclusive ranges
+ * @param {string} unitId The unitId of the exclusive range
+ * @param {string} sheetId The sheetId of the exclusive range
+ * @param {string} feature The feature of the exclusive range
+ */
+ clearExclusiveRanges(unitId: string, sheetId: string, feature: string): void;
+
+ /**
+ * @description Clear the exclusive ranges by groupId
+ * @param {string} unitId The unitId of the exclusive range
+ * @param {string} sheetId The sheetId of the exclusive range
+ * @param {string} feature The feature of the exclusive range
+ * @param {string} groupId The groupId of the exclusive range
+ */
+ clearExclusiveRangesByGroupId(unitId: string, sheetId: string, feature: string, groupId: string): void;
+ /**
+ * Check the interest group id of the giving selection
+ * @param {ISelectionWithStyle[]} selections The selections to check
+ */
+ getInterestGroupId(selections: ISelectionWithStyle[]): string[];
+}
+export const IExclusiveRangeService = createIdentifier<IExclusiveRangeService>('univer.exclusive-range-service');
+
+export class ExclusiveRangeService extends Disposable implements IExclusiveRangeService {
+ /**
+ * Exclusive range data structure is as follows: unitId -> sheetId -> feature -> range
+ */
+ private _exclusiveRanges: Map<string, Map<string, Map<string, IFeatureRange[]>>> = new Map();
+
+ private _ensureUnitMap(unitId: string) {
+ if (!this._exclusiveRanges.has(unitId)) {
+ this._exclusiveRanges.set(unitId, new Map());
+ }
+ return this._exclusiveRanges.get(unitId)!;
+ }
+
+ private _ensureSubunitMap(unitId: string, sheetId: string) {
+ const unitMap = this._ensureUnitMap(unitId);
+ if (!unitMap.has(sheetId)) {
+ unitMap.set(sheetId, new Map());
+ }
+ return unitMap.get(sheetId)!;
+ }
+
+ private _ensureFeature(unitId: string, sheetId: string, feature: string) {
+ const subunitMap = this._ensureSubunitMap(unitId, sheetId);
+ if (!subunitMap.has(feature)) {
+ subunitMap.set(feature, []);
+ }
+ return subunitMap.get(feature)!;
+ }
+
+ public addExclusiveRange(unitId: string, sheetId: string, feature: string, ranges: IFeatureRange[]) {
+ const featureMap = this._ensureFeature(unitId, sheetId, feature);
+ featureMap.push(...ranges);
+ }
+
+ public getExclusiveRanges(unitId: string, sheetId: string, feature: string): undefined | IFeatureRange[] {
+ return this._exclusiveRanges.get(unitId)?.get(sheetId)?.get(feature);
+ }
+
+ public clearExclusiveRanges(unitId: string, sheetId: string, feature: string) {
+ this._ensureFeature(unitId, sheetId, feature);
+ this._exclusiveRanges.get(unitId)!.get(sheetId)!.set(feature, []);
+ }
+
+ public clearExclusiveRangesByGroupId(unitId: string, sheetId: string, feature: string, groupId: string) {
+ const featureMap = this.getExclusiveRanges(unitId, sheetId, feature);
+ if (featureMap) {
+ const newFeatureMap = featureMap.filter((item) => item.groupId !== groupId);
+ this._exclusiveRanges.get(unitId)!.get(sheetId)!.set(feature, newFeatureMap);
+ }
+ }
+
+ public getInterestGroupId(selections: ISelectionWithStyle[]): string[] {
+ const interestGroupId: string[] = [];
+ for (const unitId of this._exclusiveRanges.keys()) {
+ for (const sheetId of this._exclusiveRanges.get(unitId)!.keys()) {
+ for (const feature of this._exclusiveRanges.get(unitId)!.get(sheetId)!.keys()) {
+ const featureMapRanges = this.getExclusiveRanges(unitId, sheetId, feature);
+ if (featureMapRanges) {
+ for (const featureMapRange of featureMapRanges) {
+ let isIntersect = false;
+ for (const selection of selections) {
+ if (Rectangle.intersects(selection.range, featureMapRange.range)) {
+ interestGroupId.push(feature);
+ isIntersect = true;
+ break;
+ }
+ }
+ if (isIntersect) {
+ break;
+ }
+ }
+ }
+ }
+ }
+ }
+ return interestGroupId;
+ }
+}
diff --git a/packages/sheets/src/sheets-plugin.ts b/packages/sheets/src/sheets-plugin.ts
index 237286748e..c3c59fc4bc 100644
--- a/packages/sheets/src/sheets-plugin.ts
+++ b/packages/sheets/src/sheets-plugin.ts
@@ -38,6 +38,7 @@ import { RangeProtectionService } from './services/permission/range-permission/r
import { ONLY_REGISTER_FORMULA_RELATED_MUTATIONS_KEY } from './controllers/config';
import { NumberCellDisplayController } from './controllers/number-cell.controller';
import { SheetsSelectionsService } from './services/selections/selection-manager.service';
+import { ExclusiveRangeService, IExclusiveRangeService } from './services/exclusive-range/exclusive-range-service';
const PLUGIN_NAME = 'SHEET_PLUGIN';
@@ -103,6 +104,10 @@ export class UniverSheetsPlugin extends Plugin {
[RangeProtectionRuleModel],
[RangeProtectionRefRangeService],
[RangeProtectionService],
+ [IExclusiveRangeService, {
+ useClass: ExclusiveRangeService,
+ deps: [SheetsSelectionsService],
+ }],
];
if (!this._config?.notExecuteFormula) {
@@ -115,5 +120,6 @@ export class UniverSheetsPlugin extends Plugin {
this._injector.get(SheetInterceptorService);
this._injector.get(RangeProtectionService);
+ this._injector.get(IExclusiveRangeService);
}
}