Skip to content

Commit

Permalink
Merge pull request #1768 from xeokit/fix/XEOK-173
Browse files Browse the repository at this point in the history
Context menu appears after each zoom or pan
  • Loading branch information
xeolabs authored Dec 24, 2024
2 parents 6194327 + 3919056 commit abb38ab
Showing 1 changed file with 17 additions and 4 deletions.
21 changes: 17 additions & 4 deletions src/plugins/lib/html/MenuEvent.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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];
Expand Down Expand Up @@ -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);
}
Expand All @@ -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);
}
Expand Down

0 comments on commit abb38ab

Please sign in to comment.