Skip to content

Commit 3e6ba9d

Browse files
committed
FIX: Fixed that an open tab caused the plugin to crash
1 parent ef8ba50 commit 3e6ba9d

4 files changed

Lines changed: 824 additions & 805 deletions

File tree

src/ui/obsidian-ui-components/item-views/sr-tab-view.tsx

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,19 +27,24 @@ export class SRTabView extends ItemView {
2727
private reviewQueueLoader: ReviewQueueLoader | null = null;
2828
private contentManager: ContentManager | null = null;
2929

30-
private plugin: SRPlugin;
30+
private plugin: SRPlugin | null = null;
3131
private viewContainerEl: HTMLElement | null = null;
3232
private viewContentEl: HTMLElement | null = null;
33-
private settings: SRSettings;
33+
private settings: SRSettings | null = null;
3434

3535
constructor(
3636
leaf: WorkspaceLeaf,
3737
plugin: SRPlugin,
3838
reviewQueueLoader: ReviewQueueLoader | null,
3939
) {
4040
super(leaf);
41-
if (plugin.dataManager === null || plugin.dataManager.data === null)
42-
throw new Error("SR plugin or data not initialized!!!");
41+
42+
if (!plugin.isDataManagerLoaded()) {
43+
this.leaf.detach();
44+
return;
45+
}
46+
47+
// throw new Error("SR plugin or data not initialized!!!");
4348
// Init properties
4449
this.plugin = plugin;
4550
this.navigation = false;
@@ -132,6 +137,11 @@ export class SRTabView extends ItemView {
132137
);
133138
}
134139

140+
if (this.settings === null || this.plugin === null) {
141+
this.leaf.detach();
142+
return;
143+
}
144+
135145
this.contentManager = new ContentManager(
136146
this.app,
137147
this.plugin,

src/ui/tab-view-manager.tsx

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,9 @@ export default class TabViewManager {
3131
},
3232
];
3333

34-
// Add any needed resourced
34+
// Add any needed resource
3535
constructor(plugin: SRPlugin) {
3636
this.plugin = plugin;
37-
38-
this.registerAllTabViews();
3937
}
4038

4139
/**

src/ui/ui-manager.tsx

Lines changed: 24 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,8 @@ export class UIManager {
6161
constructor(plugin: SRPlugin) {
6262
this.plugin = plugin;
6363
appIcon();
64-
65-
// Closes all still open tab views when the plugin is loaded, because it causes bugs / empty windows otherwise
6664
this.tabViewManager = new TabViewManager(this.plugin);
65+
this.tabViewManager.registerAllTabViews();
6766

6867
this.sidebarManager = new SidebarManager(this.plugin);
6968

@@ -78,6 +77,7 @@ export class UIManager {
7877
}
7978

8079
public async onLayoutReady() {
80+
// Closes all still open tab views when the plugin is loaded, because it causes bugs / empty windows otherwise
8181
this.tabViewManager.closeAllTabViews();
8282
await this.sidebarManager.activateReviewQueueViewPanel();
8383
await this.plugin.dataManager.sync();
@@ -110,14 +110,17 @@ export class UIManager {
110110
);
111111

112112
if (settings.showStatusBar) {
113-
this.statusBarManager.setCount(
114-
this.plugin.dataManager.osrCore.remainingDeckTree.getRepItemCount(
115-
RepItemState.AnyItem,
116-
true,
117-
),
118-
settings.showStatusBar && settings.showCardStatusBarItem,
119-
"card-review",
120-
);
113+
if (this.plugin.dataManager.osrCore.remainingDeckTree !== null) {
114+
this.statusBarManager.setCount(
115+
this.plugin.dataManager.osrCore.remainingDeckTree.getRepItemCount(
116+
RepItemState.AnyItem,
117+
true,
118+
),
119+
settings.showStatusBar && settings.showCardStatusBarItem,
120+
"card-review",
121+
);
122+
}
123+
121124
this.statusBarManager.setCount(
122125
this.plugin.dataManager.osrCore.noteReviewQueue.dueNotesCount,
123126
settings.showStatusBar && settings.showNoteStatusBarItem,
@@ -169,8 +172,8 @@ export class UIManager {
169172
// Only set focus if it was already in focus, as that is the only case where the tab would be covered by the modal
170173
this.setSRViewInFocus(
171174
(modal === null || modal === undefined) &&
172-
this.plugin.app.workspace.getActiveViewOfType(SRTabView) !== null &&
173-
this.plugin.app.workspace.getActiveViewOfType(SRTabView) !== undefined,
175+
this.plugin.app.workspace.getActiveViewOfType(SRTabView) !== null &&
176+
this.plugin.app.workspace.getActiveViewOfType(SRTabView) !== undefined,
174177
);
175178
}
176179
}
@@ -241,13 +244,7 @@ export class UIManager {
241244
};
242245
const currentWindow = remoteModule?.getCurrentWindow?.();
243246
if (currentWindow !== null && currentWindow !== undefined) {
244-
return currentWindow as {
245-
isMinimized?: () => boolean;
246-
restore?: () => void;
247-
show?: () => void;
248-
focus?: () => void;
249-
moveTop?: () => void;
250-
};
247+
return currentWindow;
251248
}
252249
} catch {
253250
// ignore
@@ -259,13 +256,7 @@ export class UIManager {
259256
};
260257
const currentWindow = electronModule?.remote?.getCurrentWindow?.();
261258
if (currentWindow !== null && currentWindow !== undefined) {
262-
return currentWindow as {
263-
isMinimized?: () => boolean;
264-
restore?: () => void;
265-
show?: () => void;
266-
focus?: () => void;
267-
moveTop?: () => void;
268-
};
259+
return currentWindow;
269260
}
270261
} catch {
271262
// ignore
@@ -310,9 +301,9 @@ export class UIManager {
310301

311302
const windowObject = activeDocument.defaultView as
312303
| (Window & {
313-
AudioContext?: typeof AudioContext;
314-
webkitAudioContext?: typeof AudioContext;
315-
})
304+
AudioContext?: typeof AudioContext;
305+
webkitAudioContext?: typeof AudioContext;
306+
})
316307
| null;
317308
const AudioContextCtor =
318309
windowObject?.AudioContext ?? windowObject?.webkitAudioContext ?? null;
@@ -369,10 +360,10 @@ export class UIManager {
369360
type ElectronApp = { dock?: DockApi };
370361
const requireFn = (
371362
activeDocument.defaultView as
372-
| (Window & {
373-
require?: (moduleName: string) => unknown;
374-
})
375-
| null
363+
| (Window & {
364+
require?: (moduleName: string) => unknown;
365+
})
366+
| null
376367
)?.require;
377368
if (requireFn === undefined) {
378369
return;

0 commit comments

Comments
 (0)