Implements freewriting prompts plugin
Initial implementation of the Freewriting Prompts plugin. This commit introduces the core functionality for generating AI-powered writing prompts using Anthropic's Claude API. It includes: - API client for interacting with the Anthropic API - Services for generating and caching prompts - Commands for displaying staggered prompts and appending prompts to notes - Settings tab for configuring the plugin
This commit is contained in:
10 files changed
+1095
-143
No files matched your search
@@ -0,0 +1,122 @@
|
||||
// ABOUTME: Note prompts command implementation that appends prompts to the current note
|
||||
// ABOUTME: Handles editor interaction, cursor positioning, and prompt formatting
|
||||
|
||||
import { Editor, MarkdownView, Notice } from 'obsidian';
|
||||
import { PromptGeneratorService } from '../services/promptGenerator';
|
||||
import { FreewritingPromptsSettings } from '../types';
|
||||
|
||||
export class NotePromptsCommand {
|
||||
constructor(private promptGenerator: PromptGeneratorService) {}
|
||||
|
||||
// MARK: - Public Methods
|
||||
|
||||
async execute(
|
||||
settings: FreewritingPromptsSettings,
|
||||
editor: Editor,
|
||||
view: MarkdownView
|
||||
): Promise<void> {
|
||||
if (!editor || !view) {
|
||||
new Notice('No active note found. Please open a note first.');
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
// Generate prompts
|
||||
const prompts = await this.promptGenerator.generateNotePrompts(settings);
|
||||
|
||||
if (prompts.length === 0) {
|
||||
new Notice('No prompts were generated');
|
||||
return;
|
||||
}
|
||||
|
||||
// Format and insert prompts into the note
|
||||
this.insertPromptsIntoNote(editor, prompts);
|
||||
|
||||
new Notice(`Added ${prompts.length} writing prompts to your note`);
|
||||
|
||||
} catch (error) {
|
||||
console.error('Error executing note prompts command:', error);
|
||||
// Error handling is done in the service layer
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Private Methods
|
||||
|
||||
private insertPromptsIntoNote(editor: Editor, prompts: string[]): void {
|
||||
const cursor = editor.getCursor();
|
||||
const currentLine = editor.getLine(cursor.line);
|
||||
|
||||
// Determine if we need to add a newline before our content
|
||||
const needsNewlineBefore = currentLine.trim().length > 0;
|
||||
|
||||
// Format the prompts
|
||||
const formattedPrompts = this.formatPrompts(prompts);
|
||||
|
||||
// Prepare the content to insert
|
||||
let contentToInsert = '';
|
||||
|
||||
if (needsNewlineBefore) {
|
||||
contentToInsert += '\n';
|
||||
}
|
||||
|
||||
contentToInsert += formattedPrompts;
|
||||
|
||||
// If we're not at the end of the document, add a trailing newline
|
||||
const lastLine = editor.lastLine();
|
||||
if (cursor.line < lastLine) {
|
||||
contentToInsert += '\n';
|
||||
}
|
||||
|
||||
// Insert the content at the cursor position
|
||||
editor.replaceSelection(contentToInsert);
|
||||
|
||||
// Position cursor at the end of the inserted content
|
||||
const lines = contentToInsert.split('\n');
|
||||
const newCursorLine = cursor.line + lines.length - 1;
|
||||
const newCursorCh = lines[lines.length - 1].length;
|
||||
|
||||
if (cursor.line === lastLine && !needsNewlineBefore) {
|
||||
// If we were at the last line and didn't add a newline before, adjust
|
||||
editor.setCursor(cursor.line, cursor.ch + contentToInsert.length);
|
||||
} else {
|
||||
editor.setCursor(newCursorLine, newCursorCh);
|
||||
}
|
||||
}
|
||||
|
||||
private formatPrompts(prompts: string[]): string {
|
||||
const timestamp = new Date().toLocaleDateString('en-US', {
|
||||
year: 'numeric',
|
||||
month: 'short',
|
||||
day: 'numeric',
|
||||
hour: '2-digit',
|
||||
minute: '2-digit'
|
||||
});
|
||||
|
||||
let formatted = `## Writing Prompts (${timestamp})\n\n`;
|
||||
|
||||
prompts.forEach((prompt, index) => {
|
||||
formatted += `${index + 1}. ${prompt}\n\n`;
|
||||
});
|
||||
|
||||
return formatted;
|
||||
}
|
||||
|
||||
// MARK: - Utility Methods
|
||||
|
||||
static canExecute(editor: Editor, view: MarkdownView): boolean {
|
||||
return !!(editor && view && view.file);
|
||||
}
|
||||
|
||||
static getExecutionContext(app: any): { editor: Editor | null; view: MarkdownView | null } {
|
||||
const activeView = app.workspace.getActiveViewOfType(MarkdownView);
|
||||
|
||||
if (!activeView) {
|
||||
return { editor: null, view: null };
|
||||
}
|
||||
|
||||
return {
|
||||
editor: activeView.editor,
|
||||
view: activeView
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,117 @@
|
||||
// ABOUTME: Staggered prompts command implementation that shows prompts as timed notifications
|
||||
// ABOUTME: Manages interval-based prompt display with proper cleanup and error handling
|
||||
|
||||
import { Notice } from 'obsidian';
|
||||
import { PromptGeneratorService } from '../services/promptGenerator';
|
||||
import { FreewritingPromptsSettings } from '../types';
|
||||
|
||||
export class StaggeredPromptsCommand {
|
||||
private activeInterval: number | null = null;
|
||||
private promptQueue: string[] = [];
|
||||
private currentIndex = 0;
|
||||
|
||||
constructor(private promptGenerator: PromptGeneratorService) {}
|
||||
|
||||
// MARK: - Public Methods
|
||||
|
||||
async execute(settings: FreewritingPromptsSettings): Promise<void> {
|
||||
// Stop any existing staggered sequence
|
||||
this.stop();
|
||||
|
||||
try {
|
||||
// Generate prompts
|
||||
const prompts = await this.promptGenerator.generateStaggeredPrompts(settings);
|
||||
|
||||
if (prompts.length === 0) {
|
||||
new Notice('No prompts were generated');
|
||||
return;
|
||||
}
|
||||
|
||||
// Initialize the prompt queue
|
||||
this.promptQueue = prompts;
|
||||
this.currentIndex = 0;
|
||||
|
||||
// Show the first prompt immediately
|
||||
this.showCurrentPrompt();
|
||||
|
||||
// If there are more prompts, start the interval
|
||||
if (prompts.length > 1) {
|
||||
this.startInterval(settings.delaySeconds);
|
||||
}
|
||||
|
||||
} catch (error) {
|
||||
console.error('Error executing staggered prompts command:', error);
|
||||
// Error handling is done in the service layer
|
||||
}
|
||||
}
|
||||
|
||||
stop(): void {
|
||||
if (this.activeInterval !== null) {
|
||||
clearInterval(this.activeInterval);
|
||||
this.activeInterval = null;
|
||||
}
|
||||
this.promptQueue = [];
|
||||
this.currentIndex = 0;
|
||||
}
|
||||
|
||||
isRunning(): boolean {
|
||||
return this.activeInterval !== null;
|
||||
}
|
||||
|
||||
// MARK: - Private Methods
|
||||
|
||||
private startInterval(delaySeconds: number): void {
|
||||
this.activeInterval = window.setInterval(() => {
|
||||
this.currentIndex++;
|
||||
|
||||
if (this.currentIndex < this.promptQueue.length) {
|
||||
this.showCurrentPrompt();
|
||||
} else {
|
||||
// We've shown all prompts, stop the interval
|
||||
this.stop();
|
||||
new Notice('All prompts have been shown');
|
||||
}
|
||||
}, delaySeconds * 1000);
|
||||
}
|
||||
|
||||
private showCurrentPrompt(): void {
|
||||
if (this.currentIndex < this.promptQueue.length) {
|
||||
const prompt = this.promptQueue[this.currentIndex];
|
||||
const promptNumber = this.currentIndex + 1;
|
||||
const totalPrompts = this.promptQueue.length;
|
||||
|
||||
// Create a longer-lasting notice for the prompt
|
||||
const notice = new Notice(
|
||||
`Writing Prompt ${promptNumber}/${totalPrompts}:\n\n${prompt}`,
|
||||
8000 // Show for 8 seconds
|
||||
);
|
||||
|
||||
// Add some styling to make it more prominent
|
||||
const noticeEl = notice.noticeEl;
|
||||
if (noticeEl) {
|
||||
noticeEl.style.maxWidth = '400px';
|
||||
noticeEl.style.whiteSpace = 'pre-wrap';
|
||||
noticeEl.style.fontSize = '14px';
|
||||
noticeEl.style.lineHeight = '1.4';
|
||||
noticeEl.addClass('freewriting-prompt-notice');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Status Methods
|
||||
|
||||
getStatus(): { isRunning: boolean; currentPrompt: number; totalPrompts: number } {
|
||||
return {
|
||||
isRunning: this.isRunning(),
|
||||
currentPrompt: this.currentIndex + 1,
|
||||
totalPrompts: this.promptQueue.length
|
||||
};
|
||||
}
|
||||
|
||||
getRemainingPrompts(): string[] {
|
||||
if (this.currentIndex >= this.promptQueue.length) {
|
||||
return [];
|
||||
}
|
||||
return this.promptQueue.slice(this.currentIndex + 1);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user