| 1 | // fixtures/box-spot.mjs — where the menu on a box goes (box-menu.mjs): |
| 2 | // the round button over the selected box, clear of its resize handles |
| 3 | // and inside the stage, and the menu under its anchor, inside the |
| 4 | // window. Pure, over viewport rects {left, top, right, bottom}. |
| 5 | |
| 6 | // How far the menu keeps from the window's edges, in CSS px; how near |
| 7 | // its box the round button is kept where it can be (a preference, not |
| 8 | // a limit: buttonSpot). |
| 9 | const EDGE = 8 |
| 10 | const REACH = 48 |
| 11 | |
| 12 | export const overlaps = (a, b) => a.left < b.right && b.left < a.right && a.top < b.bottom && b.top < a.bottom |
| 13 | /** a cut to b, where b has a box; a, where it has none (a frame drawn as display: contents). */ |
| 14 | 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) }) |
| 15 | const within = (a, b) => a.left >= b.left && a.top >= b.top && a.right <= b.right && a.bottom <= b.bottom |
| 16 | const clamp = (v, lo, hi) => Math.max(lo, Math.min(v, hi)) |
| 17 | |
| 18 | /** |
| 19 | * Where the round button goes ({left, top}, viewport px) for a box's |
| 20 | * rect, or null where nowhere will do. Every answer passes one test, |
| 21 | * fits(): inside bound and over none of the handles (each a 24px square |
| 22 | * centred on a corner or an edge's middle, reach being half of that). |
| 23 | * The other boxes' rects (avoid) are a preference, not a limit: a place |
| 24 | * clear of them wins wherever there is one. First the places that read |
| 25 | * as the box's own: above the top end corner, beside it past the end |
| 26 | * edge, under the bottom end corner, beside the top start corner, inside |
| 27 | * the top end corner below its handle. Then every place in bound, clear |
| 28 | * ones first, nearest the top end corner first, those within REACH px |
| 29 | * of the box before those further off. The search is complete: |
| 30 | * a rect that fits can always be slid up and towards the start until a |
| 31 | * bound edge or a handle stops it, so the bound's edges and the handles' |
| 32 | * far edges, with a 2px grid between them, find one if there is one. |
| 33 | * null only when none fits (box-menu.mjs then docks the button). |
| 34 | */ |
| 35 | export function buttonSpot ({ box, handles = [], avoid = [], bound = null, size, gap = 4, reach = 12, rtl = false }) { |
| 36 | const out = reach + gap |
| 37 | const flush = rtl ? box.left : box.right - size |
| 38 | const tries = [ |
| 39 | { left: flush, top: box.top - out - size }, |
| 40 | { left: rtl ? box.left - out - size : box.right + out, top: box.top }, |
| 41 | { left: flush, top: box.bottom + out }, |
| 42 | { left: rtl ? box.right + out : box.left - out - size, top: box.top }, |
| 43 | { left: rtl ? box.left + out : box.right - out - size, top: box.top + out } |
| 44 | ] |
| 45 | const rectOf = p => ({ left: p.left, top: p.top, right: p.left + size, bottom: p.top + size }) |
| 46 | const fits = p => { const r = rectOf(p); return (!bound || within(r, bound)) && !handles.some(h => overlaps(r, h)) } |
| 47 | const clear = p => !avoid.some(a => overlaps(rectOf(p), a)) |
| 48 | const found = tries.find(p => fits(p) && clear(p)) |
| 49 | if (found) return found |
| 50 | if (!bound) return tries.find(fits) || null |
| 51 | |
| 52 | // Everywhere in bound: the grid, the bound's far sides, and each |
| 53 | // handle's far edges (where a sliding rect stops). |
| 54 | const axis = (lo, hi, edges) => { |
| 55 | const v = new Set([lo, hi - size, ...edges]) |
| 56 | for (let x = lo; x <= hi - size; x += 2) v.add(x) |
| 57 | return [...v].filter(x => x >= lo && x + size <= hi) |
| 58 | } |
| 59 | const xs = axis(bound.left, bound.right, handles.flatMap(h => [h.right, h.left - size])) |
| 60 | const ys = axis(bound.top, bound.bottom, handles.flatMap(h => [h.bottom, h.top - size])) |
| 61 | const cornerX = rtl ? box.left : box.right |
| 62 | 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)) |
| 63 | 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)) |
| 64 | let best = null |
| 65 | let score = null |
| 66 | for (const top of ys) { |
| 67 | for (const left of xs) { |
| 68 | const p = { left, top } |
| 69 | if (!fits(p)) continue |
| 70 | const r = rectOf(p) |
| 71 | const s = [clear(p) ? 0 : 1, gapTo(r) > REACH ? 1 : 0, toCorner(r)] |
| 72 | const i = score ? s.findIndex((v, k) => v !== score[k]) : 0 |
| 73 | if (!score || (i >= 0 && s[i] < score[i])) { best = p; score = s } |
| 74 | } |
| 75 | } |
| 76 | return best |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * Where the menu goes ({left, top, maxHeight}, viewport px): under its |
| 81 | * anchor's start, its end on the anchor's end where the window has no |
| 82 | * room past it, above where there is none below, and always inside the |
| 83 | * window, edge px short of it, scrolling inside itself past that. |
| 84 | */ |
| 85 | export function menuSpot ({ anchor, width, height, vw, vh, edge = EDGE, gap = 4 }) { |
| 86 | let left = anchor.left |
| 87 | if (left + width > vw - edge) left = anchor.right - width |
| 88 | left = clamp(left, edge, vw - edge - width) |
| 89 | if (vw - 2 * edge < width) left = edge |
| 90 | let top = anchor.bottom + gap |
| 91 | if (top + height > vh - edge) { |
| 92 | const above = anchor.top - gap - height |
| 93 | top = above >= edge ? above : Math.max(edge, vh - edge - height) |
| 94 | } |
| 95 | return { left, top, maxHeight: Math.max(0, vh - edge - top) } |
| 96 | } |
| 97 | |