chore(react/ribbon): react note map to height changes

This commit is contained in:
Elian Doran 2025-08-23 11:12:14 +03:00
parent f7c82d6b09
commit 5f77ca31bd
No known key found for this signature in database
2 changed files with 27 additions and 9 deletions

View File

@ -470,4 +470,25 @@ export function useResizeObserver(ref: RefObject<HTMLElement>, callback: ResizeO
resizeObserver.disconnect(); resizeObserver.disconnect();
} }
}, [ ref, callback ]); }, [ ref, callback ]);
}
export function useWindowSize() {
const [ size, setSize ] = useState<{ windowWidth: number, windowHeight: number }>({
windowWidth: window.innerWidth,
windowHeight: window.innerHeight
});
useEffect(() => {
function onResize() {
setSize({
windowWidth: window.innerWidth,
windowHeight: window.innerHeight
});
}
window.addEventListener("resize", onResize);
return () => window.removeEventListener("resize", onResize);
});
return size;
} }

View File

@ -1,6 +1,6 @@
import { TabContext } from "./ribbon-interface"; import { TabContext } from "./ribbon-interface";
import NoteMapWidget from "../note_map"; import NoteMapWidget from "../note_map";
import { useLegacyWidget, useResizeObserver } from "../react/hooks"; import { useLegacyWidget, useWindowSize } from "../react/hooks";
import ActionButton from "../react/ActionButton"; import ActionButton from "../react/ActionButton";
import { t } from "../../services/i18n"; import { t } from "../../services/i18n";
import { useCallback, useEffect, useRef, useState } from "preact/hooks"; import { useCallback, useEffect, useRef, useState } from "preact/hooks";
@ -11,26 +11,23 @@ export default function NoteMapTab({ note, noteContext }: TabContext) {
const [ isExpanded, setExpanded ] = useState(false); const [ isExpanded, setExpanded ] = useState(false);
const [ height, setHeight ] = useState(SMALL_SIZE_HEIGHT); const [ height, setHeight ] = useState(SMALL_SIZE_HEIGHT);
const containerRef = useRef<HTMLDivElement>(null); const containerRef = useRef<HTMLDivElement>(null);
const { windowHeight } = useWindowSize();
const [ noteMapContainer, noteMapWidget ] = useLegacyWidget(() => new NoteMapWidget("ribbon"), { const [ noteMapContainer, noteMapWidget ] = useLegacyWidget(() => new NoteMapWidget("ribbon"), {
noteContext, noteContext,
containerClassName: "note-map-container" containerClassName: "note-map-container"
}); });
const resizeIfNeeded = useCallback(() => { useEffect(() => {
console.log("Resize if needed");
if (isExpanded && containerRef.current) { if (isExpanded && containerRef.current) {
const { top } = containerRef.current.getBoundingClientRect(); const { top } = containerRef.current.getBoundingClientRect();
const height = window.innerHeight - top; const height = windowHeight - top;
setHeight(height + "px"); setHeight(height + "px");
} else { } else {
setHeight(SMALL_SIZE_HEIGHT); setHeight(SMALL_SIZE_HEIGHT);
} }
}, [ isExpanded, containerRef ]); }, [ isExpanded, containerRef, windowHeight ]);
useEffect(() => noteMapWidget.setDimensions(), [ height ]); useEffect(() => noteMapWidget.setDimensions(), [ height ]);
useEffect(() => resizeIfNeeded(), [ isExpanded ]);
useResizeObserver(containerRef, resizeIfNeeded);
return ( return (
<div className="note-map-ribbon-widget" style={{ height }} ref={containerRef}> <div className="note-map-ribbon-widget" style={{ height }} ref={containerRef}>