Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FIX] Fix interference between ContextMenus on measurements and Canvas #1462

Merged
merged 1 commit into from
Apr 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 33 additions & 5 deletions examples/measurement/distance_createWithMouse_snapping.html
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ <h3>Assets</h3>
// Import the modules we need for this example
//------------------------------------------------------------------------------------------------------------------

import {Viewer, XKTLoaderPlugin, ContextMenu, DistanceMeasurementsPlugin, DistanceMeasurementsMouseControl, PointerLens} from "../../dist/xeokit-sdk.min.es.js";
import {Viewer, XKTLoaderPlugin, ContextMenu, DistanceMeasurementsPlugin, DistanceMeasurementsMouseControl, PointerLens} from "../../dist/xeokit-sdk.es.js";

//------------------------------------------------------------------------------------------------------------------
// Create a Viewer and arrange the camera
Expand Down Expand Up @@ -282,10 +282,13 @@ <h3>Assets</h3>
]
});

// viewer.cameraControl.on("rightClick", function (e) {
// canvasContextMenu.show(e.pagePos[0], e.pagePos[1]);
// e.event.preventDefault();
// });
viewer.scene.canvas.canvas.addEventListener('contextmenu', (event) => {
const canvasPos = getCanvasPosFromEvent(event);
console.log("canvas context menu")
canvasContextMenu.show(canvasPos[0], canvasPos[1]);
event.event.preventDefault();
event.event.stopPropagation();
});

//------------------------------------------------------------------------------------------------------------------
// Create an DistanceMeasurementsPlugin, activate its DistanceMeasuremntsControl
Expand All @@ -312,6 +315,31 @@ <h3>Assets</h3>
e.event.preventDefault();
});

const getCanvasPosFromEvent = function (event) {
const canvasPos = [];
if (!event) {
event = window.event;
canvasPos[0] = event.x;
canvasPos[1] = event.y;
} else {
let element = event.target;
let totalOffsetLeft = 0;
let totalOffsetTop = 0;
let totalScrollX = 0;
let totalScrollY = 0;
while (element.offsetParent) {
totalOffsetLeft += element.offsetLeft;
totalOffsetTop += element.offsetTop;
totalScrollX += element.scrollLeft;
totalScrollY += element.scrollTop;
element = element.offsetParent;
}
canvasPos[0] = event.pageX + totalScrollX - totalOffsetLeft;
canvasPos[1] = event.pageY + totalScrollY - totalOffsetTop;
}
return canvasPos;
};

window.viewer = viewer;

</script>
Expand Down
1 change: 1 addition & 0 deletions src/plugins/lib/html/Dot.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ class Dot {
dotClickable.addEventListener('contextmenu', (event) => {
cfg.onContextMenu(event, this);
event.preventDefault();
event.stopPropagation();
});
}

Expand Down
4 changes: 4 additions & 0 deletions src/plugins/lib/html/Label.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,14 @@ class Label {
if (cfg.onMouseDown) {
label.addEventListener('mousedown', (event) => {
cfg.onMouseDown(event, this);
event.stopPropagation();
});
}

if (cfg.onMouseUp) {
label.addEventListener('mouseup', (event) => {
cfg.onMouseUp(event, this);
event.stopPropagation();
});
}

Expand All @@ -85,6 +87,8 @@ class Label {
label.addEventListener('contextmenu', (event) => {
cfg.onContextMenu(event, this);
event.preventDefault();
event.stopPropagation();
console.log("Label context menu")
});
}
}
Expand Down
1 change: 1 addition & 0 deletions src/plugins/lib/html/Wire.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ class Wire {
wireClickable.addEventListener('contextmenu', (event) => {
cfg.onContextMenu(event, this);
event.preventDefault();
event.stopPropagation();
});
}

Expand Down
Loading