Skip to content

Commit 3b50d7b

Browse files
authored
fix: stop updating slider value when the drag target changes (#364)
* fix: stop updating slider value when the drag target changes * fix: use viewport coordinates and remove drag target check
1 parent 8b2e0d9 commit 3b50d7b

2 files changed

Lines changed: 28 additions & 4 deletions

File tree

src/ui/slider/index.ts

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,15 @@ import { transition } from '../../animation';
66
import { Component } from '../../core';
77
import { Group, Rect, Text } from '../../shapes';
88
import type { Selection } from '../../util';
9-
import { getEventPos, ifShow, parseSeriesAttr, select, subStyleProps, superStyleProps, toPrecision } from '../../util';
9+
import {
10+
getEventViewportPos,
11+
ifShow,
12+
parseSeriesAttr,
13+
select,
14+
subStyleProps,
15+
superStyleProps,
16+
toPrecision,
17+
} from '../../util';
1018
import type { SparklineStyleProps } from '../sparkline';
1119
import { Sparkline } from '../sparkline';
1220
import { CLASS_NAMES, HANDLE_DEFAULT_CFG, HANDLE_ICON_DEFAULT_CFG, HANDLE_LABEL_DEFAULT_CFG } from './constant';
@@ -556,7 +564,7 @@ export class Slider extends Component<SliderStyleProps> {
556564
private onDragStart = (target: string) => (e: any) => {
557565
e.stopPropagation();
558566
this.target = target;
559-
this.prevPos = this.getOrientVal(getEventPos(e));
567+
this.prevPos = this.getOrientVal(getEventViewportPos(e));
560568
const { x, y } = this.availableSpace;
561569
const { x: X, y: Y } = this.getBBox();
562570
this.selectionStartPos = this.getRatio(this.prevPos - this.getOrientVal([x, y]) - this.getOrientVal([+X!, +Y!]));
@@ -565,11 +573,11 @@ export class Slider extends Component<SliderStyleProps> {
565573
document.addEventListener('pointerup', this.onDragEnd);
566574
};
567575

568-
private onDragging = (e: any) => {
576+
private onDragging = (e: PointerEvent) => {
569577
const { slidable, brushable, type } = this.attributes;
570578
e.stopPropagation();
571579

572-
const currPos = this.getOrientVal(getEventPos(e));
580+
const currPos = this.getOrientVal(getEventViewportPos(e));
573581
const diffPos = currPos - this.prevPos;
574582

575583
if (!diffPos) return;

src/util/event.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,19 @@ export function getEventPos(e: any): [number, number] {
1414
if (offsetX && offsetY) return [offsetX, offsetY];
1515
return [0, 0];
1616
}
17+
18+
/**
19+
* 获得触发事件相对于页面 viewport 的坐标
20+
*/
21+
export function getEventViewportPos(e: any): [number, number] {
22+
const { nativeEvent, touches, clientX, clientY } = e;
23+
if (nativeEvent) {
24+
return [nativeEvent.clientX, nativeEvent.clientY];
25+
}
26+
if (touches) {
27+
const { clientX, clientY } = touches[0];
28+
return [clientX, clientY];
29+
}
30+
if (typeof clientX === 'number' && typeof clientY === 'number') return [clientX, clientY];
31+
return [0, 0];
32+
}

0 commit comments

Comments
 (0)