Skip to content

Commit 5141186

Browse files
committed
Fix ConsoleInput to work with EmbedFrame
1 parent 2252989 commit 5141186

File tree

4 files changed

+38
-17
lines changed

4 files changed

+38
-17
lines changed

client/modules/IDE/components/ConsoleInput.jsx

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import CodeMirror from 'codemirror';
44
import { Encode } from 'console-feed';
55

66
import RightArrowIcon from '../../../images/right-arrow.svg';
7-
import { dispatchMessage } from '../../../utils/dispatcher';
7+
import { dispatchMessage, MessageTypes } from '../../../utils/dispatcher';
88

99
// heavily inspired by
1010
// https://github.com/codesandbox/codesandbox-client/blob/92a1131f4ded6f7d9c16945dc7c18aa97c8ada27/packages/app/src/app/components/Preview/DevTools/Console/Input/index.tsx
@@ -41,8 +41,11 @@ class ConsoleInput extends React.Component {
4141
];
4242
const consoleEvent = [{ method: 'command', data: [value] }];
4343
dispatchMessage({
44-
source: 'console',
45-
messages
44+
type: MessageTypes.EXECUTE,
45+
payload: {
46+
source: 'console',
47+
messages
48+
}
4649
});
4750
this.props.dispatchConsoleEvent(consoleEvent);
4851
cm.setValue('');

client/modules/Preview/EmbedFrame.jsx

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import {
1717
NOT_EXTERNAL_LINK_REGEX
1818
} from '../../../server/utils/fileUtils';
1919
import { startTag, getAllScriptOffsets } from '../../utils/consoleUtils';
20+
import { registerFrame } from '../../utils/dispatcher';
2021

2122
const Frame = styled.iframe`
2223
min-height: 100%;
@@ -233,6 +234,16 @@ function EmbedFrame({ files, isPlaying }) {
233234
const iframe = useRef();
234235
const htmlFile = useMemo(() => getHtmlFile(files), [files]);
235236

237+
useEffect(() => {
238+
const unsubscribe = registerFrame(
239+
iframe.current.contentWindow,
240+
window.origin
241+
);
242+
return () => {
243+
unsubscribe();
244+
};
245+
});
246+
236247
function renderSketch() {
237248
const doc = iframe.current;
238249
if (isPlaying) {

client/modules/Preview/previewIndex.jsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@ const App = () => {
3838
case MessageTypes.REGISTER:
3939
dispatchMessage({ type: MessageTypes.REGISTER });
4040
break;
41+
case MessageTypes.EXECUTE:
42+
dispatchMessage(payload);
43+
break;
4144
default:
4245
break;
4346
}

client/utils/dispatcher.js

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,48 @@
11
// Inspired by
22
// https://github.com/codesandbox/codesandbox-client/blob/master/packages/codesandbox-api/src/dispatcher/index.ts
33

4-
let frame = null;
4+
const frames = {};
5+
let frameIndex = 1;
56
let listener = null;
6-
// const { origin } = window;
7-
let origin = null;
87

98
export const MessageTypes = {
109
START: 'START',
1110
STOP: 'STOP',
1211
FILES: 'FILES',
13-
REGISTER: 'REGISTER'
12+
REGISTER: 'REGISTER',
13+
EXECUTE: 'EXECUTE'
1414
};
1515

16-
// could instead register multiple frames here
1716
export function registerFrame(newFrame, newOrigin) {
18-
frame = newFrame;
19-
origin = newOrigin;
17+
const frameId = frameIndex;
18+
frameIndex += 1;
19+
frames[frameId] = { frame: newFrame, origin: newOrigin };
2020
return () => {
21-
frame = null;
22-
origin = null;
21+
delete frames[frameId];
2322
};
2423
}
2524

2625
function notifyListener(message) {
2726
if (listener) listener(message);
2827
}
2928

30-
function notifyFrame(message) {
29+
function notifyFrames(message) {
3130
const rawMessage = JSON.parse(JSON.stringify(message));
32-
if (frame && frame.postMessage) {
33-
frame.postMessage(rawMessage, origin);
34-
}
31+
Object.keys(frames).forEach((frameId) => {
32+
const { frame, origin } = frames[frameId];
33+
if (frame && frame.postMessage) {
34+
frame.postMessage(rawMessage, origin);
35+
}
36+
});
3537
}
3638

3739
export function dispatchMessage(message) {
3840
if (!message) return;
3941

42+
// maybe one day i will understand why in the codesandbox
43+
// code they leave notifyListeners in here?
4044
// notifyListener(message);
41-
notifyFrame(message);
45+
notifyFrames(message);
4246
}
4347

4448
/**

0 commit comments

Comments
 (0)