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 (NonBreaking Option) #457

Open
wants to merge 7 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
12 changes: 11 additions & 1 deletion docs/advanced/dataview.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,16 @@ 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 are passed in.

````
```dataviewjs
this.container.style.minHeight = "500px";
const { renderCalendar, initializeSettings } = app.plugins.plugins["obsidian-full-calendar"];
await initializeSettings();
Copy link
Author

Choose a reason for hiding this comment

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

This is a non breaking change, but the user is required to manually call initializeSettings(); from the dataview to solve the issue.

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!
30 changes: 26 additions & 4 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,18 @@ 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);
}
return calendarRender(containerEl, eventSources, settings);
};

processFrontmatter = toEventInput;

async activateView() {
Expand Down Expand Up @@ -202,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();
}
};
}
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