Refactors confirmation modal with Obsidian integration

Replaces custom modal implementation with Obsidian's Modal class for improved consistency and accessibility

Enhances API key management with dynamic model refreshing and fallback handling
Improves event handling and modal interaction patterns
This commit is contained in:
AlexKucera committed 2025-10-24 10:44:53 +02:00
1 parent 7b23ed6615
commit eae48521c5
1 file changed
+32 -33
+32 -33
View File
@@ -1,7 +1,7 @@
// ABOUTME: Settings interface and SettingTab implementation for the Freewriting Cleanup plugin // ABOUTME: Settings interface and SettingTab implementation for the Freewriting Cleanup plugin
// ABOUTME: Handles user configuration including API key, model selection, and prompt customization // ABOUTME: Handles user configuration including API key, model selection, and prompt customization
import { App, ButtonComponent, DropdownComponent, Notice, PluginSettingTab, Setting } from 'obsidian'; import { App, ButtonComponent, DropdownComponent, Modal, Notice, PluginSettingTab, Setting } from 'obsidian';
import FreewritingCleanupPlugin from './main'; import FreewritingCleanupPlugin from './main';
import { FreewritingCleanupSettings, COMMENTARY_STYLES, CommentaryStyle, COMMENTARY_PRESETS } from './types'; import { FreewritingCleanupSettings, COMMENTARY_STYLES, CommentaryStyle, COMMENTARY_PRESETS } from './types';
import { ModelOption } from './services/modelService'; import { ModelOption } from './services/modelService';
@@ -49,9 +49,14 @@ export class FreewritingCleanupSettingTab extends PluginSettingTab {
await this.plugin.saveSettings(); await this.plugin.saveSettings();
this.plugin.cleanupService.updateApiKey(value); this.plugin.cleanupService.updateApiKey(value);
// Trigger model refresh when API key is entered // Refresh when key is entered; revert to fallback when cleared
if (value.trim().length > 0) { const hasKey = value.trim().length > 0;
if (hasKey) {
await this.refreshModels(); await this.refreshModels();
} else if (this.modelDropdown) {
// Show fallback immediately when key is cleared
this.availableModels = await this.plugin.modelService.getAvailableModels();
this.populateModelDropdown(this.modelDropdown);
} }
})) }))
.then(setting => { .then(setting => {
@@ -320,53 +325,47 @@ export class FreewritingCleanupSettingTab extends PluginSettingTab {
} }
} }
// Simple confirmation modal // Confirmation modal using Obsidian's Modal class for consistent styling and accessibility
class ConfirmModal { class ConfirmModal extends Modal {
private app: App;
private title: string; private title: string;
private message: string; private message: string;
private callback: (result: boolean) => void; private callback: (result: boolean) => void;
private modalEl: HTMLElement | null = null;
constructor(app: App, title: string, message: string, callback: (result: boolean) => void) { constructor(app: App, title: string, message: string, callback: (result: boolean) => void) {
this.app = app; super(app);
this.title = title; this.title = title;
this.message = message; this.message = message;
this.callback = callback; this.callback = callback;
} }
open(): void { onOpen(): void {
// Create modal backdrop const { contentEl } = this;
this.modalEl = document.createElement('div');
this.modalEl.className = 'modal-container mod-dim';
// Create modal content contentEl.createEl('h2', { text: this.title });
const modal = this.modalEl.createDiv('modal'); contentEl.createEl('p', { text: this.message });
const header = modal.createDiv('modal-header');
header.createEl('h3', { text: this.title });
const content = modal.createDiv('modal-content'); const buttonContainer = contentEl.createDiv({ cls: 'modal-button-container' });
content.createEl('p', { text: this.message });
const buttons = modal.createDiv('modal-button-container'); const cancelButton = buttonContainer.createEl('button', { text: 'Cancel' });
cancelButton.addEventListener('click', () => {
this.close();
this.callback(false);
});
const cancelButton = buttons.createEl('button', { text: 'Cancel' }); const confirmButton = buttonContainer.createEl('button', {
cancelButton.onclick = () => this.close(false); text: 'Confirm',
cls: 'mod-warning'
});
confirmButton.addEventListener('click', () => {
this.close();
this.callback(true);
});
const confirmButton = buttons.createEl('button', { text: 'Confirm', cls: 'mod-warning' });
confirmButton.onclick = () => this.close(true);
if (this.modalEl) {
document.body.appendChild(this.modalEl);
}
confirmButton.focus(); confirmButton.focus();
} }
private close(result: boolean): void { onClose(): void {
if (this.modalEl && this.modalEl.parentNode) { const { contentEl } = this;
this.modalEl.parentNode.removeChild(this.modalEl); contentEl.empty();
}
this.modalEl = null;
this.callback(result);
} }
} }