From 8038a8d0f05eabbaabdfbd0618a33a18eef1543d Mon Sep 17 00:00:00 2001 From: Jarek Bird Date: Tue, 23 May 2023 14:28:03 -0600 Subject: [PATCH 1/7] Default Event Sources 1st Draft --- src/main.ts | 15 +++++++++++++-- src/ui/calendar.ts | 2 +- src/ui/view.ts | 30 +++++++++++++++--------------- 3 files changed, 29 insertions(+), 18 deletions(-) diff --git a/src/main.ts b/src/main.ts index db16e9c..18a191f 100644 --- a/src/main.ts +++ b/src/main.ts @@ -4,8 +4,9 @@ import { FULL_CALENDAR_SIDEBAR_VIEW_TYPE, FULL_CALENDAR_VIEW_TYPE, } from "./ui/view"; -import { renderCalendar } from "./ui/calendar"; +import { renderCalendar, ExtraRenderProps } from "./ui/calendar"; import { toEventInput } from "./ui/interop"; +import { translateSources } from "./ui/view" import { DEFAULT_SETTINGS, FullCalendarSettings, @@ -19,6 +20,7 @@ import FullNoteCalendar from "./calendars/FullNoteCalendar"; import DailyNoteCalendar from "./calendars/DailyNoteCalendar"; import ICSCalendar from "./calendars/ICSCalendar"; import CalDAVCalendar from "./calendars/CalDAVCalendar"; +import { EventSourceInput } from "@fullcalendar/core"; export default class FullCalendarPlugin extends Plugin { settings: FullCalendarSettings = DEFAULT_SETTINGS; @@ -58,7 +60,16 @@ export default class FullCalendarPlugin extends Plugin { FOR_TEST_ONLY: () => null, }); - renderCalendar = renderCalendar; + translateSources = translateSources; + renderCalendar = ( + containerEl: HTMLElement, + eventSources: EventSourceInput[], + settings?: ExtraRenderProps + ) => { + if (!eventSources) { eventSources = translateSources(this); } + renderCalendar(containerEl, eventSources, settings); + }; + processFrontmatter = toEventInput; async activateView() { diff --git a/src/ui/calendar.ts b/src/ui/calendar.ts index 225337e..b773cf2 100644 --- a/src/ui/calendar.ts +++ b/src/ui/calendar.ts @@ -35,7 +35,7 @@ rrulePlugin.recurringTypes[0].expand = function (errd, fr, de) { }); }; -interface ExtraRenderProps { +export interface ExtraRenderProps { eventClick?: (info: EventClickArg) => void; select?: ( startDate: Date, diff --git a/src/ui/view.ts b/src/ui/view.ts index 2bc033e..21fc093 100644 --- a/src/ui/view.ts +++ b/src/ui/view.ts @@ -50,6 +50,19 @@ function getCalendarColors(color: string | null | undefined): { }; } +export function translateSources(plugin : FullCalendarPlugin) { + return plugin.cache.getAllEvents().map( + ({ events, editable, color, id }): EventSourceInput => ({ + id, + events: events.flatMap( + (e) => toEventInput(e.id, e.event) || [] + ), + editable, + ...getCalendarColors(color), + }) + ); +} + export class CalendarView extends ItemView { plugin: FullCalendarPlugin; inSidebar: boolean; @@ -80,19 +93,6 @@ export class CalendarView extends ItemView { return this.inSidebar ? "Full Calendar" : "Calendar"; } - translateSources() { - return this.plugin.cache.getAllEvents().map( - ({ events, editable, color, id }): EventSourceInput => ({ - id, - events: events.flatMap( - (e) => toEventInput(e.id, e.event) || [] - ), - editable, - ...getCalendarColors(color), - }) - ); - } - async onOpen() { await this.plugin.loadSettings(); if (!this.plugin.cache) { @@ -116,7 +116,7 @@ export class CalendarView extends ItemView { return; } - const sources: EventSourceInput[] = this.translateSources(); + const sources: EventSourceInput[] = translateSources(this.plugin); if (this.fullCalendarView) { this.fullCalendarView.destroy(); @@ -301,7 +301,7 @@ export class CalendarView extends ItemView { this.callback = this.plugin.cache.on("update", (payload) => { if (payload.type === "resync") { this.fullCalendarView?.removeAllEventSources(); - const sources = this.translateSources(); + const sources = translateSources(this.plugin); sources.forEach((source) => this.fullCalendarView?.addEventSource(source) ); From 3607824b6afaa88759ec3e30a843f039130e861b Mon Sep 17 00:00:00 2001 From: Jarek Bird Date: Tue, 23 May 2023 15:37:05 -0600 Subject: [PATCH 2/7] Bug Fix --- src/main.ts | 15 ++++++++++----- src/ui/view.ts | 6 ++---- 2 files changed, 12 insertions(+), 9 deletions(-) diff --git a/src/main.ts b/src/main.ts index 18a191f..263302c 100644 --- a/src/main.ts +++ b/src/main.ts @@ -4,9 +4,12 @@ import { FULL_CALENDAR_SIDEBAR_VIEW_TYPE, FULL_CALENDAR_VIEW_TYPE, } from "./ui/view"; -import { renderCalendar, ExtraRenderProps } from "./ui/calendar"; +import { + renderCalendar as calendarRender, + ExtraRenderProps, +} from "./ui/calendar"; import { toEventInput } from "./ui/interop"; -import { translateSources } from "./ui/view" +import { translateSources } from "./ui/view"; import { DEFAULT_SETTINGS, FullCalendarSettings, @@ -65,9 +68,11 @@ export default class FullCalendarPlugin extends Plugin { containerEl: HTMLElement, eventSources: EventSourceInput[], settings?: ExtraRenderProps - ) => { - if (!eventSources) { eventSources = translateSources(this); } - renderCalendar(containerEl, eventSources, settings); + ) => { + if (!eventSources) { + eventSources = translateSources(this); + } + return calendarRender(containerEl, eventSources, settings); }; processFrontmatter = toEventInput; diff --git a/src/ui/view.ts b/src/ui/view.ts index 21fc093..28b5510 100644 --- a/src/ui/view.ts +++ b/src/ui/view.ts @@ -50,13 +50,11 @@ function getCalendarColors(color: string | null | undefined): { }; } -export function translateSources(plugin : FullCalendarPlugin) { +export function translateSources(plugin: FullCalendarPlugin) { return plugin.cache.getAllEvents().map( ({ events, editable, color, id }): EventSourceInput => ({ id, - events: events.flatMap( - (e) => toEventInput(e.id, e.event) || [] - ), + events: events.flatMap((e) => toEventInput(e.id, e.event) || []), editable, ...getCalendarColors(color), }) From f7c452cf1c177c93228c31a8fd00a12f8400a15f Mon Sep 17 00:00:00 2001 From: Jarek Bird Date: Tue, 23 May 2023 16:05:08 -0600 Subject: [PATCH 3/7] Add Additional Documentation --- docs/advanced/dataview.md | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/docs/advanced/dataview.md b/docs/advanced/dataview.md index 931f022..bcada2e 100644 --- a/docs/advanced/dataview.md +++ b/docs/advanced/dataview.md @@ -13,4 +13,15 @@ calendar.render() Unfortunately, there's a bug on first render and you need to interact with the calendar by changing the week or view before it renders properly. +`renderCalendar()` includes all events from all event sources set in the global settings when no event sources aer passed in. + +```` +```dataviewjs +this.container.style.minHeight = "500px"; +const { renderCalendar } = app.plugins.plugins["obsidian-full-calendar"]; +let calendar = renderCalendar(this.container); +calendar.render() +``` +```` + `renderCalendar()` exposes the FullCalendar API directly, so check out [the event parsing documentation](https://fullcalendar.io/docs/event-parsing) to see everything you can do here! From 9912a304e9d220a21499313b2a0345ffdfea34a3 Mon Sep 17 00:00:00 2001 From: Jarek Bird Date: Tue, 23 May 2023 23:58:16 -0600 Subject: [PATCH 4/7] Cover Inline Initialization Empty Cache State --- docs/advanced/dataview.md | 7 +++---- src/main.ts | 5 ++++- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/docs/advanced/dataview.md b/docs/advanced/dataview.md index bcada2e..0e2dc63 100644 --- a/docs/advanced/dataview.md +++ b/docs/advanced/dataview.md @@ -6,8 +6,8 @@ Create calendars inline with your notes from [dataviewjs](https://blacksmithgu.g ```dataviewjs this.container.style.minHeight = "500px"; const { renderCalendar } = app.plugins.plugins["obsidian-full-calendar"]; -let calendar = renderCalendar(this.container, [[{start: new Date(), id: "id", title: "Now and for an hour"}]]); -calendar.render() +renderCalendar(this.container, [[{start: new Date(), id: "id", title: "Now and for an hour"}]]) + .then(calendar => calendar.render()); ``` ```` @@ -19,8 +19,7 @@ Unfortunately, there's a bug on first render and you need to interact with the c ```dataviewjs this.container.style.minHeight = "500px"; const { renderCalendar } = app.plugins.plugins["obsidian-full-calendar"]; -let calendar = renderCalendar(this.container); -calendar.render() +renderCalendar(this.container).then(calendar => calendar.render()); ``` ```` diff --git a/src/main.ts b/src/main.ts index 263302c..255f8ef 100644 --- a/src/main.ts +++ b/src/main.ts @@ -64,12 +64,15 @@ export default class FullCalendarPlugin extends Plugin { }); translateSources = translateSources; - renderCalendar = ( + renderCalendar = async ( containerEl: HTMLElement, eventSources: EventSourceInput[], settings?: ExtraRenderProps ) => { if (!eventSources) { + if (!this.cache.initialized) { + await this.saveSettings(); + } eventSources = translateSources(this); } return calendarRender(containerEl, eventSources, settings); From 918e058a51659a9ce3dde7f06e854c486bd242a0 Mon Sep 17 00:00:00 2001 From: Jarek Bird Date: Wed, 24 May 2023 00:00:48 -0600 Subject: [PATCH 5/7] Remove Bug Mention in Advanced Docs --- docs/advanced/dataview.md | 2 -- 1 file changed, 2 deletions(-) diff --git a/docs/advanced/dataview.md b/docs/advanced/dataview.md index 0e2dc63..3fc86c2 100644 --- a/docs/advanced/dataview.md +++ b/docs/advanced/dataview.md @@ -11,8 +11,6 @@ renderCalendar(this.container, [[{start: new Date(), id: "id", title: "Now and f ``` ```` -Unfortunately, there's a bug on first render and you need to interact with the calendar by changing the week or view before it renders properly. - `renderCalendar()` includes all events from all event sources set in the global settings when no event sources aer passed in. ```` From 8a1b187ef61d1d207941fd6782f9668aff595eeb Mon Sep 17 00:00:00 2001 From: Jarek Bird Date: Wed, 24 May 2023 00:38:52 -0600 Subject: [PATCH 6/7] Typo Fix --- docs/advanced/dataview.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/advanced/dataview.md b/docs/advanced/dataview.md index 3fc86c2..875d492 100644 --- a/docs/advanced/dataview.md +++ b/docs/advanced/dataview.md @@ -11,7 +11,7 @@ renderCalendar(this.container, [[{start: new Date(), id: "id", title: "Now and f ``` ```` -`renderCalendar()` includes all events from all event sources set in the global settings when no event sources aer passed in. +`renderCalendar()` includes all events from all event sources set in the global settings when no event sources are passed in. ```` ```dataviewjs From 26d50e7bef750dccbc12b44bae8dc5406b6be1a0 Mon Sep 17 00:00:00 2001 From: Jarek Bird Date: Wed, 24 May 2023 00:32:23 -0600 Subject: [PATCH 7/7] Non Breaking Change Solution --- docs/advanced/dataview.md | 10 ++++++---- src/main.ts | 15 +++++++++------ 2 files changed, 15 insertions(+), 10 deletions(-) diff --git a/docs/advanced/dataview.md b/docs/advanced/dataview.md index 875d492..39c7434 100644 --- a/docs/advanced/dataview.md +++ b/docs/advanced/dataview.md @@ -6,8 +6,8 @@ Create calendars inline with your notes from [dataviewjs](https://blacksmithgu.g ```dataviewjs this.container.style.minHeight = "500px"; const { renderCalendar } = app.plugins.plugins["obsidian-full-calendar"]; -renderCalendar(this.container, [[{start: new Date(), id: "id", title: "Now and for an hour"}]]) - .then(calendar => calendar.render()); +let calendar = renderCalendar(this.container, [[{start: new Date(), id: "id", title: "Now and for an hour"}]]); +calendar.render() ``` ```` @@ -16,8 +16,10 @@ renderCalendar(this.container, [[{start: new Date(), id: "id", title: "Now and f ```` ```dataviewjs this.container.style.minHeight = "500px"; -const { renderCalendar } = app.plugins.plugins["obsidian-full-calendar"]; -renderCalendar(this.container).then(calendar => calendar.render()); +const { renderCalendar, initializeSettings } = app.plugins.plugins["obsidian-full-calendar"]; +await initializeSettings(); +let calendar = renderCalendar(this.container); +calendar.render(); ``` ```` diff --git a/src/main.ts b/src/main.ts index 255f8ef..2b5ee4f 100644 --- a/src/main.ts +++ b/src/main.ts @@ -64,15 +64,12 @@ export default class FullCalendarPlugin extends Plugin { }); translateSources = translateSources; - renderCalendar = async ( + renderCalendar = ( containerEl: HTMLElement, eventSources: EventSourceInput[], settings?: ExtraRenderProps ) => { if (!eventSources) { - if (!this.cache.initialized) { - await this.saveSettings(); - } eventSources = translateSources(this); } return calendarRender(containerEl, eventSources, settings); @@ -221,11 +218,17 @@ export default class FullCalendarPlugin extends Plugin { ); } - async saveSettings() { + initializeSettings = async () => { + if (!this.cache.initialized) { + await this.saveSettings(); + } + }; + + saveSettings = async () => { new Notice("Resetting the event cache with new settings..."); await this.saveData(this.settings); this.cache.reset(this.settings.calendarSources); await this.cache.populate(); this.cache.resync(); - } + }; }