X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=client%2Fsrc%2Fapp%2Fcore%2Ftheme%2Ftheme.service.ts;h=ac34689419df7a271cb7edc05ace477904083e85;hb=0882c8e6509b2a4ea48f6c48ecb2aa4aa371500a;hp=76199d1cc03215a96cdc4261932f287e69716587;hpb=d00dc28dd73ad9dd419d5a5ac6ac747cefbc6e8b;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 76199d1cc..ac3468941 100644 --- a/client/src/app/core/theme/theme.service.ts +++ b/client/src/app/core/theme/theme.service.ts @@ -1,9 +1,13 @@ +import { first } from 'rxjs/operators' import { Injectable } from '@angular/core' -import { AuthService } from '@app/core/auth' -import { ServerService } from '@app/core/server' +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 { 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 { @@ -11,29 +15,42 @@ export class ThemeService { 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 () { - this.server.configLoaded - .subscribe(() => { - this.injectThemes() + // Try to load from local storage first, so we don't have to wait network requests + this.loadAndSetFromLocalStorage() + + this.serverConfig = this.server.getHTMLConfig() + const themes = this.serverConfig.theme.registered - this.listenUserTheme() - }) + this.removeThemeFromLocalStorageIfNeeded(themes) + this.injectThemes(themes) + + this.listenUserTheme() } - private injectThemes () { - this.themes = this.server.getConfig().theme.registered + private injectThemes (themes: ServerConfigTheme[], fromLocalStorage = false) { + this.themes = themes console.log('Injecting %d themes.', this.themes.length) - const head = document.getElementsByTagName('head')[0] + const head = this.getHeadElement() for (const theme of this.themes) { + // Already added this theme? + if (fromLocalStorage === false && this.themeFromLocalStorage && this.themeFromLocalStorage.name === theme.name) continue for (const css of theme.css) { const link = document.createElement('link') @@ -45,38 +62,48 @@ export class ThemeService { link.setAttribute('title', theme.name) link.setAttribute('disabled', '') + if (fromLocalStorage === true) this.themeDOMLinksFromLocalStorage.push(link) + head.appendChild(link) } } } private getCurrentTheme () { - if (this.auth.isLoggedIn()) { - const theme = this.auth.getUser().theme - if (theme !== 'instance-default') return theme + if (this.themeFromLocalStorage) return this.themeFromLocalStorage.name + + 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.themes.find(t => t.name === 'dark') && + 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 } } } private updateCurrentTheme () { - if (this.oldThemeName) { - const oldTheme = this.getTheme(this.oldThemeName) - if (oldTheme) { - console.log('Removing scripts of old theme %s.', this.oldThemeName) - this.pluginService.removePlugin(oldTheme) - } - } + if (this.oldThemeName) this.removeThemePlugins(this.oldThemeName) const currentTheme = this.getCurrentTheme() @@ -87,23 +114,83 @@ export class ThemeService { const theme = this.getTheme(currentTheme) if (theme) { console.log('Adding scripts of theme %s.', currentTheme) - this.pluginService.addPlugin(theme) + + this.pluginService.addPlugin(theme, true) this.pluginService.reloadLoadedScopes() + + this.localStorageService.setItem(UserLocalStorageKeys.LAST_ACTIVE_THEME, JSON.stringify(theme), false) + } else { + this.localStorageService.removeItem(UserLocalStorageKeys.LAST_ACTIVE_THEME, false) } this.oldThemeName = currentTheme } private listenUserTheme () { + // We don't need them anymore + this.themeFromLocalStorage = undefined + this.themeDOMLinksFromLocalStorage = [] + if (!this.auth.isLoggedIn()) { this.updateCurrentTheme() + + this.localStorageService.watch([ UserLocalStorageKeys.THEME ]).subscribe( + () => this.updateCurrentTheme() + ) } this.auth.userInformationLoaded .subscribe(() => this.updateCurrentTheme()) } + private loadAndSetFromLocalStorage () { + const lastActiveThemeString = this.localStorageService.getItem(UserLocalStorageKeys.LAST_ACTIVE_THEME) + if (!lastActiveThemeString) return + + try { + const lastActiveTheme = JSON.parse(lastActiveThemeString) + this.themeFromLocalStorage = lastActiveTheme + + this.injectThemes([ lastActiveTheme ], true) + this.updateCurrentTheme() + } catch (err) { + console.error('Cannot parse last active theme.', err) + return + } + } + + private removeThemePlugins (themeName: string) { + const oldTheme = this.getTheme(themeName) + if (oldTheme) { + console.log('Removing scripts of old theme %s.', themeName) + this.pluginService.removePlugin(oldTheme) + } + } + + private removeThemeFromLocalStorageIfNeeded (themes: ServerConfigTheme[]) { + if (!this.themeFromLocalStorage) return + + const loadedTheme = themes.find(t => t.name === this.themeFromLocalStorage.name) + if (!loadedTheme || loadedTheme.version !== this.themeFromLocalStorage.version) { + // Need to remove this theme: we loaded an old version or a theme that does not exist anymore + this.removeThemePlugins(this.themeFromLocalStorage.name) + this.oldThemeName = undefined + + const head = this.getHeadElement() + for (const htmlLinkElement of this.themeDOMLinksFromLocalStorage) { + head.removeChild(htmlLinkElement) + } + + this.themeFromLocalStorage = undefined + this.themeDOMLinksFromLocalStorage = [] + } + } + + private getHeadElement () { + return document.getElementsByTagName('head')[0] + } + private getTheme (name: string) { return this.themes.find(t => t.name === name) }