react-disqus-comments
Version:
React Disqus thread component
216 lines (173 loc) • 9.04 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };
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);
var _propTypes = require('prop-types');
var _propTypes2 = _interopRequireDefault(_propTypes);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return 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; }
function _objectWithoutProperties(obj, keys) { var target = {}; for (var i in obj) { if (keys.indexOf(i) >= 0) continue; if (!Object.prototype.hasOwnProperty.call(obj, i)) continue; target[i] = obj[i]; } return target; }
var DISQUS_CONFIG = ['shortname', 'identifier', 'title', 'url', 'category_id', 'onNewComment', 'language'];
var __disqusAdded = false;
function copyProps(context, props) {
var onNewComment = props.onNewComment,
language = props.language,
rest = _objectWithoutProperties(props, ['onNewComment', 'language']);
for (var prop in rest) {
context.page[prop] = rest[prop];
}
// Setting the language - if none is provided, the default one is used
context.language = language;
if (onNewComment) {
context.callbacks = {
onNewComment: [onNewComment]
};
}
}
var DisqusThread = function (_React$Component) {
_inherits(DisqusThread, _React$Component);
function DisqusThread() {
_classCallCheck(this, DisqusThread);
return _possibleConstructorReturn(this, (DisqusThread.__proto__ || Object.getPrototypeOf(DisqusThread)).apply(this, arguments));
}
_createClass(DisqusThread, [{
key: 'componentDidMount',
value: function componentDidMount() {
this.loadDisqus();
}
}, {
key: 'componentDidUpdate',
value: function componentDidUpdate() {
this.loadDisqus();
}
}, {
key: 'shouldComponentUpdate',
value: function shouldComponentUpdate(nextProps, nextState) {
return nextProps.identifier !== this.props.identifier;
}
}, {
key: 'render',
value: function render() {
var _this2 = this;
var props = Object.keys(this.props).reduce(function (memo, key) {
return DISQUS_CONFIG.some(function (config) {
return config === key;
}) ? memo : _extends({}, memo, _defineProperty({}, key, _this2.props[key]));
}, {});
return _react2.default.createElement(
'div',
props,
_react2.default.createElement('div', { id: 'disqus_thread' })
);
}
}, {
key: 'addDisqusScript',
value: function addDisqusScript() {
if (__disqusAdded) {
return;
}
var child = this.disqus = document.createElement('script');
var parent = document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0];
child.async = true;
child.type = 'text/javascript';
child.src = '//' + this.props.shortname + '.disqus.com/embed.js';
parent.appendChild(child);
__disqusAdded = true;
}
}, {
key: 'loadDisqus',
value: function loadDisqus() {
var _this3 = this;
var props = {};
// Extract Disqus props that were supplied to this component
DISQUS_CONFIG.forEach(function (prop) {
// prop "shortname" is only necessary for loading the script, not for the config itself
if (prop !== 'shortname' && !!_this3.props[prop]) {
props[prop] = _this3.props[prop];
}
});
// If Disqus has already been added, reset it
if (typeof DISQUS !== 'undefined') {
DISQUS.reset({
reload: true,
config: function config() {
copyProps(this, props);
// Disqus needs hashbang URL, see https://help.disqus.com/customer/portal/articles/472107
this.page.url = this.page.url.replace(/#/, '') + '#!newthread';
}
});
} else {
// Otherwise add Disqus to the page
window.disqus_config = function () {
copyProps(this, props);
};
this.addDisqusScript();
}
}
}]);
return DisqusThread;
}(_react2.default.Component);
DisqusThread.displayName = 'DisqusThread';
DisqusThread.propTypes = {
id: _propTypes2.default.string,
/**
* `shortname` tells the Disqus service your forum's shortname,
* which is the unique identifier for your website as registered
* on Disqus. If undefined , the Disqus embed will not load.
*/
shortname: _propTypes2.default.string.isRequired,
/**
* `identifier` tells the Disqus service how to identify the
* current page. When the Disqus embed is loaded, the identifier
* is used to look up the correct thread. If disqus_identifier
* is undefined, the page's URL will be used. The URL can be
* unreliable, such as when renaming an article slug or changing
* domains, so we recommend using your own unique way of
* identifying a thread.
*/
identifier: _propTypes2.default.string,
/**
* `title` tells the Disqus service the title of the current page.
* This is used when creating the thread on Disqus for the first time.
* If undefined, Disqus will use the <title> attribute of the page.
* If that attribute could not be used, Disqus will use the URL of the page.
*/
title: _propTypes2.default.string,
/**
* `url` tells the Disqus service the URL of the current page.
* If undefined, Disqus will take the window.location.href.
* This URL is used to look up or create a thread if disqus_identifier
* is undefined. In addition, this URL is always saved when a thread is
* being created so that Disqus knows what page a thread belongs to.
*/
url: _propTypes2.default.string,
/**
* `category_id` tells the Disqus service the category to be used for
* the current page. This is used when creating the thread on Disqus
* for the first time.
*/
category_id: _propTypes2.default.string,
/**
* `onNewComment` function accepts one parameter `comment` which is a
* JavaScript object with comment `id` and `text`. This allows you to track
* user comments and replies and run a script after a comment is posted.
*/
onNewComment: _propTypes2.default.func,
/**
* `language` tells the Disqus service which language should be used.
* Please refer to https://www.transifex.com/disqus/disqus/ on which languages can be used
* If undefined, English is used as default one
*/
language: _propTypes2.default.string
};
DisqusThread.defaultProps = {
url: typeof window === 'undefined' ? null : window.location.href
};
exports.default = DisqusThread;