remove mutation observer (#723)

main
Michael Fix 6 years ago committed by GitHub
parent 4edc922f40
commit afa63c3653
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -18,7 +18,7 @@ class BackgroundSelect extends React.PureComponent {
handlePickColor = ({ rgb }) => this.props.onChange({ backgroundColor: stringifyRGBA(rgb) }) handlePickColor = ({ rgb }) => this.props.onChange({ backgroundColor: stringifyRGBA(rgb) })
render() { render() {
const { color, mode, image, onChange, aspectRatio, isVisible, toggleVisibility } = this.props const { color, mode, image, onChange, isVisible, toggleVisibility, carbonRef } = this.props
let background = typeof color === 'string' ? escape(color).replace(/\//g, '/') : color let background = typeof color === 'string' ? escape(color).replace(/\//g, '/') : color
@ -26,6 +26,8 @@ class BackgroundSelect extends React.PureComponent {
background = DEFAULT_BG_COLOR background = DEFAULT_BG_COLOR
} }
const aspectRatio = carbonRef ? carbonRef.clientWidth / carbonRef.clientHeight : 1
return ( return (
<div className="bg-select-container"> <div className="bg-select-container">
<div className={`bg-select-display ${isVisible ? 'is-visible' : ''}`}> <div className={`bg-select-display ${isVisible ? 'is-visible' : ''}`}>

@ -30,23 +30,9 @@ function searchLanguage(l) {
class Carbon extends React.PureComponent { class Carbon extends React.PureComponent {
static defaultProps = { static defaultProps = {
onAspectRatioChange: () => {},
onChange: () => {} onChange: () => {}
} }
componentDidMount() {
const node = this.props.innerRef.current
this.mo = new MutationObserver(() => {
const ratio = node.clientWidth / node.clientHeight
this.props.onAspectRatioChange(ratio)
})
this.mo.observe(node, { attributes: true, childList: true, subtree: true })
}
componentWillUnmount() {
this.mo.disconnect()
}
handleLanguageChange = debounce( handleLanguageChange = debounce(
(newCode, language) => { (newCode, language) => {
if (language === 'auto') { if (language === 'auto') {

@ -101,8 +101,6 @@ class Editor extends React.Component {
updateCode = code => this.updateState({ code }) updateCode = code => this.updateState({ code })
updateAspectRatio = aspectRatio => this.updateState({ aspectRatio })
async getCarbonImage( async getCarbonImage(
{ {
format, format,
@ -277,12 +275,11 @@ class Editor extends React.Component {
backgroundColor, backgroundColor,
backgroundImage, backgroundImage,
backgroundMode, backgroundMode,
aspectRatio,
code, code,
exportSize exportSize
} = this.state } = this.state
const config = omit(this.state, ['code', 'aspectRatio']) const config = omit(this.state, ['code'])
return ( return (
<div className="editor"> <div className="editor">
@ -304,7 +301,7 @@ class Editor extends React.Component {
mode={backgroundMode} mode={backgroundMode}
color={backgroundColor} color={backgroundColor}
image={backgroundImage} image={backgroundImage}
aspectRatio={aspectRatio} carbonRef={this.carbonNode.current}
/> />
<Settings <Settings
{...config} {...config}
@ -337,7 +334,6 @@ class Editor extends React.Component {
ref={this.carbonNode} ref={this.carbonNode}
config={this.state} config={this.state}
onChange={this.updateCode} onChange={this.updateCode}
onAspectRatioChange={this.updateAspectRatio}
loading={this.state.loading} loading={this.state.loading}
> >
{code != null ? code : DEFAULT_CODE} {code != null ? code : DEFAULT_CODE}

Loading…
Cancel
Save