convert emoji toolbar to use safe local storage

This commit is contained in:
Régis Hanol 2015-10-13 10:52:42 +02:00
parent 7c369ab2b7
commit d863defd3b
2 changed files with 20 additions and 15 deletions

View File

@ -1,3 +1,8 @@
import KeyValueStore from "discourse/lib/key-value-store";
const keyValueStore = new KeyValueStore("discourse_emojis_");
const EMOJI_USAGE = "emojiUsage";
// note that these categories are copied from Slack // note that these categories are copied from Slack
// be careful, there are ~20 differences in synonyms, e.g. :boom: vs. :collision: // be careful, there are ~20 differences in synonyms, e.g. :boom: vs. :collision:
// a few Emoji are actually missing from the Slack categories as well (?), and were added // a few Emoji are actually missing from the Slack categories as well (?), and were added
@ -85,21 +90,17 @@ var initializeUngroupedIcons = function(){
} }
}; };
try { if (!keyValueStore.getObject(EMOJI_USAGE)) {
if (localStorage && !localStorage.emojiUsage) { localStorage.emojiUsage = "{}"; } keyValueStore.setObject({key: EMOJI_USAGE, value: {}});
} catch (e) {
/* localStorage can be disabled, or cookies disabled, do not crash script here
* TODO introduce a global wrapper for dealing with local storage
* */
} }
var trackEmojiUsage = function(title){ var trackEmojiUsage = function(title) {
var recent = JSON.parse(localStorage.emojiUsage); var recent = keyValueStore.getObject(EMOJI_USAGE);
if (!recent[title]) { recent[title] = { title: title, usage: 0 }; } if (!recent[title]) { recent[title] = { title: title, usage: 0 }; }
recent[title]["usage"]++; recent[title]["usage"]++;
localStorage.emojiUsage = JSON.stringify(recent); keyValueStore.setObject({key: EMOJI_USAGE, value: recent});
// clear the cache // clear the cache
recentlyUsedIcons = null; recentlyUsedIcons = null;
@ -108,7 +109,7 @@ var trackEmojiUsage = function(title){
var initializeRecentlyUsedIcons = function(){ var initializeRecentlyUsedIcons = function(){
recentlyUsedIcons = []; recentlyUsedIcons = [];
var usage = _.map(JSON.parse(localStorage.emojiUsage)); var usage = _.map(keyValueStore.getObject(EMOJI_USAGE));
usage.sort(function(a,b){ usage.sort(function(a,b){
if(a.usage > b.usage){ if(a.usage > b.usage){
return -1; return -1;
@ -195,8 +196,8 @@ var bindEvents = function(page, offset, options) {
}; };
var render = function(page, offset, options) { var render = function(page, offset, options) {
localStorage.emojiPage = page; keyValueStore.set({key: "emojiPage", value: page});
localStorage.emojiOffset = offset; keyValueStore.set({key: "emojiOffset", value: offset});
var toolbarItems = toolbar(page); var toolbarItems = toolbar(page);
var rows = [], row = []; var rows = [], row = [];
@ -238,8 +239,8 @@ var showSelector = function(options) {
if (Discourse.Mobile.mobileView) PER_ROW = 9; if (Discourse.Mobile.mobileView) PER_ROW = 9;
var page = parseInt(localStorage.emojiPage) || 0; var page = keyValueStore.getInt("emojiPage", 0);
var offset = parseInt(localStorage.emojiOffset) || 0; var offset = keyValueStore.getInt("emojiOffset", 0);
render(page, offset, options); render(page, offset, options);
$('body, textarea').on('keydown.emoji', function(e){ $('body, textarea').on('keydown.emoji', function(e){

View File

@ -43,6 +43,10 @@ KeyValueStore.prototype = {
safeLocalStorage[this.context + opts.key] = opts.value; safeLocalStorage[this.context + opts.key] = opts.value;
}, },
setObject(opts) {
this.set({ key: opts.key, value: JSON.stringify(opts.value) });
},
get(key) { get(key) {
if (!safeLocalStorage) { return null; } if (!safeLocalStorage) { return null; }
return safeLocalStorage[this.context + key]; return safeLocalStorage[this.context + key];
@ -59,7 +63,7 @@ KeyValueStore.prototype = {
getObject(key) { getObject(key) {
if (!safeLocalStorage) { return null; } if (!safeLocalStorage) { return null; }
try { return JSON.parse(safeLocalStorage[this.context + key]); } try { return JSON.parse(safeLocalStorage[this.context + key]); }
catch (e) {} catch (e) { }
} }
}; };