feat: add date systems menu to calendar days
Right-clicking a calendar day now lists every configured date-prefixed note system. Journal notes are singletons; {title} formats support multiple notes per date, such as meetings.\n\nAdds default Journal and Meeting systems, configurable settings UI, safe filename/title handling, one shared dated-note creation path, and a compiled menu smoke test fixture in the test vault.
This commit is contained in:
1 parent
b617c275eb
commit
26a1a481ac
10 files changed
+731
-76
No files matched your search
+162
-24
@@ -10,14 +10,38 @@ import {
|
||||
getAllTags,
|
||||
moment,
|
||||
} from 'obsidian';
|
||||
import { WaypointSettings, DEFAULT_SETTINGS, PeriodNoteSettings } from 'src/settings';
|
||||
import { WaypointSettings, DEFAULT_SETTINGS, DateSystemSettings, DEFAULT_DATE_SYSTEM } from 'src/settings';
|
||||
import { WaypointSettingTab } from 'src/settings-tab';
|
||||
import { WaypointView, WAYPOINT_VIEW_TYPE } from 'src/views/waypoint-view';
|
||||
import { BookmarkItem, WaypointData } from 'src/models/bookmark';
|
||||
import { remapRenamedPath } from 'src/utils/path-utils';
|
||||
import {
|
||||
splitNameFormat,
|
||||
isInFolder,
|
||||
matchesSystemName,
|
||||
titleFromBasename,
|
||||
sanitizeTitle,
|
||||
formatHasDateToken,
|
||||
} from 'src/utils/date-systems';
|
||||
|
||||
export type PeriodKey = 'day' | 'week' | 'month' | 'quarter' | 'year';
|
||||
|
||||
/** One existing note of a date system, with the label the menu should show. */
|
||||
export interface DateSystemNote {
|
||||
file: TFile;
|
||||
/** Free-text title for many-per-date systems, else the system name. */
|
||||
label: string;
|
||||
}
|
||||
|
||||
/** A day-scoped system and the notes it already holds for one date. */
|
||||
export interface DateSystemNotes {
|
||||
system: DateSystemSettings;
|
||||
/** Sorted by basename. Empty when the date has no note in this system. */
|
||||
notes: DateSystemNote[];
|
||||
/** True when nameFormat carries {title}, i.e. many notes per date. */
|
||||
multiple: boolean;
|
||||
}
|
||||
|
||||
export default class WaypointPlugin extends Plugin {
|
||||
public settings: WaypointSettings;
|
||||
public waypointData: WaypointData;
|
||||
@@ -221,6 +245,13 @@ export default class WaypointPlugin extends Plugin {
|
||||
for (const key of PERIOD_SETTING_KEYS) {
|
||||
this.settings[key] = Object.assign({}, DEFAULT_SETTINGS[key], s[key] || {});
|
||||
}
|
||||
// Cloned, not Object.assign'd in: the defaults array would otherwise be
|
||||
// aliased into the live settings and the settings UI would edit
|
||||
// DEFAULT_SETTINGS itself. Saved entries merge over
|
||||
// DEFAULT_DATE_SYSTEM so older configs pick up fields added since.
|
||||
this.settings.dateSystems = Array.isArray(s.dateSystems)
|
||||
? s.dateSystems.map(sys => Object.assign({}, DEFAULT_DATE_SYSTEM, sys))
|
||||
: DEFAULT_SETTINGS.dateSystems.map(sys => Object.assign({}, sys));
|
||||
}
|
||||
|
||||
private applyWaypointData(saved: Record<string, unknown> | null): void {
|
||||
@@ -510,44 +541,142 @@ export default class WaypointPlugin extends Plugin {
|
||||
}
|
||||
}
|
||||
|
||||
// ── Period note creation/opening ──
|
||||
// ── Date systems: discovery, creation, opening ──
|
||||
|
||||
/** Periodic notes are date systems with a fixed one-note-per-period format. */
|
||||
private periodAsDateSystem(period: PeriodKey): DateSystemSettings {
|
||||
const config = PERIOD_CONFIGS[period];
|
||||
const periodSettings = this.settings[config.key];
|
||||
return {
|
||||
id: config.key,
|
||||
name: config.label,
|
||||
folder: periodSettings.folder,
|
||||
nameFormat: periodSettings.nameFormat,
|
||||
templateFile: periodSettings.templateFile,
|
||||
typeProperty: periodSettings.typeProperty,
|
||||
icon: 'calendar',
|
||||
};
|
||||
}
|
||||
|
||||
/** All day-scoped systems: the daily periodic note first, then settings.dateSystems. */
|
||||
dateSystems(): DateSystemSettings[] {
|
||||
return [this.periodAsDateSystem('day'), ...this.settings.dateSystems];
|
||||
}
|
||||
|
||||
/**
|
||||
* Bucket the notes every day-scoped system already holds for `date`.
|
||||
*
|
||||
* One vault scan serves all systems at once. That is affordable because
|
||||
* this runs once per right-click, not once per calendar cell, and it keeps
|
||||
* arbitrary user-defined name formats out of the incremental index.
|
||||
*/
|
||||
findDateSystemNotes(date: moment.Moment): DateSystemNotes[] {
|
||||
const buckets = this.dateSystems().map(system => {
|
||||
const parts = splitNameFormat(system.nameFormat);
|
||||
return {
|
||||
result: { system, notes: [] as DateSystemNote[], multiple: parts.hasTitle },
|
||||
// Each half is formatted on its own: the raw format may still
|
||||
// hold {title}, whose letters are live moment tokens.
|
||||
before: formatDatePart(date, parts.before),
|
||||
after: formatDatePart(date, parts.after),
|
||||
hasTitle: parts.hasTitle,
|
||||
// Empty/date-less formats have no date-specific filename. They stay
|
||||
// editable in Settings but must not contribute a menu action.
|
||||
skip: !formatHasDateToken(system.nameFormat),
|
||||
};
|
||||
});
|
||||
|
||||
for (const file of this.app.vault.getMarkdownFiles()) {
|
||||
for (const bucket of buckets) {
|
||||
if (bucket.skip) continue;
|
||||
const system = bucket.result.system;
|
||||
if (!isInFolder(file.path, system.folder)) continue;
|
||||
if (!matchesSystemName(file.basename, bucket.before, bucket.after, bucket.hasTitle)) continue;
|
||||
bucket.result.notes.push({
|
||||
file,
|
||||
label: bucket.hasTitle
|
||||
? titleFromBasename(file.basename, bucket.before, bucket.after)
|
||||
: system.name,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
for (const bucket of buckets) {
|
||||
bucket.result.notes.sort((a, b) => a.file.basename.localeCompare(b.file.basename));
|
||||
}
|
||||
return buckets
|
||||
.filter(bucket => !bucket.skip)
|
||||
.map(bucket => bucket.result);
|
||||
}
|
||||
|
||||
/**
|
||||
* Open (creating if needed) the note `system` holds for `date`.
|
||||
*
|
||||
* `opts.title` fills the `{title}` token of a many-per-date system and is
|
||||
* ignored by systems without one. Opens in `opts.leaf` when given,
|
||||
* otherwise in the active leaf.
|
||||
*/
|
||||
async openDateSystemNote(
|
||||
system: DateSystemSettings,
|
||||
date: moment.Moment,
|
||||
opts?: { title?: string; leaf?: WorkspaceLeaf },
|
||||
): Promise<void> {
|
||||
|
||||
if (!formatHasDateToken(system.nameFormat)) {
|
||||
new Notice(`Waypoint: ${system.name || 'This'} name format needs a date placeholder.`);
|
||||
return;
|
||||
}
|
||||
const parts = splitNameFormat(system.nameFormat);
|
||||
const before = formatDatePart(date, parts.before);
|
||||
const after = formatDatePart(date, parts.after);
|
||||
|
||||
let basename: string;
|
||||
if (parts.hasTitle) {
|
||||
const title = sanitizeTitle(opts?.title || '');
|
||||
// Public method, so a caller can legitimately hand us nothing;
|
||||
// creating "2026-09-07 - .md" would be worse than refusing.
|
||||
if (!title) {
|
||||
new Notice(`Waypoint: a ${system.name.toLowerCase()} note needs a title.`);
|
||||
return;
|
||||
}
|
||||
basename = before + title + after;
|
||||
} else {
|
||||
basename = before + after;
|
||||
}
|
||||
|
||||
const fullPath = system.folder ? `${system.folder}/${basename}.md` : `${basename}.md`;
|
||||
|
||||
let file = this.app.vault.getFileByPath(fullPath);
|
||||
if (!file) {
|
||||
file = await this.createDatedNote(fullPath, system, date);
|
||||
if (!file) return;
|
||||
}
|
||||
|
||||
const target = opts?.leaf || this.app.workspace.getLeaf(false);
|
||||
await target.openFile(file);
|
||||
}
|
||||
|
||||
/**
|
||||
* Open (creating if needed) the period note for `date`.
|
||||
* Opens in `leaf` when given, otherwise in the active leaf.
|
||||
*/
|
||||
async openPeriodNote(period: PeriodKey, date: moment.Moment, leaf?: WorkspaceLeaf): Promise<void> {
|
||||
const config = PERIOD_CONFIGS[period];
|
||||
const periodSettings = this.settings[config.key];
|
||||
const filename = date.format(periodSettings.nameFormat) + '.md';
|
||||
const fullPath = periodSettings.folder
|
||||
? `${periodSettings.folder}/${filename}`
|
||||
: filename;
|
||||
|
||||
let file = this.app.vault.getFileByPath(fullPath);
|
||||
if (!file) {
|
||||
file = await this.createPeriodNote(fullPath, periodSettings, date, config.label);
|
||||
if (!file) return;
|
||||
}
|
||||
|
||||
const target = leaf || this.app.workspace.getLeaf(false);
|
||||
await target.openFile(file);
|
||||
await this.openDateSystemNote(this.periodAsDateSystem(period), date, { leaf });
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a period note from its template, or from minimal frontmatter.
|
||||
* Create a date system's note from its template, or from minimal frontmatter.
|
||||
*
|
||||
* Every failure path names the offending path and the reason: the usual
|
||||
* cause is a configured folder that does not exist yet, which `vault.create`
|
||||
* refuses outright rather than creating.
|
||||
*/
|
||||
private async createPeriodNote(
|
||||
private async createDatedNote(
|
||||
fullPath: string,
|
||||
periodSettings: PeriodNoteSettings,
|
||||
system: DateSystemSettings,
|
||||
date: moment.Moment,
|
||||
label: string,
|
||||
): Promise<TFile | null> {
|
||||
const noun = label.toLowerCase();
|
||||
const noun = system.name.toLowerCase();
|
||||
const slash = fullPath.lastIndexOf('/');
|
||||
const folder = slash < 0 ? '' : fullPath.slice(0, slash);
|
||||
|
||||
@@ -563,7 +692,7 @@ export default class WaypointPlugin extends Plugin {
|
||||
return null;
|
||||
}
|
||||
|
||||
const configuredTemplate = periodSettings.templateFile;
|
||||
const configuredTemplate = system.templateFile;
|
||||
const templateFile = this.resolveTemplateFile(configuredTemplate);
|
||||
|
||||
let content: string;
|
||||
@@ -579,7 +708,7 @@ export default class WaypointPlugin extends Plugin {
|
||||
return null;
|
||||
}
|
||||
} else {
|
||||
content = `---\ntype: ${periodSettings.typeProperty}\ndate: ${date.format('YYYY-MM-DD')}\n---\n\n`;
|
||||
content = `---\ntype: ${system.typeProperty}\ndate: ${date.format('YYYY-MM-DD')}\n---\n\n`;
|
||||
}
|
||||
|
||||
let file: TFile;
|
||||
@@ -733,3 +862,12 @@ const DIAGNOSTIC_NOTICE_MS = 10000;
|
||||
function describeError(err: unknown): string {
|
||||
return err instanceof Error ? err.message : String(err);
|
||||
}
|
||||
|
||||
/**
|
||||
* Format one half of a split name format. Moment falls back to its default
|
||||
* ISO output when handed an empty format string, so an empty half must never
|
||||
* reach it.
|
||||
*/
|
||||
function formatDatePart(date: moment.Moment, part: string): string {
|
||||
return part ? date.format(part) : '';
|
||||
}
|
||||
Reference in new issue
Block a user