Skip to content

Commit 2dc6529

Browse files
committed
Add function syncFileContent, load previewScripts in EmbedFrame
1 parent 4e76e57 commit 2dc6529

File tree

5 files changed

+40
-24
lines changed

5 files changed

+40
-24
lines changed

client/modules/IDE/actions/ide.js

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -263,21 +263,7 @@ export function showRuntimeErrorWarning() {
263263
export function startSketch() {
264264
return (dispatch, getState) => {
265265
dispatch(clearConsole());
266-
// instead, need to dispatch message event
267-
// dispatch(startSketchAndRefresh());
268266
dispatch(startVisualSketch());
269-
// need to update file to latest before playing? but when?
270-
// right before? there's no guarantee it'll be done before
271-
// making the srcdoc
272-
// maybe send it together? idk man
273-
// oh but it's reactive to file changes so it's okay?
274-
// maybe only send over files if autorefresh is off?
275-
// need to trigger like, a re-render event, you know?
276-
// like a refreshing state or something
277-
// whenever sketches get updated
278-
// cases:
279-
// hit play, refresh sketch
280-
// need to make sure the latest is in embed frame
281267
const state = getState();
282268
dispatchMessage({
283269
type: MessageTypes.FILES,

client/modules/IDE/components/Toolbar.jsx

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ class Toolbar extends React.Component {
8787
<button
8888
className="toolbar__play-sketch-button"
8989
onClick={() => {
90+
this.props.syncFileContent();
9091
this.props.startAccessibleSketch();
9192
this.props.setTextOutput(true);
9293
this.props.setGridOutput(true);
@@ -98,7 +99,10 @@ class Toolbar extends React.Component {
9899
</button>
99100
<button
100101
className={playButtonClass}
101-
onClick={this.props.startSketch}
102+
onClick={() => {
103+
this.props.syncFileContent();
104+
this.props.startSketch();
105+
}}
102106
aria-label={this.props.t('Toolbar.PlayOnlyVisualSketchARIA')}
103107
disabled={this.props.infiniteLoop}
104108
>
@@ -159,7 +163,7 @@ class Toolbar extends React.Component {
159163
onBlur={this.handleProjectNameSave}
160164
onKeyPress={this.handleKeyPress}
161165
/>
162-
{(() => { // eslint-disable-line
166+
{(() => {
163167
if (this.props.owner) {
164168
return (
165169
<p className="toolbar__project-owner">
@@ -170,6 +174,7 @@ class Toolbar extends React.Component {
170174
</p>
171175
);
172176
}
177+
return null;
173178
})()}
174179
</div>
175180
<button
@@ -209,7 +214,8 @@ Toolbar.propTypes = {
209214
startAccessibleSketch: PropTypes.func.isRequired,
210215
saveProject: PropTypes.func.isRequired,
211216
currentUser: PropTypes.string,
212-
t: PropTypes.func.isRequired
217+
t: PropTypes.func.isRequired,
218+
syncFileContent: PropTypes.func.isRequired
213219
};
214220

215221
Toolbar.defaultProps = {

client/modules/IDE/pages/IDEView.jsx

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,7 @@ class IDEView extends React.Component {
185185
) {
186186
e.preventDefault();
187187
e.stopPropagation();
188+
this.syncFileContent();
188189
this.props.startSketch();
189190
// 50 === 2
190191
} else if (
@@ -242,6 +243,11 @@ class IDEView extends React.Component {
242243
return null;
243244
};
244245

246+
syncFileContent = () => {
247+
const file = this.cmController.getContent();
248+
this.props.updateFileContent(file.id, file.content);
249+
};
250+
245251
render() {
246252
return (
247253
<div className="ide">
@@ -253,7 +259,10 @@ class IDEView extends React.Component {
253259
warnIfUnsavedChanges={this.handleUnsavedChanges}
254260
cmController={this.cmController}
255261
/>
256-
<Toolbar key={this.props.project.id} />
262+
<Toolbar
263+
syncFileContent={this.syncFileContent}
264+
key={this.props.project.id}
265+
/>
257266
{this.props.ide.preferencesIsVisible && (
258267
<Overlay
259268
title={this.props.t('Preferences.Settings')}
@@ -548,6 +557,7 @@ IDEView.propTypes = {
548557
collapseConsole: PropTypes.func.isRequired,
549558
deleteFile: PropTypes.func.isRequired,
550559
updateFileName: PropTypes.func.isRequired,
560+
updateFileContent: PropTypes.func.isRequired,
551561
openProjectOptions: PropTypes.func.isRequired,
552562
closeProjectOptions: PropTypes.func.isRequired,
553563
newFolder: PropTypes.func.isRequired,

client/modules/Preview/EmbedFrame.jsx

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ function resolvePathsForElementsWithAttribute(attr, sketchDoc, files) {
8686

8787
function resolveCSSLinksInString(content, files) {
8888
let newContent = content;
89+
console.log(content);
8990
let cssFileStrings = content.match(STRING_REGEX);
9091
cssFileStrings = cssFileStrings || [];
9192
cssFileStrings.forEach((cssFileString) => {
@@ -285,9 +286,9 @@ function injectLocalFiles(files, htmlFile) {
285286
// }
286287

287288
// TODO extra stuff to inject
288-
// const previewScripts = sketchDoc.createElement('script');
289-
// previewScripts.src = '/previewScripts.js';
290-
// sketchDoc.head.appendChild(previewScripts);
289+
const previewScripts = sketchDoc.createElement('script');
290+
previewScripts.src = getConfig('PREVIEW_SCRIPTS_URL');
291+
sketchDoc.head.appendChild(previewScripts);
291292

292293
const sketchDocString = `<!DOCTYPE HTML>\n${sketchDoc.documentElement.outerHTML}`;
293294
scriptOffs = getAllScriptOffsets(sketchDocString);
@@ -322,9 +323,7 @@ function EmbedFrame({ files, isPlaying }) {
322323
function renderSketch() {
323324
const doc = iframe.current;
324325
if (isPlaying) {
325-
console.log('calling inject local files');
326326
const htmlDoc = injectLocalFiles(files, htmlFile);
327-
console.log('setting srcdoc');
328327
srcDoc.set(doc, htmlDoc);
329328
} else {
330329
doc.srcdoc = '';

server/views/previewIndex.js

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,26 @@
11
function renderPreviewIndex() {
2+
const assetsManifest =
3+
process.env.webpackAssets && JSON.parse(process.env.webpackAssets);
24
return `
35
<!DOCTYPE html>
46
<html lang="en">
57
<head>
68
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
79
<meta name="viewport" content="width=device-width, initial-scale=1.0">
8-
</head>
10+
<script>
11+
if (!window.process) {
12+
window.process = {};
13+
}
14+
if (!window.process.env) {
15+
window.process.env = {};
16+
}
17+
window.process.env.PREVIEW_SCRIPTS_URL = '${
18+
process.env.NODE_ENV === 'production'
19+
? `${assetsManifest['/previewScripts.js']}`
20+
: '/previewScripts.js'
21+
}';
22+
</script>
23+
</head>
924
<body>
1025
<div id="root" class="root-app">
1126
<script src="/preview-app.js"></script>

0 commit comments

Comments
 (0)