Skip to content

Commit

Permalink
Adds a Create Note command (doesn't open note) & an API for userscrip…
Browse files Browse the repository at this point in the history
…t consumption
  • Loading branch information
cdloh committed Mar 27, 2023
1 parent f3d7266 commit 0dd3352
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 9 deletions.
31 changes: 31 additions & 0 deletions src/api.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import PeriodicNotesPlugin from "./main"
import { Granularity } from "./types"


export default class PeriodicNotesAPI {
static instance: PeriodicNotesAPI

public static get(plugin: PeriodicNotesPlugin) {
return {
getGranularities() {
return plugin.calendarSetManager.getActiveGranularities()
},
async createPeriodicNote(granularity: Granularity, openNote = false) {
await plugin.createOrReturnPeriodicNote(
granularity,
window.moment()
);

if(openNote) {
plugin.openPeriodicNote(granularity, window.moment())
}
},
getPeriodicNote(granularity: Granularity) {
return plugin.getPeriodicNote(
granularity,
window.moment()
);
}
}
}
}
14 changes: 13 additions & 1 deletion src/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,33 +7,39 @@ interface IDisplayConfig {
periodicity: string;
relativeUnit: string;
labelOpenPresent: string;
labelCreatePresent: string;
}

export const displayConfigs: Record<Granularity, IDisplayConfig> = {
day: {
periodicity: "daily",
relativeUnit: "today",
labelOpenPresent: "Open today's daily note",
labelCreatePresent: "Create today's daily note",
},
week: {
periodicity: "weekly",
relativeUnit: "this week",
labelOpenPresent: "Open this week's note",
labelCreatePresent: "Create this week's note",
},
month: {
periodicity: "monthly",
relativeUnit: "this month",
labelOpenPresent: "Open this month's note",
labelCreatePresent: "Create this month's note",
},
quarter: {
periodicity: "quarterly",
relativeUnit: "this quarter",
labelOpenPresent: "Open this quarter's note",
labelCreatePresent: "Create this quarter's note",
},
year: {
periodicity: "yearly",
relativeUnit: "this year",
labelOpenPresent: "Open this year's note",
labelCreatePresent: "Create this year's note",
},
};

Expand Down Expand Up @@ -100,7 +106,13 @@ export function getCommands(
name: config.labelOpenPresent,
callback: () => plugin.openPeriodicNote(granularity, window.moment()),
},

{
id: `create-${config.periodicity}-note`,
name: config.labelCreatePresent,
callback: async () => {
await plugin.createOrReturnPeriodicNote(granularity, window.moment())
},
},
{
id: `next-${config.periodicity}-note`,
name: `Jump forwards to closest ${config.periodicity} note`,
Expand Down
29 changes: 21 additions & 8 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import type { Moment } from "moment";
import { addIcon, Plugin, TFile } from "obsidian";
import { writable, type Writable } from "svelte/store";

import PeriodicNotesAPI from "./api";
import { PeriodicNotesCache, type PeriodicNoteCachedMetadata } from "./cache";
import CalendarSetManager, {
DEFAULT_CALENDARSET_ID,
Expand Down Expand Up @@ -58,6 +59,8 @@ export default class PeriodicNotesPlugin extends Plugin {
public calendarSetManager: CalendarSetManager;
private timelineManager: TimelineManager;

api: PeriodicNotesAPI

unload(): void {
super.unload();
this.timelineManager?.cleanup();
Expand All @@ -70,6 +73,7 @@ export default class PeriodicNotesPlugin extends Plugin {

initializeLocaleConfigOnce(this.app);

this.api = PeriodicNotesAPI.get(this)
this.ribbonEl = null;
this.calendarSetManager = new CalendarSetManager(this);
this.cache = new PeriodicNotesCache(this.app, this);
Expand Down Expand Up @@ -228,6 +232,22 @@ export default class PeriodicNotesPlugin extends Plugin {
return this.app.vault.create(destPath, renderedContents);
}

public async createOrReturnPeriodicNote(
granularity: Granularity,
date: Moment,
calendarSet?: string
): Promise<TFile> {
let file = this.cache.getPeriodicNote(
calendarSet ?? this.calendarSetManager.getActiveId(),
granularity,
date
);
if (!file) {
file = await this.createPeriodicNote(granularity, date);
}
return file
}

public getPeriodicNote(granularity: Granularity, date: Moment): TFile | null {
return this.cache.getPeriodicNote(
this.calendarSetManager.getActiveId(),
Expand Down Expand Up @@ -273,14 +293,7 @@ export default class PeriodicNotesPlugin extends Plugin {
): Promise<void> {
const { inNewSplit = false, calendarSet } = opts ?? {};
const { workspace } = this.app;
let file = this.cache.getPeriodicNote(
calendarSet ?? this.calendarSetManager.getActiveId(),
granularity,
date
);
if (!file) {
file = await this.createPeriodicNote(granularity, date);
}
const file = await this.createOrReturnPeriodicNote(granularity, date, calendarSet);

const leaf = inNewSplit ? workspace.splitActiveLeaf() : workspace.getUnpinnedLeaf();
await leaf.openFile(file, { active: true });
Expand Down

0 comments on commit 0dd3352

Please sign in to comment.