Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refresh flashcard modal after edit and general QOL improvements to editing #1010

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 46 additions & 4 deletions src/FlashcardReviewSequencer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { CardScheduleInfo, ICardScheduleCalculator } from "./CardSchedule";
import { Note } from "./Note";
import { IDeckTreeIterator } from "./DeckTreeIterator";
import { IQuestionPostponementList } from "./QuestionPostponementList";
import { CardFrontBack, CardFrontBackUtil } from "./QuestionType";

export interface IFlashcardReviewSequencer {
get hasCurrentCard(): boolean;
Expand All @@ -23,7 +24,7 @@ export interface IFlashcardReviewSequencer {
skipCurrentCard(): void;
determineCardSchedule(response: ReviewResponse, card: Card): CardScheduleInfo;
processReview(response: ReviewResponse): Promise<void>;
updateCurrentQuestionText(text: string): Promise<void>;
updateCurrentQuestionTextAndCards(text: string): Promise<void>;
}

export class DeckStats {
Expand Down Expand Up @@ -207,11 +208,52 @@ export class FlashcardReviewSequencer implements IFlashcardReviewSequencer {
return result;
}

async updateCurrentQuestionText(text: string): Promise<void> {
const q: QuestionText = this.currentQuestion.questionText;
async updateCurrentQuestionTextAndCards(text: string): Promise<void> {
const question = this.currentQuestion;
const q: QuestionText = question.questionText;

// Update front/back properties of all cards which question is linked to
const cardFrontBackList: CardFrontBack[] = CardFrontBackUtil.expand(
question.questionType,
text,
this.settings,
);

// Prevent the edit if the number of cards linked to the question
// would be changed on edit
if (cardFrontBackList.length != question.cards.length) {
throw new CardLengthMismatchError(
"Mismatch between number of cards generated and current number of cards",
);
}

q.actualQuestion = text;
cardFrontBackList.forEach(({ front, back }) => {
if (front.length == 0 || back.length == 0) {
throw new CardFrontBackMissingError("Card's front or back has length of 0.");
}
});

q.actualQuestion = text;
await this.currentQuestion.writeQuestion(this.settings);

question.cards.forEach((card, i) => {
const { front, back } = cardFrontBackList[i];
card.front = front;
card.back = back;
});
}
}

export class CardLengthMismatchError extends Error {
constructor(message: string) {
super(message);
this.name = "CardLengthMismatchError";
}
}

export class CardFrontBackMissingError extends Error {
constructor(message: string) {
super(message);
this.name = "CardFrontBackMissingError";
}
}
17 changes: 14 additions & 3 deletions src/gui/FlashcardModal.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Modal, App } from "obsidian";
import { Modal, App, Notice } from "obsidian";
// eslint-disable-next-line @typescript-eslint/no-unused-vars
import h from "vhtml";

Expand All @@ -8,12 +8,15 @@ import { SRSettings } from "src/settings";
import { Deck } from "../Deck";
import { Question } from "../Question";
import {
CardFrontBackMissingError,
CardLengthMismatchError,
FlashcardReviewMode,
IFlashcardReviewSequencer as IFlashcardReviewSequencer,
} from "src/FlashcardReviewSequencer";
import { FlashcardEditModal } from "./EditModal";
import { DeckListView } from "./DeckListView";
import { FlashcardReviewView } from "./FlashcardReviewView";
import { t } from "src/lang/helpers";

export enum FlashcardModalMode {
DecksList,
Expand Down Expand Up @@ -121,8 +124,16 @@ export class FlashcardModal extends Modal {
const editModal = FlashcardEditModal.Prompt(this.app, textPrompt);
editModal
.then(async (modifiedCardText) => {
this.reviewSequencer.updateCurrentQuestionText(modifiedCardText);
await this.reviewSequencer.updateCurrentQuestionTextAndCards(modifiedCardText);
this.flashcardView.rerenderCardContents();
})
.catch((reason) => console.log(reason));
.catch((reason) => {
if (reason instanceof CardLengthMismatchError) {
new Notice(t("CARD_LENGTH_MISMATCH_NOTICE"));
} else if (reason instanceof CardFrontBackMissingError) {
new Notice(t("CARD_FRONT_BACK_MISSING_NOTICE"));
}
console.log(reason);
});
}
}
67 changes: 46 additions & 21 deletions src/gui/FlashcardReviewView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -131,12 +131,7 @@ export class FlashcardReviewView {

// Setup card content
this.content.empty();
const wrapper: RenderMarkdownWrapper = new RenderMarkdownWrapper(
this.app,
this.plugin,
this._currentNote.filePath,
);
await wrapper.renderMarkdownWrapper(this._currentCard.front, this.content);
await this._renderMarkdownFront();

// Setup response buttons
this._resetResponseButtons();
Expand All @@ -150,6 +145,30 @@ export class FlashcardReviewView {
document.addEventListener("keydown", this._keydownHandler);
}

/**
* If the card is already being shown, re-render just the Markdown
* contents if needed
*/
async rerenderCardContents() {
this.content.empty();
this._renderMarkdownFront();
if (this.mode == FlashcardModalMode.Back) {
this._renderMarkdownBackAndDivider();
}
}

/**
* Set up the Markdown rendering for the card's front side
*/
private async _renderMarkdownFront() {
const wrapper: RenderMarkdownWrapper = new RenderMarkdownWrapper(
this.app,
this.plugin,
this._currentNote.filePath,
);
await wrapper.renderMarkdownWrapper(this._currentCard.front, this.content);
}

/**
* Hides the FlashcardView
*/
Expand Down Expand Up @@ -276,21 +295,7 @@ export class FlashcardReviewView {

this.resetButton.disabled = false;

// Show answer text
if (this._currentQuestion.questionType !== CardType.Cloze) {
const hr: HTMLElement = document.createElement("hr");
hr.addClass("sr-card-divide");
this.content.appendChild(hr);
} else {
this.content.empty();
}

const wrapper: RenderMarkdownWrapper = new RenderMarkdownWrapper(
this.app,
this.plugin,
this._currentNote.filePath,
);
wrapper.renderMarkdownWrapper(this._currentCard.back, this.content);
this._renderMarkdownBackAndDivider();

// Show response buttons
this.answerButton.addClass("sr-is-hidden");
Expand Down Expand Up @@ -321,6 +326,26 @@ export class FlashcardReviewView {
}
}

/**
* Setup Markdown rendering for the card's back side and the divider
*/
private _renderMarkdownBackAndDivider() {
if (this._currentQuestion.questionType !== CardType.Cloze) {
const hr: HTMLElement = document.createElement("hr");
hr.addClass("sr-card-divide");
this.content.appendChild(hr);
} else {
this.content.empty();
}

const wrapper: RenderMarkdownWrapper = new RenderMarkdownWrapper(
this.app,
this.plugin,
this._currentNote.filePath,
);
wrapper.renderMarkdownWrapper(this._currentCard.back, this.content);
}

private async _processReview(response: ReviewResponse): Promise<void> {
await this.reviewSequencer.processReview(response);
await this._handleSkipCard();
Expand Down
3 changes: 3 additions & 0 deletions src/lang/locale/ar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ export default {
CURRENT_EASE_HELP_TEXT: ":السهولة الحالية",
CURRENT_INTERVAL_HELP_TEXT: ":الفاصل الزمني الحالي",
CARD_GENERATED_FROM: "${notePath} :تم إنشاؤها من",
CARD_LENGTH_MISMATCH_NOTICE:
"Unable to update flashcard. The number of cards after the edit does not match the original number of cards.",
CARD_FRONT_BACK_MISSING_NOTICE: "Unable to update flashcard. The front or back is missing.",

// main.ts
OPEN_NOTE_FOR_REVIEW: "افتح الملاحظة للمراجعة",
Expand Down
3 changes: 3 additions & 0 deletions src/lang/locale/cz.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ export default {
CURRENT_EASE_HELP_TEXT: "Current Ease: ",
CURRENT_INTERVAL_HELP_TEXT: "Current Interval: ",
CARD_GENERATED_FROM: "Generated from: ${notePath}",
CARD_LENGTH_MISMATCH_NOTICE:
"Unable to update flashcard. The number of cards after the edit does not match the original number of cards.",
CARD_FRONT_BACK_MISSING_NOTICE: "Unable to update flashcard. The front or back is missing.",

// main.ts
OPEN_NOTE_FOR_REVIEW: "Otevřít poznámku k revizi",
Expand Down
3 changes: 3 additions & 0 deletions src/lang/locale/de.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ export default {
CURRENT_EASE_HELP_TEXT: "Aktuelle Schwierigkeit: ",
CURRENT_INTERVAL_HELP_TEXT: "Aktueller Intervall: ",
CARD_GENERATED_FROM: "Erstellt von: ${notePath}",
CARD_LENGTH_MISMATCH_NOTICE:
"Unable to update flashcard. The number of cards after the edit does not match the original number of cards.",
CARD_FRONT_BACK_MISSING_NOTICE: "Unable to update flashcard. The front or back is missing.",

// main.ts
OPEN_NOTE_FOR_REVIEW: "Notiz zur Wiederholung öffnen",
Expand Down
3 changes: 3 additions & 0 deletions src/lang/locale/en.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ export default {
CURRENT_EASE_HELP_TEXT: "Current Ease: ",
CURRENT_INTERVAL_HELP_TEXT: "Current Interval: ",
CARD_GENERATED_FROM: "Generated from: ${notePath}",
CARD_LENGTH_MISMATCH_NOTICE:
"Unable to update flashcard. The number of cards after the edit does not match the original number of cards.",
CARD_FRONT_BACK_MISSING_NOTICE: "Unable to update flashcard. The front or back is missing.",

// main.ts
OPEN_NOTE_FOR_REVIEW: "Open a note for review",
Expand Down
3 changes: 3 additions & 0 deletions src/lang/locale/es.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ export default {
CURRENT_EASE_HELP_TEXT: "Facilidad Actual: ",
CURRENT_INTERVAL_HELP_TEXT: "Intervalo Actual: ",
CARD_GENERATED_FROM: "Generado Desde: ${notePath}",
CARD_LENGTH_MISMATCH_NOTICE:
"Unable to update flashcard. The number of cards after the edit does not match the original number of cards.",
CARD_FRONT_BACK_MISSING_NOTICE: "Unable to update flashcard. The front or back is missing.",

// main.ts
OPEN_NOTE_FOR_REVIEW: "Abrir nota para revisión",
Expand Down
3 changes: 3 additions & 0 deletions src/lang/locale/it.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ export default {
CURRENT_EASE_HELP_TEXT: "Difficoltà attuale: ",
CURRENT_INTERVAL_HELP_TEXT: "Intervallo attuale: ",
CARD_GENERATED_FROM: "Generato da: ${notePath}",
CARD_LENGTH_MISMATCH_NOTICE:
"Unable to update flashcard. The number of cards after the edit does not match the original number of cards.",
CARD_FRONT_BACK_MISSING_NOTICE: "Unable to update flashcard. The front or back is missing.",

// main.ts
OPEN_NOTE_FOR_REVIEW: "Apri una nota per rivisita",
Expand Down
3 changes: 3 additions & 0 deletions src/lang/locale/ja.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ export default {
CURRENT_EASE_HELP_TEXT: "Current Ease: ",
CURRENT_INTERVAL_HELP_TEXT: "Current Interval: ",
CARD_GENERATED_FROM: "Generated from: ${notePath}",
CARD_LENGTH_MISMATCH_NOTICE:
"Unable to update flashcard. The number of cards after the edit does not match the original number of cards.",
CARD_FRONT_BACK_MISSING_NOTICE: "Unable to update flashcard. The front or back is missing.",

// main.ts
OPEN_NOTE_FOR_REVIEW: "レビューするノートを開く",
Expand Down
3 changes: 3 additions & 0 deletions src/lang/locale/ko.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ export default {
CURRENT_EASE_HELP_TEXT: "Current Ease: ",
CURRENT_INTERVAL_HELP_TEXT: "Current Interval: ",
CARD_GENERATED_FROM: "Generated from: ${notePath}",
CARD_LENGTH_MISMATCH_NOTICE:
"Unable to update flashcard. The number of cards after the edit does not match the original number of cards.",
CARD_FRONT_BACK_MISSING_NOTICE: "Unable to update flashcard. The front or back is missing.",

// main.ts
OPEN_NOTE_FOR_REVIEW: "리뷰할 노트 열기",
Expand Down
3 changes: 3 additions & 0 deletions src/lang/locale/pl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ export default {
CURRENT_EASE_HELP_TEXT: "Aktualna łatwość: ",
CURRENT_INTERVAL_HELP_TEXT: "Aktualny interwał: ",
CARD_GENERATED_FROM: "Wygenerowano z: ${notePath}",
CARD_LENGTH_MISMATCH_NOTICE:
"Unable to update flashcard. The number of cards after the edit does not match the original number of cards.",
CARD_FRONT_BACK_MISSING_NOTICE: "Unable to update flashcard. The front or back is missing.",

// main.ts
OPEN_NOTE_FOR_REVIEW: "Otwórz notatkę do przeglądu",
Expand Down
3 changes: 3 additions & 0 deletions src/lang/locale/pt-br.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ export default {
CURRENT_EASE_HELP_TEXT: "Facilidade atual: ",
CURRENT_INTERVAL_HELP_TEXT: "Intervalo atual: ",
CARD_GENERATED_FROM: "Gerada a partir de: ${notePath}",
CARD_LENGTH_MISMATCH_NOTICE:
"Unable to update flashcard. The number of cards after the edit does not match the original number of cards.",
CARD_FRONT_BACK_MISSING_NOTICE: "Unable to update flashcard. The front or back is missing.",

// main.ts
OPEN_NOTE_FOR_REVIEW: "Abrir uma nota para revisar",
Expand Down
3 changes: 3 additions & 0 deletions src/lang/locale/ru.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ export default {
CURRENT_EASE_HELP_TEXT: "Текущий прогресс: ",
CURRENT_INTERVAL_HELP_TEXT: "Текущий интервал: ",
CARD_GENERATED_FROM: "Сгенерированно из: ${notePath}",
CARD_LENGTH_MISMATCH_NOTICE:
"Unable to update flashcard. The number of cards after the edit does not match the original number of cards.",
CARD_FRONT_BACK_MISSING_NOTICE: "Unable to update flashcard. The front or back is missing.",

// main.ts
OPEN_NOTE_FOR_REVIEW: "Открыть заметку для изучения",
Expand Down
3 changes: 3 additions & 0 deletions src/lang/locale/zh-cn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ export default {
CURRENT_EASE_HELP_TEXT: "目前掌握程度:",
CURRENT_INTERVAL_HELP_TEXT: "目前间隔:",
CARD_GENERATED_FROM: "生成自:${notePath}",
CARD_LENGTH_MISMATCH_NOTICE:
"Unable to update flashcard. The number of cards after the edit does not match the original number of cards.",
CARD_FRONT_BACK_MISSING_NOTICE: "Unable to update flashcard. The front or back is missing.",

// main.ts
OPEN_NOTE_FOR_REVIEW: "打开一个笔记开始复习",
Expand Down
3 changes: 3 additions & 0 deletions src/lang/locale/zh-tw.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ export default {
CURRENT_EASE_HELP_TEXT: "目前掌握程度:",
CURRENT_INTERVAL_HELP_TEXT: "目前間隔時間:",
CARD_GENERATED_FROM: "生成自:${notePath}",
CARD_LENGTH_MISMATCH_NOTICE:
"Unable to update flashcard. The number of cards after the edit does not match the original number of cards.",
CARD_FRONT_BACK_MISSING_NOTICE: "Unable to update flashcard. The front or back is missing.",

// main.ts
OPEN_NOTE_FOR_REVIEW: "打開一個筆記開始復習",
Expand Down
Loading