Skip to content

Commit abae87c

Browse files
committed
fix: tests
1 parent 45aa04d commit abae87c

File tree

4 files changed

+52
-43
lines changed

4 files changed

+52
-43
lines changed

src/hammer.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ function createEnabler(chart: Chart, state: State) {
2222
event.pointerType === 'mouse' &&
2323
(keyNotPressed(getModifierKey(panOptions), srcEvent) || keyPressed(getModifierKey(zoomOptions.drag), srcEvent))
2424
) {
25-
panOptions.onPanRejected?.({ chart, event: event.srcEvent })
25+
panOptions.onPanRejected?.({ chart, event })
2626
return false
2727
}
2828
return true
@@ -111,8 +111,8 @@ function startPan(chart: Chart, state: State, event: HammerInput) {
111111
y: event.center.y - rect.top,
112112
}
113113

114-
if (onPanStart?.({ chart, event: event.srcEvent, point }) === false) {
115-
return onPanRejected?.({ chart, event: event.srcEvent })
114+
if (onPanStart?.({ chart, event, point }) === false) {
115+
return onPanRejected?.({ chart, event })
116116
}
117117

118118
state.panScales = getEnabledScalesByPoint(state.options.pan, point, chart)

src/options.ts

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,13 @@ export type ModifierKey = 'ctrl' | 'alt' | 'shift' | 'meta'
77
export type DrawTime = 'afterDraw' | 'afterDatasetsDraw' | 'beforeDraw' | 'beforeDatasetsDraw'
88
export type ZoomTrigger = 'api' | 'drag' | 'wheel' | 'pinch'
99

10-
type RejectableStartEvent = (context: { chart: Chart; event: Event; point: Point }) => boolean | undefined
11-
type RejectEvent = (context: { chart: Chart; event: Event }) => void
10+
type RejectableStartEvent<T = Event | HammerInput> = (context: {
11+
chart: Chart
12+
event: T
13+
point: Point
14+
}) => boolean | undefined
15+
type RejectEvent<T = Event | HammerInput> = (context: { chart: Chart; event: T }) => void
16+
1217
type GenericEvent = (context: { chart: Chart }) => void
1318

1419
export interface WheelOptions {
@@ -125,9 +130,9 @@ export interface ZoomOptions {
125130
/**
126131
* Function called when wheel input occurs without modifier key
127132
*/
128-
onZoomRejected?: RejectEvent
133+
onZoomRejected?: RejectEvent<Event>
129134

130-
onZoomStart?: RejectableStartEvent
135+
onZoomStart?: RejectableStartEvent<Event>
131136
}
132137

133138
/**
@@ -178,9 +183,9 @@ export interface PanOptions {
178183
* Function called when pan fails because modifier key was not detected.
179184
* event is the Hammer event that failed - see https://hammerjs.github.io/api#event-object
180185
*/
181-
onPanRejected?: RejectEvent
186+
onPanRejected?: RejectEvent<HammerInput>
182187

183-
onPanStart?: RejectableStartEvent
188+
onPanStart?: RejectableStartEvent<HammerInput>
184189
}
185190

186191
export interface ScaleLimits {

src/utils.ts

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -60,13 +60,15 @@ const convertOverScaleMode = (
6060
scaleEnabled: { x: boolean; y: boolean },
6161
enabled: { x: boolean; y: boolean }
6262
) => {
63-
if (overScaleMode) {
64-
const overScaleEnabled = directionsEnabled(overScaleMode, chart)
65-
for (const axis of ['x', 'y'] as const) {
66-
if (overScaleEnabled[axis]) {
67-
scaleEnabled[axis] = enabled[axis]
68-
enabled[axis] = false
69-
}
63+
if (!overScaleMode) {
64+
return
65+
}
66+
67+
const overScaleEnabled = directionsEnabled(overScaleMode, chart)
68+
for (const axis of ['x', 'y'] as const) {
69+
if (overScaleEnabled[axis]) {
70+
scaleEnabled[axis] = enabled[axis]
71+
enabled[axis] = false
7072
}
7173
}
7274
}

test/types/exports.ts

Lines changed: 29 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
1-
import {Chart} from 'chart.js';
2-
import Zoom, {pan, zoom, resetZoom} from '../../src';
1+
import { Chart } from 'chart.js'
2+
import Zoom, { pan, zoom, resetZoom } from '../../src'
33

4-
Chart.register(Zoom);
5-
Chart.unregister(Zoom);
4+
Chart.register(Zoom)
5+
Chart.unregister(Zoom)
66

77
const chart = new Chart('id', {
88
type: 'bar',
99
data: {
1010
labels: [],
11-
datasets: [{
12-
data: []
13-
}]
11+
datasets: [
12+
{
13+
data: [],
14+
},
15+
],
1416
},
1517
options: {
1618
plugins: {
@@ -19,45 +21,45 @@ const chart = new Chart('id', {
1921
x: {
2022
min: 1,
2123
max: 2,
22-
minRange: 1
24+
minRange: 1,
2325
},
2426
y: {
2527
min: 1,
2628
max: 2,
27-
minRange: 1
29+
minRange: 1,
2830
},
2931
y2: {
3032
min: 10,
3133
max: 20,
32-
minRange: 5
33-
}
34+
minRange: 5,
35+
},
3436
},
3537
pan: {
3638
enabled: true,
3739
mode: 'x',
38-
onPanStart({event}) {
39-
return event.distance > 2;
40-
}
40+
onPanStart({ event }) {
41+
return event.distance > 2
42+
},
4143
},
4244
zoom: {
4345
wheel: {
44-
enabled: true
46+
enabled: true,
4547
},
4648
mode: 'x',
47-
}
49+
},
4850
},
49-
}
51+
},
5052
},
51-
plugins: [Zoom]
52-
});
53+
plugins: [Zoom],
54+
})
5355

54-
chart.resetZoom();
55-
chart.zoom(1.1);
56-
chart.zoom({x: 1, y: 1.1, focalPoint: {x: 10, y: 10}}, 'zoom');
56+
chart.resetZoom()
57+
chart.zoom(1.1)
58+
chart.zoom({ x: 1, y: 1.1, focalPoint: { x: 10, y: 10 } }, 'zoom')
5759

58-
chart.pan(10);
59-
chart.pan({x: 10, y: 20}, [chart.scales.x]);
60+
chart.pan(10)
61+
chart.pan({ x: 10, y: 20 }, [chart.scales.x])
6062

61-
pan(chart, -42, undefined, 'zoom');
62-
zoom(chart, {x: 1, y: 1.1, focalPoint: {x: 10, y: 10}}, 'zoom');
63-
resetZoom(chart, 'none');
63+
pan(chart, -42, undefined, 'zoom')
64+
zoom(chart, { x: 1, y: 1.1, focalPoint: { x: 10, y: 10 } }, 'zoom')
65+
resetZoom(chart, 'none')

0 commit comments

Comments
 (0)