diff --git a/ivette/src/dome/renderer/data/arrays.ts b/ivette/src/dome/renderer/data/arrays.ts new file mode 100644 index 0000000000000000000000000000000000000000..74a3109da28cd05bd4472495a0ddeadb8faf8453 --- /dev/null +++ b/ivette/src/dome/renderer/data/arrays.ts @@ -0,0 +1,45 @@ +/* ************************************************************************ */ +/* */ +/* This file is part of Frama-C. */ +/* */ +/* Copyright (C) 2007-2022 */ +/* CEA (Commissariat à l'énergie atomique et aux énergies */ +/* alternatives) */ +/* */ +/* you can redistribute it and/or modify it under the terms of the GNU */ +/* Lesser General Public License as published by the Free Software */ +/* Foundation, version 2.1. */ +/* */ +/* It is distributed in the hope that it will be useful, */ +/* but WITHOUT ANY WARRANTY; without even the implied warranty of */ +/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */ +/* GNU Lesser General Public License for more details. */ +/* */ +/* See the GNU Lesser General Public License version 2.1 */ +/* for more details (enclosed in the file licenses/LGPLv2.1). */ +/* */ +/* ************************************************************************ */ + +/** + Safe ARRAY utilities. + @packageDocumentation + @module dome/data/arrays +*/ + +/** Swaps items at index i and j if they are both in range. */ +export function swap<A>(ls: A[], a: number, b: number): A[] { + const n = ls.length; + if (a === b || 0 > a || a >= n || 0 > b || b >= n) return ls; + const [i, j] = a < b ? [a, b] : [b, a]; + return ls.slice(0, i).concat(ls.slice(i + 1, j + 1), ls[i], ls.slice(j + 1)); +} + +/** Remove item at index i when in range. */ +export function removeAt<A>(ls: A[], k: number): A[] { + return 0 <= k && k < ls.length ? ls.slice(0, k).concat(ls.slice(k + 1)) : ls; +} + +/** Insert an item at index i when in range or off-by-one. */ +export function insertAt<A>(ls: A[], id: A, k: number): A[] { + return 0 <= k && k <= ls.length ? ls.slice(0, k).concat(id, ls.slice(k)) : ls; +} diff --git a/ivette/src/dome/renderer/dnd.tsx b/ivette/src/dome/renderer/dnd.tsx index 7a5522854457d062559e037f596e1d1cd3b8497e..7081cff7364657a1760601900f9e6b39a0f61d21 100644 --- a/ivette/src/dome/renderer/dnd.tsx +++ b/ivette/src/dome/renderer/dnd.tsx @@ -30,6 +30,7 @@ import React from 'react'; import { classes, styles } from 'dome/misc/utils'; +import { swap } from 'dome/data/arrays'; import { DraggableCore, DraggableEvent, @@ -412,28 +413,6 @@ export function DragSource(props: DragSourceProps): JSX.Element { ); } -/* -------------------------------------------------------------------------- */ -/* --- Ordering --- */ -/* -------------------------------------------------------------------------- */ - -/** Swaps items at index i and j if they are both in range. */ -export function swap<A>(ls: A[], a: number, b: number): A[] { - const n = ls.length; - if (a === b || 0 > a || a >= n || 0 > b || b >= n) return ls; - const [i, j] = a < b ? [a, b] : [b, a]; - return ls.slice(0, i).concat(ls.slice(i + 1, j + 1), ls[i], ls.slice(j + 1)); -} - -/** Remove item at index i when in range. */ -export function removeAt<A>(ls: A[], k: number): A[] { - return 0 <= k && k < ls.length ? ls.slice(0, k).concat(ls.slice(k + 1)) : ls; -} - -/** Insert an item at index i when in range or off-by-one. */ -export function insertAt<A>(ls: A[], id: A, k: number): A[] { - return 0 <= k && k <= ls.length ? ls.slice(0, k).concat(id, ls.slice(k)) : ls; -} - /* -------------------------------------------------------------------------- */ /* --- List Container --- */ /* -------------------------------------------------------------------------- */