@@ -2,12 +2,10 @@ import PropTypes from 'prop-types';
22import React from 'react' ;
33import ReactDOM from 'react-dom' ;
44// import escapeStringRegexp from 'escape-string-regexp';
5- import { isEqual } from 'lodash' ;
65import srcDoc from 'srcdoc-polyfill' ;
76import loopProtect from 'loop-protect' ;
87import { JSHINT } from 'jshint' ;
98import decomment from 'decomment' ;
10- import { Hook , Unhook , Encode , Decode } from 'console-feed' ;
119import classNames from 'classnames' ;
1210import { getBlobUrl } from '../actions/files' ;
1311import { resolvePathToFile } from '../../../../server/utils/filePath' ;
@@ -21,7 +19,6 @@ import {
2119} from '../../../../server/utils/fileUtils' ;
2220import { hijackConsoleErrorsScript , startTag , getAllScriptOffsets }
2321 from '../../../utils/consoleUtils' ;
24- import handleConsoleExpressions from '../../../utils/evaluateConsole' ;
2522
2623
2724const shouldRenderSketch = ( props , prevProps = undefined ) => {
@@ -43,12 +40,11 @@ const shouldRenderSketch = (props, prevProps = undefined) => {
4340class PreviewFrame extends React . Component {
4441 constructor ( props ) {
4542 super ( props ) ;
46- this . handleConsoleEvent = this . handleConsoleEvent . bind ( this ) ;
43+
44+ this . iframe = React . createRef ( ) ;
4745 }
4846
4947 componentDidMount ( ) {
50- window . addEventListener ( 'message' , this . handleConsoleEvent ) ;
51-
5248 const props = {
5349 ...this . props ,
5450 previewIsRefreshing : this . props . previewIsRefreshing ,
@@ -64,77 +60,10 @@ class PreviewFrame extends React.Component {
6460 }
6561
6662 componentWillUnmount ( ) {
67- window . removeEventListener ( 'message' , this . handleConsoleEvent ) ;
68- const iframeBody = this . iframeElement . contentDocument . body ;
63+ const iframeBody = this . iframe . current . contentDocument . body ;
6964 if ( iframeBody ) { ReactDOM . unmountComponentAtNode ( iframeBody ) ; }
7065 }
7166
72- handleConsoleEvent ( messageEvent ) {
73- if ( messageEvent . origin !== window . origin ) return ;
74- if ( Array . isArray ( messageEvent . data ) ) {
75- const decodedMessages = messageEvent . data . map ( message => Object . assign (
76- Decode ( message . log ) ,
77- { source : message . source }
78- ) ) ;
79- decodedMessages . every ( ( message , index , arr ) => {
80- const { data : args , source } = message ;
81- if ( source === 'console' ) {
82- let consoleInfo = '' ;
83- const consoleBuffer = [ ] ;
84- const LOGWAIT = 100 ;
85- Hook ( window . console , ( log ) => {
86- consoleBuffer . push ( {
87- log,
88- source : 'sketch'
89- } ) ;
90- } ) ;
91- setInterval ( ( ) => {
92- if ( consoleBuffer . length > 0 ) {
93- window . postMessage ( consoleBuffer , '*' ) ;
94- consoleBuffer . length = 0 ;
95- }
96- } , LOGWAIT ) ;
97- consoleInfo = handleConsoleExpressions ( args ) ;
98- Unhook ( window . console ) ;
99- if ( ! consoleInfo ) {
100- return false ;
101- }
102- window . postMessage ( [ {
103- log : Encode ( { method : 'result' , data : Encode ( consoleInfo ) } ) ,
104- source : 'sketch'
105- } ] , '*' ) ;
106- }
107- let hasInfiniteLoop = false ;
108- Object . keys ( args ) . forEach ( ( key ) => {
109- if ( typeof args [ key ] === 'string' && args [ key ] . includes ( 'Exiting potential infinite loop' ) ) {
110- this . props . stopSketch ( ) ;
111- this . props . expandConsole ( ) ;
112- hasInfiniteLoop = true ;
113- }
114- } ) ;
115- if ( hasInfiniteLoop ) {
116- return false ;
117- }
118- if ( index === arr . length - 1 ) {
119- Object . assign ( message , { times : 1 } ) ;
120- return false ;
121- }
122- const cur = Object . assign ( message , { times : 1 } ) ;
123- const nextIndex = index + 1 ;
124- while ( isEqual ( cur . data , arr [ nextIndex ] . data ) && cur . method === arr [ nextIndex ] . method ) {
125- cur . times += 1 ;
126- arr . splice ( nextIndex , 1 ) ;
127- if ( nextIndex === arr . length ) {
128- return false ;
129- }
130- }
131- return true ;
132- } ) ;
133-
134- this . props . dispatchConsoleEvent ( decodedMessages ) ;
135- }
136- }
137-
13867 addLoopProtect ( sketchDoc ) {
13968 const scriptsInHTML = sketchDoc . getElementsByTagName ( 'script' ) ;
14069 const scriptsInHTMLArray = Array . prototype . slice . call ( scriptsInHTML ) ;
@@ -353,7 +282,7 @@ class PreviewFrame extends React.Component {
353282 }
354283
355284 renderSketch ( ) {
356- const doc = this . iframeElement ;
285+ const doc = this . iframe . current ;
357286 const localFiles = this . injectLocalFiles ( ) ;
358287 if ( this . props . isPlaying ) {
359288 this . props . clearConsole ( ) ;
@@ -382,7 +311,7 @@ class PreviewFrame extends React.Component {
382311 role = "main"
383312 frameBorder = "0"
384313 title = "sketch preview"
385- ref = { ( element ) => { this . iframeElement = element ; } }
314+ ref = { this . iframe }
386315 sandbox = { sandboxAttributes }
387316 />
388317 ) ;
@@ -404,17 +333,14 @@ PreviewFrame.propTypes = {
404333 url : PropTypes . string ,
405334 id : PropTypes . string . isRequired
406335 } ) ) . isRequired ,
407- dispatchConsoleEvent : PropTypes . func . isRequired ,
408336 endSketchRefresh : PropTypes . func . isRequired ,
409337 previewIsRefreshing : PropTypes . bool . isRequired ,
410338 fullView : PropTypes . bool ,
411339 setBlobUrl : PropTypes . func . isRequired ,
412- stopSketch : PropTypes . func . isRequired ,
413- expandConsole : PropTypes . func . isRequired ,
414340 clearConsole : PropTypes . func . isRequired ,
415341 cmController : PropTypes . shape ( {
416342 getContent : PropTypes . func
417- } ) ,
343+ } )
418344} ;
419345
420346PreviewFrame . defaultProps = {
0 commit comments