Map: a Lines setting for curved or right-angled connections
Both map views can draw their lines as right-angled runs with rounded corners instead of curves; the choice is remembered per browser. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
939ebf8d83
commit
2986734196
7 files changed
+191
-5
No files matched your search
@@ -4,6 +4,8 @@ import { api, type DependencyGraph, type DependencyGraphNode } from "../api/clie
|
||||
import { ErrorNotice } from "./Notice";
|
||||
import { PageSkeleton } from "./Skeleton";
|
||||
import { readableError } from "../utils/errors";
|
||||
import MapLinesToggle, { useMapLineStyle } from "../components/MapLinesToggle";
|
||||
import { edgePath } from "../utils/mapLines";
|
||||
|
||||
const NODE_W = 200;
|
||||
const NODE_H = 46;
|
||||
@@ -88,6 +90,7 @@ const names = (graph: DependencyGraph, keys: Iterable<string>) => [...keys].map(
|
||||
* hovering does the same for a moment. This is the answer to "what breaks if I reboot this?" before doing it.
|
||||
*/
|
||||
export default function DependencyMap() {
|
||||
const [lineStyle] = useMapLineStyle();
|
||||
const [graph, setGraph] = useState<DependencyGraph | null>(null);
|
||||
const [error, setError] = useState<string | null>(null);
|
||||
// From the command palette ("What depends on …?"): start with that box picked.
|
||||
@@ -127,6 +130,7 @@ export default function DependencyMap() {
|
||||
return (
|
||||
<div className="card">
|
||||
<div className="card-body map-scroll">
|
||||
<MapLinesToggle />
|
||||
<div className="map-canvas" style={{ width: view.width, height: view.height }}>
|
||||
<svg className="map-lines" width={view.width} height={view.height} aria-hidden="true">
|
||||
{graph.edges.map((e) => {
|
||||
@@ -137,14 +141,13 @@ export default function DependencyMap() {
|
||||
const y1 = a.y + NODE_H / 2;
|
||||
const x2 = b.x;
|
||||
const y2 = b.y + NODE_H / 2;
|
||||
const mid = (x1 + x2) / 2;
|
||||
let cls = "map-edge";
|
||||
if (sets && active) {
|
||||
const downPath = (e.from === active || sets.down.has(e.from)) && sets.down.has(e.to);
|
||||
const upPath = (e.to === active || sets.up.has(e.to)) && sets.up.has(e.from);
|
||||
cls = downPath ? "map-edge map-edge-down" : upPath ? "map-edge map-edge-up" : "map-edge map-dim";
|
||||
}
|
||||
return <path key={`${e.from}>${e.to}`} d={`M${x1},${y1} C${mid},${y1} ${mid},${y2} ${x2},${y2}`} className={cls} fill="none" strokeDasharray={e.kind !== "written" ? "5 4" : undefined} />;
|
||||
return <path key={`${e.from}>${e.to}`} d={edgePath(x1, y1, x2, y2, lineStyle)} className={cls} fill="none" strokeDasharray={e.kind !== "written" ? "5 4" : undefined} />;
|
||||
})}
|
||||
</svg>
|
||||
{[...view.placed.values()].map((n) => (
|
||||
|
||||
@@ -0,0 +1,52 @@
|
||||
import { useCallback, useEffect, useState } from "react";
|
||||
import { t } from "../i18n";
|
||||
import { MAP_LINE_STYLES, MAP_LINES_KEY, parseMapLineStyle, type MapLineStyle } from "../utils/mapLines";
|
||||
|
||||
function stored(): MapLineStyle {
|
||||
try {
|
||||
return parseMapLineStyle(localStorage.getItem(MAP_LINES_KEY));
|
||||
} catch {
|
||||
return "curved";
|
||||
}
|
||||
}
|
||||
|
||||
// The two maps share one choice, and a change in one is seen by the other without a reload.
|
||||
const listeners = new Set<(s: MapLineStyle) => void>();
|
||||
|
||||
/** The line style of the maps: remembered in this browser (like the density), shared by both maps. */
|
||||
export function useMapLineStyle(): [MapLineStyle, (style: MapLineStyle) => void] {
|
||||
const [style, setStyle] = useState<MapLineStyle>(stored);
|
||||
useEffect(() => {
|
||||
listeners.add(setStyle);
|
||||
return () => void listeners.delete(setStyle);
|
||||
}, []);
|
||||
const choose = useCallback((next: MapLineStyle) => {
|
||||
try {
|
||||
if (next === "curved") localStorage.removeItem(MAP_LINES_KEY);
|
||||
else localStorage.setItem(MAP_LINES_KEY, next);
|
||||
} catch {
|
||||
// not remembered; it still applies for this visit
|
||||
}
|
||||
listeners.forEach((l) => l(next));
|
||||
}, []);
|
||||
return [style, choose];
|
||||
}
|
||||
|
||||
/** "Lines: Curved | Right-angled" — how the connections between the boxes are drawn. */
|
||||
export default function MapLinesToggle() {
|
||||
const [style, choose] = useMapLineStyle();
|
||||
return (
|
||||
<div className="d-flex align-items-center justify-content-end gap-2 mb-2">
|
||||
<span className="text-secondary small" id="map-lines-label">
|
||||
{t("Lines")}
|
||||
</span>
|
||||
<div className="btn-group btn-group-sm" role="group" aria-labelledby="map-lines-label">
|
||||
{MAP_LINE_STYLES.map((s) => (
|
||||
<button key={s.value} type="button" className={`btn ${style === s.value ? "active" : ""}`} aria-pressed={style === s.value} onClick={() => choose(s.value)}>
|
||||
{t(s.label)}
|
||||
</button>
|
||||
))}
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@@ -206,6 +206,7 @@ export const sv: Record<string, string> = {
|
||||
"CPU load": "Processorbelastning",
|
||||
"Created": "Skapad",
|
||||
"Critical": "Kritisk",
|
||||
"Curved": "Böjda",
|
||||
"Customise": "Anpassa",
|
||||
"Dark mode": "Mörkt läge",
|
||||
"Dashboard": "Översikt",
|
||||
@@ -357,6 +358,7 @@ export const sv: Record<string, string> = {
|
||||
"Left out now": "Utelämnas nu",
|
||||
"Let someone else in (optional)": "Släpp in någon annan (valfritt)",
|
||||
"Light mode": "Ljust läge",
|
||||
"Lines": "Linjer",
|
||||
"Link": "Koppla",
|
||||
"Linked to": "Kopplad till",
|
||||
"Linking…": "Kopplar…",
|
||||
@@ -564,6 +566,7 @@ export const sv: Record<string, string> = {
|
||||
"restart: {state}": "omstart: {state}",
|
||||
"Restart…": "Starta om…",
|
||||
"restarted": "omstartad",
|
||||
"Right-angled": "Rätvinkliga",
|
||||
"Roomy": "Luftig",
|
||||
"Rotate every": "Rotera var",
|
||||
"Rotate token": "Byt nyckel",
|
||||
|
||||
@@ -9,6 +9,8 @@ import { PageSkeleton } from "../components/Skeleton";
|
||||
import { readableError } from "../utils/errors";
|
||||
import PageIntro from "../components/PageIntro";
|
||||
import { t } from "../i18n";
|
||||
import MapLinesToggle, { useMapLineStyle } from "../components/MapLinesToggle";
|
||||
import { edgePath } from "../utils/mapLines";
|
||||
|
||||
const NODE_W = 210;
|
||||
const NODE_H = 46;
|
||||
@@ -130,6 +132,7 @@ export default function MapPage({ user }: { user: CurrentUser }) {
|
||||
}
|
||||
|
||||
function ConnectionsMap() {
|
||||
const [lineStyle] = useMapLineStyle();
|
||||
const [data, setData] = useState<MapData | null>(null);
|
||||
const [error, setError] = useState<string | null>(null);
|
||||
const [focus, setFocus] = useState<string | null>(null);
|
||||
@@ -169,6 +172,7 @@ function ConnectionsMap() {
|
||||
{data && drawing && data.nodes.length > 0 && (
|
||||
<div className="card">
|
||||
<div className="card-body map-scroll">
|
||||
<MapLinesToggle />
|
||||
<div className="map-columns" style={{ width: drawing.width }}>
|
||||
{drawing.columns.map((c) => (
|
||||
<div key={c.title} className="map-column-title" style={{ left: c.x, width: NODE_W }}>
|
||||
@@ -188,9 +192,8 @@ function ConnectionsMap() {
|
||||
const y1 = l.y + NODE_H / 2;
|
||||
const x2 = r.x;
|
||||
const y2 = r.y + NODE_H / 2;
|
||||
const mid = (x1 + x2) / 2;
|
||||
const active = !lit || (lit.has(e.from) && lit.has(e.to) && (e.from === focus || e.to === focus));
|
||||
return <path key={`${e.from}>${e.to}`} d={`M${x1},${y1} C${mid},${y1} ${mid},${y2} ${x2},${y2}`} className={active ? "map-edge" : "map-edge map-dim"} fill="none" />;
|
||||
return <path key={`${e.from}>${e.to}`} d={edgePath(x1, y1, x2, y2, lineStyle)} className={active ? "map-edge" : "map-edge map-dim"} fill="none" />;
|
||||
})}
|
||||
</svg>
|
||||
{[...drawing.placed.values()].map((n) => {
|
||||
|
||||
@@ -0,0 +1,45 @@
|
||||
/**
|
||||
* How the lines between the boxes of the maps are drawn: as smooth curves (the original look), or right-angled — out from a box, across, up or down, and into the next, the way a wiring diagram
|
||||
* is drawn. Chosen per browser, like the density; it changes only the look of the lines, never what is connected to what. Pure, so it can be tested without a browser.
|
||||
*/
|
||||
export type MapLineStyle = "curved" | "right-angled";
|
||||
|
||||
export const MAP_LINE_STYLES: { value: MapLineStyle; label: string }[] = [
|
||||
{ value: "curved", label: "Curved" },
|
||||
{ value: "right-angled", label: "Right-angled" },
|
||||
];
|
||||
|
||||
export const MAP_LINES_KEY = "hlm-map-lines";
|
||||
|
||||
/** What is stored, read back as a style; anything else is the original curved look. */
|
||||
export function parseMapLineStyle(stored: string | null | undefined): MapLineStyle {
|
||||
return stored === "right-angled" ? "right-angled" : "curved";
|
||||
}
|
||||
|
||||
/** How far the corners of a right-angled line are rounded, in pixels (a hard corner reads as clutter where many lines meet). */
|
||||
export const CORNER_RADIUS = 8;
|
||||
|
||||
const r1 = (n: number) => Math.round(n * 10) / 10;
|
||||
|
||||
/**
|
||||
* The path of a line from (x1, y1) on the left to (x2, y2) on the right. Curved: an S through the middle. Right-angled: straight out to the middle, straight up or down, straight on in — with
|
||||
* the two corners rounded where there is room for it. A line between boxes at the same height is straight either way.
|
||||
*/
|
||||
export function edgePath(x1: number, y1: number, x2: number, y2: number, style: MapLineStyle): string {
|
||||
const mid = (x1 + x2) / 2;
|
||||
if (style === "curved") return `M${r1(x1)},${r1(y1)} C${r1(mid)},${r1(y1)} ${r1(mid)},${r1(y2)} ${r1(x2)},${r1(y2)}`;
|
||||
if (Math.abs(y2 - y1) < 0.5) return `M${r1(x1)},${r1(y1)} H${r1(x2)}`;
|
||||
const dy = y2 - y1;
|
||||
const down = dy > 0 ? 1 : -1;
|
||||
// the corner can be no rounder than half the room it has, along and across
|
||||
const r = Math.min(CORNER_RADIUS, Math.abs(dy) / 2, Math.abs(x2 - x1) / 4);
|
||||
if (r < 1) return `M${r1(x1)},${r1(y1)} H${r1(mid)} V${r1(y2)} H${r1(x2)}`;
|
||||
return [
|
||||
`M${r1(x1)},${r1(y1)}`,
|
||||
`H${r1(mid - r)}`,
|
||||
`Q${r1(mid)},${r1(y1)} ${r1(mid)},${r1(y1 + down * r)}`,
|
||||
`V${r1(y2 - down * r)}`,
|
||||
`Q${r1(mid)},${r1(y2)} ${r1(mid + r)},${r1(y2)}`,
|
||||
`H${r1(x2)}`,
|
||||
].join(" ");
|
||||
}
|
||||
Reference in new issue
Block a user