Skip to content

Commit

Permalink
feat: Initial API definition started (just events for now)
Browse files Browse the repository at this point in the history
  • Loading branch information
valentine195 committed Aug 25, 2023
1 parent 1e5e8b5 commit 4a7f85b
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 11 deletions.
14 changes: 7 additions & 7 deletions src/@types/api.d.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import {
Moon,
Phase,
FcEvent,
FcDate,
CalEvent,
CalDate,
LeapDay,
FcEventCategory
CalEventCategory
} from ".";

declare class API {
getCalendars(): Calendar[];
getMoons(
date?: FcDate,
date?: CalDate,
name?: string
): Array<{ moon: Moon; phase: Phase; icon: HTMLSpanElement }>;
getDay(
Expand All @@ -28,15 +28,15 @@ declare class API {
): Day;

addCategoryToCalendar(
category: FcEventCategory,
category: CalEventCategory,
calendar: Calendar | string = this.plugin.defaultCalendar
): Promise<void>;
}

export type Day = {
moons: [Moon, Phase][];
events: FcEvent[];
date: FcDate;
events: CalEvent[];
date: CalDate;
longDate: {
day: number;
month: string;
Expand Down
15 changes: 11 additions & 4 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,12 @@ declare module "obsidian" {

declare global {
interface Window {
CalendariumAPI?: API;
Calendarium?: Calendarium;
}
}
export const MODIFIER_KEY = Platform.isMacOS ? "Meta" : "Control";

export default class Calendarium extends Plugin {
api = new API();
watcher: Watcher;
async addNewCalendar(calendar: Calendar, existing?: Calendar) {
let shouldParse =
Expand Down Expand Up @@ -83,6 +82,14 @@ export default class Calendarium extends Plugin {
get calendars() {
return this.$settingsService.getCalendars();
}

getAPI(calendarName: string): API {
const store = this.getStore(calendarName);
if (!store)
throw new ReferenceError("No calendar store by that name exists.");
return new API(store);
}

private readonly stores: WeakMap<Calendar, CalendarStore> = new WeakMap();
getStore(calendar: string) {
if (!calendar) return null;
Expand Down Expand Up @@ -125,8 +132,8 @@ export default class Calendarium extends Plugin {
await this.$settingsService.loadData();

this.watcher = new Watcher(this);
(window["CalendariumAPI"] = this.api) &&
this.register(() => delete window["CalendariumAPI"]);
(window["Calendarium"] = this) &&
this.register(() => delete window["Calendarium"]);

this.registerView(
VIEW_TYPE,
Expand Down

0 comments on commit 4a7f85b

Please sign in to comment.