Skip to content

Commit

Permalink
Added Option to update with frontmatter #209
Browse files Browse the repository at this point in the history
  • Loading branch information
YukiGasai committed Oct 4, 2023
1 parent 199686b commit 247e02d
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 9 deletions.
45 changes: 41 additions & 4 deletions src/GoogleCalendarPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,18 @@ import { checkEditorForInsertedEvents } from "./helper/CheckEditorForInsertedEve
import { TemplateSuggest } from "./suggest/TemplateSuggest";
import { InsertEventsModal } from "./modal/InsertEventsModal";
import { GoogleCalendarPluginApi } from "./api/GoogleCalendarPluginApi";
import { findEventNote, getCurrentTheme } from "./helper/Helper";
import { findEventNote } from "./helper/Helper";
import { CreateNotePromptModal } from "./modal/CreateNotePromptModal";
import { checkForNewDailyNotes, checkForNewWeeklyNotes } from "./helper/DailyNoteHelper";
import { createEvent } from "../src/googleApi/GoogleCreateEvent";
import { getEventFromFrontMatter } from "./helper/FrontMatterParser";
import { getEvent } from "src/googleApi/GoogleGetEvent";
import { getEvent, googleGetEvent } 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";
import { updateEvent } from "./googleApi/GoogleUpdateEvent";
import { createNotice } from "./helper/NoticeHelper";


const DEFAULT_SETTINGS: GoogleCalendarPluginSettings = {
Expand Down Expand Up @@ -628,9 +630,9 @@ export default class GoogleCalendarPlugin extends Plugin {
return;
}


getEventFromFrontMatter(view).then((newEvent) => {
if (!newEvent) return;
if (newEvent.id) return;
createEvent(newEvent).then(createdEvent => {
let fileContent = editor.getValue();
fileContent = fileContent.replace("---", `---\nevent-id: ${createdEvent.id}`);
Expand All @@ -643,7 +645,42 @@ export default class GoogleCalendarPlugin extends Plugin {


/**
* This function will try to create a event from the yaml metadata of a file
* This function will try to update a event from the yaml metadata of a file
*/
this.addCommand({
id: "update-google-calendar-event-from-frontmatter ",
name: "Update 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;
}
getEventFromFrontMatter(view).then(async (newEvent:any) => {
if (!newEvent || !newEvent.id) {
createNotice("No event id found in note", true);
return;
}
const foundEvent = await googleGetEvent(newEvent.id);
newEvent.parent = foundEvent.parent;
updateEvent(newEvent);
})
},
});

/**
* This function will try to delete a event from the yaml metadata of a file
*/
this.addCommand({
id: "delete-google-calendar-event-from-frontmatter ",
Expand Down
7 changes: 2 additions & 5 deletions src/helper/FrontMatterParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,6 @@ 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 Expand Up @@ -196,6 +191,8 @@ export const getEventFromFrontMatter = async (view: MarkdownView): Promise<Front
}
}

frontmatter.id = frontmatter['event-id']

return frontmatter;
}

Expand Down

0 comments on commit 247e02d

Please sign in to comment.