diff --git a/src/constants.ts b/src/constants.ts index 948864e..e19420c 100644 --- a/src/constants.ts +++ b/src/constants.ts @@ -3,3 +3,11 @@ export const DEFAULT_WEEKLY_NOTE_FORMAT = "gggg-[W]ww"; export const DEFAULT_MONTHLY_NOTE_FORMAT = "YYYY-MM"; export const DEFAULT_QUARTERLY_NOTE_FORMAT = "YYYY-[Q]Q"; export const DEFAULT_YEARLY_NOTE_FORMAT = "YYYY"; + +export const DEFAULT_FORMAT = Object.freeze({ + day: DEFAULT_DAILY_NOTE_FORMAT, + week: DEFAULT_WEEKLY_NOTE_FORMAT, + month: DEFAULT_MONTHLY_NOTE_FORMAT, + quarter: DEFAULT_QUARTERLY_NOTE_FORMAT, + year: DEFAULT_YEARLY_NOTE_FORMAT, +}); diff --git a/src/index.ts b/src/index.ts index 2b23f95..f12222d 100644 --- a/src/index.ts +++ b/src/index.ts @@ -17,9 +17,7 @@ export function appHasDailyNotesPluginLoaded(): boolean { return true; } - // eslint-disable-next-line @typescript-eslint/no-explicit-any - const periodicNotes = (app).plugins.getPlugin("periodic-notes"); - return periodicNotes && periodicNotes.settings?.daily?.enabled; + return shouldUsePeriodicNotesPluginSettings("day"); } /** @@ -28,36 +26,24 @@ export function appHasDailyNotesPluginLoaded(): boolean { */ export function appHasWeeklyNotesPluginLoaded(): boolean { const { app } = window; - // eslint-disable-next-line @typescript-eslint/no-explicit-any if ((app).plugins.getPlugin("calendar")) { return true; } - // eslint-disable-next-line @typescript-eslint/no-explicit-any - const periodicNotes = (app).plugins.getPlugin("periodic-notes"); - return periodicNotes && periodicNotes.settings?.weekly?.enabled; + return shouldUsePeriodicNotesPluginSettings("week"); } export function appHasMonthlyNotesPluginLoaded(): boolean { - const { app } = window; - // eslint-disable-next-line @typescript-eslint/no-explicit-any - const periodicNotes = (app).plugins.getPlugin("periodic-notes"); - return periodicNotes && periodicNotes.settings?.monthly?.enabled; + return shouldUsePeriodicNotesPluginSettings("month"); } export function appHasQuarterlyNotesPluginLoaded(): boolean { - const { app } = window; - // eslint-disable-next-line @typescript-eslint/no-explicit-any - const periodicNotes = (app).plugins.getPlugin("periodic-notes"); - return periodicNotes && periodicNotes.settings?.quarterly?.enabled; + return shouldUsePeriodicNotesPluginSettings("quarter"); } export function appHasYearlyNotesPluginLoaded(): boolean { - const { app } = window; - // eslint-disable-next-line @typescript-eslint/no-explicit-any - const periodicNotes = (app).plugins.getPlugin("periodic-notes"); - return periodicNotes && periodicNotes.settings?.yearly?.enabled; + return shouldUsePeriodicNotesPluginSettings("year"); } export { @@ -70,11 +56,13 @@ export { import type { IGranularity, IPeriodicNoteSettings } from "./types"; import { + getPeriodicNoteSettings, getDailyNoteSettings, getWeeklyNoteSettings, getMonthlyNoteSettings, getQuarterlyNoteSettings, getYearlyNoteSettings, + shouldUsePeriodicNotesPluginSettings, } from "./settings"; import { createDailyNote, getDailyNote, getAllDailyNotes } from "./daily"; import { createWeeklyNote, getAllWeeklyNotes, getWeeklyNote } from "./weekly"; @@ -93,20 +81,6 @@ import { createYearlyNote, getAllYearlyNotes, getYearlyNote } from "./yearly"; export { getDateUID, getDateFromFile, getDateFromPath } from "./parse"; export { getTemplateInfo } from "./vault"; -function getPeriodicNoteSettings( - granularity: IGranularity -): IPeriodicNoteSettings { - const getSettings = { - day: getDailyNoteSettings, - week: getWeeklyNoteSettings, - month: getMonthlyNoteSettings, - quarter: getQuarterlyNoteSettings, - year: getYearlyNoteSettings, - }[granularity]; - - return getSettings(); -} - function createPeriodicNote( granularity: IGranularity, date: Moment diff --git a/src/parse.ts b/src/parse.ts index f4e0895..6190555 100644 --- a/src/parse.ts +++ b/src/parse.ts @@ -1,13 +1,7 @@ import type { Moment } from "moment"; import { TFile } from "obsidian"; -import { - getDailyNoteSettings, - getWeeklyNoteSettings, - getMonthlyNoteSettings, - getQuarterlyNoteSettings, - getYearlyNoteSettings, -} from "./settings"; +import { getPeriodicNoteSettings } from "./settings"; import { IGranularity } from "./types"; import { basename } from "./vault"; @@ -62,15 +56,8 @@ function getDateFromFilename( filename: string, granularity: IGranularity ): Moment | null { - const getSettings = { - day: getDailyNoteSettings, - week: getWeeklyNoteSettings, - month: getMonthlyNoteSettings, - quarter: getQuarterlyNoteSettings, - year: getYearlyNoteSettings, - }; - const format = getSettings[granularity]().format.split("/").pop(); + const format = getPeriodicNoteSettings(granularity).format.split("/").pop(); const noteDate = window.moment(filename, format, true); if (!noteDate.isValid()) { diff --git a/src/settings.ts b/src/settings.ts index ecc1228..1f74b38 100644 --- a/src/settings.ts +++ b/src/settings.ts @@ -1,151 +1,101 @@ -import { - DEFAULT_DAILY_NOTE_FORMAT, - DEFAULT_MONTHLY_NOTE_FORMAT, - DEFAULT_WEEKLY_NOTE_FORMAT, - DEFAULT_QUARTERLY_NOTE_FORMAT, - DEFAULT_YEARLY_NOTE_FORMAT, -} from "./constants"; -import { IPeriodicNoteSettings } from "./types"; +import { DEFAULT_FORMAT } from "./constants"; +import {IGranularity, IPeriodicity, IPeriodicNoteSettings} from "./types"; -export function shouldUsePeriodicNotesSettings( - periodicity: "daily" | "weekly" | "monthly" | "quarterly" | "yearly" -): boolean { +function getPeriodicNotesPlugin() { + const { app } = window; // eslint-disable-next-line @typescript-eslint/no-explicit-any - const periodicNotes = (window.app).plugins.getPlugin("periodic-notes"); - return periodicNotes && periodicNotes.settings?.[periodicity]?.enabled; + return (app).plugins.getPlugin("periodic-notes"); } -/** - * Read the user settings for the `daily-notes` plugin - * to keep behavior of creating a new note in-sync. - */ -export function getDailyNoteSettings(): IPeriodicNoteSettings { - try { - // eslint-disable-next-line @typescript-eslint/no-explicit-any - const { internalPlugins, plugins } = window.app; +export function hasPeriodicNotesPlugin(): boolean { + return Boolean(getPeriodicNotesPlugin()); +} - if (shouldUsePeriodicNotesSettings("daily")) { - const { format, folder, template } = - plugins.getPlugin("periodic-notes")?.settings?.daily || {}; - return { - format: format || DEFAULT_DAILY_NOTE_FORMAT, - folder: folder?.trim() || "", - template: template?.trim() || "", - }; - } +export function hasNewPeriodicNotesPlugin(): boolean { + const periodicNotes = getPeriodicNotesPlugin(); + // The CalenderSetManager is not available in the old version (0.0.17) of the Periodic Notes plugin + return periodicNotes && Object.hasOwn(periodicNotes, "calendarSetManager"); +} - const { folder, format, template } = - internalPlugins.getPluginById("daily-notes")?.instance?.options || {}; - return { - format: format || DEFAULT_DAILY_NOTE_FORMAT, - folder: folder?.trim() || "", - template: template?.trim() || "", - }; - } catch (err) { - console.info("No custom daily note settings found!", err); +export function shouldUsePeriodicNotesPluginSettings( + granularity: IGranularity +): boolean { + if(!hasPeriodicNotesPlugin()) return false; // no Periodic Notes Plugin + return (getPeriodicNotesPluginRawSettings(granularity))?.enabled; +} + +function getPeriodicNotesPluginRawSettings(granularity: IGranularity){ + const periodicNotes = getPeriodicNotesPlugin(); + if(!periodicNotes) return {}; // no Periodic Notes Plugin + if(hasNewPeriodicNotesPlugin()) { + // New Periodic Notes uses CalendarSetManager + return periodicNotes?.calendarSetManager?.getActiveSet?.()?.[granularity] || {} ; } + // Legacy settings + return periodicNotes?.settings?.[IPeriodicity[granularity]] || {} ; } -/** - * Read the user settings for the `weekly-notes` plugin - * to keep behavior of creating a new note in-sync. - */ -export function getWeeklyNoteSettings(): IPeriodicNoteSettings { +export function getPeriodicNotesPluginSettings(granularity: IGranularity) : IPeriodicNoteSettings { + const settings = getPeriodicNotesPluginRawSettings(granularity); + return { + format: settings.format || DEFAULT_FORMAT[granularity], + folder: settings.folder?.trim() || "", + template: (settings.templatePath ?? settings.template)?.trim() || "", + }; +} + + +export function getPeriodicNoteSettings(granularity: IGranularity): IPeriodicNoteSettings { try { - // eslint-disable-next-line @typescript-eslint/no-explicit-any - const pluginManager = (window.app).plugins; + // First try Periodic Notes Plugin + if (shouldUsePeriodicNotesPluginSettings(granularity)) { + return getPeriodicNotesPluginSettings(granularity); + } - const calendarSettings = pluginManager.getPlugin("calendar")?.options; - const periodicNotesSettings = - pluginManager.getPlugin("periodic-notes")?.settings?.weekly; + // For daily notes also check the Daily Notes plugin + if(granularity == "day") { + const { folder, format, template } = + // eslint-disable-next-line @typescript-eslint/no-explicit-any + (window.app).internalPlugins.getPluginById("daily-notes")?.instance?.options || {}; + return { + format: format || DEFAULT_FORMAT["day"], + folder: folder?.trim() || "", + template: template?.trim() || "", + }; + } - if (shouldUsePeriodicNotesSettings("weekly")) { + // For weekly notes also check the Calendar plugin + if (granularity == "week") { + // eslint-disable-next-line @typescript-eslint/no-explicit-any + const calendarSettings = (window.app).plugins.getPlugin("calendar")?.options || {}; return { - format: periodicNotesSettings.format || DEFAULT_WEEKLY_NOTE_FORMAT, - folder: periodicNotesSettings.folder?.trim() || "", - template: periodicNotesSettings.template?.trim() || "", + format: calendarSettings.weeklyNoteFormat || DEFAULT_FORMAT["week"], + folder: calendarSettings.weeklyNoteFolder?.trim() || "", + template: calendarSettings.weeklyNoteTemplate?.trim() || "", }; } - const settings = calendarSettings || {}; - return { - format: settings.weeklyNoteFormat || DEFAULT_WEEKLY_NOTE_FORMAT, - folder: settings.weeklyNoteFolder?.trim() || "", - template: settings.weeklyNoteTemplate?.trim() || "", - }; } catch (err) { - console.info("No custom weekly note settings found!", err); + console.info("No custom", IPeriodicity[granularity], "note settings found!", err); } } /** - * Read the user settings for the `periodic-notes` plugin + * Read the user settings for the `daily-notes` plugin * to keep behavior of creating a new note in-sync. */ +export function getDailyNoteSettings(): IPeriodicNoteSettings { + return getPeriodicNoteSettings("day"); +} +export function getWeeklyNoteSettings(): IPeriodicNoteSettings { + return getPeriodicNoteSettings("week"); +} export function getMonthlyNoteSettings(): IPeriodicNoteSettings { - // eslint-disable-next-line @typescript-eslint/no-explicit-any - const pluginManager = (window.app).plugins; - - try { - const settings = - (shouldUsePeriodicNotesSettings("monthly") && - pluginManager.getPlugin("periodic-notes")?.settings?.monthly) || - {}; - - return { - format: settings.format || DEFAULT_MONTHLY_NOTE_FORMAT, - folder: settings.folder?.trim() || "", - template: settings.template?.trim() || "", - }; - } catch (err) { - console.info("No custom monthly note settings found!", err); - } + return getPeriodicNoteSettings("month"); } - -/** - * Read the user settings for the `periodic-notes` plugin - * to keep behavior of creating a new note in-sync. - */ export function getQuarterlyNoteSettings(): IPeriodicNoteSettings { - // eslint-disable-next-line @typescript-eslint/no-explicit-any - const pluginManager = (window.app).plugins; - - try { - const settings = - (shouldUsePeriodicNotesSettings("quarterly") && - pluginManager.getPlugin("periodic-notes")?.settings?.quarterly) || - {}; - - return { - format: settings.format || DEFAULT_QUARTERLY_NOTE_FORMAT, - folder: settings.folder?.trim() || "", - template: settings.template?.trim() || "", - }; - } catch (err) { - console.info("No custom quarterly note settings found!", err); - } + return getPeriodicNoteSettings("quarter"); } - -/** - * Read the user settings for the `periodic-notes` plugin - * to keep behavior of creating a new note in-sync. - */ export function getYearlyNoteSettings(): IPeriodicNoteSettings { - // eslint-disable-next-line @typescript-eslint/no-explicit-any - const pluginManager = (window.app).plugins; - - try { - const settings = - (shouldUsePeriodicNotesSettings("yearly") && - pluginManager.getPlugin("periodic-notes")?.settings?.yearly) || - {}; - - return { - format: settings.format || DEFAULT_YEARLY_NOTE_FORMAT, - folder: settings.folder?.trim() || "", - template: settings.template?.trim() || "", - }; - } catch (err) { - console.info("No custom yearly note settings found!", err); - } + return getPeriodicNoteSettings("year"); } diff --git a/src/types.ts b/src/types.ts index ef804f5..ea6aada 100644 --- a/src/types.ts +++ b/src/types.ts @@ -1,4 +1,13 @@ export type IGranularity = "day" | "week" | "month" | "quarter" | "year"; + +export const IPeriodicity: Record = { + day: "daily", + week: "weekly", + month: "monthly", + quarter: "quarterly", + year: "yearly", +}; + export interface IPeriodicNoteSettings { folder?: string; format?: string;