From d4f1f981a5143663e03a1daab8105cc11b39820d Mon Sep 17 00:00:00 2001
From: JoramWilander <jwawilander@gmail.com>
Date: Thu, 22 Oct 2015 08:45:28 -0400
Subject: Auto-embed gifs from .gif links

---
 web/react/components/post_body.jsx | 49 ++++++++++++++++++++++++++++++++++++--
 1 file changed, 47 insertions(+), 2 deletions(-)

(limited to 'web/react/components/post_body.jsx')

diff --git a/web/react/components/post_body.jsx b/web/react/components/post_body.jsx
index ae94bd42e..6b15682b0 100644
--- a/web/react/components/post_body.jsx
+++ b/web/react/components/post_body.jsx
@@ -15,6 +15,9 @@ export default class PostBody extends React.Component {
         this.receivedYoutubeData = false;
 
         this.parseEmojis = this.parseEmojis.bind(this);
+        this.createEmbed = this.createEmbed.bind(this);
+        this.createGifEmbed = this.createGifEmbed.bind(this);
+        this.loadGif = this.loadGif.bind(this);
         this.createYoutubeEmbed = this.createYoutubeEmbed.bind(this);
 
         const linkData = Utils.extractLinks(this.props.post.message);
@@ -46,6 +49,7 @@ export default class PostBody extends React.Component {
 
     componentDidUpdate() {
         this.parseEmojis();
+        this.props.resize();
     }
 
     componentWillReceiveProps(nextProps) {
@@ -53,6 +57,46 @@ export default class PostBody extends React.Component {
         this.setState({links: linkData.links, message: linkData.text});
     }
 
+    createEmbed(link) {
+        let embed = this.createYoutubeEmbed(link);
+
+        if (embed != null) {
+            return embed;
+        }
+
+        embed = this.createGifEmbed(link);
+
+        return embed;
+    }
+
+    loadGif(src) {
+        const gif = new Image();
+        gif.src = src;
+        gif.onload = (
+            () => {
+                this.setState({gifLoaded: true});
+            }
+        );
+    }
+
+    createGifEmbed(link) {
+        if (link.substring(link.length - 4) !== '.gif') {
+            return null;
+        }
+
+        if (!this.state.gifLoaded) {
+            this.loadGif(link);
+            return null;
+        }
+
+        return (
+            <img
+                className='gif-div'
+                src={link}
+            />
+        );
+    }
+
     handleYoutubeTime(link) {
         const timeRegex = /[\\?&]t=([0-9hms]+)/;
 
@@ -247,7 +291,7 @@ export default class PostBody extends React.Component {
 
         let embed;
         if (filenames.length === 0 && this.state.links) {
-            embed = this.createYoutubeEmbed(this.state.links[0]);
+            embed = this.createEmbed(this.state.links[0]);
         }
 
         let fileAttachmentHolder = '';
@@ -287,5 +331,6 @@ PostBody.propTypes = {
     post: React.PropTypes.object.isRequired,
     parentPost: React.PropTypes.object,
     retryPost: React.PropTypes.func.isRequired,
-    handleCommentClick: React.PropTypes.func.isRequired
+    handleCommentClick: React.PropTypes.func.isRequired,
+    resize: React.PropTypes.func.isRequired
 };
-- 
cgit v1.2.3-1-g7c22


From 4c0e4991e76c18fd39ffadcf5b0e1943fb4cd2c3 Mon Sep 17 00:00:00 2001
From: JoramWilander <jwawilander@gmail.com>
Date: Fri, 23 Oct 2015 08:01:51 -0400
Subject: Make sure gif only loads once

---
 web/react/components/post_body.jsx | 7 +++++++
 1 file changed, 7 insertions(+)

(limited to 'web/react/components/post_body.jsx')

diff --git a/web/react/components/post_body.jsx b/web/react/components/post_body.jsx
index 6b15682b0..45eae8c6a 100644
--- a/web/react/components/post_body.jsx
+++ b/web/react/components/post_body.jsx
@@ -13,6 +13,7 @@ export default class PostBody extends React.Component {
         super(props);
 
         this.receivedYoutubeData = false;
+        this.isGifLoading = false;
 
         this.parseEmojis = this.parseEmojis.bind(this);
         this.createEmbed = this.createEmbed.bind(this);
@@ -70,6 +71,12 @@ export default class PostBody extends React.Component {
     }
 
     loadGif(src) {
+        if (this.isGifLoading) {
+            return;
+        }
+
+        this.isGifLoading = true;
+
         const gif = new Image();
         gif.src = src;
         gif.onload = (
-- 
cgit v1.2.3-1-g7c22

