[#3] Feature for configure where to pin (#4)

* lib: sync package-lock

* feat: setting ui for where-to-pin option

* fix typo

* feat: pin daily note to leaf configured
This commit is contained in:
Janghan Bae 2025-06-11 08:50:13 +09:00 committed by GitHub
parent 82c4fdf672
commit 91352c1630
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 91 additions and 7 deletions

46
main.ts
View file

@ -1,4 +1,6 @@
import { App, Plugin, TFile, WorkspaceLeaf, View } from 'obsidian'; import { App, Plugin, TFile, WorkspaceLeaf, View } from 'obsidian';
import { PinDailyNotePluginSetting as Setting, PinDailyNotePluginSettingTab as SettingTab, DEFAULT_SETTING, PinOptions } from 'setting';
interface DailyNotesSettings { interface DailyNotesSettings {
folder?: string; folder?: string;
@ -31,8 +33,10 @@ interface ObsidianView extends View {
file?: TFile; file?: TFile;
} }
export default class PinDailyNotePlugin extends Plugin { export default class PinDailyNotePlugin extends Plugin {
private obsidianApp: ObsidianApp; private obsidianApp: ObsidianApp;
settings: Setting; // non private for access from Setting class
constructor(app: App, manifest: any) { constructor(app: App, manifest: any) {
super(app, manifest); super(app, manifest);
@ -40,6 +44,25 @@ export default class PinDailyNotePlugin extends Plugin {
} }
async onload(): Promise<void> { async onload(): Promise<void> {
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<void> => { const handleDailyNote = async (): Promise<void> => {
// Get the path of today's daily note // Get the path of today's daily note
const todayPath = this.getTodayNotePath(); 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 today's daily note doesn't already exist, create it
if (!(this.obsidianApp.vault.getAbstractFileByPath(todayPath) instanceof TFile)) { if (!(this.obsidianApp.vault.getAbstractFileByPath(todayPath) instanceof TFile)) {
const dailyNotesCommand = this.obsidianApp.commands.commands['daily-notes']; const dailyNotesCommand = this.obsidianApp.commands.commands['daily-notes'];
if (dailyNotesCommand) { if (dailyNotesCommand) {
// Open a new tab leaf // 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 // Call the default daily notes command which will create the file in the new leaf
await dailyNotesCommand.callback(); await dailyNotesCommand.callback();
@ -79,7 +103,8 @@ export default class PinDailyNotePlugin extends Plugin {
// If we don't have an active leaf, create one // If we don't have an active leaf, create one
if (!leaf) { if (!leaf) {
leaf = this.obsidianApp.workspace.getLeaf(true); leaf = getLeafForDailyNote()
leaf.setPinned(true); leaf.setPinned(true);
} }
@ -99,6 +124,17 @@ export default class PinDailyNotePlugin extends Plugin {
name: 'Open today\'s daily note', name: 'Open today\'s daily note',
callback: () => handleDailyNote(), 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 { getTodayNotePath(): string | null {
@ -112,7 +148,7 @@ export default class PinDailyNotePlugin extends Plugin {
const folder = settings.folder?.trim().replace(/\/$/, '') || ''; const folder = settings.folder?.trim().replace(/\/$/, '') || '';
const format = settings.format?.trim() || 'YYYY-MM-DD'; const format = settings.format?.trim() || 'YYYY-MM-DD';
const date = window.moment(); const date = window.moment();
let filename = date.format(format); let filename = date.format(format);
if (format.includes('/')) { if (format.includes('/')) {
const formattedPath = folder const formattedPath = folder
@ -133,7 +169,7 @@ export default class PinDailyNotePlugin extends Plugin {
isDailyNotePath(path: string | undefined): boolean { isDailyNotePath(path: string | undefined): boolean {
if (!path) return false; if (!path) return false;
const dailyNotesPlugin = this.obsidianApp.internalPlugins.plugins['daily-notes']; const dailyNotesPlugin = this.obsidianApp.internalPlugins.plugins['daily-notes'];
if (!dailyNotesPlugin?.enabled) return false; if (!dailyNotesPlugin?.enabled) return false;
@ -142,7 +178,7 @@ export default class PinDailyNotePlugin extends Plugin {
if (!settings) return false; if (!settings) return false;
const folder = settings.folder?.trim().replace(/\/$/, '') || ''; const folder = settings.folder?.trim().replace(/\/$/, '') || '';
if (folder && !path.startsWith(folder)) return false; if (folder && !path.startsWith(folder)) return false;
const filename = path.slice(folder ? folder.length + 1 : 0, -3); const filename = path.slice(folder ? folder.length + 1 : 0, -3);

4
package-lock.json generated
View file

@ -1,12 +1,12 @@
{ {
"name": "obsidian-pinned-daily-notes", "name": "obsidian-pinned-daily-notes",
"version": "1.0.0", "version": "1.1.0",
"lockfileVersion": 3, "lockfileVersion": 3,
"requires": true, "requires": true,
"packages": { "packages": {
"": { "": {
"name": "obsidian-pinned-daily-notes", "name": "obsidian-pinned-daily-notes",
"version": "1.0.0", "version": "1.1.0",
"license": "MIT", "license": "MIT",
"devDependencies": { "devDependencies": {
"@types/node": "^16.11.6", "@types/node": "^16.11.6",

48
setting.ts Normal file
View file

@ -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<PinDailyNotePluginSetting> = {
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<string, PinOptions> = {
editor: PinOptions.EDITOR,
leftSideBar: PinOptions.LEFT_SIDE_BAR,
rightSideBar: PinOptions.RIGHT_SIDE_BAR
}