-
-
Notifications
You must be signed in to change notification settings - Fork 550
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(facade): add sheet hooks, onCellPointerMove hook (#2193)
- Loading branch information
Showing
5 changed files
with
166 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
118 changes: 118 additions & 0 deletions
118
packages/facade/src/apis/sheets/__tests__/f-sheet-hooks.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 } }); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters