Skip to content

Commit dd7ee6d

Browse files
committed
fixed don't run after edit ruby code.
closes #228
1 parent 5c90e4b commit dd7ee6d

File tree

5 files changed

+77
-38
lines changed

5 files changed

+77
-38
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: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,24 @@ 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;
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;
3753
} else {
3854
this.aceEditorRef.editor.focus();
3955
}

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: 11 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,25 @@ 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(this.props.vm, this.props.rubyCode.target, this.props.rubyCode.code);
49+
if (!converter.result) {
4750
this.props.vm.setEditingTarget(this.props.rubyCode.target.id);
4851
if (!this.props.rubyCode.target.isStage) {
4952
this.props.onHighlightTarget(this.props.rubyCode.target.id);
5053
}
5154
this.props.onActivateRubyTab();
5255
this.props.onShowConvertRubyToBlocksErrorAlert();
53-
this.props.updateRubyCodeErrorsState(errors);
54-
return false;
56+
this.props.updateRubyCodeErrorsState(converter.errors);
57+
return converter;
5558
}
5659
this.props.convertedRubyCodeState();
60+
return converter;
5761
}
58-
return true;
62+
return NullRubyToBlocksConverter;
5963
}
6064

6165
render () {

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

Lines changed: 24 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,30 @@ 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: () => {
1233+
return Promise.resolve();
1234+
}
1235+
};
1236+
1237+
const targetCodeToBlocks = function (vm, target, code) {
12271238
const converter = new RubyToBlocksConverter(vm);
1228-
if (converter.targetCodeToBlocks(target, code)) {
1229-
converter.applyTargetBlocks(target);
1230-
return true;
1239+
converter.result = converter.targetCodeToBlocks(target, code);
1240+
if (converter.result) {
1241+
converter.apply = () => {
1242+
return converter.applyTargetBlocks(target);
1243+
};
12311244
}
1232-
converter.errors.forEach(e => errors.push(e));
1233-
return false;
1245+
return converter;
12341246
};
12351247

12361248
export {
12371249
RubyToBlocksConverter as default,
1250+
NullRubyToBlocksConverter,
12381251
targetCodeToBlocks
12391252
};

0 commit comments

Comments
 (0)