Skip to content

Commit e20f092

Browse files
authored
Merge pull request #229 from /issues/228
fixed don't run after edit ruby code.
2 parents 5c90e4b + d1b34f9 commit e20f092

File tree

5 files changed

+78
-40
lines changed

5 files changed

+78
-40
lines changed

src/containers/controls.jsx

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,22 @@ class Controls extends React.Component {
1919
handleGreenFlagClick (e) {
2020
e.preventDefault();
2121

22-
if (!this.props.targetCodeToBlocks()) {
22+
const converter = this.props.targetCodeToBlocks();
23+
if (!converter.result) {
2324
return;
2425
}
25-
26-
if (e.shiftKey) {
27-
this.props.vm.setTurboMode(!this.props.turbo);
28-
} else {
29-
if (!this.props.isStarted) {
30-
this.props.vm.start();
31-
}
32-
this.props.vm.greenFlag();
33-
}
26+
const shiftKey = e.shiftKey;
27+
converter.apply()
28+
.then(() => {
29+
if (shiftKey) {
30+
this.props.vm.setTurboMode(!this.props.turbo);
31+
} else {
32+
if (!this.props.isStarted) {
33+
this.props.vm.start();
34+
}
35+
this.props.vm.greenFlag();
36+
}
37+
});
3438
}
3539
handleStopAllClick (e) {
3640
e.preventDefault();

src/containers/ruby-tab.jsx

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,26 @@ class RubyTab extends React.Component {
3232
this.props.vm.editingTarget && this.props.rubyCode.target &&
3333
this.props.vm.editingTarget.id !== targetId;
3434
if (changedTarget || this.props.blocksTabVisible) {
35-
if (this.props.targetCodeToBlocks()) {
36-
modified = false;
37-
} else {
38-
this.aceEditorRef.editor.focus();
35+
const converter = this.props.targetCodeToBlocks();
36+
if (converter.result) {
37+
converter.apply().then(() => {
38+
modified = false;
39+
40+
if (!modified) {
41+
if ((this.props.isVisible && !prevProps.isVisible) ||
42+
(this.props.editingTarget && this.props.editingTarget !== prevProps.editingTarget)) {
43+
this.props.updateRubyCodeTargetState(this.props.vm.editingTarget);
44+
}
45+
}
46+
47+
if (this.props.isVisible && !prevProps.isVisible) {
48+
this.aceEditorRef.editor.renderer.updateFull();
49+
this.aceEditorRef.editor.focus();
50+
}
51+
});
52+
return;
3953
}
54+
this.aceEditorRef.editor.focus();
4055
}
4156
}
4257

src/containers/sb3-downloader.jsx

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,16 +29,18 @@ class SB3Downloader extends React.Component {
2929
]);
3030
}
3131
downloadProject () {
32-
if (!this.props.targetCodeToBlocks()) {
32+
const converter = this.props.targetCodeToBlocks();
33+
if (!converter.result) {
3334
return;
3435
}
35-
36-
this.props.saveProjectSb3().then(content => {
37-
if (this.props.onSaveFinished) {
38-
this.props.onSaveFinished();
39-
}
40-
downloadBlob(this.props.projectFilename, content);
41-
});
36+
converter.apply()
37+
.then(this.props.saveProjectSb3)
38+
.then(content => {
39+
if (this.props.onSaveFinished) {
40+
this.props.onSaveFinished();
41+
}
42+
downloadBlob(this.props.projectFilename, content);
43+
});
4244
}
4345
render () {
4446
const {

src/lib/ruby-to-blocks-converter-hoc.jsx

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@ import React from 'react';
33
import PropTypes from 'prop-types';
44
import {connect} from 'react-redux';
55
import VM from 'scratch-vm';
6-
import {targetCodeToBlocks} from '../lib/ruby-to-blocks-converter';
6+
import {
7+
NullRubyToBlocksConverter,
8+
targetCodeToBlocks
9+
} from '../lib/ruby-to-blocks-converter';
710

811
import {
912
activateTab,
@@ -38,24 +41,29 @@ const RubyToBlocksConverterHOC = function (WrappedComponent) {
3841

3942
/**
4043
* targetCodeToBlocks:
41-
* @return {boolean} - True if succeeded to convert Ruby to Blocks
44+
* @return {RubyToBlocksConverter} - a block converter that translates ruby code into the blocks
4245
*/
4346
targetCodeToBlocks () {
4447
if (this.props.rubyCode.modified) {
45-
const errors = [];
46-
if (!targetCodeToBlocks(this.props.vm, this.props.rubyCode.target, this.props.rubyCode.code, errors)) {
48+
const converter = targetCodeToBlocks(
49+
this.props.vm,
50+
this.props.rubyCode.target,
51+
this.props.rubyCode.code
52+
);
53+
if (!converter.result) {
4754
this.props.vm.setEditingTarget(this.props.rubyCode.target.id);
4855
if (!this.props.rubyCode.target.isStage) {
4956
this.props.onHighlightTarget(this.props.rubyCode.target.id);
5057
}
5158
this.props.onActivateRubyTab();
5259
this.props.onShowConvertRubyToBlocksErrorAlert();
53-
this.props.updateRubyCodeErrorsState(errors);
54-
return false;
60+
this.props.updateRubyCodeErrorsState(converter.errors);
61+
return converter;
5562
}
5663
this.props.convertedRubyCodeState();
64+
return converter;
5765
}
58-
return true;
66+
return NullRubyToBlocksConverter;
5967
}
6068

6169
render () {

src/lib/ruby-to-blocks-converter/index.js

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -211,18 +211,18 @@ class RubyToBlocksConverter {
211211
}
212212
});
213213

214-
Object.keys(target.blocks._blocks).forEach(blockId => {
215-
target.blocks.deleteBlock(blockId);
216-
});
217-
218214
const extensionPromises = [];
219215
this._context.extensionIDs.forEach(extensionID => {
220216
if (!this.vm.extensionManager.isExtensionLoaded(extensionID)) {
221217
extensionPromises.push(this.vm.extensionManager.loadExtensionURL(extensionID));
222218
}
223219
});
224220

225-
Promise.all(extensionPromises).then(() => {
221+
return Promise.all(extensionPromises).then(() => {
222+
Object.keys(target.blocks._blocks).forEach(blockId => {
223+
target.blocks.deleteBlock(blockId);
224+
});
225+
226226
Object.keys(this._context.blocks).forEach(blockId => {
227227
target.blocks.createBlock(this._context.blocks[blockId]);
228228
});
@@ -1223,17 +1223,26 @@ class RubyToBlocksConverter {
12231223
}
12241224
}
12251225

1226-
const targetCodeToBlocks = function (vm, target, code, errors = []) {
1226+
/**
1227+
* Null of RubyToBlocksConverter
1228+
*/
1229+
const NullRubyToBlocksConverter = {
1230+
result: true,
1231+
errors: [],
1232+
apply: () => Promise.resolve()
1233+
};
1234+
1235+
const targetCodeToBlocks = function (vm, target, code) {
12271236
const converter = new RubyToBlocksConverter(vm);
1228-
if (converter.targetCodeToBlocks(target, code)) {
1229-
converter.applyTargetBlocks(target);
1230-
return true;
1237+
converter.result = converter.targetCodeToBlocks(target, code);
1238+
if (converter.result) {
1239+
converter.apply = () => converter.applyTargetBlocks(target);
12311240
}
1232-
converter.errors.forEach(e => errors.push(e));
1233-
return false;
1241+
return converter;
12341242
};
12351243

12361244
export {
12371245
RubyToBlocksConverter as default,
1246+
NullRubyToBlocksConverter,
12381247
targetCodeToBlocks
12391248
};

0 commit comments

Comments
 (0)