diff --git a/package.json b/package.json index ff3634c..9f78c9a 100644 --- a/package.json +++ b/package.json @@ -13,6 +13,7 @@ "@testing-library/react": "^9.5.0", "@testing-library/user-event": "^7.2.1", "blockly": "^3.20200625.2", + "file-saver": "^2.0.2", "prismjs": "^1.20.0", "react": "^16.13.1", "react-dom": "^16.13.1", diff --git a/public/media/1x1.gif b/public/media/blockly/1x1.gif similarity index 100% rename from public/media/1x1.gif rename to public/media/blockly/1x1.gif diff --git a/public/media/click.mp3 b/public/media/blockly/click.mp3 similarity index 100% rename from public/media/click.mp3 rename to public/media/blockly/click.mp3 diff --git a/public/media/click.ogg b/public/media/blockly/click.ogg similarity index 100% rename from public/media/click.ogg rename to public/media/blockly/click.ogg diff --git a/public/media/click.wav b/public/media/blockly/click.wav similarity index 100% rename from public/media/click.wav rename to public/media/blockly/click.wav diff --git a/public/media/delete.mp3 b/public/media/blockly/delete.mp3 similarity index 100% rename from public/media/delete.mp3 rename to public/media/blockly/delete.mp3 diff --git a/public/media/delete.ogg b/public/media/blockly/delete.ogg similarity index 100% rename from public/media/delete.ogg rename to public/media/blockly/delete.ogg diff --git a/public/media/delete.wav b/public/media/blockly/delete.wav similarity index 100% rename from public/media/delete.wav rename to public/media/blockly/delete.wav diff --git a/public/media/disconnect.mp3 b/public/media/blockly/disconnect.mp3 similarity index 100% rename from public/media/disconnect.mp3 rename to public/media/blockly/disconnect.mp3 diff --git a/public/media/disconnect.ogg b/public/media/blockly/disconnect.ogg similarity index 100% rename from public/media/disconnect.ogg rename to public/media/blockly/disconnect.ogg diff --git a/public/media/disconnect.wav b/public/media/blockly/disconnect.wav similarity index 100% rename from public/media/disconnect.wav rename to public/media/blockly/disconnect.wav diff --git a/public/media/dropdown-arrow.svg b/public/media/blockly/dropdown-arrow.svg similarity index 100% rename from public/media/dropdown-arrow.svg rename to public/media/blockly/dropdown-arrow.svg diff --git a/public/media/handclosed.cur b/public/media/blockly/handclosed.cur similarity index 100% rename from public/media/handclosed.cur rename to public/media/blockly/handclosed.cur diff --git a/public/media/handdelete.cur b/public/media/blockly/handdelete.cur similarity index 100% rename from public/media/handdelete.cur rename to public/media/blockly/handdelete.cur diff --git a/public/media/handopen.cur b/public/media/blockly/handopen.cur similarity index 100% rename from public/media/handopen.cur rename to public/media/blockly/handopen.cur diff --git a/public/media/pilcrow.png b/public/media/blockly/pilcrow.png similarity index 100% rename from public/media/pilcrow.png rename to public/media/blockly/pilcrow.png diff --git a/public/media/quote0.png b/public/media/blockly/quote0.png similarity index 100% rename from public/media/quote0.png rename to public/media/blockly/quote0.png diff --git a/public/media/quote1.png b/public/media/blockly/quote1.png similarity index 100% rename from public/media/quote1.png rename to public/media/blockly/quote1.png diff --git a/public/media/sprites.png b/public/media/blockly/sprites.png similarity index 100% rename from public/media/sprites.png rename to public/media/blockly/sprites.png diff --git a/public/media/sprites.svg b/public/media/blockly/sprites.svg similarity index 100% rename from public/media/sprites.svg rename to public/media/blockly/sprites.svg diff --git a/src/actions/types.js b/src/actions/types.js index 27bd18d..a086a29 100644 --- a/src/actions/types.js +++ b/src/actions/types.js @@ -5,6 +5,7 @@ export const MOVE_BLOCK = 'MOVE_BLOCK'; export const CHANGE_BLOCK = 'CHANGE_BLOCK'; export const DELETE_BLOCK = 'DELETE_BLOCK'; export const CLEAR_STATS = 'CLEAR_STATS'; +export const NAME = 'NAME'; export const TUTORIAL_SUCCESS = 'TUTORIAL_SUCCESS'; diff --git a/src/actions/workspaceActions.js b/src/actions/workspaceActions.js index a9700c8..ab2d4e3 100644 --- a/src/actions/workspaceActions.js +++ b/src/actions/workspaceActions.js @@ -1,4 +1,4 @@ -import { NEW_CODE, CHANGE_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, NAME } from './types'; import * as Blockly from 'blockly/core'; @@ -72,3 +72,10 @@ export const clearStats = () => (dispatch) => { payload: stats }); }; + +export const workspaceName = (name) => (dispatch) => { + dispatch({ + type: NAME, + payload: name + }) +} diff --git a/src/components/Blockly/BlocklyWindow.js b/src/components/Blockly/BlocklyWindow.js index decce8d..b988186 100644 --- a/src/components/Blockly/BlocklyWindow.js +++ b/src/components/Blockly/BlocklyWindow.js @@ -63,8 +63,8 @@ class BlocklyWindow extends Component { length: 1, colour: '#4EAF47', // senseBox-green snap: false - }} - media={'/media/'} + }} + media={'/media/blockly/'} move={this.props.move !== undefined && !this.props.move ? {} : { // https://developers.google.com/blockly/guides/configure/web/move scrollbars: true, diff --git a/src/components/Breadcrumbs.js b/src/components/Breadcrumbs.js index 71a57ea..dc0af20 100644 --- a/src/components/Breadcrumbs.js +++ b/src/components/Breadcrumbs.js @@ -1,27 +1,49 @@ import React, { Component } from 'react'; import { Link } from 'react-router-dom'; +import clsx from 'clsx'; -import Breadcrumbs from '@material-ui/core/Breadcrumbs'; +import { withStyles } from '@material-ui/core/styles'; +import MaterialUIBreadcrumbs from '@material-ui/core/Breadcrumbs'; import Typography from '@material-ui/core/Typography'; -class MyBreadcrumbs extends Component { +import { faHome } from "@fortawesome/free-solid-svg-icons"; +import { FontAwesomeIcon } from "@fortawesome/react-fontawesome"; + +const styles = (theme) => ({ + home: { + color: theme.palette.secondary.main, + width: '20px !important', + height: '20px', + marginTop: '2px' + }, + hover: { + '&:hover': { + color: theme.palette.primary.main + } + } +}); + +class Breadcrumbs extends Component { render() { return ( - this.props.content && this.props.content.length > 1 ? - + this.props.content && this.props.content.length > 0 ? + + + + {this.props.content.splice(0, this.props.content.length-1).map((content, i) => ( - {content.title} + {content.title} ))} {this.props.content.slice(-1)[0].title} - + : null ); }; } -export default MyBreadcrumbs; +export default withStyles(styles, {withTheme: true})(Breadcrumbs); diff --git a/src/components/ClearWorkspace.js b/src/components/ClearWorkspace.js deleted file mode 100644 index 2e31b01..0000000 --- a/src/components/ClearWorkspace.js +++ /dev/null @@ -1,46 +0,0 @@ -import React, {Component} from 'react'; -import PropTypes from 'prop-types'; -import { connect } from 'react-redux'; -import { clearStats, onChangeCode } from '../actions/workspaceActions'; -import { initialXml } from './Blockly/initialXml.js'; - -import * as Blockly from 'blockly/core'; - -import ListItem from '@material-ui/core/ListItem'; -import ListItemIcon from '@material-ui/core/ListItemIcon'; -import ListItemText from '@material-ui/core/ListItemText'; - -import { faTrashRestore } from "@fortawesome/free-solid-svg-icons"; -import { FontAwesomeIcon } from "@fortawesome/react-fontawesome"; - -class ClearWorkspace extends Component { - - clearWorkspace = () => { - const workspace = Blockly.getMainWorkspace(); - 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.onChangeCode(); - this.props.clearStats(); - } - - render() { - return ( - {this.clearWorkspace(); this.props.onClick();}}> - - - - ); - }; -} - -ClearWorkspace.propTypes = { - clearStats: PropTypes.func.isRequired, - onChangeCode: PropTypes.func.isRequired -}; - - -export default connect(null, { clearStats, onChangeCode })(ClearWorkspace); diff --git a/src/components/CodeViewer.js b/src/components/CodeViewer.js index bdf9677..e4f74da 100644 --- a/src/components/CodeViewer.js +++ b/src/components/CodeViewer.js @@ -7,6 +7,7 @@ import "prismjs/themes/prism.css"; import "prismjs/plugins/line-numbers/prism-line-numbers"; import "prismjs/plugins/line-numbers/prism-line-numbers.css"; +import withWidth from '@material-ui/core/withWidth'; import { withStyles } from '@material-ui/core/styles'; import MuiAccordion from '@material-ui/core/Accordion'; import MuiAccordionSummary from '@material-ui/core/AccordionSummary'; @@ -136,4 +137,4 @@ const mapStateToProps = state => ({ xml: state.workspace.code.xml }); -export default connect(mapStateToProps, null)(CodeViewer); +export default connect(mapStateToProps, null)(withWidth()(CodeViewer)); diff --git a/src/components/Compile.js b/src/components/Compile.js index 122a3b2..e966e62 100644 --- a/src/components/Compile.js +++ b/src/components/Compile.js @@ -1,6 +1,9 @@ import React, { Component } from 'react'; import PropTypes from 'prop-types'; import { connect } from 'react-redux'; +import { workspaceName } from '../actions/workspaceActions'; + +import { detectWhitespacesAndReturnReadableResult } from '../helpers/whitespace'; import { withStyles } from '@material-ui/core/styles'; import Button from '@material-ui/core/Button'; @@ -10,71 +13,136 @@ import DialogTitle from '@material-ui/core/DialogTitle'; import DialogContent from '@material-ui/core/DialogContent'; import DialogActions from '@material-ui/core/DialogActions'; import Dialog from '@material-ui/core/Dialog'; +import IconButton from '@material-ui/core/IconButton'; +import Tooltip from '@material-ui/core/Tooltip'; +import TextField from '@material-ui/core/TextField'; + +import { faCogs } from "@fortawesome/free-solid-svg-icons"; +import { FontAwesomeIcon } from "@fortawesome/react-fontawesome"; const styles = (theme) => ({ backdrop: { zIndex: theme.zIndex.drawer + 1, color: '#fff', + }, + button: { + backgroundColor: theme.palette.primary.main, + color: theme.palette.primary.contrastText, + width: '40px', + height: '40px', + '&:hover': { + backgroundColor: theme.palette.primary.main, + color: theme.palette.primary.contrastText, + } } }); class Compile extends Component { - state = { - progress: false, - open: false + constructor(props){ + super(props); + this.state = { + progress: false, + open: false, + file: false, + title: '', + content: '', + name: props.name + }; } + componentDidUpdate(props){ + if(props.name !== this.props.name){ + this.setState({name: this.props.name}); + } + } + + compile = () => { + this.setState({ progress: true }); const data = { "board": process.env.REACT_APP_BOARD, "sketch": this.props.arduino }; - this.setState({ progress: true }); fetch(`${process.env.REACT_APP_COMPILER_URL}/compile`, { method: "POST", headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(data) }) - .then(response => response.json()) - .then(data => { - console.log(data) - this.download(data.data.id) - }) - .catch(err => { - console.log(err); - this.setState({ progress: false, open: true }); + .then(response => response.json()) + .then(data => { + console.log(data); + this.setState({id: data.data.id}, () => { + this.createFileName(); }); + }) + .catch(err => { + console.log(err); + this.setState({ progress: false, file: false, open: true, title: 'Fehler', content: 'Etwas ist beim Kompilieren schief gelaufen. Versuche es nochmal.' }); + }); } - download = (id) => { - const filename = 'sketch' + download = () => { + const id = this.state.id; + const filename = detectWhitespacesAndReturnReadableResult(this.state.name); + this.toggleDialog(); + this.props.workspaceName(this.state.name); window.open(`${process.env.REACT_APP_COMPILER_URL}/download?id=${id}&board=${process.env.REACT_APP_BOARD}&filename=${filename}`, '_self'); this.setState({ progress: false }); } toggleDialog = () => { - this.setState({ open: !this.state }); + this.setState({ open: !this.state, progress: false }); + } + + createFileName = () => { + if(this.state.name){ + this.download(); + } + else{ + this.setState({ file: true, open: true, title: 'Blöcke kompilieren', content: 'Bitte gib einen Namen für die Bennenung des zu kompilierenden Programms ein und bestätige diesen mit einem Klick auf \'Eingabe\'.' }); + } + } + + setFileName = (e) => { + this.setState({name: e.target.value}); } render() { return ( -
- +
+ {this.props.iconButton ? + + this.compile()} + > + + + + : + + } - Fehler + {this.state.title} - Etwas ist beim Kompilieren schief gelaufen. Versuche es nochmal. + {this.state.content} + {this.state.file ? +
+ + +
+ : null}
-
@@ -84,11 +152,15 @@ class Compile extends Component { } Compile.propTypes = { - arduino: PropTypes.string.isRequired + arduino: PropTypes.string.isRequired, + name: PropTypes.string, + workspaceName: PropTypes.func.isRequired }; const mapStateToProps = state => ({ - arduino: state.workspace.code.arduino + arduino: state.workspace.code.arduino, + name: state.workspace.name }); -export default connect(mapStateToProps, null)(withStyles(styles, { withTheme: true })(Compile)); + +export default connect(mapStateToProps, { workspaceName })(withStyles(styles, {withTheme: true})(Compile)); diff --git a/src/components/Home.js b/src/components/Home.js index 5b6f71f..1f2b6a4 100644 --- a/src/components/Home.js +++ b/src/components/Home.js @@ -1,7 +1,7 @@ import React, { Component } from 'react'; import PropTypes from 'prop-types'; import { connect } from 'react-redux'; -import { clearStats } from '../actions/workspaceActions'; +import { clearStats, workspaceName } from '../actions/workspaceActions'; import * as Blockly from 'blockly/core'; @@ -58,6 +58,7 @@ class Home extends Component { componentWillUnmount(){ this.props.clearStats(); + this.props.workspaceName(null); } onChange = () => { @@ -72,7 +73,8 @@ class Home extends Component { render() { return (
- +
+
@@ -93,15 +95,15 @@ class Home extends Component { : null} -
); }; } Home.propTypes = { - clearStats: PropTypes.func.isRequired + clearStats: PropTypes.func.isRequired, + workspaceName: PropTypes.func.isRequired }; -export default connect(null, { clearStats })(withStyles(styles, { withTheme: true })(Home)); +export default connect(null, { clearStats, workspaceName })(withStyles(styles, { withTheme: true })(Home)); diff --git a/src/components/Navbar.js b/src/components/Navbar.js index fb5365f..55db003 100644 --- a/src/components/Navbar.js +++ b/src/components/Navbar.js @@ -1,7 +1,6 @@ import React, { Component } from 'react'; import { Link } from 'react-router-dom'; -import ClearWorkspace from './ClearWorkspace'; import senseboxLogo from './sensebox_logo.svg'; import { withRouter } from 'react-router-dom'; @@ -105,7 +104,6 @@ class Navbar extends Component { ))} - diff --git a/src/components/NotFound.js b/src/components/NotFound.js index 44b3ddb..85a9c6f 100644 --- a/src/components/NotFound.js +++ b/src/components/NotFound.js @@ -11,7 +11,7 @@ class NotFound extends Component { render() { return (
- + Die von Ihnen angeforderte Seite kann nicht gefunden werden. Die gesuchte Seite wurde möglicherweise entfernt, ihr Name wurde geändert oder sie ist vorübergehend nicht verfügbar. {this.props.button ? diff --git a/src/components/Tutorial/Assessment.js b/src/components/Tutorial/Assessment.js index 0ff93e0..da5e7f1 100644 --- a/src/components/Tutorial/Assessment.js +++ b/src/components/Tutorial/Assessment.js @@ -1,17 +1,32 @@ import React, { Component } from 'react'; import PropTypes from 'prop-types'; import { connect } from 'react-redux'; +import { workspaceName } from '../../actions/workspaceActions'; import BlocklyWindow from '../Blockly/BlocklyWindow'; import SolutionCheck from './SolutionCheck'; import CodeViewer from '../CodeViewer'; +import WorkspaceFunc from '../WorkspaceFunc'; +import withWidth, { isWidthDown } from '@material-ui/core/withWidth'; import Grid from '@material-ui/core/Grid'; import Card from '@material-ui/core/Card'; import Typography from '@material-ui/core/Typography'; class Assessment extends Component { + componentDidMount(){ + // alert(this.props.name); + this.props.workspaceName(this.props.name); + } + + componentDidUpdate(props){ + if(props.name !== this.props.name){ + // alert(this.props.name); + this.props.workspaceName(this.props.name); + } + } + render() { var tutorialId = this.props.currentTutorialId; var currentTask = this.props.step; @@ -21,18 +36,18 @@ class Assessment extends Component { return (
- {currentTask.headline} + {currentTask.headline} +
- - + - + Arbeitsauftrag {currentTask.text1} -
+
@@ -45,7 +60,8 @@ class Assessment extends Component { Assessment.propTypes = { currentTutorialId: PropTypes.number, status: PropTypes.array.isRequired, - change: PropTypes.number.isRequired + change: PropTypes.number.isRequired, + workspaceName: PropTypes.func.isRequired }; const mapStateToProps = state => ({ @@ -54,4 +70,4 @@ const mapStateToProps = state => ({ currentTutorialId: state.tutorial.currentId }); -export default connect(mapStateToProps, null)(Assessment); +export default connect(mapStateToProps, { workspaceName })(withWidth()(Assessment)); diff --git a/src/components/Tutorial/Requirement.js b/src/components/Tutorial/Requirement.js index c0c70aa..2d724b8 100644 --- a/src/components/Tutorial/Requirement.js +++ b/src/components/Tutorial/Requirement.js @@ -24,15 +24,15 @@ const styles = theme => ({ color: fade(theme.palette.secondary.main, 0.6) }, outerDivError: { - stroke: fade(theme.palette.error.dark, 0.2), - color: fade(theme.palette.error.dark, 0.2) + stroke: fade(theme.palette.error.dark, 0.6), + color: fade(theme.palette.error.dark, 0.6) }, outerDivSuccess: { - stroke: fade(theme.palette.primary.main, 0.2), - color: fade(theme.palette.primary.main, 0.2) + stroke: fade(theme.palette.primary.main, 0.6), + color: fade(theme.palette.primary.main, 0.6) }, outerDivOther: { - stroke: fade(theme.palette.secondary.main, 0.2) + stroke: fade(theme.palette.secondary.main, 0.6) }, innerDiv: { width: 'inherit', diff --git a/src/components/Tutorial/SolutionCheck.js b/src/components/Tutorial/SolutionCheck.js index 3156fc6..084d2d1 100644 --- a/src/components/Tutorial/SolutionCheck.js +++ b/src/components/Tutorial/SolutionCheck.js @@ -8,7 +8,7 @@ import { withRouter } from 'react-router-dom'; import Compile from '../Compile'; import tutorials from './tutorials.json'; -import { checkXml } from './compareXml'; +import { checkXml } from '../../helpers/compareXml'; import { withStyles } from '@material-ui/core/styles'; import IconButton from '@material-ui/core/IconButton'; @@ -60,10 +60,10 @@ class SolutionCheck extends Component { const steps = tutorials.filter(tutorial => tutorial.id === this.props.currentTutorialId)[0].steps; return (
- + this.check()} > diff --git a/src/components/Tutorial/Tutorial.js b/src/components/Tutorial/Tutorial.js index e2a9d38..acce0c9 100644 --- a/src/components/Tutorial/Tutorial.js +++ b/src/components/Tutorial/Tutorial.js @@ -1,6 +1,7 @@ import React, { Component } from 'react'; import PropTypes from 'prop-types'; import { connect } from 'react-redux'; +import { workspaceName } from '../../actions/workspaceActions'; import { tutorialId, tutorialStep } from '../../actions/tutorialActions'; import Breadcrumbs from '../Breadcrumbs'; @@ -10,6 +11,8 @@ import Instruction from './Instruction'; import Assessment from './Assessment'; import NotFound from '../NotFound'; +import { detectWhitespacesAndReturnReadableResult } from '../../helpers/whitespace'; + import tutorials from './tutorials.json'; import Card from '@material-ui/core/Card'; @@ -29,6 +32,7 @@ class Tutorial extends Component { componentWillUnmount(){ this.props.tutorialId(null); + this.props.workspaceName(null); } render() { @@ -36,12 +40,13 @@ class Tutorial extends Component { var tutorial = tutorials.filter(tutorial => tutorial.id === currentTutorialId)[0]; var steps = tutorial ? tutorial.steps : null; var step = steps ? steps[this.props.activeStep] : null; + var name = step ? `${detectWhitespacesAndReturnReadableResult(tutorial.title)}_${detectWhitespacesAndReturnReadableResult(step.headline)}` : null; return ( !Number.isInteger(currentTutorialId) || currentTutorialId < 1 || currentTutorialId > tutorials.length ? :
- + @@ -52,7 +57,7 @@ class Tutorial extends Component { {step ? step.type === 'instruction' ? - : // if step.type === 'assessment' + : // if step.type === 'assessment' : null}
@@ -69,6 +74,7 @@ class Tutorial extends Component { Tutorial.propTypes = { tutorialId: PropTypes.func.isRequired, tutorialStep: PropTypes.func.isRequired, + workspaceName: PropTypes.func.isRequired, currentTutorialId: PropTypes.number, status: PropTypes.array.isRequired, change: PropTypes.number.isRequired, @@ -82,4 +88,4 @@ const mapStateToProps = state => ({ activeStep: state.tutorial.activeStep }); -export default connect(mapStateToProps, { tutorialId, tutorialStep })(Tutorial); +export default connect(mapStateToProps, { tutorialId, tutorialStep, workspaceName })(Tutorial); diff --git a/src/components/Tutorial/TutorialHome.js b/src/components/Tutorial/TutorialHome.js index 95f775d..2bf99f2 100644 --- a/src/components/Tutorial/TutorialHome.js +++ b/src/components/Tutorial/TutorialHome.js @@ -29,15 +29,15 @@ const styles = (theme) => ({ color: fade(theme.palette.secondary.main, 0.6) }, outerDivError: { - stroke: fade(theme.palette.error.dark, 0.2), - color: fade(theme.palette.error.dark, 0.2) + stroke: fade(theme.palette.error.dark, 0.6), + color: fade(theme.palette.error.dark, 0.6) }, outerDivSuccess: { - stroke: fade(theme.palette.primary.main, 0.2), - color: fade(theme.palette.primary.main, 0.2) + stroke: fade(theme.palette.primary.main, 0.6), + color: fade(theme.palette.primary.main, 0.6) }, outerDivOther: { - stroke: fade(theme.palette.secondary.main, 0.2) + stroke: fade(theme.palette.secondary.main, 0.6) }, innerDiv: { width: 'inherit', @@ -54,7 +54,7 @@ class TutorialHome extends Component { render() { return (
- +

Tutorial-Übersicht

diff --git a/src/components/WorkspaceFunc.js b/src/components/WorkspaceFunc.js index 8c4826c..2beff96 100644 --- a/src/components/WorkspaceFunc.js +++ b/src/components/WorkspaceFunc.js @@ -1,58 +1,217 @@ import React, { Component } from 'react'; import PropTypes from 'prop-types'; import { connect } from 'react-redux'; +import { clearStats, onChangeCode, workspaceName } from '../actions/workspaceActions'; + +import * as Blockly from 'blockly/core'; + +import { saveAs } from 'file-saver'; + +import { detectWhitespacesAndReturnReadableResult } from '../helpers/whitespace'; +import { initialXml } from './Blockly/initialXml.js'; -import MaxBlocks from './MaxBlocks'; import Compile from './Compile'; +import SolutionCheck from './Tutorial/SolutionCheck'; +import withWidth, { isWidthDown } from '@material-ui/core/withWidth'; +import { withStyles } from '@material-ui/core/styles'; import Button from '@material-ui/core/Button'; import DialogTitle from '@material-ui/core/DialogTitle'; import DialogContent from '@material-ui/core/DialogContent'; import DialogActions from '@material-ui/core/DialogActions'; import Dialog from '@material-ui/core/Dialog'; +import IconButton from '@material-ui/core/IconButton'; +import Tooltip from '@material-ui/core/Tooltip'; +import TextField from '@material-ui/core/TextField'; +import Typography from '@material-ui/core/Typography'; + +import { faPen, faSave, faUpload, faShare } from "@fortawesome/free-solid-svg-icons"; +import { FontAwesomeIcon } from "@fortawesome/react-fontawesome"; + +const styles = (theme) => ({ + button: { + backgroundColor: theme.palette.primary.main, + color: theme.palette.primary.contrastText, + width: '40px', + height: '40px', + '&:hover': { + backgroundColor: theme.palette.primary.main, + color: theme.palette.primary.contrastText, + } + }, + workspaceName: { + backgroundColor: theme.palette.secondary.main, + borderRadius: '25px', + display: 'inline-flex', + cursor: 'pointer', + '&:hover': { + color: theme.palette.primary.main, + } + } +}); + class WorkspaceFunc extends Component { - state = { - title: '', - content: '', - open: false + constructor(props){ + super(props); + this.inputRef = React.createRef(); + this.state = { + title: '', + content: '', + open: false, + file: false, + saveXml: false, + name: props.name + }; } - getArduinoCode = () => { - this.setState({ title: 'Adurino Code', content: this.props.arduino, open: true }); - } - - getXMLCode = () => { - this.setState({ title: 'XML Code', content: this.props.xml, open: true }); + componentDidUpdate(props){ + if(props.name !== this.props.name){ + this.setState({name: this.props.name}); + } } toggleDialog = () => { this.setState({ open: !this.state }); } + saveXmlFile = () => { + var code = this.props.xml; + this.toggleDialog(); + var fileName = detectWhitespacesAndReturnReadableResult(this.state.name); + this.props.workspaceName(this.state.name); + fileName = `${fileName}.xml` + var blob = new Blob([code], { type: 'text/xml' }); + saveAs(blob, fileName); + } + + createFileName = () => { + if(this.state.name){ + this.saveXmlFile(); + } + else{ + this.setState({ file: true, saveXml: true, open: true, title: 'Blöcke speichern', content: 'Bitte gib einen Namen für die Bennenung der XML-Datei ein und bestätige diesen mit einem Klick auf \'Eingabe\'.' }); + } + } + + setFileName = (e) => { + this.setState({name: e.target.value}); + } + + uploadXmlFile = (xmlFile) => { + if(xmlFile.type !== 'text/xml'){ + this.setState({ open: true, file: false, title: 'Unzulässiger Dateityp', content: 'Die übergebene Datei entsprach nicht dem geforderten Format. Es sind nur XML-Dateien zulässig.' }); + } + else { + var reader = new FileReader(); + reader.readAsText(xmlFile); + reader.onloadend = () => { + var xmlDom = null; + try { + xmlDom = Blockly.Xml.textToDom(reader.result); + const workspace = Blockly.getMainWorkspace(); + var xmlBefore = this.props.xml; + workspace.clear(); + this.props.clearStats(); + Blockly.Xml.domToWorkspace(xmlDom, workspace); + if(workspace.getAllBlocks().length < 1){ + Blockly.Xml.domToWorkspace(Blockly.Xml.textToDom(xmlBefore), workspace) + this.setState({ open: true, file: false, title: 'Keine Blöcke', content: 'Es wurden keine Blöcke detektiert. Bitte überprüfe den XML-Code und versuche es erneut.' }); + } + else { + if(!this.props.solutionCheck){ + var extensionPosition = xmlFile.name.lastIndexOf('.'); + this.props.workspaceName(xmlFile.name.substr(0, extensionPosition)); + } + } + } catch(err){ + this.setState({ open: true, file: false, title: 'Ungültige XML', content: 'Die XML-Datei konnte nicht in Blöcke zerlegt werden. Bitte überprüfe den XML-Code und versuche es erneut.' }); + } + }; + } + } + + resetWorkspace = () => { + const workspace = Blockly.getMainWorkspace(); + 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.onChangeCode(); + this.props.clearStats(); + if(!this.props.solutionCheck){ + this.props.workspaceName(null); + } + } + render() { return ( -
+
+ {!this.props.solutionCheck ? + +
{this.setState({file: true, open: true, saveXml: false, title: 'Projekt benennen', content: 'Bitte gib einen Namen für das Projekt ein und bestätige diesen mit einem Klick auf \'Eingabe\'.'})}}> + {this.props.name && !isWidthDown('xs', this.props.width) ? {this.props.name} : null} +
+ +
+
+
+ : null} + {this.props.solutionCheck ? : } + + this.createFileName()} + > + + + +
+ {this.uploadXmlFile(e.target.files[0])}} + id="open-blocks" + type="file" + /> + +
+ + this.resetWorkspace()} + > + + + {this.state.title} {this.state.content} + {this.state.file ? +
+ + +
+ : null}
-
- - - -
); }; @@ -60,12 +219,17 @@ class WorkspaceFunc extends Component { WorkspaceFunc.propTypes = { arduino: PropTypes.string.isRequired, - xml: PropTypes.string.isRequired + xml: PropTypes.string.isRequired, + name: PropTypes.string, + clearStats: PropTypes.func.isRequired, + onChangeCode: PropTypes.func.isRequired, + workspaceName: PropTypes.func.isRequired }; const mapStateToProps = state => ({ arduino: state.workspace.code.arduino, - xml: state.workspace.code.xml + xml: state.workspace.code.xml, + name: state.workspace.name }); -export default connect(mapStateToProps, null)(WorkspaceFunc); +export default connect(mapStateToProps, { clearStats, onChangeCode, workspaceName })(withStyles(styles, {withTheme: true})(withWidth()(WorkspaceFunc))); diff --git a/src/components/WorkspaceStats.js b/src/components/WorkspaceStats.js index 9a98571..b072a34 100644 --- a/src/components/WorkspaceStats.js +++ b/src/components/WorkspaceStats.js @@ -4,12 +4,15 @@ import { connect } from 'react-redux'; import * as Blockly from 'blockly/core'; +import withWidth, { isWidthDown } from '@material-ui/core/withWidth'; import { withStyles } from '@material-ui/core/styles'; import Tooltip from '@material-ui/core/Tooltip'; +import IconButton from '@material-ui/core/IconButton'; import Chip from '@material-ui/core/Chip'; import Avatar from '@material-ui/core/Avatar'; +import Popover from '@material-ui/core/Popover'; -import { faPuzzlePiece, faTrash, faPlus, faPen, faArrowsAlt } from "@fortawesome/free-solid-svg-icons"; +import { faPuzzlePiece, faTrash, faPlus, faPen, faArrowsAlt, faEllipsisH } from "@fortawesome/free-solid-svg-icons"; import { FontAwesomeIcon } from "@fortawesome/react-fontawesome"; const styles = (theme) => ({ @@ -19,65 +22,123 @@ const styles = (theme) => ({ marginLeft: '50px', padding: '3px 10px', // borderRadius: '25%' + }, + menu: { + backgroundColor: theme.palette.secondary.main, + color: theme.palette.secondary.contrastText, + width: '40px', + height: '40px', + '&:hover': { + backgroundColor: theme.palette.secondary.main, + color: theme.palette.primary.main, + } } }); class WorkspaceStats extends Component { + state={ + anchor: null + } + + handleClose = () => { + this.setState({ anchor: null }); + } + + handleClick = (event) => { + this.setState({ anchor: event.currentTarget }); + }; + render() { + const bigDisplay = !isWidthDown('sm', this.props.width); const workspace = Blockly.getMainWorkspace(); const remainingBlocksInfinity = workspace ? workspace.remainingCapacity() !== Infinity : null; + const stats =
+ + } + label={workspace ? workspace.getAllBlocks().length : 0}> + + + + } + label={this.props.create > 0 ? this.props.create : 0}> {/* initialXML is created automatically, Block is not part of the statistics */} + + + + } + label={this.props.change}> + + + + } + label={this.props.move > 0 ? this.props.move : 0}> {/* initialXML is moved automatically, Block is not part of the statistics */} + + + + } + label={this.props.delete}> + + + {remainingBlocksInfinity ? + + + + : null} +
return ( -
- - } - label={workspace ? workspace.getAllBlocks().length : 0}> - - - - } - label={this.props.create > 0 ? this.props.create : 0}> {/* initialXML is created automatically, Block is not part of the statistics */} - - - - } - label={this.props.change}> - - - - } - label={this.props.move > 0 ? this.props.move : 0}> {/* initialXML is moved automatically, Block is not part of the statistics */} - - - - } - label={this.props.delete}> - - - {remainingBlocksInfinity ? - - - - : null} -
+ bigDisplay ? +
+ {stats} +
+ : +
+ + this.handleClick(event)} + > + + + + +
+ {stats} +
+
+
); }; } @@ -98,4 +159,4 @@ const mapStateToProps = state => ({ workspaceChange: state.workspace.change }); -export default connect(mapStateToProps, null)(withStyles(styles, { withTheme: true })(WorkspaceStats)); +export default connect(mapStateToProps, null)(withStyles(styles, { withTheme: true })(withWidth()(WorkspaceStats))); diff --git a/src/components/Tutorial/compareXml.js b/src/helpers/compareXml.js similarity index 84% rename from src/components/Tutorial/compareXml.js rename to src/helpers/compareXml.js index 5ca4619..40b6fae 100644 --- a/src/components/Tutorial/compareXml.js +++ b/src/helpers/compareXml.js @@ -12,11 +12,14 @@ const parseXml = (xmlString) => { const compareNumberOfBlocks = (originalBlocks, userBlocks) => { if(originalBlocks.length !== userBlocks.length){ + var blocks; if(originalBlocks.length > userBlocks.length){ - return {text: 'Es wurden zu wenig Blöcke verwendet.', type: 'error'}; + blocks = originalBlocks.length-userBlocks.length; + return {text: `Es wurde${blocks === 1 ? '' : 'n'} ${blocks} Bl${blocks === 1 ? 'ock' : 'öcke'} zu wenig verwendet.`, type: 'error'}; } else { - return {text: 'Es wurden zu viele Blöcke verwendet.', type: 'error'}; + blocks = userBlocks.length-originalBlocks.length; + return {text: `Es wurde${blocks === 1 ? '' : 'n'} ${blocks} Bl${blocks === 1 ? 'ock' : 'öcke'} zu viel verwendet.`, type: 'error'}; } } }; @@ -74,5 +77,5 @@ const compareXml = (originalXml, userXml) => { if(parent){return parent;} } - return {text: 'Super. Alles richtig!', type: 'success'}; + return {text: 'Super, alles richtig! Kompiliere nun die benutzen Blöcke, um eine BIN-Datei zu erhalten und damit das Programm auf die senseBox zu spielen und ausführen zu können.', type: 'success'}; }; diff --git a/src/helpers/whitespace.js b/src/helpers/whitespace.js new file mode 100644 index 0000000..e51bae2 --- /dev/null +++ b/src/helpers/whitespace.js @@ -0,0 +1,15 @@ +export const detectWhitespacesAndReturnReadableResult = (word) => { + var readableResult = ''; + var space = false; + for(var i = 0; i < word.length; i++){ + var letter = word[i]; + if(/\s/g.test(letter)){ + space = true; + } + else { + readableResult += space ? letter.toUpperCase() : letter; + space = false; + } + } + return readableResult; +}; diff --git a/src/reducers/workspaceReducer.js b/src/reducers/workspaceReducer.js index dc4987f..b9e3681 100644 --- a/src/reducers/workspaceReducer.js +++ b/src/reducers/workspaceReducer.js @@ -1,4 +1,4 @@ -import { CHANGE_WORKSPACE, NEW_CODE, 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, NAME } from '../actions/types'; const initialState = { @@ -12,7 +12,8 @@ const initialState = { delete: 0, move: -1 // initialXML is moved automatically, Block is not part of the statistics }, - change: 0 + change: 0, + name: null }; export default function(state = initialState, action){ @@ -36,6 +37,11 @@ export default function(state = initialState, action){ ...state, stats: action.payload }; + case NAME: + return { + ...state, + name: action.payload + } default: return state; } diff --git a/src/store.js b/src/store.js index bd28ae1..bf6460e 100644 --- a/src/store.js +++ b/src/store.js @@ -11,7 +11,7 @@ const store = createStore( initialState, compose( applyMiddleware(...middleware), - window.__REDUX_DEVTOOLS_EXTENSION__ && window.__REDUX_DEVTOOLS_EXTENSION__() + // window.__REDUX_DEVTOOLS_EXTENSION__ && window.__REDUX_DEVTOOLS_EXTENSION__() ) );