Skip to content

Commit c0b9ae4

Browse files
committed
Merge branch 'feature/mobile-save-sketch' of https://github.com/ghalestrilo/p5.js-web-editor into feature/mobile-save-sketch
2 parents 1503b1b + 100a6a0 commit c0b9ae4

File tree

8 files changed

+208
-42
lines changed

8 files changed

+208
-42
lines changed

client/common/icons.jsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import Preferences from '../images/preferences.svg';
1414
import Play from '../images/triangle-arrow-right.svg';
1515
import More from '../images/more.svg';
1616
import Code from '../images/code.svg';
17+
import Save from '../images/save.svg';
1718
import Terminal from '../images/terminal.svg';
1819

1920

@@ -81,3 +82,4 @@ export const PlayIcon = withLabel(Play);
8182
export const MoreIcon = withLabel(More);
8283
export const TerminalIcon = withLabel(Terminal);
8384
export const CodeIcon = withLabel(Code);
85+
export const SaveIcon = withLabel(Save);
Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,36 @@
11
import React from 'react';
2+
import PropTypes from 'prop-types';
23
import styled from 'styled-components';
3-
import { bindActionCreators } from 'redux';
4-
import { useDispatch, useSelector } from 'react-redux';
54
import { remSize } from '../../theme';
65
import IconButton from './IconButton';
7-
import { TerminalIcon } from '../../common/icons';
8-
import * as IDEActions from '../../modules/IDE/actions/ide';
96

