Skip to content

Commit b1da67a

Browse files
committed
yarn build
1 parent 9ab3e07 commit b1da67a

File tree

3 files changed

+140
-5
lines changed

3 files changed

+140
-5
lines changed

dist/component/CharacterMap.js

+118-1
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,23 @@ var CharacterMap = function (_React$Component) {
4040

4141
var _this = _possibleConstructorReturn(this, (CharacterMap.__proto__ || Object.getPrototypeOf(CharacterMap)).call(this, props));
4242

43+
try {
44+
_this.paletteCache = JSON.parse(localStorage.getItem('tenupISCcharPalette'));
45+
_this.paletteCache = _this.paletteCache.length ? _this.paletteCache : [];
46+
} catch (error) {
47+
_this.paletteCache = [];
48+
}
49+
50+
_this.secondaryPaletteCache = [];
51+
_this.leastUsedCharFromPalette = false;
52+
_this.dirtyPalette = false;
4353
_this.state = {
4454
active: 0,
4555
search: '',
4656
categoryList: '',
4757
charList: '',
48-
fullCharList: ''
58+
fullCharList: '',
59+
charPalette: _this.paletteCache
4960
};
5061
_this.resultsCache = [];
5162
_this.handleSearchChange = _this.handleSearchChange.bind(_this);
@@ -125,9 +136,97 @@ var CharacterMap = function (_React$Component) {
125136
key: 'charClickHandler',
126137
value: function charClickHandler(e, char) {
127138
e.preventDefault();
139+
this.setPalette(char);
128140
return this.props.onSelect(char, e.target);
129141
}
130142

143+
/**
144+
* Sets the charPalette state.
145+
*
146+
* @param {object} char The character object
147+
*/
148+
149+
}, {
150+
key: 'setPalette',
151+
value: function setPalette(char) {
152+
var paletteMaxSize = 5;
153+
var charAtIndex = this.paletteCache.findIndex(function (p) {
154+
return p.hex === char.hex;
155+
});
156+
157+
/* If the primary palette cache is not fully filled OR if the character is already
158+
* present in primary, then add the character to it.
159+
*/
160+
if (this.paletteCache.length < paletteMaxSize || -1 !== charAtIndex) {
161+
this.paletteCache = this.addToPalette(char, this.paletteCache);
162+
/* Else add it to the secondary cache. */
163+
} else if (-1 === charAtIndex) {
164+
this.secondaryPaletteCache = this.addToPalette(char, this.secondaryPaletteCache);
165+
}
166+
167+
/* If the primary cache is fully filled, then save the least used
168+
* character from the cache for future reference.
169+
*/
170+
if (this.paletteCache.length === paletteMaxSize) {
171+
this.leastUsedCharFromPalette = this.paletteCache[paletteMaxSize - 1];
172+
}
173+
174+
/*
175+
* Sort the palettes in descending order of the count.
176+
*/
177+
this.paletteCache.sort(function (a, b) {
178+
return b.count - a.count;
179+
});
180+
this.secondaryPaletteCache.sort(function (a, b) {
181+
return b.count - a.count;
182+
});
183+
184+
if (this.secondaryPaletteCache.length > 0) {
185+
/* If the count of the max used character in secondary is more than
186+
* the count of the least used character in the primary, then remove
187+
* that character from secondary and replace the least used character
188+
* from primary with it.
189+
*/
190+
if (this.secondaryPaletteCache[0].count > this.paletteCache[paletteMaxSize - 1].count) {
191+
var maxCountCharInSecondaryPalette = this.secondaryPaletteCache.shift();
192+
this.paletteCache[paletteMaxSize - 1] = maxCountCharInSecondaryPalette;
193+
}
194+
}
195+
196+
localStorage.setItem('tenupISCcharPalette', JSON.stringify(this.paletteCache));
197+
this.setState({ 'charPalette': this.paletteCache });
198+
}
199+
200+
/**
201+
* Adds a character to the character palette.
202+
*
203+
* @param {object} char The character object.
204+
* @param {array} palette The char palette array.
205+
* @returns {array}
206+
*/
207+
208+
}, {
209+
key: 'addToPalette',
210+
value: function addToPalette(char, palette) {
211+
var charAtIndex = palette.findIndex(function (p) {
212+
return p.hex === char.hex;
213+
});
214+
215+
if (charAtIndex !== -1) {
216+
++palette[charAtIndex].count;
217+
} else {
218+
palette.push({
219+
'char': char.char,
220+
'entity': char.entity,
221+
'hex': char.hex,
222+
'name': char.name,
223+
'count': 1
224+
});
225+
}
226+
227+
return palette;
228+
}
229+
131230
/**
132231
* Perform the character search.
133232
*
@@ -298,6 +397,10 @@ var CharacterMap = function (_React$Component) {
298397
var filterLabelText = this.props.filterLabelText || 'Filter';
299398
var categoriesLabelText = this.props.categoriesLabelText || 'Categories';
300399
var characterListLabelText = this.props.characterListLabelText || 'Character List';
400+
var mostUsedPaletteText = this.props.mostUsedPaletteText || 'Most used';
401+
402+
var _charListFromCharacte3 = this.charListFromCharacters({ 'Palette': this.paletteCache }, 0),
403+
charPalette = _charListFromCharacte3.charList;
301404

302405
return _react2.default.createElement(
303406
'div',
@@ -320,6 +423,20 @@ var CharacterMap = function (_React$Component) {
320423
ref: this.bindInputRef
321424
})
322425
),
426+
this.props.mostUsedPalette && this.paletteCache.length && _react2.default.createElement(
427+
'div',
428+
{ className: 'charMap--last-used-palette-wrapper' },
429+
_react2.default.createElement(
430+
'label',
431+
null,
432+
mostUsedPaletteText + ': '
433+
),
434+
_react2.default.createElement(
435+
'ul',
436+
{ className: 'charMap--last-used-palette', 'aria-label': mostUsedPaletteText },
437+
charPalette
438+
)
439+
),
323440
'' === search && _react2.default.createElement(
324441
'ul',
325442
{ className: 'charMap--category-menu', 'aria-label': categoriesLabelText },

dist/index.js

+9-4
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,19 @@
1-
'use strict';
1+
"use strict";
22

33
Object.defineProperty(exports, "__esModule", {
44
value: true
55
});
6-
exports.CharacterMap = undefined;
6+
exports.deleteCharPaletteData = exports.CharacterMap = undefined;
77

8-
var _CharacterMap = require('./component/CharacterMap');
8+
var _CharacterMap = require("./component/CharacterMap");
99

1010
var _CharacterMap2 = _interopRequireDefault(_CharacterMap);
1111

12+
var _utilities = require("./utilities");
13+
14+
var _utilities2 = _interopRequireDefault(_utilities);
15+
1216
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
1317

14-
exports.CharacterMap = _CharacterMap2.default;
18+
exports.CharacterMap = _CharacterMap2.default;
19+
exports.deleteCharPaletteData = _utilities2.default;

dist/utilities.js

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
'use strict';
2+
3+
Object.defineProperty(exports, "__esModule", {
4+
value: true
5+
});
6+
exports.default = deleteCharPaletteData;
7+
/**
8+
* Utility function to programatically delete character palette
9+
* local storagee data.
10+
*/
11+
function deleteCharPaletteData() {
12+
localStorage.removeItem('tenupISCcharPalette');
13+
}

0 commit comments

Comments
 (0)