[Canvas] unify pointer event x/y origin logic for localMousePosition calculation (#28748) (#31791)

This commit is contained in:
Robert Monfera 2019-02-22 11:20:04 +01:00 committed by GitHub
parent 1c97901299
commit bb06b0a0c9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -28,7 +28,7 @@ const resetHandler = () => {
window.onmouseup = null;
};
const setupHandler = (commit, target) => {
const setupHandler = (commit, target, initialCallback, initialClientX, initialClientY) => {
// Ancestor has to be identified on setup, rather than 1st interaction, otherwise events may be triggered on
// DOM elements that had been removed: kibana-canvas github issue #1093
const canvasPage = ancestorElement(target);
@ -63,6 +63,10 @@ const setupHandler = (commit, target) => {
commit('mouseEvent', { event: 'mouseUp', x, y, altKey, metaKey, shiftKey, ctrlKey });
resetHandler();
};
if (typeof initialCallback === 'function' && !isNaN(initialClientX) && !isNaN(initialClientY)) {
const { x, y } = localMousePosition(canvasOrigin, initialClientX, initialClientY);
initialCallback(x, y);
}
};
const handleMouseMove = (
@ -72,9 +76,13 @@ const handleMouseMove = (
) => {
// mouse move must be handled even before an initial click
if (!window.onmousemove && isEditable) {
const { x, y } = localMousePosition(target, clientX, clientY);
setupHandler(commit, target);
commit('cursorPosition', { x, y, altKey, metaKey, shiftKey, ctrlKey });
setupHandler(
commit,
target,
(x, y) => commit('cursorPosition', { x, y, altKey, metaKey, shiftKey, ctrlKey }),
clientX,
clientY
);
}
};
@ -85,9 +93,13 @@ const handleWheel = (
) => {
// new mouse position must be registered when page scrolls
if (isEditable) {
const { x, y } = localMousePosition(target, clientX, clientY);
setupHandler(commit, target);
commit('cursorPosition', { x, y, altKey, metaKey, shiftKey, ctrlKey });
setupHandler(
commit,
target,
(x, y) => commit('cursorPosition', { x, y, altKey, metaKey, shiftKey, ctrlKey }),
clientX,
clientY
);
}
};
@ -102,9 +114,14 @@ const handleMouseDown = (commit, e, isEditable) => {
if (!ancestor) {
return;
}
const { x, y } = localMousePosition(ancestor, clientX, clientY);
setupHandler(commit, ancestor);
commit('mouseEvent', { event: 'mouseDown', x, y, altKey, metaKey, shiftKey, ctrlKey });
setupHandler(
commit,
ancestor,
(x, y) =>
commit('mouseEvent', { event: 'mouseDown', x, y, altKey, metaKey, shiftKey, ctrlKey }),
clientX,
clientY
);
};
const keyCode = key => (key === 'Meta' ? 'MetaLeft' : 'Key' + key.toUpperCase());