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.

fix(sheet): fix toolbar state when there's overlapping selection (#1521)

ybzky1ebfe1a7befe
diff --git a/packages/sheets-ui/src/controllers/menu/merge.menu.ts b/packages/sheets-ui/src/controllers/menu/merge.menu.ts
index e9b6258282..24b4d5fa7a 100644
--- a/packages/sheets-ui/src/controllers/menu/merge.menu.ts
+++ b/packages/sheets-ui/src/controllers/menu/merge.menu.ts
@@ -20,15 +20,18 @@ import type { IMenuButtonItem, IMenuSelectorItem } from '@univerjs/ui';
import { getMenuHiddenObservable, MenuGroup, MenuItemType, MenuPosition } from '@univerjs/ui';
import type { IAccessor } from '@wendellhu/redi';
+import { combineLatestWith, map } from 'rxjs';
import {
AddWorksheetMergeAllCommand,
AddWorksheetMergeCommand,
AddWorksheetMergeHorizontalCommand,
AddWorksheetMergeVerticalCommand,
} from '../../commands/commands/add-worksheet-merge.command';
+import { getSheetSelectionsDisabled$ } from '../utils/selections-tools';
export function CellMergeMenuItemFactory(accessor: IAccessor): IMenuSelectorItem<string> {
const disabled$ = getCurrentSheetDisabled$(accessor);
+ const selectionsHasCross$ = getSheetSelectionsDisabled$(accessor);
return {
id: AddWorksheetMergeCommand.id,
@@ -39,7 +42,10 @@ export function CellMergeMenuItemFactory(accessor: IAccessor): IMenuSelectorItem
type: MenuItemType.SUBITEMS,
// selections: [...MERGE_CHILDREN],
hidden$: getMenuHiddenObservable(accessor, UniverInstanceType.SHEET),
- disabled$,
+ disabled$: disabled$.pipe(
+ combineLatestWith(selectionsHasCross$),
+ map(([disable, hasCross]) => disable || hasCross)
+ ),
};
}
export function CellMergeAllMenuItemFactory(accessor: IAccessor): IMenuButtonItem<string> {
diff --git a/packages/sheets-ui/src/controllers/utils/selections-tools.ts b/packages/sheets-ui/src/controllers/utils/selections-tools.ts
new file mode 100644
index 0000000000..d290cdfb82
--- /dev/null
+++ b/packages/sheets-ui/src/controllers/utils/selections-tools.ts
@@ -0,0 +1,39 @@
+/**
+ * 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 { Rectangle } from '@univerjs/core';
+import { SelectionManagerService } from '@univerjs/sheets';
+import type { IAccessor } from '@wendellhu/redi';
+import { filter, map } from 'rxjs';
+
+export function getSheetSelectionsDisabled$(accessor: IAccessor) {
+ const selectionManagerService = accessor.get(SelectionManagerService);
+
+ return selectionManagerService.selectionMoveEnd$.pipe(
+ filter((param) => param != null && param.length >= 2),
+ map((param) => {
+ if (!param) return false;
+ for (let i = 0; i < param.length; i++) {
+ for (let j = i + 1; j < param.length; j++) {
+ if (Rectangle.intersects(param[i].range, param[j].range)) {
+ return true;
+ }
+ }
+ }
+ return false;
+ })
+ );
+}