refactor(ui): new standard and accessible UI font (#1777)
This commit is contained in:
@@ -0,0 +1,62 @@
|
||||
import {DOCUMENT} from '@angular/common';
|
||||
import {TestBed} from '@angular/core/testing';
|
||||
import {afterEach, beforeEach, describe, expect, it} from 'vitest';
|
||||
|
||||
import {AppUiFontService} from './app-ui-font.service';
|
||||
|
||||
describe('AppUiFontService', () => {
|
||||
let service: AppUiFontService;
|
||||
let document: Document;
|
||||
|
||||
beforeEach(() => {
|
||||
localStorage.clear();
|
||||
TestBed.configureTestingModule({});
|
||||
document = TestBed.inject(DOCUMENT);
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
delete document.documentElement.dataset['uiFont'];
|
||||
localStorage.clear();
|
||||
TestBed.resetTestingModule();
|
||||
});
|
||||
|
||||
it('applies an accessible UI font to the document root', () => {
|
||||
service = TestBed.inject(AppUiFontService);
|
||||
|
||||
service.applyUiFont('atkinson');
|
||||
|
||||
expect(service.uiFont()).toBe('atkinson');
|
||||
expect(document.documentElement.dataset['uiFont']).toBe('atkinson');
|
||||
expect(localStorage.getItem('appUiFont')).toBe('atkinson');
|
||||
});
|
||||
|
||||
it('falls back to the default UI font for unsupported values', () => {
|
||||
service = TestBed.inject(AppUiFontService);
|
||||
|
||||
service.applyUiFont('unsupported');
|
||||
|
||||
expect(service.uiFont()).toBe('default');
|
||||
expect(document.documentElement.dataset['uiFont']).toBeUndefined();
|
||||
expect(localStorage.getItem('appUiFont')).toBe('default');
|
||||
});
|
||||
|
||||
it('loads a stored accessible UI font on creation', () => {
|
||||
localStorage.setItem('appUiFont', 'atkinson');
|
||||
|
||||
service = TestBed.inject(AppUiFontService);
|
||||
|
||||
expect(service.uiFont()).toBe('atkinson');
|
||||
expect(document.documentElement.dataset['uiFont']).toBe('atkinson');
|
||||
expect(localStorage.getItem('appUiFont')).toBe('atkinson');
|
||||
});
|
||||
|
||||
it('normalizes an unsupported stored UI font on creation', () => {
|
||||
localStorage.setItem('appUiFont', 'unsupported');
|
||||
|
||||
service = TestBed.inject(AppUiFontService);
|
||||
|
||||
expect(service.uiFont()).toBe('default');
|
||||
expect(document.documentElement.dataset['uiFont']).toBeUndefined();
|
||||
expect(localStorage.getItem('appUiFont')).toBe('default');
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,47 @@
|
||||
import {DOCUMENT, isPlatformBrowser} from '@angular/common';
|
||||
import {inject, Injectable, PLATFORM_ID, signal} from '@angular/core';
|
||||
|
||||
import {DEFAULT_UI_FONT, normalizeUiFont, type UiFontPreference} from '../model/ui-font.model';
|
||||
|
||||
const STORAGE_KEY = 'appUiFont';
|
||||
|
||||
@Injectable({providedIn: 'root'})
|
||||
export class AppUiFontService {
|
||||
private readonly document = inject(DOCUMENT);
|
||||
private readonly platformId = inject(PLATFORM_ID);
|
||||
private readonly uiFontSignal = signal<UiFontPreference>(DEFAULT_UI_FONT);
|
||||
|
||||
readonly uiFont = this.uiFontSignal.asReadonly();
|
||||
|
||||
constructor() {
|
||||
this.applyUiFont(this.getStoredUiFont());
|
||||
}
|
||||
|
||||
private getStoredUiFont(): UiFontPreference {
|
||||
if (!isPlatformBrowser(this.platformId)) {
|
||||
return DEFAULT_UI_FONT;
|
||||
}
|
||||
|
||||
return normalizeUiFont(localStorage.getItem(STORAGE_KEY));
|
||||
}
|
||||
|
||||
applyUiFont(uiFont: string | null | undefined): void {
|
||||
const nextUiFont = normalizeUiFont(uiFont);
|
||||
this.uiFontSignal.set(nextUiFont);
|
||||
this.applyRootAttribute(nextUiFont);
|
||||
|
||||
if (isPlatformBrowser(this.platformId)) {
|
||||
localStorage.setItem(STORAGE_KEY, nextUiFont);
|
||||
}
|
||||
}
|
||||
|
||||
private applyRootAttribute(uiFont: UiFontPreference): void {
|
||||
const root = this.document.documentElement;
|
||||
if (uiFont === DEFAULT_UI_FONT) {
|
||||
delete root.dataset['uiFont'];
|
||||
return;
|
||||
}
|
||||
|
||||
root.dataset['uiFont'] = uiFont;
|
||||
}
|
||||
}
|
||||
@@ -9,6 +9,7 @@ import {StartupService} from './startup.service';
|
||||
import {UserService} from '../../features/settings/user-management/user.service';
|
||||
import {AppThemeService} from './app-theme.service';
|
||||
import {AppLocaleService} from './app-locale.service';
|
||||
import {AppUiFontService} from './app-ui-font.service';
|
||||
|
||||
describe('StartupService', () => {
|
||||
beforeEach(() => {
|
||||
@@ -62,6 +63,7 @@ describe('StartupService', () => {
|
||||
const applySyncedTheme = vi.fn();
|
||||
const applyLocale = vi.fn().mockResolvedValue(undefined);
|
||||
const getDisplayLocale = vi.fn().mockReturnValue('de');
|
||||
const applyUiFont = vi.fn();
|
||||
|
||||
TestBed.configureTestingModule({
|
||||
providers: [
|
||||
@@ -79,6 +81,7 @@ describe('StartupService', () => {
|
||||
{provide: QueryClient, useValue: {fetchQuery}},
|
||||
{provide: AppThemeService, useValue: {applySyncedTheme, appState: () => ({themeSyncEnabled: true})}},
|
||||
{provide: AppLocaleService, useValue: {applyLocale, getDisplayLocale}},
|
||||
{provide: AppUiFontService, useValue: {applyUiFont}},
|
||||
]
|
||||
});
|
||||
|
||||
@@ -86,16 +89,18 @@ describe('StartupService', () => {
|
||||
|
||||
await expect(service.load()).resolves.toBeUndefined();
|
||||
expect(fetchQuery).not.toHaveBeenCalled();
|
||||
expect(applyUiFont).not.toHaveBeenCalled();
|
||||
expect(getDisplayLocale).toHaveBeenCalledOnce();
|
||||
expect(applyLocale).toHaveBeenCalledWith('de');
|
||||
});
|
||||
|
||||
it('uses the backend locale for authenticated users', async () => {
|
||||
const fetchQuery = vi.fn().mockResolvedValue({id: 1, username: 'admin', locale: 'en', theme: 'custom', themeAccent: 'teal', themeSyncEnabled: true});
|
||||
const fetchQuery = vi.fn().mockResolvedValue({id: 1, username: 'admin', locale: 'en', theme: 'custom', themeAccent: 'teal', themeSyncEnabled: true, uiFont: 'atkinson'});
|
||||
const updateUserProfile = vi.fn().mockReturnValue(of({id: 1, username: 'admin', locale: 'de', theme: 'custom', themeAccent: 'teal', themeSyncEnabled: true}));
|
||||
const applySyncedTheme = vi.fn();
|
||||
const applyLocale = vi.fn().mockResolvedValue(undefined);
|
||||
const getDisplayLocale = vi.fn().mockReturnValue('de');
|
||||
const applyUiFont = vi.fn();
|
||||
|
||||
TestBed.configureTestingModule({
|
||||
providers: [
|
||||
@@ -106,7 +111,7 @@ describe('StartupService', () => {
|
||||
useValue: {
|
||||
getUserQueryOptions: () => queryOptions({
|
||||
queryKey: ['user'],
|
||||
queryFn: async () => ({id: 1, username: 'admin', locale: 'en', theme: 'custom', themeAccent: 'teal', themeSyncEnabled: true}),
|
||||
queryFn: async () => ({id: 1, username: 'admin', locale: 'en', theme: 'custom', themeAccent: 'teal', themeSyncEnabled: true, uiFont: 'atkinson'}),
|
||||
}),
|
||||
updateUserProfile,
|
||||
},
|
||||
@@ -114,6 +119,7 @@ describe('StartupService', () => {
|
||||
{provide: QueryClient, useValue: {fetchQuery}},
|
||||
{provide: AppThemeService, useValue: {applySyncedTheme, appState: () => ({themeSyncEnabled: true})}},
|
||||
{provide: AppLocaleService, useValue: {applyLocale, getDisplayLocale}},
|
||||
{provide: AppUiFontService, useValue: {applyUiFont}},
|
||||
]
|
||||
});
|
||||
|
||||
@@ -123,6 +129,7 @@ describe('StartupService', () => {
|
||||
|
||||
expect(updateUserProfile).not.toHaveBeenCalled();
|
||||
expect(applySyncedTheme).toHaveBeenCalledWith('custom', 'teal');
|
||||
expect(applyUiFont).toHaveBeenCalledWith('atkinson');
|
||||
expect(applyLocale).toHaveBeenCalledWith('en');
|
||||
});
|
||||
|
||||
|
||||
@@ -5,6 +5,7 @@ import {QueryClient} from '@tanstack/angular-query-experimental';
|
||||
import {AppThemeService} from './app-theme.service';
|
||||
import {DEFAULT_CUSTOM_PRIMARY} from '../model/app-state.model';
|
||||
import {AppLocaleService} from './app-locale.service';
|
||||
import {AppUiFontService} from './app-ui-font.service';
|
||||
|
||||
@Injectable({providedIn: 'root'})
|
||||
export class StartupService {
|
||||
@@ -13,6 +14,7 @@ export class StartupService {
|
||||
private queryClient = inject(QueryClient);
|
||||
private appThemeService = inject(AppThemeService);
|
||||
private appLocaleService = inject(AppLocaleService);
|
||||
private appUiFontService = inject(AppUiFontService);
|
||||
|
||||
async load(): Promise<void> {
|
||||
if (this.authService.token()) {
|
||||
@@ -26,6 +28,7 @@ export class StartupService {
|
||||
|
||||
private async applyUserPreferences(user: User): Promise<void> {
|
||||
this.applyTheme(user);
|
||||
this.appUiFontService.applyUiFont(user.uiFont);
|
||||
await this.appLocaleService.applyLocale(user.locale);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user