mirror of
				https://github.com/zadam/trilium.git
				synced 2025-10-30 09:56:36 +01:00 
			
		
		
		
	chore(react/collections/calendar): add views & first day of week
This commit is contained in:
		| @@ -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} /> | ||||||
|   | |||||||
| @@ -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> | ||||||
|     ); |     ); | ||||||
|   | |||||||
| @@ -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()) | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user