Fix errors when node is the top level container (#853)

* Fix errors when node is the top level container

* simplify by adding findParentOrCurrentNodeOfType
This commit is contained in:
jberg 2019-08-13 03:42:40 -07:00 committed by Jordan Eldredge
parent fc2a015ae1
commit b713857951
2 changed files with 18 additions and 5 deletions

View file

@ -32,7 +32,7 @@ function handleMouseEventDispatch(node, event, eventName) {
// In order to properly calculate the x/y coordinates like MAKI does we need
// to find the container element and calculate based off of that
const container = Utils.findParentNodeOfType(node, ["container"]);
const container = Utils.findParentOrCurrentNodeOfType(node, "container");
const x = event.clientX - container.getleft();
const y = event.clientY - container.gettop();
node.js_trigger(eventName, x, y);

View file

@ -101,19 +101,32 @@ export function unimplementedWarning(name) {
}
// Operations on trees
function isNodeOfType(node, type) {
const isTypeArray = Array.isArray(type);
return (
(!isTypeArray && node.name === type) ||
(isTypeArray && type.includes(node.name))
);
}
export function findParentNodeOfType(node, type) {
let n = node;
while (n.parent) {
n = n.parent;
if (
(!Array.isArray(type) && n.name === type) ||
(Array.isArray(type) && type.includes(n.name))
) {
if (isNodeOfType(n, type)) {
return n;
}
}
}
export function findParentOrCurrentNodeOfType(node, type) {
if (isNodeOfType(node, type)) {
return node;
} else {
return findParentNodeOfType(node, type);
}
}
export function findDescendantByTypeAndId(node, type, id) {
if (node.children.length === 0) {
return null;