diff --git a/CalendarWidget.ts b/CalendarWidget.ts index a729031..b60c29a 100644 --- a/CalendarWidget.ts +++ b/CalendarWidget.ts @@ -1,5 +1,6 @@ import { App, WorkspaceLeaf, TFile, MarkdownView } from 'obsidian'; import { Stream } from './types'; +import { openStreamDate } from './streamUtils'; export class CalendarWidget { private widget: HTMLElement; @@ -26,6 +27,7 @@ export class CalendarWidget { const todayButton = collapsedView.createDiv('stream-calendar-today-button'); todayButton.setText(this.formatDate(new Date())); + // Create expanded view const expandedView = this.widget.createDiv('stream-calendar-expanded'); expandedView.style.display = 'none'; @@ -126,45 +128,8 @@ export class CalendarWidget { } private async selectDate(day: number) { - // Format the selected date as YYYY-MM-DD const selectedDate = new Date(this.currentDate.getFullYear(), this.currentDate.getMonth(), day); - const year = selectedDate.getFullYear(); - const month = String(selectedDate.getMonth() + 1).padStart(2, '0'); - const dayStr = String(day).padStart(2, '0'); - const fileName = `${year}-${month}-${dayStr}.md`; - - // Construct the full file path using the stream's folder - const folderPath = this.selectedStream.folder - .split(/[/\\]/) - .filter(Boolean) - .join('/'); - const filePath = folderPath ? `${folderPath}/${fileName}` : fileName; - - // Try to find existing file or create new one - let file = this.app.vault.getAbstractFileByPath(filePath); - if (!file) { - // Create folder if needed - if (folderPath && !this.app.vault.getAbstractFileByPath(folderPath)) { - await this.app.vault.createFolder(folderPath); - } - file = await this.app.vault.create(filePath, ''); - } - - if (file instanceof TFile) { - // Check if file is already open in a tab - const existingLeaf = this.app.workspace.getLeavesOfType('markdown') - .find(leaf => (leaf.view as MarkdownView).file?.path === file.path); - - if (existingLeaf) { - // Switch to existing tab - this.app.workspace.setActiveLeaf(existingLeaf, { focus: true }); - } else { - // Open in new tab - const leaf = this.app.workspace.getLeaf('tab'); - await leaf.openFile(file); - this.app.workspace.setActiveLeaf(leaf, { focus: true }); - } - } + await openStreamDate(this.app, this.selectedStream, selectedDate); } private toggleExpanded(collapsedView: HTMLElement, expandedView: HTMLElement) { diff --git a/main.ts b/main.ts index 965a422..391949f 100644 --- a/main.ts +++ b/main.ts @@ -1,7 +1,7 @@ import { App, Editor, MarkdownView, Modal, Notice, Plugin, PluginSettingTab, Setting, WorkspaceLeaf } from 'obsidian'; import { StreamsSettingTab } from './settings'; import { Stream, StreamsSettings } from './types'; -import { createDailyNote } from './streamUtils'; +import { createDailyNote, openStreamDate } from './streamUtils'; import { CalendarWidget } from './CalendarWidget'; // Add this type for Lucide icon names @@ -258,22 +258,7 @@ export default class StreamsPlugin extends Plugin { stream.icon, `Stream: ${stream.name} (${stream.id})`, async () => { - const file = await createDailyNote(this.app, stream.folder); - if (file) { - // Check if file is already open in a tab - const existingLeaf = this.app.workspace.getLeavesOfType('markdown') - .find(leaf => (leaf.view as MarkdownView).file?.path === file.path); - - if (existingLeaf) { - // Switch to existing tab - this.app.workspace.setActiveLeaf(existingLeaf, { focus: true }); - } else { - // Open in new tab - const leaf = this.app.workspace.getLeaf('tab'); - await leaf.openFile(file); - this.app.workspace.setActiveLeaf(leaf, { focus: true }); - } - } + await openStreamDate(this.app, stream); } ); diff --git a/streamUtils.ts b/streamUtils.ts index 0e4adca..594fe1b 100644 --- a/streamUtils.ts +++ b/streamUtils.ts @@ -55,4 +55,44 @@ export async function createDailyNote(app: App, folder: string): Promise { + // Format date as YYYY-MM-DD + const year = date.getFullYear(); + const month = String(date.getMonth() + 1).padStart(2, '0'); + const day = String(date.getDate()).padStart(2, '0'); + const fileName = `${year}-${month}-${day}.md`; + + // Normalize folder path + const folderPath = stream.folder + .split(/[/\\]/) + .filter(Boolean) + .join('/'); + const filePath = folderPath ? `${folderPath}/${fileName}` : fileName; + + // Try to find existing file or create new one + let file = app.vault.getAbstractFileByPath(filePath); + if (!file) { + if (folderPath && !app.vault.getAbstractFileByPath(folderPath)) { + await app.vault.createFolder(folderPath); + } + file = await app.vault.create(filePath, ''); + } + + if (file instanceof TFile) { + // Check if file is already open in a tab + const existingLeaf = app.workspace.getLeavesOfType('markdown') + .find(leaf => (leaf.view as MarkdownView).file?.path === file.path); + + if (existingLeaf) { + // Switch to existing tab + app.workspace.setActiveLeaf(existingLeaf, { focus: true }); + } else { + // Open in new tab + const leaf = app.workspace.getLeaf('tab'); + await leaf.openFile(file); + app.workspace.setActiveLeaf(leaf, { focus: true }); + } + } } \ No newline at end of file