Skip to content

Commit 4f95d82

Browse files
author
Ives van Hoorne
committed
Add ?forcerefresh=1 option for embeds
1 parent 39f8fc5 commit 4f95d82

File tree

5 files changed

+41
-21
lines changed

5 files changed

+41
-21
lines changed

src/app/components/sandbox/Preview/index.js

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ export default class Preview extends React.PureComponent<Props, State> {
183183
this.setState({
184184
frameInitialized: true,
185185
});
186-
this.executeCodeImmediately();
186+
this.executeCodeImmediately(true);
187187
} else {
188188
const { type } = e.data;
189189

@@ -229,7 +229,7 @@ export default class Preview extends React.PureComponent<Props, State> {
229229
return isInProjectView ? findMainModule(modules, template) : module;
230230
};
231231

232-
executeCodeImmediately = () => {
232+
executeCodeImmediately = (initialRender: boolean = false) => {
233233
const {
234234
modules,
235235
directories,
@@ -248,22 +248,26 @@ export default class Preview extends React.PureComponent<Props, State> {
248248

249249
// Do it here so we can see the dependency fetching screen if needed
250250
this.clearErrors();
251-
const renderedModule = this.getRenderedModule();
252-
this.sendMessage({
253-
type: 'compile',
254-
module: renderedModule,
255-
changedModule: module,
256-
dependencies,
257-
modules,
258-
directories,
259-
sandboxId,
260-
externalResources,
261-
template,
262-
hasActions: !!runActionFromPreview,
263-
isModuleView: !isInProjectView,
264-
// TODO remove this in 2 weeks
265-
experimentalPackager: preferences.newPackagerExperiment,
266-
});
251+
if (preferences.forceRefresh && !initialRender) {
252+
this.handleRefresh();
253+
} else {
254+
const renderedModule = this.getRenderedModule();
255+
this.sendMessage({
256+
type: 'compile',
257+
module: renderedModule,
258+
changedModule: module,
259+
dependencies,
260+
modules,
261+
directories,
262+
sandboxId,
263+
externalResources,
264+
template,
265+
hasActions: !!runActionFromPreview,
266+
isModuleView: !isInProjectView,
267+
// TODO remove this in 2 weeks
268+
experimentalPackager: preferences.newPackagerExperiment,
269+
});
270+
}
267271
};
268272

269273
clearErrors = () => {

src/app/components/sandbox/Preview/utils/should-update.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@ const MODULE_FIELDS = [
1212

1313
const DIRECTORY_FIELDS = ['title', 'directoryShortid', 'shortid'];
1414

15-
function checkFields(m1, m2, fields: Array<String>) {
15+
function checkFields(m1, m2, fields: Array<string>) {
1616
return fields.some(field => m1[field] !== m2[field]);
1717
}
1818

19-
function compareArrays(m1, m2, fields: Array<String>) {
19+
function compareArrays(m1, m2, fields: Array<string>) {
2020
return m1.some(first => {
2121
const second = m2.find(m => m.id === first.id);
2222

src/common/url.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ export const getSandboxOptions = (url: string) => {
5050
result.autoResize = url.includes('autoresize=1');
5151
result.useCodeMirror = url.includes('codemirror=1');
5252
result.enableEslint = url.includes('eslint=1');
53+
result.forceRefresh = url.includes('forcerefresh=1');
5354

5455
return result;
5556
};

src/embed/App.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ type State = {
6060
enableEslint: boolean,
6161
useCodeMirror: boolean,
6262
editorSize: number,
63+
forceRefresh: boolean,
6364
highlightedLines: Array<string>,
6465
};
6566

@@ -80,6 +81,7 @@ export default class App extends React.PureComponent<{}, State> {
8081
useCodeMirror,
8182
editorSize,
8283
highlightedLines,
84+
forceRefresh,
8385
} = getSandboxOptions(document.location.href);
8486

8587
this.state = {
@@ -97,6 +99,7 @@ export default class App extends React.PureComponent<{}, State> {
9799
enableEslint,
98100
useCodeMirror,
99101
editorSize,
102+
forceRefresh,
100103
highlightedLines: highlightedLines || [],
101104
};
102105
}
@@ -219,6 +222,7 @@ export default class App extends React.PureComponent<{}, State> {
219222
enableEslint={this.state.enableEslint}
220223
editorSize={this.state.editorSize}
221224
highlightedLines={this.state.highlightedLines}
225+
forceRefresh={this.state.forceRefresh}
222226
/>
223227
</Container>
224228
</ThemeProvider>

src/embed/components/Content.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ type Props = {
4343
enableEslint: boolean,
4444
isInProjectView: boolean,
4545
editorSize: number,
46+
forceRefresh: boolean,
4647
highlightedLines: Array<string>,
4748
};
4849

@@ -59,6 +60,14 @@ export default class Content extends React.PureComponent<Props, State> {
5960
errors: [],
6061
};
6162

63+
// constructor(props) {
64+
// super(props);
65+
66+
// if (props.forceRefresh) {
67+
// this.setCode = debounce(this.set)
68+
// }
69+
// }
70+
6271
componentDidMount() {
6372
setTimeout(() => this.handleResize());
6473
}
@@ -162,6 +171,8 @@ export default class Content extends React.PureComponent<Props, State> {
162171

163172
getPreferences = () => ({
164173
...this.preferences,
174+
forceRefresh: this.props.forceRefresh,
175+
instantPreviewEnabled: !this.props.forceRefresh,
165176
fontSize: this.props.fontSize,
166177
autoDownloadTypes: true,
167178
lintEnabled: this.props.enableEslint,
@@ -251,7 +262,7 @@ export default class Content extends React.PureComponent<Props, State> {
251262
modules={alteredModules}
252263
directories={sandbox.directories}
253264
externalResources={sandbox.externalResources}
254-
module={mainModule}
265+
module={alteredMainModule}
255266
addError={this.addError}
256267
clearErrors={this.clearErrors}
257268
preferences={this.getPreferences()}

0 commit comments

Comments
 (0)