From c937b717cc387b6341135d68be12ccda6e7686a3 Mon Sep 17 00:00:00 2001 From: Liam Cain Date: Tue, 9 Feb 2021 00:47:19 -0500 Subject: [PATCH] Remove weekly notes from core, add Periodic Notes support (#129) * Remove weekly notes from core * UI improvements to direct users to download Periodic Notes * Update readme * use v0.3.1 of calendar-ui * minor style fixes * Hide weekly settings if periodic notes - weekly is enabled * Update jest config * bump version --- README.md | 6 +- manifest.json | 2 +- package.json | 8 +- src/io/__tests__/weeklyNotes.spec.ts | 134 --------------------------- src/io/path.ts | 9 -- src/io/weeklyNotes.ts | 87 ++--------------- src/settings.ts | 46 ++++----- src/ui/Calendar.svelte | 3 +- src/ui/sources/streak.ts | 8 +- src/ui/sources/tags.ts | 7 +- src/ui/sources/tasks.ts | 8 +- src/ui/sources/wordCount.ts | 7 +- src/ui/stores.ts | 35 ++++++- src/ui/utils.ts | 17 +--- src/view.ts | 52 ++++++++--- styles.css | 11 +++ yarn.lock | 18 ++-- 17 files changed, 147 insertions(+), 311 deletions(-) delete mode 100644 src/io/__tests__/weeklyNotes.spec.ts delete mode 100644 src/io/path.ts create mode 100644 styles.css diff --git a/README.md b/README.md index 2b3b78d..8bdf1ee 100644 --- a/README.md +++ b/README.md @@ -160,11 +160,11 @@ If you want to style weekends to be distinguishable from weekdays, you can set t #### Weekly notes have a new home -The weekly note functionality has been split out into its [very own plugin](https://github.com/liamcain/obsidian-weekly-notes). In the future, the functionality will be removed from the Calendar plugin; so if you're currently using weekly notes, I encourage you to make the switch. Don't worry, the behavior is functionally identical and will still integrate with the calendar view! +The weekly note functionality has been split out into its [very own plugin](https://github.com/liamcain/obsidian-periodic-notes/). In the future, the functionality will be removed from the Calendar plugin; so if you're currently using weekly notes, I encourage you to make the switch. Don't worry, the behavior is functionally identical and will still integrate with the calendar view! -This split was inspired by the [One Thing Well](https://en.wikipedia.org/wiki/Unix_philosophy) philosophy. Plugins should be as modular. Some users might want weekly notes and have no use for a calendar view. And Vice versa. +This split was inspired by the [One Thing Well](https://en.wikipedia.org/wiki/Unix_philosophy) philosophy. Plugins should be as modular. Some users might want weekly notes and have no use for a calendar view. And vice versa. -If you are currently using weekly notes within the Calendar plugin, the new Weekly Notes plugin will migrate your settings for you automatically. +If you are currently using weekly notes within the Calendar plugin, the new Periodic Notes plugin will migrate your settings for you automatically. ### Usage diff --git a/manifest.json b/manifest.json index 6fa6d58..4a0e51b 100644 --- a/manifest.json +++ b/manifest.json @@ -2,7 +2,7 @@ "id": "calendar", "name": "Calendar", "description": "Calendar view of your daily notes", - "version": "1.4.19", + "version": "1.5.0", "author": "Liam Cain", "authorUrl": "https://github.com/liamcain/", "isDesktopOnly": false, diff --git a/package.json b/package.json index dfe43c6..9167a3e 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "calendar", - "version": "1.4.19", + "version": "1.5.0", "description": "Calendar view of your daily notes", "author": "liamcain", "main": "main.js", @@ -8,13 +8,13 @@ "scripts": { "lint": "svelte-check && eslint . --ext .ts", "build": "npm run lint && rollup -c", - "test": "jest", + "test": "jest --passWithNoTests", "test:watch": "yarn test -- --watch" }, "dependencies": { "obsidian": "obsidianmd/obsidian-api#master", - "obsidian-calendar-ui": "0.3.0", - "obsidian-daily-notes-interface": "0.5.1", + "obsidian-calendar-ui": "0.3.1", + "obsidian-daily-notes-interface": "0.7.0", "svelte": "3.31.0", "tslib": "2.0.3" }, diff --git a/src/io/__tests__/weeklyNotes.spec.ts b/src/io/__tests__/weeklyNotes.spec.ts deleted file mode 100644 index 048d4ab..0000000 --- a/src/io/__tests__/weeklyNotes.spec.ts +++ /dev/null @@ -1,134 +0,0 @@ -import moment from "moment"; -import { getTemplateContents } from "obsidian-daily-notes-interface"; - -import { getDefaultSettings } from "src/testUtils/settings"; -import mockApp from "src/testUtils/mockApp"; - -import * as weeklyNote from "../weeklyNotes"; - -jest.mock("obsidian-daily-notes-interface", () => ({ - getTemplateContents: jest.fn(), -})); - -describe("getDayOfWeekNumericalValue", () => { - beforeEach(() => { - window.app = mockApp; - window.moment = moment; - }); - - describe("start week on Sunday", () => { - beforeEach(() => { - // eslint-disable-next-line @typescript-eslint/no-explicit-any - (moment.localeData())._week.dow = 0; - }); - - test("returns 0 for sunday", () => { - expect(weeklyNote.getDayOfWeekNumericalValue("sunday")).toEqual(0); - }); - - test("returns 1 for monday", () => { - expect(weeklyNote.getDayOfWeekNumericalValue("monday")).toEqual(1); - }); - }); - - describe("start week on Monday", () => { - beforeEach(() => { - // eslint-disable-next-line @typescript-eslint/no-explicit-any - (moment.localeData())._week.dow = 1; - }); - - test("returns 0 for sunday", () => { - expect(weeklyNote.getDayOfWeekNumericalValue("sunday")).toEqual(6); - }); - - test("returns 1 for monday", () => { - expect(weeklyNote.getDayOfWeekNumericalValue("monday")).toEqual(0); - }); - }); -}); - -describe("createWeeklyNote", () => { - beforeEach(() => { - window.app = mockApp; - window.moment = moment; - }); - - test("creates note without template", async () => { - (getTemplateContents as jest.MockedFunction< - typeof getTemplateContents - >).mockResolvedValue("foo"); - - await weeklyNote.createWeeklyNote( - moment({ day: 1, month: 0, year: 2020 }), - getDefaultSettings() - ); - - expect(window.app.vault.create).toHaveBeenCalledWith("", "foo"); - }); - - test("replaces {{time}} and {{date}} in weekly note", async () => { - (getTemplateContents as jest.MockedFunction< - typeof getTemplateContents - >).mockResolvedValue("# {{date:gggg-MM}}\ncontents"); - - await weeklyNote.createWeeklyNote( - moment({ day: 1, month: 0, year: 2020 }), - getDefaultSettings() - ); - - expect(window.app.vault.create).toHaveBeenCalledWith( - "", - "# 2020-01\ncontents" - ); - }); - - describe("start week on Sunday", () => { - beforeEach(() => { - // eslint-disable-next-line @typescript-eslint/no-explicit-any - (moment.localeData())._week.dow = 0; - }); - - test("replaces {{sunday}} and {{monday}} in weekly note", async () => { - (getTemplateContents as jest.MockedFunction< - typeof getTemplateContents - >).mockResolvedValue( - "# {{sunday:gggg-MM-DD}}, {{monday:gggg-MM-DD}}, etc" - ); - - await weeklyNote.createWeeklyNote( - moment({ day: 22, month: 10, year: 2020 }), - getDefaultSettings() - ); - - expect(window.app.vault.create).toHaveBeenCalledWith( - "", - "# 2020-11-22, 2020-11-23, etc" - ); - }); - }); - - describe("start week on Monday", () => { - beforeEach(() => { - // eslint-disable-next-line @typescript-eslint/no-explicit-any - (moment.localeData())._week.dow = 1; - }); - - test("replaces {{sunday}} and {{monday}} in weekly note", async () => { - (getTemplateContents as jest.MockedFunction< - typeof getTemplateContents - >).mockResolvedValue( - "# From {{monday:gggg-MM-DD}} to {{sunday:gggg-MM-DD}}" - ); - - await weeklyNote.createWeeklyNote( - moment({ day: 22, month: 10, year: 2020 }), - getDefaultSettings({ weekStart: "monday" }) - ); - - expect(window.app.vault.create).toHaveBeenCalledWith( - "", - "# From 2020-11-16 to 2020-11-22" - ); - }); - }); -}); diff --git a/src/io/path.ts b/src/io/path.ts deleted file mode 100644 index f14785e..0000000 --- a/src/io/path.ts +++ /dev/null @@ -1,9 +0,0 @@ -import { normalizePath } from "obsidian"; -import { join } from "path"; - -export function getNotePath(directory: string, filename: string): string { - if (!filename.endsWith(".md")) { - filename += ".md"; - } - return normalizePath(join(directory, filename)); -} diff --git a/src/io/weeklyNotes.ts b/src/io/weeklyNotes.ts index 8a24375..d2eaa56 100644 --- a/src/io/weeklyNotes.ts +++ b/src/io/weeklyNotes.ts @@ -1,84 +1,13 @@ import type { Moment } from "moment"; -import { Notice, TFile } from "obsidian"; -import { getTemplateContents } from "obsidian-daily-notes-interface"; +import type { TFile } from "obsidian"; +import { + createWeeklyNote, + getWeeklyNoteSettings, +} from "obsidian-daily-notes-interface"; -import { getWeeklyNoteSettings, ISettings } from "src/settings"; +import type { ISettings } from "src/settings"; import { createConfirmationDialog } from "src/ui/modal"; -import { getNotePath } from "./path"; - -function getDaysOfWeek(): string[] { - const { moment } = window; - // eslint-disable-next-line @typescript-eslint/no-explicit-any - let weekStart = (moment.localeData())._week.dow; - const daysOfWeek = [ - "sunday", - "monday", - "tuesday", - "wednesday", - "thursday", - "friday", - "saturday", - ]; - - while (weekStart) { - daysOfWeek.push(daysOfWeek.shift()); - weekStart--; - } - return daysOfWeek; -} - -export function getDayOfWeekNumericalValue(dayOfWeekName: string): number { - return getDaysOfWeek().indexOf(dayOfWeekName.toLowerCase()); -} - -export async function createWeeklyNote( - date: Moment, - settings: ISettings -): Promise { - const { vault } = window.app; - const { template, format, folder } = getWeeklyNoteSettings(settings); - const templateContents = await getTemplateContents(template); - const filename = date.format(format); - const normalizedPath = getNotePath(folder, filename); - - try { - const createdFile = await vault.create( - normalizedPath, - templateContents - .replace( - /{{\s*(date|time)\s*:(.*?)}}/gi, - (_, _timeOrDate, momentFormat) => { - return date.format(momentFormat.trim()); - } - ) - .replace(/{{\s*title\s*}}/gi, filename) - .replace( - /{{\s*(sunday|monday|tuesday|wednesday|thursday|friday|saturday)\s*:(.*?)}}/gi, - (_, dayOfWeek, momentFormat) => { - const day = getDayOfWeekNumericalValue(dayOfWeek); - return date.weekday(day).format(momentFormat.trim()); - } - ) - ); - return createdFile; - } catch (err) { - console.error(`Failed to create file: '${normalizedPath}'`, err); - new Notice("Unable to create new file."); - } -} - -export function getWeeklyNote(date: Moment, settings: ISettings): TFile { - const { vault } = window.app; - const { format, folder } = getWeeklyNoteSettings(settings); - - const startOfWeek = date.clone().startOf("week"); - const baseFilename = startOfWeek.format(format); - - const fullPath = getNotePath(folder, baseFilename); - return vault.getAbstractFileByPath(fullPath) as TFile; -} - /** * Create a Weekly Note for a given date. */ @@ -89,11 +18,11 @@ export async function tryToCreateWeeklyNote( cb?: (file: TFile) => void ): Promise { const { workspace } = window.app; - const { format } = getWeeklyNoteSettings(settings); + const { format } = getWeeklyNoteSettings(); const filename = date.format(format); const createFile = async () => { - const dailyNote = await createWeeklyNote(date, settings); + const dailyNote = await createWeeklyNote(date); const leaf = inNewSplit ? workspace.splitActiveLeaf() : workspace.getUnpinnedLeaf(); diff --git a/src/settings.ts b/src/settings.ts index 4625f1e..e6639a2 100644 --- a/src/settings.ts +++ b/src/settings.ts @@ -1,8 +1,5 @@ import { App, PluginSettingTab, Setting } from "obsidian"; -import { - appHasDailyNotesPluginLoaded, - IDailyNoteSettings, -} from "obsidian-daily-notes-interface"; +import { appHasDailyNotesPluginLoaded } from "obsidian-daily-notes-interface"; import type { ILocaleOverride, IWeekStartOption } from "obsidian-calendar-ui"; import { DEFAULT_WEEK_FORMAT, DEFAULT_WORDS_PER_DOT } from "src/constants"; @@ -33,16 +30,6 @@ const weekdays = [ "saturday", ]; -export function getWeeklyNoteSettings(settings: ISettings): IDailyNoteSettings { - return { - format: settings.weeklyNoteFormat || DEFAULT_WEEK_FORMAT, - folder: settings.weeklyNoteFolder ? settings.weeklyNoteFolder.trim() : "", - template: settings.weeklyNoteTemplate - ? settings.weeklyNoteTemplate.trim() - : "", - }; -} - export const defaultSettings = Object.freeze({ shouldConfirmBeforeCreate: true, weekStart: "locale" as IWeekStartOption, @@ -57,6 +44,12 @@ export const defaultSettings = Object.freeze({ localeOverride: "system-default", }); +function appHasPeriodicNotesPluginLoaded() { + // eslint-disable-next-line @typescript-eslint/no-explicit-any + const periodicNotes = (window.app).plugins.getPlugin("periodic-notes"); + return periodicNotes && periodicNotes.settings?.weekly?.enabled; +} + export class CalendarSettingsTab extends PluginSettingTab { private plugin: CalendarPlugin; @@ -69,12 +62,15 @@ export class CalendarSettingsTab extends PluginSettingTab { this.containerEl.empty(); if (!appHasDailyNotesPluginLoaded()) { - this.containerEl.createEl("h3", { - text: "⚠️ Daily Notes plugin not enabled", - }); - this.containerEl.createEl("p", { - text: - "The calendar is best used in conjunction with the Daily Notes plugin. Enable it in your plugin settings for a more optimal experience.", + this.containerEl.createDiv("settings-banner", (banner) => { + banner.createEl("h3", { + text: "⚠️ Daily Notes plugin not enabled", + }); + banner.createEl("p", { + cls: "setting-item-description", + text: + "The calendar is best used in conjunction with either the Daily Notes plugin or the Periodic Notes plugin (available in the Community Plugins catalog).", + }); }); } @@ -86,10 +82,18 @@ export class CalendarSettingsTab extends PluginSettingTab { this.addConfirmCreateSetting(); this.addShowWeeklyNoteSetting(); - if (this.plugin.options.showWeeklyNote) { + if ( + this.plugin.options.showWeeklyNote && + !appHasPeriodicNotesPluginLoaded() + ) { this.containerEl.createEl("h3", { text: "Weekly Note Settings", }); + this.containerEl.createEl("p", { + cls: "setting-item-description", + text: + "Note: Weekly Note settings are moving. You are encouraged to install the 'Periodic Notes' plugin to keep the functionality in the future.", + }); this.addWeeklyNoteFormatSetting(); this.addWeeklyNoteTemplateSetting(); this.addWeeklyNoteFolderSetting(); diff --git a/src/ui/Calendar.svelte b/src/ui/Calendar.svelte index 42778ef..d2ab227 100644 --- a/src/ui/Calendar.svelte +++ b/src/ui/Calendar.svelte @@ -10,7 +10,7 @@ import { onDestroy } from "svelte"; import type { ISettings } from "src/settings"; - import { activeFile, dailyNotes, settings } from "./stores"; + import { activeFile, dailyNotes, settings, weeklyNotes } from "./stores"; let today: Moment; @@ -32,6 +32,7 @@ function getToday(settings: ISettings) { configureGlobalMomentLocale(settings.localeOverride, settings.weekStart); dailyNotes.reindex(); + weeklyNotes.reindex(); return window.moment(); } diff --git a/src/ui/sources/streak.ts b/src/ui/sources/streak.ts index 31c2339..3448b51 100644 --- a/src/ui/sources/streak.ts +++ b/src/ui/sources/streak.ts @@ -1,12 +1,10 @@ import type { Moment } from "moment"; import type { TFile } from "obsidian"; import type { ICalendarSource, IDayMetadata } from "obsidian-calendar-ui"; -import { getDailyNote } from "obsidian-daily-notes-interface"; +import { getDailyNote, getWeeklyNote } from "obsidian-daily-notes-interface"; import { get } from "svelte/store"; -import { getWeeklyNote } from "src/io/weeklyNotes"; - -import { dailyNotes, settings } from "../stores"; +import { dailyNotes, weeklyNotes } from "../stores"; import { classList } from "../utils"; const getStreakClasses = (file: TFile): string[] => { @@ -25,7 +23,7 @@ export const streakSource: ICalendarSource = { }, getWeeklyMetadata: async (date: Moment): Promise => { - const file = getWeeklyNote(date, get(settings)); + const file = getWeeklyNote(date, get(weeklyNotes)); return { classes: getStreakClasses(file), dots: [], diff --git a/src/ui/sources/tags.ts b/src/ui/sources/tags.ts index 8e20080..5c768c7 100644 --- a/src/ui/sources/tags.ts +++ b/src/ui/sources/tags.ts @@ -1,13 +1,12 @@ import type { Moment } from "moment"; import { parseFrontMatterTags, TFile } from "obsidian"; import type { ICalendarSource, IDayMetadata } from "obsidian-calendar-ui"; -import { getDailyNote } from "obsidian-daily-notes-interface"; +import { getDailyNote, getWeeklyNote } from "obsidian-daily-notes-interface"; import { get } from "svelte/store"; -import { getWeeklyNote } from "src/io/weeklyNotes"; import { partition } from "src/ui/utils"; -import { dailyNotes, settings } from "../stores"; +import { dailyNotes, weeklyNotes } from "../stores"; function getNoteTags(note: TFile | null): string[] { if (!note) { @@ -57,7 +56,7 @@ export const customTagsSource: ICalendarSource = { }; }, getWeeklyMetadata: async (date: Moment): Promise => { - const file = getWeeklyNote(date, get(settings)); + const file = getWeeklyNote(date, get(weeklyNotes)); return { dataAttributes: getFormattedTagAttributes(file), dots: [], diff --git a/src/ui/sources/tasks.ts b/src/ui/sources/tasks.ts index 2ae4848..ccbade3 100644 --- a/src/ui/sources/tasks.ts +++ b/src/ui/sources/tasks.ts @@ -1,12 +1,10 @@ import type { Moment } from "moment"; import type { TFile } from "obsidian"; import type { ICalendarSource, IDayMetadata, IDot } from "obsidian-calendar-ui"; -import { getDailyNote } from "obsidian-daily-notes-interface"; +import { getDailyNote, getWeeklyNote } from "obsidian-daily-notes-interface"; import { get } from "svelte/store"; -import { getWeeklyNote } from "src/io/weeklyNotes"; - -import { dailyNotes, settings } from "../stores"; +import { dailyNotes, weeklyNotes } from "../stores"; export async function getNumberOfRemainingTasks(note: TFile): Promise { if (!note) { @@ -47,7 +45,7 @@ export const tasksSource: ICalendarSource = { }, getWeeklyMetadata: async (date: Moment): Promise => { - const file = getWeeklyNote(date, get(settings)); + const file = getWeeklyNote(date, get(weeklyNotes)); const dots = await getDotsForDailyNote(file); return { diff --git a/src/ui/sources/wordCount.ts b/src/ui/sources/wordCount.ts index 72dc0f3..d8d06f3 100644 --- a/src/ui/sources/wordCount.ts +++ b/src/ui/sources/wordCount.ts @@ -1,13 +1,12 @@ import type { Moment } from "moment"; import type { TFile } from "obsidian"; import type { ICalendarSource, IDayMetadata, IDot } from "obsidian-calendar-ui"; -import { getDailyNote } from "obsidian-daily-notes-interface"; +import { getDailyNote, getWeeklyNote } from "obsidian-daily-notes-interface"; import { get } from "svelte/store"; import { DEFAULT_WORDS_PER_DOT } from "src/constants"; -import { getWeeklyNote } from "src/io/weeklyNotes"; -import { dailyNotes, settings } from "../stores"; +import { dailyNotes, settings, weeklyNotes } from "../stores"; import { clamp, getWordCount } from "../utils"; const NUM_MAX_DOTS = 5; @@ -52,7 +51,7 @@ export const wordCountSource: ICalendarSource = { }, getWeeklyMetadata: async (date: Moment): Promise => { - const file = getWeeklyNote(date, get(settings)); + const file = getWeeklyNote(date, get(weeklyNotes)); const dots = await getDotsForDailyNote(file); return { diff --git a/src/ui/stores.ts b/src/ui/stores.ts index 17f705b..e6917b9 100644 --- a/src/ui/stores.ts +++ b/src/ui/stores.ts @@ -1,6 +1,9 @@ import { Notice, TFile } from "obsidian"; -import { getAllDailyNotes } from "obsidian-daily-notes-interface"; -import { get, writable } from "svelte/store"; +import { + getAllDailyNotes, + getAllWeeklyNotes, +} from "obsidian-daily-notes-interface"; +import { writable } from "svelte/store"; import { defaultSettings, ISettings } from "src/settings"; @@ -18,7 +21,7 @@ function createDailyNotesStore() { } catch (err) { if (!hasError) { // Avoid error being shown multiple times - new Notice("Failed to find your Daily Note folder"); + new Notice("Failed to find your daily note folder"); console.log("[Calendar] Failed to find daily notes folder", err); } store.set({}); @@ -29,15 +32,39 @@ function createDailyNotesStore() { }; } +function createWeeklyNotesStore() { + let hasError = false; + const store = writable>(null); + return { + reindex: () => { + try { + const weeklyNotes = getAllWeeklyNotes(); + store.set(weeklyNotes); + hasError = false; + } catch (err) { + if (!hasError) { + // Avoid error being shown multiple times + new Notice("Failed to find your weekly note folder"); + console.log("[Calendar] Failed to find weekly notes folder", err); + } + store.set({}); + hasError = true; + } + }, + ...store, + }; +} + export const settings = writable(defaultSettings); export const dailyNotes = createDailyNotesStore(); +export const weeklyNotes = createWeeklyNotesStore(); function createSelectedFileStore() { const store = writable(null); return { setFile: (file: TFile) => { - const id = getDateUIDFromFile(file, get(settings)); + const id = getDateUIDFromFile(file); store.set(id); }, ...store, diff --git a/src/ui/utils.ts b/src/ui/utils.ts index 77ce6ea..084a08e 100644 --- a/src/ui/utils.ts +++ b/src/ui/utils.ts @@ -1,10 +1,6 @@ import type { TFile } from "obsidian"; import { getDateFromFile, getDateUID } from "obsidian-daily-notes-interface"; -import { getWeeklyNoteSettings } from "src/settings"; - -import type { ISettings } from "../settings"; - export const classList = (obj: Record): string[] => { return Object.entries(obj) .filter(([_k, v]) => !!v) @@ -43,24 +39,19 @@ export function partition( * * @param file */ -export function getDateUIDFromFile( - file: TFile | null, - settings: ISettings -): string { +export function getDateUIDFromFile(file: TFile | null): string { if (!file) { return null; } // TODO: I'm not checking the path! - let date = getDateFromFile(file); + let date = getDateFromFile(file, "day"); if (date) { return getDateUID(date, "day"); } - // Check to see if the active note is a weekly-note - const format = getWeeklyNoteSettings(settings).format; - date = window.moment(file.basename, format, true); - if (date.isValid()) { + date = getDateFromFile(file, "week"); + if (date) { return getDateUID(date, "week"); } return null; diff --git a/src/view.ts b/src/view.ts index d3e8b82..ccfea20 100644 --- a/src/view.ts +++ b/src/view.ts @@ -3,18 +3,20 @@ import { getDailyNote, getDailyNoteSettings, getDateFromFile, + getWeeklyNote, + getWeeklyNoteSettings, } from "obsidian-daily-notes-interface"; import { FileView, TFile, ItemView, WorkspaceLeaf } from "obsidian"; import { get } from "svelte/store"; import { TRIGGER_ON_OPEN, VIEW_TYPE_CALENDAR } from "src/constants"; import { tryToCreateDailyNote } from "src/io/dailyNotes"; -import { getWeeklyNote, tryToCreateWeeklyNote } from "src/io/weeklyNotes"; -import { getWeeklyNoteSettings, ISettings } from "src/settings"; +import { tryToCreateWeeklyNote } from "src/io/weeklyNotes"; +import type { ISettings } from "src/settings"; import Calendar from "./ui/Calendar.svelte"; import { showFileMenu } from "./ui/fileMenu"; -import { activeFile, dailyNotes, settings } from "./ui/stores"; +import { activeFile, dailyNotes, weeklyNotes, settings } from "./ui/stores"; import { customTagsSource, streakSource, @@ -32,6 +34,7 @@ export default class CalendarView extends ItemView { this.openOrCreateDailyNote = this.openOrCreateDailyNote.bind(this); this.openOrCreateWeeklyNote = this.openOrCreateWeeklyNote.bind(this); + this.onNoteSettingsUpdate = this.onNoteSettingsUpdate.bind(this); this.onFileCreated = this.onFileCreated.bind(this); this.onFileDeleted = this.onFileDeleted.bind(this); this.onFileModified = this.onFileModified.bind(this); @@ -43,6 +46,13 @@ export default class CalendarView extends ItemView { this.onContextMenuDay = this.onContextMenuDay.bind(this); this.onContextMenuWeek = this.onContextMenuWeek.bind(this); + this.registerEvent( + // eslint-disable-next-line @typescript-eslint/no-explicit-any + (this.app.workspace).on( + "periodic-notes:settings-updated", + this.onNoteSettingsUpdate + ) + ); this.registerEvent(this.app.vault.on("create", this.onFileCreated)); this.registerEvent(this.app.vault.on("delete", this.onFileDeleted)); this.registerEvent(this.app.vault.on("modify", this.onFileModified)); @@ -131,8 +141,8 @@ export default class CalendarView extends ItemView { if (!isMetaPressed) { return; } - const note = getWeeklyNote(date, this.settings); - const { format } = getWeeklyNoteSettings(this.settings); + const note = getWeeklyNote(date, get(weeklyNotes)); + const { format } = getWeeklyNoteSettings(); this.app.workspace.trigger( "link-hover", this, @@ -155,7 +165,7 @@ export default class CalendarView extends ItemView { } private onContextMenuWeek(date: Moment, event: MouseEvent): void { - const note = getWeeklyNote(date, this.settings); + const note = getWeeklyNote(date, get(weeklyNotes)); if (!note) { // If no file exists for a given day, show nothing. return; @@ -166,28 +176,40 @@ export default class CalendarView extends ItemView { }); } + private onNoteSettingsUpdate(): void { + dailyNotes.reindex(); + weeklyNotes.reindex(); + this.updateActiveFile(); + } + private async onFileDeleted(file: TFile): Promise { - const date = getDateFromFile(file); - if (date) { + if (getDateFromFile(file, "day")) { dailyNotes.reindex(); this.updateActiveFile(); } + if (getDateFromFile(file, "week")) { + weeklyNotes.reindex(); + this.updateActiveFile(); + } } private async onFileModified(file: TFile): Promise { - const date = getDateFromFile(file); + const date = getDateFromFile(file, "day") || getDateFromFile(file, "week"); if (date && this.calendar) { this.calendar.tick(); } } private onFileCreated(file: TFile): void { - if (this.app.workspace.layoutReady) { - const date = getDateFromFile(file); - if (date && this.calendar) { + if (this.app.workspace.layoutReady && this.calendar) { + if (getDateFromFile(file, "day")) { dailyNotes.reindex(); this.calendar.tick(); } + if (getDateFromFile(file, "week")) { + weeklyNotes.reindex(); + this.calendar.tick(); + } } } @@ -217,14 +239,14 @@ export default class CalendarView extends ItemView { if (activeLeaf.view instanceof FileView) { // Check to see if the active note is a daily-note - let date = getDateFromFile(activeLeaf.view.file); + let date = getDateFromFile(activeLeaf.view.file, "day"); if (date) { this.calendar.$set({ displayedMonth: date }); return; } // Check to see if the active note is a weekly-note - const format = getWeeklyNoteSettings(this.settings).format; + const { format } = getWeeklyNoteSettings(); date = moment(activeLeaf.view.file.basename, format, true); if (date.isValid()) { this.calendar.$set({ displayedMonth: date }); @@ -241,7 +263,7 @@ export default class CalendarView extends ItemView { const startOfWeek = date.clone().startOf("week"); - const existingFile = getWeeklyNote(date, this.settings); + const existingFile = getWeeklyNote(date, get(weeklyNotes)); if (!existingFile) { // File doesn't exist diff --git a/styles.css b/styles.css new file mode 100644 index 0000000..9dee433 --- /dev/null +++ b/styles.css @@ -0,0 +1,11 @@ +.settings-banner { + background-color: var(--background-secondary-alt); + border-radius: 4px; + margin-bottom: 2em; + padding: 1.5em; + text-align: left; +} + +.settings-banner h4 { + margin: 0; +} diff --git a/yarn.lock b/yarn.lock index 8157947..20c43ab 100644 --- a/yarn.lock +++ b/yarn.lock @@ -3314,19 +3314,19 @@ object.pick@^1.3.0: dependencies: isobject "^3.0.1" -obsidian-calendar-ui@0.3.0: - version "0.3.0" - resolved "https://registry.yarnpkg.com/obsidian-calendar-ui/-/obsidian-calendar-ui-0.3.0.tgz#3c3af76a6995d2b17e7185d19b1fdf180e9106ce" - integrity sha512-C07BIq04m+jKF0foZOB6dzxRXHRDFJs2vDYEH97qV2iKC00TOYHwwFg8PgRNQARs+TZGRGI3Tn05xIzy9c7Jdg== +obsidian-calendar-ui@0.3.1: + version "0.3.1" + resolved "https://registry.yarnpkg.com/obsidian-calendar-ui/-/obsidian-calendar-ui-0.3.1.tgz#cb9b6a88bb07a02e677c65296417a56cca706830" + integrity sha512-vQiFQDNC5wfYFnOIaZl1cbcKtS8FrVfClyV4/UPwUrk59Fr6t7oE1L3bAm2T3ipo51/nsXpkX47xeb7YSDCSMA== dependencies: - obsidian-daily-notes-interface "0.5.1" + obsidian-daily-notes-interface "0.7.0" svelte "3.31.0" tslib "2.0.3" -obsidian-daily-notes-interface@0.5.1: - version "0.5.1" - resolved "https://registry.yarnpkg.com/obsidian-daily-notes-interface/-/obsidian-daily-notes-interface-0.5.1.tgz#8fe28eab46e7fb789c8fc935dd3c74232b6e15f9" - integrity sha512-QwSAN7ob6r05e+IFhIY1Knn1H2zM4p3MCmUdbtNOFxPe0MQWsLAK5fjt0ST1usm7z0rVuTbNHJAzXxNRQaDlzg== +obsidian-daily-notes-interface@0.7.0: + version "0.7.0" + resolved "https://registry.yarnpkg.com/obsidian-daily-notes-interface/-/obsidian-daily-notes-interface-0.7.0.tgz#ad627987583e2dddd8ec911c57f2bc355ef52d1b" + integrity sha512-mSck8eMN7z5afeMrtVTLxUpP0GbBfciSZnoo6EyZige8ItknKQKqDRLlEoEZGEy49iExKUdQbOqvNdX7yQifPA== dependencies: obsidian obsidianmd/obsidian-api#master tslib "2.0.3"