chore(react/collections/calendar): add views & first day of week

This commit is contained in:
Elian Doran
2025-09-05 16:26:52 +03:00
parent feb984649f
commit d33b1eb394
3 changed files with 21 additions and 24 deletions

View File

@@ -1,27 +1,22 @@
import { useEffect, useRef } from "preact/hooks"; import { useEffect, useRef } from "preact/hooks";
import { Calendar as FullCalendar, PluginDef } from "@fullcalendar/core"; import { CalendarOptions, Calendar as FullCalendar, PluginDef } from "@fullcalendar/core";
interface CalendarProps { interface CalendarProps extends CalendarOptions {
view: string;
tabIndex?: number; tabIndex?: number;
plugins: PluginDef[];
} }
export default function Calendar({ tabIndex, view, plugins }: CalendarProps) { export default function Calendar({ tabIndex, ...options }: CalendarProps) {
const calendarRef = useRef<FullCalendar>(); const calendarRef = useRef<FullCalendar>();
const containerRef = useRef<HTMLDivElement>(null); const containerRef = useRef<HTMLDivElement>(null);
useEffect(() => { useEffect(() => {
if (!containerRef.current) return; if (!containerRef.current) return;
const calendar = new FullCalendar(containerRef.current, { const calendar = new FullCalendar(containerRef.current, options);
initialView: view,
plugins: plugins
});
calendar.render(); calendar.render();
return () => calendar.destroy(); return () => calendar.destroy();
}, [ containerRef ]); }, [ containerRef, options ]);
return ( return (
<div ref={containerRef} className="calendar-container" tabIndex={tabIndex} /> <div ref={containerRef} className="calendar-container" tabIndex={tabIndex} />

View File

@@ -3,20 +3,34 @@ import { ViewModeProps } from "../interface";
import Calendar from "./calendar"; import Calendar from "./calendar";
import { useEffect, useState } from "preact/hooks"; import { useEffect, useState } from "preact/hooks";
import "./index.css"; import "./index.css";
import { useTriliumOption, useTriliumOptionInt } from "../../react/hooks";
interface CalendarViewData { interface CalendarViewData {
} }
const CALENDAR_VIEWS = [
"timeGridWeek",
"dayGridMonth",
"multiMonthYear",
"listMonth"
]
export default function CalendarView({ note, noteIds }: ViewModeProps<CalendarViewData>) { export default function CalendarView({ note, noteIds }: ViewModeProps<CalendarViewData>) {
const plugins = usePlugins(false, false); const plugins = usePlugins(false, false);
const [ firstDayOfWeek ] = useTriliumOptionInt("firstDayOfWeek");
return (plugins && return (plugins &&
<div className="calendar-view"> <div className="calendar-view">
<Calendar <Calendar
plugins={plugins} plugins={plugins}
tabIndex={100} tabIndex={100}
view="dayGridMonth" initialView="dayGridMonth"
headerToolbar={{
start: "title",
end: `${CALENDAR_VIEWS.join(",")} today prev,next`
}}
firstDay={firstDayOfWeek ?? 0}
/> />
</div> </div>
); );

View File

@@ -46,12 +46,7 @@ interface Event {
endTime?: string | null endTime?: string | null
} }
const CALENDAR_VIEWS = [
"timeGridWeek",
"dayGridMonth",
"multiMonthYear",
"listMonth"
]
export default class CalendarView extends ViewMode<{}> { export default class CalendarView extends ViewMode<{}> {
@@ -91,14 +86,11 @@ export default class CalendarView extends ViewMode<{}> {
} }
const calendar = new Calendar(this.$calendarContainer[0], { const calendar = new Calendar(this.$calendarContainer[0], {
plugins,
initialView,
events: eventBuilder, events: eventBuilder,
editable: isEditable, editable: isEditable,
selectable: isEditable, selectable: isEditable,
select: (e) => this.#onCalendarSelection(e), select: (e) => this.#onCalendarSelection(e),
eventChange: (e) => this.#onEventMoved(e), eventChange: (e) => this.#onEventMoved(e),
firstDay: options.getInt("firstDayOfWeek") ?? 0,
weekends: !this.parentNote.hasAttribute("label", "calendar:hideWeekends"), weekends: !this.parentNote.hasAttribute("label", "calendar:hideWeekends"),
weekNumbers: this.parentNote.hasAttribute("label", "calendar:weekNumbers"), weekNumbers: this.parentNote.hasAttribute("label", "calendar:weekNumbers"),
locale: await getFullCalendarLocale(options.get("locale")), locale: await getFullCalendarLocale(options.get("locale")),
@@ -167,10 +159,6 @@ export default class CalendarView extends ViewMode<{}> {
} }
}, },
datesSet: (e) => this.#onDatesSet(e), datesSet: (e) => this.#onDatesSet(e),
headerToolbar: {
start: "title",
end: `${CALENDAR_VIEWS.join(",")} today prev,next`
}
}); });
new ResizeObserver(() => calendar.updateSize()) new ResizeObserver(() => calendar.updateSize())