From 5da0699db23f996cefcdf67232b92d6790bdda63 Mon Sep 17 00:00:00 2001 From: CrushCola Date: Fri, 16 Aug 2024 00:48:18 +0800 Subject: [PATCH] added custom color word support --- dist/TagCloud.d.ts | 13 ++-- dist/TagCloud.js | 140 +++++++++++++++++++++---------------------- dist/TagCloud.min.js | 2 +- examples/index.html | 17 +++++- src/index.js | 19 +++--- 5 files changed, 106 insertions(+), 85 deletions(-) diff --git a/dist/TagCloud.d.ts b/dist/TagCloud.d.ts index bd50e04..0598728 100644 --- a/dist/TagCloud.d.ts +++ b/dist/TagCloud.d.ts @@ -18,26 +18,31 @@ export interface TagCloudOptions { useHTML?: boolean; } +export interface TagEntity { + text: string; + color: string +} + export default function ( container: string, - texts: Array, + texts: Array | Array, options?: TagCloudOptions ): TagCloud; export default function ( container: Element, - texts: Array, + texts: Array | Array, options?: TagCloudOptions ): TagCloud; export default function ( container: [Element], - texts: Array, + texts: Array | Array, options?: TagCloudOptions ): TagCloud; export default function ( container: Array, - texts: Array, + texts: Array | Array, options?: TagCloudOptions ): Array | TagCloud; diff --git a/dist/TagCloud.js b/dist/TagCloud.js index 0ebcab0..d5a0aa1 100644 --- a/dist/TagCloud.js +++ b/dist/TagCloud.js @@ -9,6 +9,37 @@ (global = global || self, global.TagCloud = factory()); }(this, (function () { 'use strict'; + function _classCallCheck(a, n) { + if (!(a instanceof n)) throw new TypeError("Cannot call a class as a function"); + } + function _defineProperties(e, r) { + for (var t = 0; t < r.length; t++) { + var o = r[t]; + o.enumerable = o.enumerable || !1, o.configurable = !0, "value" in o && (o.writable = !0), Object.defineProperty(e, _toPropertyKey(o.key), o); + } + } + function _createClass(e, r, t) { + return r && _defineProperties(e.prototype, r), t && _defineProperties(e, t), Object.defineProperty(e, "prototype", { + writable: !1 + }), e; + } + function _defineProperty(e, r, t) { + return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { + value: t, + enumerable: !0, + configurable: !0, + writable: !0 + }) : e[r] = t, e; + } + function _extends() { + return _extends = Object.assign ? Object.assign.bind() : function (n) { + for (var e = 1; e < arguments.length; e++) { + var t = arguments[e]; + for (var r in t) ({}).hasOwnProperty.call(t, r) && (n[r] = t[r]); + } + return n; + }, _extends.apply(null, arguments); + } function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { @@ -30,69 +61,28 @@ } return e; } - function _classCallCheck(instance, Constructor) { - if (!(instance instanceof Constructor)) { - throw new TypeError("Cannot call a class as a 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, _toPropertyKey(descriptor.key), descriptor); - } - } - function _createClass(Constructor, protoProps, staticProps) { - if (protoProps) _defineProperties(Constructor.prototype, protoProps); - if (staticProps) _defineProperties(Constructor, staticProps); - Object.defineProperty(Constructor, "prototype", { - writable: false - }); - return Constructor; - } - function _defineProperty(obj, key, value) { - key = _toPropertyKey(key); - if (key in obj) { - Object.defineProperty(obj, key, { - value: value, - enumerable: true, - configurable: true, - writable: true - }); - } else { - obj[key] = value; - } - return obj; - } - function _extends() { - _extends = Object.assign ? Object.assign.bind() : 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 _toPrimitive(input, hint) { - if (typeof input !== "object" || input === null) return input; - var prim = input[Symbol.toPrimitive]; - if (prim !== undefined) { - var res = prim.call(input, hint || "default"); - if (typeof res !== "object") return res; + function _toPrimitive(t, r) { + if ("object" != typeof t || !t) return t; + var e = t[Symbol.toPrimitive]; + if (void 0 !== e) { + var i = e.call(t, r || "default"); + if ("object" != typeof i) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } - return (hint === "string" ? String : Number)(input); + return ("string" === r ? String : Number)(t); } - function _toPropertyKey(arg) { - var key = _toPrimitive(arg, "string"); - return typeof key === "symbol" ? key : String(key); + function _toPropertyKey(t) { + var i = _toPrimitive(t, "string"); + return "symbol" == typeof i ? i : i + ""; + } + function _typeof(o) { + "@babel/helpers - typeof"; + + return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { + return typeof o; + } : function (o) { + return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; + }, _typeof(o); } /** @@ -138,7 +128,7 @@ /* static method */ // all TagCloud list - _createClass(TagCloud, [{ + return _createClass(TagCloud, [{ key: "_createElment", value: /* instance property method */ // create elment @@ -156,8 +146,8 @@ // create texts self.items = []; - self.texts.forEach(function (text, index) { - var item = self._createTextItem(text, index); + self.texts.forEach(function (entity, index) { + var item = self._createTextItem(entity, index); $el.appendChild(item.el); self.items.push(item); }); @@ -165,10 +155,10 @@ self.$el = $el; } - // create a text + // create a entity }, { key: "_createTextItem", - value: function _createTextItem(text) { + value: function _createTextItem(entity) { var index = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 0; var self = this; var itemEl = document.createElement('span'); @@ -192,10 +182,16 @@ itemEl.style.OTransform = transform; itemEl.style.transform = transform; } + var _ref = _typeof(entity) === 'object' ? entity : { + text: entity + }, + text = _ref.text, + color = _ref.color; if (self.config.useHTML) { itemEl.innerHTML = text; } else { itemEl.innerText = text; + color && (itemEl.style.color = color); } return _objectSpread2({ el: itemEl @@ -295,7 +291,6 @@ self.mouseX = Math.abs(self.mouseX - self.mouseX0) < 1 ? self.mouseX0 : (self.mouseX + self.mouseX0) / 2; // reset distance between the mouse and rolling center x axis self.mouseY = Math.abs(self.mouseY - self.mouseY0) < 1 ? self.mouseY0 : (self.mouseY + self.mouseY0) / 2; // reset distance between the mouse and rolling center y axis } - var a = -(Math.min(Math.max(-self.mouseY, -self.size), self.size) / self.radius) * self.maxSpeed; var b = Math.min(Math.max(-self.mouseX, -self.size), self.size) / self.radius * self.maxSpeed; @@ -346,20 +341,26 @@ // params self.texts = texts || []; // judging and processing items based on texts - self.texts.forEach(function (text, index) { + self.texts.forEach(function (entity, index) { var item = self.items[index]; if (!item) { // if not had, then create - item = self._createTextItem(text, index); + item = self._createTextItem(entity, index); _extends(item, self._computePosition(index, true)); // random place self.$el.appendChild(item.el); self.items.push(item); } - // if had, replace text + // if had, replace entity + var _ref2 = _typeof(entity) === 'object' ? entity : { + text: entity + }, + text = _ref2.text, + color = _ref2.color; if (self.config.useHTML) { item.el.innerHTML = text; } else { item.el.innerText = text; + color && (item.el.style.color = color); } }); // remove redundant self.items @@ -415,7 +416,6 @@ } } }]); - return TagCloud; }(); TagCloud.list = []; // default config diff --git a/dist/TagCloud.min.js b/dist/TagCloud.min.js index d3dd3de..acf2843 100644 --- a/dist/TagCloud.min.js +++ b/dist/TagCloud.min.js @@ -3,4 +3,4 @@ * Copyright (c) 2016-2024 @ Cong Min * MIT License - https://github.com/mcc108/TagCloud */ -!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):(e=e||self).TagCloud=t()}(this,function(){"use strict";function t(t,e){var n,i=Object.keys(t);return Object.getOwnPropertySymbols&&(n=Object.getOwnPropertySymbols(t),e&&(n=n.filter(function(e){return Object.getOwnPropertyDescriptor(t,e).enumerable})),i.push.apply(i,n)),i}function a(i){for(var e=1;e{"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):(e=e||self).TagCloud=t()})(this,function(){function o(e,t){for(var n=0;n{if("object"!=typeof e||!e)return e;var n=e[Symbol.toPrimitive];if(void 0===n)return("string"===t?String:Number)(e);if("object"!=typeof(n=n.call(e,t||"default")))return n;throw new TypeError("@@toPrimitive must return a primitive value.")})(e,"string");return"symbol"==typeof e?e:e+""}function s(e){return(s="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol&&e!==Symbol.prototype?"symbol":typeof e})(e)}var l=(()=>{function r(){var e=0TagCloud.js diff --git a/src/index.js b/src/index.js index efe2efc..8657c07 100644 --- a/src/index.js +++ b/src/index.js @@ -83,8 +83,8 @@ class TagCloud { // create texts self.items = []; - self.texts.forEach((text, index) => { - const item = self._createTextItem(text, index); + self.texts.forEach((entity, index) => { + const item = self._createTextItem(entity, index); $el.appendChild(item.el); self.items.push(item); }); @@ -92,8 +92,8 @@ class TagCloud { self.$el = $el; } - // create a text - _createTextItem(text, index = 0) { + // create a entity + _createTextItem(entity, index = 0) { const self = this; const itemEl = document.createElement('span'); itemEl.className = self.config.itemClass; @@ -116,10 +116,12 @@ class TagCloud { itemEl.style.OTransform = transform; itemEl.style.transform = transform; } + const { text, color } = typeof entity === 'object' ? entity : { text: entity }; if (self.config.useHTML) { itemEl.innerHTML = text; } else { itemEl.innerText = text; + color && (itemEl.style.color = color); } return { el: itemEl, @@ -221,7 +223,6 @@ class TagCloud { a = -a; b = -b; } - if (Math.abs(a) <= 0.01 && Math.abs(b) <= 0.01) return; // pause // calculate offset @@ -271,19 +272,21 @@ class TagCloud { // params self.texts = texts || []; // judging and processing items based on texts - self.texts.forEach((text, index) => { + self.texts.forEach((entity, index) => { let item = self.items[index]; if (!item) { // if not had, then create - item = self._createTextItem(text, index); + item = self._createTextItem(entity, index); Object.assign(item, self._computePosition(index, true)); // random place self.$el.appendChild(item.el); self.items.push(item); } - // if had, replace text + // if had, replace entity + const { text, color } = typeof entity === 'object' ? entity : { text: entity }; if (self.config.useHTML) { item.el.innerHTML = text; } else { item.el.innerText = text; + color && (item.el.style.color = color); } }); // remove redundant self.items