[#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

40
main.ts
View file

@ -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<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> => {
// 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 {

4
package-lock.json generated
View file

@ -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",

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
}