// fixtures/box-spot.mjs — where the menu on a box goes (box-menu.mjs): // the round button over the selected box, clear of its resize handles // and inside the stage, and the menu under its anchor, inside the // window. Pure, over viewport rects {left, top, right, bottom}. // How far the menu keeps from the window's edges, in CSS px; how near // its box the round button is kept where it can be (a preference, not // a limit: buttonSpot). const EDGE = 8 const REACH = 48 export const overlaps = (a, b) => a.left < b.right && b.left < a.right && a.top < b.bottom && b.top < a.bottom /** a cut to b, where b has a box; a, where it has none (a frame drawn as display: contents). */ export const clip = (a, b) => (!a || !b || !b.width ? a : { left: Math.max(a.left, b.left), top: Math.max(a.top, b.top), right: Math.min(a.right, b.right), bottom: Math.min(a.bottom, b.bottom) }) const within = (a, b) => a.left >= b.left && a.top >= b.top && a.right <= b.right && a.bottom <= b.bottom const clamp = (v, lo, hi) => Math.max(lo, Math.min(v, hi)) /** * Where the round button goes ({left, top}, viewport px) for a box's * rect, or null where nowhere will do. Every answer passes one test, * fits(): inside bound and over none of the handles (each a 24px square * centred on a corner or an edge's middle, reach being half of that). * The other boxes' rects (avoid) are a preference, not a limit: a place * clear of them wins wherever there is one. First the places that read * as the box's own: above the top end corner, beside it past the end * edge, under the bottom end corner, beside the top start corner, inside * the top end corner below its handle. Then every place in bound, clear * ones first, nearest the top end corner first, those within REACH px * of the box before those further off. The search is complete: * a rect that fits can always be slid up and towards the start until a * bound edge or a handle stops it, so the bound's edges and the handles' * far edges, with a 2px grid between them, find one if there is one. * null only when none fits (box-menu.mjs then docks the button). */ export function buttonSpot ({ box, handles = [], avoid = [], bound = null, size, gap = 4, reach = 12, rtl = false }) { const out = reach + gap const flush = rtl ? box.left : box.right - size const tries = [ { left: flush, top: box.top - out - size }, { left: rtl ? box.left - out - size : box.right + out, top: box.top }, { left: flush, top: box.bottom + out }, { left: rtl ? box.right + out : box.left - out - size, top: box.top }, { left: rtl ? box.left + out : box.right - out - size, top: box.top + out } ] const rectOf = p => ({ left: p.left, top: p.top, right: p.left + size, bottom: p.top + size }) const fits = p => { const r = rectOf(p); return (!bound || within(r, bound)) && !handles.some(h => overlaps(r, h)) } const clear = p => !avoid.some(a => overlaps(rectOf(p), a)) const found = tries.find(p => fits(p) && clear(p)) if (found) return found if (!bound) return tries.find(fits) || null // Everywhere in bound: the grid, the bound's far sides, and each // handle's far edges (where a sliding rect stops). const axis = (lo, hi, edges) => { const v = new Set([lo, hi - size, ...edges]) for (let x = lo; x <= hi - size; x += 2) v.add(x) return [...v].filter(x => x >= lo && x + size <= hi) } const xs = axis(bound.left, bound.right, handles.flatMap(h => [h.right, h.left - size])) const ys = axis(bound.top, bound.bottom, handles.flatMap(h => [h.bottom, h.top - size])) const cornerX = rtl ? box.left : box.right const gapTo = r => Math.hypot(Math.max(0, box.left - r.right, r.left - box.right), Math.max(0, box.top - r.bottom, r.top - box.bottom)) const toCorner = r => Math.hypot(Math.max(0, r.left - cornerX, cornerX - r.right), Math.max(0, r.top - box.top, box.top - r.bottom)) let best = null let score = null for (const top of ys) { for (const left of xs) { const p = { left, top } if (!fits(p)) continue const r = rectOf(p) const s = [clear(p) ? 0 : 1, gapTo(r) > REACH ? 1 : 0, toCorner(r)] const i = score ? s.findIndex((v, k) => v !== score[k]) : 0 if (!score || (i >= 0 && s[i] < score[i])) { best = p; score = s } } } return best } /** * Where the menu goes ({left, top, maxHeight}, viewport px): under its * anchor's start, its end on the anchor's end where the window has no * room past it, above where there is none below, and always inside the * window, edge px short of it, scrolling inside itself past that. */ export function menuSpot ({ anchor, width, height, vw, vh, edge = EDGE, gap = 4 }) { let left = anchor.left if (left + width > vw - edge) left = anchor.right - width left = clamp(left, edge, vw - edge - width) if (vw - 2 * edge < width) left = edge let top = anchor.bottom + gap if (top + height > vh - edge) { const above = anchor.top - gap - height top = above >= edge ? above : Math.max(edge, vh - edge - height) } return { left, top, maxHeight: Math.max(0, vh - edge - top) } }