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: support hook in auto fill (#571)

YuHong07ff71dac2c6
diff --git a/packages/ui-plugin-sheets/src/commands/commands/auto-fill.command.ts b/packages/ui-plugin-sheets/src/commands/commands/auto-fill.command.ts
index e8dc3c00b1..1d174543af 100644
--- a/packages/ui-plugin-sheets/src/commands/commands/auto-fill.command.ts
+++ b/packages/ui-plugin-sheets/src/commands/commands/auto-fill.command.ts
@@ -31,6 +31,7 @@ export interface IAutoFillCommandParams {
applyRange: IRange;
selectionRange: IRange;
applyDatas: ICellData[][];
+ extendMutations: { undo: IMutationInfo[]; redo: IMutationInfo[] };
applyMergeRanges?: IRange[];
}
@@ -38,7 +39,7 @@ export const AutoFillCommand: ICommand = {
type: CommandType.COMMAND,
id: 'sheet.command.auto-fill',
// eslint-disable-next-line max-lines-per-function
- handler: async (accessor: IAccessor, params?: IAutoFillCommandParams) => {
+ handler: async (accessor: IAccessor, params: IAutoFillCommandParams) => {
const commandService = accessor.get(ICommandService);
const univerInstanceService = accessor.get(IUniverInstanceService);
const undoRedoService = accessor.get(IUndoRedoService);
@@ -136,7 +137,19 @@ export const AutoFillCommand: ICommand = {
redoSeq.push({ id: AddWorksheetMergeMutation.id, params: addMergeMutationParams });
}
- if (setRangeResult && removeMergeResult && addMergeResult && selectionResult) {
+ // deal with extend mutations by hooks
+ const { undo, redo } = params?.extendMutations;
+ const extendResult = redo.every((item: IMutationInfo) =>
+ commandService.syncExecuteCommand(item.id, item.params)
+ );
+ undo.forEach((item: IMutationInfo) => {
+ undoSeq.push(item);
+ });
+ redo.forEach((item: IMutationInfo) => {
+ redoSeq.push(item);
+ });
+
+ if (setRangeResult && removeMergeResult && addMergeResult && selectionResult && extendResult) {
undoRedoService.pushUndoRedo({
unitID: workbookId,
undoMutations: undoSeq,
diff --git a/packages/ui-plugin-sheets/src/controllers/auto-fill.controller.ts b/packages/ui-plugin-sheets/src/controllers/auto-fill.controller.ts
index 386cd34706..659d715922 100644
--- a/packages/ui-plugin-sheets/src/controllers/auto-fill.controller.ts
+++ b/packages/ui-plugin-sheets/src/controllers/auto-fill.controller.ts
@@ -6,6 +6,7 @@ import {
DisposableCollection,
ICellData,
ICommandService,
+ IMutationInfo,
IRange,
IUniverInstanceService,
LifecycleStages,
@@ -23,7 +24,6 @@ import { fillCopy, fillCopyStyles, getDataIndex, getLenS } from '../services/aut
import {
APPLY_FUNCTIONS,
APPLY_TYPE,
- APPLY_TYPE_IN_USE,
DATA_TYPE,
ICopyDataInType,
ICopyDataPiece,
@@ -35,7 +35,8 @@ import { ISelectionRenderService } from '../services/selection/selection-render.
export class AutoFillController extends Disposable {
private _direction: Direction | null = null;
private _beforeApplyData: Array<Array<Nullable<ICellData>>> = [];
- private _applyType: APPLY_TYPE_IN_USE = APPLY_TYPE.SERIES;
+ private _applyType: APPLY_TYPE = APPLY_TYPE.SERIES;
+
private _hasFillingStyle: boolean = true;
private _copyData: ICopyDataPiece[] = [];
constructor(
@@ -238,7 +239,7 @@ export class AutoFillController extends Disposable {
csLen: number,
asLen: number,
direction: Direction,
- applyType: APPLY_TYPE_IN_USE,
+ applyType: APPLY_TYPE,
hasStyle: boolean = true
) {
const applyData: Array<Nullable<ICellData>> = [];
@@ -295,7 +296,7 @@ export class AutoFillController extends Disposable {
copySquad: ICopyDataInType,
len: number,
direction: Direction,
- applyType: APPLY_TYPE_IN_USE,
+ applyType: APPLY_TYPE,
customApplyFunctions?: APPLY_FUNCTIONS
) {
const { data } = copySquad;
@@ -635,12 +636,47 @@ export class AutoFillController extends Disposable {
});
}
+ // deal with hooks
+ const hooks = this._autoFillService.getHooks();
+ const extendRedos: IMutationInfo[] = [];
+ const extendUndos: IMutationInfo[] = [];
+ const hookApplyType: APPLY_TYPE = this._hasFillingStyle ? applyType : APPLY_TYPE.NO_FORMAT;
+ const repeats = this._getRepeatRange(sourceRange, applyRange, direction);
+ hooks.forEach((h) => {
+ const { hook } = h;
+ if (hookApplyType === APPLY_TYPE.COPY) {
+ repeats.forEach((repeat) => {
+ const { repeatStartCell, relativeRange } = repeat;
+ const { redos, undos } = hook?.[hookApplyType](
+ { row: sourceRange.startRow, col: sourceRange.startColumn },
+ repeatStartCell,
+ relativeRange
+ );
+ redos.forEach((redo) => {
+ extendRedos.push(redo);
+ });
+ undos.forEach((undo) => {
+ extendUndos.push(undo);
+ });
+ });
+ } else {
+ const { redos, undos } = hook?.[hookApplyType](sourceRange, applyRange);
+ redos.forEach((redo) => {
+ extendRedos.push(redo);
+ });
+ undos.forEach((undo) => {
+ extendUndos.push(undo);
+ });
+ }
+ });
+
this._commandService.executeCommand(AutoFillCommand.id, {
selectionRange: destRange,
applyRange,
applyDatas,
workbookId: this._univerInstanceService.getCurrentUniverSheetInstance().getUnitId(),
worksheetId: this._univerInstanceService.getCurrentUniverSheetInstance().getActiveSheet().getSheetId(),
+ extendMutations: { undo: extendUndos, redo: extendRedos },
applyMergeRanges,
});
}
@@ -659,4 +695,137 @@ export class AutoFillController extends Disposable {
return res;
});
}
+
+ private _getRepeatRange(sourceRange: IRange, targetRange: IRange, direction: Direction) {
+ const repeats: Array<{
+ repeatStartCell: { col: number; row: number };
+ relativeRange: IRange;
+ }> = [];
+ // according to direction, calculate every repeat range.
+ // repeatStartCell is the start cell of each repeat range, relativeRange is the relative range of each repeat range
+ if (direction === Direction.DOWN || direction === Direction.UP) {
+ const sourceLength = sourceRange.endRow - sourceRange.startRow + 1;
+ const targetLength = targetRange.endRow - targetRange.startRow + 1;
+ const mod = targetLength / sourceLength;
+ const rest = targetLength % sourceLength;
+ const relativeRange = {
+ startRow: 0,
+ startColumn: 0,
+ endRow: sourceRange.endRow - sourceRange.startRow,
+ endColumn: sourceRange.endColumn - sourceRange.startColumn,
+ };
+ if (direction === Direction.DOWN) {
+ for (let i = 0; i < mod; i++) {
+ repeats.push({
+ repeatStartCell: {
+ row: sourceRange.startRow + (i + 1) * sourceLength,
+ col: sourceRange.startColumn,
+ },
+ relativeRange,
+ });
+ }
+ if (rest > 0) {
+ repeats.push({
+ repeatStartCell: {
+ row: sourceRange.startRow + (mod + 1) * sourceLength,
+ col: sourceRange.startColumn,
+ },
+ relativeRange: {
+ startRow: 0,
+ startColumn: 0,
+ endRow: rest - 1,
+ endColumn: sourceRange.endColumn - sourceRange.startColumn,
+ },
+ });
+ }
+ } else {
+ for (let i = 0; i < mod; i++) {
+ repeats.push({
+ repeatStartCell: {
+ row: sourceRange.startRow - (i + 1) * sourceLength,
+ col: sourceRange.startColumn,
+ },
+ relativeRange,
+ });
+ }
+ if (rest > 0) {
+ repeats.push({
+ repeatStartCell: {
+ row: sourceRange.startRow - (mod + 1) * sourceLength,
+ col: sourceRange.startColumn,
+ },
+ relativeRange: {
+ startRow: sourceLength - rest,
+ endRow: sourceLength - 1,
+ startColumn: 0,
+ endColumn: sourceRange.endColumn - sourceRange.startColumn,
+ },
+ });
+ }
+ }
+ }
+ if (direction === Direction.RIGHT || direction === Direction.LEFT) {
+ const sourceLength = sourceRange.endColumn - sourceRange.startColumn + 1;
+ const targetLength = targetRange.endColumn - targetRange.startColumn + 1;
+ const mod = targetLength / sourceLength;
+ const rest = targetLength % sourceLength;
+ const relativeRange = {
+ startRow: 0,
+ startColumn: 0,
+ endRow: sourceRange.endRow - sourceRange.startRow,
+ endColumn: sourceRange.endColumn - sourceRange.startColumn,
+ };
+ if (direction === Direction.RIGHT) {
+ for (let i = 0; i < mod; i++) {
+ repeats.push({
+ repeatStartCell: {
+ row: sourceRange.startRow,
+ col: sourceRange.startColumn + (i + 1) * sourceLength,
+ },
+ relativeRange,
+ });
+ }
+ if (rest > 0) {
+ repeats.push({
+ repeatStartCell: {
+ row: sourceRange.startRow,
+ col: sourceRange.startColumn + (mod + 1) * sourceLength,
+ },
+ relativeRange: {
+ startRow: 0,
+ startColumn: 0,
+ endRow: sourceRange.endRow - sourceRange.startRow,
+ endColumn: rest - 1,
+ },
+ });
+ }
+ } else {
+ for (let i = 0; i < mod; i++) {
+ repeats.push({
+ repeatStartCell: {
+ row: sourceRange.startRow,
+ col: sourceRange.startColumn - (i + 1) * sourceLength,
+ },
+ relativeRange,
+ });
+ }
+ if (rest > 0) {
+ repeats.push({
+ repeatStartCell: {
+ row: sourceRange.startRow,
+ col: sourceRange.startColumn - (mod + 1) * sourceLength,
+ },
+ relativeRange: {
+ startRow: 0,
+ startColumn: sourceLength - rest,
+ endRow: sourceRange.endRow - sourceRange.startRow,
+ endColumn: sourceLength - 1,
+ },
+ });
+ }
+ }
+ }
+
+ return repeats;
+ }
}
diff --git a/packages/ui-plugin-sheets/src/services/auto-fill/auto-fill.service.ts b/packages/ui-plugin-sheets/src/services/auto-fill/auto-fill.service.ts
index e7adc0a0f9..d5c975b453 100644
--- a/packages/ui-plugin-sheets/src/services/auto-fill/auto-fill.service.ts
+++ b/packages/ui-plugin-sheets/src/services/auto-fill/auto-fill.service.ts
@@ -20,7 +20,7 @@ import {
numberRule,
otherRule,
} from './rules';
-import { APPLY_TYPE, IAutoFillRule } from './type';
+import { APPLY_TYPE, IAutoFillHook, IAutoFillRule } from './type';
export interface IAutoFillService {
getRules(): IAutoFillRule[];
@@ -34,6 +34,7 @@ export interface IAutoFillService {
menu$: Observable<IApplyMenuItem[]>;
setDisableApplyType: (type: APPLY_TYPE, disable: boolean) => void;
registerRule(rule: IAutoFillRule): void;
+ getHooks(): IAutoFillHook[];
}
export interface IApplyMenuItem {
@@ -45,6 +46,7 @@ export interface IApplyMenuItem {
@OnLifecycle(LifecycleStages.Rendered, AutoFillService)
export class AutoFillService extends Disposable implements IAutoFillService {
private _rules: IAutoFillRule[] = [];
+ private _hooks: IAutoFillHook[] = [];
private readonly _applyType$: BehaviorSubject<APPLY_TYPE> = new BehaviorSubject<APPLY_TYPE>(APPLY_TYPE.SERIES);
private _isFillingStyle: boolean = true;
private _sourceRange: IRange | null = null;
@@ -98,6 +100,13 @@ export class AutoFillService extends Disposable implements IAutoFillService {
this._isFillingStyle = true;
}
+ addHook(hook: IAutoFillHook) {
+ if (this._hooks.find((h) => h.hookName === hook.hookName)) {
+ throw new Error(`Add hook failed, hook name '${hook.hookName}' already exist!`);
+ }
+ this._hooks.push(hook);
+ }
+
registerRule(rule: IAutoFillRule) {
// if rule.type is used, console error
if (this._rules.find((r) => r.type === rule.type)) {
@@ -112,6 +121,10 @@ export class AutoFillService extends Disposable implements IAutoFillService {
return this._rules;
}
+ getHooks() {
+ return this._hooks;
+ }
+
getApplyType() {
return this._applyType$.getValue();
}
diff --git a/packages/ui-plugin-sheets/src/services/auto-fill/type.ts b/packages/ui-plugin-sheets/src/services/auto-fill/type.ts
index 0fad27edb4..66b0eaafec 100644
--- a/packages/ui-plugin-sheets/src/services/auto-fill/type.ts
+++ b/packages/ui-plugin-sheets/src/services/auto-fill/type.ts
@@ -1,4 +1,4 @@
-import { Direction, ICellData, Nullable } from '@univerjs/core';
+import { Direction, ICellData, IMutationInfo, IRange, Nullable } from '@univerjs/core';
export enum DATA_TYPE {
NUMBER = 'number',
@@ -31,6 +31,24 @@ export interface IAutoFillRule {
priority: number;
}
+export interface IMutations {
+ redos: IMutationInfo[];
+ undos: IMutationInfo[];
+}
+export interface IAutoFillHook {
+ hookName: string;
+ hook: {
+ [APPLY_TYPE.SERIES]: (sourceRange: IRange, targetRange: IRange) => IMutations;
+ [APPLY_TYPE.NO_FORMAT]: (sourceRange: IRange, targetRange: IRange) => IMutations;
+ [APPLY_TYPE.ONLY_FORMAT]: (sourceRange: IRange, targetRange: IRange) => IMutations;
+ [APPLY_TYPE.COPY]: (
+ sourceStartCell: { row: number; col: number },
+ targetStartCell: { row: number; col: number },
+ relativeRange: IRange
+ ) => IMutations;
+ };
+}
+
export interface IRuleConfirmedData {
type?: string;
cellData: Nullable<ICellData>;
@@ -50,5 +68,3 @@ export enum APPLY_TYPE {
ONLY_FORMAT = '2',
NO_FORMAT = '3',
}
-
-export type APPLY_TYPE_IN_USE = Omit<APPLY_TYPE, APPLY_TYPE.NO_FORMAT>;