通用评论

helper.js 1.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. 'use strict';
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.isFunction = isFunction;
  6. exports.isUrl = isUrl;
  7. exports.arrayToObject = arrayToObject;
  8. exports.renderContent = renderContent;
  9. var _constant = require('./constant');
  10. var _emoji = require('./emoji');
  11. var _emoji2 = _interopRequireDefault(_emoji);
  12. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  13. function isFunction(functionToCheck) {
  14. return functionToCheck && {}.toString.call(functionToCheck) === '[object Function]';
  15. }
  16. function isUrl(userInput) {
  17. var regexp = /(http(s)?:\/\/.)?(www\.)?[-a-zA-Z0-9@:%._+~#=]{2,256}\.[a-z]{2,6}\b([-a-zA-Z0-9@:%_+.~#?&//=]*)/g;
  18. var res = userInput.match(regexp);
  19. if (res === null) return false;else return true;
  20. }
  21. /**
  22. * 将对象数组转换为对象
  23. * @param {array} array Array of Objects
  24. * @param {string} keyField string
  25. */
  26. function arrayToObject(array, keyField) {
  27. return array.reduce(function (obj, item) {
  28. obj[item[keyField]] = item;
  29. return obj;
  30. }, {});
  31. }
  32. /**
  33. * 渲染编辑器
  34. * [x] => <img src="x" />
  35. * @param {strig} content
  36. */
  37. function renderContent(content, onClick) {
  38. return content.replace(_constant.REGEXP, function (a, b) {
  39. var src = a.slice(1, -1);
  40. if (isUrl(src)) {
  41. return '<img src="' + src + '" alt="' + src + '" style="max-width: 300px" />';
  42. }
  43. var emojiObejct = arrayToObject(_emoji2.default, 'title');
  44. var value = emojiObejct[src].value;
  45. return '<img src="' + _emoji.prefixUrl + value + '.' + _emoji.ext + '" alt="' + value + '" />';
  46. });
  47. }
  48. //# sourceMappingURL=helper.js.map