Skip to content

Commit

Permalink
feat(facade): add sheet hooks, onCellPointerMove hook (#2193)
Browse files Browse the repository at this point in the history
  • Loading branch information
Dushusir committed May 8, 2024
1 parent 94a86d3 commit 476ffd3
Show file tree
Hide file tree
Showing 5 changed files with 166 additions and 0 deletions.
2 changes: 2 additions & 0 deletions packages/facade/src/apis/__tests__/create-test-bed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ export interface ITestBed {
get: Injector['get'];
sheet: UnitModel<Workbook>;
univerAPI: FUniver;
injector: Injector;
}

export function createTestBed(workbookData?: IWorkbookData, dependencies?: Dependency[]): ITestBed {
Expand Down Expand Up @@ -172,5 +173,6 @@ export function createTestBed(workbookData?: IWorkbookData, dependencies?: Depen
get: injector.get.bind(injector),
sheet,
univerAPI,
injector,
};
}
9 changes: 9 additions & 0 deletions packages/facade/src/apis/facade.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import { SHEET_VIEW_KEY } from '@univerjs/sheets-ui';
import { SetFormulaCalculationStartMutation } from '@univerjs/engine-formula';
import { FDocument } from './docs/f-document';
import { FWorkbook } from './sheets/f-workbook';
import { FSheetHooks } from './sheets/f-sheet-hooks';

export class FUniver {
/**
Expand Down Expand Up @@ -312,6 +313,14 @@ export class FUniver {
return ws;
}

/**
* Get sheet hooks
* @returns
*/
getSheetHooks() {
return this._injector.createInstance(FSheetHooks);
}

/**
* Get sheet render component from render by unitId and view key.
* @param unitId
Expand Down
118 changes: 118 additions & 0 deletions packages/facade/src/apis/sheets/__tests__/f-sheet-hooks.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
/**
* Copyright 2023-present DreamNum Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import type { ICellData, IStyleData, Nullable, UnitModel, Workbook } from '@univerjs/core';
import { ICommandService, IUniverInstanceService, UniverInstanceType } from '@univerjs/core';
import { SetHorizontalTextAlignCommand, SetRangeValuesCommand, SetRangeValuesMutation, SetStyleCommand, SetTextWrapCommand, SetVerticalTextAlignCommand } from '@univerjs/sheets';
import type { Injector } from '@wendellhu/redi';
import { beforeEach, describe, expect, it } from 'vitest';
import { Subject } from 'rxjs';

import type { IHoverCellPosition } from '@univerjs/sheets-ui';
import { HoverManagerService } from '@univerjs/sheets-ui';
import type { FUniver } from '../../facade';
import { createTestBed } from '../../__tests__/create-test-bed';
import { FSheetHooks } from '../f-sheet-hooks';

describe('Test FSheetHooks', () => {
let get: Injector['get'];
let injector: Injector;
let commandService: ICommandService;
let univerAPI: FUniver;
let sheet: UnitModel<Workbook>;
let getValueByPosition: (
startRow: number,
startColumn: number,
endRow: number,
endColumn: number
) => Nullable<ICellData>;
let getStyleByPosition: (
startRow: number,
startColumn: number,
endRow: number,
endColumn: number
) => Nullable<IStyleData>;
let mockHoverManagerService: Partial<HoverManagerService>;
let currentCell$: Subject<Nullable<IHoverCellPosition>>;
let sheetHooks: FSheetHooks;
let workbook: Workbook;

beforeEach(() => {
// Initialize the subject
currentCell$ = new Subject<Nullable<IHoverCellPosition>>();

// Create a mock HoverManagerService with currentCell$
mockHoverManagerService = {
currentCell$: currentCell$.asObservable(),
};

const testBed = createTestBed(undefined, [
[HoverManagerService, { useValue: mockHoverManagerService }],
]);
get = testBed.get;
injector = testBed.injector;
univerAPI = testBed.univerAPI;
sheet = testBed.sheet;
workbook = get(IUniverInstanceService).getCurrentUnitForType<Workbook>(UniverInstanceType.UNIVER_SHEET)!;

commandService = get(ICommandService);
commandService.registerCommand(SetRangeValuesCommand);
commandService.registerCommand(SetRangeValuesMutation);
commandService.registerCommand(SetStyleCommand);
commandService.registerCommand(SetVerticalTextAlignCommand);
commandService.registerCommand(SetHorizontalTextAlignCommand);
commandService.registerCommand(SetTextWrapCommand);

getValueByPosition = (
startRow: number,
startColumn: number,
endRow: number,
endColumn: number
): Nullable<ICellData> =>
get(IUniverInstanceService)
.getUniverSheetInstance('test')
?.getSheetBySheetId('sheet1')
?.getRange(startRow, startColumn, endRow, endColumn)
.getValue();

getStyleByPosition = (
startRow: number,
startColumn: number,
endRow: number,
endColumn: number
): Nullable<IStyleData> => {
const value = getValueByPosition(startRow, startColumn, endRow, endColumn);
const styles = get(IUniverInstanceService).getUniverSheetInstance('test')?.getStyles();
if (value && styles) {
return styles.getStyleByCell(value);
}
};

sheetHooks = injector.createInstance(FSheetHooks);
});

it('Test onCellPointerMove', () => {
sheetHooks.onCellPointerMove((cellPos) => {
expect(cellPos).toEqual({ location: { workbook, worksheet, unitId: workbook.getUnitId(), subUnitId: worksheet.getSheetId(), row: 0, col: 0 }, position: { startX: 0, endX: 1, startY: 0, endY: 1 } });
});

// Trigger the Observable to emit a new value
const worksheet = workbook.getActiveSheet();
const unitId = workbook.getUnitId();
const subUnitId = worksheet.getSheetId();
currentCell$.next({ location: { workbook, worksheet, unitId, subUnitId, row: 0, col: 0 }, position: { startX: 0, endX: 1, startY: 0, endY: 1 } });
});
});
36 changes: 36 additions & 0 deletions packages/facade/src/apis/sheets/f-sheet-hooks.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/**
* Copyright 2023-present DreamNum Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import { type Nullable, toDisposable } from '@univerjs/core';
import type { IDisposable } from '@wendellhu/redi';
import { Inject } from '@wendellhu/redi';

import type { IHoverCellPosition } from '@univerjs/sheets-ui';
import { HoverManagerService } from '@univerjs/sheets-ui';

export class FSheetHooks {
constructor(
@Inject(HoverManagerService) private readonly _hoverManagerService: HoverManagerService
) { }

/**
* Subscribe to the location information of the currently hovered cell
* @returns
*/
onCellPointerMove(callback: (cellPos: Nullable<IHoverCellPosition>) => void): IDisposable {
return toDisposable(this._hoverManagerService.currentCell$.subscribe(callback));
}
}
1 change: 1 addition & 0 deletions packages/sheets-ui/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,3 +88,4 @@ export { mergeSetRangeValues } from './services/clipboard/utils';
export type { IAutoFillLocation } from './services/auto-fill/type';
export type { IDiscreteRange } from './controllers/utils/range-tools';
export { virtualizeDiscreteRanges, rangeToDiscreteRange } from './controllers/utils/range-tools';
export { type IHoverCellPosition } from './services/hover-manager.service';

0 comments on commit 476ffd3

Please sign in to comment.