/
giphy-search.js
147 lines (120 loc) · 3.91 KB
/
giphy-search.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
'use strict';
var AbstractExtension = require('../../core/abstract-extension');
var Figure = require('../../figure');
var Loader = require('../../loader');
var I18n = require('../../i18n');
var Utils = require('../../utils');
/**
* @typedef {{
* apiKey: string,
* }} */
var GiphySearchParamsDef;
/**
* @typedef {{
* data: {
* image_original_url: string,
* }
* }}
*/
var GiphyJsonResponseDef;
/**
* GiphySearch main.
* @param {GiphySearchParamsDef=} opt_params Optional params.
* Default:
* {
* apiKey: null,
* }
* @extends {../../core/abstract-extension}
* @constructor
*/
var GiphySearch = function(editor, opt_params) {
var params = Utils.extend({
apiKey: null,
}, opt_params);
/** @type {string} */
this.apiKey_ = params.apiKey;
this.registerRegexes_(editor);
};
GiphySearch.prototype = Object.create(AbstractExtension.prototype);
module.exports = GiphySearch;
/**
* String name for the component class.
* @type {string}
*/
GiphySearch.CLASS_NAME = 'GiphySearch';
// Register GiphyComponent as Figure component to allow legacy
// GiphyComponent to still work by initializing a new Figure :-).
Loader.register('GiphyComponent', Figure);
/**
* Regex strings list that for matching Giphy search terms.
* @type {string}
*/
GiphySearch.GIPHY_SEARCH_REGEX = '^\\+giphy\\s(.+[a-zA-Z])$';
/**
* Giphy endpoint for random search.
* Ref: https://github.com/Giphy/GiphyAPI
* @type {string}
*/
GiphySearch.GIPHY_RANDOM_ENDPOINT = 'https://api.giphy.com/v1/gifs/random?';
/**
* Handles onInstall when the GiphySearch module installed in an editor.
* @param {../../editor} editor Instance of the editor that installed the module.
* @param {GiphySearchParamsDef=} opt_params Optional params.
*/
GiphySearch.onInstall = function(editor, opt_params) {
if (!opt_params || !opt_params.apiKey) {
throw new Error('apiKey parameter is required for GiphySearch extension');
}
};
/**
* Registers regular experessions to create giphy component from if matched.
* @param {../../editor} editor The editor to register regexes with.
* @private
*/
GiphySearch.prototype.registerRegexes_ = function(editor) {
editor.registerRegex(
I18n.get('regex.giphy') || GiphySearch.GIPHY_SEARCH_REGEX,
this.handleMatchedRegex.bind(this));
};
/**
* Creates a figure component from a link.
* @param {../../paragraph} matchedComponent Component that matched registered regex.
* @param {function(Array<../../defs.OperationDef>)} opsCallback Callback to send list of operations to exectue.
*/
GiphySearch.prototype.handleMatchedRegex = function(
matchedComponent, opsCallback) {
var searchTextSplit = matchedComponent.text.split(/\s/).slice(1);
var captionText = searchTextSplit.join(' ');
var giphyQuery = searchTextSplit.join('+');
var atIndex = matchedComponent.getIndexInSection();
var ops = [];
// Call Giphy Random Endpoint.
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (xhttp.readyState == 4 && xhttp.status == 200) {
var src;
/* jshint ignore:start */
// Get the image url from the random search response.
var json = /** @type {GiphyJsonResponseDef} */ (
JSON.parse(xhttp.responseText));
src = json['data']['image_original_url'];
/* jshint ignore:end */
// If result is found for the query, create a component.
if (src) {
var figure = new Figure({
src: src,
caption: captionText,
});
figure.section = matchedComponent.section;
// Delete current matched component with its text.
Utils.arrays.extend(ops, matchedComponent.getDeleteOps(atIndex));
// Add the new component created from the text.
Utils.arrays.extend(ops, figure.getInsertOps(atIndex));
opsCallback(ops);
}
}
};
xhttp.open('GET', GiphySearch.GIPHY_RANDOM_ENDPOINT +
'api_key=' + this.apiKey_ + '&' + giphyQuery, true);
xhttp.send();
};