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.

feat(format-painter): format pianter to single-cell will copy whole range formats (#3326)

YuHong923b5dc4ed30
diff --git a/packages/sheets-ui/src/commands/commands/__tests__/set-format-painter.command.spec.ts b/packages/sheets-ui/src/commands/commands/__tests__/set-format-painter.command.spec.ts
index 60b3911aba..626cb08620 100644
--- a/packages/sheets-ui/src/commands/commands/__tests__/set-format-painter.command.spec.ts
+++ b/packages/sheets-ui/src/commands/commands/__tests__/set-format-painter.command.spec.ts
@@ -342,5 +342,78 @@ describe('Test format painter rules in controller', () => {
expect(mergeData?.[4].startRow).toBe(13);
});
});
+
+ describe('format painter to single cell', () => {
+ it ('will copy whole original styles', async () => {
+ expect(await commandService.executeCommand(SetSelectionsOperation.id, {
+ unitId: 'workbook-01',
+ subUnitId: 'sheet-0011',
+
+ selections: [
+ {
+ range: {
+ startRow: 0,
+ endRow: 0,
+ startColumn: 0,
+ endColumn: 3,
+ },
+ },
+ ],
+ })).toBeTruthy();
+
+ expect(await commandService.executeCommand(SetOnceFormatPainterCommand.id)).toBeTruthy();
+ expect(await commandService.executeCommand(ApplyFormatPainterCommand.id, {
+ range: {
+ startRow: 5,
+ endRow: 5,
+ startColumn: 0,
+ endColumn: 0,
+ },
+ unitId: 'workbook-01',
+ subUnitId: 'sheet-0011',
+ })).toBeTruthy();
+
+ const workbook = get(IUniverInstanceService).getCurrentUnitForType<Workbook>(UniverInstanceType.UNIVER_SHEET)!;
+ if (!workbook) throw new Error('This is an error');
+ expect(workbook.getSheetBySheetId('sheet-0011')?.getCell(5, 0)?.s).toBe('yifA1t');
+ expect(workbook.getSheetBySheetId('sheet-0011')?.getCell(5, 1)?.s).toBe('M5JbP2');
+ });
+
+ describe('format painter from non-style cell to styled cell', () => {
+ it ('will clear styles', async () => {
+ expect(await commandService.executeCommand(SetSelectionsOperation.id, {
+ unitId: 'workbook-01',
+ subUnitId: 'sheet-0011',
+
+ selections: [
+ {
+ range: {
+ startRow: 6,
+ endRow: 6,
+ startColumn: 0,
+ endColumn: 0,
+ },
+ },
+ ],
+ })).toBeTruthy();
+
+ expect(await commandService.executeCommand(SetOnceFormatPainterCommand.id)).toBeTruthy();
+ expect(await commandService.executeCommand(ApplyFormatPainterCommand.id, {
+ range: {
+ startRow: 0,
+ endRow: 0,
+ startColumn: 0,
+ endColumn: 0,
+ },
+ unitId: 'workbook-01',
+ subUnitId: 'sheet-0011',
+ })).toBeTruthy();
+
+ const workbook = get(IUniverInstanceService).getCurrentUnitForType<Workbook>(UniverInstanceType.UNIVER_SHEET)!;
+ if (!workbook) throw new Error('This is an error');
+ expect(workbook.getSheetBySheetId('sheet-0011')?.getCell(0, 0)?.s).toBe(undefined);
+ });
+ });
+ });
});
});
diff --git a/packages/sheets-ui/src/controllers/format-painter/format-painter.controller.ts b/packages/sheets-ui/src/controllers/format-painter/format-painter.controller.ts
index 3553720513..e492b1b289 100644
--- a/packages/sheets-ui/src/controllers/format-painter/format-painter.controller.ts
+++ b/packages/sheets-ui/src/controllers/format-painter/format-painter.controller.ts
@@ -14,14 +14,13 @@
* limitations under the License.
*/
-import type { ICellData, IMutationInfo, IObjectMatrixPrimitiveType, IRange, IStyleData, Workbook } from '@univerjs/core';
+import type { ICellData, IMutationInfo, IRange, IStyleData, Workbook } from '@univerjs/core';
import {
Disposable,
getCellInfoInMergeData,
ICommandService,
Inject,
Injector,
- isICellData,
IUniverInstanceService,
LifecycleStages,
ObjectMatrix,
@@ -145,7 +144,7 @@ export class FormatPainterController extends Disposable {
};
}
- private _getUndoRedoMutationInfo(unitId: string, subUnitId: string, range: IRange, format: ISelectionFormatInfo) {
+ private _getUndoRedoMutationInfo(unitId: string, subUnitId: string, originRange: IRange, format: ISelectionFormatInfo) {
const sheetInterceptorService = this._sheetInterceptorService;
const univerInstanceService = this._univerInstanceService;
@@ -155,6 +154,14 @@ export class FormatPainterController extends Disposable {
const { startRow, startColumn, endRow, endColumn } = stylesMatrix.getDataRange();
const styleRowsNum = endRow - startRow + 1;
const styleColsNum = endColumn - startColumn + 1;
+ const range = (originRange.startRow === originRange.endRow && originRange.startColumn === originRange.endColumn)
+ ? {
+ startRow: originRange.startRow,
+ startColumn: originRange.startColumn,
+ endRow: originRange.startRow + styleRowsNum - 1,
+ endColumn: originRange.startColumn + styleColsNum - 1,
+ }
+ : originRange;
const styleValues: ICellData[][] = Array.from({ length: range.endRow - range.startRow + 1 }, () =>
Array.from({ length: range.endColumn - range.startColumn + 1 }, () => ({}))
);
@@ -167,7 +174,7 @@ export class FormatPainterController extends Disposable {
const style = stylesMatrix.getValue(mappedRowIndex, mappedColIndex);
if (style) {
- styleValues[rowIndex][colIndex].s = style;
+ styleValues[rowIndex][colIndex].s = Object.keys(style).length > 0 ? style : null;
}
});
});
@@ -194,37 +201,34 @@ export class FormatPainterController extends Disposable {
}
});
const currentSelections = [range];
+ const clearCellValue = new ObjectMatrix<ICellData>();
const cellValue = new ObjectMatrix<ICellData>();
- let realCellValue: IObjectMatrixPrimitiveType<ICellData> | undefined;
if (Tools.isArray(styleValues)) {
for (let i = 0; i < currentSelections.length; i++) {
const { startRow, startColumn, endRow, endColumn } = currentSelections[i];
-
for (let r = 0; r <= endRow - startRow; r++) {
for (let c = 0; c <= endColumn - startColumn; c++) {
+ clearCellValue.setValue(r + startRow, c + startColumn, { s: null });
cellValue.setValue(r + startRow, c + startColumn, styleValues[r][c]);
}
}
}
- } else if (isICellData(styleValues)) {
- for (let i = 0; i < currentSelections.length; i++) {
- const { startRow, startColumn } = currentSelections[i];
-
- cellValue.setValue(startRow, startColumn, styleValues);
- }
- } else {
- realCellValue = styleValues as IObjectMatrixPrimitiveType<ICellData>;
}
+ const clearStyleMutationParams: ISetRangeValuesMutationParams = {
+ subUnitId,
+ unitId,
+ cellValue: clearCellValue.getMatrix(),
+ };
const setRangeValuesMutationParams: ISetRangeValuesMutationParams = {
subUnitId,
unitId,
- cellValue: realCellValue ?? cellValue.getMatrix(),
+ cellValue: cellValue.getMatrix(),
};
const undoSetRangeValuesMutationParams: ISetRangeValuesMutationParams = this._injector.invoke(
SetRangeValuesUndoMutationFactory,
- setRangeValuesMutationParams
+ clearStyleMutationParams
);
const { undos: interceptorUndos, redos: interceptorRedos } = sheetInterceptorService.onCommandExecute({
@@ -277,11 +281,13 @@ export class FormatPainterController extends Disposable {
return {
undos: [
+ { id: SetRangeValuesMutation.id, params: clearStyleMutationParams },
{ id: SetRangeValuesMutation.id, params: undoSetRangeValuesMutationParams },
...interceptorUndos,
...mergeUndos,
],
redos: [
+ { id: SetRangeValuesMutation.id, params: clearStyleMutationParams },
{ id: SetRangeValuesMutation.id, params: setRangeValuesMutationParams },
...interceptorRedos,
...mergeRedos,