Skip to content

Commit

Permalink
Some refactoring of event handling code
Browse files Browse the repository at this point in the history
  • Loading branch information
claudijo committed Mar 23, 2022
1 parent f3374d7 commit bf56fc0
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 40 deletions.
22 changes: 8 additions & 14 deletions dist/index.cjs.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)), {}, {
Expand Down
22 changes: 8 additions & 14 deletions dist/index.esm.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)), {}, {
Expand Down
19 changes: 7 additions & 12 deletions lib/components/layer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit bf56fc0

Please sign in to comment.