Merge branch 'feature/mobile-save-sketch' of https://github.com/ghalestrilo/p5.js-web-editor into feature/mobile-save-sketch

This commit is contained in:
ghalestrilo 2020-08-17 15:27:25 -03:00
commit c0b9ae445e
8 changed files with 209 additions and 43 deletions

View file

@ -14,6 +14,7 @@ import Preferences from '../images/preferences.svg';
import Play from '../images/triangle-arrow-right.svg'; import Play from '../images/triangle-arrow-right.svg';
import More from '../images/more.svg'; import More from '../images/more.svg';
import Code from '../images/code.svg'; import Code from '../images/code.svg';
import Save from '../images/save.svg';
import Terminal from '../images/terminal.svg'; import Terminal from '../images/terminal.svg';
@ -81,3 +82,4 @@ export const PlayIcon = withLabel(Play);
export const MoreIcon = withLabel(More); export const MoreIcon = withLabel(More);
export const TerminalIcon = withLabel(Terminal); export const TerminalIcon = withLabel(Terminal);
export const CodeIcon = withLabel(Code); export const CodeIcon = withLabel(Code);
export const SaveIcon = withLabel(Save);

View file

@ -1,13 +1,12 @@
import React from 'react'; import React from 'react';
import PropTypes from 'prop-types';
import styled from 'styled-components'; import styled from 'styled-components';
import { bindActionCreators } from 'redux';
import { useDispatch, useSelector } from 'react-redux';
import { remSize } from '../../theme'; import { remSize } from '../../theme';
import IconButton from './IconButton'; import IconButton from './IconButton';
import { TerminalIcon } from '../../common/icons';
import * as IDEActions from '../../modules/IDE/actions/ide';
const BottomBarContent = styled.h2` const BottomBarContent = styled.div`
display: grid;
grid-template-columns: repeat(8,1fr);
padding: ${remSize(8)}; padding: ${remSize(8)};
svg { svg {
@ -15,13 +14,7 @@ const BottomBarContent = styled.h2`
} }
`; `;
export default () => { const ActionStrip = ({ actions }) => (
const { expandConsole, collapseConsole } = bindActionCreators(IDEActions, useDispatch());
const { consoleIsExpanded } = useSelector(state => state.ide);
const actions = [{ icon: TerminalIcon, aria: 'Say Something', action: consoleIsExpanded ? collapseConsole : expandConsole }];
return (
<BottomBarContent> <BottomBarContent>
{actions.map(({ icon, aria, action }) => {actions.map(({ icon, aria, action }) =>
(<IconButton (<IconButton
@ -30,6 +23,14 @@ export default () => {
key={`bottom-bar-${aria}`} key={`bottom-bar-${aria}`}
onClick={() => action()} onClick={() => action()}
/>))} />))}
</BottomBarContent> </BottomBarContent>);
);
ActionStrip.propTypes = {
actions: PropTypes.arrayOf(PropTypes.shape({
icon: PropTypes.element.isRequired,
aria: PropTypes.string.isRequired,
action: PropTypes.func.isRequired
})).isRequired
}; };
export default ActionStrip;

View file

@ -77,8 +77,9 @@ const Header = ({
</HeaderDiv> </HeaderDiv>
); );
Header.propTypes = { Header.propTypes = {
title: PropTypes.string, title: PropTypes.oneOfType([PropTypes.string, PropTypes.element]),
subtitle: PropTypes.string, subtitle: PropTypes.string,
leftButton: PropTypes.element, leftButton: PropTypes.element,
children: PropTypes.oneOfType([PropTypes.element, PropTypes.arrayOf(PropTypes.element)]), children: PropTypes.oneOfType([PropTypes.element, PropTypes.arrayOf(PropTypes.element)]),

1
client/images/save.svg Normal file
View file

@ -0,0 +1 @@
<svg xmlns="http://www.w3.org/2000/svg" height="24" viewBox="0 0 24 24" width="24"><path d="M0 0h24v24H0z" fill="none"/><path d="M17 3H5c-1.11 0-2 .9-2 2v14c0 1.1.89 2 2 2h14c1.1 0 2-.9 2-2V7l-4-4zm-5 16c-1.66 0-3-1.34-3-3s1.34-3 3-3 3 1.34 3 3-1.34 3-3 3zm3-10H5V5h10v4z"/></svg>

After

Width:  |  Height:  |  Size: 280 B

View file

@ -126,7 +126,7 @@ function getSynchedProject(currentState, responseProject) {
}; };
} }
export function saveProject(selectedFile = null, autosave = false) { export function saveProject(selectedFile = null, autosave = false, mobile = false) {
return (dispatch, getState) => { return (dispatch, getState) => {
const state = getState(); const state = getState();
if (state.project.isSaving) { if (state.project.isSaving) {
@ -185,16 +185,15 @@ export function saveProject(selectedFile = null, autosave = false) {
.then((response) => { .then((response) => {
dispatch(endSavingProject()); dispatch(endSavingProject());
const { hasChanges, synchedProject } = getSynchedProject(getState(), response.data); const { hasChanges, synchedProject } = getSynchedProject(getState(), response.data);
dispatch(setNewProject(synchedProject));
dispatch(setUnsavedChanges(false));
browserHistory.push(`${mobile ? '/mobile' : ''}/${response.data.user.username}/sketches/${response.data.id}`);
if (hasChanges) { if (hasChanges) {
dispatch(setNewProject(synchedProject));
dispatch(setUnsavedChanges(false));
browserHistory.push(`/${response.data.user.username}/sketches/${response.data.id}`);
dispatch(setUnsavedChanges(true)); dispatch(setUnsavedChanges(true));
} else {
dispatch(setNewProject(synchedProject));
dispatch(setUnsavedChanges(false));
browserHistory.push(`/${response.data.user.username}/sketches/${response.data.id}`);
} }
dispatch(projectSaveSuccess()); dispatch(projectSaveSuccess());
if (!autosave) { if (!autosave) {
if (state.preferences.autosave) { if (state.preferences.autosave) {
@ -222,9 +221,9 @@ export function saveProject(selectedFile = null, autosave = false) {
}; };
} }
export function autosaveProject() { export function autosaveProject(mobile = false) {
return (dispatch, getState) => { return (dispatch, getState) => {
saveProject(null, true)(dispatch, getState); saveProject(null, true, mobile)(dispatch, getState);
}; };
} }

View file

@ -10,6 +10,9 @@ import { bindActionCreators } from 'redux';
import * as IDEActions from '../actions/ide'; import * as IDEActions from '../actions/ide';
import * as ProjectActions from '../actions/project'; import * as ProjectActions from '../actions/project';
import * as ConsoleActions from '../actions/console';
import * as PreferencesActions from '../actions/preferences';
// Local Imports // Local Imports
import Editor from '../components/Editor'; import Editor from '../components/Editor';
@ -26,11 +29,13 @@ import { remSize } from '../../../theme';
// import OverlayManager from '../../../components/OverlayManager'; // import OverlayManager from '../../../components/OverlayManager';
import ActionStrip from '../../../components/mobile/ActionStrip'; import ActionStrip from '../../../components/mobile/ActionStrip';
import useAsModal from '../../../components/useAsModal'; import useAsModal from '../../../components/useAsModal';
import { PreferencesIcon } from '../../../common/icons'; import { PreferencesIcon, TerminalIcon, SaveIcon } from '../../../common/icons';
import Dropdown from '../../../components/Dropdown'; import Dropdown from '../../../components/Dropdown';
const isUserOwner = ({ project, user }) =>
project.owner && project.owner.id === user.id; import { useEffectWithComparison, useEventListener } from '../../../utils/custom-hooks';
import * as device from '../../../utils/device';
const withChangeDot = (title, unsavedChanges = false) => ( const withChangeDot = (title, unsavedChanges = false) => (
<span> <span>
@ -65,13 +70,111 @@ const getNatOptions = (username = undefined) =>
] ]
); );
const MobileIDEView = (props) => {
const isUserOwner = ({ project, user }) =>
project && project.owner && project.owner.id === user.id;
const canSaveProject = (project, user) =>
isUserOwner({ project, user }) || (user.authenticated && !project.owner);
// TODO: This could go into <Editor />
const handleGlobalKeydown = (props, cmController) => (e) => {
const { const {
ide, project, selectedFile, user, params, unsavedChanges, user, project, ide,
stopSketch, startSketch, getProject, clearPersistedState setAllAccessibleOutput,
saveProject, cloneProject, showErrorModal, startSketch, stopSketch,
expandSidebar, collapseSidebar, expandConsole, collapseConsole,
closeNewFolderModal, closeUploadFileModal, closeNewFileModal
} = props; } = props;
const [tmController, setTmController] = useState(null); // eslint-disable-line
const isMac = device.isMac();
// const ctrlDown = (e.metaKey && this.isMac) || (e.ctrlKey && !this.isMac);
const ctrlDown = (isMac ? e.metaKey : e.ctrlKey);
if (ctrlDown) {
if (e.shiftKey) {
if (e.keyCode === 13) {
e.preventDefault();
e.stopPropagation();
stopSketch();
} else if (e.keyCode === 13) {
e.preventDefault();
e.stopPropagation();
startSketch();
// 50 === 2
} else if (e.keyCode === 50
) {
e.preventDefault();
setAllAccessibleOutput(false);
// 49 === 1
} else if (e.keyCode === 49) {
e.preventDefault();
setAllAccessibleOutput(true);
}
} else if (e.keyCode === 83) {
// 83 === s
e.preventDefault();
e.stopPropagation();
if (canSaveProject(project, user)) saveProject(cmController.getContent(), false, true);
else if (user.authenticated) cloneProject();
else showErrorModal('forceAuthentication');
// 13 === enter
} else if (e.keyCode === 66) {
e.preventDefault();
if (!ide.sidebarIsExpanded) expandSidebar();
else collapseSidebar();
}
} else if (e.keyCode === 192 && e.ctrlKey) {
e.preventDefault();
if (ide.consoleIsExpanded) collapseConsole();
else expandConsole();
} else if (e.keyCode === 27) {
if (ide.newFolderModalVisible) closeNewFolderModal();
else if (ide.uploadFileModalVisible) closeUploadFileModal();
else if (ide.modalIsVisible) closeNewFileModal();
}
};
const autosave = (autosaveInterval, setAutosaveInterval) => (props, prevProps) => {
const {
autosaveProject, preferences, ide, selectedFile: file, project, user
} = props;
const { selectedFile: oldFile } = prevProps;
const doAutosave = () => autosaveProject(true);
if (isUserOwner(props) && project.id) {
if (preferences.autosave && ide.unsavedChanges && !ide.justOpenedProject) {
if (file.name === oldFile.name && file.content !== oldFile.content) {
if (autosaveInterval) {
clearTimeout(autosaveInterval);
}
console.log('will save project in 20 seconds');
setAutosaveInterval(setTimeout(doAutosave, 20000));
}
} else if (autosaveInterval && !preferences.autosave) {
clearTimeout(autosaveInterval);
setAutosaveInterval(null);
}
} else if (autosaveInterval) {
clearTimeout(autosaveInterval);
setAutosaveInterval(null);
}
};
const MobileIDEView = (props) => {
const {
ide, preferences, project, selectedFile, user, params, unsavedChanges, expandConsole, collapseConsole,
stopSketch, startSketch, getProject, clearPersistedState, autosaveProject, saveProject
} = props;
const [cmController, setCmController] = useState(null); // eslint-disable-line
const { username } = user; const { username } = user;
const { consoleIsExpanded } = ide; const { consoleIsExpanded } = ide;
@ -84,7 +187,10 @@ const MobileIDEView = (props) => {
// Force state reset // Force state reset
useEffect(clearPersistedState, []); useEffect(clearPersistedState, []);
useEffect(stopSketch, []); useEffect(() => {
stopSketch();
collapseConsole();
}, []);
// Load Project // Load Project
const [currentProjectID, setCurrentProjectID] = useState(null); const [currentProjectID, setCurrentProjectID] = useState(null);
@ -99,6 +205,19 @@ const MobileIDEView = (props) => {
}, [params, project, username]); }, [params, project, username]);
// TODO: This behavior could move to <Editor />
const [autosaveInterval, setAutosaveInterval] = useState(null);
useEffectWithComparison(autosave(autosaveInterval, setAutosaveInterval), {
autosaveProject, preferences, ide, selectedFile, project, user
});
useEventListener('keydown', handleGlobalKeydown(props, cmController), false, [props]);
const projectActions =
[{ icon: TerminalIcon, aria: 'Toggle console open/closed', action: consoleIsExpanded ? collapseConsole : expandConsole },
{ icon: SaveIcon, aria: 'Save project', action: () => saveProject(cmController.getContent(), false, true) }
];
return ( return (
<Screen fullscreen> <Screen fullscreen>
<Header <Header
@ -119,7 +238,7 @@ const MobileIDEView = (props) => {
</Header> </Header>
<IDEWrapper> <IDEWrapper>
<Editor provideController={setTmController} /> <Editor provideController={setCmController} />
</IDEWrapper> </IDEWrapper>
<Footer> <Footer>
@ -128,17 +247,36 @@ const MobileIDEView = (props) => {
<Console /> <Console />
</Expander> </Expander>
)} )}
<ActionStrip /> <ActionStrip actions={projectActions} />
</Footer> </Footer>
</Screen> </Screen>
); );
}; };
const handleGlobalKeydownProps = {
expandConsole: PropTypes.func.isRequired,
collapseConsole: PropTypes.func.isRequired,
expandSidebar: PropTypes.func.isRequired,
collapseSidebar: PropTypes.func.isRequired,
setAllAccessibleOutput: PropTypes.func.isRequired,
saveProject: PropTypes.func.isRequired,
cloneProject: PropTypes.func.isRequired,
showErrorModal: PropTypes.func.isRequired,
closeNewFolderModal: PropTypes.func.isRequired,
closeUploadFileModal: PropTypes.func.isRequired,
closeNewFileModal: PropTypes.func.isRequired,
};
MobileIDEView.propTypes = { MobileIDEView.propTypes = {
ide: PropTypes.shape({ ide: PropTypes.shape({
consoleIsExpanded: PropTypes.bool.isRequired, consoleIsExpanded: PropTypes.bool.isRequired,
}).isRequired, }).isRequired,
preferences: PropTypes.shape({
}).isRequired,
project: PropTypes.shape({ project: PropTypes.shape({
id: PropTypes.string, id: PropTypes.string,
name: PropTypes.string.isRequired, name: PropTypes.string.isRequired,
@ -172,6 +310,10 @@ MobileIDEView.propTypes = {
stopSketch: PropTypes.func.isRequired, stopSketch: PropTypes.func.isRequired,
getProject: PropTypes.func.isRequired, getProject: PropTypes.func.isRequired,
clearPersistedState: PropTypes.func.isRequired, clearPersistedState: PropTypes.func.isRequired,
autosaveProject: PropTypes.func.isRequired,
...handleGlobalKeydownProps
}; };
function mapStateToProps(state) { function mapStateToProps(state) {
@ -192,7 +334,9 @@ function mapStateToProps(state) {
const mapDispatchToProps = dispatch => bindActionCreators({ const mapDispatchToProps = dispatch => bindActionCreators({
...ProjectActions, ...ProjectActions,
...IDEActions ...IDEActions,
...ConsoleActions,
...PreferencesActions
}, dispatch); }, dispatch);
export default withRouter(connect(mapStateToProps, mapDispatchToProps)(MobileIDEView)); export default withRouter(connect(mapStateToProps, mapDispatchToProps)(MobileIDEView));

View file

@ -40,3 +40,20 @@ export const useModalBehavior = (hideOverlay) => {
return [visible, trigger, setRef]; return [visible, trigger, setRef];
}; };
// Usage: useEffectWithComparison((props, prevProps) => { ... }, { prop1, prop2 })
// This hook basically applies useEffect but keeping track of the last value of relevant props
// So you can passa a 2-param function to capture new and old values and do whatever with them.
export const useEffectWithComparison = (fn, props) => {
const [prevProps, update] = useState({});
return useEffect(() => {
fn(props, prevProps);
update(props);
}, Object.values(props));
};
export const useEventListener = (event, callback, useCapture = false, list = []) => useEffect(() => {
document.addEventListener(event, callback, useCapture);
return () => document.removeEventListener(event, callback, useCapture);
}, list);

1
client/utils/device.js Normal file
View file

@ -0,0 +1 @@
export const isMac = () => navigator.userAgent.toLowerCase().indexOf('mac') !== -1; // eslint-disable-line