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: trigger hover after scroll #2235

Merged
merged 11 commits into from
Jun 2, 2023
50 changes: 50 additions & 0 deletions packages/s2-core/__tests__/spreadsheet/scroll-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,56 @@ describe('Scroll Tests', () => {
},
);

// https://github.com/antvis/S2/issues/2222
test.each([
{
type: 'horizontal',
offset: {
scrollX: 20,
scrollY: 0,
},
},
{
type: 'vertical',
offset: {
scrollX: 0,
scrollY: 20,
},
},
])(
'should trigger hover cells when hover cells after scroll by %o',
async ({ offset }) => {
s2.facet.cornerBBox.maxY = -9999;
s2.facet.panelBBox.minX = -9999;
s2.facet.panelBBox.minY = -9999;

const bbox = s2.getCanvasElement().getBoundingClientRect();
const mousemoveEvent = new MouseEvent('mousemove', {
clientX: bbox.left + 100,
clientY: bbox.top + 100,
});

canvas.dispatchEvent(mousemoveEvent);

s2.container.emit = jest.fn();

const wheelEvent = new WheelEvent('wheel', {
deltaX: offset.scrollX,
deltaY: offset.scrollY,
});

canvas.dispatchEvent(wheelEvent);

// wait requestAnimationFrame and debounce
await sleep(1000);

expect(s2.container.emit).toHaveBeenCalledWith(
OriginEventType.MOUSE_MOVE,
expect.any(Object),
);
},
);

test('should not trigger scroll event on passive renders', () => {
const sheet = new PivotSheet(getContainer(), mockDataConfig, {
...s2Options,
Expand Down
21 changes: 19 additions & 2 deletions packages/s2-core/src/facet/base-facet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import {
KEY_GROUP_CORNER_RESIZE_AREA,
KEY_GROUP_ROW_INDEX_RESIZE_AREA,
KEY_GROUP_ROW_RESIZE_AREA,
OriginEventType,
S2Event,
ScrollbarPositionType,
} from '../common/constant';
Expand Down Expand Up @@ -1346,10 +1347,26 @@ export abstract class BaseFacet {
}

protected onAfterScroll = debounce(() => {
const { interaction } = this.spreadsheet;
const { interaction, container } = this.spreadsheet;
// ε¦‚ζžœζ˜―ι€‰δΈ­ε•ε…ƒζ ΌηŠΆζ€, εˆ™η»§η»­δΏη•™ hover 拦ζˆͺ, ιΏε…ζ»šεŠ¨εŽ hover 清空已选单元格
if (!interaction.isSelectedState()) {
this.spreadsheet.interaction.removeIntercepts([InterceptType.HOVER]);
interaction.removeIntercepts([InterceptType.HOVER]);

// https://github.com/antvis/S2/issues/2222
const canvasMousemoveEvent =
interaction.eventController.canvasMousemoveEvent;
if (canvasMousemoveEvent) {
const { x, y } = canvasMousemoveEvent;
const shape = container.getShape(x, y);
if (shape) {
container.emit(OriginEventType.MOUSE_MOVE, {
...canvasMousemoveEvent,
shape,
target: shape,
timestamp: performance.now(),
});
}
}
}
}, 300);

Expand Down
6 changes: 5 additions & 1 deletion packages/s2-core/src/interaction/event-controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import {
import type { EmitterType, ResizeInfo } from '../common/interface';
import type { SpreadSheet } from '../sheet-type';
import { getSelectedData, keyEqualTo } from '../utils/export/copy';
import { getTooltipOptions, verifyTheElementInTooltip } from '../utils/tooltip';
import { verifyTheElementInTooltip } from '../utils/tooltip';

interface EventListener {
target: EventTarget;
Expand Down Expand Up @@ -51,6 +51,8 @@ export class EventController {

public isCanvasEffect = false;

public canvasMousemoveEvent: CanvasEvent;

constructor(spreadsheet: SpreadSheet) {
this.spreadsheet = spreadsheet;
this.bindEvents();
Expand Down Expand Up @@ -333,6 +335,8 @@ export class EventController {
};

private onCanvasMousemove = (event: CanvasEvent) => {
this.canvasMousemoveEvent = event;

if (this.isResizeArea(event)) {
this.activeResizeArea(event);
this.spreadsheet.emit(S2Event.LAYOUT_RESIZE_MOUSE_MOVE, event);
Expand Down