From e7eb0c150056f0afbe8a943c2d962dc415e2ff3d Mon Sep 17 00:00:00 2001 From: jberg Date: Sat, 7 Sep 2019 10:29:28 -0700 Subject: [PATCH] Implement mouse position (#908) * Implement mouse position * move getMousePosition to utils --- modern/src/App.js | 9 +-------- modern/src/maki-interpreter/objects.test.js | 4 ---- modern/src/runtime/Group.ts | 12 +++++------- modern/src/runtime/PopupMenu.ts | 2 ++ modern/src/runtime/System.ts | 16 +++++++++------- modern/src/utils.ts | 11 +++++++++++ 6 files changed, 28 insertions(+), 26 deletions(-) diff --git a/modern/src/App.js b/modern/src/App.js index 6defa008..b4b51611 100644 --- a/modern/src/App.js +++ b/modern/src/App.js @@ -56,8 +56,6 @@ function useJsUpdates(node) { useEffect(() => node.js_listen("js_update", forceUpdate)); } -let mouseposition; - function handleMouseEventDispatch(node, event, eventName) { event.stopPropagation(); @@ -73,10 +71,6 @@ function handleMouseEventDispatch(node, event, eventName) { const y = clientY - (Number(container.attributes.y) || 0); node.js_trigger(eventName, x, y); - if (event.nativeEvent.type === "mousemove") { - mouseposition = { x: clientX, y: clientY }; - } - if (event.nativeEvent.type === "mousedown") { // We need to persist the react event so we can access the target event.persist(); @@ -465,7 +459,7 @@ function Button({ ); } -function Popupmenu({ id, node }) { +function Popupmenu({ id, node, x, y }) { const children = node.commands.map(item => { if (item.id === "seperator") { return
  • ; @@ -481,7 +475,6 @@ function Popupmenu({ id, node }) {
  • ); }); - const { x, y } = mouseposition; // TODO: Actually properly style element return (
    { this.js_selectCommand = value => { this.parent.js_removeChild(this); diff --git a/modern/src/runtime/System.ts b/modern/src/runtime/System.ts index 14cf055c..670d5ff6 100644 --- a/modern/src/runtime/System.ts +++ b/modern/src/runtime/System.ts @@ -1,6 +1,10 @@ import Group from "./Group"; import MakiObject from "./MakiObject"; -import { findDescendantByTypeAndId, unimplementedWarning } from "../utils"; +import { + findDescendantByTypeAndId, + getMousePosition, + unimplementedWarning, +} from "../utils"; import * as Actions from "../Actions"; import * as Selectors from "../Selectors"; @@ -372,14 +376,12 @@ class System extends MakiObject { return; } - getmouseposx() { - unimplementedWarning("getmouseposx"); - return; + getmouseposx(): number { + return getMousePosition().x; } - getmouseposy() { - unimplementedWarning("getmouseposy"); - return; + getmouseposy(): number { + return getMousePosition().y; } floattostring(value: number, ndigits: number): string { diff --git a/modern/src/utils.ts b/modern/src/utils.ts index a12e9718..d0c85abb 100644 --- a/modern/src/utils.ts +++ b/modern/src/utils.ts @@ -310,3 +310,14 @@ export async function getSizeFromUrl( const { width, height } = await loadImage(imgUrl); return { width, height }; } + +let mousePosition = { x: 0, y: 0 }; +function handleMouseMove(e) { + mousePosition = { x: e.clientX, y: e.clientY }; +} + +document.addEventListener("mousemove", handleMouseMove); + +export function getMousePosition() { + return mousePosition; +}