From 2dc1140f6e8932d0cf99d4e5234245e2144e0a7a Mon Sep 17 00:00:00 2001 From: YukiGasai Date: Sat, 1 Oct 2022 01:06:34 +0200 Subject: [PATCH] Added exposed api for templater and dataview #13 --- manifest.json | 2 +- package.json | 2 +- src/GoogleCalendarPlugin.ts | 11 ++++++----- src/googleApi/GoogleGetEvent.ts | 2 +- src/googleApi/GoogleListEvents.ts | 25 +++++++++---------------- src/helper/GoogleCalendarPluginApi.ts | 20 ++++++++++++++++++++ src/helper/types.ts | 7 +++++++ src/modal/CalendarsListModal.ts | 4 ++-- src/svelte/InsertEventsComp.svelte | 5 ++--- src/svelte/TimeLineComp.svelte | 2 +- 10 files changed, 50 insertions(+), 30 deletions(-) create mode 100644 src/helper/GoogleCalendarPluginApi.ts diff --git a/manifest.json b/manifest.json index cde663a..f55aeee 100644 --- a/manifest.json +++ b/manifest.json @@ -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", diff --git a/package.json b/package.json index 4651a5d..500fe85 100644 --- a/package.json +++ b/package.json @@ -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": { diff --git a/src/GoogleCalendarPlugin.ts b/src/GoogleCalendarPlugin.ts index 601c4d6..0a3e75e 100644 --- a/src/GoogleCalendarPlugin.ts +++ b/src/GoogleCalendarPlugin.ts @@ -1,4 +1,4 @@ -import type { GoogleCalendarPluginSettings } from "./helper/types"; +import type { GoogleCalendarPluginSettings, IGoogleCalendarPluginApi } from "./helper/types"; import { Editor, Notice, Plugin, WorkspaceLeaf } from "obsidian"; import { GoogleCalendarSettingTab, @@ -6,14 +6,13 @@ import { } 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"; @@ -21,6 +20,7 @@ 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"; @@ -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 @@ -91,7 +92,7 @@ export default class GoogleCalendarPlugin extends Plugin { async onload(): Promise { GoogleCalendarPlugin.instance = this; - + this.api = new GoogleCalendarPluginApi().make(); await this.loadSettings(); this.app.workspace.onLayoutReady(this.onLayoutReady); @@ -245,7 +246,7 @@ export default class GoogleCalendarPlugin extends Plugin { return; } - googleListTodayEvents().then((events) => { + googleListEvents().then((events) => { new EventListModal(events).open() }); }, diff --git a/src/googleApi/GoogleGetEvent.ts b/src/googleApi/GoogleGetEvent.ts index 2169eef..f34783c 100644 --- a/src/googleApi/GoogleGetEvent.ts +++ b/src/googleApi/GoogleGetEvent.ts @@ -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 { + export async function googleGetEvent(eventId: string, calendarId?: string): Promise { const plugin = GoogleCalendarPlugin.getInstance(); const updateResponse = await requestUrl({ diff --git a/src/googleApi/GoogleListEvents.ts b/src/googleApi/GoogleListEvents.ts index cc21f46..8939fe5 100644 --- a/src/googleApi/GoogleListEvents.ts +++ b/src/googleApi/GoogleListEvents.ts @@ -36,12 +36,16 @@ export function googleClearCachedEvents():void{ */ export async function googleListEventsByCalendar( GoogleCalendar: GoogleCalendar, - date: moment.Moment, + date?: moment.Moment, endDate?: moment.Moment ): Promise { const plugin = GoogleCalendarPlugin.getInstance(); + if(!date){ + date = window.moment(); + } + //Turn dates into strings for request and caching const timezone = ct.getTimezone(GoogleCalendar.timeZone); @@ -125,10 +129,12 @@ export async function googleListEventsByCalendar( } export async function googleListEvents( - date: moment.Moment, + date?: moment.Moment, endDate?: moment.Moment ): Promise { - + if(!date){ + date = window.moment() + } try { const calendarList = await googleListCalendars(); let eventList: GoogleEvent[] = []; @@ -158,19 +164,6 @@ export async function googleListEvents( } } -export async function googleListTodayEventsByCalendar(GoogleCalendar: GoogleCalendar): Promise { - const list = await googleListEventsByCalendar( - GoogleCalendar, - window.moment() - ); - return list; -} - -export async function googleListTodayEvents(): Promise { - const list = await googleListEvents(window.moment()); - return list; -} - export async function googleListEventsByMonth(dateInMonth: moment.Moment): Promise { const monthStartDate = dateInMonth.clone().startOf("month") const monthEndDate = dateInMonth.clone().endOf("month") diff --git a/src/helper/GoogleCalendarPluginApi.ts b/src/helper/GoogleCalendarPluginApi.ts new file mode 100644 index 0000000..064e375 --- /dev/null +++ b/src/helper/GoogleCalendarPluginApi.ts @@ -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), + } + } +} \ No newline at end of file diff --git a/src/helper/types.ts b/src/helper/types.ts index e4b6aa0..1dc5a9f 100644 --- a/src/helper/types.ts +++ b/src/helper/types.ts @@ -271,4 +271,11 @@ export interface EventCacheKey { export interface EventCacheValue { events: GoogleEvent[]; updated: moment.Moment; +} + +export interface IGoogleCalendarPluginApi { + getEvent: (id:string, calendarId:string) => Promise, + getEvents: (start?:moment.Moment, end?:moment.Moment) => Promise, + getCalendars: () => Promise, + getEventsFromCalendar: (calendar:GoogleCalendar, start?:moment.Moment, end?:moment.Moment) => Promise, } \ No newline at end of file diff --git a/src/modal/CalendarsListModal.ts b/src/modal/CalendarsListModal.ts index cab2681..e23b1b7 100644 --- a/src/modal/CalendarsListModal.ts +++ b/src/modal/CalendarsListModal.ts @@ -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"; /** @@ -28,7 +28,7 @@ export class CalendarsListModal extends FuzzySuggestModal { async onChooseItem( item: GoogleCalendar ): Promise { - const events = await googleListTodayEventsByCalendar(item); + const events = await googleListEventsByCalendar(item); new EventListModal(events).open(); } } diff --git a/src/svelte/InsertEventsComp.svelte b/src/svelte/InsertEventsComp.svelte index e050d5b..4d3b3d4 100644 --- a/src/svelte/InsertEventsComp.svelte +++ b/src/svelte/InsertEventsComp.svelte @@ -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"; @@ -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"; @@ -34,7 +34,6 @@ }); a += "]" - console.log(a); }); let handleSubmit = () => { diff --git a/src/svelte/TimeLineComp.svelte b/src/svelte/TimeLineComp.svelte index 7efb2e2..c49a795 100644 --- a/src/svelte/TimeLineComp.svelte +++ b/src/svelte/TimeLineComp.svelte @@ -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";