unsortable 0.1.5 → 0.1.7

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.
Files changed (2) hide show
  1. package/lib/index.js +34 -8
  2. package/package.json +1 -1
package/lib/index.js CHANGED
@@ -1,5 +1,6 @@
1
1
  import { DragDropManager, Draggable, Droppable } from '@dnd-kit/dom'
2
2
  import { getNearestParentElementFromMap, hasValue, move, toArrayAccessors, toItemAccessor } from './utils'
3
+ // import { closestCenter, pointerIntersection, directionBiased } from '@dnd-kit/collision'
3
4
 
4
5
  /**
5
6
  * @typedef {Object} UnsortableOptions
@@ -15,6 +16,9 @@ const defaultOptions = {
15
16
  autoAttach: true,
16
17
  }
17
18
 
19
+ let lastDroppable = null
20
+ let lastDroppableOriginalState = null
21
+
18
22
  const containerMap = new WeakMap()
19
23
  const itemMap = new WeakMap()
20
24
 
@@ -29,6 +33,8 @@ export class Unsortable {
29
33
  this.addDroppable = this.addDroppable.bind(this)
30
34
  this.addHandle = this.addHandle.bind(this)
31
35
  this._onDragOver = this._onDragOver.bind(this)
36
+ this._disableOwnDroppable = this._disableOwnDroppable.bind(this)
37
+ this._restoreLastDroppable = this._restoreLastDroppable.bind(this)
32
38
 
33
39
  if (this.options.autoAttach) this.attach()
34
40
  }
@@ -36,6 +42,25 @@ export class Unsortable {
36
42
  attach() {
37
43
  console.debug('Unsortable: attaching to drag manager')
38
44
  this.manager.monitor.addEventListener('dragover', this._onDragOver)
45
+ this.manager.monitor.addEventListener('dragover', this._disableOwnDroppable)
46
+ this.manager.monitor.addEventListener('beforedragstart', this._disableOwnDroppable)
47
+ this.manager.monitor.addEventListener('dragend', this._restoreLastDroppable)
48
+ }
49
+
50
+ _disableOwnDroppable(e) {
51
+ const droppable = e.operation.source.data.droppable
52
+ console.debug('Unsortable: disabling own droppable', droppable, lastDroppable)
53
+ if (lastDroppable && droppable !== lastDroppable) this._restoreLastDroppable()
54
+ lastDroppableOriginalState = droppable.disabled
55
+ droppable.disabled = true
56
+ lastDroppable = droppable
57
+ }
58
+
59
+ _restoreLastDroppable() {
60
+ console.debug('Unsortable: restoring last droppable', lastDroppable, lastDroppableOriginalState)
61
+ lastDroppable.disabled = lastDroppableOriginalState
62
+ lastDroppable = null
63
+ lastDroppableOriginalState = null
39
64
  }
40
65
 
41
66
  destroy() {
@@ -122,24 +147,25 @@ export class Unsortable {
122
147
  options.item = toItemAccessor(options.item)
123
148
 
124
149
  console.debug('unsortable: draggable options', options)
125
- const draggable = new Draggable(
150
+
151
+ const droppable = new Droppable(
126
152
  {
127
- type: options.type,
128
- ...options?.draggableOptions,
153
+ accept: options.accept || options.type,
154
+ ...options?.droppableOptions,
129
155
  id: options.item(),
130
156
  element,
131
- data: { ...options, ...options?.draggableOptions?.data, isContainer: false },
157
+ data: { ...options, ...options?.droppableOptions?.data, isContainer: false },
132
158
  },
133
159
  this.manager,
134
160
  )
135
161
 
136
- const droppable = new Droppable(
162
+ const draggable = new Draggable(
137
163
  {
138
- accept: options.accept || options.type,
139
- ...options?.droppableOptions,
164
+ type: options.type,
140
165
  id: options.item(),
141
166
  element,
142
- data: { ...options, ...options?.droppableOptions?.data, isContainer: false },
167
+ ...options?.draggableOptions,
168
+ data: { ...options, ...options?.draggableOptions?.data, droppable, isContainer: false },
143
169
  },
144
170
  this.manager,
145
171
  )
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "unsortable",
3
- "version": "0.1.5",
3
+ "version": "0.1.7",
4
4
  "description": "Headless drag-and-drop library for nested sorting using state instead of DOM mutations.",
5
5
  "main": "lib/index.js",
6
6
  "type": "module",