Skip to content

Commit

Permalink
fix: deactivate themes on signout
Browse files Browse the repository at this point in the history
  • Loading branch information
moughxyz committed Apr 21, 2020
1 parent 6be8fa9 commit 2ce981a
Show file tree
Hide file tree
Showing 5 changed files with 232 additions and 17,297 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,8 @@ class RevisionPreviewModalCtrl implements RevisionPreviewScope {
this.unregisterComponent = this.componentManager.registerHandler({
identifier: editorCopy.uuid,
areas: [ComponentArea.Editor],
contextRequestHandler: (component) => {
if (component === this.editor) {
contextRequestHandler: (componentUuid) => {
if (componentUuid === this.editor?.uuid) {
return this.note;
}
},
Expand Down
71 changes: 40 additions & 31 deletions app/assets/javascripts/services/themeManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,30 +6,37 @@ import {
ApplicationService,
SNTheme,
ComponentArea,
removeFromArray,
ApplicationEvent,
} from 'snjs';
import { AppStateEvent } from '@/ui_models/app_state';

const CACHED_THEMES_KEY = 'cachedThemes';

export class ThemeManager extends ApplicationService {

private activeThemes: SNTheme[]
private activeThemes: string[] = []
private unsubState!: () => void
private unregisterDesktop!: () => void
private unregisterComponent!: () => void

constructor(application: WebApplication) {
super(application);
this.activeThemes = [];
setImmediate(() => {
this.unsubState = this.webApplication.getAppState().addObserver(
async (eventName) => {
if (eventName === AppStateEvent.DesktopExtsReady) {
this.activateCachedThemes();
}
/** @override */
async onAppLaunch() {
super.onAppLaunch();
this.unsubState = this.webApplication.getAppState().addObserver(
async (eventName) => {
if (eventName === AppStateEvent.DesktopExtsReady) {
this.activateCachedThemes();
}
);
});
}
);
}

onAppEvent(event: ApplicationEvent) {
super.onAppEvent(event);
if (event === ApplicationEvent.SignedOut) {
this.deactivateAllThemes();
}
}

get webApplication() {
Expand Down Expand Up @@ -68,7 +75,7 @@ export class ThemeManager extends ApplicationService {
this.unregisterDesktop = this.webApplication.getDesktopService()
.registerUpdateObserver((component) => {
if (component.active && component.isTheme()) {
this.deactivateTheme(component as SNTheme);
this.deactivateTheme(component.uuid);
setTimeout(() => {
this.activateTheme(component as SNTheme);
}, 10);
Expand All @@ -82,28 +89,25 @@ export class ThemeManager extends ApplicationService {
if (component.active) {
this.activateTheme(component as SNTheme);
} else {
this.deactivateTheme(component as SNTheme);
this.deactivateTheme(component.uuid);
}
}
});
}

public deactivateAllThemes() {
const activeThemes = this.application!.componentManager!.getActiveThemes();
for (const theme of activeThemes) {
if (theme) {
this.application!.componentManager!.deregisterComponent(theme.uuid);
}
private deactivateAllThemes() {
for (const uuid of this.activeThemes) {
this.deactivateTheme(uuid, false);
}
this.activeThemes = [];
this.decacheThemes();
}

private activateTheme(theme: SNTheme, writeToCache = true) {
if (this.activeThemes.find((t) => t.uuid === theme.uuid)) {
if (this.activeThemes.find((uuid) => uuid === theme.uuid)) {
return;
}
this.activeThemes.push(theme);
this.activeThemes.push(theme.uuid);
const url = this.application!.componentManager!.urlForComponent(theme)!;
const link = document.createElement('link');
link.href = url;
Expand All @@ -117,18 +121,21 @@ export class ThemeManager extends ApplicationService {
}
}

private deactivateTheme(theme: SNTheme) {
const element = document.getElementById(theme.uuid) as HTMLLinkElement;
private deactivateTheme(uuid: string, recache = true) {
const element = document.getElementById(uuid) as HTMLLinkElement;
if (element) {
element.disabled = true;
element.parentNode!.removeChild(element);
}
_.remove(this.activeThemes, { uuid: theme.uuid });
this.cacheThemes();
removeFromArray(this.activeThemes, uuid);
if (recache) {
this.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 processedPayload = await this.application!.protocolService!.payloadByEncryptingPayload(
payload,
Expand All @@ -144,10 +151,12 @@ export class ThemeManager extends ApplicationService {
}

private async decacheThemes() {
return this.application!.removeValue(
CACHED_THEMES_KEY,
StorageValueModes.Nonwrapped
);
if (this.application) {
return this.application.removeValue(
CACHED_THEMES_KEY,
StorageValueModes.Nonwrapped
);
}
}

private async getCachedThemes() {
Expand Down
8 changes: 4 additions & 4 deletions app/assets/javascripts/views/editor/editor_view.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1030,12 +1030,12 @@ class EditorViewCtrl extends PureViewCtrl implements EditorViewScope {
ComponentArea.EditorStack,
ComponentArea.Editor
],
contextRequestHandler: (component) => {
contextRequestHandler: (componentUuid) => {
const currentEditor = this.activeEditorComponent;
if (
component.uuid === currentEditor?.uuid ||
component.uuid === this.activeTagsComponent?.uuid ||
Uuids(this.getState().activeStackComponents).includes(component.uuid)
componentUuid === currentEditor?.uuid ||
componentUuid === this.activeTagsComponent?.uuid ||
Uuids(this.getState().activeStackComponents).includes(componentUuid)
) {
return this.note;
}
Expand Down
17,444 changes: 185 additions & 17,259 deletions dist/javascripts/app.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/javascripts/app.js.map

Large diffs are not rendered by default.

0 comments on commit 2ce981a

Please sign in to comment.