From 6de516295c3b0dd8028647606ce076121c3f3c64 Mon Sep 17 00:00:00 2001 From: "Aka, but nice" <120267628+akabutnicer@users.noreply.github.com> Date: Wed, 21 Feb 2024 10:07:49 -0500 Subject: [PATCH 1/5] Use `indexedDB` for persistant data With the regular localStorage implementation, people can access that very data and manipulate it to their likings. While this isn't necessarily a bad thing, it's safer for indexedDB to be used so its more private, plus, it doesn't mess with the user's already existing localStorage data. Regards. --- packages/emoji-mart/src/helpers/store.ts | 59 ++++++++++++++++++++---- 1 file changed, 51 insertions(+), 8 deletions(-) diff --git a/packages/emoji-mart/src/helpers/store.ts b/packages/emoji-mart/src/helpers/store.ts index a689819bb..258a828dc 100644 --- a/packages/emoji-mart/src/helpers/store.ts +++ b/packages/emoji-mart/src/helpers/store.ts @@ -1,17 +1,60 @@ +function getIDB() { + try { + if (typeof indexedDB !== 'undefined') { + return indexedDB; + } + if (typeof webkitIndexedDB !== 'undefined') { + return webkitIndexedDB; + } + if (typeof mozIndexedDB !== 'undefined') { + return mozIndexedDB; + } + if (typeof OIndexedDB !== 'undefined') { + return OIndexedDB; + } + if (typeof msIndexedDB !== 'undefined') { + return msIndexedDB; + } + } catch (e) { + return; + } +} + +var idb = getIDB(); + +function getStore(callback, error) { + const db = idb.open("emoji-mart-db"); + db.onupgradeneeded = function () { + db.result.createObjectStore("emoji-store"); + }; + db.onsuccess = ({ result }) => { + const store = result + .transaction("emoji-store", "readwrite") + .objectStore("emoji-store"); + callback(store); + }; + db.onerror = error; +} +function runStoreCommand(command: string, args, success) { + getStore((store) => { + const request = store[command](...args); + request.onsuccess = success; + request.onerror = function () { + throw "Error running emoji-mart command: " + command; + }; + }); +} function set(key: string, value: string) { try { - window.localStorage[`emoji-mart.${key}`] = JSON.stringify(value) + runStoreCommand("put", value, key); } catch (error) {} } - function get(key: string): any { try { - const value = window.localStorage[`emoji-mart.${key}`] - - if (value) { - return JSON.parse(value) - } + return runStoreCommand("get", key, function ({ result }) { + return result; + }); } catch (error) {} } -export default { set, get } +export default { set, get }; From af12c86600a94122cf34e915f36fb77ebbc2cde4 Mon Sep 17 00:00:00 2001 From: "Aka, but nice" <120267628+akabutnicer@users.noreply.github.com> Date: Wed, 21 Feb 2024 11:38:57 -0500 Subject: [PATCH 2/5] Update store.ts --- packages/emoji-mart/src/helpers/store.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/emoji-mart/src/helpers/store.ts b/packages/emoji-mart/src/helpers/store.ts index 258a828dc..8069e76c7 100644 --- a/packages/emoji-mart/src/helpers/store.ts +++ b/packages/emoji-mart/src/helpers/store.ts @@ -46,12 +46,12 @@ function runStoreCommand(command: string, args, success) { } function set(key: string, value: string) { try { - runStoreCommand("put", value, key); + runStoreCommand("put", value, `emoji-mart.${key}`); } catch (error) {} } function get(key: string): any { try { - return runStoreCommand("get", key, function ({ result }) { + return runStoreCommand("get", `emoji-mart.${key}`, function ({ result }) { return result; }); } catch (error) {} From fbe29f14faf11ce72f528f6c9e85be91d0a69c3f Mon Sep 17 00:00:00 2001 From: "Aka, but nice" <120267628+akabutnicer@users.noreply.github.com> Date: Wed, 21 Feb 2024 11:48:50 -0500 Subject: [PATCH 3/5] Update store.ts From 9f7a99dc91fcce467644f2b8f7dbf9749e28b2f2 Mon Sep 17 00:00:00 2001 From: "Aka, but nice" <120267628+akabutnicer@users.noreply.github.com> Date: Wed, 21 Feb 2024 11:50:01 -0500 Subject: [PATCH 4/5] Update store.ts --- packages/emoji-mart/src/helpers/store.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/emoji-mart/src/helpers/store.ts b/packages/emoji-mart/src/helpers/store.ts index 8069e76c7..436b364eb 100644 --- a/packages/emoji-mart/src/helpers/store.ts +++ b/packages/emoji-mart/src/helpers/store.ts @@ -46,13 +46,13 @@ function runStoreCommand(command: string, args, success) { } function set(key: string, value: string) { try { - runStoreCommand("put", value, `emoji-mart.${key}`); + runStoreCommand("put", JSON.stringify(value, null, 4), `emoji-mart.${key}`); } catch (error) {} } function get(key: string): any { try { return runStoreCommand("get", `emoji-mart.${key}`, function ({ result }) { - return result; + return JSON.parse(result); }); } catch (error) {} } From 3b0d52a6213341b6b1539c56dd3aad912c7bb7bd Mon Sep 17 00:00:00 2001 From: "Aka, but nice" <120267628+akabutnicer@users.noreply.github.com> Date: Wed, 21 Feb 2024 11:55:20 -0500 Subject: [PATCH 5/5] Update store.ts --- packages/emoji-mart/src/helpers/store.ts | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/packages/emoji-mart/src/helpers/store.ts b/packages/emoji-mart/src/helpers/store.ts index 436b364eb..fb28059b6 100644 --- a/packages/emoji-mart/src/helpers/store.ts +++ b/packages/emoji-mart/src/helpers/store.ts @@ -15,9 +15,11 @@ function getIDB() { if (typeof msIndexedDB !== 'undefined') { return msIndexedDB; } + return; } catch (e) { return; } + } var idb = getIDB(); @@ -27,7 +29,7 @@ function getStore(callback, error) { db.onupgradeneeded = function () { db.result.createObjectStore("emoji-store"); }; - db.onsuccess = ({ result }) => { + db.onsuccess = ({ target: { result } }) => { const store = result .transaction("emoji-store", "readwrite") .objectStore("emoji-store"); @@ -51,7 +53,7 @@ function set(key: string, value: string) { } function get(key: string): any { try { - return runStoreCommand("get", `emoji-mart.${key}`, function ({ result }) { + return runStoreCommand("get", `emoji-mart.${key}`, function ({ target: { result } }) { return JSON.parse(result); }); } catch (error) {}