Skip to content

Commit

Permalink
Merge pull request #258 from st3v3nmw/develop
Browse files Browse the repository at this point in the history
Multiple review queues fixes
  • Loading branch information
st3v3nmw authored Aug 19, 2021
2 parents a80819a + b3ef596 commit 09e08ae
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 114 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,4 @@
"dependencies": {
"pagerank.js": "^1.0.2"
}
}
}
48 changes: 15 additions & 33 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -195,8 +195,11 @@ export default class SRPlugin extends Plugin {
this.addCommand({
id: "srs-view-stats",
name: t("View statistics"),
callback: () => {
new StatsModal(this.app, this.dueDatesFlashcards, this).open();
callback: async () => {
if (!this.flashcardsSyncLock) {
await this.flashcards_sync();
new StatsModal(this.app, this.dueDatesFlashcards, this).open();
}
},
});

Expand Down Expand Up @@ -317,29 +320,13 @@ export default class SRPlugin extends Plugin {

if (dueUnix <= now) {
this.dueNotesCount++;

for (let tag of tags) {
if (this.reviewDecks.hasOwnProperty(tag)) {
this.reviewDecks[tag].dueNotesCount++;
}
}
}

let nDays: number = Math.ceil((dueUnix - now) / (24 * 3600 * 1000));
if (!this.dueDatesNotes.hasOwnProperty(nDays)) {
this.dueDatesNotes[nDays] = 0;
}
this.dueDatesNotes[nDays]++;

for (let tag of tags) {
if (this.reviewDecks.hasOwnProperty(tag)) {
let deck = this.reviewDecks[tag];
if (!deck.dueDatesNotes.hasOwnProperty(nDays)) {
deck.dueDatesNotes[nDays] = 0;
}
deck.dueDatesNotes[nDays]++;
}
}
}

graph.rank(0.85, 0.000001, (node: string, rank: number) => {
Expand All @@ -361,9 +348,7 @@ export default class SRPlugin extends Plugin {
});

for (let deckKey in this.reviewDecks) {
if (this.reviewDecks.hasOwnProperty(deckKey)) {
this.reviewDecks[deckKey].sortNotes(this.pageranks);
}
this.reviewDecks[deckKey].sortNotes(this.pageranks);
}

let noteCountText: string = this.dueNotesCount === 1 ? t("note") : t("notes");
Expand Down Expand Up @@ -520,7 +505,7 @@ export default class SRPlugin extends Plugin {
}

async reviewNextNoteModal(): Promise<void> {
let reviewDeckNames = Object.keys(this.reviewDecks);
let reviewDeckNames: string[] = Object.keys(this.reviewDecks);
if (reviewDeckNames.length === 1) {
this.reviewNextNote(reviewDeckNames[0]);
} else {
Expand All @@ -539,9 +524,9 @@ export default class SRPlugin extends Plugin {
this.lastSelectedReviewDeck = deckKey;
let deck = this.reviewDecks[deckKey];

if (deck.dueNotesCount > 0) {
if (deck.scheduledNotes.length > 0) {
let index = this.data.settings.openRandomNote
? Math.floor(Math.random() * deck.dueNotesCount)
? Math.floor(Math.random() * deck.scheduledNotes.length)
: 0;
this.app.workspace.activeLeaf.openFile(deck.scheduledNotes[index].note);
return;
Expand Down Expand Up @@ -601,15 +586,12 @@ export default class SRPlugin extends Plugin {
let fileCachedData = this.app.metadataCache.getFileCache(note) || {};
let tags = getAllTags(fileCachedData) || [];

for (let tag of tags) {
if (
this.data.settings.flashcardTags.some(
(tagToReview) =>
tag === tagToReview || tag.startsWith(tagToReview + "/")
)
) {
deckPath = tag.substring(1).split("/");
break;
outer: for (let tagToReview of this.data.settings.flashcardTags) {
for (let tag of tags) {
if (tag === tagToReview || tag.startsWith(tagToReview + "/")) {
deckPath = tag.substring(1).split("/");
break outer;
}
}
}
}
Expand Down
8 changes: 5 additions & 3 deletions src/review-deck.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
import { App, FuzzySuggestModal, TFile } from "obsidian";
import { SchedNote } from "./main";

import { SchedNote } from "src/main";
import { t } from "src/lang/helpers";

export class ReviewDeck {
public deckName: string;
public newNotes: TFile[] = [];
public scheduledNotes: SchedNote[] = [];
public dueNotesCount: number;
public dueDatesNotes: Record<number, number> = {}; // Record<# of days in future, due count>
public activeFolders: Set<string>;

constructor(name: string) {
this.deckName = name;
this.activeFolders = new Set([t("Today")]);
}

public sortNotes(pageranks: Record<string, number>) {
Expand Down
142 changes: 65 additions & 77 deletions src/sidebar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,18 @@ import { ItemView, WorkspaceLeaf, Menu, TFile } from "obsidian";

import type SRPlugin from "src/main";
import { COLLAPSE_ICON } from "src/constants";
import { ReviewDeck } from "src/review-deck";
import { t } from "src/lang/helpers";

export const REVIEW_QUEUE_VIEW_TYPE: string = "review-queue-list-view";

export class ReviewQueueListView extends ItemView {
private plugin: SRPlugin;
private activeFolders: Set<string>;

constructor(leaf: WorkspaceLeaf, plugin: SRPlugin) {
super(leaf);

this.plugin = plugin;
this.activeFolders = new Set([t("Today")]);
this.registerEvent(this.app.workspace.on("file-open", (_: any) => this.redraw()));
this.registerEvent(this.app.vault.on("rename", (_: any) => this.redraw()));
}
Expand Down Expand Up @@ -47,78 +46,73 @@ export class ReviewQueueListView extends ItemView {
let rootEl: HTMLElement = createDiv("nav-folder mod-root"),
childrenEl: HTMLElement = rootEl.createDiv("nav-folder-children");

if (Object.keys(this.plugin.reviewDecks).length > 0) {
for (let deckKey in this.plugin.reviewDecks) {
if (this.plugin.reviewDecks.hasOwnProperty(deckKey)) {
let deck = this.plugin.reviewDecks[deckKey];

let deckFolderEl: HTMLElement = this.createRightPaneFolder(
childrenEl,
deckKey,
!this.activeFolders.has(deckKey),
true
for (let deckKey in this.plugin.reviewDecks) {
let deck: ReviewDeck = this.plugin.reviewDecks[deckKey];

let deckFolderEl: HTMLElement = this.createRightPaneFolder(
childrenEl,
deckKey,
false,
deck
).getElementsByClassName("nav-folder-children")[0] as HTMLElement;

if (deck.newNotes.length > 0) {
let newNotesFolderEl: HTMLElement = this.createRightPaneFolder(
deckFolderEl,
t("New"),
!deck.activeFolders.has(t("New")),
deck
);

for (let newFile of deck.newNotes) {
this.createRightPaneFile(
newNotesFolderEl,
newFile,
openFile! && newFile.path === openFile.path,
!deck.activeFolders.has(t("New"))
);
}
}

if (deck.newNotes.length > 0) {
let newNotesFolderEl: HTMLElement = this.createRightPaneFolder(
deckFolderEl,
"New",
!this.activeFolders.has("New")
);
if (deck.scheduledNotes.length > 0) {
let now: number = Date.now();
let currUnix: number = -1;
let schedFolderEl: HTMLElement | null = null,
folderTitle: string = "";
let maxDaysToRender: number = this.plugin.data.settings.maxNDaysNotesReviewQueue;

for (let newFile of deck.newNotes) {
this.createRightPaneFile(
newNotesFolderEl,
newFile,
openFile! && newFile.path === openFile.path,
!this.activeFolders.has("New")
);
}
}
for (let sNote of deck.scheduledNotes) {
if (sNote.dueUnix != currUnix) {
let nDays: number = Math.ceil((sNote.dueUnix - now) / (24 * 3600 * 1000));

if (deck.scheduledNotes.length > 0) {
let now: number = Date.now();
let currUnix: number = -1;
let schedFolderEl: HTMLElement | null = null,
folderTitle: string = "";
let maxDaysToRender: number =
this.plugin.data.settings.maxNDaysNotesReviewQueue;

for (let sNote of deck.scheduledNotes) {
if (sNote.dueUnix != currUnix) {
let nDays: number = Math.ceil(
(sNote.dueUnix - now) / (24 * 3600 * 1000)
);

if (nDays > maxDaysToRender) {
break;
}

folderTitle =
nDays == -1
? "Yesterday"
: nDays == 0
? "Today"
: nDays == 1
? "Tomorrow"
: new Date(sNote.dueUnix).toDateString();

schedFolderEl = this.createRightPaneFolder(
deckFolderEl,
folderTitle,
!this.activeFolders.has(folderTitle)
);
currUnix = sNote.dueUnix;
}

this.createRightPaneFile(
schedFolderEl!,
sNote.note,
openFile! && sNote.note.path === openFile.path,
!this.activeFolders.has(folderTitle)
);
if (nDays > maxDaysToRender) {
break;
}

folderTitle =
nDays == -1
? t("Yesterday")
: nDays == 0
? t("Today")
: nDays == 1
? t("Tomorrow")
: new Date(sNote.dueUnix).toDateString();

schedFolderEl = this.createRightPaneFolder(
deckFolderEl,
folderTitle,
!deck.activeFolders.has(folderTitle),
deck
);
currUnix = sNote.dueUnix;
}

this.createRightPaneFile(
schedFolderEl!,
sNote.note,
openFile! && sNote.note.path === openFile.path,
!deck.activeFolders.has(folderTitle)
);
}
}
}
Expand All @@ -132,14 +126,8 @@ export class ReviewQueueListView extends ItemView {
parentEl: HTMLElement,
folderTitle: string,
collapsed: boolean,
isRoot: boolean = false
deck: ReviewDeck
): HTMLElement {
if (!isRoot) {
parentEl = parentEl
.getElementsByClassName("nav-folder-children")[0]
.createDiv("nav-folder");
}

let folderEl: HTMLDivElement = parentEl.createDiv("nav-folder"),
folderTitleEl: HTMLDivElement = folderEl.createDiv("nav-folder-title"),
childrenEl: HTMLDivElement = folderEl.createDiv("nav-folder-children"),
Expand All @@ -160,11 +148,11 @@ export class ReviewQueueListView extends ItemView {
child.style.display = "none";
(collapseIconEl.childNodes[0] as HTMLElement).style.transform =
"rotate(-90deg)";
this.activeFolders.delete(folderTitle);
deck.activeFolders.delete(folderTitle);
} else {
child.style.display = "block";
(collapseIconEl.childNodes[0] as HTMLElement).style.transform = "";
this.activeFolders.add(folderTitle);
deck.activeFolders.add(folderTitle);
}
}
});
Expand Down
1 change: 1 addition & 0 deletions src/stats-modal.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Modal, App, MarkdownRenderer, Platform } from "obsidian";

import type SRPlugin from "src/main";
import { getKeysPreserveType } from "src/utils";
import { t } from "src/lang/helpers";
Expand Down

0 comments on commit 09e08ae

Please sign in to comment.