Skip to content

Commit

Permalink
Merge pull request #762 from busyorg/nd/youtube-preview
Browse files Browse the repository at this point in the history
Use raw YouTube embed in single post
  • Loading branch information
Sekhmet committed Oct 7, 2017
2 parents a2ced3d + eef058d commit 0ca3b4a
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 33 deletions.
10 changes: 3 additions & 7 deletions src/components/Story/Body.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ export function getHtml(body, jsonMetadata = {}, returnType = 'Object') {
const id = match[1];
const type = match[2];
const link = match[3];
const embed = embedjs.get(link, { width: '100%', height: 400, autoplay: true });
sections.push(<PostFeedEmbed key={`embed-a-${i}`} embed={embed} />);
const embed = embedjs.get(link, { width: '100%', height: 400, autoplay: false });
sections.push(<PostFeedEmbed key={`embed-a-${i}`} inPost embed={embed} />);
section = section.substring(`${id} ${type} ${link} ~~~`.length);
}
if (section !== '') {
Expand All @@ -67,11 +67,7 @@ export function getHtml(body, jsonMetadata = {}, returnType = 'Object') {

const Body = (props) => {
const htmlSections = getHtml(props.body, props.jsonMetadata);
return (
<div className={classNames('Body', { 'Body--full': props.full })}>
{htmlSections}
</div>
);
return <div className={classNames('Body', { 'Body--full': props.full })}>{htmlSections}</div>;
};

Body.propTypes = {
Expand Down
22 changes: 14 additions & 8 deletions src/components/Story/PostFeedEmbed.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ export default class PostFeedEmbed extends React.Component {
thumbnail: PropTypes.string,
embed: PropTypes.string,
}).isRequired,
inPost: PropTypes.bool,
};

static renderWithIframe(embed) {
// eslint-disable-next-line react/no-danger
return <div dangerouslySetInnerHTML={{ __html: embed }} />;
}
static defaultProps = {
inPost: false,
};

constructor(props) {
super(props);
Expand All @@ -28,6 +28,11 @@ export default class PostFeedEmbed extends React.Component {
this.setState({ showIframe: true });
};

renderWithIframe = embed => (
// eslint-disable-next-line react/no-danger
<div dangerouslySetInnerHTML={{ __html: embed }} />
);

renderThumbFirst(thumb) {
return (
<div role="presentation" className="PostFeedEmbed" onClick={this.handleThumbClick}>
Expand All @@ -40,15 +45,16 @@ export default class PostFeedEmbed extends React.Component {
}

render() {
const { embed } = this.props;
const { embed, inPost } = this.props;
const shouldRenderThumb = inPost ? false : !this.state.showIframe;

if (
(embed.provider_name === 'YouTube' || embed.provider_name === 'DTube')
&& !this.state.showIframe
(embed.provider_name === 'YouTube' || embed.provider_name === 'DTube') &&
shouldRenderThumb
) {
return this.renderThumbFirst(embed.thumbnail);
} else if (embed.embed) {
return PostFeedEmbed.renderWithIframe(embed.embed);
return this.renderWithIframe(embed.embed);
}
return <div />;
}
Expand Down
2 changes: 1 addition & 1 deletion src/components/Story/PostFeedEmbed.less
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

&__preview {
display: block;
max-width: 100%;
width: 100%;
max-height: 400px;
margin: 0 auto;
}
Expand Down
23 changes: 6 additions & 17 deletions src/stories/__snapshots__/stories.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -2171,23 +2171,12 @@ exports[`Storyshots Story Full story with embed 1`] = `
}
/>
<div
className="PostFeedEmbed"
onClick={[Function]}
role="presentation"
>
<div
className="PostFeedEmbed__playButton"
>
<i
className="iconfont icon-group icon-playon_fill"
/>
</div>
<img
alt="thumbnail"
className="PostFeedEmbed__preview"
src="https://i.ytimg.com/vi/YxFzGoDoLwU/hqdefault.jpg"
/>
</div>
dangerouslySetInnerHTML={
Object {
"__html": "<iframe width=\\"100%\\" height=\\"400\\" src=\\"//www.youtube.com/embed/YxFzGoDoLwU?autoplay=0\\" frameborder=\\"0\\" scrolling=\\"no\\" allowfullscreen></iframe>",
}
}
/>
<div
dangerouslySetInnerHTML={
Object {
Expand Down

0 comments on commit 0ca3b4a

Please sign in to comment.