Skip to content

Commit 02507ae

Browse files
author
Ives van Hoorne
committed
Progress
1 parent 47a90f8 commit 02507ae

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+2066
-928
lines changed

.eslintrc

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,5 @@
2626
"no-underscore-dangle": "off",
2727
"no-nested-ternary": "warn",
2828
"react/require-default-props": "off"
29-
},
30-
"settings": {
31-
"import/resolver": "webpack"
3229
}
3330
}

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@
3939
"packages/app",
4040
"packages/common",
4141
"packages/codesandbox-api",
42-
"packages/codesandbox-playground"
42+
"packages/sandpack",
43+
"packages/react-sandpack"
4344
],
4445
"devDependencies": {
4546
"all-contributors-cli": "^4.3.0",

packages/app/src/app/components/Preview/DevTools/Problems/index.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import React from 'react';
2-
import { listen, actions } from 'codesandbox-api';
3-
import { dispatch } from 'app/components/Preview';
2+
import { listen, dispatch, actions } from 'codesandbox-api';
43
import Tooltip from 'common/components/Tooltip';
54
import FileIcon from 'react-icons/lib/md/insert-drive-file';
65

@@ -66,7 +65,7 @@ class Problems extends React.PureComponent<*, State> {
6665
};
6766

6867
openFile = (path: string) => {
69-
dispatch(this.props.sandboxId, actions.editor.openModule(path));
68+
dispatch(actions.editor.openModule(path));
7069
};
7170

7271
render() {

packages/app/src/app/components/Preview/DevTools/Tests/index.js

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
// @flow
22
/* eslint-disable no-param-reassign */
33
import React from 'react';
4-
import { actions, listen } from 'codesandbox-api';
5-
import { dispatch } from 'app/components/Preview';
4+
import { actions, dispatch, listen } from 'codesandbox-api';
65
import SplitPane from 'react-split-pane';
76

87
import immer from 'immer';
@@ -325,7 +324,7 @@ class Tests extends React.Component<Props, State> {
325324
};
326325

327326
toggleWatching = () => {
328-
dispatch(this.props.sandboxId, {
327+
dispatch({
329328
type: 'set-test-watching',
330329
watching: !this.state.watching,
331330
});
@@ -334,7 +333,7 @@ class Tests extends React.Component<Props, State> {
334333

335334
runAllTests = () => {
336335
this.setState({ files: {} }, () => {
337-
dispatch(this.props.sandboxId, {
336+
dispatch({
338337
type: 'run-all-tests',
339338
});
340339
});
@@ -346,7 +345,7 @@ class Tests extends React.Component<Props, State> {
346345
state.files[file.fileName].tests = {};
347346
}),
348347
() => {
349-
dispatch(this.props.sandboxId, {
348+
dispatch({
350349
type: 'run-tests',
351350
path: file.fileName,
352351
});
@@ -355,7 +354,7 @@ class Tests extends React.Component<Props, State> {
355354
};
356355

357356
openFile = (path: string) => {
358-
dispatch(this.props.sandboxId, actions.editor.openModule(path));
357+
dispatch(actions.editor.openModule(path));
359358
};
360359

361360
render() {

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

Lines changed: 11 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// @flow
22
import * as React from 'react';
33
import type { Sandbox, Module, Preferences } from 'common/types';
4-
import { listen, notifyListeners } from 'codesandbox-api';
4+
import { listen, dispatch, registerFrame } from 'codesandbox-api';
55
import { debounce } from 'lodash';
66

77
import { frameUrl } from 'common/utils/url-generator';
@@ -12,29 +12,8 @@ import { generateFileFromSandbox } from 'common/templates/configuration/package-
1212
import Navigator from './Navigator';
1313
import { Container, StyledFrame } from './elements';
1414

15-
let frames = [];
16-
17-
function sendMessage(sandboxId: string, message: Object) {
18-
const rawMessage = JSON.parse(JSON.stringify(message));
19-
frames.forEach(frame => {
20-
frame.postMessage(
21-
{ ...rawMessage, codesandbox: true },
22-
frameUrl(sandboxId)
23-
);
24-
});
25-
}
26-
27-
export function dispatch(sandboxId: string, message: Object = {}) {
28-
const finalMessage = {
29-
...message,
30-
codesandbox: true,
31-
};
32-
sendMessage(sandboxId, finalMessage);
33-
notifyListeners(finalMessage, window);
34-
}
35-
36-
export function evaluateInSandbox(sandboxId: string, command: string) {
37-
dispatch(sandboxId, { type: 'evaluate', command });
15+
export function evaluateInSandbox(command: string) {
16+
dispatch({ type: 'evaluate', command });
3817
}
3918

4019
type Props = {
@@ -137,11 +116,9 @@ class BasePreview extends React.Component<Props, State> {
137116
};
138117

139118
handleMessage = (data: Object, source: any) => {
140-
if (source) {
141-
if (data.type === 'initialized') {
142-
if (frames.indexOf(source) === -1) {
143-
frames.push(source);
144-
}
119+
if (data && data.codesandbox) {
120+
if (data.type === 'initialized' && source) {
121+
registerFrame(source);
145122

146123
if (!this.state.frameInitialized && this.props.onInitialized) {
147124
this.disposeInitializer = this.props.onInitialized(this);
@@ -206,7 +183,7 @@ class BasePreview extends React.Component<Props, State> {
206183

207184
if (settings.clearConsoleEnabled) {
208185
console.clear(); // eslint-disable-line no-console
209-
dispatch(sandbox.id, { type: 'clear-console' });
186+
dispatch({ type: 'clear-console' });
210187
}
211188

212189
// Do it here so we can see the dependency fetching screen if needed
@@ -215,10 +192,7 @@ class BasePreview extends React.Component<Props, State> {
215192
this.handleRefresh();
216193
} else {
217194
if (!this.props.isInProjectView) {
218-
evaluateInSandbox(
219-
this.props.sandbox.id,
220-
`history.pushState({}, null, '/')`
221-
);
195+
evaluateInSandbox(`history.pushState({}, null, '/')`);
222196
}
223197

224198
const modulesObject = {};
@@ -241,7 +215,7 @@ class BasePreview extends React.Component<Props, State> {
241215
};
242216
}
243217

244-
sendMessage(sandbox.id, {
218+
dispatch({
245219
type: 'compile',
246220
version: 3,
247221
entry: this.getRenderedModule(),
@@ -293,7 +267,7 @@ class BasePreview extends React.Component<Props, State> {
293267
};
294268

295269
handleBack = () => {
296-
sendMessage(this.props.sandbox.id, {
270+
dispatch({
297271
type: 'urlback',
298272
});
299273

@@ -305,7 +279,7 @@ class BasePreview extends React.Component<Props, State> {
305279
};
306280

307281
handleForward = () => {
308-
sendMessage(this.props.sandbox.id, {
282+
dispatch({
309283
type: 'urlforward',
310284
});
311285

packages/codesandbox-api/src/dispatcher/index.ts

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
// import * as debug from 'debug';
22
import host from './host';
33

4+
const bundlers: Window[] = [];
5+
46
// Whether the tab has a connection with the editor
57
export const isStandalone =
68
typeof window === 'undefined' || (!window.opener && window.parent === window);
@@ -17,6 +19,7 @@ export function dispatch(message: Object) {
1719

1820
const newMessage = { ...message, codesandbox: true };
1921
notifyListeners(newMessage);
22+
notifyFrames(newMessage);
2023

2124
if (isStandalone) return;
2225

@@ -53,10 +56,36 @@ export function notifyListeners(data: Object, source?: MessageEvent['source']) {
5356
});
5457
}
5558

59+
function notifyFrames(message: Object) {
60+
const rawMessage = JSON.parse(JSON.stringify(message));
61+
bundlers.forEach(frame => {
62+
if (frame) {
63+
frame.postMessage({ ...rawMessage, codesandbox: true }, '*');
64+
}
65+
});
66+
}
67+
5668
// We now start listening so we can let our listeners know
5769
window.addEventListener('message', (e: MessageEvent) => {
5870
const { data } = e;
59-
if (data.codesandbox) {
71+
72+
if (
73+
data.codesandbox &&
74+
(e.origin.endsWith('.codesandbox.io') ||
75+
e.origin.endsWith('/codesandbox.io') ||
76+
process.env.NODE_ENV !== 'production')
77+
) {
6078
notifyListeners(data, e.source);
6179
}
6280
});
81+
82+
/**
83+
* Register an window as a output the `dispatch` function can send messages to.
84+
*
85+
* @param frame
86+
*/
87+
export function registerFrame(frame: Window) {
88+
if (bundlers.indexOf(frame) === -1) {
89+
bundlers.push(frame);
90+
}
91+
}

packages/codesandbox-api/src/typings/global.d.ts

Lines changed: 0 additions & 5 deletions
This file was deleted.

packages/codesandbox-playground/package.json

Lines changed: 0 additions & 39 deletions
This file was deleted.

packages/codesandbox-playground/src/index.ts

Lines changed: 0 additions & 8 deletions
This file was deleted.

packages/codesandbox-playground/src/utils/frame.ts

Lines changed: 0 additions & 68 deletions
This file was deleted.

0 commit comments

Comments
 (0)