Added DocStrings
This commit is contained in:
8 files changed
+797
-17
No files matched your search
@@ -9,13 +9,43 @@ import { ModelService } from './services/modelService';
|
||||
import { TimedPromptsCommand } from './commands/timedPrompts';
|
||||
import { NotePromptsCommand } from './commands/notePrompts';
|
||||
|
||||
/**
|
||||
* Main plugin class for Freewriting Prompts.
|
||||
*
|
||||
* This plugin helps writers overcome creative blocks by generating AI-powered
|
||||
* writing prompts using Claude. It provides two main features:
|
||||
* 1. Timed prompts: Shows prompts as notifications at regular intervals
|
||||
* 2. Note prompts: Inserts prompts directly into the current note
|
||||
*
|
||||
* Architecture:
|
||||
* - Services: Core business logic (prompt generation, model management)
|
||||
* - Commands: User-facing actions (timed prompts, note prompts)
|
||||
* - API Client: Low-level Anthropic API communication
|
||||
* - Settings: User configuration UI and persistence
|
||||
*
|
||||
* The plugin manages the full lifecycle including initialization, cleanup,
|
||||
* settings persistence (including model cache), and command registration.
|
||||
*/
|
||||
export default class FreewritingPromptsPlugin extends Plugin {
|
||||
/** Current plugin settings */
|
||||
settings: FreewritingPromptsSettings;
|
||||
/** Service for generating writing prompts */
|
||||
promptGenerator: PromptGeneratorService;
|
||||
/** Service for managing available Claude models */
|
||||
modelService: ModelService;
|
||||
/** Command handler for timed prompt notifications */
|
||||
timedCommand: TimedPromptsCommand;
|
||||
/** Command handler for note prompt insertion */
|
||||
noteCommand: NotePromptsCommand;
|
||||
|
||||
/**
|
||||
* Called when the plugin loads.
|
||||
*
|
||||
* Initializes all services and commands, loads settings and model cache
|
||||
* from disk, and registers commands with Obsidian. This method ensures
|
||||
* the model cache is loaded before ModelService tries to use it, avoiding
|
||||
* an unnecessary API call on startup.
|
||||
*/
|
||||
async onload() {
|
||||
// Load data once and use it for both settings and model cache
|
||||
const data = await this.loadData() as FreewritingPromptsData | null;
|
||||
@@ -39,6 +69,16 @@ export default class FreewritingPromptsPlugin extends Plugin {
|
||||
this.addSettingTab(new FreewritingPromptsSettingTab(this.app, this));
|
||||
}
|
||||
|
||||
/**
|
||||
* Called when the plugin unloads.
|
||||
*
|
||||
* Performs cleanup to prevent memory leaks and ensure proper state:
|
||||
* - Stops any running timed prompts (clears intervals)
|
||||
* - Clears the in-memory prompt cache
|
||||
*
|
||||
* Model cache is NOT cleared here because it's persisted to disk and
|
||||
* should be available on next load.
|
||||
*/
|
||||
onunload() {
|
||||
// Stop any running timed prompts
|
||||
if (this.timedCommand) {
|
||||
@@ -53,10 +93,23 @@ export default class FreewritingPromptsPlugin extends Plugin {
|
||||
|
||||
// MARK: - Settings Management
|
||||
|
||||
/**
|
||||
* Loads plugin settings from disk.
|
||||
*
|
||||
* Merges saved settings with defaults to ensure any new settings added
|
||||
* in plugin updates have sensible default values.
|
||||
*/
|
||||
async loadSettings() {
|
||||
this.settings = Object.assign({}, DEFAULT_SETTINGS, await this.loadData());
|
||||
}
|
||||
|
||||
/**
|
||||
* Saves plugin settings to disk.
|
||||
*
|
||||
* Includes both user settings and the model cache to avoid re-fetching
|
||||
* models on every startup. Updates the prompt generator with the new API
|
||||
* key if it changed.
|
||||
*/
|
||||
async saveSettings() {
|
||||
// Save settings with model cache
|
||||
const data: FreewritingPromptsData = {
|
||||
@@ -73,6 +126,16 @@ export default class FreewritingPromptsPlugin extends Plugin {
|
||||
|
||||
// MARK: - Command Registration
|
||||
|
||||
/**
|
||||
* Registers all plugin commands with Obsidian.
|
||||
*
|
||||
* Three commands are registered:
|
||||
* 1. 'timed-prompts': Starts a timed prompt sequence
|
||||
* 2. 'note-prompts': Inserts prompts into the current note
|
||||
* 3. 'stop-timed-prompts': Stops the active timed sequence
|
||||
*
|
||||
* Commands are available in the command palette and can be bound to hotkeys.
|
||||
*/
|
||||
private registerCommands() {
|
||||
// Timed prompts command
|
||||
this.addCommand({
|
||||
@@ -104,6 +167,17 @@ export default class FreewritingPromptsPlugin extends Plugin {
|
||||
|
||||
// MARK: - Command Implementations
|
||||
|
||||
/**
|
||||
* Executes the timed prompts command.
|
||||
*
|
||||
* This method:
|
||||
* 1. Checks if timed prompts are already running
|
||||
* 2. Validates settings before making API calls
|
||||
* 3. Delegates to the TimedPromptsCommand for execution
|
||||
*
|
||||
* Shows user-friendly error messages for common issues like missing
|
||||
* API key or invalid settings.
|
||||
*/
|
||||
private async executeTimedPrompts(): Promise<void> {
|
||||
// Check if timed prompts are already running
|
||||
if (this.timedCommand.isRunning()) {
|
||||
@@ -126,6 +200,20 @@ export default class FreewritingPromptsPlugin extends Plugin {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Executes the note prompts command.
|
||||
*
|
||||
* This method:
|
||||
* 1. Validates editor and view are available
|
||||
* 2. Validates settings before making API calls
|
||||
* 3. Delegates to the NotePromptsCommand for execution
|
||||
*
|
||||
* This is an editor command, so Obsidian ensures editor and view exist,
|
||||
* but we double-check for safety.
|
||||
*
|
||||
* @param editor - Active editor instance
|
||||
* @param view - Active markdown view
|
||||
*/
|
||||
private async executeNotePrompts(editor: Editor, view: MarkdownView): Promise<void> {
|
||||
// Check if we can execute the command
|
||||
if (!NotePromptsCommand.canExecute(editor, view)) {
|
||||
@@ -148,6 +236,12 @@ export default class FreewritingPromptsPlugin extends Plugin {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Stops the currently running timed prompts sequence.
|
||||
*
|
||||
* Checks if prompts are actually running before attempting to stop
|
||||
* to provide appropriate feedback messages.
|
||||
*/
|
||||
private stopTimedPrompts(): void {
|
||||
if (this.timedCommand.isRunning()) {
|
||||
this.timedCommand.stop();
|
||||
@@ -159,6 +253,13 @@ export default class FreewritingPromptsPlugin extends Plugin {
|
||||
|
||||
// MARK: - Public API for Settings
|
||||
|
||||
/**
|
||||
* Gets the current status of the timed prompts sequence.
|
||||
*
|
||||
* This is used by the settings tab or status bar to show current progress.
|
||||
*
|
||||
* @returns Object containing running state and progress information
|
||||
*/
|
||||
getTimedStatus(): { isRunning: boolean; currentPrompt: number; totalPrompts: number } {
|
||||
return this.timedCommand.getStatus();
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user