Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(facade): make facade api sync and chainable #4329

Merged
merged 19 commits into from
Dec 31, 2024
Merged
4 changes: 2 additions & 2 deletions packages/core/src/services/undoredo/undoredo.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ export const UndoCommand = new (class extends MultiImplementationCommand impleme

readonly id = UndoCommandId;

async handler(accessor: IAccessor) {
handler(accessor: IAccessor) {
const undoRedoService = accessor.get(IUndoRedoService);
const element = undoRedoService.pitchTopUndoElement();

Expand All @@ -136,7 +136,7 @@ export const RedoCommand = new (class extends MultiImplementationCommand impleme

readonly id = RedoCommandId;

async handler(accessor: IAccessor) {
handler(accessor: IAccessor) {
const undoRedoService = accessor.get(IUndoRedoService);
const element = undoRedoService.pitchTopRedoElement();
if (!element) {
Expand Down
43 changes: 24 additions & 19 deletions packages/sheets-conditional-formatting/src/facade/f-range.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,17 +74,17 @@ export interface IFRangeConditionalFormattingMixin {
/**
* Add a new conditional format
* @param {IConditionFormattingRule} rule
* @return {*} {Promise<boolean>}
* @memberof IFWorksheetConditionalFormattingMixin
* @returns {FRange} Returns the current range instance for method chaining
* @memberof IFRangeConditionalFormattingMixin
*/
addConditionalFormattingRule(rule: IConditionFormattingRule): Promise<boolean>;
addConditionalFormattingRule(rule: IConditionFormattingRule): FRange;

/**
* Delete conditional format according to `cfId`
*
* @param {string} cfId
* @return {*} {Promise<boolean>}
* @memberof IFWorksheetConditionalFormattingMixin
* @returns {FRange} Returns the current range instance for method chaining
* @memberof IFRangeConditionalFormattingMixin
* @example
* ```ts
* const workbook = univerAPI.getActiveWorkbook();
Expand All @@ -93,14 +93,15 @@ export interface IFRangeConditionalFormattingMixin {
* worksheet?.deleteConditionalFormattingRule(rules![0].cfId);
* ```
*/
deleteConditionalFormattingRule(cfId: string): Promise<boolean>;
deleteConditionalFormattingRule(cfId: string): FRange;

/**
* Modify the priority of the conditional format
* @param {string} cfId Rules that need to be moved
* @param {string} toCfId Target rule
* @param {IAnchor['type']} [type] After the default move to the destination rule, if type = before moves to the front, the default value is after
* @memberof FWorksheetConditionalFormattingMixin
* @returns {FRange} Returns the current range instance for method chaining
* @memberof FRangeConditionalFormattingMixin
* @example
* ```ts
* const workbook = univerAPI.getActiveWorkbook();
Expand All @@ -111,14 +112,14 @@ export interface IFRangeConditionalFormattingMixin {
* worksheet?.moveConditionalFormattingRule(rule.cfId, targetRule.cfId, 'before');
* ```
*/
moveConditionalFormattingRule(cfId: string, toCfId: string, type?: IAnchor['type']): Promise<boolean>;
moveConditionalFormattingRule(cfId: string, toCfId: string, type?: IAnchor['type']): FRange;

/**
* Set the conditional format according to `cfId`
* @param {string} cfId
* @param {IConditionFormattingRule} rule
* @return {*} {Promise<boolean>}
* @memberof IFWorksheetConditionalFormattingMixin
* @returns {FRange} Returns the current range instance for method chaining
* @memberof IFRangeConditionalFormattingMixin
* @example
* ```ts
* const workbook = univerAPI.getActiveWorkbook();
Expand All @@ -128,7 +129,7 @@ export interface IFRangeConditionalFormattingMixin {
* worksheet?.setConditionalFormattingRule(rule.cfId, { ...rule, ranges: [] });
* ```
*/
setConditionalFormattingRule(cfId: string, rule: IConditionFormattingRule): Promise<boolean>;
setConditionalFormattingRule(cfId: string, rule: IConditionFormattingRule): FRange;
}

export class FRangeConditionalFormattingMixin extends FRange implements IFRangeConditionalFormattingMixin {
Expand All @@ -145,36 +146,40 @@ export class FRangeConditionalFormattingMixin extends FRange implements IFRangeC
return new FConditionalFormattingBuilder({ ranges: [this._range] });
}

override addConditionalFormattingRule(rule: IConditionFormattingRule): Promise<boolean> {
override addConditionalFormattingRule(rule: IConditionFormattingRule): FRange {
const params: IAddConditionalRuleMutationParams = {
rule, unitId: this._workbook.getUnitId(), subUnitId: this._worksheet.getSheetId(),
};
return this._commandService.executeCommand(AddConditionalRuleMutation.id, params);
this._commandService.syncExecuteCommand(AddConditionalRuleMutation.id, params);
return this;
}

override deleteConditionalFormattingRule(cfId: string): Promise<boolean> {
override deleteConditionalFormattingRule(cfId: string): FRange {
const params: IDeleteConditionalRuleMutationParams = {
unitId: this._workbook.getUnitId(), subUnitId: this._worksheet.getSheetId(),
cfId,
};
return this._commandService.executeCommand(DeleteConditionalRuleMutation.id, params);
this._commandService.syncExecuteCommand(DeleteConditionalRuleMutation.id, params);
return this;
}

override moveConditionalFormattingRule(cfId: string, toCfId: string, type: IAnchor['type'] = 'after'): Promise<boolean> {
override moveConditionalFormattingRule(cfId: string, toCfId: string, type: IAnchor['type'] = 'after'): FRange {
const params: IMoveConditionalRuleMutationParams = {
unitId: this._workbook.getUnitId(), subUnitId: this._worksheet.getSheetId(),
start: { id: cfId, type: 'self' }, end: { id: toCfId, type },
};
return this._commandService.executeCommand(MoveConditionalRuleMutation.id, params);
this._commandService.syncExecuteCommand(MoveConditionalRuleMutation.id, params);
return this;
}

override setConditionalFormattingRule(cfId: string, rule: IConditionFormattingRule): Promise<boolean> {
override setConditionalFormattingRule(cfId: string, rule: IConditionFormattingRule): FRange {
const params: ISetConditionalRuleMutationParams = {
unitId: this._workbook.getUnitId(), subUnitId: this._worksheet.getSheetId(),
rule,
cfId,
};
return this._commandService.executeCommand(SetConditionalRuleMutation.id, params);
this._commandService.syncExecuteCommand(SetConditionalRuleMutation.id, params);
return this;
}
}

Expand Down
35 changes: 20 additions & 15 deletions packages/sheets-conditional-formatting/src/facade/f-worksheet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ export interface IFWorksheetConditionalFormattingMixin {
/**
* Add a new conditional format
* @param {IConditionFormattingRule} rule
* @return {*} {Promise<boolean>}
* @returns {FWorksheet} Returns the current worksheet instance for method chaining
* @memberof IFWorksheetConditionalFormattingMixin
* @example
* ```ts
Expand All @@ -84,13 +84,13 @@ export interface IFWorksheetConditionalFormattingMixin {
* worksheet?.addConditionalFormattingRule(rule!);
* ```
*/
addConditionalFormattingRule(rule: IConditionFormattingRule): Promise<boolean>;
addConditionalFormattingRule(rule: IConditionFormattingRule): FWorksheet;

/**
* Delete conditional format according to `cfId`
*
* @param {string} cfId
* @return {*} {Promise<boolean>}
* @returns {FWorksheet} Returns the current worksheet instance for method chaining
* @memberof IFWorksheetConditionalFormattingMixin
* @example
* ```ts
Expand All @@ -100,13 +100,14 @@ export interface IFWorksheetConditionalFormattingMixin {
* worksheet?.deleteConditionalFormattingRule(rules![0].cfId);
* ```
*/
deleteConditionalFormattingRule(cfId: string): Promise<boolean>;
deleteConditionalFormattingRule(cfId: string): FWorksheet;

/**
* Modify the priority of the conditional format
* @param {string} cfId Rules that need to be moved
* @param {string} toCfId Target rule
* @param {IAnchor['type']} [type] After the default move to the destination rule, if type = before moves to the front, the default value is after
* @returns {FWorksheet} Returns the current worksheet instance for method chaining
* @memberof FWorksheetConditionalFormattingMixin
* @example
* ```ts
Expand All @@ -118,13 +119,13 @@ export interface IFWorksheetConditionalFormattingMixin {
* worksheet?.moveConditionalFormattingRule(rule.cfId, targetRule.cfId, 'before');
* ```
*/
moveConditionalFormattingRule(cfId: string, toCfId: string, type?: IAnchor['type']): Promise<boolean>;
moveConditionalFormattingRule(cfId: string, toCfId: string, type?: IAnchor['type']): FWorksheet;

/**
* Set the conditional format according to `cfId`
* @param {string} cfId
* @param {IConditionFormattingRule} rule
* @return {*} {Promise<boolean>}
* @returns {FWorksheet} Returns the current worksheet instance for method chaining
* @memberof IFWorksheetConditionalFormattingMixin
* @example
* ```ts
Expand All @@ -135,7 +136,7 @@ export interface IFWorksheetConditionalFormattingMixin {
* worksheet?.setConditionalFormattingRule(rule.cfId, { ...rule, ranges: [] });
* ```
*/
setConditionalFormattingRule(cfId: string, rule: IConditionFormattingRule): Promise<boolean>;
setConditionalFormattingRule(cfId: string, rule: IConditionFormattingRule): FWorksheet;
}

export class FWorksheetConditionalFormattingMixin extends FWorksheet implements IFWorksheetConditionalFormattingMixin {
Expand All @@ -152,36 +153,40 @@ export class FWorksheetConditionalFormattingMixin extends FWorksheet implements
return new FConditionalFormattingBuilder();
}

override addConditionalFormattingRule(rule: IConditionFormattingRule): Promise<boolean> {
override addConditionalFormattingRule(rule: IConditionFormattingRule): FWorksheet {
const params: IAddConditionalRuleMutationParams = {
rule, unitId: this._workbook.getUnitId(), subUnitId: this._worksheet.getSheetId(),
};
return this._commandService.executeCommand(AddConditionalRuleMutation.id, params);
this._commandService.syncExecuteCommand(AddConditionalRuleMutation.id, params);
return this;
}

override deleteConditionalFormattingRule(cfId: string): Promise<boolean> {
override deleteConditionalFormattingRule(cfId: string): FWorksheet {
const params: IDeleteConditionalRuleMutationParams = {
unitId: this._workbook.getUnitId(), subUnitId: this._worksheet.getSheetId(),
cfId,
};
return this._commandService.executeCommand(DeleteConditionalRuleMutation.id, params);
this._commandService.syncExecuteCommand(DeleteConditionalRuleMutation.id, params);
return this;
}

override moveConditionalFormattingRule(cfId: string, toCfId: string, type: IAnchor['type'] = 'after'): Promise<boolean> {
override moveConditionalFormattingRule(cfId: string, toCfId: string, type: IAnchor['type'] = 'after'): FWorksheet {
const params: IMoveConditionalRuleMutationParams = {
unitId: this._workbook.getUnitId(), subUnitId: this._worksheet.getSheetId(),
start: { id: cfId, type: 'self' }, end: { id: toCfId, type },
};
return this._commandService.executeCommand(MoveConditionalRuleMutation.id, params);
this._commandService.syncExecuteCommand(MoveConditionalRuleMutation.id, params);
return this;
}

override setConditionalFormattingRule(cfId: string, rule: IConditionFormattingRule): Promise<boolean> {
override setConditionalFormattingRule(cfId: string, rule: IConditionFormattingRule): FWorksheet {
const params: ISetConditionalRuleMutationParams = {
unitId: this._workbook.getUnitId(), subUnitId: this._worksheet.getSheetId(),
rule,
cfId,
};
return this._commandService.executeCommand(SetConditionalRuleMutation.id, params);
this._commandService.syncExecuteCommand(SetConditionalRuleMutation.id, params);
return this;
}
}

Expand Down
17 changes: 10 additions & 7 deletions packages/sheets-crosshair-highlight/src/facade/f-univer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,28 +23,31 @@ export interface IFUniverCrosshairHighlightMixin {
* Enable or disable crosshair highlight.
* @param {boolean} enabled if crosshair highlight should be enabled
*/
setCrosshairHighlightEnabled(enabled: boolean): void;
setCrosshairHighlightEnabled(enabled: boolean): FUniver;

/**
* Set the color of the crosshair highlight.
* @param {string} color the color of the crosshair highlight
*/
setCrosshairHighlightColor(color: string): void;
setCrosshairHighlightColor(color: string): FUniver;
}

export class FUniverCrosshairHighlightMixin extends FUniver implements IFUniverCrosshairHighlightMixin {
override setCrosshairHighlightEnabled(enabled: boolean): void {
override setCrosshairHighlightEnabled(enabled: boolean): FUniver {
if (enabled) {
this._commandService.executeCommand(EnableCrosshairHighlightOperation.id);
this._commandService.syncExecuteCommand(EnableCrosshairHighlightOperation.id);
} else {
this._commandService.executeCommand(DisableCrosshairHighlightOperation.id);
this._commandService.syncExecuteCommand(DisableCrosshairHighlightOperation.id);
}

return this;
}

override setCrosshairHighlightColor(color: string): void {
this._commandService.executeCommand(SetCrosshairHighlightColorOperation.id, {
override setCrosshairHighlightColor(color: string): FUniver {
this._commandService.syncExecuteCommand(SetCrosshairHighlightColorOperation.id, {
value: color,
} as ISetCrosshairHighlightColorOperationParams);
return this;
}
}

Expand Down
8 changes: 4 additions & 4 deletions packages/sheets-data-validation/src/facade/f-range.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export interface IFRangeDataValidationMixin {
* @param rule data validation rule, build by `FUniver.newDataValidation`
* @returns current range
*/
setDataValidation(this: FRange, rule: Nullable<FDataValidation>): Promise<FRange>;
setDataValidation(this: FRange, rule: Nullable<FDataValidation>): FRange;
/**
* get first data validation rule in current range
* @returns data validation rule
Expand All @@ -42,9 +42,9 @@ export interface IFRangeDataValidationMixin {
}

export class FRangeDataValidationMixin extends FRange implements IFRangeDataValidationMixin {
override async setDataValidation(rule: Nullable<FDataValidation>): Promise<FRange> {
override setDataValidation(rule: Nullable<FDataValidation>): FRange {
if (!rule) {
this._commandService.executeCommand(ClearRangeDataValidationCommand.id, {
this._commandService.syncExecuteCommand(ClearRangeDataValidationCommand.id, {
unitId: this._workbook.getUnitId(),
subUnitId: this._worksheet.getSheetId(),
ranges: [this._range],
Expand All @@ -62,7 +62,7 @@ export class FRangeDataValidationMixin extends FRange implements IFRangeDataVali
},
};

await this._commandService.executeCommand(AddSheetDataValidationCommand.id, params);
this._commandService.syncExecuteCommand(AddSheetDataValidationCommand.id, params);
return this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ export interface ISetSheetsFilterCriteriaCommandParams extends ISheetCommandShar
export const SetSheetsFilterCriteriaCommand: ICommand<ISetSheetsFilterCriteriaCommandParams> = {
id: 'sheet.command.set-filter-criteria',
type: CommandType.COMMAND,
handler: async (accessor: IAccessor, params: ISetSheetsFilterCriteriaCommandParams) => {
handler: (accessor: IAccessor, params: ISetSheetsFilterCriteriaCommandParams) => {
const sheetsFilterService = accessor.get(SheetsFilterService);
const commandService = accessor.get(ICommandService);
const undoRedoService = accessor.get(IUndoRedoService);
Expand Down
27 changes: 15 additions & 12 deletions packages/sheets-filter/src/facade/f-filter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ import type { ISheetCommandSharedParams } from '@univerjs/sheets';
import type { FilterModel, IFilterColumn, ISetSheetsFilterCriteriaCommandParams } from '@univerjs/sheets-filter';

import { ICommandService, Inject, Injector } from '@univerjs/core';
import { FRange } from '@univerjs/sheets/facade';
import { ClearSheetsFilterCriteriaCommand, RemoveSheetFilterCommand, SetSheetsFilterCriteriaCommand } from '@univerjs/sheets-filter';
import { FRange } from '@univerjs/sheets/facade';

/**
* This interface class provides methods to modify the filter settings of a worksheet.
Expand Down Expand Up @@ -56,30 +56,32 @@ export class FFilter {
/**
* Clear the filter criteria of a column.
* @param {number} col The column number.
* @returns {Promise<boolean>} If the filter criteria is cleared.
* @returns {FFilter} The interface class to handle the filter.
*/
removeColumnFilterCriteria(col: number): Promise<boolean> {
return this._commandSrv.executeCommand(SetSheetsFilterCriteriaCommand.id, {
removeColumnFilterCriteria(col: number): FFilter {
this._commandSrv.syncExecuteCommand(SetSheetsFilterCriteriaCommand.id, {
unitId: this._workbook.getUnitId(),
subUnitId: this._worksheet.getSheetId(),
col,
criteria: null,
} as ISetSheetsFilterCriteriaCommandParams);
return this;
}

/**
* Set the filter criteria of a column.
* @param {number} col The column number.
* @param {ISetSheetsFilterCriteriaCommandParams['criteria']} criteria The new filter criteria.
* @returns {Promise<boolean>} If the filter criteria is set.
* @returns {FFilter} The interface class to handle the filter.
*/
setColumnFilterCriteria(col: number, criteria: ISetSheetsFilterCriteriaCommandParams['criteria']): Promise<boolean> {
return this._commandSrv.executeCommand(SetSheetsFilterCriteriaCommand.id, {
setColumnFilterCriteria(col: number, criteria: ISetSheetsFilterCriteriaCommandParams['criteria']): FFilter {
this._commandSrv.syncExecuteCommand(SetSheetsFilterCriteriaCommand.id, {
unitId: this._workbook.getUnitId(),
subUnitId: this._worksheet.getSheetId(),
col,
criteria,
} as ISetSheetsFilterCriteriaCommandParams);
return this;
}

/**
Expand All @@ -95,16 +97,17 @@ export class FFilter {
* Remove the filter criteria of all columns.
* @returns {Promise<boolean>} If the filter criteria is removed.
*/
removeFilterCriteria(): Promise<boolean> {
return this._commandSrv.executeCommand(ClearSheetsFilterCriteriaCommand.id);
removeFilterCriteria(): FFilter {
this._commandSrv.syncExecuteCommand(ClearSheetsFilterCriteriaCommand.id);
return this;
}

/**
* Remove the filter from the worksheet.
* @returns {Promise<boolean>} If the filter is removed.
* @returns {boolean} If the filter is removed.
*/
remove(): Promise<boolean> {
return this._commandSrv.executeCommand(RemoveSheetFilterCommand.id, {
remove(): boolean {
return this._commandSrv.syncExecuteCommand(RemoveSheetFilterCommand.id, {
unitId: this._workbook.getUnitId(),
subUnitId: this._worksheet.getSheetId(),
} as ISheetCommandSharedParams);
Expand Down
Loading
Loading