From b713857951ecb37647a06c601908765273f3d346 Mon Sep 17 00:00:00 2001 From: jberg Date: Tue, 13 Aug 2019 03:42:40 -0700 Subject: [PATCH] Fix errors when node is the top level container (#853) * Fix errors when node is the top level container * simplify by adding findParentOrCurrentNodeOfType --- modern/src/App.js | 2 +- modern/src/utils.js | 21 +++++++++++++++++---- 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/modern/src/App.js b/modern/src/App.js index 599e6760..53d6535f 100644 --- a/modern/src/App.js +++ b/modern/src/App.js @@ -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); diff --git a/modern/src/utils.js b/modern/src/utils.js index 2ebdde88..65bda88e 100644 --- a/modern/src/utils.js +++ b/modern/src/utils.js @@ -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;