docmarionum1_obsidian-pinne.../main.ts

196 lines
6.8 KiB
TypeScript
Raw Permalink Normal View History

2025-03-11 00:48:16 +00:00
import { App, Plugin, TFile, WorkspaceLeaf, View } from 'obsidian';
2025-06-11 00:44:59 +00:00
import { PinDailyNotePluginSetting as Setting, PinDailyNotePluginSettingTab as SettingTab, DEFAULT_SETTING, PinOptions, PluginOptions } from 'setting';
2025-03-11 00:20:47 +00:00
interface DailyNotesSettings {
folder?: string;
format?: string;
}
interface DailyNotesPlugin {
enabled: boolean;
instance?: {
options: DailyNotesSettings;
};
}
interface ObsidianApp extends App {
internalPlugins: {
plugins: {
'daily-notes': DailyNotesPlugin;
};
};
commands: {
commands: Record<string, { callback: () => Promise<void> }>;
};
}
interface ObsidianWorkspaceLeaf extends WorkspaceLeaf {
pinned?: boolean;
}
interface ObsidianView extends View {
file?: TFile;
}
export default class PinDailyNotePlugin extends Plugin {
2025-03-11 00:20:47 +00:00
private obsidianApp: ObsidianApp;
2025-06-11 00:19:57 +00:00
settings: Setting;
2025-03-11 00:20:47 +00:00
constructor(app: App, manifest: any) {
super(app, manifest);
this.obsidianApp = app as ObsidianApp;
}
async onload(): Promise<void> {
2025-06-11 00:10:45 +00:00
const getLeafForDailyNote = (whereToPin?: PinOptions): WorkspaceLeaf => {
if (!whereToPin) {
whereToPin = this.settings.whereToPin;
}
switch (whereToPin) {
case PinOptions.RIGHT_SIDE_BAR:
return this.obsidianApp.workspace.getRightLeaf(false)
case PinOptions.LEFT_SIDE_BAR:
return this.obsidianApp.workspace.getLeftLeaf(false)
default:
return this.obsidianApp.workspace.getLeaf(true)
}
}
2025-03-11 00:20:47 +00:00
const handleDailyNote = async (): Promise<void> => {
2025-04-15 23:47:02 +00:00
// Get the path of today's daily note
2025-03-11 00:20:47 +00:00
const todayPath = this.getTodayNotePath();
if (!todayPath) return;
2025-04-15 23:47:02 +00:00
// Find the pinned daily note leaf
// It could be today's or any matching daily note that's already pinned
const leaves: ObsidianWorkspaceLeaf[] = this.obsidianApp.workspace.getLeavesOfType('markdown');
2025-03-11 00:20:47 +00:00
let leaf = leaves.find(leaf => {
2025-04-15 23:47:02 +00:00
const view = leaf.view as ObsidianView;
return leaf.pinned && this.isDailyNotePath(view?.file?.path);
2025-03-11 00:20:47 +00:00
});
2025-04-15 23:47:02 +00:00
// If today's daily note doesn't already exist, create it
if (!(this.obsidianApp.vault.getAbstractFileByPath(todayPath) instanceof TFile)) {
2025-06-11 00:44:59 +00:00
// Get the command to use for creating the daily note
let dailyNotesCommand: { callback: () => Promise<void> } | undefined;
if (this.settings.plugin === PluginOptions.DAILY_NOTES) {
dailyNotesCommand = this.obsidianApp.commands.commands['daily-notes'];
} else {
dailyNotesCommand = this.obsidianApp.commands.commands['periodic-notes:open-daily-note'];
}
2025-03-11 00:20:47 +00:00
if (dailyNotesCommand) {
2025-06-11 00:10:45 +00:00
// Open a new tab leaf in the editor
// Daily notes plugin only supports editor leaf
const newLeaf = getLeafForDailyNote(PinOptions.EDITOR);
2025-04-15 23:47:02 +00:00
// Call the default daily notes command which will create the file in the new leaf
2025-03-11 00:20:47 +00:00
await dailyNotesCommand.callback();
2025-04-15 23:47:02 +00:00
// If we found a pinned daily note earlier, we can close newLeaf
2025-06-11 00:10:45 +00:00
// Or if the user wants to pin the daily note in someplace other
// than the editor, we can also close newLeaf
if (leaf || this.settings.whereToPin !== PinOptions.EDITOR) {
2025-03-11 00:20:47 +00:00
newLeaf.detach();
2025-06-11 00:10:45 +00:00
} else {
// Otherwise we will use the new leaf as the new pinned daily note leaf
2025-04-15 23:47:02 +00:00
newLeaf.setPinned(true);
return;
}
}
2025-03-11 00:20:47 +00:00
}
2025-04-15 23:47:02 +00:00
// Get today's file after possibly creating it above
const todayFile = this.obsidianApp.vault.getAbstractFileByPath(todayPath);
// If we don't have an active leaf, create one
if (!leaf) {
leaf = getLeafForDailyNote()
2025-04-15 23:47:02 +00:00
leaf.setPinned(true);
}
// Open today's file in the pinned leaf
2025-03-11 00:20:47 +00:00
if (todayFile instanceof TFile) {
await leaf.openFile(todayFile);
this.obsidianApp.workspace.setActiveLeaf(leaf, { focus: true });
}
};
2025-03-11 00:20:47 +00:00
this.addRibbonIcon('calendar-plus', 'Open today\'s daily note (Pinned)', () => {
handleDailyNote();
});
2025-03-11 00:20:47 +00:00
this.addCommand({
id: 'open-todays-daily-note-pinned',
name: 'Open today\'s daily note',
callback: () => handleDailyNote(),
});
await this.loadSettings()
this.addSettingTab(new SettingTab(this.app, this))
}
async loadSettings() {
this.settings = Object.assign({}, DEFAULT_SETTING, await this.loadData())
}
async saveSettings() {
this.saveData(this.settings)
}
getTodayNotePath(): string | null {
2025-03-11 00:20:47 +00:00
const dailyNotesPlugin = this.obsidianApp.internalPlugins.plugins['daily-notes'];
if (!dailyNotesPlugin?.enabled) return null;
2025-03-11 00:20:47 +00:00
try {
const settings = dailyNotesPlugin.instance?.options;
if (!settings) return null;
2025-03-11 00:20:47 +00:00
const folder = settings.folder?.trim().replace(/\/$/, '') || '';
const format = settings.format?.trim() || 'YYYY-MM-DD';
const date = window.moment();
2025-03-11 00:20:47 +00:00
let filename = date.format(format);
if (format.includes('/')) {
const formattedPath = folder
? `${folder}/${filename}`
: filename;
return formattedPath + '.md';
} else {
const path = folder
? `${folder}/${filename}`
: filename;
return path + '.md';
}
} catch (error) {
console.error('Error generating daily note path:', error);
return null;
}
}
2025-03-11 00:20:47 +00:00
isDailyNotePath(path: string | undefined): boolean {
if (!path) return false;
2025-03-11 00:20:47 +00:00
const dailyNotesPlugin = this.obsidianApp.internalPlugins.plugins['daily-notes'];
if (!dailyNotesPlugin?.enabled) return false;
2025-03-11 00:20:47 +00:00
try {
const settings = dailyNotesPlugin.instance?.options;
if (!settings) return false;
2025-03-11 00:20:47 +00:00
const folder = settings.folder?.trim().replace(/\/$/, '') || '';
2025-03-11 00:20:47 +00:00
if (folder && !path.startsWith(folder)) return false;
2025-03-11 00:20:47 +00:00
const filename = path.slice(folder ? folder.length + 1 : 0, -3);
return window.moment(filename, settings.format?.trim() || 'YYYY-MM-DD', true).isValid();
} catch (error) {
return false;
}
}
}