bento-layout.ts 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. /**
  15. * Layout tiers: columns, and the viewport width where each starts. Cells stay
  16. * between ~230px and ~350px from small tablets up, the range the cards' larger
  17. * type is drawn for; phones get their own sizes.
  18. */
  19. export const TIERS = [
  20. { cols: 2, min: 0 },
  21. { cols: 2, min: 640 },
  22. { cols: 3, min: 768 },
  23. { cols: 4, min: 1024 },
  24. ] as const;
  25. export const tierFor = (width: number) => {
  26. let tier = 0;
  27. TIERS.forEach((t, i) => {
  28. if (width >= t.min) tier = i;
  29. });
  30. return tier;
  31. };
  32. /** First fit from the top-left, in order (what CSS `row dense` does). */
  33. export function pack(
  34. ids: readonly string[],
  35. sizeOf: (id: string) => Size,
  36. cols: number,
  37. ): Cell[] {
  38. const taken: boolean[][] = [];
  39. const fits = (x: number, y: number, w: number, h: number) => {
  40. for (let r = y; r < y + h; r++) {
  41. for (let c = x; c < x + w; c++) if (taken[r]?.[c]) return false;
  42. }
  43. return true;
  44. };
  45. return ids.map((id) => {
  46. const [sw, h] = sizeOf(id);
  47. const w = Math.min(sw, cols);
  48. for (let y = 0; ; y++) {
  49. for (let x = 0; x + w <= cols; x++) {
  50. if (!fits(x, y, w, h)) continue;
  51. for (let r = y; r < y + h; r++) {
  52. taken[r] ??= [];
  53. for (let c = x; c < x + w; c++) taken[r][c] = true;
  54. }
  55. return { id, x, y, w, h };
  56. }
  57. }
  58. });
  59. }
  60. /**
  61. * Hold card `id` at (x, y) and pack every other card around it, first fit in
  62. * `order`. If nothing is above the held card, it floats up to meet the rest.
  63. */
  64. export function placeAt(
  65. order: readonly string[],
  66. sizeOf: (id: string) => Size,
  67. cols: number,
  68. id: string,
  69. x: number,
  70. y: number,
  71. ): Cell[] {
  72. const [sw, h] = sizeOf(id);
  73. const w = Math.min(sw, cols);
  74. const taken: boolean[][] = [];
  75. const mark = (c: Cell, value: boolean) => {
  76. for (let r = c.y; r < c.y + c.h; r++) {
  77. taken[r] ??= [];
  78. for (let col = c.x; col < c.x + c.w; col++) taken[r][col] = value;
  79. }
  80. };
  81. const fits = (cx: number, cy: number, cw: number, ch: number) => {
  82. for (let r = cy; r < cy + ch; r++) {
  83. for (let col = cx; col < cx + cw; col++)
  84. if (taken[r]?.[col]) return false;
  85. }
  86. return true;
  87. };
  88. const held: Cell = { id, x: Math.min(Math.max(x, 0), cols - w), y, w, h };
  89. mark(held, true);
  90. const others = order
  91. .filter((o) => o !== id)
  92. .map((o) => {
  93. const [ow0, oh] = sizeOf(o);
  94. const ow = Math.min(ow0, cols);
  95. for (let cy = 0; ; cy++) {
  96. for (let cx = 0; cx + ow <= cols; cx++) {
  97. if (!fits(cx, cy, ow, oh)) continue;
  98. const cell = { id: o, x: cx, y: cy, w: ow, h: oh };
  99. mark(cell, true);
  100. return cell;
  101. }
  102. }
  103. });
  104. // Don't leave the held card hanging below empty space.
  105. mark(held, false);
  106. while (held.y > 0 && fits(held.x, held.y - 1, w, 1)) held.y--;
  107. return [held, ...others];
  108. }
  109. export const sameLayout = (a: Cell[], b: Cell[]) =>
  110. a.length === b.length &&
  111. a.every((c) => {
  112. const d = b.find((x) => x.id === c.id);
  113. return d && d.x === c.x && d.y === c.y;
  114. });
  115. /** Reading order of a layout: top to bottom, left to right. */
  116. export const readingOrder = (cells: Cell[]) =>
  117. [...cells].sort((a, b) => a.y - b.y || a.x - b.x).map((c) => c.id);