mirror of
https://github.com/lutu-gl/Obsidian-Minimal-Quiz.git
synced 2026-07-22 05:42:37 +00:00
added modal and better question/answer extraction
This commit is contained in:
parent
fadab9aad8
commit
413d98876d
1 changed files with 74 additions and 137 deletions
211
main.ts
211
main.ts
|
|
@ -15,109 +15,46 @@ export default class MinimalQuizPlugin extends Plugin {
|
|||
async onload() {
|
||||
await this.loadSettings();
|
||||
this.addRibbonIcon('dice', 'Greet', () => {
|
||||
new Notice('Hello, world!');
|
||||
new Notice('Hello, world!2');
|
||||
});
|
||||
|
||||
this.addCommand({
|
||||
id: 'show-questions-modal',
|
||||
name: 'Start quiz on current file',
|
||||
editorCallback: (editor: Editor, view: MarkdownView) => {
|
||||
const content = editor.getValue();
|
||||
const matches = [...content.matchAll(/(.*?)(?=\?)/g)];
|
||||
const results = matches.map(match => match[1].trim()).filter(Boolean);
|
||||
if (results.length > 0) {
|
||||
new QuestionsModal(this.app, results).open();
|
||||
} else {
|
||||
new Notice('No questions found.');
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
this.addCommand({
|
||||
id: 'find-text-before-question-marks',
|
||||
name: 'Find text before question marks',
|
||||
editorCallback: (editor: Editor, view: MarkdownView) => {
|
||||
const content = editor.getValue();
|
||||
const matches = [...content.matchAll(/(.*?)(?=\?)/g)];
|
||||
const results = matches.map(match => match[1].trim()).filter(Boolean);
|
||||
|
||||
if (results.length > 0) {
|
||||
new Notice(`Found: \n${results.join('\n')}`);
|
||||
} else {
|
||||
new Notice('No questions found.');
|
||||
this.addCommand({
|
||||
id: 'show-questions-modal',
|
||||
name: 'Start quiz on current file',
|
||||
editorCallback: (editor: Editor, view: MarkdownView) => {
|
||||
new Notice('Yup worked');
|
||||
const content = editor.getValue();
|
||||
const qaMap = this.extractQuestionsAndAnswers(content);
|
||||
const entries = Array.from(qaMap.entries());
|
||||
if (entries.length > 0) {
|
||||
new QuestionsModal(this.app, entries).open();
|
||||
} else {
|
||||
new Notice('No questions found.');
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
extractQuestionsAndAnswers(content: string): Map<string, string> {
|
||||
const qaMap = new Map<string, string>();
|
||||
const regex = /(.*\?)\n([\s\S]*?)(?=\n\n|$)/g;
|
||||
let match;
|
||||
|
||||
while ((match = regex.exec(content)) !== null) {
|
||||
const question = match[1].trim();
|
||||
const answer = match[2].trim();
|
||||
if (question && answer) {
|
||||
qaMap.set(question, answer);
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
async backup(){
|
||||
// This creates an icon in the left ribbon.
|
||||
const ribbonIconEl = this.addRibbonIcon('dice', 'Sample Plugin', (evt: MouseEvent) => {
|
||||
// Called when the user clicks the icon.
|
||||
new Notice('This is a notice!');
|
||||
});
|
||||
// Perform additional things with the ribbon
|
||||
ribbonIconEl.addClass('my-plugin-ribbon-class');
|
||||
|
||||
// This adds a status bar item to the bottom of the app. Does not work on mobile apps.
|
||||
const statusBarItemEl = this.addStatusBarItem();
|
||||
statusBarItemEl.setText('Status Bar Text');
|
||||
|
||||
// This adds a simple command that can be triggered anywhere
|
||||
this.addCommand({
|
||||
id: 'open-sample-modal-simple',
|
||||
name: 'Open sample modal (simple)',
|
||||
callback: () => {
|
||||
new SampleModal(this.app).open();
|
||||
}
|
||||
});
|
||||
// This adds an editor command that can perform some operation on the current editor instance
|
||||
this.addCommand({
|
||||
id: 'sample-editor-command',
|
||||
name: 'Sample editor command',
|
||||
editorCallback: (editor: Editor, view: MarkdownView) => {
|
||||
console.log(editor.getSelection());
|
||||
editor.replaceSelection('Sample Editor Command');
|
||||
}
|
||||
});
|
||||
// This adds a complex command that can check whether the current state of the app allows execution of the command
|
||||
this.addCommand({
|
||||
id: 'open-sample-modal-complex',
|
||||
name: 'Open sample modal (complex)',
|
||||
checkCallback: (checking: boolean) => {
|
||||
// Conditions to check
|
||||
const markdownView = this.app.workspace.getActiveViewOfType(MarkdownView);
|
||||
if (markdownView) {
|
||||
// If checking is true, we're simply "checking" if the command can be run.
|
||||
// If checking is false, then we want to actually perform the operation.
|
||||
if (!checking) {
|
||||
new SampleModal(this.app).open();
|
||||
}
|
||||
|
||||
// This command will only show up in Command Palette when the check function returns true
|
||||
return true;
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
// This adds a settings tab so the user can configure various aspects of the plugin
|
||||
this.addSettingTab(new SampleSettingTab(this.app, this));
|
||||
|
||||
// If the plugin hooks up any global DOM events (on parts of the app that doesn't belong to this plugin)
|
||||
// Using this function will automatically remove the event listener when this plugin is disabled.
|
||||
this.registerDomEvent(document, 'click', (evt: MouseEvent) => {
|
||||
console.log('click', evt);
|
||||
});
|
||||
|
||||
// When registering intervals, this function will automatically clear the interval when the plugin is disabled.
|
||||
this.registerInterval(window.setInterval(() => console.log('setInterval'), 5 * 60 * 1000));
|
||||
|
||||
return qaMap;
|
||||
}
|
||||
|
||||
onunload() {
|
||||
console.log('Unloading Plugin')
|
||||
}
|
||||
|
||||
|
||||
async loadSettings() {
|
||||
this.settings = Object.assign({}, DEFAULT_SETTINGS, await this.loadData());
|
||||
}
|
||||
|
|
@ -128,60 +65,60 @@ export default class MinimalQuizPlugin extends Plugin {
|
|||
}
|
||||
|
||||
class QuestionsModal extends Modal {
|
||||
questions: string[];
|
||||
entries: [string, string][];
|
||||
answerVisible = false;
|
||||
currentIndex = 0;
|
||||
|
||||
constructor(app: App, questions: string[]) {
|
||||
constructor(app: App, questions: [string, string][]) {
|
||||
super(app);
|
||||
this.questions = questions;
|
||||
this.entries = questions;
|
||||
}
|
||||
|
||||
onOpen() {
|
||||
const {contentEl, modalEl} = this;
|
||||
contentEl.setText('Woah!');
|
||||
//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)
|
||||
}
|
||||
|
||||
modalEl.style.backdropFilter = 'blur(10px)';
|
||||
modalEl.style.backgroundColor = 'rgba(0, 0, 0, 0.7)';
|
||||
modalEl.style.color = 'white';
|
||||
modalEl.style.padding = '20px';
|
||||
modalEl.style.borderRadius = '8px';
|
||||
handleKeyDown = (event: KeyboardEvent) => {
|
||||
if (event.key === ' ' || event.key === 'Enter') {
|
||||
this.toggleAnswer();
|
||||
event.preventDefault();
|
||||
}
|
||||
};
|
||||
|
||||
render() {
|
||||
const { contentEl } = this;
|
||||
contentEl.empty();
|
||||
|
||||
const [question, answer] = this.entries[this.currentIndex];
|
||||
contentEl.createEl('h2', question);
|
||||
|
||||
contentEl.createEl('h2', { text: 'Questions found:'});
|
||||
|
||||
const list = contentEl.createEl('ul');
|
||||
this.questions.forEach(question => {
|
||||
const item = list.createEl('li', { text: question });
|
||||
item.style.marginBottom = '8px';
|
||||
});
|
||||
const answerEl = contentEl.createEl('p', { text: this.answerVisible ? answer : ''});
|
||||
answerEl.style.marginTop = '20px';
|
||||
|
||||
const button = contentEl.createEl('button', { text: this.answerVisible ? 'Next Questions' : 'Show Answer'});
|
||||
button.style.marginTop = '20px';
|
||||
button.addEventListener('click', () => this.toggleAnswer());
|
||||
}
|
||||
|
||||
toggleAnswer(){
|
||||
if (this.answerVisible){
|
||||
this.currentIndex++;
|
||||
this.answerVisible = false;
|
||||
this.render();
|
||||
}
|
||||
}
|
||||
|
||||
onClose() {
|
||||
const {contentEl} = this;
|
||||
contentEl.empty();
|
||||
window.removeEventListener('keydown', this.handleKeyDown);
|
||||
}
|
||||
}
|
||||
|
||||
class SampleSettingTab extends PluginSettingTab {
|
||||
plugin: MinimalQuizPlugin;
|
||||
|
||||
constructor(app: App, plugin: MinimalQuizPlugin) {
|
||||
super(app, plugin);
|
||||
this.plugin = plugin;
|
||||
}
|
||||
|
||||
display(): void {
|
||||
const {containerEl} = this;
|
||||
|
||||
containerEl.empty();
|
||||
|
||||
new Setting(containerEl)
|
||||
.setName('Setting #1')
|
||||
.setDesc('It\'s a secret')
|
||||
.addText(text => text
|
||||
.setPlaceholder('Enter your secret')
|
||||
.setValue(this.plugin.settings.mySetting)
|
||||
.onChange(async (value) => {
|
||||
this.plugin.settings.mySetting = value;
|
||||
await this.plugin.saveSettings();
|
||||
}));
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue