Skip to content

Commit

Permalink
Added exposed api for templater and dataview #13
Browse files Browse the repository at this point in the history
  • Loading branch information
YukiGasai committed Sep 30, 2022
1 parent d483baf commit 2dc1140
Show file tree
Hide file tree
Showing 10 changed files with 50 additions and 30 deletions.
2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"id": "google-calendar",
"name": "Google Calendar",
"version": "1.3.2",
"version": "1.4.0",
"minAppVersion": "0.12.0",
"description": "Interact with your Google Calendar from Inside Obsidian",
"author": "YukiGasai",
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "google-calendar",
"version": "1.3.2",
"version": "1.4.0",
"description": "Interact with your Google Calendar from Inside Obsidian",
"main": "main.js",
"scripts": {
Expand Down
11 changes: 6 additions & 5 deletions src/GoogleCalendarPlugin.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
import type { GoogleCalendarPluginSettings } from "./helper/types";
import type { GoogleCalendarPluginSettings, IGoogleCalendarPluginApi } from "./helper/types";
import { Editor, Notice, Plugin, WorkspaceLeaf } from "obsidian";
import {
GoogleCalendarSettingTab,
settingsAreCompleteAndLoggedIn,
} from "./view/GoogleCalendarSettingTab";
import { googleListCalendars } from "./googleApi/GoogleListCalendars";
import { CalendarsListModal } from "./modal/CalendarsListModal";
import { googleClearCachedEvents, googleListTodayEvents } from "./googleApi/GoogleListEvents";
import { googleClearCachedEvents, googleListEvents } from "./googleApi/GoogleListEvents";
import { checkEditorForCodeBlocks } from "./helper/CheckEditorForCodeBlocks";
import { DayCalendarView, VIEW_TYPE_GOOGLE_CALENDAR_DAY } from "./view/DayCalendarView";
import { MonthCalendarView, VIEW_TYPE_GOOGLE_CALENDAR_MONTH } from "./view/MonthCalendarView";
import { WebCalendarView, VIEW_TYPE_GOOGLE_CALENDAR_WEB } from "./view/WebCalendarView";
import { ScheduleCalendarView, VIEW_TYPE_GOOGLE_CALENDAR_SCHEDULE } from "./view/ScheduleCalendarView";
import { checkEditorForAtDates } from "./helper/CheckEditorForAtDates";
import { insertTodayEventsIntoFile } from "./helper/InsertTodayEventsIntoFile";
import { getRefreshToken } from "./helper/LocalStorage";
import { EventListModal } from './modal/EventListModal';
import { checkForEventNotes } from "./helper/AutoEventNoteCreator";
import { EventDetailsModal } from "./modal/EventDetailsModal";
import { checkEditorForInsertedEvents } from "./helper/CheckEditorForInsertedEvents";
import { TemplateSuggest } from "./helper/TemplateSuggest";
import { InsertEventsModal } from "./modal/InsertEventsModal";
import { GoogleCalendarPluginApi } from "./helper/GoogleCalendarPluginApi";



Expand Down Expand Up @@ -48,6 +48,7 @@ export default class GoogleCalendarPlugin extends Plugin {
return GoogleCalendarPlugin.instance;
}

api: IGoogleCalendarPluginApi;

settings: GoogleCalendarPluginSettings;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
Expand Down Expand Up @@ -91,7 +92,7 @@ export default class GoogleCalendarPlugin extends Plugin {
async onload(): Promise<void> {

GoogleCalendarPlugin.instance = this;

this.api = new GoogleCalendarPluginApi().make();
await this.loadSettings();

this.app.workspace.onLayoutReady(this.onLayoutReady);
Expand Down Expand Up @@ -245,7 +246,7 @@ export default class GoogleCalendarPlugin extends Plugin {
return;
}

googleListTodayEvents().then((events) => {
googleListEvents().then((events) => {
new EventListModal(events).open()
});
},
Expand Down
2 changes: 1 addition & 1 deletion src/googleApi/GoogleGetEvent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import type { GoogleEvent } from "../helper/types";
* @param calendarId The id of the calendar the event is in
* @returns The found Event
*/
export async function googleGetEvent(eventId: string, calendarId: string): Promise<GoogleEvent> {
export async function googleGetEvent(eventId: string, calendarId?: string): Promise<GoogleEvent> {
const plugin = GoogleCalendarPlugin.getInstance();

const updateResponse = await requestUrl({
Expand Down
25 changes: 9 additions & 16 deletions src/googleApi/GoogleListEvents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,16 @@ export function googleClearCachedEvents():void{
*/
export async function googleListEventsByCalendar(
GoogleCalendar: GoogleCalendar,
date: moment.Moment,
date?: moment.Moment,
endDate?: moment.Moment
): Promise<GoogleEvent[]> {

const plugin = GoogleCalendarPlugin.getInstance();

if(!date){
date = window.moment();
}

//Turn dates into strings for request and caching
const timezone = ct.getTimezone(GoogleCalendar.timeZone);

Expand Down Expand Up @@ -125,10 +129,12 @@ export async function googleListEventsByCalendar(
}

export async function googleListEvents(
date: moment.Moment,
date?: moment.Moment,
endDate?: moment.Moment
): Promise<GoogleEvent[]> {

if(!date){
date = window.moment()
}
try {
const calendarList = await googleListCalendars();
let eventList: GoogleEvent[] = [];
Expand Down Expand Up @@ -158,19 +164,6 @@ export async function googleListEvents(
}
}

export async function googleListTodayEventsByCalendar(GoogleCalendar: GoogleCalendar): Promise<GoogleEvent[]> {
const list = await googleListEventsByCalendar(
GoogleCalendar,
window.moment()
);
return list;
}

export async function googleListTodayEvents(): Promise<GoogleEvent[]> {
const list = await googleListEvents(window.moment());
return list;
}

export async function googleListEventsByMonth(dateInMonth: moment.Moment): Promise<GoogleEvent[]> {
const monthStartDate = dateInMonth.clone().startOf("month")
const monthEndDate = dateInMonth.clone().endOf("month")
Expand Down
20 changes: 20 additions & 0 deletions src/helper/GoogleCalendarPluginApi.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@

import type { GoogleCalendar, IGoogleCalendarPluginApi } from './types';
import {googleListEvents, googleListEventsByCalendar} from "../googleApi/GoogleListEvents";
import {googleGetEvent} from "../googleApi/GoogleGetEvent";
import { googleListCalendars } from '../googleApi/GoogleListCalendars';

export class GoogleCalendarPluginApi {

constructor() {
}

public make(): IGoogleCalendarPluginApi {
return {
getCalendars: () => googleListCalendars(),
getEvents: (start?:moment.Moment, end?:moment.Moment) => googleListEvents(start, end),
getEventsFromCalendar: (calendar:GoogleCalendar, start?:moment.Moment, end?:moment.Moment) => googleListEventsByCalendar(calendar, start, end),
getEvent: (id:string, calendarId:string) => googleGetEvent(id, calendarId),
}
}
}
7 changes: 7 additions & 0 deletions src/helper/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -271,4 +271,11 @@ export interface EventCacheKey {
export interface EventCacheValue {
events: GoogleEvent[];
updated: moment.Moment;
}

export interface IGoogleCalendarPluginApi {
getEvent: (id:string, calendarId:string) => Promise<GoogleEvent>,
getEvents: (start?:moment.Moment, end?:moment.Moment) => Promise<GoogleEvent[]>,
getCalendars: () => Promise<GoogleCalendar[]>,
getEventsFromCalendar: (calendar:GoogleCalendar, start?:moment.Moment, end?:moment.Moment) => Promise<GoogleEvent[]>,
}
4 changes: 2 additions & 2 deletions src/modal/CalendarsListModal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type { GoogleCalendar } from "../helper/types";

import GoogleCalendarPlugin from "src/GoogleCalendarPlugin";
import { FuzzySuggestModal } from "obsidian";
import { googleListTodayEventsByCalendar } from "../googleApi/GoogleListEvents";
import { googleListEventsByCalendar } from "../googleApi/GoogleListEvents";
import { EventListModal } from "./EventListModal";

/**
Expand All @@ -28,7 +28,7 @@ export class CalendarsListModal extends FuzzySuggestModal<GoogleCalendar> {
async onChooseItem(
item: GoogleCalendar
): Promise<void> {
const events = await googleListTodayEventsByCalendar(item);
const events = await googleListEventsByCalendar(item);
new EventListModal(events).open();
}
}
5 changes: 2 additions & 3 deletions src/svelte/InsertEventsComp.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import { onMount } from "svelte";
import { googleListCalendars } from "../googleApi/GoogleListCalendars";
import { GoogleEventSuggestionList } from "../helper/GoogleEventSuggestionList";
import { googleListEvents, googleListTodayEvents } from "../googleApi/GoogleListEvents";
import { googleListEvents } from "../googleApi/GoogleListEvents";
import GoogleCalendarPlugin from "../GoogleCalendarPlugin";
import { AskNameModal } from "../modal/AskNameModal";
import { createNotice } from "../helper/NoticeHelper";
Expand All @@ -25,7 +25,7 @@
const totalCalendarList = await googleListCalendars();
calendarList = totalCalendarList.map(calendar => {return [calendar, true]});
const totalEventList = await googleListTodayEvents();
const totalEventList = await googleListEvents();
eventList = totalEventList.map(event => {return [event, true]});
let a = "export const GoogleEventSuggestionList = [\n";
Expand All @@ -34,7 +34,6 @@
});
a += "]"
console.log(a);
});
let handleSubmit = () => {
Expand Down
2 changes: 1 addition & 1 deletion src/svelte/TimeLineComp.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import { dateToPercent } from "../helper/CanvasDrawHelper";
import {getEventStartPosition, getEventHeight} from "../helper/CanvasDrawHelper";
import { googleClearCachedEvents, googleListEvents, googleListTodayEvents } from "../googleApi/GoogleListEvents";
import { googleClearCachedEvents, googleListEvents } from "../googleApi/GoogleListEvents";
import {EventDetailsModal} from '../modal/EventDetailsModal'
import {getColorFromEvent} from '../googleApi/GoogleColors'
import { onDestroy } from "svelte";
Expand Down

0 comments on commit 2dc1140

Please sign in to comment.