mirror of
https://github.com/bfloydd/streams.git
synced 2026-07-22 05:49:02 +00:00
add updateStreamBarFromFile to plugin api
This commit is contained in:
parent
c00b545ce4
commit
b126986c08
5 changed files with 127 additions and 5 deletions
|
|
@ -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).
|
||||
21
main.ts
21
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<boolean> {
|
||||
return serviceRegistry.api?.updateStreamBarFromFile(filePath) || false;
|
||||
}
|
||||
|
||||
// Internal Services (for plugin functionality)
|
||||
getFileOperationsService(): any {
|
||||
return serviceRegistry.fileOperations;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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<void> {
|
||||
|
|
@ -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<boolean> {
|
||||
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
|
||||
|
|
|
|||
|
|
@ -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<boolean>;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -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<void> {
|
||||
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');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue