Refines settings UI and error handling

Simplifies commentary prompt assignments and streamlines
the Freewriting Cleanup Plugin command registration.

Improves consistency and readability in the settings interface
by standardizing heading and button labels. Removes redundant
model selection logging.

This enhances user experience by making the interface
more intuitive and declutters the codebase for easier maintenance.
This commit is contained in:
AlexKucera committed 2025-11-09 17:17:37 +01:00
1 parent c8ddeaeb2d
commit 1f69c568bb
4 files changed
+24 -31

No files matched your search

+1 -1
View File
@@ -78,7 +78,7 @@ export class AnthropicClient {
} else if (commentaryStyle === 'custom') { } else if (commentaryStyle === 'custom') {
throw new ConfigurationError('Custom commentary style requires a custom prompt. Please provide customCommentaryPrompt.'); throw new ConfigurationError('Custom commentary style requires a custom prompt. Please provide customCommentaryPrompt.');
} else { } else {
commentaryPrompt = COMMENTARY_PRESETS[commentaryStyle as Exclude<CommentaryStyle, 'custom'>]; commentaryPrompt = COMMENTARY_PRESETS[commentaryStyle];
} }
} }
+2 -2
View File
@@ -72,7 +72,7 @@ export default class FreewritingCleanupPlugin extends Plugin {
*/ */
async loadSettings(): Promise<FreewritingCleanupData | null> { async loadSettings(): Promise<FreewritingCleanupData | null> {
const data = await this.loadData() as FreewritingCleanupData | null; const data = await this.loadData() as FreewritingCleanupData | null;
const { modelCache, ...persisted } = data ?? {}; const { ...persisted } = data ?? {};
this.settings = { this.settings = {
...DEFAULT_SETTINGS, ...DEFAULT_SETTINGS,
...(persisted as Partial<FreewritingCleanupSettings>) ...(persisted as Partial<FreewritingCleanupSettings>)
@@ -111,7 +111,7 @@ export default class FreewritingCleanupPlugin extends Plugin {
*/ */
private registerCommand() { private registerCommand() {
this.addCommand({ this.addCommand({
id: 'freewriting-cleanup-cleanup-text', id: 'cleanup-text',
name: 'Clean up text', name: 'Clean up text',
editorCallback: async (editor: Editor, view: MarkdownView) => { editorCallback: async (editor: Editor, view: MarkdownView) => {
await this.executeCleanupCommand(editor, view); await this.executeCleanupCommand(editor, view);
-6
View File
@@ -1,7 +1,6 @@
// ABOUTME: Service class for orchestrating freewriting text cleanup operations // ABOUTME: Service class for orchestrating freewriting text cleanup operations
// ABOUTME: Handles validation, API coordination, and result formatting // ABOUTME: Handles validation, API coordination, and result formatting
import { Notice } from 'obsidian';
import { AnthropicClient } from '../api/anthropicClient'; import { AnthropicClient } from '../api/anthropicClient';
import { FreewritingCleanupSettings, CleanupResult, ANTHROPIC_LIMITS } from '../types'; import { FreewritingCleanupSettings, CleanupResult, ANTHROPIC_LIMITS } from '../types';
import { ApiKeyError, TextTooLongError } from '../errors'; import { ApiKeyError, TextTooLongError } from '../errors';
@@ -64,7 +63,6 @@ export class CleanupService {
const startTime = Date.now(); const startTime = Date.now();
try {
const { cleanedText, commentary, usage } = await this.client.cleanupText( const { cleanedText, commentary, usage } = await this.client.cleanupText(
text, text,
settings.model, settings.model,
@@ -90,10 +88,6 @@ export class CleanupService {
}; };
return result; return result;
} catch (error) {
// Let the command layer handle user feedback to avoid duplicate notices
throw error;
}
} }
/** /**
+21 -22
View File
@@ -54,7 +54,7 @@ export class FreewritingCleanupSettingTab extends PluginSettingTab {
private scheduleSave(): void { private scheduleSave(): void {
if (this.saveDebounce !== null) window.clearTimeout(this.saveDebounce); if (this.saveDebounce !== null) window.clearTimeout(this.saveDebounce);
this.saveDebounce = window.setTimeout(() => { this.saveDebounce = window.setTimeout(() => {
this.plugin.saveSettings(); void this.plugin.saveSettings();
this.saveDebounce = null; this.saveDebounce = null;
}, 300); }, 300);
} }
@@ -70,7 +70,7 @@ export class FreewritingCleanupSettingTab extends PluginSettingTab {
const { containerEl } = this; const { containerEl } = this;
containerEl.empty(); containerEl.empty();
containerEl.createEl('h2', { text: 'Freewriting Cleanup Settings' }); containerEl.createEl('h2', { text: 'Freewriting cleanup settings' });
// Load models asynchronously in the background // Load models asynchronously in the background
this.loadModels() this.loadModels()
@@ -86,10 +86,10 @@ export class FreewritingCleanupSettingTab extends PluginSettingTab {
// MARK: - API Configuration // MARK: - API Configuration
containerEl.createEl('h3', { text: 'API Configuration' }); new Setting(containerEl).setName('API configuration').setHeading();
new Setting(containerEl) new Setting(containerEl)
.setName('Anthropic API Key') .setName('Anthropic API key')
.setDesc('Your Anthropic API key for Claude. Get one at https://console.anthropic.com/') .setDesc('Your Anthropic API key for Claude. Get one at https://console.anthropic.com/')
.addText(text => text .addText(text => text
.setPlaceholder('sk-ant-...') .setPlaceholder('sk-ant-...')
@@ -115,16 +115,16 @@ export class FreewritingCleanupSettingTab extends PluginSettingTab {
}); });
new Setting(containerEl) new Setting(containerEl)
.setName('Test API Key') .setName('Test API key')
.setDesc('Test your API key to verify it works correctly') .setDesc('Test your API key to verify it works correctly')
.addButton(button => button .addButton(button => button
.setButtonText('Test Connection') .setButtonText('Test connection')
.onClick(async () => { .onClick(async () => {
await this.testApiKey(button); await this.testApiKey(button);
})); }));
new Setting(containerEl) new Setting(containerEl)
.setName('Claude Model') .setName('Claude model')
.setDesc('Which Claude model to use for text cleanup') .setDesc('Which Claude model to use for text cleanup')
.addDropdown(dropdown => { .addDropdown(dropdown => {
this.modelDropdown = dropdown; this.modelDropdown = dropdown;
@@ -139,16 +139,16 @@ export class FreewritingCleanupSettingTab extends PluginSettingTab {
// MARK: - Cleanup Configuration // MARK: - Cleanup Configuration
containerEl.createEl('h3', { text: 'Cleanup Configuration' }); new Setting(containerEl).setName('Cleanup configuration').setHeading();
new Setting(containerEl) new Setting(containerEl)
.setName('Cleanup Prompt') .setName('Cleanup prompt')
.setDesc('Instructions for how Claude should clean up your freewriting text') .setDesc('Instructions for how Claude should clean up your freewriting text')
.addTextArea(textArea => { .addTextArea(textArea => {
textArea textArea
.setPlaceholder('Enter your cleanup instructions...') .setPlaceholder('Enter your cleanup instructions...')
.setValue(this.plugin.settings.cleanupPrompt) .setValue(this.plugin.settings.cleanupPrompt)
.onChange(async (value) => { .onChange((value) => {
this.plugin.settings.cleanupPrompt = value; this.plugin.settings.cleanupPrompt = value;
this.scheduleSave(); this.scheduleSave();
}); });
@@ -160,14 +160,14 @@ export class FreewritingCleanupSettingTab extends PluginSettingTab {
// MARK: - Commentary Configuration // MARK: - Commentary Configuration
containerEl.createEl('h3', { text: 'AI Commentary' }); new Setting(containerEl).setName('AI commentary').setHeading();
new Setting(containerEl) new Setting(containerEl)
.setName('AI should comment on text') .setName('AI should comment on text')
.setDesc('After cleaning up your text, AI will provide thoughtful commentary on your freewriting session') .setDesc('After cleaning up your text, AI will provide thoughtful commentary on your freewriting session')
.addToggle(toggle => toggle .addToggle(toggle => toggle
.setValue(this.plugin.settings.enableCommentary) .setValue(this.plugin.settings.enableCommentary)
.onChange(async (value) => { .onChange((value) => {
this.plugin.settings.enableCommentary = value; this.plugin.settings.enableCommentary = value;
this.scheduleSave(); this.scheduleSave();
this.display(); // Refresh to show/hide commentary options this.display(); // Refresh to show/hide commentary options
@@ -175,7 +175,7 @@ export class FreewritingCleanupSettingTab extends PluginSettingTab {
if (this.plugin.settings.enableCommentary) { if (this.plugin.settings.enableCommentary) {
new Setting(containerEl) new Setting(containerEl)
.setName('Commentary Style') .setName('Commentary style')
.setDesc('Choose the type of feedback AI should provide') .setDesc('Choose the type of feedback AI should provide')
.addDropdown(dropdown => { .addDropdown(dropdown => {
COMMENTARY_STYLES.forEach(style => { COMMENTARY_STYLES.forEach(style => {
@@ -184,7 +184,7 @@ export class FreewritingCleanupSettingTab extends PluginSettingTab {
}); });
dropdown dropdown
.setValue(this.plugin.settings.commentaryStyle) .setValue(this.plugin.settings.commentaryStyle)
.onChange(async (value) => { .onChange((value) => {
this.plugin.settings.commentaryStyle = value as CommentaryStyle; this.plugin.settings.commentaryStyle = value as CommentaryStyle;
this.scheduleSave(); this.scheduleSave();
this.display(); // Refresh to show/hide custom prompt this.display(); // Refresh to show/hide custom prompt
@@ -193,13 +193,13 @@ export class FreewritingCleanupSettingTab extends PluginSettingTab {
if (this.plugin.settings.commentaryStyle === 'custom') { if (this.plugin.settings.commentaryStyle === 'custom') {
new Setting(containerEl) new Setting(containerEl)
.setName('Custom Commentary Prompt') .setName('Custom commentary prompt')
.setDesc('Instructions for how AI should comment on your freewriting') .setDesc('Instructions for how AI should comment on your freewriting')
.addTextArea(textArea => { .addTextArea(textArea => {
textArea textArea
.setPlaceholder('Enter your commentary instructions...') .setPlaceholder('Enter your commentary instructions...')
.setValue(this.plugin.settings.customCommentaryPrompt) .setValue(this.plugin.settings.customCommentaryPrompt)
.onChange(async (value) => { .onChange((value) => {
this.plugin.settings.customCommentaryPrompt = value; this.plugin.settings.customCommentaryPrompt = value;
this.scheduleSave(); this.scheduleSave();
}); });
@@ -210,7 +210,7 @@ export class FreewritingCleanupSettingTab extends PluginSettingTab {
}); });
} else { } else {
// Show the current preset for reference // Show the current preset for reference
const presetText = COMMENTARY_PRESETS[this.plugin.settings.commentaryStyle as Exclude<CommentaryStyle, 'custom'>]; const presetText = COMMENTARY_PRESETS[this.plugin.settings.commentaryStyle];
const descDiv = containerEl.createDiv({ cls: 'setting-item-description freewriting-cleanup-description' }); const descDiv = containerEl.createDiv({ cls: 'setting-item-description freewriting-cleanup-description' });
descDiv.empty(); descDiv.empty();
descDiv.createEl('strong', { text: 'Selected style: ' }); descDiv.createEl('strong', { text: 'Selected style: ' });
@@ -220,7 +220,7 @@ export class FreewritingCleanupSettingTab extends PluginSettingTab {
// MARK: - Usage Information // MARK: - Usage Information
containerEl.createEl('h3', { text: 'Usage Information' }); new Setting(containerEl).setName('Usage information').setHeading();
const usageDiv = containerEl.createDiv(); const usageDiv = containerEl.createDiv();
usageDiv.empty(); usageDiv.empty();
@@ -243,13 +243,13 @@ export class FreewritingCleanupSettingTab extends PluginSettingTab {
// MARK: - Reset Settings // MARK: - Reset Settings
containerEl.createEl('h3', { text: 'Reset' }); new Setting(containerEl).setName('Reset').setHeading();
new Setting(containerEl) new Setting(containerEl)
.setName('Reset to Defaults') .setName('Reset to defaults')
.setDesc('Reset all settings to their default values') .setDesc('Reset all settings to their default values')
.addButton(button => button .addButton(button => button
.setButtonText('Reset Settings') .setButtonText('Reset settings')
.setWarning() .setWarning()
.onClick(async () => { .onClick(async () => {
await this.resetSettings(); await this.resetSettings();
@@ -334,7 +334,6 @@ export class FreewritingCleanupSettingTab extends PluginSettingTab {
const newModel = this.availableModels[0].id; const newModel = this.availableModels[0].id;
this.plugin.settings.model = newModel; this.plugin.settings.model = newModel;
this.modelDropdown.setValue(newModel); this.modelDropdown.setValue(newModel);
console.log(`Model '${currentModel}' no longer available, switched to '${newModel}'`);
} }
} }