diff --git a/manifest.json b/manifest.json index 766c47a..2e4969f 100644 --- a/manifest.json +++ b/manifest.json @@ -1,7 +1,7 @@ { "id": "google-calendar", "name": "Google Calendar", - "version": "1.10.8", + "version": "1.10.9", "minAppVersion": "0.12.0", "description": "Interact with your Google Calendar from Inside Obsidian", "author": "YukiGasai", diff --git a/package.json b/package.json index 639b34b..6c5f837 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "google-calendar", - "version": "1.10.8", + "version": "1.10.9", "description": "Interact with your Google Calendar from Inside Obsidian", "main": "main.js", "scripts": { diff --git a/src/GoogleCalendarPlugin.ts b/src/GoogleCalendarPlugin.ts index 1303415..0ef323e 100644 --- a/src/GoogleCalendarPlugin.ts +++ b/src/GoogleCalendarPlugin.ts @@ -33,6 +33,7 @@ import { getEvent } from "src/googleApi/GoogleGetEvent"; import { createNotification } from "src/helper/NotificationHelper"; import { getTodaysCustomTasks } from "src/helper/customTask/GetCustomTask"; import { FinishLoginGoogleMobile } from "src/googleApi/GoogleAuth"; +import { deleteEventFromFrontmatter } from "./helper/FrontMatterDelete"; const DEFAULT_SETTINGS: GoogleCalendarPluginSettings = { @@ -608,7 +609,7 @@ export default class GoogleCalendarPlugin extends Plugin { */ this.addCommand({ id: "create-google-calendar-event-from-frontmatter ", - name: "Create gCal Event From Frontmatter", + name: "Create gCal Event from Frontmatter", editorCheckCallback: ( checking: boolean, editor: Editor, @@ -630,11 +631,44 @@ export default class GoogleCalendarPlugin extends Plugin { getEventFromFrontMatter(view).then((newEvent) => { if (!newEvent) return; - createEvent(newEvent) + createEvent(newEvent).then(createdEvent => { + let fileContent = editor.getValue(); + fileContent = fileContent.replace("---", `---\nevent-id: ${createdEvent.id}`); + editor.setValue(fileContent); + }) }) }, }); + + + /** + * This function will try to create a event from the yaml metadata of a file + */ + this.addCommand({ + id: "delete-google-calendar-event-from-frontmatter ", + name: "Delete gCal Event from Frontmatter", + editorCheckCallback: ( + checking: boolean, + editor: Editor, + view: MarkdownView + ): boolean => { + const canRun = settingsAreCompleteAndLoggedIn(); + if (checking) { + return canRun; + } + + if (!canRun) { + return; + } + + if (!view.file) { + return; + } + deleteEventFromFrontmatter(editor, view) + }, + }); + this.settingsTab = new GoogleCalendarSettingTab(this.app, this); this.addSettingTab(this.settingsTab); diff --git a/src/googleApi/GoogleGetEvent.ts b/src/googleApi/GoogleGetEvent.ts index 706ae26..3fd5c70 100644 --- a/src/googleApi/GoogleGetEvent.ts +++ b/src/googleApi/GoogleGetEvent.ts @@ -10,18 +10,31 @@ import { GoogleApiError } from "./GoogleApiError"; * @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 { if (!settingsAreCompleteAndLoggedIn()){ throw new GoogleApiError("Not logged in", null, 401, {error: "Not logged in"}) }; - - const foundEvent = await callRequest(`https://www.googleapis.com/calendar/v3/calendars/${calendarId}/events/${eventId}`, "GET", null) + const calendars = await googleListCalendars(); + if(calendarId){ + const foundEvent = await callRequest(`https://www.googleapis.com/calendar/v3/calendars/${calendarId}/events/${eventId}`, "GET", null) + foundEvent.parent = calendars.find(calendar => calendar.id === calendarId); + return foundEvent; + } - foundEvent.parent = calendars.find(calendar => calendar.id === calendarId); + for (const calendar of calendars) { + try { + const foundEvent = await callRequest(`https://www.googleapis.com/calendar/v3/calendars/${calendar.id}/events/${eventId}`, "GET", null) + if(foundEvent && foundEvent.id === eventId){ + foundEvent.parent = calendar; + return foundEvent; + } + } catch(err) { + //Do nothing + } + } - return foundEvent; } export async function getEvent(eventId: string, calendarId?: string): Promise { diff --git a/src/helper/FrontMatterDelete.ts b/src/helper/FrontMatterDelete.ts new file mode 100644 index 0000000..4d0692b --- /dev/null +++ b/src/helper/FrontMatterDelete.ts @@ -0,0 +1,29 @@ +import type { Editor, MarkdownView } from "obsidian"; +import { createNotice } from "./NoticeHelper"; +import { googleGetEvent } from "../googleApi/GoogleGetEvent"; +import _ from "lodash"; +import { googleDeleteEvent } from "../googleApi/GoogleDeleteEvent"; + +export const deleteEventFromFrontmatter = async (editor: Editor, view: MarkdownView) => { + let fileContent = editor.getValue(); + + //Use a copy to prevent problems when running the command multiple times + const frontmatter: any = _.cloneDeep(app?.metadataCache?.getFileCache(view.file)?.frontmatter) ?? {}; + + if(!frontmatter["event-id"]){ + createNotice("No event id found in note", true); + return; + } + + const event_id = frontmatter["event-id"]; + const event = await googleGetEvent(event_id); + const gotDeleted = await googleDeleteEvent(event); + + if(gotDeleted) { + const lineRegex = new RegExp(`event-id: ${event_id}(\r\n|\r|\n)`, "g"); + fileContent = fileContent.replace(lineRegex, ""); + editor.setValue(fileContent); + } +} + + diff --git a/src/helper/FrontMatterParser.ts b/src/helper/FrontMatterParser.ts index 473ef85..d529e3f 100644 --- a/src/helper/FrontMatterParser.ts +++ b/src/helper/FrontMatterParser.ts @@ -54,6 +54,11 @@ export const getEventFromFrontMatter = async (view: MarkdownView): Promise