File tree Expand file tree Collapse file tree 4 files changed +38
-17
lines changed Expand file tree Collapse file tree 4 files changed +38
-17
lines changed Original file line number Diff line number Diff line change @@ -4,7 +4,7 @@ import CodeMirror from 'codemirror';
4
4
import { Encode } from 'console-feed' ;
5
5
6
6
import RightArrowIcon from '../../../images/right-arrow.svg' ;
7
- import { dispatchMessage } from '../../../utils/dispatcher' ;
7
+ import { dispatchMessage , MessageTypes } from '../../../utils/dispatcher' ;
8
8
9
9
// heavily inspired by
10
10
// 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 {
41
41
] ;
42
42
const consoleEvent = [ { method : 'command' , data : [ value ] } ] ;
43
43
dispatchMessage ( {
44
- source : 'console' ,
45
- messages
44
+ type : MessageTypes . EXECUTE ,
45
+ payload : {
46
+ source : 'console' ,
47
+ messages
48
+ }
46
49
} ) ;
47
50
this . props . dispatchConsoleEvent ( consoleEvent ) ;
48
51
cm . setValue ( '' ) ;
Original file line number Diff line number Diff line change @@ -17,6 +17,7 @@ import {
17
17
NOT_EXTERNAL_LINK_REGEX
18
18
} from '../../../server/utils/fileUtils' ;
19
19
import { startTag , getAllScriptOffsets } from '../../utils/consoleUtils' ;
20
+ import { registerFrame } from '../../utils/dispatcher' ;
20
21
21
22
const Frame = styled . iframe `
22
23
min-height: 100%;
@@ -233,6 +234,16 @@ function EmbedFrame({ files, isPlaying }) {
233
234
const iframe = useRef ( ) ;
234
235
const htmlFile = useMemo ( ( ) => getHtmlFile ( files ) , [ files ] ) ;
235
236
237
+ useEffect ( ( ) => {
238
+ const unsubscribe = registerFrame (
239
+ iframe . current . contentWindow ,
240
+ window . origin
241
+ ) ;
242
+ return ( ) => {
243
+ unsubscribe ( ) ;
244
+ } ;
245
+ } ) ;
246
+
236
247
function renderSketch ( ) {
237
248
const doc = iframe . current ;
238
249
if ( isPlaying ) {
Original file line number Diff line number Diff line change @@ -38,6 +38,9 @@ const App = () => {
38
38
case MessageTypes . REGISTER :
39
39
dispatchMessage ( { type : MessageTypes . REGISTER } ) ;
40
40
break ;
41
+ case MessageTypes . EXECUTE :
42
+ dispatchMessage ( payload ) ;
43
+ break ;
41
44
default :
42
45
break ;
43
46
}
Original file line number Diff line number Diff line change 1
1
// Inspired by
2
2
// https://github.com/codesandbox/codesandbox-client/blob/master/packages/codesandbox-api/src/dispatcher/index.ts
3
3
4
- let frame = null ;
4
+ const frames = { } ;
5
+ let frameIndex = 1 ;
5
6
let listener = null ;
6
- // const { origin } = window;
7
- let origin = null ;
8
7
9
8
export const MessageTypes = {
10
9
START : 'START' ,
11
10
STOP : 'STOP' ,
12
11
FILES : 'FILES' ,
13
- REGISTER : 'REGISTER'
12
+ REGISTER : 'REGISTER' ,
13
+ EXECUTE : 'EXECUTE'
14
14
} ;
15
15
16
- // could instead register multiple frames here
17
16
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 } ;
20
20
return ( ) => {
21
- frame = null ;
22
- origin = null ;
21
+ delete frames [ frameId ] ;
23
22
} ;
24
23
}
25
24
26
25
function notifyListener ( message ) {
27
26
if ( listener ) listener ( message ) ;
28
27
}
29
28
30
- function notifyFrame ( message ) {
29
+ function notifyFrames ( message ) {
31
30
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
+ } ) ;
35
37
}
36
38
37
39
export function dispatchMessage ( message ) {
38
40
if ( ! message ) return ;
39
41
42
+ // maybe one day i will understand why in the codesandbox
43
+ // code they leave notifyListeners in here?
40
44
// notifyListener(message);
41
- notifyFrame ( message ) ;
45
+ notifyFrames ( message ) ;
42
46
}
43
47
44
48
/**
You can’t perform that action at this time.
0 commit comments