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 error edit position after merging (#1520)

panghu8685e1490467
diff --git a/packages/sheets-ui/src/commands/commands/add-worksheet-merge.command.ts b/packages/sheets-ui/src/commands/commands/add-worksheet-merge.command.ts
index 7ccd525c91..f2c5acd190 100644
--- a/packages/sheets-ui/src/commands/commands/add-worksheet-merge.command.ts
+++ b/packages/sheets-ui/src/commands/commands/add-worksheet-merge.command.ts
@@ -36,6 +36,7 @@ import {
import { IConfirmService } from '@univerjs/ui';
import type { IAccessor } from '@wendellhu/redi';
+import { AddMergeRedoSelectionsOperationFactory, AddMergeUndoSelectionsOperationFactory } from '@univerjs/sheets/commands/utils/handle-merge-operation.js';
import { checkCellContentInRanges, getClearContentMutationParamsForRanges } from '../../common/utils';
import { getMergeableSelectionsByType, MergeType } from './utils/selection-utils';
@@ -110,6 +111,12 @@ export const AddWorksheetMergeCommand: ICommand = {
undoMutations.push(...data.undos);
}
+ const addMergeRedoSelectionsMutation = AddMergeRedoSelectionsOperationFactory(accessor, params, ranges);
+ addMergeRedoSelectionsMutation && redoMutations.push(addMergeRedoSelectionsMutation);
+
+ const addMergeUndoSelectionsMutation = AddMergeUndoSelectionsOperationFactory(accessor, params);
+ addMergeUndoSelectionsMutation && undoMutations.push(addMergeUndoSelectionsMutation);
+
const result = sequenceExecute(redoMutations, commandService);
if (result.result) {
undoRedoService.pushUndoRedo({
diff --git a/packages/sheets/src/commands/utils/handle-merge-operation.ts b/packages/sheets/src/commands/utils/handle-merge-operation.ts
new file mode 100644
index 0000000000..3c10353148
--- /dev/null
+++ b/packages/sheets/src/commands/utils/handle-merge-operation.ts
@@ -0,0 +1,103 @@
+/**
+ * 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 { IAccessor } from '@wendellhu/redi';
+import type { IRange } from '@univerjs/core';
+import { Dimension } from '@univerjs/core';
+import type { IAddMergeCommandParams } from '../commands/add-worksheet-merge.command';
+import type { ISetSelectionsOperationParams } from '../..';
+import { NORMAL_SELECTION_PLUGIN_NAME, SelectionManagerService, SetSelectionsOperation } from '../..';
+
+export const AddMergeRedoSelectionsOperationFactory = (accessor: IAccessor, params: IAddMergeCommandParams, ranges: IRange[]) => {
+ const selectionManagerService = accessor.get(SelectionManagerService);
+ const selectionsBeforeMutation = selectionManagerService.getSelections();
+ const { value, selections, unitId, subUnitId } = params;
+ if (selectionsBeforeMutation) {
+ const lastSelectionBeforeMutation = selectionsBeforeMutation[selectionsBeforeMutation?.length - 1];
+ const primaryBeforeMutation = lastSelectionBeforeMutation.primary;
+ if (primaryBeforeMutation) {
+ const { actualColumn, actualRow } = primaryBeforeMutation;
+ let { startRow, startColumn, endRow, endColumn } = selections[selections.length - 1];
+ if (value === Dimension.COLUMNS) {
+ const rangeByColumn = ranges.find((item) => item.startColumn === actualColumn && item.endColumn === actualColumn && actualRow === item.startRow);
+ if (rangeByColumn) {
+ endColumn = rangeByColumn.endColumn;
+ startRow = rangeByColumn.startRow;
+ endRow = rangeByColumn.endRow;
+ }
+ } else if (value === Dimension.ROWS) {
+ const rangeByRow = ranges.find((item) => item.startRow === actualRow && item.endRow === actualRow && actualColumn === item.startColumn);
+ if (rangeByRow) {
+ endRow = rangeByRow.endRow;
+ startColumn = rangeByRow.startColumn;
+ endColumn = rangeByRow.endColumn;
+ }
+ }
+ const primary = {
+ startRow,
+ startColumn,
+ endRow,
+ endColumn,
+ actualRow,
+ actualColumn,
+ isMerged: true,
+ isMergedMainCell: startRow === actualRow && startColumn === actualColumn,
+ };
+ const selectionsByRedo = selectionsBeforeMutation.map((selection, index, selections) => {
+ return {
+ range: selection.range,
+ style: null,
+ primary: index === selections.length - 1 ? primary : null,
+ };
+ });
+ const setSelectionsParamByRedo: ISetSelectionsOperationParams = {
+ unitId,
+ subUnitId,
+ pluginName: NORMAL_SELECTION_PLUGIN_NAME,
+ selections: selectionsByRedo,
+ };
+ return {
+ id: SetSelectionsOperation.id,
+ params: setSelectionsParamByRedo,
+ };
+ }
+ return null;
+ }
+ return null;
+};
+
+export const AddMergeUndoSelectionsOperationFactory = (accessor: IAccessor, params: IAddMergeCommandParams) => {
+ const selectionManagerService = accessor.get(SelectionManagerService);
+ const selectionsBeforeMutation = selectionManagerService.getSelections();
+ const { unitId, subUnitId } = params;
+ if (selectionsBeforeMutation) {
+ const lastSelectionBeforeMutation = selectionsBeforeMutation[selectionsBeforeMutation?.length - 1];
+ const primaryBeforeMutation = lastSelectionBeforeMutation.primary;
+ if (primaryBeforeMutation) {
+ const setSelectionsParamByUndo: ISetSelectionsOperationParams = {
+ unitId,
+ subUnitId,
+ pluginName: NORMAL_SELECTION_PLUGIN_NAME,
+ selections: [...selectionsBeforeMutation],
+ };
+ return {
+ id: SetSelectionsOperation.id,
+ params: setSelectionsParamByUndo,
+ };
+ }
+ }
+ return null;
+};
diff --git a/packages/ui/src/services/layout/layout.service.ts b/packages/ui/src/services/layout/layout.service.ts
index eaa9cadb60..35ecbebf5d 100644
--- a/packages/ui/src/services/layout/layout.service.ts
+++ b/packages/ui/src/services/layout/layout.service.ts
@@ -22,6 +22,7 @@ import { fromEvent } from 'rxjs';
type FocusHandlerFn = (unitId: string) => void;
export const FOCUSING_UNIVER = 'FOCUSING_UNIVER';
+const collectionOfCnForFocusableEle = ['univer-app-layout', 'univer-toolbar-btn', 'univer-menu-item', 'univer-button'];
export interface ILayoutService {
readonly isFocused: boolean;
@@ -145,7 +146,7 @@ export class DesktopLayoutService extends Disposable implements ILayoutService {
this.disposeWithMe(
fromEvent(window, 'focusin').subscribe((event) => {
const target = event.target as HTMLElement;
- if (target.classList.contains('univer-app-layout')) {
+ if (collectionOfCnForFocusableEle.some((item) => target.classList.contains(item))) {
queueMicrotask(() => this.focus());
return;
}