Skip to content

Commit

Permalink
#207 Allow the event deletion from frontmatter
Browse files Browse the repository at this point in the history
  • Loading branch information
YukiGasai committed Oct 1, 2023
1 parent 95dc189 commit e7e6f35
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 9 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.10.8",
"version": "1.10.9",
"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.10.8",
"version": "1.10.9",
"description": "Interact with your Google Calendar from Inside Obsidian",
"main": "main.js",
"scripts": {
Expand Down
38 changes: 36 additions & 2 deletions src/GoogleCalendarPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down Expand Up @@ -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,
Expand All @@ -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);
Expand Down
23 changes: 18 additions & 5 deletions src/googleApi/GoogleGetEvent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<GoogleEvent> {
export async function googleGetEvent(eventId: string, calendarId?: string): Promise<GoogleEvent> {

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<GoogleEvent> {
Expand Down
29 changes: 29 additions & 0 deletions src/helper/FrontMatterDelete.ts
Original file line number Diff line number Diff line change
@@ -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);
}
}


5 changes: 5 additions & 0 deletions src/helper/FrontMatterParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@ export const getEventFromFrontMatter = async (view: MarkdownView): Promise<Front
return;
}

if(frontmatter["event-id"]){
createNotice("Event already created", true);
return;
}

const frontMatterMapping = getFrontMatterMapping(frontmatter);
//Use the mapping to resolve the values to the selected event details
for (const [key, value] of Object.entries(frontMatterMapping)) {
Expand Down

0 comments on commit e7e6f35

Please sign in to comment.