From bf56fc0ecbbf7e09c853e119846a4f4bf8c0ab4e Mon Sep 17 00:00:00 2001 From: claudijo Date: Wed, 23 Mar 2022 18:48:13 +0300 Subject: [PATCH] Some refactoring of event handling code --- dist/index.cjs.js | 22 ++++++++-------------- dist/index.esm.js | 22 ++++++++-------------- lib/components/layer.js | 19 +++++++------------ 3 files changed, 23 insertions(+), 40 deletions(-) diff --git a/dist/index.cjs.js b/dist/index.cjs.js index 6477ce0..d6d9c06 100644 --- a/dist/index.cjs.js +++ b/dist/index.cjs.js @@ -825,26 +825,20 @@ function Layer(_ref) { var rect = event.target.getBoundingClientRect(); var point = localCoordinatesFromEvent(rect, event, scale); - var childTarget = getEventTargetAt(point); // Handle mouse event for Layer component by calling corresponding passed - // event handler - - var handlers = Object.keys(rest).reduce(function (acc, key) { - acc[key.toLowerCase()] = rest[key]; - return acc; - }, {}); + var childTarget = getEventTargetAt(point); var eventInit = _objectSpread2(_objectSpread2({}, mouseEventInit(event)), {}, { clientX: point.x, clientY: point.y - }); - - var handler = handlers['on' + event.type]; + }); // Handle mouse event for Layer component by calling corresponding passed + // event handler - if (handler) { - // FIXME: This event will not have correct event target, since it is not dispatched - handler(new MouseEvent(event.type, eventInit)); - } // Handle mouse events for child components + Object.keys(rest).forEach(function (key) { + if (key.toLowerCase() === "on".concat(event.type)) { + rest[key](new MouseEvent(event.type, eventInit)); + } + }); // Handle mouse events for child components if (childTarget) { childTarget.dispatchEvent(new MouseEvent(event.type, _objectSpread2(_objectSpread2({}, mouseEventInit(event)), {}, { diff --git a/dist/index.esm.js b/dist/index.esm.js index dc3b748..4c8b55a 100644 --- a/dist/index.esm.js +++ b/dist/index.esm.js @@ -817,26 +817,20 @@ function Layer(_ref) { var rect = event.target.getBoundingClientRect(); var point = localCoordinatesFromEvent(rect, event, scale); - var childTarget = getEventTargetAt(point); // Handle mouse event for Layer component by calling corresponding passed - // event handler - - var handlers = Object.keys(rest).reduce(function (acc, key) { - acc[key.toLowerCase()] = rest[key]; - return acc; - }, {}); + var childTarget = getEventTargetAt(point); var eventInit = _objectSpread2(_objectSpread2({}, mouseEventInit(event)), {}, { clientX: point.x, clientY: point.y - }); - - var handler = handlers['on' + event.type]; + }); // Handle mouse event for Layer component by calling corresponding passed + // event handler - if (handler) { - // FIXME: This event will not have correct event target, since it is not dispatched - handler(new MouseEvent(event.type, eventInit)); - } // Handle mouse events for child components + Object.keys(rest).forEach(function (key) { + if (key.toLowerCase() === "on".concat(event.type)) { + rest[key](new MouseEvent(event.type, eventInit)); + } + }); // Handle mouse events for child components if (childTarget) { childTarget.dispatchEvent(new MouseEvent(event.type, _objectSpread2(_objectSpread2({}, mouseEventInit(event)), {}, { diff --git a/lib/components/layer.js b/lib/components/layer.js index 337db06..4092c09 100644 --- a/lib/components/layer.js +++ b/lib/components/layer.js @@ -181,24 +181,19 @@ export default function Layer({ children, ...rest }) { const point = localCoordinatesFromEvent(rect, event, scale); const childTarget = getEventTargetAt(point); - // Handle mouse event for Layer component by calling corresponding passed - // event handler - const handlers = Object.keys(rest).reduce((acc, key) => { - acc[key.toLowerCase()] = rest[key]; - return acc; - }, {}); - const eventInit = { ...mouseEventInit(event), clientX: point.x, clientY: point.y, }; - const handler = handlers['on' + event.type]; - if (handler) { - // FIXME: This event will not have correct event target, since it is not dispatched - handler(new MouseEvent(event.type, eventInit)); - } + // Handle mouse event for Layer component by calling corresponding passed + // event handler + Object.keys(rest).forEach(key => { + if (key.toLowerCase() === `on${event.type}`) { + rest[key](new MouseEvent(event.type, eventInit)) + } + }); // Handle mouse events for child components if (childTarget) {