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: paste should clear target cell value #4268

Merged
merged 2 commits into from
Dec 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,9 @@ import {
InsertColMutation,
InsertRowMutation,
MAX_CELL_PER_SHEET_KEY,
MoveColsMutation,
MoveRangeMutation,
MoveRowsMutation,
RemoveColMutation,
RemoveRowMutation,
SetRangeValuesMutation,
Expand Down Expand Up @@ -109,6 +112,17 @@ import {
* This controller add basic clipboard logic for basic features such as text color / BISU / row widths to the clipboard
* service. You can create a similar clipboard controller to add logic for your own features.
*/

const shouldRemoveShapeIds = [
InsertColMutation.id,
InsertRowMutation.id,
RemoveColMutation.id,
RemoveRowMutation.id,
MoveRangeMutation.id,
MoveRowsMutation.id,
MoveColsMutation.id,
];

export class SheetClipboardController extends RxDisposable {
constructor(
@Inject(Injector) private readonly _injector: Injector,
Expand Down Expand Up @@ -873,6 +887,8 @@ export class SheetClipboardController extends RxDisposable {
this._commandService.onCommandExecuted((command: ICommandInfo) => {
if (command.id === AddWorksheetMergeCommand.id) {
this._sheetClipboardService.removeMarkSelection();
} else if (shouldRemoveShapeIds.includes(command.id)) {
this._sheetClipboardService.removeMarkSelection();
}
})
);
Expand Down
52 changes: 51 additions & 1 deletion packages/sheets-ui/src/controllers/clipboard/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,11 @@ export function getDefaultOnPasteCellMutations(
redoMutationsInfo.push(...clearStyleRedos);
undoMutationsInfo.push(...clearStyleUndos);

// clear value
const { undos: clearValueUndos, redos: clearValueRedos } = getClearCellValueMutations(pasteTo, data, accessor);
redoMutationsInfo.push(...clearValueRedos);
undoMutationsInfo.push(...clearValueUndos);

// set values
const { undos: setValuesUndos, redos: setValuesRedos } = getSetCellValueMutations(pasteTo, pasteFrom, data, accessor);
redoMutationsInfo.push(...setValuesRedos);
Expand Down Expand Up @@ -309,7 +314,11 @@ export function getSetCellValueMutations(
const { row: realRow, col: realCol } = mapFunc(row, col);

if (value.p?.body) {
valueMatrix.setValue(realRow, realCol, Tools.deepClone({ p: value.p, v: originNumberValue ?? value.v }));
const newValue = Tools.deepClone({ p: value.p, v: originNumberValue ?? value.v });
if (newValue.p.body?.textRuns) {
newValue.p.body.textRuns = [];
}
valueMatrix.setValue(realRow, realCol, newValue);
} else {
valueMatrix.setValue(realRow, realCol, Tools.deepClone({ v: originNumberValue ?? value.v, t: value.t }));
}
Expand Down Expand Up @@ -506,6 +515,47 @@ export function getClearCellStyleMutations(
return { undos: undoMutationsInfo, redos: redoMutationsInfo };
}

export function getClearCellValueMutations(
pasteTo: ISheetDiscreteRangeLocation,
matrix: ObjectMatrix<ICellDataWithSpanInfo>,
accessor: IAccessor
) {
const redoMutationsInfo: IMutationInfo[] = [];
const undoMutationsInfo: IMutationInfo[] = [];
const clearValueMatrix = new ObjectMatrix<ICellData>();
const { unitId, subUnitId, range } = pasteTo;
const { mapFunc } = virtualizeDiscreteRanges([range]);

matrix.forValue((row, col, _value) => {
const { row: actualRow, col: actualCol } = mapFunc(row, col);
clearValueMatrix.setValue(actualRow, actualCol, { v: null, p: null });
});
if (clearValueMatrix.getLength() > 0) {
const clearMutation: ISetRangeValuesMutationParams = {
subUnitId,
unitId,
cellValue: Tools.deepClone(clearValueMatrix.getMatrix()),
};
redoMutationsInfo.push({
id: SetRangeValuesMutation.id,
params: clearMutation,
});

// undo
const undoClearMutation: ISetRangeValuesMutationParams = SetRangeValuesUndoMutationFactory(
accessor,
clearMutation
);

undoMutationsInfo.push({
id: SetRangeValuesMutation.id,
params: undoClearMutation,
});
}

return { undos: undoMutationsInfo, redos: redoMutationsInfo };
}

export function getClearAndSetMergeMutations(
pasteTo: ISheetDiscreteRangeLocation,
matrix: ObjectMatrix<ICellDataWithSpanInfo>,
Expand Down
11 changes: 8 additions & 3 deletions packages/sheets-ui/src/services/clipboard/clipboard.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -667,7 +667,7 @@ export class SheetClipboardService extends Disposable implements ISheetClipboard
});

// setting the selection should be done separately, regardless of the pasting type.
const setSelectionOperation = this._getSetSelectionOperation(unitId, subUnitId, pastedRange, cellMatrix);
const setSelectionOperation = this._getSetSelectionOperation(unitId, subUnitId, pastedRange, cellMatrix, pasteType);
if (setSelectionOperation) {
redoMutationsInfo.push(setSelectionOperation);
}
Expand Down Expand Up @@ -698,7 +698,8 @@ export class SheetClipboardService extends Disposable implements ISheetClipboard
unitId: string,
subUnitId: string,
range: IDiscreteRange,
cellMatrix: ObjectMatrix<ICellDataWithSpanInfo>
cellMatrix: ObjectMatrix<ICellDataWithSpanInfo>,
pasteType?: string
) {
const worksheet = this._univerInstanceService.getUniverSheetInstance(unitId)?.getSheetBySheetId(subUnitId);
if (!worksheet) {
Expand All @@ -723,7 +724,11 @@ export class SheetClipboardService extends Disposable implements ISheetClipboard
const rowSpan = mainCell?.rowSpan || 1;
const colSpan = mainCell?.colSpan || 1;

if (rowSpan > 1 || colSpan > 1) {
const shouldExpandPrimary = pasteType === PREDEFINED_HOOK_NAME.DEFAULT_PASTE
|| pasteType === PREDEFINED_HOOK_NAME.SPECIAL_PASTE_BESIDES_BORDER
|| pasteType === PREDEFINED_HOOK_NAME.SPECIAL_PASTE_FORMAT;

if (shouldExpandPrimary && (rowSpan > 1 || colSpan > 1)) {
const mergeRange = {
startRow,
endRow: startRow + rowSpan - 1,
Expand Down
Loading