Skip to content
This repository was archived by the owner on Nov 16, 2024. It is now read-only.

Add ability to cache user color choices based on config #30

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
const Picker = require('./picker');
const { markerIcon, textIcon } = require('./icons');
const { getDefaultColorCache, handleCSSVariables } = require('./picker/utils/main');
const {getColorsFromLocalStorage} = require("./localStorageService");
require('./index.css').toString();

/**
Expand Down Expand Up @@ -110,7 +111,9 @@ class Color {
},
hasCustomPicker: this.hasCustomPicker,
defaultColor: this.config.defaultColor,
colorCollections: this.config.colorCollections,
allowUserCachedColors: this.config.allowUserCachedColors ?? false,
numberOfUserCachedColors: this.config.numberOfUserCachedColors ?? 0,
colorCollections: this.config.allowUserCachedColors ? (this.config.colorCollections ?? []).concat(getColorsFromLocalStorage()) : this.config.colorCollections,
type: this.pluginType
});
}
Expand Down
36 changes: 36 additions & 0 deletions src/localStorageService/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
const localStorageFontKey = 'editorFontColor';

/**
*
* @returns {string[]}
* Get list of colors stored in local storage
*/
export const getColorsFromLocalStorage = () => {
const colorsAsString = localStorage.getItem(localStorageFontKey);
try {
if (colorsAsString) {
return JSON.parse(colorsAsString);
}
return [];
} catch (e) {
return [];
}
}

/**
*
* @param color {string}
* @param maxCacheSize {number}
* Save the color to local storage array of saved colours.
* We only store up to 10 entries at a time
*/
export const setColorInLocalStorage = (color, maxCacheSize) => {
const currentLocalStorageColors = getColorsFromLocalStorage();
if (currentLocalStorageColors.length === maxCacheSize) {
currentLocalStorageColors.pop();
}
if (!currentLocalStorageColors.includes(color)) {
currentLocalStorageColors.unshift(color);
}
localStorage.setItem(localStorageFontKey, JSON.stringify(currentLocalStorageColors));
}
7 changes: 6 additions & 1 deletion src/picker/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
CONVERTER_BTN,
CONVERTER_PANEL,
} from './utils/main';
import {setColorInLocalStorage} from "../localStorageService";
const ColorCollections = ['#ff1300','#EC7878','#9C27B0','#673AB7','#3F51B5','#0070FF','#03A9F4','#00BCD4','#4CAF50','#8BC34A','#CDDC39','#FFE500','#FFBF00','#FF9800','#795548','#9E9E9E','#5A5A5A','#FFF'];
class ColorPlugin extends HTMLElement {

Expand All @@ -23,7 +24,8 @@ class ColorPlugin extends HTMLElement {
this.pluginType = options.type;
this.hasCustomPicker = options.hasCustomPicker;
this.customColor = getCustomColorCache(this.pluginType);

this.allowUserCachedColors = options.allowUserCachedColors
this.numberOfUserCachedColors = options.numberOfUserCachedColors
shadowRoot.innerHTML = `
<style>
:host{
Expand Down Expand Up @@ -238,6 +240,9 @@ class ColorPlugin extends HTMLElement {
isCustomPickerPseudoClick = true;
customPicker.click();
}, 30))
pickerInput.addEventListener('change', (ev) => {
setColorInLocalStorage(ev.target.value, this.numberOfUserCachedColors)
})
document.body.appendChild(pickerInput);
setTimeout(() => {
pickerInput.focus();
Expand Down