diff --git a/doc/CHANGES.md b/doc/CHANGES.md index 2f3f2e0f7..4921608cf 100644 --- a/doc/CHANGES.md +++ b/doc/CHANGES.md @@ -112,6 +112,7 @@ Change log * fix: [#2628](https://github.com/gridstack/gridstack.js/issues/2628) `removeAll()` does not trigger Angular's ngOnDestroy * fix: [#2503](https://github.com/gridstack/gridstack.js/issues/2503) Drag and drop a widget on top of a locked widget - Thank you [JakubEleniuk](https://github.com/JakubEleniuk) * fix: [#2584](https://github.com/gridstack/gridstack.js/issues/2584) wrong sort order during 1 column resize - Thank you [JakubEleniuk](https://github.com/JakubEleniuk) again. +* fix: [#2639](https://github.com/gridstack/gridstack.js/issues/2639) load() with mix of new item without coordinates ## 10.1.1 (2024-03-03) * fix: [#2620](https://github.com/gridstack/gridstack.js/pull/2620) allow resizing with sizeToContent:NUMBER is uses diff --git a/spec/e2e/html/2639_load_missing_coord.html b/spec/e2e/html/2639_load_missing_coord.html new file mode 100644 index 000000000..f85cd6e2f --- /dev/null +++ b/spec/e2e/html/2639_load_missing_coord.html @@ -0,0 +1,74 @@ + + + + + + + #2639 load() fix + + + + +
+

#2639 load() fix with mix of missing coordinates.

+
+ + +
+

+
+ +
+ + + + diff --git a/src/gridstack.ts b/src/gridstack.ts index b69879403..a697771f2 100644 --- a/src/gridstack.ts +++ b/src/gridstack.ts @@ -699,6 +699,12 @@ export class GridStack { items = Utils.cloneDeep(items); // so we can mod const column = this.getColumn(); + // if we have a mix of new items without coordinates and existing items, separate them out so they can be added after #2639 + let addAfter = items.filter(n => (n.x === undefined || n.y === undefined) && !Utils.find(this.engine.nodes, n.id)); + if (addAfter.length && addAfter.length !== items.length) { + items = items.filter(n => !Utils.find(addAfter, n.id)); + } else addAfter = []; + // if passed list has coordinates, use them (insert from end to beginning for conflict resolution) else keep widget order const haveCoord = items.some(w => w.x !== undefined || w.y !== undefined); if (haveCoord) items = Utils.sort(items, -1); @@ -776,6 +782,11 @@ export class GridStack { } }); + // finally append any separate ones that didn't have explicit coordinates last so they can find next empty spot + if (addRemove) { + addAfter.forEach(w => this.addWidget(w)) + } + this.engine.removedNodes = removed; this.batchUpdate(false);