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: problems when do copy-paste in numfmt & formula (#635)

YuHongb04dc4b6e5c3
diff --git a/packages/sheets-ui/src/services/clipboard/clipboard.service.ts b/packages/sheets-ui/src/services/clipboard/clipboard.service.ts
index aa9be32035..7f166b00f3 100644
--- a/packages/sheets-ui/src/services/clipboard/clipboard.service.ts
+++ b/packages/sheets-ui/src/services/clipboard/clipboard.service.ts
@@ -1,17 +1,11 @@
-import type {
- ICellData,
- IMutationInfo,
- IRange,
- ObjectMatrix,
- ObjectMatrixPrimitiveType,
- Worksheet,
-} from '@univerjs/core';
+import type { ICellData, IMutationInfo, IRange, ObjectMatrixPrimitiveType, Worksheet } from '@univerjs/core';
import {
Disposable,
ICommandService,
ILogService,
IUndoRedoService,
IUniverInstanceService,
+ ObjectMatrix,
Rectangle,
toDisposable,
Tools,
@@ -112,7 +106,15 @@ export class SheetClipboardService extends Disposable implements ISheetClipboard
const workbook = this._currentUniverService.getCurrentUniverSheetInstance();
const worksheet = workbook.getActiveSheet();
const matrix = worksheet.getMatrixWithMergedCells(startRow, startColumn, endRow, endColumn);
- const matrixFragment = matrix.getFragments(startRow, endRow, startColumn, endColumn);
+ const matrixFragment = new ObjectMatrix<ICellDataWithSpanInfo>();
+ for (let r = startRow; r <= endRow; r++) {
+ for (let c = startColumn; c <= endColumn; c++) {
+ const cellData = matrix.getValue(r, c);
+ if (cellData) {
+ matrixFragment.setValue(r - startRow, c - startColumn, Tools.deepClone(cellData));
+ }
+ }
+ }
// 4. use filteredRows into to remove rows for the matrix
// TODO: filtering
@@ -142,6 +144,9 @@ export class SheetClipboardService extends Disposable implements ISheetClipboard
// 9. mark the copy range
const style = this._selectionManagerService.createCopyPasteSelection();
+ if (this._copyMarkId) {
+ this._markSelectionService.removeShape(this._copyMarkId);
+ }
this._copyMarkId = this._markSelectionService.addShape({ ...selection, style });
// tell hooks to clean up
@@ -185,7 +190,15 @@ export class SheetClipboardService extends Disposable implements ISheetClipboard
this._logService.error('[SheetClipboardService]', 'hook already exists', hook.hookName);
return { dispose: () => {} };
}
- this._clipboardHooks.push(hook);
+ // hook added should be ordered at meaning while
+ const insertIndex = this._clipboardHooks.findIndex((existingHook) => {
+ const existingHookPriority = existingHook.priority || 0;
+ const hookPriority = hook.priority || 0;
+ return hookPriority < existingHookPriority;
+ });
+
+ this._clipboardHooks.splice(insertIndex !== -1 ? insertIndex : this._clipboardHooks.length, 0, hook);
+
this._notifyClipboardHook();
return toDisposable(() => {
const index = this._clipboardHooks.indexOf(hook);
@@ -319,7 +332,7 @@ export class SheetClipboardService extends Disposable implements ISheetClipboard
}
});
- const { startColumn, endColumn, startRow, endRow } = cellMatrix.getDataRange();
+ const { startColumn, endColumn, startRow, endRow } = range;
const pastedRange = this._transformPastedData(
endRow - startRow + 1,
endColumn - startColumn + 1,
@@ -379,7 +392,14 @@ export class SheetClipboardService extends Disposable implements ISheetClipboard
}
if (!cellMatrix) return false;
- const copyInfo = source ? { copyRange: source.range, copyType: source.copyType } : { copyType: COPY_TYPE.COPY };
+ const copyInfo = source
+ ? {
+ copyRange: source.range,
+ copyType: source.copyType,
+ workbookId: source.workbookId,
+ worksheetId: source.worksheetId,
+ }
+ : { copyType: COPY_TYPE.COPY };
const redoMutationsInfo: IMutationInfo[] = [];
const undoMutationsInfo: IMutationInfo[] = [];
@@ -388,7 +408,8 @@ export class SheetClipboardService extends Disposable implements ISheetClipboard
// other hooks will be executed only when the paste type is the same as the hook name, including the default one
const filteredHooks: ISheetClipboardHook[] = hooks.filter(
(h) =>
- (!h.specialPasteInfo && h.hookName !== PREDEFINED_HOOK_NAME.DEFAULT_PASTE) || h.hookName === pasteType
+ (h.specialPasteInfo && pasteType === h.hookName) ||
+ (!h.specialPasteInfo && pasteType === PREDEFINED_HOOK_NAME.DEFAULT_PASTE)
);
filteredHooks.forEach((h) => {
if (rowProperties) {
diff --git a/packages/sheets-ui/src/services/clipboard/type.ts b/packages/sheets-ui/src/services/clipboard/type.ts
index c9e8747d59..592c9b1afc 100644
--- a/packages/sheets-ui/src/services/clipboard/type.ts
+++ b/packages/sheets-ui/src/services/clipboard/type.ts
@@ -114,6 +114,8 @@ export interface ISheetClipboardHook {
copyInfo: {
copyType: COPY_TYPE;
copyRange?: IRange;
+ worksheetId?: string;
+ workbookId?: string;
}
): {
undos: IMutationInfo[];
diff --git a/packages/sheets-ui/src/services/mark-selection/mark-selection.service.ts b/packages/sheets-ui/src/services/mark-selection/mark-selection.service.ts
index 9d9cecda10..530cac73e9 100644
--- a/packages/sheets-ui/src/services/mark-selection/mark-selection.service.ts
+++ b/packages/sheets-ui/src/services/mark-selection/mark-selection.service.ts
@@ -2,7 +2,7 @@ import type { ICommandInfo } from '@univerjs/core';
import { Disposable, ICommandService, IUniverInstanceService, ThemeService, Tools } from '@univerjs/core';
import { IRenderManagerService } from '@univerjs/engine-render';
import type { ISelectionWithStyle } from '@univerjs/sheets';
-import { SelectionManagerService } from '@univerjs/sheets';
+import { SelectionManagerService, SetWorksheetActivateMutation } from '@univerjs/sheets';
import { createIdentifier, Inject } from '@wendellhu/redi';
import { SetCellEditVisibleOperation } from '../../commands/operations/cell-edit.operation';
@@ -16,11 +16,19 @@ export interface IMarkSelectionService {
removeAllShapes(): void;
}
+interface IMarkSelectionInfo {
+ workbookId: string;
+ worksheetId: string;
+ selection: ISelectionWithStyle;
+ zIndex: number;
+ control: SelectionShape | null;
+}
+
const DEFAULT_Z_INDEX = 10000;
export const IMarkSelectionService = createIdentifier<IMarkSelectionService>('univer.mark-selection-service');
export class MarkSelectionService extends Disposable implements IMarkSelectionService {
- private _shapeMap: Map<string, SelectionShape> = new Map();
+ private _shapeMap: Map<string, IMarkSelectionInfo> = new Map();
constructor(
@IUniverInstanceService private readonly _currentService: IUniverInstanceService,
@@ -33,37 +41,60 @@ export class MarkSelectionService extends Disposable implements IMarkSelectionSe
) {
super();
this._addRemoveListener();
+ this._addRefreshListener();
}
addShape(selection: ISelectionWithStyle, zIndex: number = DEFAULT_Z_INDEX): string | null {
const workbook = this._currentService.getCurrentUniverSheetInstance();
- const { style } = selection;
- const { scene } = this._renderManagerService.getRenderById(workbook.getUnitId()) || {};
-
- const { rangeWithCoord, primaryWithCoord } =
- this._selectionRenderService.convertSelectionRangeToData(selection);
- const skeleton = this._sheetSkeletonManagerService.getCurrent()?.skeleton;
- if (!scene || !skeleton) return null;
- const { rowHeaderWidth, columnHeaderHeight } = skeleton;
-
- const control = new SelectionShape(scene, zIndex, false, this._themeService);
-
- control.update(rangeWithCoord, rowHeaderWidth, columnHeaderHeight, style, primaryWithCoord);
+ const worksheetId = workbook.getActiveSheet().getSheetId();
const id = Tools.generateRandomId();
- this._shapeMap.set(id, control);
+ this._shapeMap.set(id, {
+ selection,
+ worksheetId,
+ workbookId: workbook.getUnitId(),
+ zIndex,
+ control: null,
+ });
+ this.refreshShapes();
return id;
}
+ refreshShapes() {
+ const currentWorkbookId = this._currentService.getCurrentUniverSheetInstance().getUnitId();
+ const currentWorksheetId = this._currentService.getCurrentUniverSheetInstance().getActiveSheet().getSheetId();
+ setTimeout(() => {
+ this._shapeMap.forEach((shape) => {
+ const { workbookId, worksheetId, selection, control: oldControl, zIndex } = shape;
+ if (workbookId !== currentWorkbookId || worksheetId !== currentWorksheetId) {
+ oldControl && oldControl.dispose();
+ return;
+ }
+ const { style } = selection;
+ const { scene } = this._renderManagerService.getRenderById(workbookId) || {};
+ const { rangeWithCoord, primaryWithCoord } =
+ this._selectionRenderService.convertSelectionRangeToData(selection);
+ const skeleton = this._sheetSkeletonManagerService.getCurrent()?.skeleton;
+ if (!scene || !skeleton) return;
+ const { rowHeaderWidth, columnHeaderHeight } = skeleton;
+ const control = new SelectionShape(scene, zIndex, false, this._themeService);
+ control.update(rangeWithCoord, rowHeaderWidth, columnHeaderHeight, style, primaryWithCoord);
+ shape.control = control;
+ });
+ }, 0);
+ }
+
removeShape(id: string): void {
- const shape = this._shapeMap.get(id);
- if (!shape) return;
- shape.dispose();
+ const shapeInfo = this._shapeMap.get(id);
+ if (!shapeInfo) return;
+ const { control } = shapeInfo;
+ control && control.dispose();
this._shapeMap.delete(id);
}
removeAllShapes(): void {
for (const shape of this._shapeMap.values()) {
- shape.dispose();
+ const { control } = shape;
+ control && control.dispose();
}
this._shapeMap.clear();
}
@@ -78,4 +109,15 @@ export class MarkSelectionService extends Disposable implements IMarkSelectionSe
})
);
}
+
+ private _addRefreshListener() {
+ const refreshCommands = [SetWorksheetActivateMutation.id];
+ this.disposeWithMe(
+ this._commandService.onCommandExecuted((command: ICommandInfo) => {
+ if (refreshCommands.includes(command.id)) {
+ this.refreshShapes();
+ }
+ })
+ );
+ }
}