Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: bugs in format painter #677

Merged
merged 7 commits into from
Dec 9, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
refactor: remove functions
  • Loading branch information
yuhongz committed Dec 8, 2023
commit c22a2a4c82d26c26c47566077b490ae9540d6d94
Original file line number Diff line number Diff line change
@@ -1,109 +1,34 @@
import type { ICellData, ICommand, IMutationInfo, IRange, Worksheet } from '@univerjs/core';
import type { ICommand, IMutationInfo, IRange } from '@univerjs/core';
import {
CommandType,
Dimension,
ICommandService,
IUndoRedoService,
IUniverInstanceService,
LocaleService,
ObjectMatrix,
sequenceExecute,
} from '@univerjs/core';
import type {
IAddWorksheetMergeMutationParams,
IRemoveWorksheetMergeMutationParams,
ISetRangeValuesMutationParams,
} from '@univerjs/sheets';
import type { IAddWorksheetMergeMutationParams, IRemoveWorksheetMergeMutationParams } from '@univerjs/sheets';
import {
AddMergeUndoMutationFactory,
AddWorksheetMergeMutation,
getAddMergeMutationRangeByType,
RemoveMergeUndoMutationFactory,
RemoveWorksheetMergeMutation,
SelectionManagerService,
SetRangeValuesMutation,
SetRangeValuesUndoMutationFactory,
} from '@univerjs/sheets';
import { IConfirmService } from '@univerjs/ui';
import type { IAccessor } from '@wendellhu/redi';

import { checkCellContentInRanges, getClearContentMutationParamsForRanges } from '../../common/utils';

export interface IAddMergeCommandParams {
value?: Dimension.ROWS | Dimension.COLUMNS;
selections: IRange[];
workbookId: string;
worksheetId: string;
}

export function checkCellContentInRanges(worksheet: Worksheet, ranges: IRange[]): boolean {
return ranges.some((range) => checkCellContentInRange(worksheet, range));
}

export function checkCellContentInRange(worksheet: Worksheet, range: IRange): boolean {
const { startRow, startColumn, endColumn, endRow } = range;
const cellMatrix = worksheet.getMatrixWithMergedCells(startRow, startColumn, endRow, endColumn);

let someCellGoingToBeRemoved = false;
cellMatrix.forValue((row, col, cellData) => {
if (cellData && (row !== startRow || col !== startColumn) && worksheet.cellHasValue(cellData)) {
someCellGoingToBeRemoved = true;
return false;
}
});
return someCellGoingToBeRemoved;
}

export function getClearContentMutationParamsForRanges(
accessor: IAccessor,
workbookId: string,
worksheet: Worksheet,
ranges: IRange[]
): {
undos: IMutationInfo[];
redos: IMutationInfo[];
} {
const undos: IMutationInfo[] = [];
const redos: IMutationInfo[] = [];

const worksheetId = worksheet.getSheetId();

// Use the following file as a reference.
// packages/sheets/src/commands/commands/clear-selection-all.command.ts
// packages/sheets/src/commands/mutations/set-range-values.mutation.ts
ranges.forEach((range) => {
const redoMatrix = getClearContentMutationParamForRange(worksheet, range);
const redoMutationParams: ISetRangeValuesMutationParams = {
workbookId,
worksheetId,
cellValue: redoMatrix.getData(),
};
const undoMutationParams: ISetRangeValuesMutationParams = SetRangeValuesUndoMutationFactory(
accessor,
redoMutationParams
);

undos.push({ id: SetRangeValuesMutation.id, params: undoMutationParams });
redos.push({ id: SetRangeValuesMutation.id, params: redoMutationParams });
});

return {
undos,
redos,
};
}

export function getClearContentMutationParamForRange(worksheet: Worksheet, range: IRange): ObjectMatrix<ICellData> {
const { startRow, startColumn, endColumn, endRow } = range;
const cellMatrix = worksheet.getMatrixWithMergedCells(startRow, startColumn, endRow, endColumn);
const redoMatrix = new ObjectMatrix<ICellData>();
cellMatrix.forValue((row, col, cellData) => {
if (cellData && (row !== startRow || col !== startColumn)) {
redoMatrix.setValue(row, col, null);
}
});

return redoMatrix;
}

