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.
TypeScript boardcollaborationdocdocxexceloffce365officepdfpptpptxpresentationsdksheetslidesspreadsheetuniverwordwordprocessorworkspacexlsx
feat(sheet): numfmt support cut/paste (#645)
Gpound.liuf214f38e6c43
diff --git a/packages/sheets-numfmt/src/controllers/numfmt.copy-paste.controller.ts b/packages/sheets-numfmt/src/controllers/numfmt.copy-paste.controller.tsnew file mode 100644index 0000000000..5ee3cb3337--- /dev/null+++ b/packages/sheets-numfmt/src/controllers/numfmt.copy-paste.controller.ts@@ -0,0 +1,149 @@+import type { IRange, Nullable } from '@univerjs/core';+import {+ Disposable,+ IUniverInstanceService,+ LifecycleStages,+ ObjectMatrix,+ OnLifecycle,+ Range,+ Rectangle,+} from '@univerjs/core';+import type { FormatType, ISetNumfmtMutationParams } from '@univerjs/sheets';+import { factorySetNumfmtUndoMutation, INumfmtService, SetNumfmtMutation } from '@univerjs/sheets';+import { COPY_TYPE, ISheetClipboardService } from '@univerjs/sheets-ui';+import { Inject, Injector } from '@wendellhu/redi';++@OnLifecycle(LifecycleStages.Rendered, NumfmtCopyPasteController)+export class NumfmtCopyPasteController extends Disposable {+ private _copyInfo: Nullable<{+ matrix: ObjectMatrix<{ pattern: string; type: FormatType }>;+ info: {+ workbookId: string;+ worksheetId: string;+ };+ }>;++ constructor(+ @Inject(ISheetClipboardService) private _sheetClipboardService: ISheetClipboardService,+ @Inject(Injector) private _injector: Injector,+ @Inject(INumfmtService) private _numfmtService: INumfmtService,+ @Inject(IUniverInstanceService) private _univerInstanceService: IUniverInstanceService+ ) {+ super();+ this._initClipboardHook();+ }++ private _initClipboardHook() {+ this.disposeWithMe(+ this._sheetClipboardService.addClipboardHook({+ hookName: 'numfmt',+ onBeforeCopy: (workbookId, worksheetId, range) => this._collectNumfmt(workbookId, worksheetId, range),+ onPasteCells: (pastedRange, _m, _p, _copyInfo) => this._generateNumfmtMutations(pastedRange, _copyInfo),+ })+ );+ }++ private _collectNumfmt(workbookId: string, worksheetId: string, range: IRange) {+ const matrix = new ObjectMatrix<{ pattern: string; type: FormatType }>();+ this._copyInfo = {+ matrix,+ info: {+ workbookId,+ worksheetId,+ },+ };+ const model = this._numfmtService.getModel(workbookId, worksheetId);+ if (!model) {+ return;+ }+ Range.foreach(range, (row, col) => {+ const numfmtValue = this._numfmtService.getValue(workbookId, worksheetId, row, col, model);+ if (!numfmtValue) {+ return;+ }+ const relativeRange = Rectangle.getRelativeRange(+ {+ startRow: row,+ endRow: row,+ startColumn: col,+ endColumn: col,+ },+ range+ );+ matrix.setValue(relativeRange.startRow, relativeRange.startColumn, {+ pattern: numfmtValue.pattern,+ type: numfmtValue.type,+ });+ });+ }++ private _generateNumfmtMutations(+ pastedRange: IRange,+ copyInfo: {+ copyType: COPY_TYPE;+ copyRange?: IRange;+ }+ ) {+ const workbook = this._univerInstanceService.getCurrentUniverSheetInstance();+ const sheet = workbook.getActiveSheet();+ const workbookId = workbook.getUnitId();+ const worksheetId = sheet.getSheetId();++ if (!this._copyInfo || !this._copyInfo.matrix.getSizeOf()) {+ return { redos: [], undos: [] };+ }++ const redos: ISetNumfmtMutationParams = { workbookId, worksheetId, values: [] };++ // Clears the destination area data format+ Range.foreach(pastedRange, (row, col) => {+ redos.values.push({ row, col });+ });++ // Set up according to the data collected. This will overlap with the cleanup, but that's okay+ this._copyInfo.matrix.forValue((row, col, value) => {+ const range = Rectangle.getPositionRange(+ {+ startRow: row,+ endRow: row,+ startColumn: col,+ endColumn: col,+ },+ pastedRange+ );+ redos.values.push({+ row: range.startRow,+ col: range.startColumn,+ pattern: value.pattern,+ type: value.type,+ });+ });++ // If is clipping, need to clear the data format of the original area+ if (copyInfo.copyType === COPY_TYPE.CUT && copyInfo.copyRange) {+ this._copyInfo.matrix.forValue((row, col) => {+ const range = Rectangle.getPositionRange(+ {+ startRow: row,+ endRow: row,+ startColumn: col,+ endColumn: col,+ },+ copyInfo.copyRange!+ );+ redos.values.unshift({+ row: range.startRow,+ col: range.startColumn,+ });+ });+ this._copyInfo = null;+ }++ const undos: ISetNumfmtMutationParams = factorySetNumfmtUndoMutation(this._injector, redos);++ return {+ redos: [{ id: SetNumfmtMutation.id, params: redos }],+ undos: [{ id: SetNumfmtMutation.id, params: undos }],+ };+ }+}diff --git a/packages/sheets-numfmt/src/numfmt-plugin.ts b/packages/sheets-numfmt/src/numfmt-plugin.tsindex 50448415e5..3ccbb7628f 100644--- a/packages/sheets-numfmt/src/numfmt-plugin.ts+++ b/packages/sheets-numfmt/src/numfmt-plugin.ts@@ -3,6 +3,7 @@ import { Inject, Injector } from '@wendellhu/redi';import { SHEET_NUMFMT_PLUGIN } from './base/const/PLUGIN_NAME';import { NumfmtController } from './controllers/numfmt.controller';+import { NumfmtCopyPasteController } from './controllers/numfmt.copy-paste.controller';import { NumfmtEditorController } from './controllers/numfmt.editor.controller';import { NumfmtRefRangeController } from './controllers/numfmt.ref-range.controller';import { NumfmtSheetController } from './controllers/numfmt.sheet.controller';@@ -21,5 +22,6 @@ export class NumfmtPlugin extends Plugin {this._injector.add([UserHabitController]);this._injector.add([NumfmtRefRangeController]);this._injector.add([NumfmtSheetController]);+ this._injector.add([NumfmtCopyPasteController]);}}diff --git a/packages/sheets-ui/src/index.ts b/packages/sheets-ui/src/index.tsindex 63a73568f2..87520ddb58 100644--- a/packages/sheets-ui/src/index.ts+++ b/packages/sheets-ui/src/index.ts@@ -20,6 +20,8 @@ export { getRepeatRange } from './services/auto-fill/tools';export type { IAutoFillHook, ICopyDataPiece } from './services/auto-fill/type';export { APPLY_TYPE, DATA_TYPE, type IAutoFillRule } from './services/auto-fill/type';export { type ICopyDataInTypeIndexInfo } from './services/auto-fill/type';+export { ISheetClipboardService } from './services/clipboard/clipboard.service';+export { COPY_TYPE } from './services/clipboard/type';export { CellEditorManagerService, ICellEditorManagerService } from './services/editor/cell-editor-manager.service';export { IFormulaEditorManagerService } from './services/editor/formula-editor-manager.service';export {