Enhances code documentation and structure for plugin
Adds comprehensive documentation for all major classes and methods in the Freewriting Cleanup plugin Improves code readability by: - Adding detailed JSDoc comments explaining purpose and functionality - Clarifying method behaviors and input/output expectations - Providing inline context for complex operations Standardizes error handling and adds more robust validation across services
This commit is contained in:
7 files changed
+584
-24
No files matched your search
@@ -8,12 +8,26 @@ import { CleanupService } from './services/cleanupService';
|
||||
import { ModelService } from './services/modelService';
|
||||
import { CleanupCommand } from './commands/cleanupCommand';
|
||||
|
||||
/**
|
||||
* Main plugin class for Freewriting Cleanup
|
||||
*
|
||||
* Coordinates the plugin lifecycle, manages services, and registers commands.
|
||||
* Uses modular architecture with separate services for cleanup operations,
|
||||
* model management, and command execution.
|
||||
*/
|
||||
export default class FreewritingCleanupPlugin extends Plugin {
|
||||
settings: FreewritingCleanupSettings;
|
||||
cleanupService: CleanupService;
|
||||
modelService: ModelService;
|
||||
cleanupCommand: CleanupCommand;
|
||||
|
||||
/**
|
||||
* Plugin initialization
|
||||
*
|
||||
* Loads settings and model cache, initializes services, registers commands,
|
||||
* and adds settings tab. This implementation avoids redundant data loads by
|
||||
* reusing the settings data for cache restoration.
|
||||
*/
|
||||
async onload() {
|
||||
// Load settings and reuse the data to avoid double loadData() call
|
||||
const data = await this.loadSettings();
|
||||
@@ -35,18 +49,38 @@ export default class FreewritingCleanupPlugin extends Plugin {
|
||||
this.addSettingTab(new FreewritingCleanupSettingTab(this.app, this));
|
||||
}
|
||||
|
||||
/**
|
||||
* Plugin cleanup
|
||||
*
|
||||
* Called when plugin is disabled. Currently no cleanup needed as services
|
||||
* are stateless and will be garbage collected.
|
||||
*/
|
||||
onunload() {
|
||||
// Cleanup if needed
|
||||
}
|
||||
|
||||
// MARK: - Settings Management
|
||||
|
||||
/**
|
||||
* Loads plugin settings from Obsidian storage
|
||||
*
|
||||
* Merges stored settings with defaults to ensure all required fields exist.
|
||||
* Returns the raw data for reuse in cache loading.
|
||||
*
|
||||
* @returns Raw plugin data including settings and optional cache, or null if no data exists
|
||||
*/
|
||||
async loadSettings(): Promise<FreewritingCleanupData | null> {
|
||||
const data = await this.loadData() as FreewritingCleanupData | null;
|
||||
this.settings = Object.assign({}, DEFAULT_SETTINGS, data);
|
||||
return data;
|
||||
}
|
||||
|
||||
/**
|
||||
* Saves plugin settings and model cache to Obsidian storage
|
||||
*
|
||||
* Persists current settings along with model cache for faster plugin startup.
|
||||
* Updates the cleanup service with new API key when settings change.
|
||||
*/
|
||||
async saveSettings() {
|
||||
// Save settings with model cache
|
||||
const data: FreewritingCleanupData = {
|
||||
@@ -63,6 +97,12 @@ export default class FreewritingCleanupPlugin extends Plugin {
|
||||
|
||||
// MARK: - Command Registration
|
||||
|
||||
/**
|
||||
* Registers the cleanup command with Obsidian
|
||||
*
|
||||
* Creates the "Clean up text" command with editor callback that executes
|
||||
* the cleanup operation on selected text.
|
||||
*/
|
||||
private registerCommand() {
|
||||
this.addCommand({
|
||||
id: 'cleanup-text',
|
||||
@@ -75,6 +115,15 @@ export default class FreewritingCleanupPlugin extends Plugin {
|
||||
|
||||
// MARK: - Command Implementation
|
||||
|
||||
/**
|
||||
* Executes the cleanup command
|
||||
*
|
||||
* Delegates to the CleanupCommand instance with current settings.
|
||||
* Catches and logs any errors that occur during execution.
|
||||
*
|
||||
* @param editor - Obsidian editor instance
|
||||
* @param view - Current markdown view
|
||||
*/
|
||||
private async executeCleanupCommand(editor: Editor, view: MarkdownView): Promise<void> {
|
||||
try {
|
||||
await this.cleanupCommand.execute(editor, view, this.settings);
|
||||
|
||||
Reference in new issue
Block a user