Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Default Calendar Sources for renderCalendar + Cache Bugfix (Breaking Option) #456

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions docs/advanced/dataview.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,19 @@ 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());
```
````

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.
Copy link
Author

@jarekbird jarekbird May 24, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the cache had not been initialized, then the bug mentioned above would occur. This PR fixes this problem by conditionally initializing the cache as part of the renderCalendar() function.

This solution required setting the renderCalendar() function to be an async function because the cache initialization is async, which changes the public API. :/ Docs are updated to reflect this. This is a breaking change.

`renderCalendar()` includes all events from all event sources set in the global settings when no event sources are passed in.

````
```dataviewjs
this.container.style.minHeight = "500px";
const { renderCalendar } = app.plugins.plugins["obsidian-full-calendar"];
renderCalendar(this.container).then(calendar => 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!
23 changes: 21 additions & 2 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,12 @@ import {
FULL_CALENDAR_SIDEBAR_VIEW_TYPE,
FULL_CALENDAR_VIEW_TYPE,
} from "./ui/view";
import { renderCalendar } from "./ui/calendar";
import {
renderCalendar as calendarRender,
ExtraRenderProps,
} from "./ui/calendar";
import { toEventInput } from "./ui/interop";
import { translateSources } from "./ui/view";
import {
DEFAULT_SETTINGS,
FullCalendarSettings,
Expand All @@ -19,6 +23,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;
Expand Down Expand Up @@ -58,7 +63,21 @@ export default class FullCalendarPlugin extends Plugin {
FOR_TEST_ONLY: () => null,
});

renderCalendar = renderCalendar;
translateSources = translateSources;
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);
};

processFrontmatter = toEventInput;

async activateView() {
Expand Down
2 changes: 1 addition & 1 deletion src/ui/calendar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ rrulePlugin.recurringTypes[0].expand = function (errd, fr, de) {
});
};

interface ExtraRenderProps {
export interface ExtraRenderProps {
eventClick?: (info: EventClickArg) => void;
select?: (
startDate: Date,
Expand Down
28 changes: 13 additions & 15 deletions src/ui/view.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,17 @@ 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;
Expand Down Expand Up @@ -80,19 +91,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) {
Expand All @@ -116,7 +114,7 @@ export class CalendarView extends ItemView {
return;
}

const sources: EventSourceInput[] = this.translateSources();
const sources: EventSourceInput[] = translateSources(this.plugin);

if (this.fullCalendarView) {
this.fullCalendarView.destroy();
Expand Down Expand Up @@ -301,7 +299,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)
);
Expand Down