diff --git a/components/Carbon.js b/components/Carbon.js index e2c91b7..a3b7dbe 100644 --- a/components/Carbon.js +++ b/components/Carbon.js @@ -1,7 +1,6 @@ import React from 'react' import dynamic from 'next/dynamic' import * as hljs from 'highlight.js' -import ResizeObserver from 'resize-observer-polyfill' import debounce from 'lodash.debounce' import ms from 'ms' import { Controlled as CodeMirror } from 'react-codemirror2' @@ -22,16 +21,21 @@ const Watermark = dynamic(() => import('../components/svg/Watermark'), { class Carbon extends React.PureComponent { static defaultProps = { onAspectRatioChange: () => {}, - updateCode: () => {}, + onChange: () => {}, innerRef: () => {} } componentDidMount() { - const ro = new ResizeObserver(entries => { - const cr = entries[0].contentRect - this.props.onAspectRatioChange(cr.width / cr.height) + const node = this.exportContainerNode + this.mo = new MutationObserver(() => { + const ratio = node.clientWidth / node.clientHeight + this.props.onAspectRatioChange(ratio) }) - ro.observe(this.exportContainerNode) + this.mo.observe(node, { attributes: true, childList: true, subtree: true }) + } + + componentWillUnmount() { + this.mo.disconnect() } handleLanguageChange = debounce( @@ -58,7 +62,7 @@ class Carbon extends React.PureComponent { onBeforeChange = (editor, meta, code) => { if (!this.props.readOnly) { - this.props.updateCode(code) + this.props.onChange(code) } } diff --git a/components/Editor.js b/components/Editor.js index 830596d..26a625f 100644 --- a/components/Editor.js +++ b/components/Editor.js @@ -384,7 +384,7 @@ class Editor extends React.Component { {this.state.code} diff --git a/yarn.lock b/yarn.lock index c206daf..683b9b4 100644 --- a/yarn.lock +++ b/yarn.lock @@ -6112,11 +6112,6 @@ require-main-filename@^1.0.1: resolved "https://registry.yarnpkg.com/require-main-filename/-/require-main-filename-1.0.1.tgz#97f717b69d48784f5f526a6c5aa8ffdda055a4d1" integrity sha1-l/cXtp1IeE9fUmpsWqj/3aBVpNE= -resize-observer-polyfill@^1.5.1: - version "1.5.1" - resolved "https://registry.yarnpkg.com/resize-observer-polyfill/-/resize-observer-polyfill-1.5.1.tgz#0e9020dd3d21024458d4ebd27e23e40269810464" - integrity sha512-LwZrotdHOo12nQuZlHEmtuXdqGoOD0OhaxopaNFxWzInpEgaLWoVuAMbTzixuosCx2nEG58ngzW3vxdWoxIgdg== - resolve-from@^3.0.0: version "3.0.0" resolved "https://registry.yarnpkg.com/resolve-from/-/resolve-from-3.0.0.tgz#b22c7af7d9d6881bc8b6e653335eebcb0a188748"