diff --git a/src/@types/settings.d.ts b/src/@types/settings.d.ts index 456cefb..28a9bfd 100644 --- a/src/@types/settings.d.ts +++ b/src/@types/settings.d.ts @@ -1,4 +1,5 @@ export interface Card { + hash: number; filePath: string; heading: string; content: string; diff --git a/src/flashcards-Api.ts b/src/flashcards-Api.ts index ab4907e..32e8017 100644 --- a/src/flashcards-Api.ts +++ b/src/flashcards-Api.ts @@ -12,6 +12,6 @@ export class SimpleFlashcardsApi { * @returns The card embed text, or null if no cards available */ embedCard(): string | null { - return this.plugin.getCardEmbedText(); + return this.plugin.createEmbedText(); } } diff --git a/src/flashcards-CardParser.ts b/src/flashcards-CardParser.ts index d7393ed..447e703 100644 --- a/src/flashcards-CardParser.ts +++ b/src/flashcards-CardParser.ts @@ -49,9 +49,14 @@ export class CardParser { heading: heading.heading, content: cardContent, key: `${file.path}#${heading.heading}`, + hash: this.hash(Math.random()), }); } return cards; } + + hash(input: number): number { + return 31 * input; + } } diff --git a/src/flashcards-Plugin.ts b/src/flashcards-Plugin.ts index a4c1766..4ec7e78 100644 --- a/src/flashcards-Plugin.ts +++ b/src/flashcards-Plugin.ts @@ -36,31 +36,31 @@ export default class SimpleFlashcardsPlugin extends Plugin { } window.simpleFlashcards.api = this.api; - // Command: Show Random Activity Card + // Command: Show Random Card this.addCommand({ - id: "show-random-card", - name: "Show Activity Card", + id: "show-card", + name: "Show Card", callback: () => { - this.showRandomCard(); + this.showFlashCard(); }, }); - // Command: Embed Random Card + // Command: Embed Card this.addCommand({ id: "embed-card", - name: "Embed Activity Card", + name: "Embed Card", editorCallback: (editor) => { - this.insertCardEmbed(editor); + this.embedCard(editor); }, }); - // Command: Refresh Activity Cards + // Command: Refresh Cards this.addCommand({ id: "refresh-cards", - name: "Refresh Activity Cards", + name: "Refresh Cards", callback: async () => { await this.scanCards(); - new Notice("Activity cards refreshed"); + new Notice("Flash cards refreshed"); }, }); }); @@ -130,18 +130,20 @@ export default class SimpleFlashcardsPlugin extends Plugin { return null; } - if (this.settings.selectionMode === "random") { - return pool[Math.floor(Math.random() * pool.length)]; - } - // least-recent: sort by lastSeen timestamp const sorted = pool.slice().sort((a, b) => { const aTime = this.data.cardViews[a.key] || 0; const bTime = this.data.cardViews[b.key] || 0; + if (aTime === bTime) { + // consistent, pseudo-randomizing sort + return a.hash - b.hash; + } return aTime - bTime; }); - return sorted[0]; + return this.settings.selectionMode === "random" + ? pool[Math.floor(Math.random() * pool.length)] + : sorted[0]; } recordView(cardKey: string) { @@ -152,7 +154,7 @@ export default class SimpleFlashcardsPlugin extends Plugin { }); } - showRandomCard() { + showFlashCard() { const deckPath = this.settings.defaultDeckPath || undefined; const card = this.selectCard(deckPath); @@ -164,7 +166,7 @@ export default class SimpleFlashcardsPlugin extends Plugin { new FlashcardModal(this.app, this, card, deckPath).open(); } - getCardEmbedText(): string | null { + createEmbedText(): string | null { const deckPath = this.settings.defaultDeckPath || undefined; const card = this.selectCard(deckPath); @@ -190,8 +192,8 @@ export default class SimpleFlashcardsPlugin extends Plugin { return embedText; } - insertCardEmbed(editor: Editor) { - const embedText = this.getCardEmbedText(); + embedCard(editor: Editor) { + const embedText = this.createEmbedText(); if (!embedText) { new Notice("No cards available. Check your settings.");