dragselect 2.4.4 → 2.5.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.
- package/CHANGELOG.md +6 -0
- package/README.md +84 -21
- package/dist/DragSelect.d.ts +26 -4
- package/dist/DragSelect.es6m.js +835 -781
- package/dist/DragSelect.js +835 -781
- package/dist/ds.es6m.min.js +1 -1
- package/dist/ds.min.js +1 -1
- package/dist/methods/addModificationObservers.d.ts +9 -1
- package/dist/methods/getAllParentNodes.d.ts +0 -43
- package/dist/methods/index.d.ts +1 -0
- package/dist/methods/isCollision.d.ts +1 -1
- package/dist/methods/subscriberAliases.d.ts +3 -1
- package/dist/modules/Area.d.ts +2 -12
- package/dist/modules/DropZone.d.ts +0 -43
- package/dist/modules/DropZones.d.ts +0 -43
- package/dist/modules/SelectableSet.d.ts +11 -0
- package/dist/modules/Selection.d.ts +1 -0
- package/dist/modules/index.d.ts +1 -0
- package/dist/types.d.ts +72 -2
- package/jest-puppeteer.config.js +1 -0
- package/package.json +5 -4
- package/src/DragSelect.js +29 -5
- package/src/methods/addModificationObservers.js +26 -5
- package/src/methods/canScroll.js +1 -1
- package/src/methods/getAllParentNodes.js +18 -0
- package/src/methods/handleElementPositionAttribute.js +1 -1
- package/src/methods/hydrateSettings.js +16 -0
- package/src/methods/index.js +1 -0
- package/src/methods/isCollision.js +24 -5
- package/src/methods/scrollElement.js +1 -1
- package/src/methods/subscriberAliases.js +16 -5
- package/src/methods/toArray.js +3 -2
- package/src/modules/Area.js +12 -44
- package/src/modules/DropZone.js +224 -0
- package/src/modules/DropZones.js +153 -0
- package/src/modules/SelectableSet.js +22 -0
- package/src/modules/Selection.js +9 -11
- package/src/modules/index.js +1 -0
- package/src/stores/SettingsStore.js +0 -1
- package/src/types.js +30 -2
package/src/modules/Area.js
CHANGED
|
@@ -6,9 +6,9 @@ import {
|
|
|
6
6
|
addModificationObservers,
|
|
7
7
|
debounce,
|
|
8
8
|
getAreaRect,
|
|
9
|
-
removeModificationObservers,
|
|
10
9
|
scrollElement,
|
|
11
10
|
handleElementPositionAttribute,
|
|
11
|
+
getAllParentNodes,
|
|
12
12
|
} from '../methods'
|
|
13
13
|
|
|
14
14
|
export default class Area {
|
|
@@ -18,20 +18,10 @@ export default class Area {
|
|
|
18
18
|
*/
|
|
19
19
|
DS
|
|
20
20
|
/**
|
|
21
|
-
* @type {
|
|
21
|
+
* @type {{cleanup:() => void}}
|
|
22
22
|
* @private
|
|
23
23
|
*/
|
|
24
|
-
|
|
25
|
-
/**
|
|
26
|
-
* @type {MutationObserver}
|
|
27
|
-
* @private
|
|
28
|
-
*/
|
|
29
|
-
_modificationObserver
|
|
30
|
-
/**
|
|
31
|
-
* @type {number}
|
|
32
|
-
* @private
|
|
33
|
-
*/
|
|
34
|
-
_zoom
|
|
24
|
+
_observers
|
|
35
25
|
/**
|
|
36
26
|
* @type {DSArea}
|
|
37
27
|
* @private
|
|
@@ -72,15 +62,6 @@ export default class Area {
|
|
|
72
62
|
this.setArea(settings.area)
|
|
73
63
|
)
|
|
74
64
|
|
|
75
|
-
this._modificationCallback = debounce((event) => {
|
|
76
|
-
this.DS.PubSub.publish('Area:modified:pre', { event, item: this })
|
|
77
|
-
this.reset()
|
|
78
|
-
this.DS.PubSub.publish('Area:modified', { event, item: this })
|
|
79
|
-
}, 60)
|
|
80
|
-
this._modificationObserver = new MutationObserver(
|
|
81
|
-
this._modificationCallback
|
|
82
|
-
)
|
|
83
|
-
|
|
84
65
|
this.DS.PubSub.subscribe('Interaction:init', this.start)
|
|
85
66
|
this.DS.PubSub.subscribe('Interaction:end', this.reset)
|
|
86
67
|
}
|
|
@@ -102,10 +83,13 @@ export default class Area {
|
|
|
102
83
|
}
|
|
103
84
|
|
|
104
85
|
start = () => {
|
|
105
|
-
addModificationObservers(
|
|
86
|
+
this._observers = addModificationObservers(
|
|
106
87
|
this.parentNodes,
|
|
107
|
-
|
|
108
|
-
|
|
88
|
+
debounce((event) => {
|
|
89
|
+
this.DS.PubSub.publish('Area:modified:pre', { event, item: this })
|
|
90
|
+
this.reset()
|
|
91
|
+
this.DS.PubSub.publish('Area:modified', { event, item: this })
|
|
92
|
+
}, 60)
|
|
109
93
|
)
|
|
110
94
|
}
|
|
111
95
|
|
|
@@ -117,10 +101,7 @@ export default class Area {
|
|
|
117
101
|
}
|
|
118
102
|
|
|
119
103
|
stop = () => {
|
|
120
|
-
|
|
121
|
-
this._modificationObserver,
|
|
122
|
-
this._modificationCallback
|
|
123
|
-
)
|
|
104
|
+
this._observers.cleanup()
|
|
124
105
|
this.reset()
|
|
125
106
|
}
|
|
126
107
|
|
|
@@ -169,7 +150,7 @@ export default class Area {
|
|
|
169
150
|
*/
|
|
170
151
|
get computedStyle() {
|
|
171
152
|
if (this._computedStyle) return this._computedStyle
|
|
172
|
-
if (this.HTMLNode instanceof
|
|
153
|
+
if (this.HTMLNode instanceof Document)
|
|
173
154
|
return (this._computedStyle = window.getComputedStyle(
|
|
174
155
|
this.HTMLNode.body || this.HTMLNode.documentElement
|
|
175
156
|
))
|
|
@@ -187,19 +168,6 @@ export default class Area {
|
|
|
187
168
|
|
|
188
169
|
get parentNodes() {
|
|
189
170
|
if (this._parentNodes) return this._parentNodes
|
|
190
|
-
|
|
191
|
-
const traverse = (toWatch, index = 0) => {
|
|
192
|
-
const parent = toWatch[index]?.parentNode
|
|
193
|
-
if (parent) {
|
|
194
|
-
toWatch.push(parent)
|
|
195
|
-
index++
|
|
196
|
-
return traverse(toWatch, index)
|
|
197
|
-
} else {
|
|
198
|
-
return toWatch
|
|
199
|
-
}
|
|
200
|
-
}
|
|
201
|
-
|
|
202
|
-
this._parentNodes = traverse([this.HTMLNode])
|
|
203
|
-
return this._parentNodes
|
|
171
|
+
return (this._parentNodes = getAllParentNodes(this.HTMLNode))
|
|
204
172
|
}
|
|
205
173
|
}
|
|
@@ -0,0 +1,224 @@
|
|
|
1
|
+
// @ts-check
|
|
2
|
+
import '../types'
|
|
3
|
+
import DragSelect from '../DragSelect'
|
|
4
|
+
|
|
5
|
+
import { isCollision, toArray, debounce, addModificationObservers, getAllParentNodes } from '../methods'
|
|
6
|
+
|
|
7
|
+
export default class DropZone {
|
|
8
|
+
/**
|
|
9
|
+
* @type {string}
|
|
10
|
+
*/
|
|
11
|
+
id
|
|
12
|
+
/**
|
|
13
|
+
* @type {DSElement}
|
|
14
|
+
*/
|
|
15
|
+
element
|
|
16
|
+
/**
|
|
17
|
+
* @type {DSElements}
|
|
18
|
+
*/
|
|
19
|
+
droppables
|
|
20
|
+
/**
|
|
21
|
+
* @type {DOMRect}
|
|
22
|
+
* @private
|
|
23
|
+
*/
|
|
24
|
+
_rect
|
|
25
|
+
/**
|
|
26
|
+
* @type {{cleanup:()=>void}}
|
|
27
|
+
* @private
|
|
28
|
+
*/
|
|
29
|
+
_observers
|
|
30
|
+
/**
|
|
31
|
+
* @type {NodeJS.Timeout}
|
|
32
|
+
* @private
|
|
33
|
+
*/
|
|
34
|
+
_timeout
|
|
35
|
+
/**
|
|
36
|
+
* @type {DSElements}
|
|
37
|
+
* @private
|
|
38
|
+
*/
|
|
39
|
+
_itemsDropped = []
|
|
40
|
+
/**
|
|
41
|
+
* @type {DSElements}
|
|
42
|
+
* @private
|
|
43
|
+
*/
|
|
44
|
+
_itemsInside
|
|
45
|
+
/**
|
|
46
|
+
* @constructor Drag
|
|
47
|
+
* @param {Object} obj
|
|
48
|
+
* @param {DragSelect} obj.DS
|
|
49
|
+
* @param {string} obj.id
|
|
50
|
+
* @param {DSElement} obj.element
|
|
51
|
+
* @param {DSInputElements} obj.droppables
|
|
52
|
+
* @ignore
|
|
53
|
+
*/
|
|
54
|
+
constructor({ DS, id, element, droppables }) {
|
|
55
|
+
this.DS = DS
|
|
56
|
+
this.Settings = DS.stores.SettingsStore.s
|
|
57
|
+
|
|
58
|
+
this.id = id
|
|
59
|
+
this.element = element
|
|
60
|
+
this.droppables = toArray(droppables)
|
|
61
|
+
this.element.classList.add(`${this.Settings.dropZoneClass}`)
|
|
62
|
+
|
|
63
|
+
// @ts-ignore: @todo: update to typescript
|
|
64
|
+
this.DS.subscribe('Settings:updated:dropZoneClass', ({ settings }) => {
|
|
65
|
+
this.element.classList.remove(settings['dropZoneClass:pre'])
|
|
66
|
+
this.element.classList.add(settings['dropZoneClass'])
|
|
67
|
+
})
|
|
68
|
+
|
|
69
|
+
this._observers = addModificationObservers(
|
|
70
|
+
this.parentNodes,
|
|
71
|
+
debounce(() => this._rect = null, this.Settings.refreshMemoryRate),
|
|
72
|
+
)
|
|
73
|
+
|
|
74
|
+
this.DS.subscribe('Interaction:start', this.start)
|
|
75
|
+
this.DS.subscribe('Interaction:end', this.stop)
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* @param {'add'|'remove'} action
|
|
80
|
+
*/
|
|
81
|
+
setReadyClasses = (action) => {
|
|
82
|
+
if(this.isDestroyed) return
|
|
83
|
+
const selectedEls = this.droppables.filter((el) => this.DS.SelectedSet.has(el))
|
|
84
|
+
if (!selectedEls.length) return
|
|
85
|
+
selectedEls.forEach((item) => {
|
|
86
|
+
item.classList[action](`${this.Settings.droppableClass}`)
|
|
87
|
+
item.classList[action](`${this.Settings.droppableClass}-${this.id}`)
|
|
88
|
+
})
|
|
89
|
+
this.element.classList[action](`${this.Settings.dropZoneReadyClass}`)
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* This zone is NOT the target of a drop
|
|
94
|
+
*/
|
|
95
|
+
handleNoDrop = () => {
|
|
96
|
+
if(this.isDestroyed) return
|
|
97
|
+
// for each selected element that is not part of the target zone, remove the classes
|
|
98
|
+
this.DS.SelectedSet.forEach((item) => {
|
|
99
|
+
item.classList.remove(this.Settings.droppedTargetClass)
|
|
100
|
+
item.classList.remove(`${this.Settings.droppedTargetClass}-${this.id}`)
|
|
101
|
+
})
|
|
102
|
+
// and remove them from the zones dropped items
|
|
103
|
+
this._itemsDropped = this._itemsDropped.filter((item) => !this.DS.SelectedSet.has(item))
|
|
104
|
+
// if the zone has no dropped left, also remove the zones class
|
|
105
|
+
if(!this._itemsDropped?.length)
|
|
106
|
+
this.element.classList.remove(`${this.Settings.dropZoneTargetClass}`)
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/**
|
|
110
|
+
* This zone IS the target of a drop
|
|
111
|
+
*/
|
|
112
|
+
handleDrop = () => {
|
|
113
|
+
if(this.isDestroyed) return
|
|
114
|
+
// @ts-ignore
|
|
115
|
+
this._itemsDropped = [...new Set([...this._itemsDropped, ...this.droppables?.filter((item) => this.DS.SelectedSet.has(item))])]
|
|
116
|
+
// add the target class to the zones dropped items
|
|
117
|
+
this._itemsDropped?.forEach((item) => {
|
|
118
|
+
item.classList.add(`${this.Settings.droppedTargetClass}`)
|
|
119
|
+
item.classList.add(`${this.Settings.droppedTargetClass}-${this.id}`)
|
|
120
|
+
})
|
|
121
|
+
// if the zone has dropped, add the zones class
|
|
122
|
+
if(this._itemsDropped?.length)
|
|
123
|
+
this.element.classList.add(`${this.Settings.dropZoneTargetClass}`)
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
handleItemsInsideClasses = () => {
|
|
127
|
+
let isAnyInside = false
|
|
128
|
+
this.droppables.forEach((item) => {
|
|
129
|
+
if (this.itemsInside.includes(item)) {
|
|
130
|
+
item.classList.add(`${this.Settings.droppedInsideClass}`)
|
|
131
|
+
item.classList.add(`${this.Settings.droppedInsideClass}-${this.id}`)
|
|
132
|
+
isAnyInside = true
|
|
133
|
+
} else {
|
|
134
|
+
item.classList.remove(`${this.Settings.droppedInsideClass}`)
|
|
135
|
+
item.classList.remove(`${this.Settings.droppedInsideClass}-${this.id}`)
|
|
136
|
+
}
|
|
137
|
+
})
|
|
138
|
+
|
|
139
|
+
if (isAnyInside)
|
|
140
|
+
this.element.classList.add(`${this.Settings.dropZoneInsideClass}`)
|
|
141
|
+
else
|
|
142
|
+
this.element.classList.remove(`${this.Settings.dropZoneInsideClass}`)
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
start = ({ isDragging }) => {
|
|
146
|
+
if (!isDragging || this.isDestroyed) return
|
|
147
|
+
this.setReadyClasses('add')
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
stop = ({ isDragging }) => {
|
|
151
|
+
if (!isDragging || this.isDestroyed) return
|
|
152
|
+
this.setReadyClasses('remove')
|
|
153
|
+
this.handleItemsInsideClasses()
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
destroy() {
|
|
157
|
+
this._observers.cleanup()
|
|
158
|
+
this.element.classList.remove(`${this.Settings.dropZoneClass}`)
|
|
159
|
+
this.element.classList.remove(`${this.Settings.dropZoneTargetClass}`)
|
|
160
|
+
this.element.classList.remove(`${this.Settings.dropZoneReadyClass}`)
|
|
161
|
+
this.droppables.forEach((item) => {
|
|
162
|
+
item.classList.remove(`${this.Settings.droppedTargetClass}`)
|
|
163
|
+
item.classList.remove(`${this.Settings.droppedTargetClass}-${this.id}`)
|
|
164
|
+
item.classList.remove(`${this.Settings.droppableClass}`)
|
|
165
|
+
item.classList.remove(`${this.Settings.droppableClass}-${this.id}`)
|
|
166
|
+
})
|
|
167
|
+
this.DS.unsubscribe('Interaction:start', this.start)
|
|
168
|
+
this.DS.unsubscribe('Interaction:end', this.stop)
|
|
169
|
+
this.element = null
|
|
170
|
+
this.droppables = null
|
|
171
|
+
this.id = null
|
|
172
|
+
this._itemsDropped = null
|
|
173
|
+
this._itemsInside = null
|
|
174
|
+
this.isDestroyed = true
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
/**
|
|
178
|
+
* @returns {DSDropZone}
|
|
179
|
+
*/
|
|
180
|
+
toObject = () => ({
|
|
181
|
+
id: this.id,
|
|
182
|
+
element: this.element,
|
|
183
|
+
droppables: this.droppables,
|
|
184
|
+
itemsDropped: this.itemsDropped,
|
|
185
|
+
itemsInside: this.itemsInside,
|
|
186
|
+
})
|
|
187
|
+
|
|
188
|
+
get rect() {
|
|
189
|
+
if (this.isDestroyed) return null
|
|
190
|
+
if (this._rect) return this._rect
|
|
191
|
+
return (this._rect = this.element.getBoundingClientRect())
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
get itemsDropped() {
|
|
195
|
+
if(this.isDestroyed) return null
|
|
196
|
+
return this._itemsDropped
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
get itemsInside() {
|
|
200
|
+
if (this.isDestroyed) return null
|
|
201
|
+
if (this._itemsInside) return this._itemsInside
|
|
202
|
+
|
|
203
|
+
this._itemsInside = this.droppables.flatMap((item) => {
|
|
204
|
+
if(isCollision(
|
|
205
|
+
this.DS.SelectableSet.rects.get(item),
|
|
206
|
+
this.rect,
|
|
207
|
+
this.Settings.dropInsideThreshold
|
|
208
|
+
))
|
|
209
|
+
return [item]
|
|
210
|
+
return []
|
|
211
|
+
})
|
|
212
|
+
|
|
213
|
+
// since elements can be moved while this getter is called, we need to update the values every X seconds
|
|
214
|
+
if (this._timeout) clearTimeout(this._timeout)
|
|
215
|
+
this._timeout = setTimeout(() => this._itemsInside = null, this.Settings.refreshMemoryRate)
|
|
216
|
+
|
|
217
|
+
return this._itemsInside
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
get parentNodes() {
|
|
221
|
+
if (this._parentNodes) return this._parentNodes
|
|
222
|
+
return (this._parentNodes = getAllParentNodes(this.element))
|
|
223
|
+
}
|
|
224
|
+
}
|
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
// @ts-check
|
|
2
|
+
|
|
3
|
+
import '../types'
|
|
4
|
+
import DragSelect from '../DragSelect'
|
|
5
|
+
import DropZone from './DropZone'
|
|
6
|
+
|
|
7
|
+
import { isCollision, toArray } from '../methods'
|
|
8
|
+
|
|
9
|
+
export default class DropZones {
|
|
10
|
+
/**
|
|
11
|
+
* Get the drop zone by the zone element
|
|
12
|
+
* @type {Map<DSElement, DropZone>}
|
|
13
|
+
* @private
|
|
14
|
+
*/
|
|
15
|
+
_zoneByElement = new Map()
|
|
16
|
+
/**
|
|
17
|
+
* Get the drop zone by the zone id
|
|
18
|
+
* @type {Map<string, DropZone>}
|
|
19
|
+
* @private
|
|
20
|
+
*/
|
|
21
|
+
_zoneById = new Map()
|
|
22
|
+
/**
|
|
23
|
+
* Get the drop zones by one zone item
|
|
24
|
+
* @type {Map<DSElement,DropZone[]>}
|
|
25
|
+
* @private
|
|
26
|
+
*/
|
|
27
|
+
_zonesByDroppable = new Map()
|
|
28
|
+
/**
|
|
29
|
+
* Get the drop zones by one zone item
|
|
30
|
+
* @type {DropZone[]}
|
|
31
|
+
* @private
|
|
32
|
+
*/
|
|
33
|
+
_zones
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* @constructor Drag
|
|
37
|
+
* @param {{DS:DragSelect}} obj
|
|
38
|
+
* @ignore
|
|
39
|
+
*/
|
|
40
|
+
constructor({ DS }) {
|
|
41
|
+
this.DS = DS
|
|
42
|
+
this.Settings = DS.stores.SettingsStore.s
|
|
43
|
+
|
|
44
|
+
// @ts-ignore: @todo: update to typescript
|
|
45
|
+
this.DS.subscribe('Settings:updated:dropZones', this.setDropZones)
|
|
46
|
+
this.setDropZones({ dropZones: /** @type {DSDropZone[]} */(this.DS.stores.SettingsStore.s.dropZones) })
|
|
47
|
+
|
|
48
|
+
this.DS.subscribe('Interaction:start', this.start)
|
|
49
|
+
this.DS.subscribe('Interaction:end', this.stop)
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* @param {Object} obj
|
|
54
|
+
* @param {DSDropZone[]} [obj.dropZones]
|
|
55
|
+
*/
|
|
56
|
+
setDropZones = ({ dropZones }) => {
|
|
57
|
+
if (!dropZones) return
|
|
58
|
+
if (this._zones) this._zones.forEach((zone) => zone.destroy())
|
|
59
|
+
|
|
60
|
+
this._zones = dropZones.map((zone) => new DropZone({ DS: this.DS, ...zone }))
|
|
61
|
+
this._zones.forEach((zone) => {
|
|
62
|
+
this._zoneByElement.set(zone.element, zone)
|
|
63
|
+
this._zoneById.set(zone.id, zone)
|
|
64
|
+
zone.droppables.forEach((droppable) => {
|
|
65
|
+
const zones = this._zonesByDroppable.get(droppable)
|
|
66
|
+
if (!zones?.length) return this._zonesByDroppable.set(droppable, [zone])
|
|
67
|
+
// @ts-ignore
|
|
68
|
+
this._zonesByDroppable.set(droppable, [...new Set([...zones, zone])])
|
|
69
|
+
})
|
|
70
|
+
})
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
_handleDrop = (target) => {
|
|
74
|
+
this._zones.forEach((zone) => {
|
|
75
|
+
if (zone === target) return
|
|
76
|
+
zone.handleNoDrop()
|
|
77
|
+
})
|
|
78
|
+
if (!target) return
|
|
79
|
+
target.handleDrop()
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* @param {DSElements} elements
|
|
84
|
+
* @param {Vect2} point
|
|
85
|
+
* @returns {DropZone|undefined}
|
|
86
|
+
*/
|
|
87
|
+
_getZoneByElementsFromPoint = (elements, { x, y }) => {
|
|
88
|
+
for(let i = 0, il = elements.length; i < il; i++) {
|
|
89
|
+
const zone = this._zoneByElement.get(elements[i])
|
|
90
|
+
if (
|
|
91
|
+
zone &&
|
|
92
|
+
isCollision(
|
|
93
|
+
zone.rect,
|
|
94
|
+
{ left: x, right: x, top: y, bottom: y },
|
|
95
|
+
Math.min(this.Settings.dropTargetThreshold, 0.5)
|
|
96
|
+
)
|
|
97
|
+
) {
|
|
98
|
+
return zone
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
start = ({ isDragging }) => {
|
|
104
|
+
if (!isDragging) return
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
stop = ({ isDragging }) => {
|
|
108
|
+
if (!isDragging) return
|
|
109
|
+
const target = this.getTarget()
|
|
110
|
+
this._handleDrop(target)
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
//////////////////////////////////////////////////////////////////////////////////////
|
|
114
|
+
// Getters
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* @param {string} zoneId
|
|
118
|
+
* @returns {DSElements|void}
|
|
119
|
+
*/
|
|
120
|
+
getItemsDroppedById = (zoneId) => {
|
|
121
|
+
const zone = this._zoneById.get(zoneId)
|
|
122
|
+
if (!zone) return console.warn(`[DragSelect] No zone found (id: ${zoneId})`)
|
|
123
|
+
return zone.itemsDropped
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/**
|
|
127
|
+
* @param {string} zoneId
|
|
128
|
+
* @param {boolean} addClasses
|
|
129
|
+
* @returns {DSElements|void}
|
|
130
|
+
*/
|
|
131
|
+
getItemsInsideById = (zoneId, addClasses) => {
|
|
132
|
+
const zone = this._zoneById.get(zoneId)
|
|
133
|
+
if (!zone) return console.warn(`[DragSelect] No zone found (id: ${zoneId})`)
|
|
134
|
+
const itemsInside = zone.itemsInside
|
|
135
|
+
if(addClasses) zone.handleItemsInsideClasses()
|
|
136
|
+
return itemsInside
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
/**
|
|
140
|
+
* Returns first DropsZone under current pointer
|
|
141
|
+
* @param {Vect2} [coordinates]
|
|
142
|
+
* @returns {DropZone | undefined}
|
|
143
|
+
*/
|
|
144
|
+
getTarget = (coordinates) => {
|
|
145
|
+
if (!this._zones?.length) return
|
|
146
|
+
|
|
147
|
+
const x = coordinates?.x || this.DS.stores.PointerStore.currentVal.x
|
|
148
|
+
const y = coordinates?.y || this.DS.stores.PointerStore.currentVal.y
|
|
149
|
+
|
|
150
|
+
const elements = document.elementsFromPoint(x, y)
|
|
151
|
+
return this._getZoneByElementsFromPoint(/** @type {DSElements} */(elements), { x, y })
|
|
152
|
+
}
|
|
153
|
+
}
|
|
@@ -5,6 +5,16 @@ import DragSelect from '../DragSelect'
|
|
|
5
5
|
import { toArray, handleElementPositionAttribute } from '../methods'
|
|
6
6
|
|
|
7
7
|
export default class SelectableSet extends Set {
|
|
8
|
+
/**
|
|
9
|
+
* @type {Map<DSElement,DSBoundingRect>}
|
|
10
|
+
* @private
|
|
11
|
+
*/
|
|
12
|
+
_rects
|
|
13
|
+
/**
|
|
14
|
+
* @type {NodeJS.Timeout}
|
|
15
|
+
* @private
|
|
16
|
+
*/
|
|
17
|
+
_timeout
|
|
8
18
|
/**
|
|
9
19
|
* @constructor SelectableSet
|
|
10
20
|
* @param {{DS:DragSelect}} obj
|
|
@@ -112,4 +122,16 @@ export default class SelectableSet extends Set {
|
|
|
112
122
|
get elements() {
|
|
113
123
|
return Array.from(this.values())
|
|
114
124
|
}
|
|
125
|
+
|
|
126
|
+
get rects() {
|
|
127
|
+
if (this._rects) return this._rects
|
|
128
|
+
this._rects = new Map()
|
|
129
|
+
this.forEach((element) => this._rects.set(element, element.getBoundingClientRect()))
|
|
130
|
+
|
|
131
|
+
// since elements can be moved, we need to update the rects every X ms
|
|
132
|
+
if (this._timeout) clearTimeout(this._timeout)
|
|
133
|
+
this._timeout = setTimeout(() => this._rects = null, this.Settings.refreshMemoryRate)
|
|
134
|
+
|
|
135
|
+
return this._rects
|
|
136
|
+
}
|
|
115
137
|
}
|
package/src/modules/Selection.js
CHANGED
|
@@ -18,6 +18,7 @@ export default class Selection {
|
|
|
18
18
|
*/
|
|
19
19
|
constructor({ DS }) {
|
|
20
20
|
this.DS = DS
|
|
21
|
+
this.Settings = this.DS.stores.SettingsStore.s
|
|
21
22
|
this.DS.subscribe('Interaction:start', this.start)
|
|
22
23
|
this.DS.subscribe('Interaction:update', this.update)
|
|
23
24
|
}
|
|
@@ -60,23 +61,20 @@ export default class Selection {
|
|
|
60
61
|
const { SelectableSet, SelectorArea, Selector } = this.DS
|
|
61
62
|
|
|
62
63
|
/** @type {any} */
|
|
63
|
-
const
|
|
64
|
-
return [element, element.getBoundingClientRect()]
|
|
65
|
-
})
|
|
64
|
+
const elRects = SelectableSet.rects
|
|
66
65
|
|
|
67
66
|
const select = []
|
|
68
67
|
const unselect = []
|
|
69
68
|
|
|
70
|
-
for
|
|
71
|
-
if (!SelectorArea.isInside(
|
|
72
|
-
if (isCollision(
|
|
73
|
-
|
|
74
|
-
else unselect.push(elPosCombo[i][0])
|
|
69
|
+
for(const [element, rect] of elRects) {
|
|
70
|
+
if (!SelectorArea.isInside(element, rect)) continue
|
|
71
|
+
if (isCollision(rect, Selector.rect, this.Settings.selectionThreshold)) select.push(element)
|
|
72
|
+
else unselect.push(element)
|
|
75
73
|
}
|
|
76
74
|
|
|
77
75
|
const multiSelectionToggle =
|
|
78
76
|
(this.DS.stores.KeyStore.isMultiSelectKeyPressed(event)) &&
|
|
79
|
-
this.
|
|
77
|
+
this.Settings.multiSelectToggling
|
|
80
78
|
|
|
81
79
|
if (this.DS.continue) return
|
|
82
80
|
select.forEach((element) =>
|
|
@@ -85,7 +83,7 @@ export default class Selection {
|
|
|
85
83
|
force,
|
|
86
84
|
multiSelectionToggle,
|
|
87
85
|
SelectedSet: this.DS.SelectedSet,
|
|
88
|
-
hoverClassName: this.
|
|
86
|
+
hoverClassName: this.Settings.hoverClass,
|
|
89
87
|
})
|
|
90
88
|
)
|
|
91
89
|
unselect.forEach((element) =>
|
|
@@ -93,7 +91,7 @@ export default class Selection {
|
|
|
93
91
|
element,
|
|
94
92
|
force,
|
|
95
93
|
SelectedSet: this.DS.SelectedSet,
|
|
96
|
-
hoverClassName: this.
|
|
94
|
+
hoverClassName: this.Settings.hoverClass,
|
|
97
95
|
PrevSelectedSet: this._prevSelectedSet,
|
|
98
96
|
})
|
|
99
97
|
)
|
package/src/modules/index.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
export { default as Area } from './Area'
|
|
2
2
|
export { default as Drag } from './Drag'
|
|
3
|
+
export { default as DropZones } from './DropZones'
|
|
3
4
|
export { default as Interaction } from './Interaction'
|
|
4
5
|
export { default as PubSub } from './PubSub'
|
|
5
6
|
export { default as SelectableSet } from './SelectableSet'
|
package/src/types.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* The Settings to be passed to the Class
|
|
3
3
|
* @typedef {Object} Settings
|
|
4
|
-
* @property {HTMLElement|SVGElement|
|
|
4
|
+
* @property {HTMLElement|SVGElement|Document} [area=document] area in which you can drag. If not provided it will be the whole document
|
|
5
5
|
* @property {DSInputElements} [selectables=[]] the elements that can be selected
|
|
6
6
|
* @property {number} [autoScrollSpeed=5] Speed in which the area scrolls while selecting (if available). Unit is pixel per movement.
|
|
7
7
|
* @property {Vect2} [overflowTolerance={x:25,y:25}] Tolerance for autoScroll (how close one has to be near an edges for autoScroll to start)
|
|
@@ -11,18 +11,30 @@
|
|
|
11
11
|
* @property {boolean} [multiSelectToggling=true] Whether or not to toggle already active elements while multi-selecting
|
|
12
12
|
* @property {DSMultiSelectKeys} [multiSelectKeys=['Control', 'Shift', 'Meta']] Keys that allows switching to the multi-select mode (see the multiSelectMode option). Any key value is possible ([see MDN docs](https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/key)). Note that the best support is given for <kbd>Control</kbd>, <kbd>Shift</kbd> and <kbd>Meta</kbd>. Provide an empty array `[]` if you want to turn off the functionality.
|
|
13
13
|
* @property {HTMLElement} [selector=HTMLElement] the square that will draw the selection
|
|
14
|
+
* @property {number} [selectionThreshold=0] how much % of the element has to be selected to be considered selected (0 = just touching, 1 = inside the selection)
|
|
14
15
|
* @property {boolean} [draggability=true] When a user is dragging on an already selected element, the selection is dragged.
|
|
15
16
|
* @property {boolean} [immediateDrag=true] Whether an element is draggable from the start or needs to be selected first
|
|
16
17
|
* @property {boolean} [keyboardDrag=true] Whether or not the user can drag with the keyboard (we don't recommend disabling it)
|
|
17
18
|
* @property {DSDragKeys} [dragKeys={up:['ArrowUp'],down:['ArrowDown'],left:['ArrowLeft'],righ:['ArrowRight']}] The keys available to drag element using the keyboard.
|
|
18
19
|
* @property {number} [keyboardDragSpeed=10] The speed at which elements are dragged using the keyboard. In pixels per keydown.
|
|
19
20
|
* @property {boolean} [useTransform=true] Whether to use hardware accelerated css transforms when dragging or top/left instead
|
|
21
|
+
* @property {number} [refreshMemoryRate=80] Refresh rate on memoization, higher numbers mean better performance but more lag if elements are moving, lower numbers mean less lag but worse performance. If none of your DOMNodes are moving, you can set it to a very high number to increase performance. Value in milliseconds.
|
|
22
|
+
* @property {DSInputDropZone[]} [dropZones=[]] one or more drop-elements: where the selectables can be dropped into
|
|
23
|
+
* @property {number} [dropInsideThreshold=1] how much % of the item has to be inside the dropzone to be considered inside (0 = barely touching, 1 = completely inside)
|
|
24
|
+
* @property {number} [dropTargetThreshold=0] how much % of the zone does the pointer has to be in to be considered a target (0 = anywhere in the zone, max: 0.5 = has to point at the center of the zone)
|
|
20
25
|
* @property {boolean} [usePointerEvents=false] Whether to use Pointer Events to replace traditional Mouse or Touch Events. Useful for tools like Google Blockly.
|
|
21
26
|
* @property {string} [hoverClass=ds-hover] the class assigned to the mouse hovered items
|
|
22
27
|
* @property {string} [selectableClass=ds-selectable] the class assigned to the elements that can be selected
|
|
23
28
|
* @property {string} [selectedClass=ds-selected] the class assigned to the selected items
|
|
24
29
|
* @property {string} [selectorClass=ds-selector] the class assigned to the square selector helper
|
|
25
30
|
* @property {string} [selectorAreaClass=ds-selector-area] the class assigned to the square in which the selector resides. By default it's invisible
|
|
31
|
+
* @property {string} [droppedTargetClass=ds-dropped-target] on an item corresponding the target dropzone. This is also the prefix for ds-dropped-target-${zone.id}
|
|
32
|
+
* @property {string} [droppedInsideClass=ds-dropped-inside] on an item that is within its dropzone bounds after a drop. This is also the prefix for ds-dropped-inside-${zone.id}
|
|
33
|
+
* @property {string} [droppableClass=ds-droppable] on element that can be dropped into at least one container. This is also the prefix for ds-droppable-${zone.id}
|
|
34
|
+
* @property {string} [dropZoneClass=ds-dropzone] on each dropZone
|
|
35
|
+
* @property {string} [dropZoneReadyClass=ds-dropzone-ready] on corresponding dropZone when element is dragged
|
|
36
|
+
* @property {string} [dropZoneTargetClass=ds-dropzone-target] on dropZone that has elements from any successful target drop
|
|
37
|
+
* @property {string} [dropZoneInsideClass=ds-dropzone-inside] on dropZone that has elements inside after any drop
|
|
26
38
|
*/
|
|
27
39
|
|
|
28
40
|
/**
|
|
@@ -37,17 +49,33 @@
|
|
|
37
49
|
* @property {Settings} [settings] the settings being updates/manipulated/passed, also holds the previous value. i.e. updating selectorClass will publish { settings: { selectorClass: 'newVal', 'selectorClass:pre': 'oldVal' } }
|
|
38
50
|
* @property {Array.<'top'|'bottom'|'left'|'right'|undefined>} [scroll_directions]
|
|
39
51
|
* @property {number} [scroll_multiplier]
|
|
52
|
+
* @property {DSDropZone} [dropTarget] The dropZone element that the element was dropped into (or the mouse is currently hovering over)
|
|
40
53
|
*/
|
|
41
54
|
/**
|
|
42
55
|
* @typedef {function} DSCallback
|
|
43
56
|
* @param {CallbackObject} data
|
|
44
57
|
*/
|
|
45
58
|
|
|
59
|
+
/**
|
|
60
|
+
* @typedef {Object} DSInputDropZone
|
|
61
|
+
* @property {string} id
|
|
62
|
+
* @property {DSElement} element
|
|
63
|
+
* @property {DSInputElements} droppables
|
|
64
|
+
*/
|
|
65
|
+
/**
|
|
66
|
+
* @typedef {Object} DSDropZone
|
|
67
|
+
* @property {string} id
|
|
68
|
+
* @property {DSElement} element
|
|
69
|
+
* @property {DSElements} droppables
|
|
70
|
+
* @property {DSElements} [itemsDropped] the items related to the target zone
|
|
71
|
+
* @property {DSElements} [itemsInside] the items that are within the targets bounds
|
|
72
|
+
*/
|
|
73
|
+
|
|
46
74
|
/** @typedef {{x: number, y: number}} Vect2 */
|
|
47
75
|
/** @typedef {{x:number,y:number,w:number,h:number,r:number,b:number}} DSElementPos */
|
|
48
76
|
/** @typedef {Array.<'top'|'bottom'|'left'|'right'|undefined>} DSEdges */
|
|
49
77
|
|
|
50
|
-
/** @typedef {HTMLElement|SVGElement|
|
|
78
|
+
/** @typedef {HTMLElement|SVGElement|Document} DSArea area within which you can drag */
|
|
51
79
|
/** @typedef {HTMLElement} DSSelectorArea area in which you can drag */
|
|
52
80
|
/** @typedef {Array.<HTMLElement|SVGElement> | HTMLElement | SVGElement} DSInputElements the elements that can be selected */
|
|
53
81
|
/** @typedef {Array.<HTMLElement|SVGElement>} DSElements the elements that can be selected */
|