@dbugger/drag-grouped-list 0.1.1

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.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024 Invoice Master
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,28 @@
1
+ # @dbugger/drag-grouped-list
2
+
3
+ Headless helpers for grouped list reordering with drag-and-drop. This package only handles data transforms and expects the host app to render UI and own persistence.
4
+
5
+ Usage example with `@hello-pangea/dnd` result shapes:
6
+
7
+ ```js
8
+ import { applyItemDrag } from "@dbugger/drag-grouped-list";
9
+
10
+ const onDragEnd = ({ source, destination }) => {
11
+ const result = applyItemDrag({
12
+ itemsByColumn,
13
+ source,
14
+ destination,
15
+ });
16
+
17
+ if (!result.movedItem) return;
18
+
19
+ setItemsByColumn(result.itemsByColumn);
20
+ onMoveTask({
21
+ taskId: result.movedItem.id,
22
+ columnId: result.toColumnId,
23
+ position: result.toIndex,
24
+ });
25
+ };
26
+ ```
27
+
28
+ The package is ESM only and declares `@hello-pangea/dnd` as a peer dependency.
package/dist/index.js ADDED
@@ -0,0 +1,55 @@
1
+ const s = (e, n, r) => {
2
+ const o = Array.from(e), [d] = o.splice(n, 1);
3
+ return o.splice(r, 0, d), o;
4
+ }, c = (e, n, r, o) => {
5
+ const d = Array.from(e), t = Array.from(n), [l] = d.splice(r, 1);
6
+ return l === void 0 ? { sourceItems: e, destinationItems: n } : (t.splice(o, 0, l), { sourceItems: d, destinationItems: t, movedItem: l });
7
+ }, f = ({ itemsByColumn: e, source: n, destination: r }) => {
8
+ if (!r)
9
+ return {
10
+ itemsByColumn: e,
11
+ movedItem: null,
12
+ fromColumnId: null,
13
+ toColumnId: null,
14
+ fromIndex: null,
15
+ toIndex: null
16
+ };
17
+ const o = n.droppableId, d = r.droppableId, t = e[o] || [], l = e[d] || [], x = t[n.index];
18
+ if (!x)
19
+ return {
20
+ itemsByColumn: e,
21
+ movedItem: null,
22
+ fromColumnId: o,
23
+ toColumnId: d,
24
+ fromIndex: n.index,
25
+ toIndex: r.index
26
+ };
27
+ let I = e;
28
+ if (o === d) {
29
+ if (n.index !== r.index) {
30
+ const m = s(t, n.index, r.index);
31
+ I = { ...e, [o]: m };
32
+ }
33
+ } else {
34
+ const m = c(t, l, n.index, r.index);
35
+ I = {
36
+ ...e,
37
+ [o]: m.sourceItems,
38
+ [d]: m.destinationItems
39
+ };
40
+ }
41
+ return {
42
+ itemsByColumn: I,
43
+ movedItem: x,
44
+ fromColumnId: o,
45
+ toColumnId: d,
46
+ fromIndex: n.index,
47
+ toIndex: r.index
48
+ };
49
+ };
50
+ export {
51
+ f as applyItemDrag,
52
+ c as moveItemBetweenLists,
53
+ s as reorderList
54
+ };
55
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sources":["../src/reorder.js"],"sourcesContent":["export const reorderList = (list, startIndex, endIndex) => {\n\tconst result = Array.from(list);\n\tconst [removed] = result.splice(startIndex, 1);\n\tresult.splice(endIndex, 0, removed);\n\treturn result;\n};\n\nexport const moveItemBetweenLists = (sourceItems, destinationItems, sourceIndex, destinationIndex) => {\n\tconst sourceClone = Array.from(sourceItems);\n\tconst destClone = Array.from(destinationItems);\n\tconst [moved] = sourceClone.splice(sourceIndex, 1);\n\tif (moved === undefined) {\n\t\treturn { sourceItems, destinationItems };\n\t}\n\tdestClone.splice(destinationIndex, 0, moved);\n\treturn { sourceItems: sourceClone, destinationItems: destClone, movedItem: moved };\n};\n\nexport const applyItemDrag = ({ itemsByColumn, source, destination }) => {\n\tif (!destination) {\n\t\treturn {\n\t\t\titemsByColumn,\n\t\t\tmovedItem: null,\n\t\t\tfromColumnId: null,\n\t\t\ttoColumnId: null,\n\t\t\tfromIndex: null,\n\t\t\ttoIndex: null,\n\t\t};\n\t}\n\n\tconst fromColumnId = source.droppableId;\n\tconst toColumnId = destination.droppableId;\n\tconst fromItems = itemsByColumn[fromColumnId] || [];\n\tconst toItems = itemsByColumn[toColumnId] || [];\n\tconst movedItem = fromItems[source.index];\n\n\tif (!movedItem) {\n\t\treturn {\n\t\t\titemsByColumn,\n\t\t\tmovedItem: null,\n\t\t\tfromColumnId,\n\t\t\ttoColumnId,\n\t\t\tfromIndex: source.index,\n\t\t\ttoIndex: destination.index,\n\t\t};\n\t}\n\n\tlet nextItemsByColumn = itemsByColumn;\n\n\tif (fromColumnId === toColumnId) {\n\t\tif (source.index !== destination.index) {\n\t\t\tconst reordered = reorderList(fromItems, source.index, destination.index);\n\t\t\tnextItemsByColumn = { ...itemsByColumn, [fromColumnId]: reordered };\n\t\t}\n\t} else {\n\t\tconst moved = moveItemBetweenLists(fromItems, toItems, source.index, destination.index);\n\t\tnextItemsByColumn = {\n\t\t\t...itemsByColumn,\n\t\t\t[fromColumnId]: moved.sourceItems,\n\t\t\t[toColumnId]: moved.destinationItems,\n\t\t};\n\t}\n\n\treturn {\n\t\titemsByColumn: nextItemsByColumn,\n\t\tmovedItem,\n\t\tfromColumnId,\n\t\ttoColumnId,\n\t\tfromIndex: source.index,\n\t\ttoIndex: destination.index,\n\t};\n};\n"],"names":["reorderList","list","startIndex","endIndex","result","removed","moveItemBetweenLists","sourceItems","destinationItems","sourceIndex","destinationIndex","sourceClone","destClone","moved","applyItemDrag","itemsByColumn","source","destination","fromColumnId","toColumnId","fromItems","toItems","movedItem","nextItemsByColumn","reordered"],"mappings":"AAAY,MAACA,IAAc,CAACC,GAAMC,GAAYC,MAAa;AAC1D,QAAMC,IAAS,MAAM,KAAKH,CAAI,GACxB,CAACI,CAAO,IAAID,EAAO,OAAOF,GAAY,CAAC;AAC7C,SAAAE,EAAO,OAAOD,GAAU,GAAGE,CAAO,GAC3BD;AACR,GAEaE,IAAuB,CAACC,GAAaC,GAAkBC,GAAaC,MAAqB;AACrG,QAAMC,IAAc,MAAM,KAAKJ,CAAW,GACpCK,IAAY,MAAM,KAAKJ,CAAgB,GACvC,CAACK,CAAK,IAAIF,EAAY,OAAOF,GAAa,CAAC;AACjD,SAAII,MAAU,SACN,EAAE,aAAAN,GAAa,kBAAAC,EAAgB,KAEvCI,EAAU,OAAOF,GAAkB,GAAGG,CAAK,GACpC,EAAE,aAAaF,GAAa,kBAAkBC,GAAW,WAAWC,EAAK;AACjF,GAEaC,IAAgB,CAAC,EAAE,eAAAC,GAAe,QAAAC,GAAQ,aAAAC,EAAW,MAAO;AACxE,MAAI,CAACA;AACJ,WAAO;AAAA,MACN,eAAAF;AAAA,MACA,WAAW;AAAA,MACX,cAAc;AAAA,MACd,YAAY;AAAA,MACZ,WAAW;AAAA,MACX,SAAS;AAAA,IACZ;AAGC,QAAMG,IAAeF,EAAO,aACtBG,IAAaF,EAAY,aACzBG,IAAYL,EAAcG,CAAY,KAAK,CAAA,GAC3CG,IAAUN,EAAcI,CAAU,KAAK,CAAA,GACvCG,IAAYF,EAAUJ,EAAO,KAAK;AAExC,MAAI,CAACM;AACJ,WAAO;AAAA,MACN,eAAAP;AAAA,MACA,WAAW;AAAA,MACX,cAAAG;AAAA,MACA,YAAAC;AAAA,MACA,WAAWH,EAAO;AAAA,MAClB,SAASC,EAAY;AAAA,IACxB;AAGC,MAAIM,IAAoBR;AAExB,MAAIG,MAAiBC;AACpB,QAAIH,EAAO,UAAUC,EAAY,OAAO;AACvC,YAAMO,IAAYxB,EAAYoB,GAAWJ,EAAO,OAAOC,EAAY,KAAK;AACxE,MAAAM,IAAoB,EAAE,GAAGR,GAAe,CAACG,CAAY,GAAGM,EAAS;AAAA,IAClE;AAAA,SACM;AACN,UAAMX,IAAQP,EAAqBc,GAAWC,GAASL,EAAO,OAAOC,EAAY,KAAK;AACtF,IAAAM,IAAoB;AAAA,MACnB,GAAGR;AAAA,MACH,CAACG,CAAY,GAAGL,EAAM;AAAA,MACtB,CAACM,CAAU,GAAGN,EAAM;AAAA,IACvB;AAAA,EACC;AAEA,SAAO;AAAA,IACN,eAAeU;AAAA,IACf,WAAAD;AAAA,IACA,cAAAJ;AAAA,IACA,YAAAC;AAAA,IACA,WAAWH,EAAO;AAAA,IAClB,SAASC,EAAY;AAAA,EACvB;AACA;"}
package/package.json ADDED
@@ -0,0 +1,27 @@
1
+ {
2
+ "name": "@dbugger/drag-grouped-list",
3
+ "version": "0.1.1",
4
+ "description": "Headless helpers for grouped list reordering with drag-and-drop.",
5
+ "license": "MIT",
6
+ "type": "module",
7
+ "main": "./dist/index.js",
8
+ "exports": {
9
+ ".": "./dist/index.js"
10
+ },
11
+ "files": [
12
+ "dist",
13
+ "README.md",
14
+ "LICENSE"
15
+ ],
16
+ "peerDependencies": {
17
+ "@hello-pangea/dnd": "^18.0.1"
18
+ },
19
+ "scripts": {
20
+ "build": "vite build",
21
+ "test": "vitest run"
22
+ },
23
+ "devDependencies": {
24
+ "vite": "^5.3.4",
25
+ "vitest": "^1.5.1"
26
+ }
27
+ }