Skip to content

Commit

Permalink
Merge pull request #292 from twreporter/revert-286-jason/issue_60
Browse files Browse the repository at this point in the history
Revert "Jason/issue 60"
  • Loading branch information
duidae authored Jul 20, 2022
2 parents 5ff16a8 + 1574eed commit 887546f
Showing 1 changed file with 13 additions and 72 deletions.
Original file line number Diff line number Diff line change
@@ -1,18 +1,14 @@
import React, { useRef } from 'react'
import predefinedPropTypes from '../../constants/prop-types/body'
import PropTypes from 'prop-types'
import React from 'react'
import styled from 'styled-components'
import { Waypoint } from 'react-waypoint'

import themeConst from '../../constants/theme'
import colorConst from '../../constants/color'
// lodash
import forEach from 'lodash/forEach'
import get from 'lodash/get'
import merge from 'lodash/merge'

// twreporter
import themeConst from '../../constants/theme'
import colorConst from '../../constants/color'
import predefinedPropTypes from '../../constants/prop-types/body'

const _ = {
forEach,
get,
Expand Down Expand Up @@ -59,9 +55,7 @@ function dispatchWindowLoadEvent() {
window.dispatchEvent(loadEvent)
}

const infogramEmbed = 'infogram'

class EmbeddedCode extends React.PureComponent {
export default class EmbeddedCode extends React.PureComponent {
static propTypes = {
className: PropTypes.string,
data: predefinedPropTypes.elementData,
Expand All @@ -71,37 +65,12 @@ class EmbeddedCode extends React.PureComponent {
className: '',
}

state = {
isLoaded: false,
}

constructor(props) {
super(props)
this._embedded = React.createRef()
const { caption, embeddedCodeWithoutScript } = _.get(
this.props,
['data', 'content', 0],
{}
)
this._caption = caption
this._embeddedCodeWithoutScript = embeddedCodeWithoutScript
}

componentDidMount() {
// Delay loading infogram in loadEmbed()
if (!this._embeddedCodeWithoutScript?.includes(infogramEmbed)) {
this.executeScript()
}
}

componentWillUnmount() {
this._embedded = null
this._caption = null
this._embeddedCodeWithoutScript = null
}

executeScript = () => {
this.setState({ isLoaded: true })
const node = this._embedded.current
const scripts = _.get(this.props, ['data', 'content', 0, 'scripts'])
if (node && Array.isArray(scripts)) {
Expand Down Expand Up @@ -145,49 +114,21 @@ class EmbeddedCode extends React.PureComponent {
}
}

loadEmbed = () => {
if (!this.state.isLoaded) {
this.executeScript()
}
}

render() {
const { className } = this.props
const embed = (
const { caption, embeddedCodeWithoutScript } = _.get(
this.props,
['data', 'content', 0],
{}
)
return (
<div className={className}>
<Block
ref={this._embedded}
dangerouslySetInnerHTML={{ __html: this._embeddedCodeWithoutScript }}
dangerouslySetInnerHTML={{ __html: embeddedCodeWithoutScript }}
/>
{this._caption ? <Caption>{this._caption}</Caption> : null}
{caption ? <Caption>{caption}</Caption> : null}
</div>
)

if (this._embeddedCodeWithoutScript?.includes(infogramEmbed)) {
return this.state.isLoaded ? embed : null
}

return embed
}
}

// Serious layout shifts show up when loading bunch of infograms due to lack of heights,
// so here we apply waypoint wrapper to load infogram dynamically to avoid layout shifts for anchors.
// https://twreporter-org.atlassian.net/browse/TWREPORTER-60
const WayPointWrapper = props => {
const embedRef = useRef(null)

const onEnter = () => {
embedRef.current.loadEmbed()
}

return (
<Waypoint onEnter={onEnter} fireOnRapidScroll={false}>
<div>
<EmbeddedCode {...props} ref={embedRef} />
</div>
</Waypoint>
)
}

export default WayPointWrapper

1 comment on commit 887546f

@vercel
Copy link

@vercel vercel bot commented on 887546f Jul 20, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.