Skip to content

Commit 70bd41e

Browse files
towerofnixbenjiwheeler
authored andcommitted
Also save dimensions in workspaceMetrics
1 parent 62e36fd commit 70bd41e

File tree

2 files changed

+43
-15
lines changed

2 files changed

+43
-15
lines changed

src/containers/blocks.jsx

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -301,12 +301,17 @@ class Blocks extends React.Component {
301301
onWorkspaceMetricsChange () {
302302
const target = this.props.vm.editingTarget;
303303
if (target && target.id) {
304-
this.props.updateMetrics({
305-
targetID: target.id,
306-
scrollX: this.workspace.scrollX,
307-
scrollY: this.workspace.scrollY,
308-
scale: this.workspace.scale
309-
});
304+
// Dispatch updateMetrics later, since onWorkspaceMetricsChange may be (very indirectly)
305+
// called from a reducer, i.e. when you create a custom procedure.
306+
// TODO: Is this a vehement hack?
307+
setTimeout(() => {
308+
this.props.updateMetrics({
309+
targetID: target.id,
310+
scrollX: this.workspace.scrollX,
311+
scrollY: this.workspace.scrollY,
312+
scale: this.workspace.scale
313+
});
314+
}, 0);
310315
}
311316
}
312317
onScriptGlowOn (data) {
@@ -353,7 +358,7 @@ class Blocks extends React.Component {
353358
this.props.updateToolboxState(toolboxXML);
354359
}
355360

356-
if (this.props.vm.editingTarget && !this.props.workspaceMetrics[this.props.vm.editingTarget.id]) {
361+
if (this.props.vm.editingTarget && !this.props.workspaceMetrics.targets[this.props.vm.editingTarget.id]) {
357362
this.onWorkspaceMetricsChange();
358363
}
359364

@@ -379,8 +384,8 @@ class Blocks extends React.Component {
379384
}
380385
this.workspace.addChangeListener(this.props.vm.blockListener);
381386

382-
if (this.props.vm.editingTarget && this.props.workspaceMetrics[this.props.vm.editingTarget.id]) {
383-
const {scrollX, scrollY, scale} = this.props.workspaceMetrics[this.props.vm.editingTarget.id];
387+
if (this.props.vm.editingTarget && this.props.workspaceMetrics.targets[this.props.vm.editingTarget.id]) {
388+
const {scrollX, scrollY, scale} = this.props.workspaceMetrics.targets[this.props.vm.editingTarget.id];
384389
this.workspace.scrollX = scrollX;
385390
this.workspace.scrollY = scrollY;
386391
this.workspace.scale = scale;

src/reducers/workspace-metrics.js

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,47 @@
1+
const UPDATE_DIMENSIONS = 'scratch-gui/workspace-metrics/UPDATE_DIMENSIONS';
12
const UPDATE_METRICS = 'scratch-gui/workspace-metrics/UPDATE_METRICS';
23

3-
const initialState = {};
4+
const initialState = {
5+
dimensions: {
6+
width: 0,
7+
height: 0
8+
},
9+
targets: {}
10+
};
411

512
const reducer = function (state, action) {
613
if (typeof state === 'undefined') state = initialState;
714

815
switch (action.type) {
9-
case UPDATE_METRICS:
16+
case UPDATE_DIMENSIONS:
1017
return Object.assign({}, state, {
11-
[action.targetID]: {
12-
scrollX: action.scrollX,
13-
scrollY: action.scrollY,
14-
scale: action.scale
18+
dimensions: {
19+
width: action.width,
20+
height: action.height
1521
}
1622
});
23+
case UPDATE_METRICS:
24+
return Object.assign({}, state, {
25+
targets: Object.assign({}, state.targets, {
26+
[action.targetID]: {
27+
scrollX: action.scrollX,
28+
scrollY: action.scrollY,
29+
scale: action.scale
30+
}
31+
})
32+
});
1733
default:
1834
return state;
1935
}
2036
};
2137

38+
const updateDimensions = function (dimensions) {
39+
return {
40+
type: UPDATE_DIMENSIONS,
41+
...dimensions
42+
};
43+
};
44+
2245
const updateMetrics = function (metrics) {
2346
return {
2447
type: UPDATE_METRICS,

0 commit comments

Comments
 (0)