From f7c82d6b09cd289bc2ddec277f58761ac41f5158 Mon Sep 17 00:00:00 2001 From: Elian Doran Date: Sat, 23 Aug 2025 11:00:25 +0300 Subject: [PATCH] chore(react/ribbon): watch note map size --- apps/client/src/widgets/react/hooks.tsx | 18 ++++++- apps/client/src/widgets/ribbon/NoteMapTab.tsx | 23 ++++----- .../src/widgets/ribbon_widgets/note_map.ts | 48 ------------------- 3 files changed, 29 insertions(+), 60 deletions(-) delete mode 100644 apps/client/src/widgets/ribbon_widgets/note_map.ts diff --git a/apps/client/src/widgets/react/hooks.tsx b/apps/client/src/widgets/react/hooks.tsx index d2a253377..6f1595bfb 100644 --- a/apps/client/src/widgets/react/hooks.tsx +++ b/apps/client/src/widgets/react/hooks.tsx @@ -12,7 +12,7 @@ import FNote from "../../entities/fnote"; import attributes from "../../services/attributes"; import FBlob from "../../entities/fblob"; import NoteContextAwareWidget from "../note_context_aware_widget"; -import { VNode } from "preact"; +import { RefObject, VNode } from "preact"; type TriliumEventHandler = (data: EventData) => void; const registeredHandlers: Map[]>> = new Map(); @@ -454,4 +454,20 @@ export function useLegacyWidget(widgetFactory: () => T, { }, [ noteContext ]); return [
, widget ] +} + +export function useResizeObserver(ref: RefObject, callback: ResizeObserverCallback) { + useEffect(() => { + if (!ref.current) { + return; + } + + const element = ref.current; + const resizeObserver = new ResizeObserver(callback); + resizeObserver.observe(element); + return () => { + resizeObserver.unobserve(element); + resizeObserver.disconnect(); + } + }, [ ref, callback ]); } \ No newline at end of file diff --git a/apps/client/src/widgets/ribbon/NoteMapTab.tsx b/apps/client/src/widgets/ribbon/NoteMapTab.tsx index 4a35ab3c0..a5b0a98e5 100644 --- a/apps/client/src/widgets/ribbon/NoteMapTab.tsx +++ b/apps/client/src/widgets/ribbon/NoteMapTab.tsx @@ -1,9 +1,9 @@ import { TabContext } from "./ribbon-interface"; import NoteMapWidget from "../note_map"; -import { useLegacyWidget } from "../react/hooks"; +import { useLegacyWidget, useResizeObserver } from "../react/hooks"; import ActionButton from "../react/ActionButton"; import { t } from "../../services/i18n"; -import { useEffect, useRef, useState } from "preact/hooks"; +import { useCallback, useEffect, useRef, useState } from "preact/hooks"; const SMALL_SIZE_HEIGHT = "300px"; @@ -17,19 +17,20 @@ export default function NoteMapTab({ note, noteContext }: TabContext) { containerClassName: "note-map-container" }); - useEffect(() => noteMapWidget.setDimensions(), [ height ]); - - function toggleExpanded(newValue: boolean) { - setExpanded(newValue); - - if (newValue && containerRef.current) { + const resizeIfNeeded = useCallback(() => { + console.log("Resize if needed"); + if (isExpanded && containerRef.current) { const { top } = containerRef.current.getBoundingClientRect(); const height = window.innerHeight - top; setHeight(height + "px"); } else { setHeight(SMALL_SIZE_HEIGHT); } - } + }, [ isExpanded, containerRef ]); + + useEffect(() => noteMapWidget.setDimensions(), [ height ]); + useEffect(() => resizeIfNeeded(), [ isExpanded ]); + useResizeObserver(containerRef, resizeIfNeeded); return (
@@ -40,14 +41,14 @@ export default function NoteMapTab({ note, noteContext }: TabContext) { icon="bx bx-arrow-to-bottom" text={t("note_map.open_full")} className="open-full-button" - onClick={() => toggleExpanded(true)} + onClick={() => setExpanded(true)} /> ) : ( toggleExpanded(false)} + onClick={() => setExpanded(false)} /> )}
diff --git a/apps/client/src/widgets/ribbon_widgets/note_map.ts b/apps/client/src/widgets/ribbon_widgets/note_map.ts deleted file mode 100644 index 93facf3db..000000000 --- a/apps/client/src/widgets/ribbon_widgets/note_map.ts +++ /dev/null @@ -1,48 +0,0 @@ -import NoteContextAwareWidget from "../note_context_aware_widget.js"; -import NoteMapWidget from "../note_map.js"; -import { t } from "../../services/i18n.js"; - -export default class NoteMapRibbonWidget extends NoteContextAwareWidget { - - private openState!: "small" | "full"; - private $container!: JQuery; - private $openFullButton!: JQuery; - private $collapseButton!: JQuery; - - - doRender() { - this.$widget = $(TPL); - this.contentSized(); - this.$container = this.$widget.find(".note-map-container"); - this.$container.append(this.noteMapWidget.render()); - - this.openState = "small"; - - this.$openFullButton = this.$widget.find(".open-full-button"); - this.$openFullButton.on("click", () => { - this.setFullHeight(); - - this.$openFullButton.hide(); - this.$collapseButton.show(); - - this.openState = "full"; - - this.noteMapWidget.setDimensions(); - }); - - this.$collapseButton = this.$widget.find(".collapse-button"); - this.$collapseButton.on("click", () => { - this.setSmallSize(); - - this.$openFullButton.show(); - this.$collapseButton.hide(); - - this.openState = "small"; - - this.noteMapWidget.setDimensions(); - }); - - new ResizeObserver(handleResize).observe(this.$widget[0]); - } - -}