Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add static mode #35

Merged
merged 2 commits into from
Oct 28, 2024
Merged
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
12 changes: 6 additions & 6 deletions src/modules/clock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ function updateProgressBar() {
}

async function updateTime() {
const time = store.getState('time') || getTime();
const time = store.get('time') || getTime();

if (store.getState('fade')) {
if (store.get('fade')) {
fadeOutQuote();
}

Expand All @@ -31,11 +31,11 @@ async function updateTime() {
updateFavicon(time);
}

if (store.getState('theme')?.startsWith('dynamic')) {
if (store.get('theme')?.startsWith('dynamic')) {
setDayParameters();
}

if (store.getState('theme')?.startsWith('photo')) {
if (store.get('theme')?.startsWith('photo')) {
setDynamicBackgroundPicture();
} else {
removeBackgroundImage();
Expand All @@ -54,8 +54,8 @@ async function updateTime() {
}

export function initClock() {
const testTime = store.getState('time');
const testQuote = store.getState('quote');
const testTime = store.get('time');
const testQuote = store.get('quote');
const isTest = !!(testTime || testQuote);

updateTime();
Expand Down
2 changes: 1 addition & 1 deletion src/modules/dynamic.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { getDayParameters, getDayProgress } from './dynamic';
vi.mock('../store', () => {
return {
store: {
getState: () => '',
get: () => '',
},
};
});
Expand Down
4 changes: 2 additions & 2 deletions src/modules/dynamic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export function getDayProgress() {
const seconds = now.getHours() * 3600 + now.getMinutes() * 60 + now.getSeconds();
const progress = (seconds * 100) / 86400;

return parseFloat(store.getState('progress') || progress.toFixed(2));
return parseFloat(store.get('progress') || progress.toFixed(2));
}

export function getDayParameters() {
Expand Down Expand Up @@ -47,7 +47,7 @@ export function getDayParameters() {
const period = progress < 50 ? 'am' : 'pm';

return {
scene: store.getState('scene') || scene,
scene: store.get('scene') || scene,
progress,
period,
segment,
Expand Down
2 changes: 1 addition & 1 deletion src/modules/fade.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export function initFadeMode() {
}

function toggleFadeMode() {
const isFadeMode = store.toggleState('fade');
const isFadeMode = store.toggle('fade');

if (!isFadeMode) {
document.getElementById('quote')?.classList.remove('fade-in', 'fade-out');
Expand Down
12 changes: 6 additions & 6 deletions src/modules/font.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,23 +38,23 @@ function createOption(value: string) {
}

export function initFont() {
const font = store.getState('font');
const font = store.get('font');
const fontSelect = document.querySelector<HTMLSelectElement>('#font-select');

FONTS.forEach((fontName) => {
fontSelect?.appendChild(createOption(fontName));
});

if (font !== 'default' && !FONTS.includes(font)) {
store.setState('custom-font', font);
store.set('custom-font', font);
}

const customFont = store.getState('custom-font');
const customFont = store.get('custom-font');
if (customFont && !FONTS.includes(customFont)) {
fontSelect?.appendChild(createOption(customFont));
}

store.setState('font', font, false);
store.set('font', font, false);
if (fontSelect) {
fontSelect.value = font;
}
Expand All @@ -74,7 +74,7 @@ function setFont() {
const root = document.querySelector<HTMLElement>(':root');

if (font) {
store.setState('font', font);
store.set('font', font);

if (font === 'default') {
root?.style.removeProperty(CSS_FONT_VARIABLE);
Expand All @@ -95,5 +95,5 @@ export function resetFont() {
if (fontSelect) {
fontSelect.value = 'default';
}
store.setState('font', 'default');
store.set('font', 'default');
}
2 changes: 2 additions & 0 deletions src/modules/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { initStatic } from './static';
import { initClock } from './clock';
import { initCopy } from './copy';
import { initFadeMode } from './fade';
Expand All @@ -12,6 +13,7 @@ import { initWorkMode } from './work';
import { initZenMode } from './zen';

const MODULES = [
initStatic,
initClock,
initCopy,
initFadeMode,
Expand Down
14 changes: 7 additions & 7 deletions src/modules/locales.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,11 @@ export function resolveLocale(locale = navigator.language): Locale | 'random' {
}

export function initLocale() {
const locale = store.getState('locale');
const locale = store.get('locale');
const localeSelect = document.querySelector<HTMLSelectElement>('#locale-select');

if (locale !== 'random') {
store.setState('last-locale', locale);
store.set('last-locale', locale);
}

translateStrings(locale);
Expand All @@ -61,12 +61,12 @@ export function initLocale() {
localeSelect?.addEventListener('change', (e) => {
const languageSelectValue = (e.target as HTMLInputElement).value;
const isRandomLocale = languageSelectValue === 'random';
const locale = isRandomLocale ? store.getState('last-locale') || 'en-US' : (languageSelectValue as Locale);
const locale = isRandomLocale ? store.get('last-locale') || 'en-US' : (languageSelectValue as Locale);
translateStrings(locale);
store.setState('locale', languageSelectValue);
store.set('locale', languageSelectValue);

if (!isRandomLocale) {
store.setState('last-locale', languageSelectValue);
store.set('last-locale', languageSelectValue);
updateQuote({ useIndex: true });
}
});
Expand All @@ -75,14 +75,14 @@ export function initLocale() {
export function getStrings(): Translations {
const localeSelect = document.querySelector<HTMLSelectElement>('#locale-select');
const resolvedLocale = resolveLocale(localeSelect?.value);
const lastLocale = store.getState('last-locale') as keyof typeof TRANSLATIONS;
const lastLocale = store.get('last-locale') as keyof typeof TRANSLATIONS;

return TRANSLATIONS[resolvedLocale === 'random' ? lastLocale || 'en-US' : resolvedLocale];
}

function translateStrings(locale = navigator.language) {
const time = getTime();
const lastLocale = store.getState('last-locale') as keyof typeof TRANSLATIONS;
const lastLocale = store.get('last-locale') as keyof typeof TRANSLATIONS;
const strings = getStrings();

document.documentElement.lang = locale === 'random' ? lastLocale?.substring(0, 2) || 'en' : locale.substring(0, 2);
Expand Down
18 changes: 9 additions & 9 deletions src/modules/quotes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ async function getQuotes(time: string, locale: Locale): Promise<Quote[]> {

let quotes = (await response.json()) as Quote[];

if (store.getState('work') && !store.getState('index')) {
if (store.get('work') && !store.get('index')) {
quotes = quotes.filter((q) => q.sfw !== 'nsfw');
}

Expand Down Expand Up @@ -63,8 +63,8 @@ async function getQuote(time: string, locale: Locale, useIndex: boolean = false)
}
}

if (store.getState('index')) {
const urlParamsIndex = parseInt(store.getState('index')!);
if (store.get('index')) {
const urlParamsIndex = parseInt(store.get('index')!);
if (!isNaN(urlParamsIndex) && quotes[urlParamsIndex]) {
quoteIndex = urlParamsIndex;
}
Expand All @@ -79,7 +79,7 @@ async function getQuote(time: string, locale: Locale, useIndex: boolean = false)
quote.fallback = true;
}

if (store.getState('quote')) {
if (store.get('quote')) {
quote.title = strings.title;
quote.author = strings.author;
}
Expand All @@ -88,14 +88,14 @@ async function getQuote(time: string, locale: Locale, useIndex: boolean = false)
}

export async function updateQuote({ time = getTime(), useIndex = false } = {}) {
const testQuote = store.getState('quote');
let locale = store.getState('locale') as Locale;
const testQuote = store.get('quote');
let locale = store.get('locale') as Locale;

if (!locale) {
return;
}

if (store.getState('locale') === 'random') {
if (store.get('locale') === 'random') {
locale = getRandomLocale();
}

Expand Down Expand Up @@ -130,13 +130,13 @@ export async function updateQuote({ time = getTime(), useIndex = false } = {}) {
blockquote.removeAttribute('data-fallback');
}

if (store.getState('fade')) {
if (store.get('fade')) {
fadeInQuote();
}

fitQuote();

if (store.getState('theme')?.includes('color')) {
if (store.get('theme')?.includes('color')) {
setTheme();
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/modules/screensaver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ function screensaver() {
}

export function initScreensaver() {
if (store.getState('screensaver')) {
if (store.get('screensaver')) {
startScreensaver();
}

Expand All @@ -44,7 +44,7 @@ export function startScreensaver() {
}

function toggleScreensaverMode() {
if (store.toggleState('screensaver')) {
if (store.toggle('screensaver')) {
startScreensaver();
return;
}
Expand All @@ -54,5 +54,5 @@ function toggleScreensaverMode() {

export function exitScreensaverMode() {
clearInterval(screensaverInterval);
store.setState('screensaver', false);
store.set('screensaver', false);
}
4 changes: 2 additions & 2 deletions src/modules/share.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ async function shareQuote() {
];

const url = new URL('https://literatureclock.netlify.app/');
const locale = store.getState('locale');
const theme = store.getState('theme');
const locale = store.get('locale');
const theme = store.get('theme');

if (locale) {
url.searchParams.append('locale', locale);
Expand Down
7 changes: 7 additions & 0 deletions src/modules/static.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { store } from '../store';

export function initStatic() {
if (store.get('static')) {
document.querySelector('footer')?.remove();
}
}
10 changes: 5 additions & 5 deletions src/modules/themes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ function getRandomThemeColor() {
}

export function initTheme() {
let [theme, variant = 'system'] = store.getState('theme').split('-');
let [theme, variant = 'system'] = store.get('theme').split('-');
const themeSelect = document.querySelector<HTMLSelectElement>('#theme-select');
const variantSelect = document.querySelector<HTMLSelectElement>('#variant-select');
const preferDarkThemes = window.matchMedia('(prefers-color-scheme: dark)');
Expand All @@ -41,12 +41,12 @@ export function initTheme() {
themeSelect?.addEventListener('change', () => setTheme());
variantSelect?.addEventListener('change', () => setTheme({ isVariantChange: true }));
preferDarkThemes.addEventListener('change', (e) => {
const [_, variant] = store.getState('theme').split('-');
const [_, variant] = store.get('theme').split('-');

if (variant === 'system') {
const [theme] = store.getState('theme').split('-');
const [theme] = store.get('theme').split('-');

store.setState('theme', `${theme}-system`);
store.set('theme', `${theme}-system`);
document.documentElement.dataset.theme = `${theme}-${e.matches ? 'dark' : 'light'}`;
}
});
Expand All @@ -68,7 +68,7 @@ export function setTheme({ isVariantChange = false } = {}) {
});
resetFont();
}
store.setState('theme', `${theme}-${variant}`);
store.set('theme', `${theme}-${variant}`);

if (theme === 'color') {
theme = getRandomThemeColor();
Expand Down
2 changes: 1 addition & 1 deletion src/modules/time.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { store } from '../store';

export function initTimeMode() {
document.getElementById('showtime')?.addEventListener('click', () => store.toggleState('showtime'));
document.getElementById('showtime')?.addEventListener('click', () => store.toggle('showtime'));
}
2 changes: 1 addition & 1 deletion src/modules/work.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export function initWorkMode() {

function toggleWorkMode() {
const quote = document.getElementById('quote');
const isWorkMode = store.toggleState('work');
const isWorkMode = store.toggle('work');

if ((isWorkMode && quote?.dataset.sfw === 'nsfw') || (!isWorkMode && quote?.dataset.fallback === 'true')) {
updateQuote();
Expand Down
6 changes: 3 additions & 3 deletions src/modules/zen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ import { exitScreensaverMode } from './screensaver';
import { store } from '../store';

export function initZenMode() {
if (store.getState('zen')) {
if (store.get('zen')) {
exitScreensaverMode();
}

document.getElementById('zen')?.addEventListener('click', () => {
if (store.toggleState('zen')) {
if (store.toggle('zen')) {
exitScreensaverMode();
}
});
document.getElementById('exit-zen')?.addEventListener('click', () => store.setState('zen', false));
document.getElementById('exit-zen')?.addEventListener('click', () => store.set('zen', false));
}
11 changes: 6 additions & 5 deletions src/store/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export interface Stateless {
scene?: string;
progress?: string;
index?: string;
static?: boolean;
}

type State = Stateful & Stateless;
Expand Down Expand Up @@ -84,12 +85,12 @@ export class Store {
}

// Get current state
getState<K extends keyof State>(key: K) {
get<K extends keyof State>(key: K) {
return this.state[key];
}

// Update a state property and synchronize to localStorage and URL (for this key)
setState<K extends keyof State>(key: K, value: State[K], syncToUrl: boolean = true) {
set<K extends keyof State>(key: K, value: State[K], syncToUrl: boolean = true) {
const oldState = { ...this.state };
this.state[key] = value;

Expand All @@ -107,10 +108,10 @@ export class Store {
return value;
}

toggleState(key: keyof State) {
toggle(key: keyof State) {
if (typeof this.state[key] === 'boolean') {
const newValue = !this.getState(key);
this.setState(key, newValue);
const newValue = !this.get(key);
this.set(key, newValue);

return newValue;
} else {
Expand Down
Loading
Loading