/** * Build a tool toolbar inside an engine-provided container (MapLibre `ctrl-group` * / OpenLayers `ol-control` / a plain Leaflet div), so buttons inherit the * engine's native control look. Placement/flow via {@link ToolbarOptions}. * * An item with `children` becomes a **submenu**: clicking its button opens a flyout * of the child buttons, positioned *into the map* based on the toolbar edge. */ import type { ToolbarItem, ToolbarOptions } from "./index.js"; import { CHROME_SURFACE, CHROME_BORDER, CHROME_SHADOW, CHROME_HOVER, CHROME_INK, CHROME_BTN_PX } from "./chrome.js"; const STYLE_ID = "draw-adapter-toolbar-style"; const TOOLBAR_CLASS = "draw-adapter-toolbar"; /** Engine-stable hook class added to every toolbar container (so shared CSS + `setToolbarActive` * target one selector across MapLibre/OpenLayers/Leaflet). */ const SHARED_CLASS = "dap-toolbar"; /** Default highlight for the active tool button (the former per-engine `#dbeafe`). */ const DEFAULT_ACTIVE_BG = "#dbeafe"; /** Per-container registry: maps every item id (incl. submenu descendants) to its **bar** button, plus * the consumer's `activeStyle`. Lets {@link setToolbarActive} drive the highlight by id. */ const toolbarReg = new WeakMap; activeStyle: ToolbarOptions["activeStyle"] }>(); /** Inline props {@link setToolbarActive} writes for the active state (cleared on deactivate). */ function applyActiveStyle(btn: HTMLElement, s: ToolbarOptions["activeStyle"]): void { btn.style.background = s?.background ?? DEFAULT_ACTIVE_BG; if (s?.color) btn.style.color = s.color; if (s?.outline) btn.style.outline = s.outline; if (s?.boxShadow) btn.style.boxShadow = s.boxShadow; } function clearActiveStyle(btn: HTMLElement): void { btn.style.background = ""; btn.style.color = ""; btn.style.outline = ""; btn.style.boxShadow = ""; } /** * Set (or clear) the **active** tool highlight on a toolbar built by {@link populateToolbar}. * Consumer-driven: `id` = a `ToolbarItem` id → its bar button (a submenu/toggle descendant id marks * its parent bar trigger); `null` → clear. One active at a time; idempotent. The highlight is applied * inline (default `#dbeafe`, overridable via `ToolbarOptions.activeStyle`) so it wins uniformly on all * three engines regardless of their own button CSS. No-op if `el` isn't a known toolbar. */ export function setToolbarActive(el: HTMLElement, id: string | null): void { const reg = toolbarReg.get(el); el.querySelectorAll("button.active").forEach((b) => { b.classList.remove("active"); clearActiveStyle(b); }); if (id == null) return; const btn = reg?.byId.get(id); if (!btn) return; btn.classList.add("active"); applyActiveStyle(btn, reg?.activeStyle); } /** Collect an item's id + all its (recursive) submenu descendant ids — all map to the same bar button. */ function collectIds(item: ToolbarItem, acc: string[]): void { acc.push(item.id); for (const c of item.children ?? []) collectIds(c, acc); } /** Neutral placeholder icon used when a {@link ToolbarItem} provides no `svg`. */ const FALLBACK_ICON = ``; /** The four directions a flyout can open toward (relative to its trigger). */ type Side = "down" | "up" | "right" | "left"; /** Toolbar edge → the side the **first** submenu flyout opens toward (always into the map). */ const SUBMENU_SIDE: Record = { top: "down", bottom: "up", left: "right", right: "left", }; /** * Direction for the **next** nesting level: it flips the axis so menus zig-zag (a vertical * column spawns rows, a horizontal row spawns columns). With a top bar that's the requested * `toolbar (h) → submenu (v) → sub-submenu (h) → …` — and it keeps alternating to any depth. */ const ALTERNATE: Record = { down: "right", up: "right", right: "down", left: "down" }; /** * Flex flow per open direction so `child[0]` is ALWAYS the one adjacent to the trigger, whatever * the side — and the rest stack away from it. DOM/declaration order is kept (append in array order), * only the *visual* flow flips: opening up/left reverses so the first child lands next to the trigger * instead of farthest from it. `down`/`right` keep the natural flow (no change for top/left bars). */ const FLOW: Record = { down: "column", up: "column-reverse", right: "row", left: "row-reverse" }; function ensureToolbarStyle(): void { if (typeof document === "undefined" || document.getElementById(STYLE_ID)) return; const style = document.createElement("style"); style.id = STYLE_ID; style.textContent = `.${TOOLBAR_CLASS} button svg{display:block;margin:auto}` + `.${TOOLBAR_CLASS} button{color:${CHROME_INK}}` + `.${TOOLBAR_CLASS} button:disabled{opacity:.28;filter:grayscale(1);cursor:not-allowed}` + // A mouse click leaves the button focused; drop the focus ring then (it reads as "selected"), // but KEEP it for keyboard focus (`:focus-visible`) for accessibility. The active-tool highlight // is the `.active` class, applied by `setToolbarActive` (consumer-driven), not by focus. `.${SHARED_CLASS} button:focus:not(:focus-visible){outline:none}` + // Submenu: the trigger is a plain bar button (no wrapper → native first/last-child // styling intact). The flyout is appended to and `position:fixed`, JS-placed // next to the trigger via getBoundingClientRect — so it's never clipped (Leaflet's // `overflow:hidden`) nor mis-anchored by a transformed toolbar (centred positions). `.dap-submenu{position:fixed;display:none;flex-direction:column;background:${CHROME_SURFACE};` + `border:${CHROME_BORDER};border-radius:4px;box-shadow:${CHROME_SHADOW};overflow:hidden;z-index:1000}` + `.dap-submenu.open{display:flex}` + `.dap-submenu button{position:relative;color:${CHROME_INK};background:${CHROME_SURFACE};border:0;width:${CHROME_BTN_PX}px;height:${CHROME_BTN_PX}px;cursor:pointer;padding:0}` + `.dap-submenu button svg{display:block;margin:auto}` + `.dap-submenu button:hover{background:${CHROME_HOVER}}` + `.dap-submenu button:disabled{opacity:.28;filter:grayscale(1);cursor:not-allowed}` + // A nested-submenu trigger (a submenu sitting *inside* a flyout) shows a little chevron // pointing the way its own flyout opens — set per-trigger via a `dap-sub-` class. `.dap-submenu .dap-submenu-trigger::after{content:"";position:absolute;width:0;height:0;opacity:.5}` + `.dap-submenu .dap-sub-right::after{right:2px;top:50%;transform:translateY(-50%);border:3px solid transparent;border-left-color:currentColor}` + `.dap-submenu .dap-sub-left::after{left:2px;top:50%;transform:translateY(-50%);border:3px solid transparent;border-right-color:currentColor}` + `.dap-submenu .dap-sub-down::after{bottom:2px;left:50%;transform:translateX(-50%);border:3px solid transparent;border-top-color:currentColor}` + `.dap-submenu .dap-sub-up::after{top:2px;left:50%;transform:translateX(-50%);border:3px solid transparent;border-bottom-color:currentColor}`; document.head.appendChild(style); } export function applyToolbarLayout(el: HTMLElement, opts?: ToolbarOptions): void { const pad = opts?.padding ?? "10px"; const side = (s: "top" | "right" | "bottom" | "left"): string => typeof pad === "string" ? pad : (pad[s] ?? "10px"); const pos = opts?.position ?? "top-left"; const [edge, sec] = pos.split("-"); const horizontal = edge === "top" || edge === "bottom"; el.style.position = "absolute"; el.style.zIndex = "3"; el.style.top = el.style.bottom = el.style.left = el.style.right = "auto"; el.style.transform = "none"; if (edge === "top") el.style.top = side("top"); else if (edge === "bottom") el.style.bottom = side("bottom"); else if (edge === "left") el.style.left = side("left"); else el.style.right = side("right"); if (horizontal) { if (sec === "left") el.style.left = side("left"); else if (sec === "right") el.style.right = side("right"); else (el.style.left = "50%"), (el.style.transform = "translateX(-50%)"); } else { if (sec === "top") el.style.top = side("top"); else if (sec === "bottom") el.style.bottom = side("bottom"); else (el.style.top = "50%"), (el.style.transform = "translateY(-50%)"); } el.style.display = "flex"; el.style.flexWrap = "nowrap"; el.style.flexDirection = horizontal ? "row" : "column"; // flow is defined by the edge el.style.gap = opts?.gap ?? ""; if (opts?.className) el.classList.add(...opts.className.split(/\s+/).filter(Boolean)); } /** Create a plain `