10-
const BottomBarContent = styled.h2`
7+
const BottomBarContent = styled.div`
8+
display: grid;
9+
grid-template-columns: repeat(8,1fr);
1110
padding: ${remSize(8)};
1211
1312
svg {
1413
max-height: ${remSize(32)};
1514
}
1615
`;
1716

18-
export default () => {
19-
const { expandConsole, collapseConsole } = bindActionCreators(IDEActions, useDispatch());
20-
const { consoleIsExpanded } = useSelector(state => state.ide);
17+
const ActionStrip = ({ actions }) => (
18+
<BottomBarContent>
19+
{actions.map(({ icon, aria, action }) =>
20+
(<IconButton
21+
icon={icon}
22+
aria-label={aria}
23+
key={`bottom-bar-${aria}`}
24+
onClick={() => action()}
25+
/>))}
26+
</BottomBarContent>);
2127

22-
const actions = [{ icon: TerminalIcon, aria: 'Say Something', action: consoleIsExpanded ? collapseConsole : expandConsole }];
23-
24-
return (
25-
<BottomBarContent>
26-
{actions.map(({ icon, aria, action }) =>
27-
(<IconButton
28-
icon={icon}
29-
aria-label={aria}
30-
key={`bottom-bar-${aria}`}
31-
onClick={() => action()}
32-
/>))}
33-
</BottomBarContent>
34-
);
28+
ActionStrip.propTypes = {
29+
actions: PropTypes.arrayOf(PropTypes.shape({
30+
icon: PropTypes.element.isRequired,
31+
aria: PropTypes.string.isRequired,
32+
action: PropTypes.func.isRequired
33+
})).isRequired
3534
};
35+
36+
export default ActionStrip;

client/components/mobile/Header.jsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,9 @@ const Header = ({
7777
</HeaderDiv>
7878
);
7979

80+
8081
Header.propTypes = {
81-
title: PropTypes.string,
82+
title: PropTypes.oneOfType([PropTypes.string, PropTypes.element]),
8283
subtitle: PropTypes.string,
8384
leftButton: PropTypes.element,
8485
children: PropTypes.oneOfType([PropTypes.element, PropTypes.arrayOf(PropTypes.element)]),

client/images/save.svg

Lines changed: 1 addition & 0 deletions
Loading

client/modules/IDE/actions/project.js

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ function getSynchedProject(currentState, responseProject) {
126126
};
127127
}
128128

129-
export function saveProject(selectedFile = null, autosave = false) {
129+
export function saveProject(selectedFile = null, autosave = false, mobile = false) {
130130
return (dispatch, getState) => {
131131
const state = getState();
132132
if (state.project.isSaving) {
@@ -185,16 +185,15 @@ export function saveProject(selectedFile = null, autosave = false) {
185185
.then((response) => {
186186
dispatch(endSavingProject());
187187
const { hasChanges, synchedProject } = getSynchedProject(getState(), response.data);
188+
189+
dispatch(setNewProject(synchedProject));
190+
dispatch(setUnsavedChanges(false));
191+
browserHistory.push(`${mobile ? '/mobile' : ''}/${response.data.user.username}/sketches/${response.data.id}`);
192+
188193
if (hasChanges) {
189-
dispatch(setNewProject(synchedProject));
190-
dispatch(setUnsavedChanges(false));
191-
browserHistory.push(`/${response.data.user.username}/sketches/${response.data.id}`);
192194
dispatch(setUnsavedChanges(true));
193-
} else {
194-
dispatch(setNewProject(synchedProject));
195-
dispatch(setUnsavedChanges(false));
196-
browserHistory.push(`/${response.data.user.username}/sketches/${response.data.id}`);
197195
}
196+
198197
dispatch(projectSaveSuccess());
199198
if (!autosave) {
200199
if (state.preferences.autosave) {
@@ -222,9 +221,9 @@ export function saveProject(selectedFile = null, autosave = false) {
222221
};
223222
}
224223

225-
export function autosaveProject() {
224+
export function autosaveProject(mobile = false) {
226225
return (dispatch, getState) => {
227-
saveProject(null, true)(dispatch, getState);
226+
saveProject(null, true, mobile)(dispatch, getState);
228227
};
229228
}
230229

client/modules/IDE/pages/MobileIDEView.jsx

Lines changed: 154 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ import { bindActionCreators } from 'redux';
1010

1111
import * as IDEActions from '../actions/ide';
1212
import * as ProjectActions from '../actions/project';
13+
import * as ConsoleActions from '../actions/console';
14+
import * as PreferencesActions from '../actions/preferences';
15+
1316

1417
// Local Imports
1518
import Editor from '../components/Editor';
@@ -26,11 +29,13 @@ import { remSize } from '../../../theme';
2629
// import OverlayManager from '../../../components/OverlayManager';
2730
import ActionStrip from '../../../components/mobile/ActionStrip';
2831
import useAsModal from '../../../components/useAsModal';
29-
import { PreferencesIcon } from '../../../common/icons';
32+
import { PreferencesIcon, TerminalIcon, SaveIcon } from '../../../common/icons';
3033
import Dropdown from '../../../components/Dropdown';
3134

32-
const isUserOwner = ({ project, user }) =>
33-
project.owner && project.owner.id === user.id;
35+
36+
import { useEffectWithComparison, useEventListener } from '../../../utils/custom-hooks';
37+
38+
import * as device from '../../../utils/device';
3439

3540
const withChangeDot = (title, unsavedChanges = false) => (
3641
<span>
@@ -65,13 +70,111 @@ const getNatOptions = (username = undefined) =>
6570
]
6671
);
6772

73+
74+
const isUserOwner = ({ project, user }) =>
75+
project && project.owner && project.owner.id === user.id;
76+
77+
const canSaveProject = (project, user) =>
78+
isUserOwner({ project, user }) || (user.authenticated && !project.owner);
79+
80+
// TODO: This could go into <Editor />
81+
const handleGlobalKeydown = (props, cmController) => (e) => {
82+
const {
83+
user, project, ide,
84+
setAllAccessibleOutput,
85+
saveProject, cloneProject, showErrorModal, startSketch, stopSketch,
86+
expandSidebar, collapseSidebar, expandConsole, collapseConsole,
87+
closeNewFolderModal, closeUploadFileModal, closeNewFileModal
88+
} = props;
89+
90+
91+
const isMac = device.isMac();
92+
93+
// const ctrlDown = (e.metaKey && this.isMac) || (e.ctrlKey && !this.isMac);
94+
const ctrlDown = (isMac ? e.metaKey : e.ctrlKey);
95+
96+
if (ctrlDown) {
97+
if (e.shiftKey) {
98+
if (e.keyCode === 13) {
99+
e.preventDefault();
100+
e.stopPropagation();
101+
stopSketch();
102+
} else if (e.keyCode === 13) {
103+
e.preventDefault();
104+
e.stopPropagation();
105+
startSketch();
106+
// 50 === 2
107+
} else if (e.keyCode === 50
108+
) {
109+
e.preventDefault();
110+
setAllAccessibleOutput(false);
111+
// 49 === 1
112+
} else if (e.keyCode === 49) {
113+
e.preventDefault();
114+
setAllAccessibleOutput(true);
115+
}
116+
} else if (e.keyCode === 83) {
117+
// 83 === s
118+
e.preventDefault();
119+
e.stopPropagation();
120+
if (canSaveProject(project, user)) saveProject(cmController.getContent(), false, true);
121+
else if (user.authenticated) cloneProject();
122+
else showErrorModal('forceAuthentication');
123+
124+
// 13 === enter
125+
} else if (e.keyCode === 66) {
126+
e.preventDefault();
127+
if (!ide.sidebarIsExpanded) expandSidebar();
128+
else collapseSidebar();
129+
}
130+
} else if (e.keyCode === 192 && e.ctrlKey) {
131+
e.preventDefault();
132+
if (ide.consoleIsExpanded) collapseConsole();
133+
else expandConsole();
134+
} else if (e.keyCode === 27) {
135+
if (ide.newFolderModalVisible) closeNewFolderModal();
136+
else if (ide.uploadFileModalVisible) closeUploadFileModal();
137+
else if (ide.modalIsVisible) closeNewFileModal();
138+
}
139+
};
140+
141+
142+
const autosave = (autosaveInterval, setAutosaveInterval) => (props, prevProps) => {
143+
const {
144+
autosaveProject, preferences, ide, selectedFile: file, project, user
145+
} = props;
146+
147+
const { selectedFile: oldFile } = prevProps;
148+
149+
const doAutosave = () => autosaveProject(true);
150+
151+
if (isUserOwner(props) && project.id) {
152+
if (preferences.autosave && ide.unsavedChanges && !ide.justOpenedProject) {
153+
if (file.name === oldFile.name && file.content !== oldFile.content) {
154+
if (autosaveInterval) {
155+
clearTimeout(autosaveInterval);
156+
}
157+
console.log('will save project in 20 seconds');
158+
setAutosaveInterval(setTimeout(doAutosave, 20000));
159+
}
160+
} else if (autosaveInterval && !preferences.autosave) {
161+
clearTimeout(autosaveInterval);
162+
setAutosaveInterval(null);
163+
}
164+
} else if (autosaveInterval) {
165+
clearTimeout(autosaveInterval);
166+
setAutosaveInterval(null);
167+
}
168+
};
169+
68170
const MobileIDEView = (props) => {
69171
const {
70-
ide, project, selectedFile, user, params, unsavedChanges,
71-
stopSketch, startSketch, getProject, clearPersistedState
172+
ide, preferences, project, selectedFile, user, params, unsavedChanges, expandConsole, collapseConsole,
173+
stopSketch, startSketch, getProject, clearPersistedState, autosaveProject, saveProject
72174
} = props;
73175

74-
const [tmController, setTmController] = useState(null); // eslint-disable-line
176+
177+
const [cmController, setCmController] = useState(null); // eslint-disable-line
75178

76179
const { username } = user;
77180
const { consoleIsExpanded } = ide;
@@ -84,7 +187,10 @@ const MobileIDEView = (props) => {
84187

85188
// Force state reset
86189
useEffect(clearPersistedState, []);
87-
useEffect(stopSketch, []);
190+
useEffect(() => {
191+
stopSketch();
192+
collapseConsole();
193+
}, []);
88194

89195
// Load Project
90196
const [currentProjectID, setCurrentProjectID] = useState(null);
@@ -99,6 +205,19 @@ const MobileIDEView = (props) => {
99205
}, [params, project, username]);
100206

101207

208+
// TODO: This behavior could move to <Editor />
209+
const [autosaveInterval, setAutosaveInterval] = useState(null);
210+
useEffectWithComparison(autosave(autosaveInterval, setAutosaveInterval), {
211+
autosaveProject, preferences, ide, selectedFile, project, user
212+
});
213+
214+
useEventListener('keydown', handleGlobalKeydown(props, cmController), false, [props]);
215+
216+
const projectActions =
217+
[{ icon: TerminalIcon, aria: 'Toggle console open/closed', action: consoleIsExpanded ? collapseConsole : expandConsole },
218+
{ icon: SaveIcon, aria: 'Save project', action: () => saveProject(cmController.getContent(), false, true) }
219+
];
220+
102221
return (
103222
<Screen fullscreen>
104223
<Header
@@ -119,7 +238,7 @@ const MobileIDEView = (props) => {
119238
</Header>
120239

121240
<IDEWrapper>
122-
<Editor provideController={setTmController} />
241+
<Editor provideController={setCmController} />
123242
</IDEWrapper>
124243

125244
<Footer>
@@ -128,17 +247,36 @@ const MobileIDEView = (props) => {
128247
<Console />
129248
</Expander>
130249
)}
131-
<ActionStrip />
250+
<ActionStrip actions={projectActions} />
132251
</Footer>
133252
</Screen>
134253
);
135254
};
136255

256+
const handleGlobalKeydownProps = {
257+
expandConsole: PropTypes.func.isRequired,
258+
collapseConsole: PropTypes.func.isRequired,
259+
expandSidebar: PropTypes.func.isRequired,
260+
collapseSidebar: PropTypes.func.isRequired,
261+
262+
setAllAccessibleOutput: PropTypes.func.isRequired,
263+
saveProject: PropTypes.func.isRequired,
264+
cloneProject: PropTypes.func.isRequired,
265+
showErrorModal: PropTypes.func.isRequired,
266+
267+
closeNewFolderModal: PropTypes.func.isRequired,
268+
closeUploadFileModal: PropTypes.func.isRequired,
269+
closeNewFileModal: PropTypes.func.isRequired,
270+
};
271+
137272
MobileIDEView.propTypes = {
138273
ide: PropTypes.shape({
139274
consoleIsExpanded: PropTypes.bool.isRequired,
140275
}).isRequired,
141276

277+
preferences: PropTypes.shape({
278+
}).isRequired,
279+
142280
project: PropTypes.shape({
143281
id: PropTypes.string,
144282
name: PropTypes.string.isRequired,
@@ -172,6 +310,10 @@ MobileIDEView.propTypes = {
172310
stopSketch: PropTypes.func.isRequired,
173311
getProject: PropTypes.func.isRequired,
174312
clearPersistedState: PropTypes.func.isRequired,
313+
autosaveProject: PropTypes.func.isRequired,
314+
315+
316+
...handleGlobalKeydownProps
175317
};
176318

177319
function mapStateToProps(state) {
@@ -192,7 +334,9 @@ function mapStateToProps(state) {
192334

193335
const mapDispatchToProps = dispatch => bindActionCreators({
194336
...ProjectActions,
195-
...IDEActions
337+
...IDEActions,
338+
...ConsoleActions,
339+
...PreferencesActions
196340
}, dispatch);
197341

198342
export default withRouter(connect(mapStateToProps, mapDispatchToProps)(MobileIDEView));

client/utils/custom-hooks.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,3 +40,20 @@ export const useModalBehavior = (hideOverlay) => {
4040

4141
return [visible, trigger, setRef];
4242
};
43+
44+
// Usage: useEffectWithComparison((props, prevProps) => { ... }, { prop1, prop2 })
45+
// This hook basically applies useEffect but keeping track of the last value of relevant props
46+
// So you can passa a 2-param function to capture new and old values and do whatever with them.
47+
export const useEffectWithComparison = (fn, props) => {
48+
const [prevProps, update] = useState({});
49+
50+
return useEffect(() => {
51+
fn(props, prevProps);
52+
update(props);
53+
}, Object.values(props));
54+
};
55+
56+
export const useEventListener = (event, callback, useCapture = false, list = []) => useEffect(() => {
57+
document.addEventListener(event, callback, useCapture);
58+
return () => document.removeEventListener(event, callback, useCapture);
59+
}, list);

client/utils/device.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const isMac = () => navigator.userAgent.toLowerCase().indexOf('mac') !== -1; // eslint-disable-line

0 commit comments

Comments
 (0)