mirror of https://github.com/sgoudham/carbon.git
You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
122 lines
2.5 KiB
JavaScript
122 lines
2.5 KiB
JavaScript
6 years ago
|
// Theirs
|
||
|
import React from 'react'
|
||
|
import Head from 'next/head'
|
||
|
import { withRouter } from 'next/router'
|
||
|
|
||
|
// Ours
|
||
5 years ago
|
import ApiContext from '../../components/ApiContext'
|
||
|
import { StylesheetLink, CodeMirrorLink, MetaTags } from '../../components/Meta'
|
||
|
import Carbon from '../../components/Carbon'
|
||
|
import { DEFAULT_CODE, DEFAULT_SETTINGS } from '../../lib/constants'
|
||
|
import { getRouteState } from '../../lib/routing'
|
||
6 years ago
|
|
||
|
const Page = props => (
|
||
6 years ago
|
<React.Fragment>
|
||
6 years ago
|
<Head>
|
||
6 years ago
|
<title>Carbon Embeds</title>
|
||
6 years ago
|
</Head>
|
||
6 years ago
|
<MetaTags />
|
||
|
<StylesheetLink theme={props.theme} />
|
||
|
<CodeMirrorLink />
|
||
6 years ago
|
{props.children}
|
||
|
<style jsx global>
|
||
|
{`
|
||
|
html,
|
||
|
body {
|
||
|
margin: 0;
|
||
|
background: transparent;
|
||
|
min-height: 0;
|
||
|
}
|
||
|
`}
|
||
|
</style>
|
||
6 years ago
|
</React.Fragment>
|
||
6 years ago
|
)
|
||
|
|
||
|
class Embed extends React.Component {
|
||
6 years ago
|
static contextType = ApiContext
|
||
|
|
||
6 years ago
|
state = {
|
||
|
...DEFAULT_SETTINGS,
|
||
|
code: DEFAULT_CODE,
|
||
6 years ago
|
mounted: false,
|
||
5 years ago
|
readOnly: true,
|
||
|
key: 0
|
||
6 years ago
|
}
|
||
|
|
||
5 years ago
|
snippet = {}
|
||
|
|
||
6 years ago
|
async componentDidMount() {
|
||
5 years ago
|
const { queryState } = getRouteState(this.props.router)
|
||
5 years ago
|
|
||
6 years ago
|
this.setState(
|
||
|
{
|
||
5 years ago
|
...this.props.snippet,
|
||
6 years ago
|
...queryState,
|
||
|
copyable: queryState.copy !== false,
|
||
|
readOnly: queryState.readonly !== false,
|
||
6 years ago
|
mounted: true
|
||
|
},
|
||
|
this.postMessage
|
||
|
)
|
||
6 years ago
|
}
|
||
|
|
||
6 years ago
|
ref = React.createRef()
|
||
6 years ago
|
|
||
6 years ago
|
postMessage = () => {
|
||
6 years ago
|
setTimeout(
|
||
|
() =>
|
||
6 years ago
|
window.top.postMessage(
|
||
6 years ago
|
JSON.stringify({
|
||
|
// Used by embed provider
|
||
|
src: window.location.toString(),
|
||
|
context: 'iframe.resize',
|
||
|
height: this.ref.current.offsetHeight
|
||
|
}),
|
||
|
'*'
|
||
|
),
|
||
|
0
|
||
6 years ago
|
)
|
||
6 years ago
|
}
|
||
6 years ago
|
|
||
6 years ago
|
updateCode = code => {
|
||
|
this.setState({ code }, this.postMessage)
|
||
|
|
||
6 years ago
|
window.top.postMessage(
|
||
6 years ago
|
{
|
||
|
id: this.state.id ? `carbon:${this.state.id}` : 'carbon',
|
||
|
code
|
||
|
},
|
||
|
'*'
|
||
|
)
|
||
|
}
|
||
6 years ago
|
|
||
6 years ago
|
render() {
|
||
|
return (
|
||
|
<Page theme={this.state.theme}>
|
||
5 years ago
|
<div hidden={!this.state.mounted}>
|
||
6 years ago
|
<Carbon
|
||
5 years ago
|
key={this.state.mounted}
|
||
6 years ago
|
ref={this.ref}
|
||
6 years ago
|
config={this.state}
|
||
|
readOnly={this.state.readOnly}
|
||
|
copyable={this.state.copyable}
|
||
6 years ago
|
onChange={this.updateCode}
|
||
6 years ago
|
>
|
||
6 years ago
|
{this.state.code}
|
||
|
</Carbon>
|
||
5 years ago
|
</div>
|
||
6 years ago
|
<style jsx global>
|
||
|
{`
|
||
|
.eliminateOnRender,
|
||
|
.twitter-png-fix {
|
||
|
display: none;
|
||
|
}
|
||
|
`}
|
||
|
</style>
|
||
6 years ago
|
</Page>
|
||
|
)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
export default withRouter(Embed)
|