This commit is contained in:
Mario Pesch 2021-03-24 15:33:08 +01:00
parent 65fa627aeb
commit 190d676e5b
2 changed files with 23 additions and 7 deletions

View File

@ -21,9 +21,18 @@ class BlocklyWindow extends Component {
} }
componentDidMount() { componentDidMount() {
console.log(this.props);
const workspace = Blockly.getMainWorkspace(); const workspace = Blockly.getMainWorkspace();
this.props.onChangeWorkspace({});
this.props.clearStats(); if (this.props.workspaceXML !== "") {
Blockly.Xml.clearWorkspaceAndLoadFromXml(
Blockly.Xml.textToDom(this.props.workspaceXML),
workspace
);
} else {
this.props.onChangeWorkspace({});
this.props.clearStats();
}
workspace.addChangeListener((event) => { workspace.addChangeListener((event) => {
this.props.onChangeWorkspace(event); this.props.onChangeWorkspace(event);
// switch on that a block is displayed disabled or not depending on whether it is correctly connected // switch on that a block is displayed disabled or not depending on whether it is correctly connected
@ -38,6 +47,8 @@ class BlocklyWindow extends Component {
componentDidUpdate(props) { componentDidUpdate(props) {
const workspace = Blockly.getMainWorkspace(); const workspace = Blockly.getMainWorkspace();
var xml = this.props.initialXml; var xml = this.props.initialXml;
console.log(xml)
console.log(this.props.xml)
// if svg is true, then the update process is done in the BlocklySvg component // if svg is true, then the update process is done in the BlocklySvg component
if (props.initialXml !== xml && !this.props.svg) { if (props.initialXml !== xml && !this.props.svg) {
// guarantees that the current xml-code (this.props.initialXml) is rendered // guarantees that the current xml-code (this.props.initialXml) is rendered
@ -45,6 +56,7 @@ class BlocklyWindow extends Component {
if (!xml) xml = initialXml; if (!xml) xml = initialXml;
Blockly.Xml.domToWorkspace(Blockly.Xml.textToDom(xml), workspace); Blockly.Xml.domToWorkspace(Blockly.Xml.textToDom(xml), workspace);
} }
if (props.language !== this.props.language) { if (props.language !== this.props.language) {
// change language // change language
if (!xml) xml = initialXml; if (!xml) xml = initialXml;
@ -101,12 +113,14 @@ BlocklyWindow.propTypes = {
onChangeWorkspace: PropTypes.func.isRequired, onChangeWorkspace: PropTypes.func.isRequired,
clearStats: PropTypes.func.isRequired, clearStats: PropTypes.func.isRequired,
renderer: PropTypes.string.isRequired, renderer: PropTypes.string.isRequired,
language: PropTypes.string.isRequired language: PropTypes.string.isRequired,
workspaceXML: PropTypes.string.isRequired,
}; };
const mapStateToProps = state => ({ const mapStateToProps = state => ({
renderer: state.general.renderer, renderer: state.general.renderer,
language: state.general.language language: state.general.language.Blockly,
workspaceXML: state.workspace.code.xml,
}); });
export default connect(mapStateToProps, { onChangeWorkspace, clearStats })(BlocklyWindow); export default connect(mapStateToProps, { onChangeWorkspace, clearStats })(BlocklyWindow);

View File

@ -114,7 +114,7 @@ class Home extends Component {
<div className='blocklyWindow'> <div className='blocklyWindow'>
{this.props.project ? {this.props.project ?
< BlocklyWindow blocklyCSS={{ height: '80vH' }} initialXml={this.props.project.xml} /> < BlocklyWindow blocklyCSS={{ height: '80vH' }} initialXml={this.props.project.xml} />
: < BlocklyWindow blocklyCSS={{ height: '80vH' }} /> : < BlocklyWindow blocklyCSS={{ height: '80vH' }}/>
} }
</div> </div>
</Grid> </Grid>
@ -141,12 +141,14 @@ Home.propTypes = {
clearStats: PropTypes.func.isRequired, clearStats: PropTypes.func.isRequired,
workspaceName: PropTypes.func.isRequired, workspaceName: PropTypes.func.isRequired,
message: PropTypes.object.isRequired, message: PropTypes.object.isRequired,
statistics: PropTypes.bool.isRequired statistics: PropTypes.bool.isRequired,
}; };
const mapStateToProps = state => ({ const mapStateToProps = state => ({
message: state.message, message: state.message,
statistics: state.general.statistics statistics: state.general.statistics,
}); });