From baa047f8d7a8669658c2cdc7e9fa66163c150b61 Mon Sep 17 00:00:00 2001 From: Delucse <46593742+Delucse@users.noreply.github.com> Date: Mon, 10 Aug 2020 14:30:08 +0200 Subject: [PATCH] "code-on", "close trashcan", "delete trashcan" buttons --- src/components/Home.js | 52 ++++++++++++++--- src/components/TrashcanButtons.js | 94 +++++++++++++++++++++++++++++++ 2 files changed, 138 insertions(+), 8 deletions(-) create mode 100644 src/components/TrashcanButtons.js diff --git a/src/components/Home.js b/src/components/Home.js index 3038ba0..3779d3b 100644 --- a/src/components/Home.js +++ b/src/components/Home.js @@ -6,10 +6,36 @@ import WorkspaceStats from './WorkspaceStats'; import WorkspaceFunc from './WorkspaceFunc'; import BlocklyWindow from './Blockly/BlocklyWindow'; import CodeViewer from './CodeViewer'; +import TrashcanButtons from './TrashcanButtons'; import Grid from '@material-ui/core/Grid'; -import FormControlLabel from '@material-ui/core/FormControlLabel'; -import Switch from '@material-ui/core/Switch'; +import IconButton from '@material-ui/core/IconButton'; +import Tooltip from '@material-ui/core/Tooltip'; +import { withStyles } from '@material-ui/core/styles'; + +import { faCode } from "@fortawesome/free-solid-svg-icons"; +import { FontAwesomeIcon } from "@fortawesome/react-fontawesome"; + +const styles = (theme) => ({ + codeOn: { + backgroundColor: theme.palette.primary.main, + color: theme.palette.primary.contrastText, + '&:hover': { + backgroundColor: theme.palette.primary.contrastText, + color: theme.palette.primary.main, + border: `1px solid ${theme.palette.secondary.main}` + } + }, + codeOff: { + backgroundColor: theme.palette.primary.contrastText, + color: theme.palette.primary.main, + border: `1px solid ${theme.palette.secondary.main}`, + '&:hover': { + backgroundColor: theme.palette.primary.main, + color: theme.palette.primary.contrastText, + } + } +}); class Home extends Component { @@ -29,6 +55,11 @@ class Home extends Component { onChange = () => { this.setState({ codeOn: !this.state.codeOn }); + const workspace = Blockly.getMainWorkspace(); + // https://github.com/google/blockly/blob/master/core/blockly.js#L314 + if (workspace.trashcan && workspace.trashcan.flyout) { + workspace.trashcan.flyout.hide(); // in case of resize, the trash flyout does not reposition + } } render() { @@ -37,11 +68,16 @@ class Home extends Component { - } - label="Code" - /> + + this.onChange()} + > + + + + {this.state.codeOn ? @@ -56,4 +92,4 @@ class Home extends Component { }; } -export default Home; +export default withStyles(styles, { withTheme: true })(Home); diff --git a/src/components/TrashcanButtons.js b/src/components/TrashcanButtons.js new file mode 100644 index 0000000..2545d59 --- /dev/null +++ b/src/components/TrashcanButtons.js @@ -0,0 +1,94 @@ +import React, { Component } from 'react'; +import PropTypes from 'prop-types'; +import { connect } from 'react-redux'; + +import * as Blockly from 'blockly/core'; + +import IconButton from '@material-ui/core/IconButton'; +import Tooltip from '@material-ui/core/Tooltip'; +import { withStyles } from '@material-ui/core/styles'; + +import { faTimes, faTrash } from "@fortawesome/free-solid-svg-icons"; +import { FontAwesomeIcon } from "@fortawesome/react-fontawesome"; + +const styles = (theme) => ({ + closeTrash: { + '&:hover': { + backgroundColor: 'transparent', + color: theme.palette.primary.main, + } + }, + deleteTrash: { + '&:hover': { + backgroundColor: 'transparent', + color: theme.palette.primary.main, + } + } +}); + + +class TrashcanButtons extends Component { + + state = { + closeTrashFlyout: false + } + + componentDidUpdate(previousProps, previousState) { + const workspace = Blockly.getMainWorkspace(); + const contentsIsOpen = workspace.trashcan.contentsIsOpen(); + if(previousState.closeTrashFlyout !== contentsIsOpen){ + this.setState({ closeTrashFlyout: contentsIsOpen }); + } + } + + closeTrashcan = () => { + this.setState({ closeTrashFlyout: false }); + const workspace = Blockly.getMainWorkspace(); + // https://github.com/google/blockly/blob/master/core/blockly.js#L314 + workspace.trashcan.flyout.hide(); + }; + + clearTrashcan = () => { + this.setState({ closeTrashFlyout: false }); + const workspace = Blockly.getMainWorkspace(); + // https://developers.google.com/blockly/reference/js/Blockly.Trashcan#emptyContents + workspace.trashcan.emptyContents(); + } + + render() { + return ( + this.state.closeTrashFlyout ? +
+ + this.closeTrashcan()} + > + + + + + this.clearTrashcan()} + > + + + +
+ : null + ); + }; +} + +TrashcanButtons.propTypes = { + workspaceChange: PropTypes.number.isRequired +}; + +const mapStateToProps = state => ({ + workspaceChange: state.workspace.change +}); + +export default connect(mapStateToProps, null)(withStyles(styles, { withTheme: true })(TrashcanButtons));