Skip to content

Commit bcfe50c

Browse files
committed
refactor(image-cropper): prefer dom events
1 parent 1f33d1c commit bcfe50c

File tree

3 files changed

+66
-39
lines changed

3 files changed

+66
-39
lines changed

packages/machines/image-cropper/src/image-cropper.connect.ts

Lines changed: 0 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -232,43 +232,6 @@ export function connect<T extends PropTypes>(
232232
const point = getEventPoint(event)
233233
send({ type: "PAN_POINTER_DOWN", point })
234234
},
235-
onWheel(event) {
236-
const viewportEl = event.currentTarget
237-
const rect = viewportEl.getBoundingClientRect()
238-
const point = {
239-
x: event.clientX - rect.left,
240-
y: event.clientY - rect.top,
241-
}
242-
send({ type: "ZOOM", trigger: "wheel", delta: event.deltaY, point })
243-
},
244-
onTouchStart(event) {
245-
if (event.touches.length >= 2) {
246-
const touches = Array.from(event.touches).map((touch) => ({
247-
x: touch.clientX,
248-
y: touch.clientY,
249-
}))
250-
251-
send({ type: "PINCH_START", touches })
252-
}
253-
},
254-
onTouchMove(event) {
255-
if (event.touches.length >= 2) {
256-
event.preventDefault()
257-
258-
const touches = Array.from(event.touches).map((touch) => ({
259-
x: touch.clientX,
260-
y: touch.clientY,
261-
}))
262-
263-
send({ type: "PINCH_MOVE", touches })
264-
}
265-
},
266-
267-
onTouchEnd(event) {
268-
if (event.touches.length < 2) {
269-
send({ type: "PINCH_END" })
270-
}
271-
},
272235
})
273236
},
274237

packages/machines/image-cropper/src/image-cropper.machine.ts

Lines changed: 65 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ export const machine = createMachine<ImageCropperSchema>({
187187
states: {
188188
idle: {
189189
entry: ["checkImageStatus"],
190-
effects: ["trackViewportResize"],
190+
effects: ["trackViewportResize", "trackWheelEvent", "trackTouchEvents"],
191191
on: {
192192
SET_NATURAL_SIZE: {
193193
actions: ["setNaturalSize"],
@@ -810,6 +810,70 @@ export const machine = createMachine<ImageCropperSchema>({
810810
resizeObserver.disconnect()
811811
}
812812
},
813+
814+
trackWheelEvent({ scope, send }) {
815+
const viewportEl = dom.getViewportEl(scope)
816+
if (!viewportEl) return
817+
818+
function onWheel(event: WheelEvent) {
819+
event.preventDefault()
820+
821+
if (!viewportEl) return
822+
823+
const rect = viewportEl.getBoundingClientRect()
824+
const point = {
825+
x: event.clientX - rect.left,
826+
y: event.clientY - rect.top,
827+
}
828+
829+
send({ type: "ZOOM", trigger: "wheel", delta: event.deltaY, point })
830+
}
831+
832+
return addDomEvent(viewportEl, "wheel", onWheel, { passive: false })
833+
},
834+
835+
trackTouchEvents({ scope, send }) {
836+
const viewportEl = dom.getViewportEl(scope)
837+
if (!viewportEl) return
838+
839+
function onTouchStart(event: TouchEvent) {
840+
if (event.touches.length >= 2) {
841+
event.preventDefault()
842+
843+
const touches = Array.from(event.touches).map((touch) => ({
844+
x: touch.clientX,
845+
y: touch.clientY,
846+
}))
847+
848+
send({ type: "PINCH_START", touches })
849+
}
850+
}
851+
852+
function onTouchMove(event: TouchEvent) {
853+
if (event.touches.length >= 2) {
854+
event.preventDefault()
855+
856+
const touches = Array.from(event.touches).map((touch) => ({
857+
x: touch.clientX,
858+
y: touch.clientY,
859+
}))
860+
861+
send({ type: "PINCH_MOVE", touches })
862+
}
863+
}
864+
865+
function onTouchEnd(event: TouchEvent) {
866+
if (event.touches.length < 2) {
867+
send({ type: "PINCH_END" })
868+
}
869+
}
870+
871+
return callAll(
872+
addDomEvent(viewportEl, "touchstart", onTouchStart, { passive: false }),
873+
addDomEvent(viewportEl, "touchmove", onTouchMove, { passive: false }),
874+
addDomEvent(viewportEl, "touchend", onTouchEnd),
875+
)
876+
},
813877
},
814878
},
815879
})

website/next-env.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/// <reference types="next" />
22
/// <reference types="next/image-types/global" />
3-
import "./.next/types/routes.d.ts"
3+
import "./.next/dev/types/routes.d.ts"
44

55
// NOTE: This file should not be edited
66
// see https://nextjs.org/docs/pages/api-reference/config/typescript for more information.

0 commit comments

Comments
 (0)