MCPcopy
hub / github.com/react-grid-layout/react-grid-layout / moveElement

Function moveElement

src/core/layout.ts:258–349  ·  view source on GitHub ↗
(
  layout: Layout,
  l: LayoutItem,
  x: number | undefined,
  y: number | undefined,
  isUserAction: boolean | undefined,
  preventCollision: boolean | undefined,
  compactType: CompactType,
  cols: number,
  allowOverlap?: boolean
)

Source from the content-addressed store, hash-verified

256 * @returns The updated layout
257 */
258export function moveElement(
259 layout: Layout,
260 l: LayoutItem,
261 x: number | undefined,
262 y: number | undefined,
263 isUserAction: boolean | undefined,
264 preventCollision: boolean | undefined,
265 compactType: CompactType,
266 cols: number,
267 allowOverlap?: boolean
268): LayoutItem[] {
269 // Static items can't be moved unless explicitly draggable
270 if (l.static && l.isDraggable !== true) {
271 return [...layout];
272 }
273
274 // Short-circuit if position unchanged
275 if (l.y === y && l.x === x) {
276 return [...layout];
277 }
278
279 const oldX = l.x;
280 const oldY = l.y;
281
282 // Update position (mutates l directly - see JSDoc note)
283 if (typeof x === "number") (l as Mutable<LayoutItem>).x = x;
284 if (typeof y === "number") (l as Mutable<LayoutItem>).y = y;
285 (l as Mutable<LayoutItem>).moved = true;
286
287 // Sort for proper collision detection order
288 let sorted = sortLayoutItems(layout, compactType);
289 const movingUp =
290 compactType === "vertical" && typeof y === "number"
291 ? oldY >= y
292 : compactType === "horizontal" && typeof x === "number"
293 ? oldX >= x
294 : false;
295
296 if (movingUp) {
297 sorted = sorted.reverse();
298 }
299
300 const collisions = getAllCollisions(sorted, l);
301 const hasCollisions = collisions.length > 0;
302
303 // Handle overlap mode - just clone and return
304 if (hasCollisions && allowOverlap) {
305 return cloneLayout(layout);
306 }
307
308 // Handle prevent collision mode - revert position
309 // Return same reference to signal no change occurred
310 if (hasCollisions && preventCollision) {
311 (l as Mutable<LayoutItem>).x = oldX;
312 (l as Mutable<LayoutItem>).y = oldY;
313 (l as Mutable<LayoutItem>).moved = false;
314 return layout as LayoutItem[];
315 }

Callers 6

benchmark-test.jsFile · 0.90
compactAndMoveFunction · 0.90
utils-test.jsFile · 0.90
GridLayoutFunction · 0.85
useGridLayoutFunction · 0.85

Calls 4

sortLayoutItemsFunction · 0.85
getAllCollisionsFunction · 0.85
cloneLayoutFunction · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…