Skip to content

Commit e7a02bb

Browse files
CopilotAlexzjt
andauthored
fix(tooltip): 修复页面滚动时tooltip不自动隐藏的问题 (#3310)
* Initial plan * fix(tooltip): 修复页面滚动时tooltip不自动隐藏的问题 (#3307) Co-authored-by: Alexzjt <9548248+Alexzjt@users.noreply.github.com> * perf(tooltip): 对scroll事件处理器添加throttle优化性能 Co-authored-by: Alexzjt <9548248+Alexzjt@users.noreply.github.com> * fix(tooltip): 将scroll处理器存储为类属性以避免内存泄漏 Co-authored-by: Alexzjt <9548248+Alexzjt@users.noreply.github.com> --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: Alexzjt <9548248+Alexzjt@users.noreply.github.com>
1 parent 8be0068 commit e7a02bb

3 files changed

Lines changed: 17 additions & 1 deletion

File tree

‎packages/s2-core/__tests__/unit/interaction/event-controller-spec.ts‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,7 @@ describe('Interaction Event Controller Tests', () => {
204204
OriginEventType.KEY_UP,
205205
OriginEventType.POINTER_UP,
206206
OriginEventType.POINTER_MOVE,
207+
OriginEventType.SCROLL,
207208
];
208209

209210
expect(eventController.domEventListeners).toHaveLength(
@@ -542,6 +543,15 @@ describe('Interaction Event Controller Tests', () => {
542543
expect(spreadsheet.hideTooltip).toHaveBeenCalled();
543544
});
544545

546+
test('should hide tooltip when window scrolls', () => {
547+
spreadsheet.hideTooltip = jest.fn();
548+
549+
window.dispatchEvent(new Event('scroll'));
550+
551+
// throttle defaults to leading: true, so hideTooltip is called immediately
552+
expect(spreadsheet.hideTooltip).toHaveBeenCalled();
553+
});
554+
545555
test('should reset if current mouse inside the canvas container, but outside the panel facet', () => {
546556
spreadsheet.facet = {
547557
...spreadsheet.facet,

‎packages/s2-core/src/common/constant/events/origin.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,5 @@ export enum OriginEventType {
2424
POINTER_OVER = 'pointerover',
2525
RIGHT_DOWN = 'rightdown',
2626
RIGHT_UP = 'rightup',
27+
SCROLL = 'scroll',
2728
}

‎packages/s2-core/src/interaction/event-controller.ts‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import {
55
type Group,
66
type PointLike,
77
} from '@antv/g';
8-
import { each, get, hasIn, isEmpty, isFunction, isNil } from 'lodash';
8+
import { each, get, hasIn, isEmpty, isFunction, isNil, throttle } from 'lodash';
99
import { GuiIcon, InteractionName } from '../common';
1010
import {
1111
CellType,
@@ -58,6 +58,10 @@ export class EventController {
5858

5959
public canvasMousemoveEvent: CanvasEvent;
6060

61+
private onScroll = throttle(() => {
62+
this.spreadsheet.hideTooltip();
63+
}, 16);
64+
6165
constructor(spreadsheet: SpreadSheet) {
6266
this.spreadsheet = spreadsheet;
6367
this.bindEvents();
@@ -126,6 +130,7 @@ export class EventController {
126130
this.addDomEventListener(window, OriginEventType.POINTER_MOVE, (event) => {
127131
this.spreadsheet.emit(S2Event.GLOBAL_MOUSE_MOVE, event);
128132
});
133+
this.addDomEventListener(window, OriginEventType.SCROLL, this.onScroll);
129134
}
130135

131136
// 不能单独判断是否 Image Shape, 用户如果自定义单元格绘制图片, 会导致判断错误

0 commit comments

Comments
 (0)