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.

refactor(sheet): format-painter supports adding hooks to extend command (#2505)

YuHong1580ee0d5fb4
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 787bc670f6..07de025ce5 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
@@ -268,11 +268,15 @@ describe('Test format painter rules in controller', () => {
],
});
await commandService.executeCommand(SetOnceFormatPainterCommand.id);
- await (formatPainterController as any)._applyFormatPainter({
- startRow: 0,
- endRow: 4,
- startColumn: 2,
- endColumn: 3,
+ await commandService.executeCommand(ApplyFormatPainterCommand.id, {
+ range: {
+ startRow: 0,
+ endRow: 4,
+ startColumn: 2,
+ endColumn: 3,
+ },
+ unitId: 'workbook-01',
+ subUnitId: 'sheet-0011',
});
expect(workbook.getSheetBySheetId('sheet-0011')?.getCell(0, 2)?.s).toBe('yifA1t');
expect(workbook.getSheetBySheetId('sheet-0011')?.getCell(0, 3)?.s).toBe('M5JbP2');
@@ -320,11 +324,15 @@ describe('Test format painter rules in controller', () => {
});
await commandService.executeCommand(SetOnceFormatPainterCommand.id);
- await (formatPainterController as any)._applyFormatPainter({
- startRow: 10,
- endRow: 13,
- startColumn: 12,
- endColumn: 13,
+ await commandService.executeCommand(ApplyFormatPainterCommand.id, {
+ range: {
+ startRow: 10,
+ endRow: 13,
+ startColumn: 12,
+ endColumn: 13,
+ },
+ unitId: 'workbook-01',
+ subUnitId: 'sheet-0011',
});
const mergeData = workbook.getSheetBySheetId('sheet-0011')?.getMergeData();
expect(mergeData?.length).toBe(5);
diff --git a/packages/sheets-ui/src/commands/commands/set-format-painter.command.ts b/packages/sheets-ui/src/commands/commands/set-format-painter.command.ts
index 76352bf3f5..628cb3489d 100644
--- a/packages/sheets-ui/src/commands/commands/set-format-painter.command.ts
+++ b/packages/sheets-ui/src/commands/commands/set-format-painter.command.ts
@@ -14,39 +14,13 @@
* limitations under the License.
*/
-import type { ICellData, ICommand, IMutationInfo, IObjectMatrixPrimitiveType, IRange } from '@univerjs/core';
+import type { ICommand, IRange } from '@univerjs/core';
import {
CommandType,
ICommandService,
- isICellData,
- IUndoRedoService,
- IUniverInstanceService,
- ObjectMatrix,
- sequenceExecute,
- Tools,
} from '@univerjs/core';
-import type {
- IAddWorksheetMergeMutationParams,
- IRemoveWorksheetMergeMutationParams,
- ISetRangeValuesMutationParams,
-} from '@univerjs/sheets';
-import {
- AddMergeUndoMutationFactory,
- AddWorksheetMergeMutation,
- getAddMergeMutationRangeByType,
- getSheetCommandTarget,
- RemoveMergeUndoMutationFactory,
- RemoveWorksheetMergeMutation,
- SelectionManagerService,
- SetRangeValuesCommand,
- SetRangeValuesMutation,
- SetRangeValuesUndoMutationFactory,
- SheetInterceptorService,
-
-} from '@univerjs/sheets';
import type { IAccessor } from '@wendellhu/redi';
-import { checkCellContentInRanges, getClearContentMutationParamsForRanges } from '../../common/utils';
import { FormatPainterStatus, IFormatPainterService } from '../../services/format-painter/format-painter.service';
import { SetFormatPainterOperation } from '../operations/set-format-painter.operation';
@@ -92,137 +66,14 @@ export const SetOnceFormatPainterCommand: ICommand = {
export interface IApplyFormatPainterCommandParams {
subUnitId: string;
unitId: string;
- styleRange: IRange;
- styleValues: ICellData[][];
- mergeRanges: IRange[];
+ range: IRange;
}
export const ApplyFormatPainterCommand: ICommand = {
type: CommandType.COMMAND,
id: 'sheet.command.apply-format-painter',
- // eslint-disable-next-line max-lines-per-function
handler: async (accessor: IAccessor, params: IApplyFormatPainterCommandParams) => {
- const commandService = accessor.get(ICommandService);
- const undoRedoService = accessor.get(IUndoRedoService);
- const univerInstanceService = accessor.get(IUniverInstanceService);
- const selectionManagerService = accessor.get(SelectionManagerService);
- const sheetInterceptorService = accessor.get(SheetInterceptorService);
-
- const target = getSheetCommandTarget(univerInstanceService, params);
- if (!target) return false;
-
- const { worksheet, unitId, subUnitId } = target;
-
- const {
- styleValues: value,
- styleRange: range,
- mergeRanges,
- } = params;
-
- const currentSelections = range ? [range] : selectionManagerService.getSelectionRanges();
- if (!currentSelections || !currentSelections.length) {
- return false;
- }
-
- const cellValue = new ObjectMatrix<ICellData>();
- let realCellValue: IObjectMatrixPrimitiveType<ICellData> | undefined;
-
- if (Tools.isArray(value)) {
- 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++) {
- cellValue.setValue(r + startRow, c + startColumn, value[r][c]);
- }
- }
- }
- } else if (isICellData(value)) {
- for (let i = 0; i < currentSelections.length; i++) {
- const { startRow, startColumn } = currentSelections[i];
-
- cellValue.setValue(startRow, startColumn, value);
- }
- } else {
- realCellValue = value as IObjectMatrixPrimitiveType<ICellData>;
- }
-
- const setRangeValuesMutationParams: ISetRangeValuesMutationParams = {
- subUnitId,
- unitId,
- cellValue: realCellValue ?? cellValue.getMatrix(),
- };
- const undoSetRangeValuesMutationParams: ISetRangeValuesMutationParams = SetRangeValuesUndoMutationFactory(
- accessor,
- setRangeValuesMutationParams
- );
-
- const setValueMutationResult = commandService.syncExecuteCommand(
- SetRangeValuesMutation.id,
- setRangeValuesMutationParams
- );
-
- const { undos: interceptorUndos, redos: interceptorRedos } = sheetInterceptorService.onCommandExecute({
- id: SetRangeValuesCommand.id,
- params: { ...setRangeValuesMutationParams, range: currentSelections },
- });
-
- // handle merge
- const ranges = getAddMergeMutationRangeByType(mergeRanges);
-
- const mergeRedos: IMutationInfo[] = [];
- const mergeUndos: IMutationInfo[] = [];
-
- // First we should check if there are values in the going-to-be-merged cells.
- const willRemoveSomeCell = checkCellContentInRanges(worksheet, ranges);
-
- // prepare redo mutations
- const removeMergeMutationParams: IRemoveWorksheetMergeMutationParams = {
- unitId,
- subUnitId,
- ranges,
- };
- const addMergeMutationParams: IAddWorksheetMergeMutationParams = {
- unitId,
- subUnitId,
- ranges,
- };
- mergeRedos.push({ id: RemoveWorksheetMergeMutation.id, params: removeMergeMutationParams });
- mergeRedos.push({ id: AddWorksheetMergeMutation.id, params: addMergeMutationParams });
-
- // prepare undo mutations
- const undoRemoveMergeMutationParams = RemoveMergeUndoMutationFactory(accessor, removeMergeMutationParams);
- const undoMutationParams = AddMergeUndoMutationFactory(accessor, addMergeMutationParams);
- mergeUndos.push({ id: RemoveWorksheetMergeMutation.id, params: undoMutationParams });
- mergeUndos.push({ id: AddWorksheetMergeMutation.id, params: undoRemoveMergeMutationParams });
-
- // add set range values mutations to undo redo mutations
- if (willRemoveSomeCell) {
- const data = getClearContentMutationParamsForRanges(accessor, unitId, worksheet, ranges);
- mergeRedos.unshift(...data.redos);
- mergeUndos.push(...data.undos);
- }
-
- const result = await sequenceExecute([...interceptorRedos, ...mergeRedos], commandService);
-
- if (setValueMutationResult && result.result) {
- undoRedoService.pushUndoRedo({
- unitID: unitId,
- undoMutations: [
- { id: SetRangeValuesMutation.id, params: undoSetRangeValuesMutationParams },
- ...interceptorUndos,
- ...mergeUndos,
- ],
- redoMutations: [
- { id: SetRangeValuesMutation.id, params: setRangeValuesMutationParams },
- ...interceptorRedos,
- ...mergeRedos,
- ],
- });
-
- return true;
- }
-
- return false;
+ const formatPainterService = accessor.get(IFormatPainterService);
+ return formatPainterService.applyFormatPainter(params.unitId, params.subUnitId, params.range);
},
};
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 db60175b65..fdc802852c 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,24 +14,32 @@
* limitations under the License.
*/
-import type { ICellData, IRange, Workbook } from '@univerjs/core';
+import type { ICellData, IMutationInfo, IObjectMatrixPrimitiveType, IRange, IStyleData, Workbook } from '@univerjs/core';
import {
Disposable,
ICommandService,
+ isICellData,
IUniverInstanceService,
LifecycleStages,
+ ObjectMatrix,
OnLifecycle,
toDisposable,
+ Tools,
UniverInstanceType,
} from '@univerjs/core';
-import type { IApplyFormatPainterCommandParams } from '../../commands/commands/set-format-painter.command';
+import { Inject, Injector } from '@wendellhu/redi';
+import type { IAddWorksheetMergeMutationParams, IRemoveWorksheetMergeMutationParams, ISetRangeValuesMutationParams } from '@univerjs/sheets';
+import { AddMergeUndoMutationFactory, AddWorksheetMergeMutation, getAddMergeMutationRangeByType, RemoveMergeUndoMutationFactory, RemoveWorksheetMergeMutation, SelectionManagerService, SetRangeValuesCommand, SetRangeValuesMutation, SetRangeValuesUndoMutationFactory, SheetInterceptorService } from '@univerjs/sheets';
+import { getCellInfoInMergeData } from '@univerjs/engine-render';
import {
ApplyFormatPainterCommand,
SetOnceFormatPainterCommand,
} from '../../commands/commands/set-format-painter.command';
+import type { IFormatPainterHook, ISelectionFormatInfo } from '../../services/format-painter/format-painter.service';
import { FormatPainterStatus, IFormatPainterService } from '../../services/format-painter/format-painter.service';
import { ISelectionRenderService } from '../../services/selection/selection-render.service';
+import { checkCellContentInRanges, getClearContentMutationParamsForRanges } from '../../common/utils';
@OnLifecycle(LifecycleStages.Rendered, FormatPainterController)
export class FormatPainterController extends Disposable {
@@ -39,7 +47,10 @@ export class FormatPainterController extends Disposable {
@ICommandService private readonly _commandService: ICommandService,
@IFormatPainterService private readonly _formatPainterService: IFormatPainterService,
@IUniverInstanceService private readonly _univerInstanceService: IUniverInstanceService,
- @ISelectionRenderService private readonly _selectionRenderService: ISelectionRenderService
+ @ISelectionRenderService private readonly _selectionRenderService: ISelectionRenderService,
+ @Inject(SelectionManagerService) private readonly _selectionManagerService: SelectionManagerService,
+ @Inject(SheetInterceptorService) private readonly _sheetInterceptorService: SheetInterceptorService,
+ @Inject(Injector) private readonly _injector: Injector
) {
super();
@@ -48,6 +59,7 @@ export class FormatPainterController extends Disposable {
private _initialize() {
this._commandExecutedListener();
+ this._addDefaultHook();
}
private _commandExecutedListener() {
@@ -56,11 +68,15 @@ export class FormatPainterController extends Disposable {
this._selectionRenderService.selectionMoveEnd$.subscribe((selections) => {
if (this._formatPainterService.getStatus() !== FormatPainterStatus.OFF) {
const { rangeWithCoord } = selections[selections.length - 1];
- this._applyFormatPainter({
- startRow: rangeWithCoord.startRow,
- startColumn: rangeWithCoord.startColumn,
- endRow: rangeWithCoord.endRow,
- endColumn: rangeWithCoord.endColumn,
+ this._commandService.executeCommand(ApplyFormatPainterCommand.id, {
+ unitId: this._univerInstanceService.getFocusedUnit()?.getUnitId() || '',
+ subUnitId: (this._univerInstanceService.getFocusedUnit() as Workbook).getActiveSheet().getSheetId(),
+ range: {
+ startRow: rangeWithCoord.startRow,
+ startColumn: rangeWithCoord.startColumn,
+ endRow: rangeWithCoord.endRow,
+ endColumn: rangeWithCoord.endColumn,
+ },
});
// if once, turn off the format painter
if (this._formatPainterService.getStatus() === FormatPainterStatus.ONCE) {
@@ -72,12 +88,68 @@ export class FormatPainterController extends Disposable {
);
}
- private async _applyFormatPainter(range: IRange) {
- const { styles: stylesMatrix, merges } = this._formatPainterService.getSelectionFormat();
+ private _addDefaultHook() {
+ const defaultHook: IFormatPainterHook = {
+ id: 'default-format-painter',
+ priority: 0,
+ isDefaultHook: true,
+ onStatusChange: (status: FormatPainterStatus) => {
+ if (status !== FormatPainterStatus.OFF) {
+ const format = this._collectSelectionRangeFormat();
+ if (format) {
+ this._formatPainterService.setSelectionFormat(format);
+ }
+ }
+ },
+ onApply: (unitId, subUnitId, range, format) => {
+ return this._getUndoRedoMutationInfo(unitId, subUnitId, range, format);
+ },
+ };
+ this._formatPainterService.addHook(defaultHook);
+ }
+
+ private _collectSelectionRangeFormat() {
+ const selection = this._selectionManagerService.getLast();
+ const range = selection?.range;
+ if (!range) return null;
+ const { startRow, endRow, startColumn, endColumn } = range;
const workbook = this._univerInstanceService.getCurrentUnitForType<Workbook>(UniverInstanceType.UNIVER_SHEET)!;
- const unitId = workbook.getUnitId();
- const subUnitId = workbook.getActiveSheet().getSheetId();
- if (!stylesMatrix) return;
+ const worksheet = workbook?.getActiveSheet();
+ const cellData = worksheet.getCellMatrix();
+ const mergeData = this._univerInstanceService.getCurrentUnitForType<Workbook>(UniverInstanceType.UNIVER_SHEET)!.getActiveSheet().getMergeData();
+
+ const styles = workbook.getStyles();
+ const stylesMatrix = new ObjectMatrix<IStyleData>();
+ const merges = [];
+ for (let r = startRow; r <= endRow; r++) {
+ for (let c = startColumn; c <= endColumn; c++) {
+ const cell = cellData.getValue(r, c) as ICellData;
+ stylesMatrix.setValue(r, c, styles.getStyleByCell(cell) || {});
+ const { isMergedMainCell, ...mergeInfo } = getCellInfoInMergeData(r, c, mergeData);
+ if (isMergedMainCell) {
+ merges.push({
+ startRow: mergeInfo.startRow,
+ startColumn: mergeInfo.startColumn,
+ endRow: mergeInfo.endRow,
+ endColumn: mergeInfo.endColumn,
+ });
+ }
+ }
+ }
+ return {
+ styles: stylesMatrix,
+ merges,
+ };
+ }
+
+ private _getUndoRedoMutationInfo(unitId: string, subUnitId: string, range: IRange, format: ISelectionFormatInfo) {
+ const sheetInterceptorService = this._sheetInterceptorService;
+ const univerInstanceService = this._univerInstanceService;
+ const accessor = {
+ get: this._injector.get.bind(this._injector),
+ };
+ const { merges, styles: stylesMatrix } = format;
+ if (!stylesMatrix) return { undos: [], redos: [] };
const { startRow, startColumn, endRow, endColumn } = stylesMatrix.getDataRange();
const styleRowsNum = endRow - startRow + 1;
@@ -120,15 +192,93 @@ export class FormatPainterController extends Disposable {
}
}
});
+ const currentSelections = [range];
+ 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++) {
+ 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 setRangeValuesMutationParams: ISetRangeValuesMutationParams = {
+ subUnitId,
+ unitId,
+ cellValue: realCellValue ?? cellValue.getMatrix(),
+ };
+ const undoSetRangeValuesMutationParams: ISetRangeValuesMutationParams = SetRangeValuesUndoMutationFactory(
+ accessor,
+ setRangeValuesMutationParams
+ );
+
+ const { undos: interceptorUndos, redos: interceptorRedos } = sheetInterceptorService.onCommandExecute({
+ id: SetRangeValuesCommand.id,
+ params: { ...setRangeValuesMutationParams, range: currentSelections },
+ });
- const ApplyFormatPainterCommandParams: IApplyFormatPainterCommandParams = {
+ // handle merge
+ const ranges = getAddMergeMutationRangeByType(mergeRanges);
+
+ const mergeRedos: IMutationInfo[] = [];
+ const mergeUndos: IMutationInfo[] = [];
+
+ // First we should check if there are values in the going-to-be-merged cells.
+ const worksheet = (univerInstanceService.getUnit(unitId) as Workbook).getSheetBySheetId(subUnitId)!;
+ const willRemoveSomeCell = checkCellContentInRanges(worksheet, ranges);
+
+ // prepare redo mutations
+ const removeMergeMutationParams: IRemoveWorksheetMergeMutationParams = {
+ unitId,
subUnitId,
+ ranges,
+ };
+ const addMergeMutationParams: IAddWorksheetMergeMutationParams = {
unitId,
- styleRange: range,
- styleValues,
- mergeRanges,
+ subUnitId,
+ ranges,
};
+ mergeRedos.push({ id: RemoveWorksheetMergeMutation.id, params: removeMergeMutationParams });
+ mergeRedos.push({ id: AddWorksheetMergeMutation.id, params: addMergeMutationParams });
+
+ // prepare undo mutations
+ const undoRemoveMergeMutationParams = RemoveMergeUndoMutationFactory(accessor, removeMergeMutationParams);
+ const undoMutationParams = AddMergeUndoMutationFactory(accessor, addMergeMutationParams);
+ mergeUndos.push({ id: RemoveWorksheetMergeMutation.id, params: undoMutationParams });
+ mergeUndos.push({ id: AddWorksheetMergeMutation.id, params: undoRemoveMergeMutationParams });
- await this._commandService.executeCommand(ApplyFormatPainterCommand.id, ApplyFormatPainterCommandParams);
+ // add set range values mutations to undo redo mutations
+ if (willRemoveSomeCell) {
+ const data = getClearContentMutationParamsForRanges(accessor, unitId, worksheet, ranges);
+ mergeRedos.unshift(...data.redos);
+ mergeUndos.push(...data.undos);
+ }
+
+ return {
+ undos: [
+ { id: SetRangeValuesMutation.id, params: undoSetRangeValuesMutationParams },
+ ...interceptorUndos,
+ ...mergeUndos,
+ ],
+ redos: [
+ { id: SetRangeValuesMutation.id, params: setRangeValuesMutationParams },
+ ...interceptorRedos,
+ ...mergeRedos,
+ ],
+ };
}
}
diff --git a/packages/sheets-ui/src/services/format-painter/format-painter.service.ts b/packages/sheets-ui/src/services/format-painter/format-painter.service.ts
index 13a287fbca..a14be91b40 100644
--- a/packages/sheets-ui/src/services/format-painter/format-painter.service.ts
+++ b/packages/sheets-ui/src/services/format-painter/format-painter.service.ts
@@ -14,9 +14,8 @@
* limitations under the License.
*/
-import type { ICellData, IRange, IStyleData, Workbook } from '@univerjs/core';
-import { Disposable, IUniverInstanceService, ObjectMatrix, UniverInstanceType } from '@univerjs/core';
-import { getCellInfoInMergeData } from '@univerjs/engine-render';
+import type { IMutationInfo, IRange, IStyleData } from '@univerjs/core';
+import { Disposable, ICommandService, ILogService, IUndoRedoService, IUniverInstanceService, ObjectMatrix } from '@univerjs/core';
import { SelectionManagerService, SetRangeValuesMutation } from '@univerjs/sheets';
import { createIdentifier, Inject } from '@wendellhu/redi';
import type { Observable } from 'rxjs';
@@ -26,15 +25,34 @@ import { IMarkSelectionService } from '../mark-selection/mark-selection.service'
export interface IFormatPainterService {
status$: Observable<FormatPainterStatus>;
+ addHook(hooks: IFormatPainterHook): void;
+ getHooks(): IFormatPainterHook[];
setStatus(status: FormatPainterStatus): void;
getStatus(): FormatPainterStatus;
+ setSelectionFormat(format: ISelectionFormatInfo): void;
getSelectionFormat(): ISelectionFormatInfo;
+ applyFormatPainter(unitId: string, subUnitId: string, range: IRange): boolean;
}
export interface ISelectionFormatInfo {
styles: ObjectMatrix<IStyleData>;
merges: IRange[];
}
+
+export interface IFormatPainterHook {
+ id: string;
+ isDefaultHook?: boolean;
+ priority?: number;
+ onStatusChange(status: FormatPainterStatus): void;
+ onApply(
+ unitId: string,
+ subUnitId: string,
+ range: IRange,
+ format: ISelectionFormatInfo): {
+ undos: IMutationInfo[];
+ redos: IMutationInfo[];
+ };
+}
export enum FormatPainterStatus {
OFF,
ONCE,
@@ -48,11 +66,16 @@ export class FormatPainterService extends Disposable implements IFormatPainterSe
private _selectionFormat: ISelectionFormatInfo;
private _markId: string | null = null;
private readonly _status$: BehaviorSubject<FormatPainterStatus>;
+ private _defaultHook: IFormatPainterHook | null = null;
+ private _extendHooks: IFormatPainterHook[] = [];
constructor(
@Inject(SelectionManagerService) private readonly _selectionManagerService: SelectionManagerService,
@IUniverInstanceService private readonly _univerInstanceService: IUniverInstanceService,
- @IMarkSelectionService private readonly _markSelectionService: IMarkSelectionService
+ @IMarkSelectionService private readonly _markSelectionService: IMarkSelectionService,
+ @ILogService private readonly _logService: ILogService,
+ @ICommandService private readonly _commandService: ICommandService,
+ @IUndoRedoService private readonly _undoRedoService: IUndoRedoService
) {
super();
@@ -61,21 +84,77 @@ export class FormatPainterService extends Disposable implements IFormatPainterSe
this._selectionFormat = { styles: new ObjectMatrix<IStyleData>(), merges: [] };
}
- setStatus(status: FormatPainterStatus) {
- if (status !== FormatPainterStatus.OFF) {
- this._getSelectionRangeFormat();
+ addHook(hook: IFormatPainterHook) {
+ if (hook.isDefaultHook && (hook.priority ?? 0) > (this._defaultHook?.priority ?? -1)) {
+ this._defaultHook = hook;
+ } else {
+ this._extendHooks.push(hook);
+ this._extendHooks.sort((a, b) => (a.priority || 0) - (b.priority || 0));
}
+ }
+
+ getHooks() {
+ return this._defaultHook ? [this._defaultHook, ...this._extendHooks] : this._extendHooks;
+ }
+
+ setStatus(status: FormatPainterStatus) {
this._updateRangeMark(status);
this._status$.next(status);
+ const hooks = this.getHooks();
+ hooks.forEach((hook) => {
+ hook.onStatusChange(status);
+ });
}
getStatus(): FormatPainterStatus {
return this._status$.getValue();
}
+ setSelectionFormat(format: ISelectionFormatInfo) {
+ this._selectionFormat = format;
+ }
+
+ getSelectionFormat() {
+ return this._selectionFormat;
+ }
+
+ applyFormatPainter(unitId: string, subUnitId: string, range: IRange) {
+ const hooks = this.getHooks();
+ const redoMutationsInfo: IMutationInfo[] = [];
+ const undoMutationsInfo: IMutationInfo[] = [];
+ hooks.forEach((h) => {
+ const applyReturn = h.onApply(
+ unitId,
+ subUnitId,
+ range,
+ this._selectionFormat
+ );
+ if (applyReturn) {
+ redoMutationsInfo.push(...applyReturn.redos);
+ undoMutationsInfo.push(...applyReturn.undos);
+ }
+ });
+
+ this._logService.log('[FormatPainterService]', 'apply mutations', {
+ undoMutationsInfo,
+ redoMutationsInfo,
+ });
+
+ const result = redoMutationsInfo.every((m) => this._commandService.executeCommand(m.id, m.params));
+ if (result) {
+ // add to undo redo services
+ this._undoRedoService.pushUndoRedo({
+ unitID: unitId,
+ undoMutations: undoMutationsInfo,
+ redoMutations: redoMutationsInfo,
+ });
+ }
+
+ return result;
+ }
+
private _updateRangeMark(status: FormatPainterStatus) {
this._markSelectionService.removeAllShapes();
- this._markId = null;
if (status !== FormatPainterStatus.OFF) {
const selection = this._selectionManagerService.getLast();
@@ -91,39 +170,4 @@ export class FormatPainterService extends Disposable implements IFormatPainterSe
}
}
}
-
- private _getSelectionRangeFormat() {
- const selection = this._selectionManagerService.getLast();
- const range = selection?.range;
- if (!range) return;
- const { startRow, endRow, startColumn, endColumn } = range;
- const workbook = this._univerInstanceService.getCurrentUnitForType<Workbook>(UniverInstanceType.UNIVER_SHEET)!;
- const worksheet = workbook?.getActiveSheet();
- const cellData = worksheet.getCellMatrix();
- const mergeData = this._univerInstanceService.getCurrentUnitForType<Workbook>(UniverInstanceType.UNIVER_SHEET)!.getActiveSheet().getMergeData();
-
- const styles = workbook.getStyles();
- const stylesMatrix = new ObjectMatrix<IStyleData>();
- this._selectionFormat.merges = [];
- for (let r = startRow; r <= endRow; r++) {
- for (let c = startColumn; c <= endColumn; c++) {
- const cell = cellData.getValue(r, c) as ICellData;
- stylesMatrix.setValue(r, c, styles.getStyleByCell(cell) || {});
- const { isMergedMainCell, ...mergeInfo } = getCellInfoInMergeData(r, c, mergeData);
- if (isMergedMainCell) {
- this._selectionFormat.merges.push({
- startRow: mergeInfo.startRow,
- startColumn: mergeInfo.startColumn,
- endRow: mergeInfo.endRow,
- endColumn: mergeInfo.endColumn,
- });
- }
- }
- }
- this._selectionFormat.styles = stylesMatrix;
- }
-
- getSelectionFormat() {
- return this._selectionFormat;
- }
}