From 6987c2cf3aaf33cdf8dc138002ac5b1ad2835267 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Loi=CC=88c=20Correnson?= <loic.correnson@cea.fr> Date: Sat, 4 Jun 2022 11:23:18 +0200 Subject: [PATCH] [dome/dnd] ordering facilities --- ivette/src/dome/renderer/newdnd.tsx | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/ivette/src/dome/renderer/newdnd.tsx b/ivette/src/dome/renderer/newdnd.tsx index 9225744d11e..790f77eb3ba 100644 --- a/ivette/src/dome/renderer/newdnd.tsx +++ b/ivette/src/dome/renderer/newdnd.tsx @@ -410,14 +410,16 @@ export function DragSource(props: DragSourceProps): JSX.Element { /* --- Ordering --- */ /* -------------------------------------------------------------------------- */ +/** Swaps items at index i and j if they are both in range. */ export function swap(items: string[], i: number, j: number): string[] { - if (0 <= i && i < j) { + const n = items.length; + if (0 <= i && i < j && j < n) { const a = items[i]; return items.slice(0, i).concat( items.slice(i + 1, j + 1), a, items.slice(j + 1) ); } - if (0 <= j && j < i) { + if (0 <= j && j < i && i < n) { const a = items[j]; return items.slice(0, j).concat( items.slice(j + 1, i + 1), a, items.slice(i + 1) @@ -426,12 +428,22 @@ export function swap(items: string[], i: number, j: number): string[] { return items; } +/** Remove item at index i when in range. */ export function removeAt(items: string[], k: number): string[] { - return items.slice(0, k).concat(items.slice(k + 1)); + const n = items.length; + if (0 <= k && k < n) { + return items.slice(0, k).concat(items.slice(k + 1)); + } + return items; } +/** Insert an item at index i when in range or off-by-one. */ export function insertAt(items: string[], id: string, k: number): string[] { - return items.slice(0, k).concat(id, items.slice(k)); + const n = items.length; + if (0 <= k && k <= n) { + return items.slice(0, k).concat(id, items.slice(k)); + } + return items; } /* -------------------------------------------------------------------------- */ -- GitLab