Skip to content

Commit

Permalink
fix(sidebar): duplicate icons & reviewQueueView undefined exception
Browse files Browse the repository at this point in the history
  • Loading branch information
st3v3nmw committed Oct 20, 2024
1 parent e313a51 commit 8f976c2
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 67 deletions.
3 changes: 2 additions & 1 deletion src/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,12 @@ export class OsrCore {
osrNoteLinkInfoFinder: IOsrVaultNoteLinkInfoFinder,
settings: SRSettings,
dataChangedHandler: () => void,
noteReviewQueue: NoteReviewQueue,
): void {
this.settings = settings;
this.osrNoteLinkInfoFinder = osrNoteLinkInfoFinder;
this.dataChangedHandler = dataChangedHandler;
this._noteReviewQueue = new NoteReviewQueue();
this._noteReviewQueue = noteReviewQueue;
this._questionPostponementList = questionPostponementList;
this._dueDateFlashcardHistogram = new CardDueDateHistogram();
this._dueDateNoteHistogram = new NoteDueDateHistogram();
Expand Down
12 changes: 8 additions & 4 deletions src/gui/review-queue-list-view.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { App, ItemView, Menu, TFile, WorkspaceLeaf } from "obsidian";
import { ItemView, Menu, TFile, WorkspaceLeaf } from "obsidian";

import { COLLAPSE_ICON, TICKS_PER_DAY } from "src/constants";
import { t } from "src/lang/helpers";
Expand All @@ -18,16 +18,18 @@ export class ReviewQueueListView extends ItemView {

constructor(
leaf: WorkspaceLeaf,
app: App,
nextNoteReviewHandler: NextNoteReviewHandler,
settings: SRSettings,
) {
super(leaf);

this.nextNoteReviewHandler = nextNoteReviewHandler;
this.settings = settings;
this.registerEvent(this.app.workspace.on("file-open", () => this.redraw()));
this.registerEvent(this.app.vault.on("rename", () => this.redraw()));

if (this.settings.enableNoteReviewPaneOnStartup) {
this.registerEvent(this.app.workspace.on("file-open", () => this.redraw()));
this.registerEvent(this.app.vault.on("rename", () => this.redraw()));
}
}

public getViewType(): string {
Expand All @@ -53,6 +55,8 @@ export class ReviewQueueListView extends ItemView {
}

public redraw(): void {
if (!this.noteReviewQueue.reviewDecks) return;

const activeFile: TFile | null = this.app.workspace.getActiveFile();

const rootEl: HTMLElement = createDiv("tree-item nav-folder mod-root");
Expand Down
37 changes: 12 additions & 25 deletions src/gui/sidebar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,52 +25,39 @@ export class OsrSidebar {
}

redraw(): void {
if (this.getActiveLeaf(REVIEW_QUEUE_VIEW_TYPE)) this.reviewQueueListView.redraw();
this.reviewQueueListView.redraw();
}

private getActiveLeaf(type: string): WorkspaceLeaf | null {
const leaves = this.app.workspace.getLeavesOfType(type);
if (leaves.length == 0) {
return null;
return this.app.workspace.getRightLeaf(false);
}

return leaves[0];
}

async init(): Promise<void> {
init(): void {
this.plugin.registerView(REVIEW_QUEUE_VIEW_TYPE, (leaf) => {
return (this.reviewQueueListView = new ReviewQueueListView(
leaf,
this.app,
this.nextNoteReviewHandler,
this.settings,
));
});

if (
this.settings.enableNoteReviewPaneOnStartup &&
this.getActiveLeaf(REVIEW_QUEUE_VIEW_TYPE) == null
) {
await this.activateReviewQueueViewPanel();
}
}

private async activateReviewQueueViewPanel(): Promise<void> {
await this.app.workspace.getRightLeaf(false).setViewState({
type: REVIEW_QUEUE_VIEW_TYPE,
active: true,
});
async activateReviewQueueViewPanel(): Promise<void> {
if (this.settings.enableNoteReviewPaneOnStartup) {
await this.getActiveLeaf(REVIEW_QUEUE_VIEW_TYPE).setViewState({
type: REVIEW_QUEUE_VIEW_TYPE,
active: true,
});
}
}

async openReviewQueueView(): Promise<void> {
let reviewQueueLeaf = this.getActiveLeaf(REVIEW_QUEUE_VIEW_TYPE);
if (reviewQueueLeaf == null) {
await this.activateReviewQueueViewPanel();
reviewQueueLeaf = this.getActiveLeaf(REVIEW_QUEUE_VIEW_TYPE);
}

if (reviewQueueLeaf !== null) {
this.app.workspace.revealLeaf(reviewQueueLeaf);
}
const reviewQueueLeaf = this.getActiveLeaf(REVIEW_QUEUE_VIEW_TYPE);
this.app.workspace.revealLeaf(reviewQueueLeaf);
}
}
5 changes: 1 addition & 4 deletions src/gui/statistics.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -143,10 +143,7 @@ export class StatisticsView {
t("INTERVALS_DESC"),
Object.keys(cardStats.intervals.dict),
Object.values(cardStats.intervals.dict),
t("INTERVALS_SUMMARY", {
avg: averageInterval,
longest: longestInterval,
}),
t("INTERVALS_SUMMARY", { avg: averageInterval, longest: longestInterval }),
t("COUNT"),
t("DAYS"),
t("NUMBER_OF_CARDS"),
Expand Down
43 changes: 20 additions & 23 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import { t } from "src/lang/helpers";
import { NextNoteReviewHandler } from "src/next-note-review-handler";
import { Note } from "src/note";
import { NoteFileLoader } from "src/note-file-loader";
import { NoteReviewQueue } from "src/note-review-queue";
import { setDebugParser } from "src/parser";
import { DEFAULT_DATA, PluginData } from "src/plugin-data";
import { QuestionPostponementList } from "src/question-postponement-list";
Expand All @@ -57,12 +58,24 @@ export default class SRPlugin extends Plugin {
async onload(): Promise<void> {
await this.loadPluginData();

this.initLogicClasses();
const noteReviewQueue = new NoteReviewQueue();
this.nextNoteReviewHandler = new NextNoteReviewHandler(
this.app,
this.data.settings,
noteReviewQueue,
);

this.initGuiItems();
}
this.osrSidebar = new OsrSidebar(this, this.data.settings, this.nextNoteReviewHandler);
this.osrSidebar.init();
this.app.workspace.onLayoutReady(async () => {
await this.osrSidebar.activateReviewQueueViewPanel();
setTimeout(async () => {
if (!this.osrAppCore.syncLock) {
await this.sync();
}
}, 2000);
});

private initLogicClasses() {
const questionPostponementList: QuestionPostponementList = new QuestionPostponementList(
this,
this.data.settings,
Expand All @@ -78,16 +91,9 @@ export default class SRPlugin extends Plugin {
osrNoteLinkInfoFinder,
this.data.settings,
this.onOsrVaultDataChanged.bind(this),
noteReviewQueue,
);
}

private initGuiItems() {
this.nextNoteReviewHandler = new NextNoteReviewHandler(
this.app,
this.data.settings,
this.app.workspace,
this.osrAppCore.noteReviewQueue,
);
appIcon();

this.showStatusBar(this.data.settings.showStatusBar);
Expand All @@ -99,16 +105,6 @@ export default class SRPlugin extends Plugin {
this.addPluginCommands();

this.addSettingTab(new SRSettingTab(this.app, this));

this.osrSidebar = new OsrSidebar(this, this.data.settings, this.nextNoteReviewHandler);
this.app.workspace.onLayoutReady(async () => {
await this.osrSidebar.init();
setTimeout(async () => {
if (!this.osrAppCore.syncLock) {
await this.sync();
}
}, 2000);
});
}

showFileMenuItems(status: boolean) {
Expand Down Expand Up @@ -355,7 +351,8 @@ export default class SRPlugin extends Plugin {
),
}),
);
this.osrSidebar.redraw();

if (this.data.settings.enableNoteReviewPaneOnStartup) this.osrSidebar.redraw();
}

async loadNote(noteFile: TFile): Promise<Note> {
Expand Down
11 changes: 2 additions & 9 deletions src/next-note-review-handler.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { App, Notice, TFile, Workspace } from "obsidian";
import { App, Notice, TFile } from "obsidian";

import { ReviewDeckSelectionModal } from "src/gui/review-deck-selection-modal";
import { t } from "src/lang/helpers";
Expand All @@ -8,7 +8,6 @@ import { SRSettings } from "src/settings";
export class NextNoteReviewHandler {
private app: App;
private settings: SRSettings;
private workspace: Workspace;
private _noteReviewQueue: NoteReviewQueue;
private _lastSelectedReviewDeck: string;

Expand All @@ -20,15 +19,9 @@ export class NextNoteReviewHandler {
return this._noteReviewQueue;
}

constructor(
app: App,
settings: SRSettings,
workspace: Workspace,
noteReviewQueue: NoteReviewQueue,
) {
constructor(app: App, settings: SRSettings, noteReviewQueue: NoteReviewQueue) {
this.app = app;
this.settings = settings;
this.workspace = workspace;
this._noteReviewQueue = noteReviewQueue;
}

Expand Down
9 changes: 8 additions & 1 deletion tests/unit/helpers/unit-test-core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import * as fs from "fs";
import * as path from "path";

import { OsrCore } from "src/core";
import { NoteReviewQueue } from "src/note-review-queue";
import { QuestionPostponementList } from "src/question-postponement-list";
import { SRSettings } from "src/settings";

Expand All @@ -24,7 +25,13 @@ export class UnitTestOsrCore extends OsrCore {
settings,
this.buryList,
);
this.init(questionPostponementList, this.infoFinder, settings, () => {});
this.init(
questionPostponementList,
this.infoFinder,
settings,
() => {},
new NoteReviewQueue(),
);
}

// Needed for unit testing: Setup fileMap and the link "info finder"
Expand Down

0 comments on commit 8f976c2

Please sign in to comment.