fix: deactivate themes on signout

This commit is contained in:
Mo Bitar
2020-04-21 17:24:11 -05:00
parent 6be8fa93f8
commit 2ce981a1ea
5 changed files with 232 additions and 17297 deletions

View File

@@ -72,8 +72,8 @@ class RevisionPreviewModalCtrl implements RevisionPreviewScope {
this.unregisterComponent = this.componentManager.registerHandler({ this.unregisterComponent = this.componentManager.registerHandler({
identifier: editorCopy.uuid, identifier: editorCopy.uuid,
areas: [ComponentArea.Editor], areas: [ComponentArea.Editor],
contextRequestHandler: (component) => { contextRequestHandler: (componentUuid) => {
if (component === this.editor) { if (componentUuid === this.editor?.uuid) {
return this.note; return this.note;
} }
}, },

View File

@@ -6,6 +6,8 @@ import {
ApplicationService, ApplicationService,
SNTheme, SNTheme,
ComponentArea, ComponentArea,
removeFromArray,
ApplicationEvent,
} from 'snjs'; } from 'snjs';
import { AppStateEvent } from '@/ui_models/app_state'; import { AppStateEvent } from '@/ui_models/app_state';
@@ -13,23 +15,28 @@ const CACHED_THEMES_KEY = 'cachedThemes';
export class ThemeManager extends ApplicationService { export class ThemeManager extends ApplicationService {
private activeThemes: SNTheme[] private activeThemes: string[] = []
private unsubState!: () => void private unsubState!: () => void
private unregisterDesktop!: () => void private unregisterDesktop!: () => void
private unregisterComponent!: () => void private unregisterComponent!: () => void
constructor(application: WebApplication) { /** @override */
super(application); async onAppLaunch() {
this.activeThemes = []; super.onAppLaunch();
setImmediate(() => { this.unsubState = this.webApplication.getAppState().addObserver(
this.unsubState = this.webApplication.getAppState().addObserver( async (eventName) => {
async (eventName) => { if (eventName === AppStateEvent.DesktopExtsReady) {
if (eventName === AppStateEvent.DesktopExtsReady) { this.activateCachedThemes();
this.activateCachedThemes();
}
} }
); }
}); );
}
onAppEvent(event: ApplicationEvent) {
super.onAppEvent(event);
if (event === ApplicationEvent.SignedOut) {
this.deactivateAllThemes();
}
} }
get webApplication() { get webApplication() {
@@ -68,7 +75,7 @@ export class ThemeManager extends ApplicationService {
this.unregisterDesktop = this.webApplication.getDesktopService() this.unregisterDesktop = this.webApplication.getDesktopService()
.registerUpdateObserver((component) => { .registerUpdateObserver((component) => {
if (component.active && component.isTheme()) { if (component.active && component.isTheme()) {
this.deactivateTheme(component as SNTheme); this.deactivateTheme(component.uuid);
setTimeout(() => { setTimeout(() => {
this.activateTheme(component as SNTheme); this.activateTheme(component as SNTheme);
}, 10); }, 10);
@@ -82,28 +89,25 @@ export class ThemeManager extends ApplicationService {
if (component.active) { if (component.active) {
this.activateTheme(component as SNTheme); this.activateTheme(component as SNTheme);
} else { } else {
this.deactivateTheme(component as SNTheme); this.deactivateTheme(component.uuid);
} }
} }
}); });
} }
public deactivateAllThemes() { private deactivateAllThemes() {
const activeThemes = this.application!.componentManager!.getActiveThemes(); for (const uuid of this.activeThemes) {
for (const theme of activeThemes) { this.deactivateTheme(uuid, false);
if (theme) {
this.application!.componentManager!.deregisterComponent(theme.uuid);
}
} }
this.activeThemes = []; this.activeThemes = [];
this.decacheThemes(); this.decacheThemes();
} }
private activateTheme(theme: SNTheme, writeToCache = true) { private activateTheme(theme: SNTheme, writeToCache = true) {
if (this.activeThemes.find((t) => t.uuid === theme.uuid)) { if (this.activeThemes.find((uuid) => uuid === theme.uuid)) {
return; return;
} }
this.activeThemes.push(theme); this.activeThemes.push(theme.uuid);
const url = this.application!.componentManager!.urlForComponent(theme)!; const url = this.application!.componentManager!.urlForComponent(theme)!;
const link = document.createElement('link'); const link = document.createElement('link');
link.href = url; link.href = url;
@@ -117,18 +121,21 @@ export class ThemeManager extends ApplicationService {
} }
} }
private deactivateTheme(theme: SNTheme) { private deactivateTheme(uuid: string, recache = true) {
const element = document.getElementById(theme.uuid) as HTMLLinkElement; const element = document.getElementById(uuid) as HTMLLinkElement;
if (element) { if (element) {
element.disabled = true; element.disabled = true;
element.parentNode!.removeChild(element); element.parentNode!.removeChild(element);
} }
_.remove(this.activeThemes, { uuid: theme.uuid }); removeFromArray(this.activeThemes, uuid);
this.cacheThemes(); if (recache) {
this.cacheThemes();
}
} }
private async cacheThemes() { private async cacheThemes() {
const mapped = await Promise.all(this.activeThemes.map(async (theme) => { const themes = this.application!.getAll(this.activeThemes) as SNTheme[];
const mapped = await Promise.all(themes.map(async (theme) => {
const payload = theme.payloadRepresentation(); const payload = theme.payloadRepresentation();
const processedPayload = await this.application!.protocolService!.payloadByEncryptingPayload( const processedPayload = await this.application!.protocolService!.payloadByEncryptingPayload(
payload, payload,
@@ -144,10 +151,12 @@ export class ThemeManager extends ApplicationService {
} }
private async decacheThemes() { private async decacheThemes() {
return this.application!.removeValue( if (this.application) {
CACHED_THEMES_KEY, return this.application.removeValue(
StorageValueModes.Nonwrapped CACHED_THEMES_KEY,
); StorageValueModes.Nonwrapped
);
}
} }
private async getCachedThemes() { private async getCachedThemes() {

View File

@@ -1030,12 +1030,12 @@ class EditorViewCtrl extends PureViewCtrl implements EditorViewScope {
ComponentArea.EditorStack, ComponentArea.EditorStack,
ComponentArea.Editor ComponentArea.Editor
], ],
contextRequestHandler: (component) => { contextRequestHandler: (componentUuid) => {
const currentEditor = this.activeEditorComponent; const currentEditor = this.activeEditorComponent;
if ( if (
component.uuid === currentEditor?.uuid || componentUuid === currentEditor?.uuid ||
component.uuid === this.activeTagsComponent?.uuid || componentUuid === this.activeTagsComponent?.uuid ||
Uuids(this.getState().activeStackComponents).includes(component.uuid) Uuids(this.getState().activeStackComponents).includes(componentUuid)
) { ) {
return this.note; return this.note;
} }

17444
dist/javascripts/app.js vendored

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long