export const AddWorksheetMergeCommand: ICommand = {
type: CommandType.COMMAND,
id: 'sheet.command.add-worksheet-merge',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ import {
} from '@univerjs/sheets';
import type { IAccessor } from '@wendellhu/redi';

import { checkCellContentInRanges, getClearContentMutationParamsForRanges } from '../../common/utils';
import { FormatPainterStatus, IFormatPainterService } from '../../services/format-painter/format-painter.service';
import { SetFormatPainterOperation } from '../operations/set-format-painter.operation';
import { checkCellContentInRanges, getClearContentMutationParamsForRanges } from './add-worksheet-merge.command';

export interface ISetFormatPainterCommandParams {
status: FormatPainterStatus;
Expand Down
75 changes: 75 additions & 0 deletions packages/sheets-ui/src/common/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import type { ICellData, IMutationInfo, IRange, Worksheet } from '@univerjs/core';
import { ObjectMatrix } from '@univerjs/core';
import type { ISetRangeValuesMutationParams } from '@univerjs/sheets';
import { SetRangeValuesMutation, SetRangeValuesUndoMutationFactory } from '@univerjs/sheets';
import type { IAccessor } from '@wendellhu/redi';

export function checkCellContentInRanges(worksheet: Worksheet, ranges: IRange[]): boolean {
return ranges.some((range) => checkCellContentInRange(worksheet, range));
}

export function checkCellContentInRange(worksheet: Worksheet, range: IRange): boolean {
const { startRow, startColumn, endColumn, endRow } = range;
const cellMatrix = worksheet.getMatrixWithMergedCells(startRow, startColumn, endRow, endColumn);

let someCellGoingToBeRemoved = false;
cellMatrix.forValue((row, col, cellData) => {
if (cellData && (row !== startRow || col !== startColumn) && worksheet.cellHasValue(cellData)) {
someCellGoingToBeRemoved = true;
return false;
}
});
return someCellGoingToBeRemoved;
}

export function getClearContentMutationParamsForRanges(
accessor: IAccessor,
workbookId: string,
worksheet: Worksheet,
ranges: IRange[]
): {
undos: IMutationInfo[];
redos: IMutationInfo[];
} {
const undos: IMutationInfo[] = [];
const redos: IMutationInfo[] = [];

const worksheetId = worksheet.getSheetId();

// Use the following file as a reference.
// packages/sheets/src/commands/commands/clear-selection-all.command.ts
// packages/sheets/src/commands/mutations/set-range-values.mutation.ts
ranges.forEach((range) => {
const redoMatrix = getClearContentMutationParamForRange(worksheet, range);
const redoMutationParams: ISetRangeValuesMutationParams = {
workbookId,
worksheetId,
cellValue: redoMatrix.getData(),
};
const undoMutationParams: ISetRangeValuesMutationParams = SetRangeValuesUndoMutationFactory(
accessor,
redoMutationParams
);

undos.push({ id: SetRangeValuesMutation.id, params: undoMutationParams });
redos.push({ id: SetRangeValuesMutation.id, params: redoMutationParams });
});

return {
undos,
redos,
};
}

export function getClearContentMutationParamForRange(worksheet: Worksheet, range: IRange): ObjectMatrix<ICellData> {
const { startRow, startColumn, endColumn, endRow } = range;
const cellMatrix = worksheet.getMatrixWithMergedCells(startRow, startColumn, endRow, endColumn);
const redoMatrix = new ObjectMatrix<ICellData>();
cellMatrix.forValue((row, col, cellData) => {
if (cellData && (row !== startRow || col !== startColumn)) {
redoMatrix.setValue(row, col, null);
}
});

return redoMatrix;
}
Loading