@bookbox/view-html
Version:
Bookbox view for html
46 lines (45 loc) • 1.68 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.THEME_STORE_KEY = void 0;
exports.setTheme = setTheme;
exports.setSavedTheme = setSavedTheme;
exports.THEME_STORE_KEY = 'book-box-theme';
function getBookboxNodes(options) {
const { element, selector = '.book-box' } = options !== null && options !== void 0 ? options : {};
if (element)
return [element];
return Array.from(document.querySelectorAll(selector));
}
const themeClassNames = {
light: '',
dark: 'book-box_theme-dark',
sepia: 'book-box_theme-sepia',
};
function setTheme(options) {
const { theme, storageKey } = options;
for (const node of getBookboxNodes(options)) {
for (const t of Object.keys(themeClassNames)) {
const className = themeClassNames[t];
if (t === theme) {
if (className)
node.classList.add(className);
}
else if (className !== '') {
node.classList.remove(className);
}
}
}
localStorage.setItem(storageKey !== null && storageKey !== void 0 ? storageKey : exports.THEME_STORE_KEY, theme);
}
/**
* set theme from localStorage
*/
function setSavedTheme(options) {
const { storageKey } = options !== null && options !== void 0 ? options : {};
const savedTheme = localStorage.getItem(storageKey !== null && storageKey !== void 0 ? storageKey : exports.THEME_STORE_KEY);
if (!savedTheme)
return;
if (!themeClassNames.hasOwnProperty(savedTheme))
return;
setTheme(Object.assign({ theme: savedTheme }, (options !== null && options !== void 0 ? options : {})));
}