From fdde372ef338b118b70ec377018e343de8033b25 Mon Sep 17 00:00:00 2001
From: Delucse <46593742+Delucse@users.noreply.github.com>
Date: Mon, 10 Aug 2020 11:54:36 +0200
Subject: [PATCH] reset Workspace with initial XML-Block
---
src/actions/workspaceActions.js | 15 ++++++++-------
src/components/Blockly/BlocklyWindow.js | 4 ++--
src/components/Blockly/initialXml.js | 3 +++
src/components/Blockly/toolbox/Toolbox.js | 5 ++++-
src/components/ClearWorkspace.js | 15 ++++++++++-----
src/components/WorkspaceStats.js | 4 ++--
6 files changed, 29 insertions(+), 17 deletions(-)
create mode 100644 src/components/Blockly/initialXml.js
diff --git a/src/actions/workspaceActions.js b/src/actions/workspaceActions.js
index 40d837d..e7e08ab 100644
--- a/src/actions/workspaceActions.js
+++ b/src/actions/workspaceActions.js
@@ -5,14 +5,10 @@ import * as Blockly from 'blockly/core';
export const workspaceChange = () => (dispatch) => {
dispatch({
type: CHANGE_WORKSPACE
- })
-}
+ });
+};
-
-export const onChangeWorkspace = (event) => (dispatch, getState) => {
- dispatch({
- type: CHANGE_WORKSPACE,
- })
+export const onChangeCode = () => (dispatch, getState) => {
const workspace = Blockly.getMainWorkspace();
var code = getState().workspace.code;
code.arduino = Blockly.Arduino.workspaceToCode(workspace);
@@ -22,6 +18,11 @@ export const onChangeWorkspace = (event) => (dispatch, getState) => {
type: NEW_CODE,
payload: code
});
+};
+
+export const onChangeWorkspace = (event) => (dispatch, getState) => {
+ dispatch(workspaceChange());
+ dispatch(onChangeCode());
var stats = getState().workspace.stats;
if (event.type === Blockly.Events.BLOCK_CREATE) {
stats.create += event.ids.length;
diff --git a/src/components/Blockly/BlocklyWindow.js b/src/components/Blockly/BlocklyWindow.js
index 228867b..e6dcc1f 100644
--- a/src/components/Blockly/BlocklyWindow.js
+++ b/src/components/Blockly/BlocklyWindow.js
@@ -7,6 +7,7 @@ import BlocklyComponent from './';
import * as Blockly from 'blockly/core';
import './blocks/index';
import './generator/index';
+import { initialXml } from './initialXml.js';
@@ -53,8 +54,7 @@ class BlocklyWindow extends Component {
drag: true,
wheel: false
}}
- initialXml={`
- `}
+ initialXml={initialXml}
>
diff --git a/src/components/Blockly/initialXml.js b/src/components/Blockly/initialXml.js
new file mode 100644
index 0000000..dc8d02e
--- /dev/null
+++ b/src/components/Blockly/initialXml.js
@@ -0,0 +1,3 @@
+export const initialXml = `
+ `
+// Block is initial Block of the workspace and must always be a part of the workspace
diff --git a/src/components/Blockly/toolbox/Toolbox.js b/src/components/Blockly/toolbox/Toolbox.js
index 9cadec6..cfa858b 100644
--- a/src/components/Blockly/toolbox/Toolbox.js
+++ b/src/components/Blockly/toolbox/Toolbox.js
@@ -286,12 +286,15 @@ class Toolbox extends React.Component {
+ {/* this block is the initial block of the workspace; not necessary
+ to display, because it can only be used once anyway
+ */}
);
};
}
-export default Toolbox;
\ No newline at end of file
+export default Toolbox;
diff --git a/src/components/ClearWorkspace.js b/src/components/ClearWorkspace.js
index 5b48648..64cf1cd 100644
--- a/src/components/ClearWorkspace.js
+++ b/src/components/ClearWorkspace.js
@@ -1,7 +1,8 @@
import React, {Component} from 'react';
import PropTypes from 'prop-types';
import { connect } from 'react-redux';
-import { clearStats, workspaceChange } from '../actions/workspaceActions';
+import { clearStats, onChangeCode } from '../actions/workspaceActions';
+import { initialXml } from './Blockly/initialXml.js';
import * as Blockly from 'blockly/core';
@@ -16,9 +17,13 @@ class ClearWorkspace extends Component {
clearWorkspace = () => {
const workspace = Blockly.getMainWorkspace();
- workspace.clear();
+ Blockly.Events.disable(); // https://groups.google.com/forum/#!topic/blockly/m7e3g0TC75Y
+ // if events are disabled, then the workspace will be cleared AND the blocks are not in the trashcan
+ const xmlDom = Blockly.Xml.textToDom(initialXml)
+ Blockly.Xml.clearWorkspaceAndLoadFromXml(xmlDom, workspace);
+ Blockly.Events.enable();
workspace.options.maxBlocks = Infinity;
- this.props.workspaceChange();
+ this.props.onChangeCode();
this.props.clearStats();
}
@@ -34,8 +39,8 @@ class ClearWorkspace extends Component {
ClearWorkspace.propTypes = {
clearStats: PropTypes.func.isRequired,
- workspaceChange: PropTypes.func.isRequired
+ onChangeWorkspace: PropTypes.func.isRequired
};
-export default connect(null, { clearStats, workspaceChange })(ClearWorkspace);
+export default connect(null, { clearStats, onChangeCode })(ClearWorkspace);
diff --git a/src/components/WorkspaceStats.js b/src/components/WorkspaceStats.js
index 1ca2bff..c3cacee 100644
--- a/src/components/WorkspaceStats.js
+++ b/src/components/WorkspaceStats.js
@@ -87,7 +87,7 @@ WorkspaceStats.propTypes = {
change: PropTypes.number.isRequired,
delete: PropTypes.number.isRequired,
move: PropTypes.number.isRequired,
- worskpaceChange: PropTypes.number.isRequired
+ workspaceChange: PropTypes.number.isRequired
};
const mapStateToProps = state => ({
@@ -95,7 +95,7 @@ const mapStateToProps = state => ({
change: state.workspace.stats.change,
delete: state.workspace.stats.delete,
move: state.workspace.stats.move,
- worskpaceChange: state.workspace.change
+ workspaceChange: state.workspace.change
});
export default connect(mapStateToProps, null)(withStyles(styles, { withTheme: true })(WorkspaceStats));