mirror of
				https://github.com/zadam/trilium.git
				synced 2025-10-30 09:56:36 +01:00 
			
		
		
		
	chore(react/collections/calendar): handle resize
This commit is contained in:
		| @@ -3,7 +3,7 @@ import { ViewModeProps } from "../interface"; | |||||||
| import Calendar from "./calendar"; | import Calendar from "./calendar"; | ||||||
| import { useEffect, useRef, useState } from "preact/hooks"; | import { useEffect, useRef, useState } from "preact/hooks"; | ||||||
| import "./index.css"; | import "./index.css"; | ||||||
| import { useNoteLabel, useNoteLabelBoolean, useSpacedUpdate, useTriliumOption, useTriliumOptionInt } from "../../react/hooks"; | import { useNoteLabel, useNoteLabelBoolean, useResizeObserver, useSpacedUpdate, useTriliumOption, useTriliumOptionInt } from "../../react/hooks"; | ||||||
| import { LOCALE_IDS } from "@triliumnext/commons"; | import { LOCALE_IDS } from "@triliumnext/commons"; | ||||||
| import { Calendar as FullCalendar } from "@fullcalendar/core"; | import { Calendar as FullCalendar } from "@fullcalendar/core"; | ||||||
| import { setLabel } from "../../../services/attributes"; | import { setLabel } from "../../../services/attributes"; | ||||||
| @@ -36,6 +36,7 @@ const LOCALE_MAPPINGS: Record<LOCALE_IDS, (() => Promise<{ default: LocaleInput | |||||||
| }; | }; | ||||||
|  |  | ||||||
| export default function CalendarView({ note, noteIds }: ViewModeProps<CalendarViewData>) { | export default function CalendarView({ note, noteIds }: ViewModeProps<CalendarViewData>) { | ||||||
|  |     const containerRef = useRef<HTMLDivElement>(null); | ||||||
|     const calendarRef = useRef<FullCalendar>(null); |     const calendarRef = useRef<FullCalendar>(null); | ||||||
|     const plugins = usePlugins(false, false); |     const plugins = usePlugins(false, false); | ||||||
|     const locale = useLocale(); |     const locale = useLocale(); | ||||||
| @@ -45,9 +46,10 @@ export default function CalendarView({ note, noteIds }: ViewModeProps<CalendarVi | |||||||
|     const [ calendarView, setCalendarView ] = useNoteLabel(note, "calendar:view"); |     const [ calendarView, setCalendarView ] = useNoteLabel(note, "calendar:view"); | ||||||
|     const initialView = useRef(calendarView); |     const initialView = useRef(calendarView); | ||||||
|     const viewSpacedUpdate = useSpacedUpdate(() => setCalendarView(initialView.current)); |     const viewSpacedUpdate = useSpacedUpdate(() => setCalendarView(initialView.current)); | ||||||
|  |     useResizeObserver(containerRef, () => calendarRef.current?.updateSize()); | ||||||
|  |  | ||||||
|     return (plugins && |     return (plugins && | ||||||
|         <div className="calendar-view"> |         <div className="calendar-view" ref={containerRef}> | ||||||
|             <Calendar |             <Calendar | ||||||
|                 calendarRef={calendarRef} |                 calendarRef={calendarRef} | ||||||
|                 plugins={plugins} |                 plugins={plugins} | ||||||
| @@ -60,6 +62,7 @@ export default function CalendarView({ note, noteIds }: ViewModeProps<CalendarVi | |||||||
|                 firstDay={firstDayOfWeek ?? 0} |                 firstDay={firstDayOfWeek ?? 0} | ||||||
|                 weekends={!hideWeekends} |                 weekends={!hideWeekends} | ||||||
|                 weekNumbers={weekNumbers} |                 weekNumbers={weekNumbers} | ||||||
|  |                 handleWindowResize={false} | ||||||
|                 locale={locale} |                 locale={locale} | ||||||
|                 viewDidMount={({ view }) => { |                 viewDidMount={({ view }) => { | ||||||
|                     if (initialView.current !== view.type) { |                     if (initialView.current !== view.type) { | ||||||
|   | |||||||
| @@ -627,3 +627,18 @@ export function useTouchBar( | |||||||
|         parentComponent?.triggerCommand("refreshTouchBar"); |         parentComponent?.triggerCommand("refreshTouchBar"); | ||||||
|     }, inputs); |     }, inputs); | ||||||
| } | } | ||||||
|  |  | ||||||
|  | export function useResizeObserver(ref: RefObject<HTMLElement>, callback: () => void) { | ||||||
|  |     const resizeObserver = useRef<ResizeObserver>(null); | ||||||
|  |     useEffect(() => { | ||||||
|  |         resizeObserver.current?.disconnect(); | ||||||
|  |         const observer = new ResizeObserver(callback); | ||||||
|  |         resizeObserver.current = observer; | ||||||
|  |  | ||||||
|  |         if (ref.current) { | ||||||
|  |             observer.observe(ref.current); | ||||||
|  |         } | ||||||
|  |  | ||||||
|  |         return () => observer.disconnect(); | ||||||
|  |     }, [ callback, ref ]); | ||||||
|  | } | ||||||
|   | |||||||
| @@ -70,7 +70,6 @@ export default class CalendarView extends ViewMode<{}> { | |||||||
|             eventChange: (e) => this.#onEventMoved(e), |             eventChange: (e) => this.#onEventMoved(e), | ||||||
|             height: "100%", |             height: "100%", | ||||||
|             nowIndicator: true, |             nowIndicator: true, | ||||||
|             handleWindowResize: false, |  | ||||||
|             eventDidMount: (e) => { |             eventDidMount: (e) => { | ||||||
|                 const { iconClass, promotedAttributes } = e.event.extendedProps; |                 const { iconClass, promotedAttributes } = e.event.extendedProps; | ||||||
|  |  | ||||||
| @@ -135,9 +134,6 @@ export default class CalendarView extends ViewMode<{}> { | |||||||
|             datesSet: (e) => this.#onDatesSet(e), |             datesSet: (e) => this.#onDatesSet(e), | ||||||
|         }); |         }); | ||||||
|  |  | ||||||
|         new ResizeObserver(() => calendar.updateSize()) |  | ||||||
|             .observe(this.$calendarContainer[0]); |  | ||||||
|  |  | ||||||
|         return this.$root; |         return this.$root; | ||||||
|     } |     } | ||||||
|  |  | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user