last-draft-js-plugins
Version:
Last Draft draft-js-plugins
188 lines (164 loc) • 6.54 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = undefined;
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
var _react = require('react');
var _react2 = _interopRequireDefault(_react);
require('whatwg-fetch');
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; } /* eslint no-undef: 0 */
var _default = function (_Component) {
_inherits(_default, _Component);
function _default(props) {
_classCallCheck(this, _default);
var _this = _possibleConstructorReturn(this, (_default.__proto__ || Object.getPrototypeOf(_default)).call(this, props));
_this.state = {
gifs: [],
searchValue: '',
giphySearchUrl: 'http://api.giphy.com/v1/gifs/search?api_key=dc6zaTOxFJmzC',
giphyTrendingUrl: 'http://api.giphy.com/v1/gifs/trending?api_key=dc6zaTOxFJmzC'
};
_this.loadTrendingGifs();
return _this;
}
_createClass(_default, [{
key: 'loadTrendingGifs',
value: function loadTrendingGifs() {
var _this2 = this;
var giphyTrendingUrl = this.state.giphyTrendingUrl;
fetch(giphyTrendingUrl, {
method: 'get'
}).then(function (response) {
return response.json();
}).then(function (response) {
var gifs = response.data.map(function (g, i) {
return g.images;
});
_this2.setState({ gifs: gifs });
});
}
}, {
key: 'searchGifs',
value: function searchGifs() {
var _this3 = this;
var _state = this.state,
giphySearchUrl = _state.giphySearchUrl,
searchValue = _state.searchValue;
if (searchValue.length < 1) {
return;
}
var url = giphySearchUrl + '&q=' + searchValue.replace(' ', '+');
this.setState({ gifs: [] });
fetch(url, {
method: 'get'
}).then(function (response) {
return response.json();
}).then(function (response) {
var gifs = response.data.map(function (g, i) {
return g.images;
});
_this3.setState({ gifs: gifs });
});
}
}, {
key: 'onGiphySelect',
value: function onGiphySelect(gif) {
this.props.onSelected(gif);
}
}, {
key: 'onSearchChange',
value: function onSearchChange(event) {
var _this4 = this;
event.stopPropagation();
this.setState({ searchValue: event.target.value }, function () {
return _this4.searchGifs();
});
}
}, {
key: 'onKeyDown',
value: function onKeyDown(event) {
if (event.key === 'Escape') {
event.preventDefault();
this.reset();
}
}
}, {
key: 'reset',
value: function reset() {
this.setState({ searchValue: '' });
}
}, {
key: 'render',
value: function render() {
var _this5 = this;
var gifs = this.state.gifs;
var _props = this.props,
visible = _props.visible,
theme = _props.theme;
return _react2.default.createElement(
'div',
{ className: theme.gifPickerStyles.wrapper, visible: visible },
_react2.default.createElement(
'div',
{
className: theme.gifPickerStyles.closeWrapper,
onClick: this.props.closeModal
},
_react2.default.createElement(
'svg',
{ className: theme.gifPickerStyles.close, width: '24', height: '24', viewBox: '0 0 24 24' },
_react2.default.createElement(
'g',
{ fill: 'currentColor', fillRule: 'evenodd' },
_react2.default.createElement('path', { d: 'M16.95 5.636l1.414 1.414L7.05 18.364 5.636 16.95z' }),
_react2.default.createElement('path', { d: 'M16.95 18.364l1.414-1.414L7.05 5.636 5.636 7.05z' })
)
)
),
_react2.default.createElement('input', {
className: theme.gifPickerStyles.input,
name: 'giphy-search',
type: 'text',
autoCapitalize: 'none',
autoComplete: 'off',
autoCorrect: 'off',
onChange: this.onSearchChange.bind(this),
value: this.state.searchValue,
onKeyDown: this.onKeyDown.bind(this),
placeholder: 'Search for gifs' }),
_react2.default.createElement(
'div',
{ className: theme.gifPickerStyles.picker },
gifs.map(function (g, i) {
var gifUrl = g.fixed_width.url;
return _react2.default.createElement('img', { className: theme.gifPickerStyles.gif,
key: i,
src: gifUrl,
onClick: function onClick() {
_this5.onGiphySelect(g);
} });
})
)
);
}
}], [{
key: 'propTypes',
get: function get() {
return {
onSelected: _react2.default.PropTypes.func.isRequired,
visible: _react2.default.PropTypes.bool
};
}
}, {
key: 'defaultProps',
get: function get() {
return { visible: true };
}
}]);
return _default;
}(_react.Component);
exports.default = _default;