danielgm-ui 0.1.1 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,55 @@
1
+ /** Reconciliación de claves: las altas y el nuevo orden esperan mientras exista una salida. */
2
+ export function reconcileGridSlots(previous, desiredKeys, desiredItems, lastItems, heights) {
3
+ const desired = new Set(desiredKeys);
4
+ const retained = previous.map((slot) => {
5
+ if (desired.has(slot.key)) {
6
+ return slot.exiting ? { ...slot, exiting: false, exited: false } : slot;
7
+ }
8
+ if (slot.exiting)
9
+ return slot;
10
+ return {
11
+ ...slot,
12
+ item: lastItems.has(slot.key) ? lastItems.get(slot.key) : slot.item,
13
+ exiting: true,
14
+ exited: false,
15
+ exitHeight: heights.get(slot.key) ?? 0,
16
+ };
17
+ });
18
+ if (retained.some((slot) => slot.exiting))
19
+ return retained;
20
+ const existing = new Map(retained.map((slot) => [slot.key, slot]));
21
+ return desiredKeys.map((key) => existing.get(key) ?? {
22
+ key,
23
+ item: desiredItems.get(key),
24
+ exiting: false,
25
+ exited: false,
26
+ appear: true,
27
+ exitHeight: 0,
28
+ });
29
+ }
30
+ /**
31
+ * Las celdas de una fila comparten offsetTop aunque su contenido tenga alturas distintas.
32
+ * Agrupar esos valores evita confundir el cambio de altura de una fila con un salto de fila.
33
+ */
34
+ export function readGridPositions(nodes) {
35
+ const measured = [...nodes]
36
+ .filter(([, node]) => node.isConnected)
37
+ .map(([key, node]) => ({
38
+ key,
39
+ left: node.offsetLeft,
40
+ top: node.offsetTop,
41
+ width: node.offsetWidth,
42
+ height: node.offsetHeight,
43
+ }));
44
+ const rows = new Map();
45
+ let row = -1;
46
+ let rowTop = Number.NEGATIVE_INFINITY;
47
+ for (const { key, top } of [...measured].sort((a, b) => a.top - b.top)) {
48
+ if (row < 0 || Math.abs(rowTop - top) > 2) {
49
+ row++;
50
+ rowTop = top;
51
+ }
52
+ rows.set(key, row);
53
+ }
54
+ return new Map(measured.map(({ key, ...position }) => [key, { ...position, row: rows.get(key) }]));
55
+ }