X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=client%2Fsrc%2Fapp%2Fcore%2Ftheme%2Ftheme.service.ts;h=ac34689419df7a271cb7edc05ace477904083e85;hb=0882c8e6509b2a4ea48f6c48ecb2aa4aa371500a;hp=9dbf22e200147195b35014b752ede3054fa5f2a7;hpb=67ed6552b831df66713bac9e672738796128d33f;p=github%2FChocobozzz%2FPeerTube.git diff --git a/client/src/app/core/theme/theme.service.ts b/client/src/app/core/theme/theme.service.ts index 9dbf22e20..ac3468941 100644 --- a/client/src/app/core/theme/theme.service.ts +++ b/client/src/app/core/theme/theme.service.ts @@ -1,13 +1,13 @@ import { first } from 'rxjs/operators' import { Injectable } from '@angular/core' +import { UserLocalStorageKeys } from '@root-helpers/users' +import { HTMLServerConfig, ServerConfigTheme } from '@shared/models' +import { environment } from '../../../environments/environment' import { AuthService } from '../auth' import { PluginService } from '../plugins/plugin.service' import { ServerService } from '../server' -import { LocalStorageService } from '../wrappers/storage.service' -import { User } from '../users/user.model' import { UserService } from '../users/user.service' -import { ServerConfig, ServerConfigTheme } from '@shared/models' -import { environment } from '../../../environments/environment' +import { LocalStorageService } from '../wrappers/storage.service' @Injectable() export class ThemeService { @@ -18,7 +18,7 @@ export class ThemeService { private themeFromLocalStorage: ServerConfigTheme private themeDOMLinksFromLocalStorage: HTMLLinkElement[] = [] - private serverConfig: ServerConfig + private serverConfig: HTMLServerConfig constructor ( private auth: AuthService, @@ -32,18 +32,13 @@ export class ThemeService { // Try to load from local storage first, so we don't have to wait network requests this.loadAndSetFromLocalStorage() - this.serverConfig = this.server.getTmpConfig() - this.server.getConfig() - .subscribe(config => { - this.serverConfig = config - - const themes = this.serverConfig.theme.registered + this.serverConfig = this.server.getHTMLConfig() + const themes = this.serverConfig.theme.registered - this.removeThemeFromLocalStorageIfNeeded(themes) - this.injectThemes(themes) + this.removeThemeFromLocalStorageIfNeeded(themes) + this.injectThemes(themes) - this.listenUserTheme() - }) + this.listenUserTheme() } private injectThemes (themes: ServerConfigTheme[], fromLocalStorage = false) { @@ -82,14 +77,26 @@ export class ThemeService { : this.userService.getAnonymousUser().theme if (theme !== 'instance-default') return theme - return this.serverConfig.theme.default + + const instanceTheme = this.serverConfig.theme.default + if (instanceTheme !== 'default') return instanceTheme + + // Default to dark theme if available and wanted by the user + if ( + this.themes.find(t => t.name === 'dark') && + window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches + ) { + return 'dark' + } + + return instanceTheme } private loadTheme (name: string) { const links = document.getElementsByTagName('link') for (let i = 0; i < links.length; i++) { - const link = links[ i ] - if (link.getAttribute('rel').indexOf('style') !== -1 && link.getAttribute('title')) { + const link = links[i] + if (link.getAttribute('rel').includes('style') && link.getAttribute('title')) { link.disabled = link.getAttribute('title') !== name } } @@ -107,13 +114,14 @@ export class ThemeService { const theme = this.getTheme(currentTheme) if (theme) { console.log('Adding scripts of theme %s.', currentTheme) + this.pluginService.addPlugin(theme, true) this.pluginService.reloadLoadedScopes() - this.localStorageService.setItem(User.KEYS.THEME, JSON.stringify(theme), false) + this.localStorageService.setItem(UserLocalStorageKeys.LAST_ACTIVE_THEME, JSON.stringify(theme), false) } else { - this.localStorageService.removeItem(User.KEYS.THEME, false) + this.localStorageService.removeItem(UserLocalStorageKeys.LAST_ACTIVE_THEME, false) } this.oldThemeName = currentTheme @@ -127,18 +135,17 @@ export class ThemeService { if (!this.auth.isLoggedIn()) { this.updateCurrentTheme() - this.localStorageService.watch([User.KEYS.THEME]).subscribe( + this.localStorageService.watch([ UserLocalStorageKeys.THEME ]).subscribe( () => this.updateCurrentTheme() ) } this.auth.userInformationLoaded - .pipe(first()) .subscribe(() => this.updateCurrentTheme()) } private loadAndSetFromLocalStorage () { - const lastActiveThemeString = this.localStorageService.getItem(User.KEYS.THEME) + const lastActiveThemeString = this.localStorageService.getItem(UserLocalStorageKeys.LAST_ACTIVE_THEME) if (!lastActiveThemeString) return try {