Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds a Create Note command (doesn't open note) & an API for userscript consumption #182

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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, date = window.moment()) {
await plugin.createOrReturnPeriodicNote(
granularity,
date
);

if(openNote) {
plugin.openPeriodicNote(granularity, date)
}
},
getPeriodicNote(granularity: Granularity, date = window.moment()) {
return plugin.getPeriodicNote(
granularity,
date
);
}
}
}
}
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