Navigate to streams date by ribbon button

This commit is contained in:
Ben Floyd 2025-02-03 12:57:18 -07:00
parent eac8097059
commit fa20905307
3 changed files with 45 additions and 55 deletions

View file

@ -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) {

19
main.ts
View file

@ -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);
}
);

View file

@ -55,4 +55,44 @@ export async function createDailyNote(app: App, folder: string): Promise<TFile |
}
return file instanceof TFile ? file : null;
}
export async function openStreamDate(app: App, stream: Stream, date: Date = new Date()): Promise<void> {
// 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 });
}
}
}