Skip to content

Commit 4434818

Browse files
authored
Fix long-press to open contextmenu on touch devices (#3624)
1 parent 4e12800 commit 4434818

File tree

1 file changed

+22
-8
lines changed

1 file changed

+22
-8
lines changed

src/extensions/core/simpleTouchSupport.ts

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -61,13 +61,19 @@ app.registerExtension({
6161
if (touchTime && !e.touches?.length) {
6262
if (new Date().getTime() - touchTime.getTime() > 600) {
6363
if (e.target === app.canvasEl) {
64-
app.canvasEl.dispatchEvent(
65-
new PointerEvent('pointerdown', {
66-
button: 2,
67-
clientX: e.changedTouches[0].clientX,
68-
clientY: e.changedTouches[0].clientY
69-
})
70-
)
64+
const touch = {
65+
button: 2, // Right click
66+
clientX: e.changedTouches[0].clientX,
67+
clientY: e.changedTouches[0].clientY,
68+
pointerId: 1, // changedTouches' id is 0, set it to any number
69+
isPrimary: true // changedTouches' isPrimary is false, so set it to true
70+
}
71+
// context menu info set in 'pointerdown' event
72+
app.canvasEl.dispatchEvent(new PointerEvent('pointerdown', touch))
73+
// then, context menu open after 'pointerup' event
74+
setTimeout(() => {
75+
app.canvasEl.dispatchEvent(new PointerEvent('pointerup', touch))
76+
})
7177
e.preventDefault()
7278
}
7379
}
@@ -79,7 +85,15 @@ app.registerExtension({
7985
app.canvasEl.parentElement?.addEventListener(
8086
'touchmove',
8187
(e) => {
82-
touchTime = null
88+
// make a threshold for touchmove to prevent clear touchTime for long press
89+
if (touchTime && lastTouch && e.touches?.length === 1) {
90+
const onlyTouch = e.touches[0]
91+
const deltaX = onlyTouch.clientX - lastTouch.clientX
92+
const deltaY = onlyTouch.clientY - lastTouch.clientY
93+
if (deltaX * deltaX + deltaY * deltaY > 30) {
94+
touchTime = null
95+
}
96+
}
8397
if (e.touches?.length === 2 && lastTouch && !e.ctrlKey && !e.shiftKey) {
8498
e.preventDefault() // Prevent browser from zooming when two textareas are touched
8599
app.canvas.pointer.isDown = false

0 commit comments

Comments
 (0)