From 2b5b319ed558b101053d5b7dab92c7da4a1f8832 Mon Sep 17 00:00:00 2001 From: Martin Bielik Date: Sat, 25 Dec 2021 16:18:46 +0100 Subject: [PATCH] v2.2.0 --- lib/TagCloud.js | 53 ++++++++++++++++++++++++++++-------------- lib/defaultRenderer.js | 10 ++++---- lib/helpers.js | 5 ++-- package.json | 2 +- 4 files changed, 45 insertions(+), 25 deletions(-) diff --git a/lib/TagCloud.js b/lib/TagCloud.js index c0148bd..3dc8c4c 100644 --- a/lib/TagCloud.js +++ b/lib/TagCloud.js @@ -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 @@ -15,13 +15,15 @@ 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 }; } @@ -29,7 +31,7 @@ function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArra 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; } @@ -37,20 +39,31 @@ function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableTo 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; @@ -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) { @@ -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) { @@ -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) { @@ -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)); } @@ -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 = { diff --git a/lib/defaultRenderer.js b/lib/defaultRenderer.js index a6b101b..59492c2 100644 --- a/lib/defaultRenderer.js +++ b/lib/defaultRenderer.js @@ -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; } @@ -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); diff --git a/lib/helpers.js b/lib/helpers.js index 1459d9f..52370ec 100644 --- a/lib/helpers.js +++ b/lib/helpers.js @@ -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. diff --git a/package.json b/package.json index 07ee2d7..745ba93 100644 --- a/package.json +++ b/package.json @@ -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",