removing workspace from Redux
This commit is contained in:
		
							parent
							
								
									c2405de04a
								
							
						
					
					
						commit
						168c5fb575
					
				| @ -1,6 +1,5 @@ | |||||||
| export const NEW_CODE = 'NEW_CODE'; | export const NEW_CODE = 'NEW_CODE'; | ||||||
| export const CHANGE_WORKSPACE = 'CHANGE_WORKSPACE'; | export const CHANGE_WORKSPACE = 'CHANGE_WORKSPACE'; | ||||||
| export const NEW_WORKSPACE = 'NEW_WORKSPACE'; |  | ||||||
| export const CREATE_BLOCK = 'CREATE_BLOCK'; | export const CREATE_BLOCK = 'CREATE_BLOCK'; | ||||||
| export const MOVE_BLOCK = 'MOVE_BLOCK'; | export const MOVE_BLOCK = 'MOVE_BLOCK'; | ||||||
| export const CHANGE_BLOCK = 'CHANGE_BLOCK'; | export const CHANGE_BLOCK = 'CHANGE_BLOCK'; | ||||||
|  | |||||||
| @ -1,4 +1,4 @@ | |||||||
| import { NEW_CODE, CHANGE_WORKSPACE, NEW_WORKSPACE, CREATE_BLOCK, MOVE_BLOCK, CHANGE_BLOCK, DELETE_BLOCK, CLEAR_STATS } from './types'; | import { NEW_CODE, CHANGE_WORKSPACE, CREATE_BLOCK, MOVE_BLOCK, CHANGE_BLOCK, DELETE_BLOCK, CLEAR_STATS } from './types'; | ||||||
| 
 | 
 | ||||||
| import * as Blockly from 'blockly/core'; | import * as Blockly from 'blockly/core'; | ||||||
| 
 | 
 | ||||||
| @ -8,22 +8,12 @@ export const workspaceChange = () => (dispatch) => { | |||||||
|   }) |   }) | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| export const initWorkspace = (workspace) => (dispatch) => { |  | ||||||
|   dispatch({ |  | ||||||
|     type: NEW_WORKSPACE, |  | ||||||
|     payload: workspace |  | ||||||
|   }); |  | ||||||
| } |  | ||||||
| 
 | 
 | ||||||
| export const onChangeWorkspace = (event) => (dispatch, getState) => { | export const onChangeWorkspace = (event) => (dispatch, getState) => { | ||||||
|     const workspace = Blockly.getMainWorkspace(); |  | ||||||
|     dispatch({ |  | ||||||
|       type: NEW_WORKSPACE, |  | ||||||
|       payload: workspace |  | ||||||
|     }); |  | ||||||
|     dispatch({ |     dispatch({ | ||||||
|       type: CHANGE_WORKSPACE, |       type: CHANGE_WORKSPACE, | ||||||
|     }) |     }) | ||||||
|  |     const workspace = Blockly.getMainWorkspace(); | ||||||
|     var code = getState().workspace.code; |     var code = getState().workspace.code; | ||||||
|     code.arduino = Blockly.Arduino.workspaceToCode(workspace); |     code.arduino = Blockly.Arduino.workspaceToCode(workspace); | ||||||
|     var xmlDom = Blockly.Xml.workspaceToDom(workspace); |     var xmlDom = Blockly.Xml.workspaceToDom(workspace); | ||||||
| @ -78,11 +68,3 @@ export const clearStats = () => (dispatch) => { | |||||||
|     payload: stats |     payload: stats | ||||||
|   }); |   }); | ||||||
| }; | }; | ||||||
| 
 |  | ||||||
| 
 |  | ||||||
| export const setWorkspace = (workspace) => (dispatch, getState) => { |  | ||||||
|   dispatch({ |  | ||||||
|     type: NEW_WORKSPACE, |  | ||||||
|     payload: {new: workspace, old: getState().workspace.new} |  | ||||||
|   }); |  | ||||||
| }; |  | ||||||
|  | |||||||
| @ -3,6 +3,8 @@ import PropTypes from 'prop-types'; | |||||||
| import { connect } from 'react-redux'; | import { connect } from 'react-redux'; | ||||||
| import { clearStats, workspaceChange } from '../actions/workspaceActions'; | import { clearStats, workspaceChange } from '../actions/workspaceActions'; | ||||||
| 
 | 
 | ||||||
