2020-08-05 18:08:23 +00:00
|
|
|
import React, { useEffect } from 'react';
|
2020-06-09 19:51:57 +00:00
|
|
|
import PropTypes from 'prop-types';
|
2020-06-15 20:46:56 +00:00
|
|
|
import { connect } from 'react-redux';
|
|
|
|
import { withRouter } from 'react-router';
|
|
|
|
import { useState } from 'react';
|
2020-07-15 20:24:12 +00:00
|
|
|
import styled from 'styled-components';
|
2020-06-12 19:09:30 +00:00
|
|
|
|
2020-06-15 20:46:56 +00:00
|
|
|
// Imports to be Refactored
|
|
|
|
import { bindActionCreators } from 'redux';
|
2020-07-24 19:30:14 +00:00
|
|
|
|
2020-06-15 20:46:56 +00:00
|
|
|
import * as IDEActions from '../actions/ide';
|
|
|
|
import * as ProjectActions from '../actions/project';
|
|
|
|
|
|
|
|
// Local Imports
|
|
|
|
import Editor from '../components/Editor';
|
2020-07-30 18:44:03 +00:00
|
|
|
import { PlayIcon, MoreIcon } from '../../../common/icons';
|
2020-08-12 13:55:54 +00:00
|
|
|
import UnsavedChangesDotIcon from '../../../images/unsaved-changes-dot.svg';
|
2020-06-15 20:46:56 +00:00
|
|
|
|
2020-06-16 20:38:43 +00:00
|
|
|
import IconButton from '../../../components/mobile/IconButton';
|
|
|
|
import Header from '../../../components/mobile/Header';
|
|
|
|
import Screen from '../../../components/mobile/MobileScreen';
|
|
|
|
import Footer from '../../../components/mobile/Footer';
|
2020-06-18 19:01:13 +00:00
|
|
|
import IDEWrapper from '../../../components/mobile/IDEWrapper';
|
2020-07-15 20:24:12 +00:00
|
|
|
import Console from '../components/Console';
|
|
|
|
import { remSize } from '../../../theme';
|
2020-07-29 19:01:40 +00:00
|
|
|
// import OverlayManager from '../../../components/OverlayManager';
|
2020-07-21 15:14:58 +00:00
|
|
|
import ActionStrip from '../../../components/mobile/ActionStrip';
|
2020-07-30 17:30:45 +00:00
|
|
|
import useAsModal from '../../../components/useAsModal';
|
2020-07-29 19:01:40 +00:00
|
|
|
import { PreferencesIcon } from '../../../common/icons';
|
|
|
|
import Dropdown from '../../../components/Dropdown';
|
2020-06-16 20:23:49 +00:00
|
|
|
|
2020-07-24 21:11:10 +00:00
|
|
|
const isUserOwner = ({ project, user }) =>
|
|
|
|
project.owner && project.owner.id === user.id;
|
2020-07-21 15:14:58 +00:00
|
|
|
|
2020-08-12 13:56:46 +00:00
|
|
|
const withChangeDot = (title, unsavedChanges = false) => (
|
2020-08-12 13:55:54 +00:00
|
|
|
<span>
|
|
|
|
{title}
|
|
|
|
<span className="editor__unsaved-changes">
|
|
|
|
{unsavedChanges &&
|
|
|
|
<UnsavedChangesDotIcon role="img" aria-label="Sketch has unsaved changes" focusable="false" />}
|
|
|
|
</span>
|
|
|
|
</span>
|
|
|
|
);
|
|
|
|
|
2020-07-21 15:14:58 +00:00
|
|
|
const Expander = styled.div`
|
|
|
|
height: ${props => (props.expanded ? remSize(160) : remSize(27))};
|
2020-07-20 21:51:42 +00:00
|
|
|
`;
|
|
|
|
|
2020-07-29 21:09:22 +00:00
|
|
|
const NavItem = styled.li`
|
|
|
|
position: relative;
|
|
|
|
`;
|
|
|
|
|
2020-08-05 18:08:23 +00:00
|
|
|
const getNatOptions = (username = undefined) =>
|
|
|
|
(username
|
|
|
|
? [
|
|
|
|
{ icon: PreferencesIcon, title: 'Preferences', href: '/mobile/preferences', },
|
|
|
|
{ icon: PreferencesIcon, title: 'My Stuff', href: `/mobile/${username}/sketches` },
|
|
|
|
{ icon: PreferencesIcon, title: 'Examples', href: '/mobile/p5/sketches' },
|
|
|
|
{ icon: PreferencesIcon, title: 'Original Editor', href: '/', },
|
|
|
|
]
|
|
|
|
: [
|
|
|
|
{ icon: PreferencesIcon, title: 'Preferences', href: '/mobile/preferences', },
|
|
|
|
{ icon: PreferencesIcon, title: 'Examples', href: '/mobile/p5/sketches' },
|
|
|
|
{ icon: PreferencesIcon, title: 'Original Editor', href: '/', },
|
|
|
|
]
|
|
|
|
);
|
|
|
|
|
2020-06-22 18:10:20 +00:00
|
|
|
const MobileIDEView = (props) => {
|
2020-06-15 20:59:11 +00:00
|
|
|
const {
|
2020-08-12 14:27:22 +00:00
|
|
|
ide, project, selectedFile, user, params, unsavedChanges,
|
2020-08-12 14:15:36 +00:00
|
|
|
stopSketch, startSketch, getProject, clearPersistedState
|
2020-06-15 20:59:11 +00:00
|
|
|
} = props;
|
2020-06-12 19:09:30 +00:00
|
|
|
|
2020-06-29 17:45:17 +00:00
|
|
|
const [tmController, setTmController] = useState(null); // eslint-disable-line
|
2020-06-16 19:25:40 +00:00
|
|
|
|
2020-07-30 19:13:00 +00:00
|
|
|
const { username } = user;
|
2020-08-12 14:27:22 +00:00
|
|
|
const { consoleIsExpanded } = ide;
|
|
|
|
const { name: filename } = selectedFile;
|
2020-07-21 22:03:22 +00:00
|
|
|
|
2020-07-31 13:21:26 +00:00
|
|
|
const [triggerNavDropdown, NavDropDown] = useAsModal(<Dropdown
|
2020-08-05 18:08:23 +00:00
|
|
|
items={getNatOptions(username)}
|
2020-07-31 13:49:19 +00:00
|
|
|
align="right"
|
2020-07-31 13:21:26 +00:00
|
|
|
/>);
|
2020-06-16 19:25:40 +00:00
|
|
|
|
2020-08-05 18:27:22 +00:00
|
|
|
// Force state reset
|
|
|
|
useEffect(clearPersistedState, []);
|
|
|
|
useEffect(stopSketch, []);
|
|
|
|
|
2020-08-05 18:08:23 +00:00
|
|
|
// Load Project
|
2020-08-05 18:27:22 +00:00
|
|
|
const [currentProjectID, setCurrentProjectID] = useState(null);
|
2020-08-05 18:08:23 +00:00
|
|
|
useEffect(() => {
|
2020-08-06 13:08:41 +00:00
|
|
|
if (!username) return;
|
2020-08-05 18:27:22 +00:00
|
|
|
if (params.project_id && !currentProjectID) {
|
|
|
|
if (params.project_id !== project.id) {
|
2020-08-06 13:08:41 +00:00
|
|
|
getProject(params.project_id, params.username);
|
2020-08-05 18:27:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
setCurrentProjectID(params.project_id);
|
2020-08-06 13:08:41 +00:00
|
|
|
}, [params, project, username]);
|
2020-08-05 18:08:23 +00:00
|
|
|
|
|
|
|
|
2020-06-15 20:46:56 +00:00
|
|
|
return (
|
2020-06-23 18:54:09 +00:00
|
|
|
<Screen fullscreen>
|
2020-06-30 22:11:48 +00:00
|
|
|
<Header
|
2020-08-12 13:56:46 +00:00
|
|
|
title={withChangeDot(project.name, unsavedChanges)}
|
2020-08-12 14:27:22 +00:00
|
|
|
subtitle={filename}
|
2020-06-30 22:11:48 +00:00
|
|
|
>
|
2020-07-29 21:09:22 +00:00
|
|
|
<NavItem>
|
2020-07-29 20:52:57 +00:00
|
|
|
<IconButton
|
|
|
|
onClick={triggerNavDropdown}
|
|
|
|
icon={MoreIcon}
|
|
|
|
aria-label="Options"
|
|
|
|
/>
|
|
|
|
<NavDropDown />
|
2020-07-29 21:09:22 +00:00
|
|
|
</NavItem>
|
2020-07-29 20:52:57 +00:00
|
|
|
<li>
|
|
|
|
<IconButton to="/mobile/preview" onClick={() => { startSketch(); }} icon={PlayIcon} aria-label="Run sketch" />
|
|
|
|
</li>
|
2020-06-15 20:59:11 +00:00
|
|
|
</Header>
|
2020-06-12 19:09:30 +00:00
|
|
|
|
2020-06-18 19:01:13 +00:00
|
|
|
<IDEWrapper>
|
2020-08-12 14:15:36 +00:00
|
|
|
<Editor provideController={setTmController} />
|
2020-06-18 19:01:13 +00:00
|
|
|
</IDEWrapper>
|
2020-07-22 18:37:44 +00:00
|
|
|
|
2020-07-20 21:51:42 +00:00
|
|
|
<Footer>
|
2020-08-12 14:27:22 +00:00
|
|
|
{consoleIsExpanded && (
|
2020-07-24 21:11:10 +00:00
|
|
|
<Expander expanded>
|
|
|
|
<Console />
|
|
|
|
</Expander>
|
|
|
|
)}
|
2020-07-21 15:14:58 +00:00
|
|
|
<ActionStrip />
|
2020-07-15 20:24:12 +00:00
|
|
|
</Footer>
|
2020-06-15 20:46:56 +00:00
|
|
|
</Screen>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2020-06-22 18:10:20 +00:00
|
|
|
MobileIDEView.propTypes = {
|
2020-06-15 20:46:56 +00:00
|
|
|
ide: PropTypes.shape({
|
|
|
|
consoleIsExpanded: PropTypes.bool.isRequired,
|
|
|
|
}).isRequired,
|
|
|
|
|
|
|
|
project: PropTypes.shape({
|
|
|
|
id: PropTypes.string,
|
|
|
|
name: PropTypes.string.isRequired,
|
|
|
|
owner: PropTypes.shape({
|
|
|
|
username: PropTypes.string,
|
2020-07-24 21:11:10 +00:00
|
|
|
id: PropTypes.string,
|
2020-06-15 20:46:56 +00:00
|
|
|
}),
|
|
|
|
}).isRequired,
|
|
|
|
|
|
|
|
|
|
|
|
selectedFile: PropTypes.shape({
|
|
|
|
id: PropTypes.string.isRequired,
|
|
|
|
content: PropTypes.string.isRequired,
|
2020-07-24 21:11:10 +00:00
|
|
|
name: PropTypes.string.isRequired,
|
2020-06-15 20:46:56 +00:00
|
|
|
}).isRequired,
|
|
|
|
|
2020-06-15 21:59:21 +00:00
|
|
|
user: PropTypes.shape({
|
|
|
|
authenticated: PropTypes.bool.isRequired,
|
|
|
|
id: PropTypes.string,
|
2020-07-24 21:11:10 +00:00
|
|
|
username: PropTypes.string,
|
2020-06-15 21:59:21 +00:00
|
|
|
}).isRequired,
|
2020-08-05 18:27:22 +00:00
|
|
|
|
2020-08-06 13:08:41 +00:00
|
|
|
params: PropTypes.shape({
|
|
|
|
project_id: PropTypes.string,
|
|
|
|
username: PropTypes.string
|
|
|
|
}).isRequired,
|
2020-08-12 14:27:22 +00:00
|
|
|
|
|
|
|
unsavedChanges: PropTypes.bool.isRequired,
|
|
|
|
|
|
|
|
startSketch: PropTypes.func.isRequired,
|
|
|
|
stopSketch: PropTypes.func.isRequired,
|
|
|
|
getProject: PropTypes.func.isRequired,
|
|
|
|
clearPersistedState: PropTypes.func.isRequired,
|
2020-06-15 20:46:56 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
function mapStateToProps(state) {
|
|
|
|
return {
|
2020-07-24 21:11:10 +00:00
|
|
|
selectedFile:
|
|
|
|
state.files.find(file => file.isSelectedFile) ||
|
2020-06-15 20:46:56 +00:00
|
|
|
state.files.find(file => file.name === 'sketch.js') ||
|
|
|
|
state.files.find(file => file.name !== 'root'),
|
|
|
|
ide: state.ide,
|
2020-08-12 14:27:22 +00:00
|
|
|
unsavedChanges: state.ide.unsavedChanges,
|
2020-06-15 20:46:56 +00:00
|
|
|
preferences: state.preferences,
|
|
|
|
user: state.user,
|
|
|
|
project: state.project,
|
|
|
|
toast: state.toast,
|
2020-07-24 21:11:10 +00:00
|
|
|
console: state.console,
|
2020-06-15 20:46:56 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-08-12 14:31:37 +00:00
|
|
|
const mapDispatchToProps = dispatch => bindActionCreators({
|
|
|
|
...ProjectActions,
|
|
|
|
...IDEActions
|
|
|
|
}, dispatch);
|
2020-06-15 20:46:56 +00:00
|
|
|
|
2020-06-22 18:10:20 +00:00
|
|
|
export default withRouter(connect(mapStateToProps, mapDispatchToProps)(MobileIDEView));
|