Skip to content

Commit

Permalink
fix(sheet): fix error edit position after merging (#1520)
Browse files Browse the repository at this point in the history
Co-authored-by: yuanbin <ybzkyfafa@gamil.com>
  • Loading branch information
ybzky and yuanbin committed Mar 19, 2024
1 parent fbf87b6 commit 8685e14
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 1 deletion.
Expand Up @@ -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';

Expand Down Expand Up @@ -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({
Expand Down
103 changes: 103 additions & 0 deletions 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;
};
3 changes: 2 additions & 1 deletion packages/ui/src/services/layout/layout.service.ts
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
Expand Down

0 comments on commit 8685e14

Please sign in to comment.