X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=client%2Fsrc%2Fapp%2Fcore%2Ftheme%2Ftheme.service.ts;h=ead1770ba1b30bd87fe0e5ddeb617f3b53885439;hb=2c8380a46f34631e705b1564938343cacfa4b0bc;hp=3eebc1acc8e7142e44b8d3d14ea6a7be5af1cf23;hpb=92744e8311e1d0938cb72087a0d75c29dbb1aeaa;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 3eebc1acc..ead1770ba 100644 --- a/client/src/app/core/theme/theme.service.ts +++ b/client/src/app/core/theme/theme.service.ts @@ -1,50 +1,64 @@ import { Injectable } from '@angular/core' -import { AuthService } from '@app/core/auth' -import { ServerService } from '@app/core/server' +import { logger } from '@root-helpers/logger' +import { capitalizeFirstLetter } from '@root-helpers/string' +import { UserLocalStorageKeys } from '@root-helpers/users' +import { HTMLServerConfig, ServerConfigTheme } from '@shared/models' import { environment } from '../../../environments/environment' -import { PluginService } from '@app/core/plugins/plugin.service' -import { ServerConfigTheme } from '@shared/models' -import { peertubeLocalStorage } from '@app/shared/misc/peertube-web-storage' -import { first } from 'rxjs/operators' +import { AuthService } from '../auth' +import { PluginService } from '../plugins/plugin.service' +import { ServerService } from '../server' +import { UserService } from '../users/user.service' +import { LocalStorageService } from '../wrappers/storage.service' @Injectable() export class ThemeService { - private static KEYS = { - LAST_ACTIVE_THEME: 'last_active_theme' - } - private oldThemeName: string private themes: ServerConfigTheme[] = [] private themeFromLocalStorage: ServerConfigTheme private themeDOMLinksFromLocalStorage: HTMLLinkElement[] = [] + private serverConfig: HTMLServerConfig + constructor ( private auth: AuthService, + private userService: UserService, private pluginService: PluginService, - private server: ServerService + private server: ServerService, + private localStorageService: LocalStorageService ) {} initialize () { // Try to load from local storage first, so we don't have to wait network requests this.loadAndSetFromLocalStorage() - this.server.configLoaded - .subscribe(() => { - const themes = this.server.getConfig().theme.registered + this.serverConfig = this.server.getHTMLConfig() + const themes = this.serverConfig.theme.registered + + this.removeThemeFromLocalStorageIfNeeded(themes) + this.injectThemes(themes) + + this.listenUserTheme() + } + + getDefaultThemeLabel () { + if (this.hasDarkTheme()) { + return $localize`Light/Orange or Dark` + } - this.removeThemeFromLocalStorageIfNeeded(themes) - this.injectThemes(themes) + return $localize`Light/Orange` + } - this.listenUserTheme() - }) + buildAvailableThemes () { + return this.serverConfig.theme.registered + .map(t => ({ id: t.name, label: capitalizeFirstLetter(t.name) })) } private injectThemes (themes: ServerConfigTheme[], fromLocalStorage = false) { this.themes = themes - console.log('Injecting %d themes.', this.themes.length) + logger.info(`Injecting ${this.themes.length} themes.`) const head = this.getHeadElement() @@ -72,19 +86,28 @@ export class ThemeService { private getCurrentTheme () { if (this.themeFromLocalStorage) return this.themeFromLocalStorage.name - if (this.auth.isLoggedIn()) { - const theme = this.auth.getUser().theme - if (theme !== 'instance-default') return theme + const theme = this.auth.isLoggedIn() + ? this.auth.getUser().theme + : this.userService.getAnonymousUser().theme + + if (theme !== 'instance-default') return theme + + const instanceTheme = this.serverConfig.theme.default + if (instanceTheme !== 'default') return instanceTheme + + // Default to dark theme if available and wanted by the user + if (this.hasDarkTheme() && window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches) { + return 'dark' } - return this.server.getConfig().theme.default + 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 } } @@ -95,20 +118,21 @@ export class ThemeService { const currentTheme = this.getCurrentTheme() - console.log('Enabling %s theme.', currentTheme) + logger.info(`Enabling ${currentTheme} theme.`) this.loadTheme(currentTheme) const theme = this.getTheme(currentTheme) if (theme) { - console.log('Adding scripts of theme %s.', currentTheme) + logger.info(`Adding scripts of theme ${currentTheme}`) + this.pluginService.addPlugin(theme, true) this.pluginService.reloadLoadedScopes() - peertubeLocalStorage.setItem(ThemeService.KEYS.LAST_ACTIVE_THEME, JSON.stringify(theme)) + this.localStorageService.setItem(UserLocalStorageKeys.LAST_ACTIVE_THEME, JSON.stringify(theme), false) } else { - peertubeLocalStorage.removeItem(ThemeService.KEYS.LAST_ACTIVE_THEME) + this.localStorageService.removeItem(UserLocalStorageKeys.LAST_ACTIVE_THEME, false) } this.oldThemeName = currentTheme @@ -121,15 +145,18 @@ export class ThemeService { if (!this.auth.isLoggedIn()) { this.updateCurrentTheme() + + this.localStorageService.watch([ UserLocalStorageKeys.THEME ]).subscribe( + () => this.updateCurrentTheme() + ) } this.auth.userInformationLoaded - .pipe(first()) .subscribe(() => this.updateCurrentTheme()) } private loadAndSetFromLocalStorage () { - const lastActiveThemeString = peertubeLocalStorage.getItem(ThemeService.KEYS.LAST_ACTIVE_THEME) + const lastActiveThemeString = this.localStorageService.getItem(UserLocalStorageKeys.LAST_ACTIVE_THEME) if (!lastActiveThemeString) return try { @@ -139,7 +166,7 @@ export class ThemeService { this.injectThemes([ lastActiveTheme ], true) this.updateCurrentTheme() } catch (err) { - console.error('Cannot parse last active theme.', err) + logger.error('Cannot parse last active theme.', err) return } } @@ -147,7 +174,7 @@ export class ThemeService { private removeThemePlugins (themeName: string) { const oldTheme = this.getTheme(themeName) if (oldTheme) { - console.log('Removing scripts of old theme %s.', themeName) + logger.info(`Removing scripts of old theme ${themeName}.`) this.pluginService.removePlugin(oldTheme) } } @@ -178,4 +205,8 @@ export class ThemeService { private getTheme (name: string) { return this.themes.find(t => t.name === name) } + + private hasDarkTheme () { + return this.serverConfig.theme.registered.some(t => t.name === 'dark') + } }