From 3919056145861a18bc11a1718f3507ab3a3f751b Mon Sep 17 00:00:00 2001 From: ijlal99 Date: Tue, 24 Dec 2024 19:40:25 +0500 Subject: [PATCH] Context menu appears after each zoom or pan --- src/plugins/lib/html/MenuEvent.js | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/src/plugins/lib/html/MenuEvent.js b/src/plugins/lib/html/MenuEvent.js index 39946a013..05b419ca1 100644 --- a/src/plugins/lib/html/MenuEvent.js +++ b/src/plugins/lib/html/MenuEvent.js @@ -10,14 +10,17 @@ export function addContextMenuListener(elem, callback) { const touchStartHandler = (event) => { event.preventDefault(); - const touch = event.touches[0]; - startX = touch.clientX; - startY = touch.clientY; - + if (timeout) { clearTimeout(timeout); timeout = null; } + // If more than one finger touches the screen, cancel the timeout + if (event.touches.length > 1) return; + + const touch = event.touches[0]; + startX = touch.clientX; + startY = touch.clientY; timeout = setTimeout(() => { event.clientX = touch.clientX; @@ -28,6 +31,14 @@ export function addContextMenuListener(elem, callback) { }, longPressTimer); }; + const globalTouchStartHandler = (event) => { + // Cancel timeout if multiple touches are detected anywhere on the screen + if (event.touches.length > 1 && timeout) { + clearTimeout(timeout); + timeout = null; + } + }; + const touchMoveHandler = (event) => { if (!timeout) return; const touch = event.touches[0]; @@ -58,6 +69,7 @@ export function addContextMenuListener(elem, callback) { elem.addEventListener('touchstart', touchStartHandler); elem.addEventListener('touchmove', touchMoveHandler); elem.addEventListener('touchend', touchEndHandler); + window.addEventListener('touchstart', globalTouchStartHandler); } else { elem.addEventListener('contextmenu', contextMenuHandler); } @@ -67,6 +79,7 @@ export function addContextMenuListener(elem, callback) { elem.removeEventListener('touchstart', touchStartHandler); elem.removeEventListener('touchmove', touchMoveHandler); elem.removeEventListener('touchend', touchEndHandler); + window.removeEventListener('touchstart', globalTouchStartHandler); } else { elem.removeEventListener('contextmenu', contextMenuHandler); }