Skip to content

Commit 6cae64d

Browse files
Return trigger, delta and amount in onZoom and onPan callbacks.
- onPan/Zoom, return the amount panned/zoomed - onPan: Introduce PanTrigger to allow distinguishing between user and api calls much like for ZoomTrigger.
1 parent 137d924 commit 6cae64d

File tree

3 files changed

+8
-7
lines changed

3 files changed

+8
-7
lines changed

src/core.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { panFunctions, updateRange, zoomFunctions, zoomRectFunctions } from './s
33
import { getState, type OriginalScaleLimits, type ScaleRange, type State, type UpdatedScaleLimits } from './state.js'
44
import { directionEnabled, getEnabledScalesByPoint } from './utils.js'
55
import type { Chart, Point, Scale, UpdateMode } from 'chart.js'
6-
import type { LimitOptions, ZoomTrigger } from './options.js'
6+
import type { LimitOptions, PanTrigger, ZoomTrigger } from './options.js'
77
import type { ZoomAmount } from './types.js'
88

99
function shouldUpdateScaleLimits(
@@ -89,7 +89,7 @@ export function zoom(chart: Chart, amount: ZoomAmount, transition: UpdateMode =
8989

9090
chart.update(transition)
9191

92-
zoomOptions?.onZoom?.({ chart, trigger })
92+
zoomOptions?.onZoom?.({ chart, trigger, amount: { x, y, focalPoint } })
9393
}
9494

9595
export function zoomRect(
@@ -207,7 +207,7 @@ function panScale(scale: Scale, delta: number, limits: LimitOptions, state: Stat
207207

208208
type PanAmount = number | Partial<Point>
209209

210-
export function pan(chart: Chart, delta: PanAmount, enabledScales?: Scale[], transition: UpdateMode = 'none') {
210+
export function pan(chart: Chart, delta: PanAmount, enabledScales?: Scale[], transition: UpdateMode = 'none', trigger: PanTrigger = "other") {
211211
const { x = 0, y = 0 } = typeof delta === 'number' ? { x: delta, y: delta } : delta
212212
const state = getState(chart)
213213
const {
@@ -232,7 +232,7 @@ export function pan(chart: Chart, delta: PanAmount, enabledScales?: Scale[], tra
232232

233233
chart.update(transition)
234234

235-
onPan?.({ chart })
235+
onPan?.({ chart, trigger, delta: { x, y } })
236236
}
237237

238238
export function getInitialScaleBounds(chart: Chart) {

src/options.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ export type ModeOption = Mode | ModeFn
66
export type ModifierKey = 'ctrl' | 'alt' | 'shift' | 'meta'
77
export type DrawTime = 'afterDraw' | 'afterDatasetsDraw' | 'beforeDraw' | 'beforeDatasetsDraw'
88
export type ZoomTrigger = 'api' | 'drag' | 'wheel' | 'pinch'
9+
export type PanTrigger = 'api' | 'drag' | 'wheel' | 'other'
910

1011
type RejectableStartEvent<T = Event | HammerInput> = (context: {
1112
chart: Chart
@@ -120,7 +121,7 @@ export interface ZoomOptions {
120121
/**
121122
* Function called while the user is zooming
122123
*/
123-
onZoom?: (context: { chart: Chart; trigger: ZoomTrigger }) => void
124+
onZoom?: (context: { chart: Chart; trigger: ZoomTrigger; amount?: { x: number, y: number } & { focalPoint: Point } }) => void
124125

125126
/**
126127
* Function called once zooming is completed
@@ -172,7 +173,7 @@ export interface PanOptions {
172173
/**
173174
* Function called while the user is panning
174175
*/
175-
onPan?: GenericEvent
176+
onPan?: (context: { chart: Chart; trigger: PanTrigger; delta: { x: number, y: number } }) => void
176177

177178
/**
178179
* Function called once panning is completed

src/plugin.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ function draw(chart: Chart, caller: string, options: ZoomPluginOptions) {
5050
}
5151

5252
const bindApi = (chart: Chart) => {
53-
chart.pan = (delta, panScales, transition) => pan(chart, delta, panScales, transition)
53+
chart.pan = (delta, panScales, transition) => pan(chart, delta, panScales, transition, "api")
5454
chart.zoom = (args, transition) => zoom(chart, args, transition)
5555
chart.zoomRect = (p0, p1, transition) => zoomRect(chart, p0, p1, transition)
5656
chart.zoomScale = (id, range, transition) => zoomScale(chart, id, range, transition)

0 commit comments

Comments
 (0)