Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Giphy options for modularity with sensible, performant defaults #1437

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 22 additions & 13 deletions plugins/giphy/trumbowyg.giphy.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
// Customizes Giphy plugin. A PR has been submitted:
// https://github.com/Alex-D/Trumbowyg/pull/1437
// Revert to upstream version once this PR is merged.
(function ($) {
'use strict';

Expand Down Expand Up @@ -71,20 +74,18 @@

var html = response.data
.filter(function (gifData) {
// jshint camelcase:false
var downsized = gifData.images.downsized || gifData.images.downsized_medium;
// jshint camelcase:true
return !!downsized.url;
var image = gifData.images[trumbowyg.o.plugins.giphy.pickerRendition];
return !!image[trumbowyg.o.plugins.giphy.pickerRenditionUrlAttribute];
})
.map(function (gifData) {
// jshint camelcase:false
var downsized = gifData.images.downsized || gifData.images.downsized_medium;
// jshint camelcase:true
var image = downsized,
imageRatio = image.height / image.width,
var image = gifData.images[trumbowyg.o.plugins.giphy.pickerRendition];
var selectionImage = gifData.images[trumbowyg.o.plugins.giphy.selectionRendition];
var imageRatio = image.height / image.width,
altText = gifData.title;

var imgHtml = '<img src=' + image.url + ' width="' + width + '" height="' + imageRatio * width + '" alt="' + altText + '" loading="lazy" />';
var url = image[trumbowyg.o.plugins.giphy.pickerRenditionUrlAttribute];
var selectionUrl = selectionImage[trumbowyg.o.plugins.giphy.selectionRenditionUrlAttribute];
var imgHtml = '<img src=' + url + ' width="' + width + '" height="' + imageRatio * width + '" alt="' + altText + '" data-selectionurl="' + selectionUrl + '" loading="lazy" />';
return '<div class="img-container">' + imgHtml + '</div>';
})
.join('')
Expand Down Expand Up @@ -117,7 +118,7 @@
});

$('img', $giphyModal).on('click', function () {
var src = $(this).attr('src'),
var src = $(this).data('selectionurl'),
alt = $(this).attr('alt');
trumbowyg.restoreRange();
trumbowyg.execCmd('insertImage', src, false, true);
Expand All @@ -134,11 +135,18 @@
});
}

// see: https://developers.giphy.com/explorer/
var defaultOptions = {
rating: 'g',
apiKey: null,
throttleDelay: 300,
noResultGifUrl: 'https://media.giphy.com/media/2Faz9FbRzmwxY0pZS/giphy.gif'
limit: 50,
bundle: 'low_bandwidth',
noResultGifUrl: 'https://media.giphy.com/media/2Faz9FbRzmwxY0pZS/giphy.gif',
pickerRendition: 'fixed_width_small', // see: https://developers.giphy.com/docs/optional-settings/#renditions-on-demand
pickerRenditionUrlAttribute: 'webp', // can be 'url' or 'mp4' or 'webp'
selectionRendition: 'original',
selectionRenditionUrlAttribute: 'url'
};

// Add dropdown with font sizes
Expand All @@ -157,7 +165,8 @@
throw new Error('You must set a Giphy API Key');
}

var BASE_URL = 'https://api.giphy.com/v1/gifs/search?api_key=' + trumbowyg.o.plugins.giphy.apiKey + '&rating=' + trumbowyg.o.plugins.giphy.rating,
var BASE_URL = 'https://api.giphy.com/v1/gifs/search?api_key=' + trumbowyg.o.plugins.giphy.apiKey + '&rating=' + trumbowyg.o.plugins.giphy.rating +
'&limit=' + trumbowyg.o.plugins.giphy.limit + '&bundle=' + trumbowyg.o.plugins.giphy.bundle,
DEFAULT_URL = BASE_URL.replace('/search', '/trending');
var previousAjaxCall = {abort: function () {}};
var prefix = trumbowyg.o.prefix;
Expand Down