dragselect 3.0.2 → 3.0.3
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 +5 -1
- package/dist/{DragSelect.es6m.js → DragSelect.esm.js} +1 -1
- package/dist/DragSelect.js +1 -1
- package/package.json +2 -2
- package/src/DragSelect.ts +0 -425
- package/src/methods/addModificationObservers.ts +0 -51
- package/src/methods/canScroll.ts +0 -19
- package/src/methods/createSelectorAreaElement.ts +0 -8
- package/src/methods/createSelectorElement.ts +0 -13
- package/src/methods/debounce.ts +0 -31
- package/src/methods/ensureArray.ts +0 -13
- package/src/methods/getAllParentNodes.ts +0 -15
- package/src/methods/getAreaRect.ts +0 -27
- package/src/methods/getCurrentScroll.ts +0 -14
- package/src/methods/getDocumentScroll.ts +0 -5
- package/src/methods/getOverflowEdges.ts +0 -27
- package/src/methods/getPointerPos.ts +0 -9
- package/src/methods/getSelectionRect.ts +0 -29
- package/src/methods/getSelectorPosition.ts +0 -74
- package/src/methods/getStylePosition.spec.ts +0 -114
- package/src/methods/getStylePosition.ts +0 -83
- package/src/methods/handleElementPositionAttribute.ts +0 -15
- package/src/methods/handleKeyboardDragPosDifference.ts +0 -35
- package/src/methods/handleSelection.ts +0 -26
- package/src/methods/handleUnSelection.ts +0 -36
- package/src/methods/hydrateSettings.ts +0 -245
- package/src/methods/isCollision.ts +0 -66
- package/src/methods/limitDirection.ts +0 -36
- package/src/methods/moveElement.ts +0 -18
- package/src/methods/scrollElement.ts +0 -26
- package/src/methods/setStylePosition.ts +0 -16
- package/src/methods/subscriberAliases.ts +0 -240
- package/src/methods/updateElementStylePos.ts +0 -7
- package/src/methods/vect2.ts +0 -34
- package/src/modules/Area.ts +0 -169
- package/src/modules/Drag.ts +0 -236
- package/src/modules/DropZone.ts +0 -223
- package/src/modules/DropZones.ts +0 -151
- package/src/modules/Interaction.ts +0 -251
- package/src/modules/PubSub.ts +0 -111
- package/src/modules/SelectableSet.ts +0 -146
- package/src/modules/SelectedSet.ts +0 -92
- package/src/modules/Selection.ts +0 -133
- package/src/modules/Selector.ts +0 -81
- package/src/modules/SelectorArea.ts +0 -161
- package/src/stores/KeyStore.ts +0 -92
- package/src/stores/PointerStore.ts +0 -194
- package/src/stores/ScrollStore.ts +0 -87
- package/src/stores/SettingsStore.ts +0 -88
- package/src/types.ts +0 -158
- /package/dist/{ds.es6m.min.js → ds.esm.min.js} +0 -0
package/src/modules/Area.ts
DELETED
|
@@ -1,169 +0,0 @@
|
|
|
1
|
-
import DragSelect from '../DragSelect'
|
|
2
|
-
import PubSub from './PubSub'
|
|
3
|
-
import {
|
|
4
|
-
DSArea,
|
|
5
|
-
DSBoundingRect,
|
|
6
|
-
DSEdges,
|
|
7
|
-
DSEdgesObj,
|
|
8
|
-
DSInputElement,
|
|
9
|
-
} from '../types'
|
|
10
|
-
import { DSSettings } from '../stores/SettingsStore'
|
|
11
|
-
import {
|
|
12
|
-
MutationCallbackEvent,
|
|
13
|
-
addModificationObservers,
|
|
14
|
-
} from '../methods/addModificationObservers'
|
|
15
|
-
import { debounce } from '../methods/debounce'
|
|
16
|
-
import { getAllParentNodes } from '../methods/getAllParentNodes'
|
|
17
|
-
import { getAreaRect } from '../methods/getAreaRect'
|
|
18
|
-
import { handleElementPositionAttribute } from '../methods/handleElementPositionAttribute'
|
|
19
|
-
import { scrollElement } from '../methods/scrollElement'
|
|
20
|
-
|
|
21
|
-
export type DSAreaPublishEventNames =
|
|
22
|
-
| 'Area:modified:pre'
|
|
23
|
-
| 'Area:modified'
|
|
24
|
-
| 'Area:scroll'
|
|
25
|
-
| 'Area:scroll:pre'
|
|
26
|
-
|
|
27
|
-
export type DSAreaPublishEventData<E extends DSInputElement> = {
|
|
28
|
-
/** The single item currently interacted with */
|
|
29
|
-
item: DSArea
|
|
30
|
-
/** The respective event object */
|
|
31
|
-
event?: MutationCallbackEvent
|
|
32
|
-
scroll_directions: DSEdges
|
|
33
|
-
scroll_multiplier: number
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
export type DSAreaPublish<E extends DSInputElement> = {
|
|
37
|
-
'Area:modified:pre': Pick<DSAreaPublishEventData<E>, 'event' | 'item'>
|
|
38
|
-
'Area:modified': Pick<DSAreaPublishEventData<E>, 'event' | 'item'>
|
|
39
|
-
'Area:scroll:pre': Pick<
|
|
40
|
-
DSAreaPublishEventData<E>,
|
|
41
|
-
'scroll_directions' | 'scroll_multiplier'
|
|
42
|
-
>
|
|
43
|
-
'Area:scroll': Pick<
|
|
44
|
-
DSAreaPublishEventData<E>,
|
|
45
|
-
'scroll_directions' | 'scroll_multiplier'
|
|
46
|
-
>
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
export default class Area<E extends DSInputElement> {
|
|
50
|
-
private DS: DragSelect<E>
|
|
51
|
-
private PS: PubSub<E>
|
|
52
|
-
private Settings: DSSettings<E>
|
|
53
|
-
private _observers?: { cleanup: () => void }
|
|
54
|
-
private _node: DSArea
|
|
55
|
-
private _parentNodes?: Node[]
|
|
56
|
-
private _computedStyle?: CSSStyleDeclaration
|
|
57
|
-
private _computedBorder?: DSEdgesObj
|
|
58
|
-
private _rect?: DSBoundingRect
|
|
59
|
-
|
|
60
|
-
constructor({ DS, PS }: { DS: DragSelect<E>; PS: PubSub<E> }) {
|
|
61
|
-
this.DS = DS
|
|
62
|
-
this.PS = PS
|
|
63
|
-
this.Settings = this.DS.stores.SettingsStore.s
|
|
64
|
-
|
|
65
|
-
this._node = this.Settings.area
|
|
66
|
-
this.setArea(this.Settings.area)
|
|
67
|
-
this.PS.subscribe('Settings:updated:area', ({ settings: { area } }) =>
|
|
68
|
-
this.setArea(area)
|
|
69
|
-
)
|
|
70
|
-
|
|
71
|
-
this.PS.subscribe('Interaction:init', this.init)
|
|
72
|
-
this.PS.subscribe('Interaction:end', this.reset)
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
private setArea = (area: DSArea) => {
|
|
76
|
-
this.reset()
|
|
77
|
-
this._node = area
|
|
78
|
-
handleElementPositionAttribute({
|
|
79
|
-
computedStyle: this.computedStyle,
|
|
80
|
-
node: this._node,
|
|
81
|
-
})
|
|
82
|
-
|
|
83
|
-
// first immediate debounce to update values after dom-update
|
|
84
|
-
setTimeout(() => {
|
|
85
|
-
this.PS.publish('Area:modified:pre', { item: this.HTMLNode })
|
|
86
|
-
this.reset()
|
|
87
|
-
this.PS.publish('Area:modified', { item: this.HTMLNode })
|
|
88
|
-
})
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
private init = () => {
|
|
92
|
-
this._observers = addModificationObservers(
|
|
93
|
-
this.parentNodes,
|
|
94
|
-
debounce((event: MutationCallbackEvent) => {
|
|
95
|
-
this.PS.publish('Area:modified:pre', { event, item: this.HTMLNode })
|
|
96
|
-
this.reset()
|
|
97
|
-
this.PS.publish('Area:modified', { event, item: this.HTMLNode })
|
|
98
|
-
}, 60)
|
|
99
|
-
)
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
private reset = () => {
|
|
103
|
-
this._computedStyle = undefined
|
|
104
|
-
this._rect = undefined
|
|
105
|
-
this._computedBorder = undefined
|
|
106
|
-
this._parentNodes = undefined
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
public stop = () => {
|
|
110
|
-
this._observers?.cleanup()
|
|
111
|
-
this.reset()
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
/// ///////////////////////////////////////////////////////////////////////////////////
|
|
115
|
-
// Scroll
|
|
116
|
-
|
|
117
|
-
/** Scroll the area in the specified direction */
|
|
118
|
-
public scroll = (directions: DSEdges, multiplier: number) => {
|
|
119
|
-
const data = {
|
|
120
|
-
scroll_directions: directions,
|
|
121
|
-
scroll_multiplier: multiplier,
|
|
122
|
-
}
|
|
123
|
-
this.PS.publish('Area:scroll:pre', data)
|
|
124
|
-
scrollElement(this._node, directions, multiplier)
|
|
125
|
-
this.PS.publish('Area:scroll', data)
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
/// ///////////////////////////////////////////////////////////////////////////////////
|
|
129
|
-
// Node Getters
|
|
130
|
-
|
|
131
|
-
public get HTMLNode() {
|
|
132
|
-
return this._node
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
/** The computed border from the element (caches result) */
|
|
136
|
-
public get computedBorder() {
|
|
137
|
-
if (this._computedBorder) return this._computedBorder
|
|
138
|
-
return {
|
|
139
|
-
top: parseInt(this.computedStyle.borderTopWidth),
|
|
140
|
-
bottom: parseInt(this.computedStyle.borderBottomWidth),
|
|
141
|
-
left: parseInt(this.computedStyle.borderLeftWidth),
|
|
142
|
-
right: parseInt(this.computedStyle.borderRightWidth),
|
|
143
|
-
}
|
|
144
|
-
}
|
|
145
|
-
|
|
146
|
-
/** The computed styles from the element (caches result) */
|
|
147
|
-
private get computedStyle() {
|
|
148
|
-
if (this._computedStyle) return this._computedStyle
|
|
149
|
-
if (this.HTMLNode instanceof Document)
|
|
150
|
-
return (this._computedStyle = window.getComputedStyle(
|
|
151
|
-
this.HTMLNode.body || this.HTMLNode.documentElement
|
|
152
|
-
))
|
|
153
|
-
return (this._computedStyle = window.getComputedStyle(this.HTMLNode!))
|
|
154
|
-
}
|
|
155
|
-
|
|
156
|
-
/** The element rect (caches result) (without scrollbar or borders) */
|
|
157
|
-
public get rect() {
|
|
158
|
-
if (this._rect) return this._rect
|
|
159
|
-
return (this._rect = getAreaRect(
|
|
160
|
-
this.HTMLNode,
|
|
161
|
-
this.DS.stores.SettingsStore.s.zoom
|
|
162
|
-
))
|
|
163
|
-
}
|
|
164
|
-
|
|
165
|
-
private get parentNodes() {
|
|
166
|
-
if (this._parentNodes) return this._parentNodes
|
|
167
|
-
return (this._parentNodes = getAllParentNodes(this.HTMLNode))
|
|
168
|
-
}
|
|
169
|
-
}
|
package/src/modules/Drag.ts
DELETED
|
@@ -1,236 +0,0 @@
|
|
|
1
|
-
import DragSelect from '../DragSelect'
|
|
2
|
-
import PubSub from './PubSub'
|
|
3
|
-
import { DSBoundingRect, DSDragKeys, DSInputElement, Vect2 } from '../types'
|
|
4
|
-
import { DSSettings } from '../stores/SettingsStore'
|
|
5
|
-
import { calcVect, num2vect, vect2rect } from '../methods/vect2'
|
|
6
|
-
import { handleKeyboardDragPosDifference } from '../methods/handleKeyboardDragPosDifference'
|
|
7
|
-
import { moveElement } from '../methods/moveElement'
|
|
8
|
-
import { limitDirection } from '../methods/limitDirection'
|
|
9
|
-
|
|
10
|
-
export default class Drag<E extends DSInputElement> {
|
|
11
|
-
private _prevCursorPos?: Vect2
|
|
12
|
-
private _prevScrollPos?: Vect2
|
|
13
|
-
private _elements: E[] = []
|
|
14
|
-
private _dragKeys?: DSDragKeys
|
|
15
|
-
private _dragKeysFlat: string[] = []
|
|
16
|
-
private _selectionRect: DSBoundingRect = vect2rect(num2vect(0))
|
|
17
|
-
DS: DragSelect<E>
|
|
18
|
-
PS: PubSub<E>
|
|
19
|
-
Settings: DSSettings<E>
|
|
20
|
-
|
|
21
|
-
constructor({ DS, PS }: { DS: DragSelect<E>; PS: PubSub<E> }) {
|
|
22
|
-
this.DS = DS
|
|
23
|
-
this.PS = PS
|
|
24
|
-
this.Settings = this.DS.stores.SettingsStore.s
|
|
25
|
-
|
|
26
|
-
this.PS.subscribe('Settings:updated:dragKeys', this.assignDragKeys)
|
|
27
|
-
this.assignDragKeys()
|
|
28
|
-
|
|
29
|
-
this.PS.subscribe('Interaction:start', this.start)
|
|
30
|
-
this.PS.subscribe('Interaction:end', this.stop)
|
|
31
|
-
this.PS.subscribe('Interaction:update', this.update)
|
|
32
|
-
this.PS.subscribe('KeyStore:down', this.keyboardDrag)
|
|
33
|
-
this.PS.subscribe('KeyStore:up', this.keyboardEnd)
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
private assignDragKeys = () => {
|
|
37
|
-
this._dragKeys = {
|
|
38
|
-
up: this.Settings.dragKeys.up.map((k) => k.toLowerCase()),
|
|
39
|
-
down: this.Settings.dragKeys.down.map((k) => k.toLowerCase()),
|
|
40
|
-
left: this.Settings.dragKeys.left.map((k) => k.toLowerCase()),
|
|
41
|
-
right: this.Settings.dragKeys.right.map((k) => k.toLowerCase()),
|
|
42
|
-
}
|
|
43
|
-
this._dragKeysFlat = [
|
|
44
|
-
...this._dragKeys.up,
|
|
45
|
-
...this._dragKeys.down,
|
|
46
|
-
...this._dragKeys.left,
|
|
47
|
-
...this._dragKeys.right,
|
|
48
|
-
]
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
private keyboardDrag = ({
|
|
52
|
-
event,
|
|
53
|
-
key,
|
|
54
|
-
}: {
|
|
55
|
-
event: KeyboardEvent
|
|
56
|
-
key: string
|
|
57
|
-
}) => {
|
|
58
|
-
const _key = key.toLowerCase()
|
|
59
|
-
if (
|
|
60
|
-
!this.Settings.keyboardDrag ||
|
|
61
|
-
!this._dragKeysFlat.includes(_key) ||
|
|
62
|
-
!this.DS.SelectedSet.size ||
|
|
63
|
-
!this.Settings.draggability ||
|
|
64
|
-
this.DS.continue
|
|
65
|
-
)
|
|
66
|
-
return
|
|
67
|
-
|
|
68
|
-
const publishData = {
|
|
69
|
-
event,
|
|
70
|
-
isDragging: true,
|
|
71
|
-
isDraggingKeyboard: true,
|
|
72
|
-
key,
|
|
73
|
-
}
|
|
74
|
-
this.PS.publish(['Interaction:start:pre', 'Interaction:start'], publishData)
|
|
75
|
-
|
|
76
|
-
this._elements = this.DS.getSelection()
|
|
77
|
-
this._selectionRect = this.DS.Selection.boundingRect
|
|
78
|
-
this.handleZIndex(true)
|
|
79
|
-
|
|
80
|
-
let posDirection = handleKeyboardDragPosDifference({
|
|
81
|
-
shiftKey: this.DS.stores.KeyStore.currentValues.includes('shift'),
|
|
82
|
-
keyboardDragSpeed: this.Settings.keyboardDragSpeed,
|
|
83
|
-
zoom: this.Settings.zoom,
|
|
84
|
-
key: _key,
|
|
85
|
-
scrollDiff: this._scrollDiff,
|
|
86
|
-
dragKeys: this._dragKeys,
|
|
87
|
-
})
|
|
88
|
-
|
|
89
|
-
posDirection = limitDirection({
|
|
90
|
-
direction: posDirection,
|
|
91
|
-
containerRect: this.DS.SelectorArea.rect,
|
|
92
|
-
scrollAmount: this.DS.stores.ScrollStore.scrollAmount,
|
|
93
|
-
selectionRect: this._selectionRect,
|
|
94
|
-
})
|
|
95
|
-
|
|
96
|
-
this.moveElements(posDirection)
|
|
97
|
-
|
|
98
|
-
this.PS.publish(
|
|
99
|
-
['Interaction:update:pre', 'Interaction:update'],
|
|
100
|
-
publishData
|
|
101
|
-
)
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
private keyboardEnd = ({
|
|
105
|
-
event,
|
|
106
|
-
key,
|
|
107
|
-
}: {
|
|
108
|
-
event: KeyboardEvent
|
|
109
|
-
key: string
|
|
110
|
-
}) => {
|
|
111
|
-
const _key = key.toLowerCase()
|
|
112
|
-
if (
|
|
113
|
-
!this.Settings.keyboardDrag ||
|
|
114
|
-
!this._dragKeysFlat.includes(_key) ||
|
|
115
|
-
!this.DS.SelectedSet.size ||
|
|
116
|
-
!this.Settings.draggability
|
|
117
|
-
)
|
|
118
|
-
return
|
|
119
|
-
const publishData = {
|
|
120
|
-
event,
|
|
121
|
-
isDragging: this.Settings.draggability,
|
|
122
|
-
isDraggingKeyboard: true,
|
|
123
|
-
key,
|
|
124
|
-
}
|
|
125
|
-
this.PS.publish(['Interaction:end:pre', 'Interaction:end'], publishData)
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
private start = ({
|
|
129
|
-
isDragging,
|
|
130
|
-
isDraggingKeyboard,
|
|
131
|
-
}: {
|
|
132
|
-
isDragging?: boolean
|
|
133
|
-
isDraggingKeyboard?: boolean
|
|
134
|
-
}) => {
|
|
135
|
-
if (!isDragging || isDraggingKeyboard) return
|
|
136
|
-
this._prevCursorPos = undefined
|
|
137
|
-
this._prevScrollPos = undefined
|
|
138
|
-
this._elements = this.DS.getSelection()
|
|
139
|
-
this._selectionRect = this.DS.Selection.boundingRect
|
|
140
|
-
this.handleZIndex(true)
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
public stop = () => {
|
|
144
|
-
this._prevCursorPos = undefined
|
|
145
|
-
this._prevScrollPos = undefined
|
|
146
|
-
this.handleZIndex(false)
|
|
147
|
-
this._elements = []
|
|
148
|
-
}
|
|
149
|
-
|
|
150
|
-
private update = ({
|
|
151
|
-
isDragging,
|
|
152
|
-
isDraggingKeyboard,
|
|
153
|
-
}: {
|
|
154
|
-
isDragging?: boolean
|
|
155
|
-
isDraggingKeyboard?: boolean
|
|
156
|
-
}) => {
|
|
157
|
-
if (
|
|
158
|
-
!isDragging ||
|
|
159
|
-
!this._elements.length ||
|
|
160
|
-
isDraggingKeyboard ||
|
|
161
|
-
this.DS.continue
|
|
162
|
-
)
|
|
163
|
-
return
|
|
164
|
-
|
|
165
|
-
let posDirection = calcVect(this._cursorDiff, '+', this._scrollDiff)
|
|
166
|
-
posDirection = limitDirection({
|
|
167
|
-
direction: posDirection,
|
|
168
|
-
containerRect: this.DS.SelectorArea.rect,
|
|
169
|
-
scrollAmount: this.DS.stores.ScrollStore.scrollAmount,
|
|
170
|
-
selectionRect: this._selectionRect,
|
|
171
|
-
})
|
|
172
|
-
|
|
173
|
-
this.moveElements(posDirection)
|
|
174
|
-
}
|
|
175
|
-
|
|
176
|
-
private handleZIndex = (add: boolean) => {
|
|
177
|
-
this._elements.forEach(
|
|
178
|
-
(element) =>
|
|
179
|
-
(element.style.zIndex = `${
|
|
180
|
-
(parseInt(element.style.zIndex) || 0) + (add ? 9999 : -9998)
|
|
181
|
-
}`)
|
|
182
|
-
)
|
|
183
|
-
}
|
|
184
|
-
|
|
185
|
-
private moveElements = (posDirection: Vect2) => {
|
|
186
|
-
// [PUBLICLY EXPOSED METHOD]
|
|
187
|
-
const { elements, direction } = this.filterDragElements({
|
|
188
|
-
elements: this._elements,
|
|
189
|
-
direction: posDirection,
|
|
190
|
-
})
|
|
191
|
-
elements.forEach((element) => {
|
|
192
|
-
moveElement({
|
|
193
|
-
element,
|
|
194
|
-
posDirection: direction,
|
|
195
|
-
containerRect: this.DS.SelectorArea.rect,
|
|
196
|
-
useTransform: this.Settings.useTransform,
|
|
197
|
-
})
|
|
198
|
-
})
|
|
199
|
-
}
|
|
200
|
-
|
|
201
|
-
private get _cursorDiff() {
|
|
202
|
-
const currentPointerVal = this.DS.stores.PointerStore.currentVal
|
|
203
|
-
const cursorDiff = this._prevCursorPos
|
|
204
|
-
? calcVect(currentPointerVal, '-', this._prevCursorPos)
|
|
205
|
-
: { x: 0, y: 0 }
|
|
206
|
-
this._prevCursorPos = currentPointerVal
|
|
207
|
-
return cursorDiff
|
|
208
|
-
}
|
|
209
|
-
|
|
210
|
-
private get _scrollDiff() {
|
|
211
|
-
const currentScrollVal = this.DS.stores.ScrollStore.currentVal
|
|
212
|
-
const scrollDiff = this._prevScrollPos
|
|
213
|
-
? calcVect(currentScrollVal, '-', this._prevScrollPos)
|
|
214
|
-
: { x: 0, y: 0 }
|
|
215
|
-
this._prevScrollPos = currentScrollVal
|
|
216
|
-
return scrollDiff
|
|
217
|
-
}
|
|
218
|
-
|
|
219
|
-
////
|
|
220
|
-
// [PUBLICLY EXPOSED METHODS]
|
|
221
|
-
|
|
222
|
-
/**
|
|
223
|
-
* Can be overridden to apply further filtering logic after the items to move are identified but before they actually get moved
|
|
224
|
-
* Is expected to return the elements in the same shape as passed in
|
|
225
|
-
*/
|
|
226
|
-
public filterDragElements = ({
|
|
227
|
-
elements,
|
|
228
|
-
direction,
|
|
229
|
-
}: {
|
|
230
|
-
elements: E[]
|
|
231
|
-
direction: Vect2
|
|
232
|
-
}) => ({
|
|
233
|
-
elements,
|
|
234
|
-
direction,
|
|
235
|
-
})
|
|
236
|
-
}
|
package/src/modules/DropZone.ts
DELETED
|
@@ -1,223 +0,0 @@
|
|
|
1
|
-
import DragSelect from '../DragSelect'
|
|
2
|
-
import PubSub from './PubSub'
|
|
3
|
-
import { DSInputElement } from '../types'
|
|
4
|
-
import { DSSettings } from '../stores/SettingsStore'
|
|
5
|
-
import { addModificationObservers } from '../methods/addModificationObservers'
|
|
6
|
-
import { debounce } from '../methods/debounce'
|
|
7
|
-
import { getAllParentNodes } from '../methods/getAllParentNodes'
|
|
8
|
-
import { isCollision } from '../methods/isCollision'
|
|
9
|
-
import { ensureArray } from '../methods/ensureArray'
|
|
10
|
-
|
|
11
|
-
export type DSDropZone<E extends DSInputElement> = {
|
|
12
|
-
id: string
|
|
13
|
-
element: E
|
|
14
|
-
droppables: E[]
|
|
15
|
-
/** items related to the target zone */
|
|
16
|
-
itemsDropped?: E[]
|
|
17
|
-
/** items that are within the targets bounds */
|
|
18
|
-
itemsInside?: E[]
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
export default class DropZone<E extends DSInputElement> {
|
|
22
|
-
public id: string
|
|
23
|
-
public element: E
|
|
24
|
-
private _droppables?: E[]
|
|
25
|
-
private _rect?: DOMRect
|
|
26
|
-
private _observers?: { cleanup: () => void }
|
|
27
|
-
private _timeout?: NodeJS.Timeout
|
|
28
|
-
private _itemsDropped: E[] = []
|
|
29
|
-
private _itemsInside?: E[]
|
|
30
|
-
private DS: DragSelect<E>
|
|
31
|
-
private PS: PubSub<E>
|
|
32
|
-
private Settings: DSSettings<E>
|
|
33
|
-
private isDestroyed: boolean = false
|
|
34
|
-
private _parentNodes?: Node[]
|
|
35
|
-
|
|
36
|
-
constructor({
|
|
37
|
-
DS,
|
|
38
|
-
PS,
|
|
39
|
-
id,
|
|
40
|
-
element,
|
|
41
|
-
droppables,
|
|
42
|
-
}: {
|
|
43
|
-
DS: DragSelect<E>
|
|
44
|
-
PS: PubSub<E>
|
|
45
|
-
id: string
|
|
46
|
-
element: E
|
|
47
|
-
droppables?: E[]
|
|
48
|
-
}) {
|
|
49
|
-
this.DS = DS
|
|
50
|
-
this.PS = PS
|
|
51
|
-
this.Settings = this.DS.stores.SettingsStore.s
|
|
52
|
-
this.id = id
|
|
53
|
-
this.element = element
|
|
54
|
-
if (droppables) this.droppables = ensureArray(droppables)
|
|
55
|
-
this.element.classList.add(`${this.Settings.dropZoneClass}`)
|
|
56
|
-
|
|
57
|
-
this.PS.subscribe('Settings:updated:dropZoneClass', ({ settings }) => {
|
|
58
|
-
if (!this.element) return
|
|
59
|
-
this.element.classList.remove(settings['dropZoneClass:pre'])
|
|
60
|
-
this.element.classList.add(settings.dropZoneClass)
|
|
61
|
-
})
|
|
62
|
-
|
|
63
|
-
this._observers = addModificationObservers(
|
|
64
|
-
this.parentNodes,
|
|
65
|
-
debounce(() => (this._rect = undefined), this.Settings.refreshMemoryRate)
|
|
66
|
-
)
|
|
67
|
-
|
|
68
|
-
this.PS.subscribe('Interaction:start', this.start)
|
|
69
|
-
this.PS.subscribe('Interaction:end', this.stop)
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
private setReadyClasses = (action: 'add' | 'remove') => {
|
|
73
|
-
if (this.isDestroyed) return
|
|
74
|
-
const selectedEls = this.droppables.filter((el) =>
|
|
75
|
-
this.DS.SelectedSet.has(el)
|
|
76
|
-
)
|
|
77
|
-
if (!selectedEls.length) return
|
|
78
|
-
selectedEls.forEach((item) => {
|
|
79
|
-
item.classList[action](`${this.Settings.droppableClass}`)
|
|
80
|
-
item.classList[action](`${this.Settings.droppableClass}-${this.id}`)
|
|
81
|
-
})
|
|
82
|
-
this.element.classList[action](`${this.Settings.dropZoneReadyClass}`)
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
/** This zone is NOT the target of a drop */
|
|
86
|
-
public handleNoDrop = () => {
|
|
87
|
-
if (this.isDestroyed) return
|
|
88
|
-
// for each selected element that is not part of the target zone, remove the classes
|
|
89
|
-
this.DS.SelectedSet.forEach((item) => {
|
|
90
|
-
item.classList.remove(this.Settings.droppedTargetClass)
|
|
91
|
-
item.classList.remove(`${this.Settings.droppedTargetClass}-${this.id}`)
|
|
92
|
-
})
|
|
93
|
-
// and remove them from the zones dropped items
|
|
94
|
-
this._itemsDropped = this._itemsDropped.filter(
|
|
95
|
-
(item) => !this.DS.SelectedSet.has(item)
|
|
96
|
-
)
|
|
97
|
-
// if the zone has no dropped left, also remove the zones class
|
|
98
|
-
if (!this._itemsDropped?.length)
|
|
99
|
-
this.element.classList.remove(`${this.Settings.dropZoneTargetClass}`)
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
/** This zone IS the target of a drop */
|
|
103
|
-
public handleDrop = () => {
|
|
104
|
-
if (this.isDestroyed) return
|
|
105
|
-
this._itemsDropped = [
|
|
106
|
-
...new Set([
|
|
107
|
-
...this._itemsDropped,
|
|
108
|
-
...this.droppables?.filter((item) => this.DS.SelectedSet.has(item)),
|
|
109
|
-
]),
|
|
110
|
-
]
|
|
111
|
-
// add the target class to the zones dropped items
|
|
112
|
-
this._itemsDropped?.forEach((item) => {
|
|
113
|
-
item.classList.add(`${this.Settings.droppedTargetClass}`)
|
|
114
|
-
item.classList.add(`${this.Settings.droppedTargetClass}-${this.id}`)
|
|
115
|
-
})
|
|
116
|
-
// if the zone has dropped, add the zones class
|
|
117
|
-
if (this._itemsDropped?.length)
|
|
118
|
-
this.element.classList.add(`${this.Settings.dropZoneTargetClass}`)
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
public handleItemsInsideClasses = () => {
|
|
122
|
-
let isAnyInside = false
|
|
123
|
-
this.droppables.forEach((item) => {
|
|
124
|
-
if (this.itemsInside?.includes(item)) {
|
|
125
|
-
item.classList.add(`${this.Settings.droppedInsideClass}`)
|
|
126
|
-
item.classList.add(`${this.Settings.droppedInsideClass}-${this.id}`)
|
|
127
|
-
isAnyInside = true
|
|
128
|
-
} else {
|
|
129
|
-
item.classList.remove(`${this.Settings.droppedInsideClass}-${this.id}`)
|
|
130
|
-
if (!item.className.includes(`${this.Settings.droppedInsideClass}-`))
|
|
131
|
-
item.classList.remove(`${this.Settings.droppedInsideClass}`)
|
|
132
|
-
}
|
|
133
|
-
})
|
|
134
|
-
|
|
135
|
-
if (isAnyInside)
|
|
136
|
-
this.element.classList.add(`${this.Settings.dropZoneInsideClass}`)
|
|
137
|
-
else this.element.classList.remove(`${this.Settings.dropZoneInsideClass}`)
|
|
138
|
-
}
|
|
139
|
-
|
|
140
|
-
private start = ({ isDragging }: { isDragging: boolean }) => {
|
|
141
|
-
if (!isDragging || this.isDestroyed) return
|
|
142
|
-
this.setReadyClasses('add')
|
|
143
|
-
}
|
|
144
|
-
|
|
145
|
-
private stop = ({ isDragging }: { isDragging: boolean }) => {
|
|
146
|
-
if (!isDragging || this.isDestroyed) return
|
|
147
|
-
this.setReadyClasses('remove')
|
|
148
|
-
this.handleItemsInsideClasses()
|
|
149
|
-
}
|
|
150
|
-
|
|
151
|
-
public destroy() {
|
|
152
|
-
this._observers?.cleanup()
|
|
153
|
-
this.element.classList.remove(`${this.Settings.dropZoneClass}`)
|
|
154
|
-
this.element.classList.remove(`${this.Settings.dropZoneTargetClass}`)
|
|
155
|
-
this.element.classList.remove(`${this.Settings.dropZoneReadyClass}`)
|
|
156
|
-
this.droppables.forEach((item) => {
|
|
157
|
-
item.classList.remove(`${this.Settings.droppedTargetClass}`)
|
|
158
|
-
item.classList.remove(`${this.Settings.droppedTargetClass}-${this.id}`)
|
|
159
|
-
item.classList.remove(`${this.Settings.droppableClass}`)
|
|
160
|
-
item.classList.remove(`${this.Settings.droppableClass}-${this.id}`)
|
|
161
|
-
})
|
|
162
|
-
this.PS.unsubscribe('Interaction:start', this.start)
|
|
163
|
-
this.PS.unsubscribe('Interaction:end', this.stop)
|
|
164
|
-
this.isDestroyed = true
|
|
165
|
-
}
|
|
166
|
-
|
|
167
|
-
public toObject = (): DSDropZone<E> => ({
|
|
168
|
-
id: this.id,
|
|
169
|
-
element: this.element,
|
|
170
|
-
droppables: this.droppables,
|
|
171
|
-
itemsDropped: this.itemsDropped,
|
|
172
|
-
itemsInside: this.itemsInside,
|
|
173
|
-
})
|
|
174
|
-
|
|
175
|
-
public get rect() {
|
|
176
|
-
if (this.isDestroyed) return undefined
|
|
177
|
-
if (this._rect) return this._rect
|
|
178
|
-
return (this._rect = this.element.getBoundingClientRect())
|
|
179
|
-
}
|
|
180
|
-
|
|
181
|
-
public get itemsDropped() {
|
|
182
|
-
if (this.isDestroyed) return undefined
|
|
183
|
-
return this._itemsDropped
|
|
184
|
-
}
|
|
185
|
-
|
|
186
|
-
public get itemsInside() {
|
|
187
|
-
if (this.isDestroyed) return undefined
|
|
188
|
-
if (this._itemsInside) return this._itemsInside
|
|
189
|
-
|
|
190
|
-
this._itemsInside = this.droppables.flatMap((item) => {
|
|
191
|
-
const itemRect = this.DS.SelectableSet.rects.get(item)
|
|
192
|
-
if (
|
|
193
|
-
this.rect &&
|
|
194
|
-
isCollision(itemRect, this.rect, this.Settings.dropInsideThreshold)
|
|
195
|
-
)
|
|
196
|
-
return [item]
|
|
197
|
-
return []
|
|
198
|
-
})
|
|
199
|
-
|
|
200
|
-
// since elements can be moved while this getter is called, we need to update the values every X seconds
|
|
201
|
-
if (this._timeout) clearTimeout(this._timeout)
|
|
202
|
-
this._timeout = setTimeout(
|
|
203
|
-
() => (this._itemsInside = undefined),
|
|
204
|
-
this.Settings.refreshMemoryRate
|
|
205
|
-
)
|
|
206
|
-
|
|
207
|
-
return this._itemsInside
|
|
208
|
-
}
|
|
209
|
-
|
|
210
|
-
private get parentNodes() {
|
|
211
|
-
if (this._parentNodes) return this._parentNodes
|
|
212
|
-
return (this._parentNodes = getAllParentNodes(this.element))
|
|
213
|
-
}
|
|
214
|
-
|
|
215
|
-
public get droppables() {
|
|
216
|
-
if (this._droppables) return this._droppables
|
|
217
|
-
return this.DS.SelectableSet.elements
|
|
218
|
-
}
|
|
219
|
-
|
|
220
|
-
private set droppables(value) {
|
|
221
|
-
this._droppables = value
|
|
222
|
-
}
|
|
223
|
-
}
|