Skip to content

Commit

Permalink
FIX: Fixed that the keylistener was still active while the sr tab was…
Browse files Browse the repository at this point in the history
… not in focus & some formatting
  • Loading branch information
Kyle Klus committed Dec 2, 2024
1 parent 1db9540 commit bc81a22
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 38 deletions.
8 changes: 6 additions & 2 deletions src/gui/card-ui.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -207,8 +207,12 @@ export class CardUI {
}

private _keydownHandler = (e: KeyboardEvent) => {
// Prevents any input, if the edit modal is open
if (document.activeElement.nodeName === "TEXTAREA" || this.mode === FlashcardMode.Closed) {
// Prevents any input, if the edit modal is open or if the view is not in focus
if (
document.activeElement.nodeName === "TEXTAREA" ||
this.mode === FlashcardMode.Closed ||
!this.plugin.getSRInFocusState()
) {
return;
}

Expand Down
32 changes: 16 additions & 16 deletions src/gui/settings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -169,20 +169,20 @@ export class SRSettingTab extends PluginSettingTab {
.addOptions(
deckOrderEnabled
? {
// eslint-disable-next-line camelcase
PrevDeckComplete_Sequential: t(
"REVIEW_DECK_ORDER_PREV_DECK_COMPLETE_SEQUENTIAL",
),
// eslint-disable-next-line camelcase
PrevDeckComplete_Random: t(
"REVIEW_DECK_ORDER_PREV_DECK_COMPLETE_RANDOM",
),
}
// eslint-disable-next-line camelcase
PrevDeckComplete_Sequential: t(
"REVIEW_DECK_ORDER_PREV_DECK_COMPLETE_SEQUENTIAL",
),
// eslint-disable-next-line camelcase
PrevDeckComplete_Random: t(
"REVIEW_DECK_ORDER_PREV_DECK_COMPLETE_RANDOM",
),
}
: {
EveryCardRandomDeckAndCard: t(
"REVIEW_DECK_ORDER_RANDOM_DECK_AND_CARD",
),
},
EveryCardRandomDeckAndCard: t(
"REVIEW_DECK_ORDER_RANDOM_DECK_AND_CARD",
),
},
)
.setValue(
deckOrderEnabled
Expand Down Expand Up @@ -1115,9 +1115,9 @@ export class SRSettingTab extends PluginSettingTab {
scrollPosition: number;
tabName: string;
} = {
scrollPosition: 0,
tabName: "main-flashcards",
};
scrollPosition: 0,
tabName: "main-flashcards",
};
private rememberLastPosition(containerElement: HTMLElement) {
const lastPosition = this.lastPosition;

Expand Down
2 changes: 1 addition & 1 deletion src/gui/sr-modal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ import {
FlashcardReviewMode,
IFlashcardReviewSequencer as IFlashcardReviewSequencer,
} from "src/flashcard-review-sequencer";
import { CardUI } from "src/gui/card-ui";
import { DeckUI } from "src/gui/deck-ui";
import { FlashcardEditModal } from "src/gui/edit-modal";
import { CardUI } from "src/gui/card-ui";
import type SRPlugin from "src/main";
import { Question } from "src/question";
import { SRSettings } from "src/settings";
Expand Down
5 changes: 3 additions & 2 deletions src/gui/sr-tab-view.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import { ItemView, WorkspaceLeaf } from "obsidian";

import { SR_TAB_VIEW } from "src/constants";
import { Deck } from "src/deck";
import { FlashcardReviewMode, IFlashcardReviewSequencer } from "src/flashcard-review-sequencer";
import { CardUI } from "src/gui/card-ui";
import { DeckUI } from "src/gui/deck-ui";
import { FlashcardEditModal } from "src/gui/edit-modal";
import { CardUI } from "src/gui/card-ui";
import SRPlugin from "src/main";
import { Question } from "src/question";
import { SRSettings } from "src/settings";
import { SR_TAB_VIEW } from "src/constants";

/**
* Represents a tab view for spaced repetition plugin.
Expand Down
15 changes: 8 additions & 7 deletions src/tab-view-manager.ts → src/gui/tab-view-manager.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { PaneType, TFile, WorkspaceLeaf } from "obsidian";

import { SR_TAB_VIEW } from "src/constants";
import { OsrAppCore } from "src/core";
import { Deck } from "src/deck";
Expand Down Expand Up @@ -67,7 +68,7 @@ export default class TabViewManager {
this.plugin = plugin;
this.shouldOpenSingeNoteTabView = false;

this._registerAllTabViews();
this.registerAllTabViews();
}

/**
Expand All @@ -93,7 +94,7 @@ export default class TabViewManager {
this.shouldOpenSingeNoteTabView = singleNote !== undefined;
if (singleNote) this.chosenSingleNoteForTabbedView = singleNote;

await this._openTabView(SR_TAB_VIEW, true);
await this.openTabView(SR_TAB_VIEW, true);
}

/**
Expand All @@ -103,22 +104,22 @@ export default class TabViewManager {
* their corresponding leaves from the workspace, effectively closing them.
*/
public closeAllTabViews() {
this._forEachTabViewType((viewType) => {
this.forEachTabViewType((viewType) => {
this.plugin.app.workspace.detachLeavesOfType(viewType.type);
});
}

private _forEachTabViewType(callback: (type: TabViewType) => void) {
public forEachTabViewType(callback: (type: TabViewType) => void) {
this.tabViewTypes.forEach((type) => callback(type));
}

private _registerAllTabViews() {
this._forEachTabViewType((viewType) =>
public registerAllTabViews() {
this.forEachTabViewType((viewType) =>
this.plugin.registerView(viewType.type, viewType.viewCreator),
);
}

private async _openTabView(type: string, newLeaf?: PaneType | boolean) {
public async openTabView(type: string, newLeaf?: PaneType | boolean) {
const { workspace } = this.plugin.app;

let leaf: WorkspaceLeaf | null = null;
Expand Down
22 changes: 12 additions & 10 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,12 @@ import {
FlashcardReviewSequencer,
IFlashcardReviewSequencer,
} from "src/flashcard-review-sequencer";
import { FlashcardModal } from "src/gui/sr-modal";
import { REVIEW_QUEUE_VIEW_TYPE } from "src/gui/review-queue-list-view";
import { SRSettingTab } from "src/gui/settings";
import { OsrSidebar } from "src/gui/sidebar";
import { FlashcardModal } from "src/gui/sr-modal";
import { SRTabView } from "src/gui/sr-tab-view";
import TabViewManager from "src/gui/tab-view-manager";
import { appIcon } from "src/icons/app-icon";
import { t } from "src/lang/helpers";
import { NextNoteReviewHandler } from "src/next-note-review-handler";
Expand All @@ -39,8 +41,6 @@ import { QuestionPostponementList } from "src/question-postponement-list";
import { DEFAULT_SETTINGS, SettingsUtil, SRSettings, upgradeSettings } from "src/settings";
import { TopicPath } from "src/topic-path";
import { convertToStringOrEmpty, TextDirection } from "src/utils/strings";
import TabViewManager from "src/tab-view-manager";
import { SRTabView } from "./gui/sr-tab-view";

export default class SRPlugin extends Plugin {
public data: PluginData;
Expand Down Expand Up @@ -353,23 +353,25 @@ export default class SRPlugin extends Plugin {
}

public registerSRFocusListener() {
this.registerEvent(this.app.workspace.on("active-leaf-change", this.handleFocusChange));
this.registerEvent(
this.app.workspace.on("active-leaf-change", this.handleFocusChange.bind(this)),
);
}

public removeSRFocusListener() {
this.setSRViewInFocus(false);
this.app.workspace.off("active-leaf-change", this.handleFocusChange);
this.app.workspace.off("active-leaf-change", this.handleFocusChange.bind(this));
}

public handleFocusChange(leaf: WorkspaceLeaf | null) {
this.setSRViewInFocus(leaf?.view instanceof SRTabView);
this.setSRViewInFocus(leaf !== null && leaf.view instanceof SRTabView);
}

public setSRViewInFocus(value: boolean) {
this.isSRInFocus = value;
}

public getSRFocusState(): boolean {
public getSRInFocusState(): boolean {
return this.isSRInFocus;
}

Expand Down Expand Up @@ -435,9 +437,9 @@ export default class SRPlugin extends Plugin {
console.log(`SR: ${t("DECKS")}`, this.osrAppCore.reviewableDeckTree);
console.log(
"SR: " +
t("SYNC_TIME_TAKEN", {
t: Date.now() - now.valueOf(),
}),
t("SYNC_TIME_TAKEN", {
t: Date.now() - now.valueOf(),
}),
);
}
}
Expand Down

0 comments on commit bc81a22

Please sign in to comment.