post message

main
Mike Fix 6 years ago
parent f149e115d5
commit fd37d6f0fa

@ -12,6 +12,15 @@ import { DEFAULT_CODE, DEFAULT_SETTINGS } from '../lib/constants'
import { getQueryStringState } from '../lib/routing' import { getQueryStringState } from '../lib/routing'
const isInIFrame = morph.get('parent.window.parent') const isInIFrame = morph.get('parent.window.parent')
const getParent = win => {
const iFrame = isInIFrame(win)
if (iFrame) {
return iFrame
}
return win.parent
}
const Page = props => ( const Page = props => (
<React.Fragment> <React.Fragment>
@ -49,35 +58,43 @@ class Embed extends React.Component {
const queryParams = getQueryStringState(query) const queryParams = getQueryStringState(query)
const initialState = Object.keys(queryParams).length ? queryParams : {} const initialState = Object.keys(queryParams).length ? queryParams : {}
this.setState({ this.setState(
{
...initialState, ...initialState,
id: query.id, id: query.id,
copyable: queryParams.copy !== false, copyable: queryParams.copy !== false,
readOnly: queryParams.readonly !== false, readOnly: queryParams.readonly !== false,
mounted: true mounted: true
}) },
this.postMessage
)
} }
updateCode = code => { ref = React.createRef()
this.setState({ code })
const iFrame = isInIFrame(window) postMessage = () => {
if (iFrame) { getParent(window).postMessage(
iFrame.postMessage( JSON.stringify({
{ // Used by embed provider
src: window.location.toString(),
context: 'iframe.resize',
height: this.ref.current.exportContainerNode.offsetHeight,
// Carbon specific data
id: this.state.id ? `carbon:${this.state.id}` : 'carbon', id: this.state.id ? `carbon:${this.state.id}` : 'carbon',
code code: this.state.code
}, }),
'*' '*'
) )
} }
}
updateCode = code => this.setState({ code }, this.postMessage)
render() { render() {
return ( return (
<Page theme={this.state.theme}> <Page theme={this.state.theme}>
{this.state.mounted && ( {this.state.mounted && (
<Carbon <Carbon
ref={this.ref}
config={this.state} config={this.state}
readOnly={this.state.readOnly} readOnly={this.state.readOnly}
copyable={this.state.copyable} copyable={this.state.copyable}

Loading…
Cancel
Save