Skip to content

Commit

Permalink
v2.2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
madox2 committed Dec 25, 2021
1 parent c7fc765 commit 2b5b319
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 25 deletions.
53 changes: 35 additions & 18 deletions lib/TagCloud.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"use strict";

function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); }

Object.defineProperty(exports, "__esModule", {
value: true
Expand All @@ -15,42 +15,55 @@ var _shuffleArray = _interopRequireDefault(require("shuffle-array"));

var _randomcolor = _interopRequireDefault(require("randomcolor"));

var _seedrandom = _interopRequireDefault(require("seedrandom"));

var _defaultRenderer = require("./defaultRenderer");

var _helpers = require("./helpers");

function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function _getRequireWildcardCache() { return cache; }; return cache; }
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }

function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { "default": obj }; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj["default"] = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { "default": obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj["default"] = obj; if (cache) { cache.set(obj, newObj); } return newObj; }

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }

function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }

function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }

function _iterableToArrayLimit(arr, i) { if (typeof Symbol === "undefined" || !(Symbol.iterator in Object(arr))) return; var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; }
function _iterableToArrayLimit(arr, i) { var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"]; if (_i == null) return; var _arr = []; var _n = true; var _d = false; var _s, _e; try { for (_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; }

function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }

function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); }

function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }

function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(n); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }

function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && Symbol.iterator in Object(iter)) return Array.from(iter); }
function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); }

function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); }

function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }

function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }

function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }

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; }

var handlersPropNames = ['onClick', 'onDoubleClick', 'onMouseMove', 'onMouseOver', 'onMouseOut'];
var cloudPropNames = ['tags', 'shuffle', 'renderer', 'maxSize', 'minSize', 'colorOptions', 'disableRandomColor', 'randomNumberGenerator'];
var cloudPropNames = ['tags', 'shuffle', 'renderer', 'maxSize', 'minSize', 'colorOptions', 'disableRandomColor', 'randomSeed', 'randomNumberGenerator'];

function getTagHashCode(tag) {
return tag.key + tag.value + tag.count;
}

function generateColor(tag, _ref) {
var disableRandomColor = _ref.disableRandomColor,
colorOptions = _ref.colorOptions;
colorOptions = _ref.colorOptions,
randomSeed = _ref.randomSeed;

if (tag.color) {
return tag.color;
Expand All @@ -60,7 +73,9 @@ function generateColor(tag, _ref) {
return undefined;
}

return (0, _randomcolor["default"])(colorOptions);
return (0, _randomcolor["default"])(_objectSpread({
seed: randomSeed && "".concat(randomSeed, ":").concat(getTagHashCode(tag))
}, colorOptions));
}

function withTagCloudHandlers(elem, tag, cloudHandlers) {
Expand All @@ -73,7 +88,7 @@ function withTagCloudHandlers(elem, tag, cloudHandlers) {

return acc;
}, {});
return _react["default"].cloneElement(elem, props);
return /*#__PURE__*/_react["default"].cloneElement(elem, props);
}

function renderTags(props, data) {
Expand All @@ -98,16 +113,19 @@ function renderTags(props, data) {
function randomize(props) {
var tags = props.tags,
shuffle = props.shuffle,
randomSeed = props.randomSeed,
randomNumberGenerator = props.randomNumberGenerator;
var data = tags.map(function (tag) {
var rng = randomSeed ? (0, _seedrandom["default"])(randomSeed) : randomNumberGenerator;
var copy = tags.slice();
var data = shuffle ? (0, _shuffleArray["default"])(copy, {
rng: rng
}) : copy;
return data.map(function (tag) {
return {
tag: tag,
color: generateColor(tag, props)
};
});
return shuffle ? (0, _shuffleArray["default"])(data, {
rng: randomNumberGenerator
}) : data;
}

function TagCloud(props) {
Expand All @@ -116,13 +134,11 @@ function TagCloud(props) {
data = _useState2[0],
setData = _useState2[1];

var tagsComparison = props.tags.map(function (t) {
return t.key || t.value;
}).join(':'); // randomize (color, shuffle) when tags or certain props change
var tagsComparison = props.tags.map(getTagHashCode).join(':'); // randomize (color, shuffle) when tags or certain props change

(0, _react.useEffect)(function () {
setData(randomize(props));
}, [props.colorOptions, props.shuffle, props.disableRandomColor, tagsComparison]);
}, [props.colorOptions, props.randomSeed, props.shuffle, props.disableRandomColor, tagsComparison]);
var other = (0, _helpers.omit)(props, [].concat(cloudPropNames, handlersPropNames));
return /*#__PURE__*/_react["default"].createElement("div", other, renderTags(props, data));
}
Expand All @@ -136,6 +152,7 @@ TagCloud.propTypes = {
disableRandomColor: _propTypes["default"].bool,
renderer: _propTypes["default"].func,
className: _propTypes["default"].string,
randomSeed: _propTypes["default"].any,
randomNumberGenerator: _propTypes["default"].func
};
TagCloud.defaultProps = {
Expand Down
10 changes: 6 additions & 4 deletions lib/defaultRenderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@ exports.defaultRenderer = void 0;

var _react = _interopRequireDefault(require("react"));

var _excluded = ["className", "style"];

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }

function _extends() { _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; }; return _extends.apply(this, arguments); }

function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; }
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }

function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }

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; }

Expand All @@ -25,12 +27,12 @@ var defaultRenderer = function defaultRenderer(tag, size, color) {
var _ref = tag.props || {},
className = _ref.className,
style = _ref.style,
props = _objectWithoutProperties(_ref, ["className", "style"]);
props = _objectWithoutProperties(_ref, _excluded);

var fontSize = size + 'px';
var key = tag.key || tag.value;

var tagStyle = _objectSpread({}, styles, {
var tagStyle = _objectSpread(_objectSpread({}, styles), {}, {
color: color,
fontSize: fontSize
}, style);
Expand Down
5 changes: 3 additions & 2 deletions lib/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.pick = pick;
exports.fontSizeConverter = void 0;
exports.keys = keys;
exports.omit = exports.fontSizeConverter = void 0;
exports.omit = void 0;
exports.pick = pick;

/**
* Computes appropriate font size of tag.
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "react-tagcloud",
"version": "2.1.1",
"version": "2.2.0",
"description": "Tag/word cloud component for react",
"keywords": [
"react-tagcloud",
Expand Down

0 comments on commit 2b5b319

Please sign in to comment.