Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
refactor(client/overlay): share the fullscreen button between the two maps
Elian Dorandd6a5e55b483
diff --git a/CLAUDE.md b/CLAUDE.mdindex 555c14b3d5..8279907446 100644--- a/CLAUDE.md+++ b/CLAUDE.md@@ -339,6 +339,7 @@ Use `note.getOwnedAttribute()` for direct, `note.getAttribute()` for inherited.- Import sorting via `eslint-plugin-simple-import-sort`- **Never use the TypeScript non-null assertion operator (postfix `!`)** — including in tests. Narrow instead: optional chaining (`?.`), a `?? fallback`, an explicit null check before use, or an `*OrThrow` accessor (e.g. `becca.getNoteOrThrow(id)` rather than `becca.getNote(id)!`).- **Helper placement** — when extracting a standalone helper function from a component, widget, hook, or route, place it **below** the primary export it supports (or in a separate module), not wedged between the imports and the main definition. Keep the file's primary export near the top so the entry point reads first; supporting helpers follow it.+- **No ~10-SLOC modules** — do not give a component, hook, or helper a file of its own when it amounts to about ten lines of substance. A file that exists to hold one small thing costs a module boundary, an import at every call site, and a second place to look, and buys nothing. Add it to an existing module instead — preferably one it is already built from, or that owns the same concept, so the group of related things stays discoverable from a single import. Example: `OverlayFullscreenButton` is a preset `OverlayControlButton`, so it lives in `apps/client/src/widgets/react/OverlayControlGroup.tsx` beside the group and button it composes, not in a file of its own — and its tests live in that module's spec for the same reason. Split a module out once it has grown enough substance to stand alone.## Testingdiff --git a/apps/client/src/translations/en/translation.json b/apps/client/src/translations/en/translation.jsonindex e78c496fb4..47442da047 100644--- a/apps/client/src/translations/en/translation.json+++ b/apps/client/src/translations/en/translation.json@@ -2945,8 +2945,6 @@"zoom-out": "Zoom out","enter-3d": "Tilt into a 3D view\nHold Ctrl and drag to tilt the map freely","exit-3d": "Lay the map flat again",- "fullscreen": "Fullscreen",- "exit-fullscreen": "Exit fullscreen","close-details": "Close","marker-color": "Marker color","copy-coordinates": "Copy coordinates"@@ -3745,8 +3743,6 @@"zoom-out": "Zoom out","reset-zoom": "Reset zoom","center-map": "Center the map",- "fullscreen": "Fullscreen",- "exit-fullscreen": "Exit fullscreen","direction-left": "Branches to the left","direction-right": "Branches to the right","direction-side": "Branches to both sides",@@ -3852,7 +3848,9 @@},"common": {"save": "Save",- "cancel": "Cancel"+ "cancel": "Cancel",+ "fullscreen": "Fullscreen",+ "exit_fullscreen": "Exit fullscreen"},"spreadsheet": {"read-only": "This note is read-only.",diff --git a/apps/client/src/widgets/collections/geomap/MapToolbar.tsx b/apps/client/src/widgets/collections/geomap/MapToolbar.tsxindex e5beef28ab..5f635207ea 100644--- a/apps/client/src/widgets/collections/geomap/MapToolbar.tsx+++ b/apps/client/src/widgets/collections/geomap/MapToolbar.tsx@@ -6,7 +6,7 @@ import { useContext, useEffect, useState } from "preact/hooks";import { t } from "../../../services/i18n";import { isMobile } from "../../../services/utils";import { useFullscreen } from "../../react/hooks";-import OverlayControlGroup, { OverlayControlButton } from "../../react/OverlayControlGroup";+import OverlayControlGroup, { OverlayControlButton, OverlayFullscreenButton } from "../../react/OverlayControlGroup";import { ParentMap, useMapPitch } from "./map";/**@@ -75,14 +75,10 @@ export default function MapToolbar() {onClick={() => map.zoomIn()}/></>}- <OverlayControlButton- title={isFullscreen ? t("geo-map.exit-fullscreen") : t("geo-map.fullscreen")}- icon={isFullscreen ? "bx-exit-fullscreen" : "bx-fullscreen"}- // Nothing here is measured across the change: the map keeps the middle of its view- // through a resize of its own accord, and it is told of the new size by the view- // itself (see `useElementSize` in map.tsx).- onClick={() => void toggleFullscreen()}- />+ {/* Nothing is measured across the change: the map keeps the middle of its view through+ a resize of its own accord, and it is told of the new size by the view itself (see+ `useElementSize` in map.tsx). */}+ <OverlayFullscreenButton isFullscreen={isFullscreen} onToggle={toggleFullscreen} /></OverlayControlGroup>);}diff --git a/apps/client/src/widgets/react/OverlayControlGroup.spec.tsx b/apps/client/src/widgets/react/OverlayControlGroup.spec.tsxindex a9b81e0c21..4c3e06d8db 100644--- a/apps/client/src/widgets/react/OverlayControlGroup.spec.tsx+++ b/apps/client/src/widgets/react/OverlayControlGroup.spec.tsx@@ -5,8 +5,9 @@ import { afterEach, describe, expect, it, vi } from "vitest";// The bootstrap tooltip needs real layout; capture what it would have been given instead.const { staticTooltipSpy } = vi.hoisted(() => ({ staticTooltipSpy: vi.fn() }));vi.mock("./hooks", () => ({ useStaticTooltip: staticTooltipSpy }));+vi.mock("../../services/i18n", () => ({ t: (key: string) => key }));-import OverlayControlGroup, { OverlayControlButton } from "./OverlayControlGroup";+import OverlayControlGroup, { OverlayControlButton, OverlayFullscreenButton } from "./OverlayControlGroup";let container: HTMLDivElement;@@ -198,3 +199,29 @@ describe("OverlayControlGroup", () => {expect(staticTooltipSpy.mock.calls.at(-1)?.[1]).toBe(first);});});++describe("OverlayFullscreenButton", () => {+ it("offers the screen, and once in it offers the way back — in its mark and in its name alike", () => {+ mount(<OverlayFullscreenButton isFullscreen={false} onToggle={vi.fn()} />);+ const offered = container.querySelector("button");+ expect(offered?.className).toContain("bx-fullscreen");+ expect(offered?.className).not.toContain("bx-exit-fullscreen");+ expect(offered?.getAttribute("aria-label")).toBe("common.fullscreen");++ act(() => render(<OverlayFullscreenButton isFullscreen onToggle={vi.fn()} />, container));+ const held = container.querySelector("button");+ expect(held?.className).toContain("bx-exit-fullscreen");+ expect(held?.getAttribute("aria-label")).toBe("common.exit_fullscreen");+ });++ it("asks the caller to make the change, which is the one thing it does not do itself", () => {+ const onToggle = vi.fn();+ mount(<OverlayFullscreenButton isFullscreen={false} onToggle={onToggle} />);++ act(() => container.querySelector("button")?.click());++ // Called with nothing: the press is the caller's cue, not something to hand on.+ expect(onToggle).toHaveBeenCalledTimes(1);+ expect(onToggle).toHaveBeenCalledWith();+ });+});diff --git a/apps/client/src/widgets/react/OverlayControlGroup.tsx b/apps/client/src/widgets/react/OverlayControlGroup.tsxindex 6578c2859b..70fc505e50 100644--- a/apps/client/src/widgets/react/OverlayControlGroup.tsx+++ b/apps/client/src/widgets/react/OverlayControlGroup.tsx@@ -2,6 +2,7 @@ import clsx from "clsx";import { type ComponentChildren, createContext, type HTMLAttributes } from "preact";import { useContext, useMemo, useRef } from "preact/hooks";+import { t } from "../../services/i18n";import type { ActionButtonProps } from "./ActionButton";import { useStaticTooltip } from "./hooks";@@ -103,5 +104,31 @@ export function OverlayControlButton(props: OverlayControlButtonProps) {);}+interface OverlayFullscreenButtonProps {+ /** Whether what the button stands over has the screen to itself. */+ isFullscreen: boolean;+ /** Gives it the screen, or takes it back. */+ onToggle: () => void;+}++/**+ * The button that gives what the group stands over the whole screen, and takes it back again — its+ * mark and what it is called both naming the way out once it is in. Every such button in the app says+ * the same two things, so it says them here rather than once per map.+ *+ * The state is handed to it rather than read: {@link useFullscreen} is what follows the browser, and+ * a caller may have to wrap it — the mind map takes the middle of its view before the change so as to+ * put it back after (see `useMapFullscreen`), which no button could do on its behalf.+ */+export function OverlayFullscreenButton({ isFullscreen, onToggle }: OverlayFullscreenButtonProps) {+ return (+ <OverlayControlButton+ title={isFullscreen ? t("common.exit_fullscreen") : t("common.fullscreen")}+ icon={isFullscreen ? "bx-exit-fullscreen" : "bx-fullscreen"}+ onClick={() => onToggle()}+ />+ );+}+/** Which way the tooltips on a group open, handed down by the group rather than repeated on each button. */const TooltipDirection = createContext<ActionButtonProps["titlePosition"]>("top");diff --git a/apps/client/src/widgets/type_widgets/mind_map/MapToolbar.tsx b/apps/client/src/widgets/type_widgets/mind_map/MapToolbar.tsxindex cd1d099e53..2e1cee4225 100644--- a/apps/client/src/widgets/type_widgets/mind_map/MapToolbar.tsx+++ b/apps/client/src/widgets/type_widgets/mind_map/MapToolbar.tsx@@ -6,7 +6,7 @@ import { useCallback, useEffect, useRef, useState } from "preact/hooks";import { t } from "../../../services/i18n";import { isMobile } from "../../../services/utils";import { useFullscreen } from "../../react/hooks";-import OverlayControlGroup, { OverlayControlButton } from "../../react/OverlayControlGroup";+import OverlayControlGroup, { OverlayControlButton, OverlayFullscreenButton } from "../../react/OverlayControlGroup";import OverlayToolbar, { OverlayToolbarButton } from "../../react/OverlayToolbar";import { centerMapOn, type MapPoint, readMapCenter, stepZoom } from "./viewport";@@ -79,11 +79,7 @@ export default function MapToolbar({ mind }: MapToolbarProps) {icon="bx-current-location"onClick={() => mind.toCenter()}/>- <OverlayControlButton- title={isFullscreen ? t("mind-map.exit-fullscreen") : t("mind-map.fullscreen")}- icon={isFullscreen ? "bx-exit-fullscreen" : "bx-fullscreen"}- onClick={toggleFullscreen}- />+ <OverlayFullscreenButton isFullscreen={isFullscreen} onToggle={toggleFullscreen} /></OverlayControlGroup>);}