bento-layout.ts 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /**
  2. * Layout for the bento grid, in grid cells.
  3. *
  4. * The first layout packs cards in order exactly like CSS
  5. * `grid-auto-flow: row dense` (first fit from the top-left), so the server can
  6. * render it. While a card is dragged, it claims the cell under it and every
  7. * other card re-packs tightly around it in the order they had when the drag
  8. * began, like icons and widgets on a phone's home screen. The result depends
  9. * only on where the card is held, so the grid never oscillates, and there are
  10. * no holes left behind.
  11. */
  12. export type Size = readonly [w: number, h: number];
  13. export type Cell = { id: string; x: number; y: number; w: number; h: number };
  14. /** Layout tiers: columns, and the viewport width where each starts. */
  15. export const TIERS = [
  16. { cols: 2, min: 0 },
  17. { cols: 4, min: 640 },
  18. { cols: 4, min: 1024 },
  19. ] as const;
  20. export const tierFor = (width: number) =>
  21. width >= TIERS[2].min ? 2 : width >= TIERS[1].min ? 1 : 0;
  22. /** First fit from the top-left, in order (what CSS `row dense` does). */
  23. export function pack(
  24. ids: readonly string[],
  25. sizeOf: (id: string) => Size,
  26. cols: number,
  27. ): Cell[] {
  28. const taken: boolean[][] = [];
  29. const fits = (x: number, y: number, w: number, h: number) => {
  30. for (let r = y; r < y + h; r++) {
  31. for (let c = x; c < x + w; c++) if (taken[r]?.[c]) return false;
  32. }
  33. return true;
  34. };
  35. return ids.map((id) => {
  36. const [sw, h] = sizeOf(id);
  37. const w = Math.min(sw, cols);
  38. for (let y = 0; ; y++) {
  39. for (let x = 0; x + w <= cols; x++) {
  40. if (!fits(x, y, w, h)) continue;
  41. for (let r = y; r < y + h; r++) {
  42. taken[r] ??= [];
  43. for (let c = x; c < x + w; c++) taken[r][c] = true;
  44. }
  45. return { id, x, y, w, h };
  46. }
  47. }
  48. });
  49. }
  50. /**
  51. * Hold card `id` at (x, y) and pack every other card around it, first fit in
  52. * `order`. If nothing is above the held card, it floats up to meet the rest.
  53. */
  54. export function placeAt(
  55. order: readonly string[],
  56. sizeOf: (id: string) => Size,
  57. cols: number,
  58. id: string,
  59. x: number,
  60. y: number,
  61. ): Cell[] {
  62. const [sw, h] = sizeOf(id);
  63. const w = Math.min(sw, cols);
  64. const taken: boolean[][] = [];
  65. const mark = (c: Cell, value: boolean) => {
  66. for (let r = c.y; r < c.y + c.h; r++) {
  67. taken[r] ??= [];
  68. for (let col = c.x; col < c.x + c.w; col++) taken[r][col] = value;
  69. }
  70. };
  71. const fits = (cx: number, cy: number, cw: number, ch: number) => {
  72. for (let r = cy; r < cy + ch; r++) {
  73. for (let col = cx; col < cx + cw; col++)
  74. if (taken[r]?.[col]) return false;
  75. }
  76. return true;
  77. };
  78. const held: Cell = { id, x: Math.min(Math.max(x, 0), cols - w), y, w, h };
  79. mark(held, true);
  80. const others = order
  81. .filter((o) => o !== id)
  82. .map((o) => {
  83. const [ow0, oh] = sizeOf(o);
  84. const ow = Math.min(ow0, cols);
  85. for (let cy = 0; ; cy++) {
  86. for (let cx = 0; cx + ow <= cols; cx++) {
  87. if (!fits(cx, cy, ow, oh)) continue;
  88. const cell = { id: o, x: cx, y: cy, w: ow, h: oh };
  89. mark(cell, true);
  90. return cell;
  91. }
  92. }
  93. });
  94. // Don't leave the held card hanging below empty space.
  95. mark(held, false);
  96. while (held.y > 0 && fits(held.x, held.y - 1, w, 1)) held.y--;
  97. return [held, ...others];
  98. }
  99. export const sameLayout = (a: Cell[], b: Cell[]) =>
  100. a.length === b.length &&
  101. a.every((c) => {
  102. const d = b.find((x) => x.id === c.id);
  103. return d && d.x === c.x && d.y === c.y;
  104. });
  105. /** Reading order of a layout: top to bottom, left to right. */
  106. export const readingOrder = (cells: Cell[]) =>
  107. [...cells].sort((a, b) => a.y - b.y || a.x - b.x).map((c) => c.id);