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

ybzky3babc2fd0c0e
diff --git a/packages/sheets-ui/src/controllers/clipboard/clipboard.controller.ts b/packages/sheets-ui/src/controllers/clipboard/clipboard.controller.ts
index 78936c7675..466a7fc2a5 100644
--- a/packages/sheets-ui/src/controllers/clipboard/clipboard.controller.ts
+++ b/packages/sheets-ui/src/controllers/clipboard/clipboard.controller.ts
@@ -73,6 +73,9 @@ import {
InsertColMutation,
InsertRowMutation,
MAX_CELL_PER_SHEET_KEY,
+ MoveColsMutation,
+ MoveRangeMutation,
+ MoveRowsMutation,
RemoveColMutation,
RemoveRowMutation,
SetRangeValuesMutation,
@@ -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,
@@ -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();
}
})
);
diff --git a/packages/sheets-ui/src/controllers/clipboard/utils.ts b/packages/sheets-ui/src/controllers/clipboard/utils.ts
index 74d39ee35d..a4be6bdfd3 100644
--- a/packages/sheets-ui/src/controllers/clipboard/utils.ts
+++ b/packages/sheets-ui/src/controllers/clipboard/utils.ts
@@ -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);
@@ -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 }));
}
@@ -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>,
diff --git a/packages/sheets-ui/src/services/clipboard/clipboard.service.ts b/packages/sheets-ui/src/services/clipboard/clipboard.service.ts
index ca93a9b030..d0bcbaebca 100644
--- a/packages/sheets-ui/src/services/clipboard/clipboard.service.ts
+++ b/packages/sheets-ui/src/services/clipboard/clipboard.service.ts
@@ -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);
}
@@ -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) {
@@ -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,