From b126986c0805cfa273e17858b82403f5925c34be Mon Sep 17 00:00:00 2001 From: Ben Floyd Date: Fri, 10 Oct 2025 22:26:36 -0600 Subject: [PATCH] add updateStreamBarFromFile to plugin api --- README.md | 1 + main.ts | 21 ++++++-- src/slices/api/APIService.ts | 54 +++++++++++++++++++ src/slices/api/StreamsAPI.ts | 7 +++ .../CommandRegistrationService.ts | 49 ++++++++++++++++- 5 files changed, 127 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 2e173c5..5d43fb8 100644 --- a/README.md +++ b/README.md @@ -96,5 +96,6 @@ if (streamsPlugin) { - `getRibbonStreams()` - Get streams enabled for ribbon display - `getCommandStreams()` - Get streams with commands enabled - `hasStreams()` - Check if any streams are configured +- `updateStreamBarFromFile(filePath)` - Update the stream bar to match an opened file For detailed API documentation, see [src/api/README.md](src/api/README.md). \ No newline at end of file diff --git a/main.ts b/main.ts index 389c721..0d226c9 100644 --- a/main.ts +++ b/main.ts @@ -105,6 +105,11 @@ export default class StreamsPlugin extends Plugin implements StreamsAPI { await this.saveData(this.settings); } + // ============================================================================ + // PUBLIC API METHODS - Available to other plugins + // ============================================================================ + + // Stream Management setActiveStream(streamId: string, force?: boolean): void { serviceRegistry.streamManagement?.setActiveStream(streamId, force); } @@ -113,6 +118,7 @@ export default class StreamsPlugin extends Plugin implements StreamsAPI { return serviceRegistry.streamManagement?.getActiveStream() || null; } + // Stream Data Access getStreams(): Stream[] { return serviceRegistry.api?.getStreams() || [...this.settings.streams]; } @@ -145,10 +151,7 @@ export default class StreamsPlugin extends Plugin implements StreamsAPI { return serviceRegistry.api?.getStreamInfo(streamId) || null; } - getVersion(): any { - return serviceRegistry.api?.getVersion() || { version: '1.0.0', minAppVersion: '0.15.0', name: 'Streams', id: 'streams' }; - } - + // Stream Status & Information hasStream(streamId: string): boolean { return serviceRegistry.api?.hasStream(streamId) || false; } @@ -161,6 +164,16 @@ export default class StreamsPlugin extends Plugin implements StreamsAPI { return serviceRegistry.api?.hasStreams() || false; } + getVersion(): any { + return serviceRegistry.api?.getVersion() || { version: '1.0.0', minAppVersion: '0.15.0', name: 'Streams', id: 'streams' }; + } + + // Stream Bar Updates + async updateStreamBarFromFile(filePath: string): Promise { + return serviceRegistry.api?.updateStreamBarFromFile(filePath) || false; + } + + // Internal Services (for plugin functionality) getFileOperationsService(): any { return serviceRegistry.fileOperations; } diff --git a/src/slices/api/APIService.ts b/src/slices/api/APIService.ts index b9c6580..6d78ece 100644 --- a/src/slices/api/APIService.ts +++ b/src/slices/api/APIService.ts @@ -1,6 +1,7 @@ import { PluginAwareSliceService } from '../../shared/base-slice'; import { Stream, StreamsSettings } from '../../shared/types'; import { StreamsAPI, StreamInfo, PluginVersion } from './StreamsAPI'; +import { eventBus, EVENTS } from '../../shared/event-bus'; export class APIService extends PluginAwareSliceService implements StreamsAPI { async initialize(): Promise { @@ -183,6 +184,59 @@ export class APIService extends PluginAwareSliceService implements StreamsAPI { return this.getStreamCount() > 0; } + /** + * Update the stream bar to match an opened file + * @param filePath The path of the file that was opened + * @returns True if successful, false if stream not found or update failed + */ + public async updateStreamBarFromFile(filePath: string): Promise { + try { + const plugin = this.getPlugin() as any; + + // Detect stream from file path + const stream = this.getStreamForFile(filePath); + if (!stream) { + this.log(`No stream found for file: ${filePath}`); + return false; + } + + // Extract date from file path (e.g., "2025-10-08.md" -> Date) + const fileName = filePath.split('/').pop() || ''; + const dateMatch = fileName.match(/(\d{4}-\d{2}-\d{2})/); + let targetDate = new Date(); // Default to current date + + if (dateMatch) { + const dateString = dateMatch[1]; + // Parse date as local date to avoid timezone issues + const [year, month, day] = dateString.split('-').map(Number); + targetDate = new Date(year, month - 1, day); // month is 0-indexed + + if (isNaN(targetDate.getTime())) { + targetDate = new Date(); // Fallback to current date + } + } + + // Set the stream context + plugin.settings.activeStreamId = stream.id; + await plugin.saveSettings(); + + // Update the date state manager to reflect the file's date + const { DateStateManager } = await import('../../shared/date-state-manager'); + const dateStateManager = DateStateManager.getInstance(); + dateStateManager.setCurrentDate(targetDate); + + // Emit ACTIVE_STREAM_CHANGED event to trigger UI refresh + eventBus.emit(EVENTS.ACTIVE_STREAM_CHANGED, { streamId: stream.id }, 'api'); + + this.log(`Updated stream bar to "${stream.name}" for file: ${filePath} with date: ${targetDate.toISOString()}`); + return true; + + } catch (error) { + this.error(`Failed to update stream bar for file ${filePath}:`, error); + return false; + } + } + /** * Normalize a path for comparison * @param path The path to normalize diff --git a/src/slices/api/StreamsAPI.ts b/src/slices/api/StreamsAPI.ts index eaa5737..1f76113 100644 --- a/src/slices/api/StreamsAPI.ts +++ b/src/slices/api/StreamsAPI.ts @@ -63,6 +63,13 @@ export interface StreamsAPI { * @returns Plugin version details */ getVersion(): PluginVersion; + + /** + * Update the stream bar to match an opened file + * @param filePath The path of the file that was opened + * @returns True if successful, false if stream not found or update failed + */ + updateStreamBarFromFile(filePath: string): Promise; } /** diff --git a/src/slices/command-registration/CommandRegistrationService.ts b/src/slices/command-registration/CommandRegistrationService.ts index 85e252c..0b924e2 100644 --- a/src/slices/command-registration/CommandRegistrationService.ts +++ b/src/slices/command-registration/CommandRegistrationService.ts @@ -32,7 +32,14 @@ export class CommandRegistrationService extends PluginAwareSliceService implemen private registerStreamCommands(plugin: any): void { - // No stream commands currently registered + // Debug command for updateStreamBarFromFile functionality + plugin.addCommand({ + id: 'streams-debug-update-stream-bar', + name: 'Debug: Update Stream Bar from File', + callback: async () => { + await this.testUpdateStreamBarFromFile(); + } + }); } @@ -47,4 +54,44 @@ export class CommandRegistrationService extends PluginAwareSliceService implemen } return undefined; } + + private async testUpdateStreamBarFromFile(): Promise { + const plugin = this.getPlugin() as any; + const apiService = this.getService('api'); + + if (!apiService) { + plugin.log?.error('API service not available for testing'); + return; + } + + // Get the first available stream for testing + const streams = apiService.getStreams(); + if (streams.length === 0) { + plugin.log?.warn('No streams available for testing'); + return; + } + + const testStream = streams[0]; + const testFilePath = `${testStream.folder}/2024-01-15.md`; // Example file path + + plugin.log?.info(`Testing updateStreamBarFromFile for file: ${testFilePath}`); + plugin.log?.info(`Expected stream: ${testStream.name} (${testStream.id})`); + + // Test the updateStreamBarFromFile method + const result = await apiService.updateStreamBarFromFile(testFilePath); + + if (result) { + plugin.log?.info('✅ updateStreamBarFromFile test PASSED - Stream bar updated successfully'); + + // Verify the update by checking the active stream + const activeStream = apiService.getActiveStream(); + if (activeStream && activeStream.id === testStream.id) { + plugin.log?.info(`✅ Verification PASSED - Stream bar now shows: ${activeStream.name}`); + } else { + plugin.log?.warn('⚠️ Verification FAILED - Stream bar not updated correctly'); + } + } else { + plugin.log?.error('❌ updateStreamBarFromFile test FAILED - Method returned false'); + } + } }