Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Isolate iframe code from parent window (#57)
fixes #45
- Loading branch information
1 parent
9ccae36
commit 03e41a3
Showing
16 changed files
with
222 additions
and
236 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
cypress/plugins/ | ||
cypress/fixtures/ | ||
dist |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import React, { Component } from 'react'; | ||
import queryString from 'query-string'; | ||
import CatchErrors from './CatchErrors/CatchErrors'; | ||
import RenderCode from './RenderCode/RenderCode'; | ||
|
||
const themesImport = require('./themes'); | ||
const componentsImport = require('./components'); | ||
const frameComponentImport = require('./frameComponent'); | ||
|
||
const getQueryParams = () => { | ||
try { | ||
const hash = window.location.hash.replace(/^#/, ''); | ||
return queryString.parse(hash); | ||
} catch (err) { | ||
return {}; | ||
} | ||
}; | ||
|
||
export default class Frame extends Component { | ||
constructor(props) { | ||
super(props); | ||
|
||
const { themeName, code = '' } = getQueryParams(); | ||
|
||
this.state = { | ||
themeName, | ||
themes: themesImport, | ||
components: componentsImport, | ||
FrameComponent: frameComponentImport, | ||
code | ||
}; | ||
} | ||
|
||
componentDidMount() { | ||
window.addEventListener('hashchange', () => { | ||
const { themeName, code } = getQueryParams(); | ||
|
||
if (themeName && code) { | ||
this.setState({ themeName, code }); | ||
} | ||
}); | ||
|
||
if (module.hot) { | ||
module.hot.accept('./themes', () => { | ||
this.setState({ themes: require('./themes') }); | ||
}); | ||
|
||
module.hot.accept('./components', () => { | ||
this.setState({ components: require('./components') }); | ||
}); | ||
|
||
module.hot.accept('./frameComponent', () => { | ||
this.setState({ frameComponent: require('./frameComponent') }); | ||
}); | ||
} | ||
} | ||
|
||
render() { | ||
const { FrameComponent, themes, themeName, components, code } = this.state; | ||
|
||
const resolvedThemeName = | ||
themeName === '__PLAYROOM__NO_THEME__' ? null : themeName; | ||
const resolvedTheme = resolvedThemeName ? themes[resolvedThemeName] : null; | ||
|
||
return ( | ||
<CatchErrors code={code}> | ||
<FrameComponent themeName={resolvedThemeName} theme={resolvedTheme}> | ||
<RenderCode code={code} scope={components} /> | ||
</FrameComponent> | ||
</CatchErrors> | ||
); | ||
} | ||
} |
Oops, something went wrong.