11import { makeDecorator } from '@storybook/addons' ;
22import { EditorElement } from './editor' ;
33
4+ const mgtScriptName = './mgt.storybook.js' ;
5+
46// function is used for dragging and moving
57const setupEditorResize = ( first , separator , last , dragComplete ) => {
68 var md ; // remember mouse down info
@@ -63,14 +65,11 @@ export const withCodeEditor = makeDecorator({
6365 let storyHtml ;
6466 const root = document . createElement ( 'div' ) ;
6567 let storyElementWrapper = document . createElement ( 'div' ) ;
66- let storyElement ;
6768
6869 if ( story . strings ) {
6970 storyHtml = story . strings [ 0 ] ;
70- storyElement = document . createElement ( 'div' ) ;
7171 } else {
7272 storyHtml = story . innerHTML ;
73- storyElement = story ;
7473 }
7574
7675 let scriptMatches = scriptRegex . exec ( storyHtml ) ;
@@ -93,8 +92,46 @@ export const withCodeEditor = makeDecorator({
9392 } ;
9493
9594 editor . addEventListener ( 'fileUpdated' , ( ) => {
96- storyElement . innerHTML = editor . files . html + `<style>${ editor . files . css } </style>` ;
97- eval ( editor . files . js ) ;
95+ const storyElement = document . createElement ( 'iframe' ) ;
96+
97+ storyElement . addEventListener ( 'load' , ( ) => {
98+ let doc = storyElement . contentDocument ;
99+
100+ let { html, css, js } = editor . files ;
101+ js = js . replace (
102+ / i m p o r t \{ ( [ ^ \} ] + ) \} \s + f r o m \s + [ ' " ] @ m i c r o s o f t \/ m g t [ ' " ] ; / gm,
103+ `import {$1} from '${ mgtScriptName } ';`
104+ ) ;
105+
106+ const docContent = `
107+ <html>
108+ <head>
109+ <script type="module" src="${ mgtScriptName } "></script>
110+ <script type="module">
111+ import {Providers, MockProvider} from "${ mgtScriptName } ";
112+ Providers.globalProvider = new MockProvider(true);
113+ </script>
114+ <style>
115+ ${ css }
116+ </style>
117+ </head>
118+ <body>
119+ ${ html }
120+ <script type="module">
121+ ${ js }
122+ </script>
123+ </body>
124+ </html>
125+ ` ;
126+
127+ doc . open ( ) ;
128+ doc . write ( docContent ) ;
129+ doc . close ( ) ;
130+ } ) ;
131+
132+ storyElement . className = 'story-mgt-preview' ;
133+ storyElementWrapper . innerHTML = '' ;
134+ storyElementWrapper . appendChild ( storyElement ) ;
98135 } ) ;
99136
100137 const separator = document . createElement ( 'div' ) ;
@@ -103,12 +140,10 @@ export const withCodeEditor = makeDecorator({
103140
104141 root . className = 'story-mgt-root' ;
105142 storyElementWrapper . className = 'story-mgt-preview-wrapper' ;
106- storyElement . className = 'story-mgt-preview' ;
107143 separator . className = 'story-mgt-separator' ;
108144 editor . className = 'story-mgt-editor' ;
109145
110146 root . appendChild ( storyElementWrapper ) ;
111- storyElementWrapper . appendChild ( storyElement ) ;
112147 root . appendChild ( separator ) ;
113148 root . appendChild ( editor ) ;
114149
0 commit comments