Skip to content

Commit 776c3d2

Browse files
committed
♻️ refactor call to PreviewFrame#renderSketch
1 parent a11bc44 commit 776c3d2

File tree

2 files changed

+58
-81
lines changed

2 files changed

+58
-81
lines changed

client/modules/IDE/components/PreviewFrame.jsx

Lines changed: 25 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -22,61 +22,43 @@ import {
2222
import { hijackConsoleErrorsScript, startTag, getAllScriptOffsets }
2323
from '../../../utils/consoleUtils';
2424

25+
26+
// Kept inside class just for linter's
27+
const shouldRenderSketch = (props, prevProps = undefined) => {
28+
const { isPlaying, previewIsRefreshing, fullView } = props;
29+
30+
// if the user explicitly clicks on the play button
31+
if (isPlaying && previewIsRefreshing) return true;
32+
33+
if (!prevProps) return false;
34+
35+
return (props.isPlaying !== prevProps.isPlaying // if sketch starts or stops playing, want to rerender
36+
|| props.isAccessibleOutputPlaying !== prevProps.isAccessibleOutputPlaying // if user switches textoutput preferences
37+
|| props.textOutput !== prevProps.textOutput
38+
|| props.gridOutput !== prevProps.gridOutput
39+
|| props.soundOutput !== prevProps.soundOutput
40+
|| (fullView && props.files[0].id !== prevProps.files[0].id));
41+
};
42+
2543
class PreviewFrame extends React.Component {
2644
constructor(props) {
2745
super(props);
2846
this.handleConsoleEvent = this.handleConsoleEvent.bind(this);
2947
}
3048

3149
componentDidMount() {
32-
console.log(`componentDidMount: ${this.props.isPlaying}`);
3350
window.addEventListener('message', this.handleConsoleEvent);
3451

35-
// TODO: maybe encapsulate this into a function (together with code from componentDidUpdate)
36-
if (this.props.isPlaying && this.props.previewIsRefreshing) {
37-
this.renderSketch();
38-
}
52+
const props = {
53+
...this.props,
54+
previewIsRefreshing: this.props.previewIsRefreshing,
55+
isAccessibleOutputPlaying: this.props.isAccessibleOutputPlaying
56+
};
57+
if (shouldRenderSketch(props)) this.renderSketch();
3958
}
4059

4160
componentDidUpdate(prevProps) {
42-
console.log(`componentDidUpdate: ${this.props.isPlaying}`);
43-
// if sketch starts or stops playing, want to rerender
44-
if (this.props.isPlaying !== prevProps.isPlaying) {
45-
this.renderSketch();
46-
return;
47-
}
48-
49-
// if the user explicitly clicks on the play button
50-
if (this.props.isPlaying && this.props.previewIsRefreshing) {
51-
this.renderSketch();
52-
return;
53-
}
54-
55-
// if user switches textoutput preferences
56-
if (this.props.isAccessibleOutputPlaying !== prevProps.isAccessibleOutputPlaying) {
57-
this.renderSketch();
58-
return;
59-
}
60-
61-
if (this.props.textOutput !== prevProps.textOutput) {
62-
this.renderSketch();
63-
return;
64-
}
65-
66-
if (this.props.gridOutput !== prevProps.gridOutput) {
67-
this.renderSketch();
68-
return;
69-
}
70-
71-
if (this.props.soundOutput !== prevProps.soundOutput) {
72-
this.renderSketch();
73-
return;
74-
}
75-
76-
if (this.props.fullView && this.props.files[0].id !== prevProps.files[0].id) {
77-
this.renderSketch();
78-
}
79-
61+
if (shouldRenderSketch(this.props, prevProps)) this.renderSketch();
8062
// small bug - if autorefresh is on, and the usr changes files
8163
// in the sketch, preview will reload
8264
}

client/modules/Mobile/MobileSketchView.jsx

Lines changed: 33 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/* eslint-disable */
12
import React from 'react';
23
import PropTypes from 'prop-types';
34
import { Link } from 'react-router';
@@ -13,8 +14,6 @@ import * as PreferencesActions from '../IDE/actions/preferences';
1314
import * as ConsoleActions from '../IDE/actions/console';
1415
import * as FilesActions from '../IDE/actions/files';
1516

16-
import FullView from '../IDE/pages/FullView';
17-
1817
import { getHTMLFile } from '../IDE/reducers/files';
1918

2019

@@ -24,7 +23,7 @@ import { remSize } from '../../theme';
2423

2524
const Content = styled.div`
2625
z-index: 0;
27-
margin-top: ${remSize(48)};
26+
margin-top: ${remSize(68)};
2827
`;
2928

3029
const IconLinkWrapper = styled(Link)`
@@ -41,7 +40,7 @@ const MobileSketchView = (props) => {
4140
// const files = useSelector(state => state.files);
4241

4342
const {
44-
htmlFile, files, selectedFile
43+
htmlFile, files, selectedFile, projectName
4544
} = props;
4645

4746
// Actions
@@ -66,43 +65,37 @@ const MobileSketchView = (props) => {
6665
<ExitIcon viewBox="0 0 16 16" />
6766
</IconLinkWrapper>
6867
<div>
69-
<h2>Hello</h2>
68+
<h2>{projectName}</h2>
7069
<h3><br /></h3>
7170
</div>
7271
</Header>
7372
<Content>
74-
<h1>Hello</h1>
75-
<section className="preview-frame-holder">
76-
<PreviewFrame
77-
htmlFile={htmlFile}
78-
files={files}
79-
head={
80-
<link type="text/css" rel="stylesheet" href="/preview-styles.css" />
81-
}
82-
83-
content={selectedFile.content}
84-
85-
fullView
86-
isPlaying
87-
isAccessibleOutputPlaying={ide.isAccessibleOutputPlaying}
88-
previewIsRefreshing={ide.previewIsRefreshing}
89-
90-
textOutput={preferences.textOutput}
91-
gridOutput={preferences.gridOutput}
92-
soundOutput={preferences.soundOutput}
93-
autorefresh={preferences.autorefresh}
94-
95-
setTextOutput={setTextOutput}
96-
setGridOutput={setGridOutput}
97-
setSoundOutput={setSoundOutput}
98-
dispatchConsoleEvent={dispatchConsoleEvent}
99-
endSketchRefresh={endSketchRefresh}
100-
stopSketch={stopSketch}
101-
setBlobUrl={setBlobUrl}
102-
expandConsole={expandConsole}
103-
clearConsole={clearConsole}
104-
/>
105-
</section>
73+
<PreviewFrame
74+
htmlFile={htmlFile}
75+
files={files}
76+
head={<link type="text/css" rel="stylesheet" href="/preview-styles.css" />}
77+
78+
content={selectedFile.content}
79+
80+
isPlaying
81+
isAccessibleOutputPlaying={ide.isAccessibleOutputPlaying}
82+
previewIsRefreshing={ide.previewIsRefreshing}
83+
84+
textOutput={preferences.textOutput}
85+
gridOutput={preferences.gridOutput}
86+
soundOutput={preferences.soundOutput}
87+
autorefresh={preferences.autorefresh}
88+
89+
setTextOutput={setTextOutput}
90+
setGridOutput={setGridOutput}
91+
setSoundOutput={setSoundOutput}
92+
dispatchConsoleEvent={dispatchConsoleEvent}
93+
endSketchRefresh={endSketchRefresh}
94+
stopSketch={stopSketch}
95+
setBlobUrl={setBlobUrl}
96+
expandConsole={expandConsole}
97+
clearConsole={clearConsole}
98+
/>
10699
</Content>
107100
</Screen>);
108101
};
@@ -171,6 +164,8 @@ MobileSketchView.propTypes = {
171164
uploadFileModalVisible: PropTypes.bool.isRequired
172165
}).isRequired,
173166

167+
projectName: PropTypes.func.isRequired,
168+
174169
setTextOutput: PropTypes.func.isRequired,
175170
setGridOutput: PropTypes.func.isRequired,
176171
setSoundOutput: PropTypes.func.isRequired,
@@ -185,7 +180,7 @@ MobileSketchView.propTypes = {
185180
function mapStateToProps(state) {
186181
return {
187182
htmlFile: getHTMLFile(state.files),
188-
project: state.project,
183+
projectName: state.project.name,
189184
files: state.files,
190185
ide: state.ide,
191186
preferences: state.preferences,

0 commit comments

Comments
 (0)