From 2428eb48c7503891c2bc0a4a7165de978c21ce45 Mon Sep 17 00:00:00 2001 From: Lutu-gl <74654393+Lutu-gl@users.noreply.github.com> Date: Sun, 2 Mar 2025 21:29:20 +0100 Subject: [PATCH] first working code for obsidian --- main.ts | 46 +++++++++++++++++++++++++++------------------- 1 file changed, 27 insertions(+), 19 deletions(-) diff --git a/main.ts b/main.ts index b083524..b7c0086 100644 --- a/main.ts +++ b/main.ts @@ -1,5 +1,4 @@ import { App, Editor, MarkdownView, Modal, Notice, Plugin, PluginSettingTab, Setting } from 'obsidian'; -// Remember to rename these classes and interfaces! interface MyPluginSettings { mySetting: string; @@ -27,6 +26,7 @@ export default class MinimalQuizPlugin extends Plugin { const qaMap = this.extractQuestionsAndAnswers(content); const entries = Array.from(qaMap.entries()); if (entries.length > 0) { + new Notice(entries.toString()); new QuestionsModal(this.app, entries).open(); } else { new Notice('No questions found.'); @@ -75,15 +75,8 @@ class QuestionsModal extends Modal { } onOpen() { - //const {contentEl, modalEl} = this; this.render(); - this.modalEl.style.backdropFilter = 'blur(10px)'; - this.modalEl.style.backgroundColor = 'rgba(0, 0, 0, 0.7)'; - this.modalEl.style.color = 'white'; - this.modalEl.style.padding = '20px'; - this.modalEl.style.borderRadius = '8px'; - - window.addEventListener('keydown', this.handleKeyDown) + window.addEventListener('keydown', this.handleKeyDown); } handleKeyDown = (event: KeyboardEvent) => { @@ -97,27 +90,42 @@ class QuestionsModal extends Modal { const { contentEl } = this; contentEl.empty(); - const [question, answer] = this.entries[this.currentIndex]; - contentEl.createEl('h2', question); - - const answerEl = contentEl.createEl('p', { text: this.answerVisible ? answer : ''}); - answerEl.style.marginTop = '20px'; + if (this.currentIndex >= this.entries.length) { + new Notice('You finished the Quiz - Good Job!'); + this.close(); + return; + } - const button = contentEl.createEl('button', { text: this.answerVisible ? 'Next Questions' : 'Show Answer'}); + const [question, answer] = this.entries[this.currentIndex]; + + const questionEl = contentEl.createEl('h2'); + questionEl.textContent = question; + + const answerEl = contentEl.createEl('p', { + text: this.answerVisible ? answer : '', + }); + answerEl.style.marginTop = '5px'; + + const button = contentEl.createEl('button', { + text: this.answerVisible ? 'Next Question' : 'Show Answer', + }); + button.style.marginTop = '20px'; button.addEventListener('click', () => this.toggleAnswer()); } - toggleAnswer(){ - if (this.answerVisible){ + toggleAnswer() { + if (this.answerVisible) { this.currentIndex++; this.answerVisible = false; - this.render(); + } else { + this.answerVisible = true; } + this.render(); } onClose() { - const {contentEl} = this; + const { contentEl } = this; contentEl.empty(); window.removeEventListener('keydown', this.handleKeyDown); }