From db403dcad9dc3a71fe2729bb06eb5f0d67e2069b Mon Sep 17 00:00:00 2001
From: Paul Kaplan <pkaplan@media.mit.edu>
Date: Wed, 26 Apr 2017 14:04:48 -0400
Subject: [PATCH] Update shadow block values with debounced targetsUpdate
 handler

---
 package.json              |  1 +
 src/containers/blocks.jsx | 23 ++++++++++++++++++++++-
 2 files changed, 23 insertions(+), 1 deletion(-)

diff --git a/package.json b/package.json
index 7d221cc7c..bd02cf068 100644
--- a/package.json
+++ b/package.json
@@ -39,6 +39,7 @@
     "gh-pages": "^0.12.0",
     "html-webpack-plugin": "2.28.0",
     "lodash.bindall": "4.4.0",
+    "lodash.debounce": "4.0.8",
     "lodash.defaultsdeep": "4.6.0",
     "lodash.isequal": "4.5.0",
     "lodash.omit": "4.5.0",
diff --git a/src/containers/blocks.jsx b/src/containers/blocks.jsx
index 487bed480..a2146d3f8 100644
--- a/src/containers/blocks.jsx
+++ b/src/containers/blocks.jsx
@@ -1,4 +1,5 @@
 const bindAll = require('lodash.bindall');
+const debounce = require('lodash.debounce');
 const defaultsDeep = require('lodash.defaultsdeep');
 const PropTypes = require('prop-types');
 const React = require('react');
@@ -30,6 +31,7 @@ class Blocks extends React.Component {
             'onScriptGlowOff',
             'onBlockGlowOn',
             'onBlockGlowOff',
+            'onTargetsUpdate',
             'onVisualReport',
             'onWorkspaceUpdate',
             'onWorkspaceMetricsChange',
@@ -40,6 +42,7 @@ class Blocks extends React.Component {
             workspaceMetrics: {},
             prompt: null
         };
+        this.onTargetsUpdate = debounce(this.onTargetsUpdate, 100);
     }
     componentDidMount () {
         const workspaceConfig = defaultsDeep({}, Blocks.defaultOptions, this.props.options);
@@ -85,6 +88,7 @@ class Blocks extends React.Component {
         this.props.vm.addListener('BLOCK_GLOW_OFF', this.onBlockGlowOff);
         this.props.vm.addListener('VISUAL_REPORT', this.onVisualReport);
         this.props.vm.addListener('workspaceUpdate', this.onWorkspaceUpdate);
+        this.props.vm.addListener('targetsUpdate', this.onTargetsUpdate);
     }
     detachVM () {
         this.props.vm.removeListener('SCRIPT_GLOW_ON', this.onScriptGlowOn);
@@ -93,8 +97,25 @@ class Blocks extends React.Component {
         this.props.vm.removeListener('BLOCK_GLOW_OFF', this.onBlockGlowOff);
         this.props.vm.removeListener('VISUAL_REPORT', this.onVisualReport);
         this.props.vm.removeListener('workspaceUpdate', this.onWorkspaceUpdate);
+        this.props.vm.removeListener('targetsUpdate', this.onTargetsUpdate);
+    }
+    updateToolboxBlockValue (id, value) {
+        this.workspace
+            .getFlyout()
+            .getWorkspace()
+            .getBlockById(id)
+            .inputList[0]
+            .fieldRow[0]
+            .setValue(value);
+    }
+    onTargetsUpdate () {
+        if (this.props.vm.editingTarget) {
+            ['glide', 'move', 'set'].forEach(prefix => {
+                this.updateToolboxBlockValue(`${prefix}x`, this.props.vm.editingTarget.x.toFixed(0));
+                this.updateToolboxBlockValue(`${prefix}y`, this.props.vm.editingTarget.y.toFixed(0));
+            });
+        }
     }
-
     onWorkspaceMetricsChange () {
         const target = this.props.vm.editingTarget;
         if (target && target.id) {
-- 
GitLab