From 06c9335caeeadf3a2de825da274abdfb5ee093d3 Mon Sep 17 00:00:00 2001 From: Muness Castle <931+muness@users.noreply.github.com> Date: Thu, 9 Nov 2023 20:45:18 -0500 Subject: [PATCH] Add data view syntax for event start and end --- src/main.ts | 48 ++++++++++++++----------- src/settings/ICSSettings.ts | 4 ++- src/settings/ICSSettingsTab.ts | 64 ++++++++++++++++++++-------------- 3 files changed, 68 insertions(+), 48 deletions(-) diff --git a/src/main.ts b/src/main.ts index 502f207..7f1f457 100644 --- a/src/main.ts +++ b/src/main.ts @@ -40,14 +40,33 @@ export default class ICSPlugin extends Plugin { await this.saveSettings(); } + formatEvent(e: IEvent): string { + const callLinkOrLocation = e.callType ? `[${e.callType}](${e.callUrl})` : e.location; + + // Conditionally format start and end time based on dataViewSyntax setting + const startTimeFormatted = this.data.format.dataViewSyntax ? `[startTime:: ${e.time}]` : `${e.time}`; + const endTimeFormatted = e.format.includeEventEndTime ? (this.data.format.dataViewSyntax ? `[endTime:: ${e.endTime}]` : `- ${e.endTime}`) : ''; + + // Combine all parts of the formatted event string + return [ + `- ${e.format.checkbox ? '[ ]' : ''}`, + startTimeFormatted, + endTimeFormatted, + e.format.icsName ? e.icsName : '', + e.format.summary ? e.summary : '', + e.format.location ? callLinkOrLocation : '', + e.format.description && e.description ? `\n\t- ${e.description}` : '', + ].filter(Boolean).join(' ').trim(); +} + async getEvents(date: string) : Promise { let events: IEvent[] = []; let errorMessages: string[] = []; // To store error messages - + for (const calendar in this.data.calendars) { const calendarSetting = this.data.calendars[calendar]; let icsArray: any[] = []; - + // Exception handling for downloading try { icsArray = parseIcs(await request({ @@ -59,11 +78,11 @@ export default class ICSPlugin extends Plugin { } var dateEvents; - + // Exception handling for parsing and filtering try { dateEvents = filterMatchingEvents(icsArray, date); - + } catch (filterError) { console.error(`Error filtering events for calendar ${calendarSetting.icsName}: ${filterError}`); errorMessages.push(`Error filtering events in calendar "${calendarSetting.icsName}"`); @@ -72,7 +91,7 @@ export default class ICSPlugin extends Plugin { try { dateEvents.forEach((e) => { const { callUrl, callType } = extractMeetingInfo(e); - + let event: IEvent = { utime: moment(e.start).format('X'), time: moment(e.start).format(this.data.format.timeFormat), @@ -92,15 +111,15 @@ export default class ICSPlugin extends Plugin { errorMessages.push(`Error parsing events in calendar "${calendarSetting.icsName}"`); } } - + // Notify the user if any errors were encountered if (errorMessages.length > 0) { const message = `Encountered ${errorMessages.length} error(s) while processing calendars:\n\n${errorMessages.join('\n')}\nSee console for details.`; new Notice(message); } - + return events; - } + } async onload() { await this.loadSettings(); @@ -112,18 +131,7 @@ export default class ICSPlugin extends Plugin { const fileDate = getDateFromFile(view.file, "day").format("YYYY-MM-DD"); var events: any[] = await this.getEvents(fileDate); - const mdArray = events.sort((a,b) => a.utime - b.utime).map(e => { - const callLinkOrlocation = e.callType ? `[${e.callType}](${e.callUrl})` : e.location; - return [ - `- ${e.format?.checkbox ? '[ ]' : ''}`, - `${e.time}`, - e.format?.includeEventEndTime ? `- ${e.endTime}` : null, - e.format?.icsName ? e.icsName : null, - e.format?.summary ? e.summary : null, - e.format?.location ? callLinkOrlocation : null, - e.format?.description && e.description ? `\n\t- ${e.description}` : null, - ].filter(Boolean).join(' ') - }); + const mdArray = events.sort((a,b) => a.utime - b.utime).map(this.formatEvent, this); editor.replaceRange(mdArray.join("\n"), editor.getCursor()); } }); diff --git a/src/settings/ICSSettings.ts b/src/settings/ICSSettings.ts index 82c21ec..c41dfc7 100644 --- a/src/settings/ICSSettings.ts +++ b/src/settings/ICSSettings.ts @@ -1,6 +1,7 @@ export interface ICSSettings { format: { timeFormat: string + dataViewSyntax: boolean, }, calendars: Record < string, Calendar > ; } @@ -29,7 +30,8 @@ export const DEFAULT_CALENDAR_FORMAT = { export const DEFAULT_SETTINGS: ICSSettings = { format: { - timeFormat: "HH:mm" + timeFormat: "HH:mm", + dataViewSyntax: false, }, calendars: { } diff --git a/src/settings/ICSSettingsTab.ts b/src/settings/ICSSettingsTab.ts index 2594f61..222732c 100644 --- a/src/settings/ICSSettingsTab.ts +++ b/src/settings/ICSSettingsTab.ts @@ -38,34 +38,40 @@ export default class ICSSettingsTab extends PluginSettingTab { this.plugin = plugin; } + // use this same format to create a description for the dataViewSyntax setting + private timeFormattingDescription(): DocumentFragment { + this.updateTimeFormatExample(); + + const descEl = document.createDocumentFragment(); + descEl.appendText('Time format for events. HH:mm is 00:15. hh:mma is 12:15am.'); + descEl.appendText(' For more syntax, refer to '); + descEl.appendChild(this.getMomentDocsLink()); + descEl.appendText('.'); + + descEl.appendChild(document.createElement('p')); + descEl.appendText('Your current time format syntax looks like this: '); + descEl.appendChild(this.timeFormatExample); + descEl.appendText('.'); + return descEl; + } -private timeFormattingDescription() { - this.updateTimeFormatExample(); - - const descEl = document.createDocumentFragment(); - descEl.appendText('Time format for events. HH:mm is 00:15. hh:mma is 12:15am.'); - descEl.appendText(' For more syntax, refer to '); - descEl.appendChild(this.getMomentDocsLink()); - descEl.appendText('.'); - - descEl.appendChild(document.createElement('p')); - descEl.appendText('Your current time format syntax looks like this: '); - descEl.appendChild(this.timeFormatExample); - descEl.appendText('.'); - return descEl; -} + private getMomentDocsLink(): HTMLAnchorElement { + const a = document.createElement('a'); + a.href = 'https://momentjs.com/docs/#/displaying/format/'; + a.text = 'format reference'; + a.target = '_blank'; + return a; + } -private updateTimeFormatExample() { - this.timeFormatExample.innerText = moment(new Date()).format(this.plugin.data.format.timeFormat); -} + private updateTimeFormatExample() { + this.timeFormatExample.innerText = moment(new Date()).format(this.plugin.data.format.timeFormat); + } -private getMomentDocsLink() { - const a = document.createElement('a'); - a.href = 'https://momentjs.com/docs/#/displaying/format/'; - a.text = 'format reference'; - a.target = '_blank'; - return a; -} + private dataViewSyntaxDescription(): DocumentFragment { + const descEl = document.createDocumentFragment(); + descEl.appendText('Enable this option if you use the DataView plugin to query event start and end times.'); + return descEl; + } display(): void { let { @@ -166,8 +172,12 @@ private getMomentDocsLink() { }); }); - - + const dataViewSyntaxSetting = new Setting(containerEl) + .setName('DataView Metadata syntax for start and end times') + .setDesc(this.dataViewSyntaxDescription()) + .addToggle(toggle => toggle + .setValue(this.plugin.data.format.dataViewSyntax || false) + .onChange(value => this.plugin.data.format.dataViewSyntax = value)); // Sponsor link - Thank you! const divSponsor = containerEl.createDiv();