diff --git a/main.ts b/main.ts index 04d0aed..e59efbe 100644 --- a/main.ts +++ b/main.ts @@ -1,4 +1,6 @@ import { App, Plugin, TFile, WorkspaceLeaf, View } from 'obsidian'; +import { PinDailyNotePluginSetting as Setting, PinDailyNotePluginSettingTab as SettingTab, DEFAULT_SETTING, PinOptions } from 'setting'; + interface DailyNotesSettings { folder?: string; @@ -31,8 +33,10 @@ interface ObsidianView extends View { file?: TFile; } + export default class PinDailyNotePlugin extends Plugin { private obsidianApp: ObsidianApp; + settings: Setting; // non private for access from Setting class constructor(app: App, manifest: any) { super(app, manifest); @@ -40,6 +44,25 @@ export default class PinDailyNotePlugin extends Plugin { } async onload(): Promise { + const getLeafForDailyNote = (): WorkspaceLeaf => { + const { whereToPin } = this.settings; + + /** + * if we get right/left leaf with true param + * the side leaf is split horizontally + * + * Further todo? configure split or not + */ + 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) + } + } + const handleDailyNote = async (): Promise => { // Get the path of today's daily note const todayPath = this.getTodayNotePath(); @@ -56,9 +79,10 @@ export default class PinDailyNotePlugin extends Plugin { // If today's daily note doesn't already exist, create it if (!(this.obsidianApp.vault.getAbstractFileByPath(todayPath) instanceof TFile)) { const dailyNotesCommand = this.obsidianApp.commands.commands['daily-notes']; + if (dailyNotesCommand) { // Open a new tab leaf - const newLeaf = this.obsidianApp.workspace.getLeaf(true); + const newLeaf = getLeafForDailyNote(); // Call the default daily notes command which will create the file in the new leaf await dailyNotesCommand.callback(); @@ -79,7 +103,8 @@ export default class PinDailyNotePlugin extends Plugin { // If we don't have an active leaf, create one if (!leaf) { - leaf = this.obsidianApp.workspace.getLeaf(true); + leaf = getLeafForDailyNote() + leaf.setPinned(true); } @@ -99,6 +124,17 @@ export default class PinDailyNotePlugin extends Plugin { 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 { @@ -112,7 +148,7 @@ export default class PinDailyNotePlugin extends Plugin { const folder = settings.folder?.trim().replace(/\/$/, '') || ''; const format = settings.format?.trim() || 'YYYY-MM-DD'; const date = window.moment(); - + let filename = date.format(format); if (format.includes('/')) { const formattedPath = folder @@ -133,7 +169,7 @@ export default class PinDailyNotePlugin extends Plugin { isDailyNotePath(path: string | undefined): boolean { if (!path) return false; - + const dailyNotesPlugin = this.obsidianApp.internalPlugins.plugins['daily-notes']; if (!dailyNotesPlugin?.enabled) return false; @@ -142,7 +178,7 @@ export default class PinDailyNotePlugin extends Plugin { if (!settings) return false; const folder = settings.folder?.trim().replace(/\/$/, '') || ''; - + if (folder && !path.startsWith(folder)) return false; const filename = path.slice(folder ? folder.length + 1 : 0, -3); diff --git a/package-lock.json b/package-lock.json index 33764ab..1b62ddc 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "obsidian-pinned-daily-notes", - "version": "1.0.0", + "version": "1.1.0", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "obsidian-pinned-daily-notes", - "version": "1.0.0", + "version": "1.1.0", "license": "MIT", "devDependencies": { "@types/node": "^16.11.6", diff --git a/setting.ts b/setting.ts new file mode 100644 index 0000000..ed5c167 --- /dev/null +++ b/setting.ts @@ -0,0 +1,48 @@ +import PinDailyNotePlugin from "./main" +import { App, DropdownComponent, PluginSettingTab, Setting } from "obsidian" + +export interface PinDailyNotePluginSetting { + whereToPin: string +} + +export enum PinOptions { + EDITOR = 'editor', + LEFT_SIDE_BAR = 'leftSideBar', + RIGHT_SIDE_BAR = 'rightSideBar', +} + +export const DEFAULT_SETTING: Partial = { + whereToPin: PinOptions.EDITOR +} + +export class PinDailyNotePluginSettingTab extends PluginSettingTab { + plugin: PinDailyNotePlugin + + constructor(app: App, plugin: PinDailyNotePlugin) { + super(app, plugin) + this.plugin = plugin; + } + + display(): void { + const { containerEl } = this; + containerEl.empty() + + new Setting(containerEl) + .setName('Where to Pin') + .setDesc('Default place to pin Daily Note') + .addDropdown((dropdown) => { + dropdown.addOptions(whereToPinDropdownOptions) + dropdown.setValue(this.plugin.settings.whereToPin) + dropdown.onChange(async (value) => { + this.plugin.settings.whereToPin = value; + await this.plugin.saveSettings() + }) + }) + } +} + +const whereToPinDropdownOptions: Record = { + editor: PinOptions.EDITOR, + leftSideBar: PinOptions.LEFT_SIDE_BAR, + rightSideBar: PinOptions.RIGHT_SIDE_BAR +} \ No newline at end of file