|  | import * as Blockly from 'blockly/core'; | ||||||
|  | 
 | ||||||
| import ListItem from '@material-ui/core/ListItem'; | import ListItem from '@material-ui/core/ListItem'; | ||||||
| import ListItemIcon from '@material-ui/core/ListItemIcon'; | import ListItemIcon from '@material-ui/core/ListItemIcon'; | ||||||
| import ListItemText from '@material-ui/core/ListItemText'; | import ListItemText from '@material-ui/core/ListItemText'; | ||||||
| @ -13,15 +15,11 @@ import { FontAwesomeIcon } from "@fortawesome/react-fontawesome"; | |||||||
| class ClearWorkspace extends Component { | class ClearWorkspace extends Component { | ||||||
| 
 | 
 | ||||||
|   clearWorkspace = () => { |   clearWorkspace = () => { | ||||||
|     if(this.props.workspace){ |     const workspace = Blockly.getMainWorkspace(); | ||||||
|       this.props.workspace.clear(); |     workspace.clear(); | ||||||
|       this.props.workspace.options.maxBlocks = Infinity; |     workspace.options.maxBlocks = Infinity; | ||||||
|       this.props.workspaceChange(); |     this.props.workspaceChange(); | ||||||
|       this.props.clearStats(); |     this.props.clearStats(); | ||||||
|     } |  | ||||||
|     else { |  | ||||||
|       alert() |  | ||||||
|     } |  | ||||||
|   } |   } | ||||||
| 
 | 
 | ||||||
|   render() { |   render() { | ||||||
| @ -35,13 +33,9 @@ class ClearWorkspace extends Component { | |||||||
| } | } | ||||||
| 
 | 
 | ||||||
| ClearWorkspace.propTypes = { | ClearWorkspace.propTypes = { | ||||||
|   workspace: PropTypes.object.isRequired, |  | ||||||
|   clearStats: PropTypes.func.isRequired, |   clearStats: PropTypes.func.isRequired, | ||||||
|   workspaceChange: PropTypes.func.isRequired |   workspaceChange: PropTypes.func.isRequired | ||||||
| }; | }; | ||||||
| 
 | 
 | ||||||
| const mapStateToProps = state => ({ |  | ||||||
|   workspace: state.workspace.workspace |  | ||||||
| }); |  | ||||||
| 
 | 
 | ||||||
| export default connect(mapStateToProps, { clearStats, workspaceChange })(ClearWorkspace); | export default connect(null, { clearStats, workspaceChange })(ClearWorkspace); | ||||||
|  | |||||||
| @ -1,8 +1,7 @@ | |||||||
| import React, { Component } from 'react'; | import React, { Component } from 'react'; | ||||||
| 
 |  | ||||||
| import PropTypes from 'prop-types'; | import PropTypes from 'prop-types'; | ||||||
| import { connect } from 'react-redux'; | import { connect } from 'react-redux'; | ||||||
| import { onChangeWorkspace, initWorkspace } from '../actions/workspaceActions'; | import { onChangeWorkspace } from '../actions/workspaceActions'; | ||||||
| 
 | 
 | ||||||
| import WorkspaceStats from './WorkspaceStats'; | import WorkspaceStats from './WorkspaceStats'; | ||||||
| import WorkspaceFunc from './WorkspaceFunc'; | import WorkspaceFunc from './WorkspaceFunc'; | ||||||
| @ -16,7 +15,7 @@ import './Blockly/blocks/index'; | |||||||
| import './Blockly/generator/index'; | import './Blockly/generator/index'; | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
| class Home extends React.Component { | class Home extends Component { | ||||||
|   constructor(props) { |   constructor(props) { | ||||||
|     super(props); |     super(props); | ||||||
| 
 | 
 | ||||||
| @ -24,8 +23,7 @@ class Home extends React.Component { | |||||||
|   } |   } | ||||||
| 
 | 
 | ||||||
|   componentDidMount() { |   componentDidMount() { | ||||||
|     let workspace = Blockly.getMainWorkspace(); |     const workspace = Blockly.getMainWorkspace(); | ||||||
|     this.props.initWorkspace(workspace); |  | ||||||
|     workspace.addChangeListener((event) => { |     workspace.addChangeListener((event) => { | ||||||
|       this.props.onChangeWorkspace(event); |       this.props.onChangeWorkspace(event); | ||||||
|     }); |     }); | ||||||
| @ -72,22 +70,9 @@ class Home extends React.Component { | |||||||
|   }; |   }; | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| WorkspaceStats.propTypes = { | Home.propTypes = { | ||||||
|   workspace: PropTypes.object.isRequired, |   onChangeWorkspace: PropTypes.func.isRequired | ||||||
|   create: PropTypes.number.isRequired, |  | ||||||
|   change: PropTypes.number.isRequired, |  | ||||||
|   delete: PropTypes.number.isRequired, |  | ||||||
|   move: PropTypes.number.isRequired, |  | ||||||
|   worskpaceChange: PropTypes.number.isRequired |  | ||||||
| }; | }; | ||||||
| 
 | 
 | ||||||
| const mapStateToProps = state => ({ |  | ||||||
|   workspace: state.workspace.workspace, |  | ||||||
|   create: state.workspace.stats.create, |  | ||||||
|   change: state.workspace.stats.change, |  | ||||||
|   delete: state.workspace.stats.delete, |  | ||||||
|   move: state.workspace.stats.move, |  | ||||||
|   worskpaceChange: state.workspace.change |  | ||||||
| }); |  | ||||||
| 
 | 
 | ||||||
| export default connect(null, { onChangeWorkspace, initWorkspace })(Home); | export default connect(null, { onChangeWorkspace })(Home); | ||||||
|  | |||||||
| @ -43,4 +43,8 @@ class MaxBlocks extends Component { | |||||||
|   }; |   }; | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  | MaxBlocks.propTypes = { | ||||||
|  |   workspaceChange: PropTypes.func.isRequired | ||||||
|  | }; | ||||||
|  | 
 | ||||||
| export default connect(null, { workspaceChange })(MaxBlocks); | export default connect(null, { workspaceChange })(MaxBlocks); | ||||||
|  | |||||||
| @ -24,13 +24,14 @@ const styles = (theme) => ({ | |||||||
| class WorkspaceStats extends Component { | class WorkspaceStats extends Component { | ||||||
| 
 | 
 | ||||||
|   render() { |   render() { | ||||||
|     const remainingBlocksInfinity = this.props.workspace ? this.props.workspace.remainingCapacity() !== Infinity : null; |     const workspace = Blockly.getMainWorkspace(); | ||||||
|  |     const remainingBlocksInfinity = workspace ? workspace.remainingCapacity() !== Infinity : null; | ||||||
|     return ( |     return ( | ||||||
|       <div style={{marginBottom: '20px'}}> |       <div style={{marginBottom: '20px'}}> | ||||||
|         <Tooltip title="Anzahl aktueller Blöcke" style={{marginLeft: 0}} className={this.props.classes.stats}> |         <Tooltip title="Anzahl aktueller Blöcke" style={{marginLeft: 0}} className={this.props.classes.stats}> | ||||||
|           <div> |           <div> | ||||||
|             <FontAwesomeIcon icon={faPuzzlePiece} /> |             <FontAwesomeIcon icon={faPuzzlePiece} /> | ||||||
|             <Typography style={{display: 'inline'}}> {this.props.workspace ? this.props.workspace.getAllBlocks().length : 0}</Typography> |             <Typography style={{display: 'inline'}}> {workspace ? workspace.getAllBlocks().length : 0}</Typography> | ||||||
|           </div> |           </div> | ||||||
|         </Tooltip> |         </Tooltip> | ||||||
|         <Tooltip title="Anzahl neuer Blöcke" className={this.props.classes.stats}> |         <Tooltip title="Anzahl neuer Blöcke" className={this.props.classes.stats}> | ||||||
| @ -63,7 +64,7 @@ class WorkspaceStats extends Component { | |||||||
|         </Tooltip> |         </Tooltip> | ||||||
|         {remainingBlocksInfinity ? |         {remainingBlocksInfinity ? | ||||||
|           <Tooltip title="verbleibende Blöcke" className={this.props.classes.stats}> |           <Tooltip title="verbleibende Blöcke" className={this.props.classes.stats}> | ||||||
|             <Typography style={{display: 'inline'}}>{this.props.workspace.remainingCapacity()} verbleibende Blöcke</Typography> |             <Typography style={{display: 'inline'}}>{workspace.remainingCapacity()} verbleibende Blöcke</Typography> | ||||||
|           </Tooltip> : null} |           </Tooltip> : null} | ||||||
|       </div> |       </div> | ||||||
|     ); |     ); | ||||||
| @ -71,7 +72,6 @@ class WorkspaceStats extends Component { | |||||||
| } | } | ||||||
| 
 | 
 | ||||||
| WorkspaceStats.propTypes = { | WorkspaceStats.propTypes = { | ||||||
|   workspace: PropTypes.object.isRequired, |  | ||||||
|   create: PropTypes.number.isRequired, |   create: PropTypes.number.isRequired, | ||||||
|   change: PropTypes.number.isRequired, |   change: PropTypes.number.isRequired, | ||||||
|   delete: PropTypes.number.isRequired, |   delete: PropTypes.number.isRequired, | ||||||
| @ -80,7 +80,6 @@ WorkspaceStats.propTypes = { | |||||||
| }; | }; | ||||||
| 
 | 
 | ||||||
| const mapStateToProps = state => ({ | const mapStateToProps = state => ({ | ||||||
|   workspace: state.workspace.workspace, |  | ||||||
|   create: state.workspace.stats.create, |   create: state.workspace.stats.create, | ||||||
|   change: state.workspace.stats.change, |   change: state.workspace.stats.change, | ||||||
|   delete: state.workspace.stats.delete, |   delete: state.workspace.stats.delete, | ||||||
|  | |||||||
| @ -1,4 +1,4 @@ | |||||||
| import { CHANGE_WORKSPACE, NEW_CODE, NEW_WORKSPACE, CREATE_BLOCK, MOVE_BLOCK, CHANGE_BLOCK, DELETE_BLOCK, CLEAR_STATS } from '../actions/types'; | import { CHANGE_WORKSPACE, NEW_CODE, CREATE_BLOCK, MOVE_BLOCK, CHANGE_BLOCK, DELETE_BLOCK, CLEAR_STATS } from '../actions/types'; | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
| const initialState = { | const initialState = { | ||||||
| @ -6,7 +6,6 @@ const initialState = { | |||||||
|     arduino: '', |     arduino: '', | ||||||
|     xml: '' |     xml: '' | ||||||
|   }, |   }, | ||||||
|   workspace: null, |  | ||||||
|   stats: { |   stats: { | ||||||
|     create: 0, |     create: 0, | ||||||
|     change: 0, |     change: 0, | ||||||
| @ -28,11 +27,6 @@ export default function(state = initialState, action){ | |||||||
|         ...state, |         ...state, | ||||||
|         change: state.change += 1 |         change: state.change += 1 | ||||||
|       }; |       }; | ||||||
|     case NEW_WORKSPACE: |  | ||||||
|       return { |  | ||||||
|         ...state, |  | ||||||
|         workspace: action.payload |  | ||||||
|       }; |  | ||||||
|     case CREATE_BLOCK: |     case CREATE_BLOCK: | ||||||
|     case MOVE_BLOCK: |     case MOVE_BLOCK: | ||||||
|     case CHANGE_BLOCK: |     case CHANGE_BLOCK: | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user