Don't change tile size when dragging

This commit is contained in:
Robin Townsend 2023-06-17 17:28:54 -04:00
parent 8b8d6fd0e0
commit 4f582c6ad7
4 changed files with 131 additions and 20 deletions

View File

@ -18,7 +18,7 @@ limitations under the License.
contain: strict; contain: strict;
position: relative; position: relative;
flex-grow: 1; flex-grow: 1;
padding: 0 20px; padding: 0 20px var(--footerHeight);
overflow-y: auto; overflow-y: auto;
overflow-x: hidden; overflow-x: hidden;
} }
@ -28,7 +28,6 @@ limitations under the License.
display: grid; display: grid;
grid-auto-rows: 163px; grid-auto-rows: 163px;
gap: 8px; gap: 8px;
padding-bottom: var(--footerHeight);
} }
.slot { .slot {
@ -37,7 +36,7 @@ limitations under the License.
@media (min-width: 800px) { @media (min-width: 800px) {
.grid { .grid {
padding: 0 22px; padding: 0 22px var(--footerHeight);
} }
.slotGrid { .slotGrid {

View File

@ -44,6 +44,7 @@ import {
forEachCellInArea, forEachCellInArea,
cycleTileSize, cycleTileSize,
appendItems, appendItems,
tryMoveTile,
} from "./model"; } from "./model";
import { TileWrapper } from "./TileWrapper"; import { TileWrapper } from "./TileWrapper";
@ -280,6 +281,8 @@ export const NewVideoGrid: FC<Props> = ({
const animateDraggedTile = (endOfGesture: boolean) => { const animateDraggedTile = (endOfGesture: boolean) => {
const { tileId, tileX, tileY, cursorX, cursorY } = dragState.current!; const { tileId, tileX, tileY, cursorX, cursorY } = dragState.current!;
const tile = tiles.find((t) => t.item.id === tileId)!; const tile = tiles.find((t) => t.item.id === tileId)!;
const originIndex = grid!.cells.findIndex((c) => c?.item.id === tileId);
const originCell = grid!.cells[originIndex]!;
springRef.current springRef.current
.find((c) => (c.item as Tile).item.id === tileId) .find((c) => (c.item as Tile).item.id === tileId)
@ -310,23 +313,36 @@ export const NewVideoGrid: FC<Props> = ({
} }
); );
const overTile = tiles.find( const columns = grid!.columns;
(t) => const rows = row(grid!.cells.length - 1, grid!) + 1;
cursorX >= t.x &&
cursorX < t.x + t.width && const cursorColumn = Math.floor(
cursorY >= t.y && (cursorX / slotGrid!.clientWidth) * columns
cursorY < t.y + t.height
); );
if (overTile !== undefined && overTile.item.id !== tileId) { const cursorRow = Math.floor((cursorY / slotGrid!.clientHeight) * rows);
setGrid((g) => ({
...g!, const cursorColumnOnTile = Math.floor(
cells: g!.cells.map((c) => { ((cursorX - tileX) / tile.width) * originCell.columns
if (c?.item === overTile.item) return { ...c, item: tile.item }; );
if (c?.item === tile.item) return { ...c, item: overTile.item }; const cursorRowOnTile = Math.floor(
return c; ((cursorY - tileY) / tile.height) * originCell.rows
}), );
}));
} const dest =
Math.max(
0,
Math.min(
columns - originCell.columns,
cursorColumn - cursorColumnOnTile
)
) +
grid!.columns *
Math.max(
0,
Math.min(rows - originCell.rows, cursorRow - cursorRowOnTile)
);
if (dest !== originIndex) setGrid((g) => tryMoveTile(g, originIndex, dest));
}; };
// Callback for useDrag. We could call useDrag here, but the default // Callback for useDrag. We could call useDrag here, but the default

View File

@ -175,6 +175,8 @@ const areaEnd = (
g: Grid g: Grid
): number => start + columns - 1 + g.columns * (rows - 1); ): number => start + columns - 1 + g.columns * (rows - 1);
const cloneGrid = (g: Grid): Grid => ({ ...g, cells: [...g.cells] });
/** /**
* Gets the index of the next gap in the grid that should be backfilled by 1×1 * Gets the index of the next gap in the grid that should be backfilled by 1×1
* tiles. * tiles.
@ -257,6 +259,39 @@ function moveTile(g: Grid, from: number, to: number) {
); );
} }
/**
* Moves the tile at index "from" over to index "to", if there is space.
*/
export function tryMoveTile(g: Grid, from: number, to: number): Grid {
const tile = g.cells[from]!;
if (
to > 0 &&
to < g.cells.length &&
column(to, g) <= g.columns - tile.columns
) {
const fromEnd = areaEnd(from, tile.columns, tile.rows, g);
const toEnd = areaEnd(to, tile.columns, tile.rows, g);
// The contents of a given cell are 'displaceable' if it's empty, holds a
// 1×1 tile, or is part of the original tile we're trying to reposition
const displaceable = (c: Cell | undefined, i: number): boolean =>
c === undefined ||
(c.columns === 1 && c.rows === 1) ||
inArea(i, from, fromEnd, g);
if (allCellsInArea(to, toEnd, g, displaceable)) {
// The target space is free; move
const gClone = cloneGrid(g);
moveTile(gClone, from, to);
return gClone;
}
}
// The target space isn't free; don't move
return g;
}
/** /**
* Attempts to push a tile upwards by one row, displacing 1×1 tiles and shifting * Attempts to push a tile upwards by one row, displacing 1×1 tiles and shifting
* enlarged tiles around when necessary. * enlarged tiles around when necessary.
@ -291,7 +326,7 @@ function pushTileUp(g: Grid, from: number): boolean {
* Backfill any gaps in the grid. * Backfill any gaps in the grid.
*/ */
export function fillGaps(g: Grid): Grid { export function fillGaps(g: Grid): Grid {
const result: Grid = { ...g, cells: [...g.cells] }; const result = cloneGrid(g);
// This will hopefully be the size of the grid after we're done here, assuming // This will hopefully be the size of the grid after we're done here, assuming
// that we can pack the large tiles tightly enough // that we can pack the large tiles tightly enough

View File

@ -22,6 +22,7 @@ import {
forEachCellInArea, forEachCellInArea,
Grid, Grid,
row, row,
tryMoveTile,
} from "../../src/video-grid/model"; } from "../../src/video-grid/model";
import { TileDescriptor } from "../../src/video-grid/TileDescriptor"; import { TileDescriptor } from "../../src/video-grid/TileDescriptor";
@ -325,3 +326,63 @@ def`;
); );
expect(showGrid(appendItems(newItems, mkGrid(grid1)))).toBe(grid2); expect(showGrid(appendItems(newItems, mkGrid(grid1)))).toBe(grid2);
}); });
function testTryMoveTile(
title: string,
from: number,
to: number,
input: string,
output: string
): void {
test(`tryMoveTile ${title}`, () => {
expect(showGrid(tryMoveTile(mkGrid(input), from, to))).toBe(output);
});
}
testTryMoveTile(
"refuses to move a tile too far to the left",
1,
-1,
`
abc`,
`
abc`
);
testTryMoveTile(
"refuses to move a tile too far to the right",
1,
3,
`
abc`,
`
abc`
);
testTryMoveTile(
"moves a large tile to an unoccupied space",
3,
1,
`
a b
ccd
cce`,
`
acc
bcc
d e`
);
testTryMoveTile(
"refuses to move a large tile to an occupied space",
3,
1,
`
abb
ccd
cce`,
`
abb
ccd
cce`
);