11const url_re = / h t t p s ? : \/ \/ ( w w w \. ) ? [ - a - z A - Z 0 - 9 @ : % . _ \+ ~ # = ] { 2 , 256 } \. [ a - z ] { 2 , 10 } \b ( [ - a - z A - Z 0 - 9 @ : % _ \+ . ~ # ? & / / = ] * ) / ;
22const youtube_re = / (?: h t t p s ? : \/ \/ | \/ \/ ) ? (?: w w w \. | m \. ) ? (?: y o u t u \. b e \/ | y o u t u b e \. c o m \/ (?: e m b e d \/ | v \/ | w a t c h \? v = | w a t c h \? .+ & v = ) ) ( [ \w - ] { 11 } ) (? ! [ \w - ] ) / ;
3+ const giphy_re = / (?: h t t p s ? : \/ \/ ) ? (?: m e d i a 0 \. ) ? (?: g i p h y \. c o m \/ m e d i a \/ ) / ;
34
45$ ( document ) . ready ( function ( ) {
5- embedded_resource = '' ;
6+ var embedded_resource = '' ;
7+ const GIPHY_API_KEY = document . contxt . giphy_key ;
8+
69 let button = document . querySelector ( '#btn_post' ) ;
10+
11+ function selectGif ( e ) {
12+ embedded_resource = $ ( e . target ) . data ( 'src' ) ;
13+ $ ( '#preview-img' ) . attr ( 'src' , embedded_resource ) ;
14+ $ ( '#preview' ) . show ( ) ;
15+ $ ( '#thumbnail' ) . hide ( ) ;
16+ }
17+
18+
19+ function injectGiphy ( query ) {
20+ const endpoint = 'https://api.giphy.com/v1/gifs/search?limit=13&api_key=' + GIPHY_API_KEY + '&offset=0&rating=G&lang=en&q=' + query ;
21+ const result = fetchData ( endpoint ) ;
22+
23+ $ . when ( result ) . then ( function ( response ) {
24+ $ ( '.pick-gif' ) . remove ( ) ;
25+
26+ for ( let i = 0 ; i < response . data . length ; i ++ ) {
27+ let item = response . data [ i ] ;
28+ let downsize = item . images . original . webp ;
29+ let preview = item . images . fixed_width_downsampled . webp ;
30+
31+ $ ( '.gif-grid' ) . append ( '<img class="pick-gif" src="' + preview + '" data-src="' + downsize + '" alt="' + item . slug + '">' ) ;
32+ }
33+ $ ( '.pick-gif' ) . on ( 'click' , selectGif ) ;
34+ } ) ;
35+ }
36+
37+ $ ( '#search-gif' ) . on ( 'input' , function ( e ) {
38+ e . preventDefault ( ) ;
39+ const query = e . target . value ;
40+
41+ injectGiphy ( query ) ;
42+ } ) ;
743
844 if ( button ) {
945 button . addEventListener (
@@ -31,6 +67,11 @@ $(document).ready(function() {
3167 const youtube = e . target . value . match ( youtube_re ) ;
3268 const no_lb = e . originalEvent . inputType !== 'insertLineBreak' ;
3369
70+ // GIF has priority, no other display info allowed
71+ if ( typeof embedded_resource === 'string' && embedded_resource . match ( giphy_re ) ) {
72+ return ;
73+ }
74+
3475 if ( youtube !== null && youtube [ 1 ] . length === 11 && no_lb ) {
3576 let videoId = youtube [ 1 ] ;
3677
@@ -46,6 +87,7 @@ $(document).ready(function() {
4687 $ ( '#thumbnail-img' ) . attr ( 'src' , 'https://img.youtube.com/vi/' + videoId + '/default.jpg' ) ;
4788 embedded_resource = youtube [ 0 ] ;
4889 $ ( '#thumbnail' ) . show ( ) ;
90+ $ ( '#preview' ) . hide ( ) ;
4991 } else {
5092 $ ( '#thumbnail' ) . hide ( ) ;
5193 $ ( '#thumbnail-desc' ) . text ( '' ) ;
@@ -75,6 +117,7 @@ $(document).ready(function() {
75117
76118 embedded_resource = url ;
77119 $ ( '#thumbnail' ) . show ( ) ;
120+ $ ( '#preview' ) . hide ( ) ;
78121 } else {
79122 $ ( '#thumbnail' ) . hide ( ) ;
80123 $ ( '#thumbnail-desc' ) . text ( '' ) ;
@@ -192,18 +235,25 @@ $(document).ready(function() {
192235 const image = $ ( '#thumbnail-img' ) . attr ( 'src' ) ;
193236 const youtube = embedded_resource . match ( youtube_re ) ;
194237
195- if ( youtube !== null && youtube [ 1 ] . length === 11 ) {
238+ if ( embedded_resource . match ( giphy_re ) ) {
239+ data . append ( 'resource' , 'gif' ) ;
240+ data . append ( 'resourceProvider' , 'giphy' ) ;
241+ data . append ( 'resourceId' , embedded_resource ) ;
242+ } else if ( youtube !== null && youtube [ 1 ] . length === 11 ) {
196243 data . append ( 'resource' , 'video' ) ;
197244 data . append ( 'resourceProvider' , 'youtube' ) ;
198245 data . append ( 'resourceId' , youtube [ 1 ] ) ;
246+ data . append ( 'title' , title ) ;
247+ data . append ( 'description' , description ) ;
248+ data . append ( 'image' , image ) ;
199249 } else {
200250 data . append ( 'resource' , 'content' ) ;
201251 data . append ( 'resourceProvider' , link ) ;
202252 data . append ( 'resourceId' , embedded_resource ) ;
253+ data . append ( 'title' , title ) ;
254+ data . append ( 'description' , description ) ;
255+ data . append ( 'image' , image ) ;
203256 }
204- data . append ( 'title' , title ) ;
205- data . append ( 'description' , description ) ;
206- data . append ( 'image' , image ) ;
207257 }
208258
209259 fetch ( '/api/v0.1/activity' , {
@@ -216,7 +266,9 @@ $(document).ready(function() {
216266 $ ( '#thumbnail-title' ) . text ( '' ) ;
217267 $ ( '#thumbnail-provider' ) . text ( '' ) ;
218268 $ ( '#thumbnail-desc' ) . text ( '' ) ;
219- $ ( '#thumbnail-img' ) . attr ( '' ) ;
269+ $ ( '#thumbnail-img' ) . attr ( 'src' , '' ) ;
270+ $ ( '#preview' ) . hide ( ) ;
271+ $ ( '#preview-img' ) . attr ( 'src' , '' ) ;
220272 embedded_resource = '' ;
221273
222274 _alert (
@@ -243,6 +295,8 @@ $(document).ready(function() {
243295 } )
244296 . catch ( err => console . log ( 'Error ' , err ) ) ;
245297 }
298+
299+ injectGiphy ( 'latest' ) ;
246300} ) ;
247301window . addEventListener ( 'DOMContentLoaded' , function ( ) {
248302 var button = document . querySelector ( '#emoji-button' ) ;
0 commit comments