dragselect 3.0.1 → 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.d.ts +877 -0
- package/dist/DragSelect.esm.js +2597 -0
- package/dist/DragSelect.js +2605 -0
- package/dist/ds.esm.min.js +1 -0
- package/dist/ds.min.js +1 -0
- package/package.json +9 -1
- package/deployment.md +0 -33
- package/jest-puppeteer.config.js +0 -16
- package/jest.config.js +0 -10
- package/rollup.config.mjs +0 -82
- 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/tsconfig.json +0 -105
|
@@ -1,251 +0,0 @@
|
|
|
1
|
-
import DragSelect from '../DragSelect'
|
|
2
|
-
import PubSub from './PubSub'
|
|
3
|
-
import { DSEdges, DSInputElement } from '../types'
|
|
4
|
-
import { DSSettings } from '../stores/SettingsStore'
|
|
5
|
-
|
|
6
|
-
export type DSInteractionPublishEventNames =
|
|
7
|
-
| 'Interaction:init:pre'
|
|
8
|
-
| 'Interaction:init'
|
|
9
|
-
| 'Interaction:start:pre'
|
|
10
|
-
| 'Interaction:start'
|
|
11
|
-
| 'Interaction:update:pre'
|
|
12
|
-
| 'Interaction:update'
|
|
13
|
-
| 'Interaction:end:pre'
|
|
14
|
-
| 'Interaction:end'
|
|
15
|
-
|
|
16
|
-
export type DSInteractionPublishEventData = {
|
|
17
|
-
event: InteractionEvent | KeyboardEvent
|
|
18
|
-
/** Whether the interaction is a drag or a select */
|
|
19
|
-
isDragging: boolean
|
|
20
|
-
/** Whether or not the drag interaction is via keyboard */
|
|
21
|
-
isDraggingKeyboard?: boolean
|
|
22
|
-
key?: string
|
|
23
|
-
scroll_directions?: DSEdges
|
|
24
|
-
scroll_multiplier?: number
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
export type DSInteractionPublish = {
|
|
28
|
-
'Interaction:init:pre': {}
|
|
29
|
-
'Interaction:init': {}
|
|
30
|
-
'Interaction:start:pre': DSInteractionPublishEventData
|
|
31
|
-
'Interaction:start': DSInteractionPublishEventData
|
|
32
|
-
'Interaction:update:pre': Partial<DSInteractionPublishEventData>
|
|
33
|
-
'Interaction:update': Partial<DSInteractionPublishEventData>
|
|
34
|
-
'Interaction:end:pre': DSInteractionPublishEventData
|
|
35
|
-
'Interaction:end': DSInteractionPublishEventData
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
export type InteractionEvent = MouseEvent | PointerEvent | TouchEvent
|
|
39
|
-
|
|
40
|
-
export default class Interaction<E extends DSInputElement> {
|
|
41
|
-
private isInteracting?: boolean
|
|
42
|
-
public isDragging: boolean = false
|
|
43
|
-
private DS: DragSelect<E>
|
|
44
|
-
private PS: PubSub<E>
|
|
45
|
-
private Settings: DSSettings<E>
|
|
46
|
-
|
|
47
|
-
constructor({ DS, PS }: { DS: DragSelect<E>; PS: PubSub<E> }) {
|
|
48
|
-
this.DS = DS
|
|
49
|
-
this.PS = PS
|
|
50
|
-
this.Settings = this.DS.stores.SettingsStore.s
|
|
51
|
-
|
|
52
|
-
// not on every modification, just on change of area
|
|
53
|
-
this.PS.subscribe('Settings:updated:area', ({ settings }) => {
|
|
54
|
-
this.removeAreaEventListeners(settings['area:pre'])
|
|
55
|
-
this.setAreaEventListeners(settings['area'])
|
|
56
|
-
})
|
|
57
|
-
this.PS.subscribe('PointerStore:updated', ({ event }) =>
|
|
58
|
-
this.update({ event })
|
|
59
|
-
)
|
|
60
|
-
this.PS.subscribe('Selectable:click', this.onClick)
|
|
61
|
-
this.PS.subscribe('Selectable:pointer', ({ event }) => this.start(event))
|
|
62
|
-
this.PS.subscribe('Interaction:start:pre', ({ event }) =>
|
|
63
|
-
this._start(event)
|
|
64
|
-
)
|
|
65
|
-
this.PS.subscribe('Interaction:init:pre', this._init)
|
|
66
|
-
this.PS.subscribe('Interaction:end:pre', ({ event }) => this._reset(event))
|
|
67
|
-
this.PS.subscribe('Area:scroll', this.update)
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
public init = () => this.PS.publish('Interaction:init:pre', { init: true })
|
|
71
|
-
|
|
72
|
-
private _init = () => {
|
|
73
|
-
this.stop()
|
|
74
|
-
this.setAreaEventListeners()
|
|
75
|
-
this.PS.publish('Interaction:init', { init: true })
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
private _canInteract(event: KeyboardEvent | InteractionEvent) {
|
|
79
|
-
const isKeyboardClick =
|
|
80
|
-
'clientX' in event &&
|
|
81
|
-
event.clientX === 0 &&
|
|
82
|
-
event.clientY === 0 &&
|
|
83
|
-
event.detail === 0 &&
|
|
84
|
-
event.target
|
|
85
|
-
|
|
86
|
-
if (
|
|
87
|
-
('button' in event && event.button === 2) || // right-clicks
|
|
88
|
-
this.isInteracting || // fix double-click issues
|
|
89
|
-
(event.target && !this.DS.SelectorArea.isInside(event.target as E)) || // fix outside elements issue
|
|
90
|
-
(!isKeyboardClick && !this.DS.SelectorArea.isClicked(event)) // make sure the mouse click is inside the area
|
|
91
|
-
)
|
|
92
|
-
return false
|
|
93
|
-
|
|
94
|
-
return true
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
private start = (event: Event) =>
|
|
98
|
-
this.PS.publish('Interaction:start:pre', {
|
|
99
|
-
event: event as InteractionEvent, // Event to satisfy event listeners but we know it’s an InteractionEvent
|
|
100
|
-
isDragging: this.isDragging,
|
|
101
|
-
})
|
|
102
|
-
|
|
103
|
-
private _start = (event: KeyboardEvent | InteractionEvent) => {
|
|
104
|
-
if (event.type === 'touchstart') event.preventDefault() // Call preventDefault() to prevent double click issue, see https://github.com/ThibaultJanBeyer/DragSelect/pull/29 & https://developer.mozilla.org/vi/docs/Web/API/Touch_events/Supporting_both_TouchEvent_and_MouseEvent
|
|
105
|
-
if (!this._canInteract(event)) return
|
|
106
|
-
|
|
107
|
-
this.isInteracting = true
|
|
108
|
-
this.isDragging = this.isDragEvent(event)
|
|
109
|
-
|
|
110
|
-
this.PS.publish('Interaction:start', {
|
|
111
|
-
event,
|
|
112
|
-
isDragging: this.isDragging,
|
|
113
|
-
})
|
|
114
|
-
|
|
115
|
-
this.setDocEventListeners()
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
private isDragEvent = (event: InteractionEvent | KeyboardEvent) => {
|
|
119
|
-
let clickedElement: E | null = null
|
|
120
|
-
if (event.target && 'closest' in event.target)
|
|
121
|
-
clickedElement = (event.target as E).closest(
|
|
122
|
-
`.${this.Settings.selectableClass}`
|
|
123
|
-
)
|
|
124
|
-
|
|
125
|
-
if (
|
|
126
|
-
!this.Settings.draggability ||
|
|
127
|
-
this.DS.stores.KeyStore.isMultiSelectKeyPressed(event) ||
|
|
128
|
-
!clickedElement
|
|
129
|
-
)
|
|
130
|
-
return false
|
|
131
|
-
|
|
132
|
-
if (this.Settings.immediateDrag) {
|
|
133
|
-
if (!this.DS.SelectedSet.size) this.DS.SelectedSet.add(clickedElement)
|
|
134
|
-
else if (!this.DS.SelectedSet.has(clickedElement)) {
|
|
135
|
-
this.DS.SelectedSet.clear()
|
|
136
|
-
this.DS.SelectedSet.add(clickedElement)
|
|
137
|
-
}
|
|
138
|
-
}
|
|
139
|
-
|
|
140
|
-
if (this.DS.SelectedSet.has(clickedElement)) return true
|
|
141
|
-
|
|
142
|
-
return false
|
|
143
|
-
}
|
|
144
|
-
|
|
145
|
-
/**
|
|
146
|
-
* Triggers when a node is actively selected: <button> nodes that are pressed via the keyboard.
|
|
147
|
-
* Making DragSelect accessible for everyone!
|
|
148
|
-
*/
|
|
149
|
-
private onClick = ({ event }: { event: MouseEvent }) => {
|
|
150
|
-
if (!this._canInteract(event)) return
|
|
151
|
-
if (event.detail > 0) return // mouse interaction
|
|
152
|
-
|
|
153
|
-
const {
|
|
154
|
-
stores: { PointerStore, KeyStore },
|
|
155
|
-
SelectableSet,
|
|
156
|
-
SelectedSet,
|
|
157
|
-
} = this.DS
|
|
158
|
-
|
|
159
|
-
PointerStore.start(event)
|
|
160
|
-
|
|
161
|
-
const node = event.target as E
|
|
162
|
-
if (node && !SelectableSet.has(node)) return
|
|
163
|
-
|
|
164
|
-
if (!KeyStore.isMultiSelectKeyPressed(event)) SelectedSet.clear()
|
|
165
|
-
if (node) SelectedSet.toggle(node)
|
|
166
|
-
|
|
167
|
-
this.reset(event) // simulate mouse-up (that does not exist on keyboard)
|
|
168
|
-
}
|
|
169
|
-
|
|
170
|
-
stop = (area = this.DS.Area.HTMLNode) => {
|
|
171
|
-
this.removeAreaEventListeners(area)
|
|
172
|
-
this.removeDocEventListeners()
|
|
173
|
-
}
|
|
174
|
-
|
|
175
|
-
update = ({
|
|
176
|
-
event,
|
|
177
|
-
scroll_directions,
|
|
178
|
-
scroll_multiplier,
|
|
179
|
-
}: {
|
|
180
|
-
event?: InteractionEvent
|
|
181
|
-
scroll_directions?: DSEdges
|
|
182
|
-
scroll_multiplier?: number
|
|
183
|
-
}) => {
|
|
184
|
-
if (this.isInteracting)
|
|
185
|
-
this.PS.publish(['Interaction:update:pre', 'Interaction:update'], {
|
|
186
|
-
event,
|
|
187
|
-
scroll_directions,
|
|
188
|
-
scroll_multiplier,
|
|
189
|
-
isDragging: this.isDragging,
|
|
190
|
-
})
|
|
191
|
-
}
|
|
192
|
-
|
|
193
|
-
reset = (event: InteractionEvent) =>
|
|
194
|
-
this.PS.publish('Interaction:end:pre', {
|
|
195
|
-
event,
|
|
196
|
-
isDragging: this.isDragging,
|
|
197
|
-
})
|
|
198
|
-
|
|
199
|
-
_reset = (event: InteractionEvent | KeyboardEvent) => {
|
|
200
|
-
const { isDragging } = this
|
|
201
|
-
this.isInteracting = false
|
|
202
|
-
this.isDragging = false
|
|
203
|
-
this.removeDocEventListeners()
|
|
204
|
-
this.PS.publish('Interaction:end', { event, isDragging })
|
|
205
|
-
}
|
|
206
|
-
|
|
207
|
-
//////////////////////////////////////////////////////////////////////////////////////
|
|
208
|
-
// Event Listeners
|
|
209
|
-
|
|
210
|
-
private setAreaEventListeners = (area = this.DS.Area.HTMLNode) => {
|
|
211
|
-
// @TODO: fix pointer events mixing issue see [PR](https://github.com/ThibaultJanBeyer/DragSelect/pull/128#issuecomment-1154885289)
|
|
212
|
-
if (this.Settings.usePointerEvents)
|
|
213
|
-
area.addEventListener('pointerdown', this.start, {
|
|
214
|
-
passive: false,
|
|
215
|
-
})
|
|
216
|
-
else area.addEventListener('mousedown', this.start)
|
|
217
|
-
area.addEventListener('touchstart', this.start, {
|
|
218
|
-
passive: false,
|
|
219
|
-
})
|
|
220
|
-
}
|
|
221
|
-
private removeAreaEventListeners = (area = this.DS.Area.HTMLNode) => {
|
|
222
|
-
// @TODO: fix pointer events mixing issue see [PR](https://github.com/ThibaultJanBeyer/DragSelect/pull/128#issuecomment-1154885289)
|
|
223
|
-
if (this.Settings.usePointerEvents) {
|
|
224
|
-
area.removeEventListener('pointerdown', this.start, {
|
|
225
|
-
// @ts-ignore
|
|
226
|
-
passive: false,
|
|
227
|
-
})
|
|
228
|
-
} else area.removeEventListener('mousedown', this.start)
|
|
229
|
-
area.removeEventListener('touchstart', this.start, {
|
|
230
|
-
// @ts-ignore
|
|
231
|
-
passive: false,
|
|
232
|
-
})
|
|
233
|
-
}
|
|
234
|
-
|
|
235
|
-
private setDocEventListeners = () => {
|
|
236
|
-
// @TODO: fix pointer events mixing issue see [PR](https://github.com/ThibaultJanBeyer/DragSelect/pull/128#issuecomment-1154885289)
|
|
237
|
-
if (this.Settings.usePointerEvents) {
|
|
238
|
-
document.addEventListener('pointerup', this.reset)
|
|
239
|
-
document.addEventListener('pointercancel', this.reset)
|
|
240
|
-
} else document.addEventListener('mouseup', this.reset)
|
|
241
|
-
document.addEventListener('touchend', this.reset)
|
|
242
|
-
}
|
|
243
|
-
private removeDocEventListeners = () => {
|
|
244
|
-
// @TODO: fix pointer events mixing issue see [PR](https://github.com/ThibaultJanBeyer/DragSelect/pull/128#issuecomment-1154885289)
|
|
245
|
-
if (this.Settings.usePointerEvents) {
|
|
246
|
-
document.removeEventListener('pointerup', this.reset)
|
|
247
|
-
document.removeEventListener('pointercancel', this.reset)
|
|
248
|
-
} else document.removeEventListener('mouseup', this.reset)
|
|
249
|
-
document.removeEventListener('touchend', this.reset)
|
|
250
|
-
}
|
|
251
|
-
}
|
package/src/modules/PubSub.ts
DELETED
|
@@ -1,111 +0,0 @@
|
|
|
1
|
-
import DragSelect from '../DragSelect'
|
|
2
|
-
import { DSAreaPublish } from './Area'
|
|
3
|
-
import { DSInteractionPublish } from './Interaction'
|
|
4
|
-
import { DSKeyStorePublish } from '../stores/KeyStore'
|
|
5
|
-
import { DSPointerStorePublish } from '../stores/PointerStore'
|
|
6
|
-
import { DSSelectablePublish } from './SelectableSet'
|
|
7
|
-
import { DSSelectedPublish } from './SelectedSet'
|
|
8
|
-
import { DSSettingsPublish } from '../stores/SettingsStore'
|
|
9
|
-
import { DSPublicPublish } from '../methods/subscriberAliases'
|
|
10
|
-
import { DSInputElement } from '../types'
|
|
11
|
-
|
|
12
|
-
export type DSPublishMappings<E extends DSInputElement> = DSSettingsPublish<E> &
|
|
13
|
-
DSAreaPublish<E> &
|
|
14
|
-
DSKeyStorePublish &
|
|
15
|
-
DSPointerStorePublish &
|
|
16
|
-
DSInteractionPublish &
|
|
17
|
-
DSSelectablePublish<E> &
|
|
18
|
-
DSSelectedPublish<E> &
|
|
19
|
-
DSPublicPublish<E>
|
|
20
|
-
|
|
21
|
-
export type DSCallback<T> = (data: T) => any
|
|
22
|
-
|
|
23
|
-
export default class PubSub<E extends DSInputElement> {
|
|
24
|
-
subscribers: {
|
|
25
|
-
[K in keyof DSPublishMappings<E>]?: DSCallback<DSPublishMappings<E>[K]>[]
|
|
26
|
-
} = {}
|
|
27
|
-
DS: DragSelect<E>
|
|
28
|
-
|
|
29
|
-
constructor({ DS }: { DS: DragSelect<E> }) {
|
|
30
|
-
this.DS = DS
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
/**
|
|
34
|
-
* Subscribe to an event
|
|
35
|
-
* @returns event id, can be used to unsubscribe more efficiently
|
|
36
|
-
*/
|
|
37
|
-
subscribe = <K extends keyof DSPublishMappings<E>>(
|
|
38
|
-
eventName: K,
|
|
39
|
-
callback: DSCallback<DSPublishMappings<E>[K]>
|
|
40
|
-
) => {
|
|
41
|
-
if (!Array.isArray(this.subscribers[eventName]))
|
|
42
|
-
this.subscribers[eventName] = []
|
|
43
|
-
const subscribers = this.subscribers[eventName] as DSCallback<
|
|
44
|
-
DSPublishMappings<E>[K]
|
|
45
|
-
>[]
|
|
46
|
-
subscribers.push(callback)
|
|
47
|
-
return subscribers.length - 1
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
/**
|
|
51
|
-
* Removes event subscription
|
|
52
|
-
* @param callback the callback method signature, has to be exactly the same as when subscribing. Consider using "id" instead.
|
|
53
|
-
* @param id event id returned when subscribed (more performant than callback search)
|
|
54
|
-
*/
|
|
55
|
-
unsubscribe = <K extends keyof DSPublishMappings<E>>(
|
|
56
|
-
eventName: K,
|
|
57
|
-
callback?: DSCallback<DSPublishMappings<E>[K]>,
|
|
58
|
-
id?: number
|
|
59
|
-
) => {
|
|
60
|
-
const index =
|
|
61
|
-
id ?? this.subscribers[eventName]?.findIndex((cb) => cb === callback)
|
|
62
|
-
this.subscribers[eventName]?.splice(Number(index), 1)
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
/**
|
|
66
|
-
* Publishes an event to all subscribers
|
|
67
|
-
* @param eventName
|
|
68
|
-
* @param data passed to the subscription method
|
|
69
|
-
*/
|
|
70
|
-
publish = <K extends keyof DSPublishMappings<E>>(
|
|
71
|
-
eventName: K | K[],
|
|
72
|
-
data: DSPublishMappings<E>[K]
|
|
73
|
-
) => {
|
|
74
|
-
if (Array.isArray(eventName))
|
|
75
|
-
eventName.forEach((name) => this._publish(name, data))
|
|
76
|
-
else this._publish(eventName, data)
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
private _publish = <K extends keyof DSPublishMappings<E>>(
|
|
80
|
-
eventName: K,
|
|
81
|
-
data: DSPublishMappings<E>[K]
|
|
82
|
-
) => {
|
|
83
|
-
const subscribers: DSCallback<DSPublishMappings<E>[K]>[] =
|
|
84
|
-
this.subscribers[eventName] ?? []
|
|
85
|
-
if (eventName.includes(`:pre`)) this._handlePrePublish(subscribers, data)
|
|
86
|
-
else this._handlePublish(subscribers, data)
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
// non-pre events are executed first in first out
|
|
90
|
-
private _handlePublish = <K extends keyof DSPublishMappings<E>>(
|
|
91
|
-
subscribers: DSCallback<DSPublishMappings<E>[K]>[],
|
|
92
|
-
data: DSPublishMappings<E>[K]
|
|
93
|
-
) => {
|
|
94
|
-
for (let i = 0, il = subscribers.length; i < il; i++) {
|
|
95
|
-
if (this.DS.stopped) return
|
|
96
|
-
subscribers[i]?.(data)
|
|
97
|
-
}
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
// pre events are executed last in first out (so user callbacks are called before DS callbacks)
|
|
101
|
-
_handlePrePublish = <K extends keyof DSPublishMappings<E>>(
|
|
102
|
-
subscribers: DSCallback<DSPublishMappings<E>[K]>[],
|
|
103
|
-
data: DSPublishMappings<E>[K]
|
|
104
|
-
) => {
|
|
105
|
-
let i = subscribers.length
|
|
106
|
-
while (i--) {
|
|
107
|
-
if (this.DS.stopped) return
|
|
108
|
-
subscribers[i]?.(data)
|
|
109
|
-
}
|
|
110
|
-
}
|
|
111
|
-
}
|
|
@@ -1,146 +0,0 @@
|
|
|
1
|
-
import DragSelect from "../DragSelect"
|
|
2
|
-
import PubSub from "./PubSub"
|
|
3
|
-
import { DSBoundingRect, DSInputElement } from "../types"
|
|
4
|
-
import { DSSettings } from "../stores/SettingsStore"
|
|
5
|
-
import { InteractionEvent } from "./Interaction"
|
|
6
|
-
import { handleElementPositionAttribute } from "../methods/handleElementPositionAttribute"
|
|
7
|
-
import { ensureArray } from "../methods/ensureArray"
|
|
8
|
-
|
|
9
|
-
export type DSSelectablePublishEventNames = "Selectable:added:pre"|"Selectable:added"|"Selectable:removed"|"Selectable:removed:pre"|"Selectable:click:pre"|"Selectable:click"|"Selectable:pointer:pre"|"Selectable:pointer"
|
|
10
|
-
|
|
11
|
-
export type DSSelectablePublishEventData<E extends DSInputElement> = {
|
|
12
|
-
/** The items currently selected */
|
|
13
|
-
items: E[],
|
|
14
|
-
/** The item currently selected */
|
|
15
|
-
item: E,
|
|
16
|
-
};
|
|
17
|
-
|
|
18
|
-
export type DSSelectablePublish<E extends DSInputElement> = {
|
|
19
|
-
"Selectable:added:pre": DSSelectablePublishEventData<E>
|
|
20
|
-
"Selectable:added": DSSelectablePublishEventData<E>
|
|
21
|
-
"Selectable:removed:pre": DSSelectablePublishEventData<E>
|
|
22
|
-
"Selectable:removed": DSSelectablePublishEventData<E>
|
|
23
|
-
"Selectable:click:pre": { event: MouseEvent }
|
|
24
|
-
"Selectable:click": { event: MouseEvent }
|
|
25
|
-
"Selectable:pointer:pre": { event: InteractionEvent }
|
|
26
|
-
"Selectable:pointer": { event: InteractionEvent }
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
export default class SelectableSet<E extends DSInputElement> extends Set<E> {
|
|
30
|
-
private _rects?: Map<E, DSBoundingRect>
|
|
31
|
-
private _timeout?: NodeJS.Timeout
|
|
32
|
-
private DS: DragSelect<E>
|
|
33
|
-
private PS: PubSub<E>
|
|
34
|
-
private Settings: DSSettings<E>
|
|
35
|
-
|
|
36
|
-
constructor({ DS, PS }: { DS: DragSelect<E>, PS: PubSub<E> }) {
|
|
37
|
-
super()
|
|
38
|
-
this.DS = DS
|
|
39
|
-
this.PS = PS
|
|
40
|
-
this.Settings = this.DS.stores.SettingsStore.s
|
|
41
|
-
this.PS.subscribe('Interaction:init', this.init)
|
|
42
|
-
this.PS.subscribe('Settings:updated:selectables', () => {
|
|
43
|
-
this.clear()
|
|
44
|
-
this.init()
|
|
45
|
-
})
|
|
46
|
-
this.PS.subscribe('Settings:updated:selectableClass', ({ settings }) => {
|
|
47
|
-
this.forEach((el) => {
|
|
48
|
-
el.classList.remove(settings['selectableClass:pre'])
|
|
49
|
-
el.classList.add(settings.selectableClass)
|
|
50
|
-
})
|
|
51
|
-
})
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
private init = () => ensureArray(this.Settings.selectables).forEach((el) => this.add(el))
|
|
55
|
-
|
|
56
|
-
public add(element: E) {
|
|
57
|
-
if (!element || super.has(element)) return this
|
|
58
|
-
const publishData = {
|
|
59
|
-
items: this.elements,
|
|
60
|
-
item: element,
|
|
61
|
-
}
|
|
62
|
-
this.PS.publish('Selectable:added:pre', publishData)
|
|
63
|
-
element.classList.add(this.Settings.selectableClass)
|
|
64
|
-
element.addEventListener('click', this._onClick)
|
|
65
|
-
|
|
66
|
-
if (this.Settings.usePointerEvents)
|
|
67
|
-
element.addEventListener('pointerdown', this._onPointer, { passive: false })
|
|
68
|
-
else element.addEventListener('mousedown', this._onPointer)
|
|
69
|
-
|
|
70
|
-
element.addEventListener('touchstart', this._onPointer, { passive: false })
|
|
71
|
-
|
|
72
|
-
if (this.Settings.draggability && !this.Settings.useTransform)
|
|
73
|
-
handleElementPositionAttribute({
|
|
74
|
-
computedStyle: window.getComputedStyle(element),
|
|
75
|
-
node: element,
|
|
76
|
-
})
|
|
77
|
-
|
|
78
|
-
this.PS.publish('Selectable:added', publishData)
|
|
79
|
-
return super.add(element)
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
public delete(element: E) {
|
|
83
|
-
if (!element || !super.has(element)) return true
|
|
84
|
-
const publishData = {
|
|
85
|
-
items: this.elements,
|
|
86
|
-
item: element,
|
|
87
|
-
}
|
|
88
|
-
this.PS.publish('Selectable:removed:pre', publishData)
|
|
89
|
-
element.classList.remove(this.Settings.selectableClass)
|
|
90
|
-
element.classList.remove(this.Settings.hoverClass)
|
|
91
|
-
element.removeEventListener('click', this._onClick)
|
|
92
|
-
|
|
93
|
-
if (this.Settings.usePointerEvents)
|
|
94
|
-
element.removeEventListener('pointerdown', this._onPointer, {
|
|
95
|
-
// @ts-ignore
|
|
96
|
-
passive: false,
|
|
97
|
-
})
|
|
98
|
-
else element.removeEventListener('mousedown', this._onPointer)
|
|
99
|
-
|
|
100
|
-
element.removeEventListener('touchstart', this._onPointer, {
|
|
101
|
-
// @ts-ignore
|
|
102
|
-
passive: false,
|
|
103
|
-
})
|
|
104
|
-
this.PS.publish('Selectable:removed', publishData)
|
|
105
|
-
return super.delete(element)
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
public clear = () => this.forEach((el) => this.delete(el))
|
|
109
|
-
|
|
110
|
-
private _onClick = (event: Event) => // we know it’s only a MouseEvent
|
|
111
|
-
this.PS.publish(['Selectable:click:pre', 'Selectable:click'], { event: event as MouseEvent })
|
|
112
|
-
|
|
113
|
-
private _onPointer = (event: Event) => // we know it’s only an InteractionEvent
|
|
114
|
-
this.PS.publish(['Selectable:pointer:pre', 'Selectable:pointer'], { event: event as InteractionEvent })
|
|
115
|
-
|
|
116
|
-
public addAll = (elements: E[]) => elements.forEach((el) => this.add(el))
|
|
117
|
-
|
|
118
|
-
public deleteAll = (elements: E[]) => elements.forEach((el) => this.delete(el))
|
|
119
|
-
|
|
120
|
-
/**
|
|
121
|
-
* Gets the bounding rect from private memory if available. If not gets it from the DOM.
|
|
122
|
-
* => Does not force rect calculation on all elements
|
|
123
|
-
*/
|
|
124
|
-
public getElementRect = (element: E) => this._rects ? this._rects.get(element) : element.getBoundingClientRect()
|
|
125
|
-
|
|
126
|
-
get elements() {
|
|
127
|
-
return Array.from(this.values())
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
get rects() {
|
|
131
|
-
if (this._rects) return this._rects
|
|
132
|
-
this._rects = new Map()
|
|
133
|
-
this.forEach((element) =>
|
|
134
|
-
this._rects?.set(element, element.getBoundingClientRect())
|
|
135
|
-
)
|
|
136
|
-
|
|
137
|
-
// since elements can be moved, we need to update the rects every X ms
|
|
138
|
-
if (this._timeout) clearTimeout(this._timeout)
|
|
139
|
-
this._timeout = setTimeout(
|
|
140
|
-
() => (this._rects = undefined),
|
|
141
|
-
this.Settings.refreshMemoryRate
|
|
142
|
-
)
|
|
143
|
-
|
|
144
|
-
return this._rects
|
|
145
|
-
}
|
|
146
|
-
}
|
|
@@ -1,92 +0,0 @@
|
|
|
1
|
-
import DragSelect from "../DragSelect"
|
|
2
|
-
import PubSub from "./PubSub"
|
|
3
|
-
import { DSSettings } from "../stores/SettingsStore"
|
|
4
|
-
import { DSBoundingRect, DSInputElement } from "../types";
|
|
5
|
-
|
|
6
|
-
export type DSSelectedPublishEventNames = "Selected:added:pre"|"Selected:added"|"Selected:removed"|"Selected:removed:pre"
|
|
7
|
-
|
|
8
|
-
export type DSSelectedPublishEventData<E extends DSInputElement> = {
|
|
9
|
-
items: E[],
|
|
10
|
-
item: E,
|
|
11
|
-
};
|
|
12
|
-
|
|
13
|
-
export type DSSelectedPublish<E extends DSInputElement> = {
|
|
14
|
-
[K in DSSelectedPublishEventNames]: DSSelectedPublishEventData<E>
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
export default class SelectedSet<E extends DSInputElement> extends Set<E> {
|
|
18
|
-
private _rects?: Map<E, DSBoundingRect>
|
|
19
|
-
private _timeout?: NodeJS.Timeout
|
|
20
|
-
private DS: DragSelect<E>
|
|
21
|
-
private PS: PubSub<E>
|
|
22
|
-
private Settings: DSSettings<E>
|
|
23
|
-
|
|
24
|
-
constructor({ DS, PS }: { DS: DragSelect<E>, PS: PubSub<E> }) {
|
|
25
|
-
super()
|
|
26
|
-
this.DS = DS
|
|
27
|
-
this.PS = PS
|
|
28
|
-
this.Settings = this.DS.stores.SettingsStore.s
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
public add(element?: E) {
|
|
32
|
-
if (!element || super.has(element)) return this
|
|
33
|
-
const publishData = {
|
|
34
|
-
items: this.elements,
|
|
35
|
-
item: element,
|
|
36
|
-
}
|
|
37
|
-
this.PS.publish('Selected:added:pre', publishData)
|
|
38
|
-
super.add(element)
|
|
39
|
-
element.classList.add(this.Settings.selectedClass)
|
|
40
|
-
element.style.zIndex = `${(parseInt(element.style.zIndex) || 0) + 1}`
|
|
41
|
-
this.PS.publish('Selected:added', publishData)
|
|
42
|
-
return this
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
public delete(element: E) {
|
|
46
|
-
if (!element || !super.has(element)) return true
|
|
47
|
-
const publishData = {
|
|
48
|
-
items: this.elements,
|
|
49
|
-
item: element,
|
|
50
|
-
}
|
|
51
|
-
this.PS.publish('Selected:removed:pre', publishData)
|
|
52
|
-
const deleted = super.delete(element)
|
|
53
|
-
element.classList.remove(this.Settings.selectedClass)
|
|
54
|
-
element.style.zIndex = `${(parseInt(element.style.zIndex) || 0) - 1}`
|
|
55
|
-
this.PS.publish('Selected:removed', publishData)
|
|
56
|
-
return deleted
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
public clear = () => this.forEach((el) => this.delete(el))
|
|
60
|
-
|
|
61
|
-
/** Adds/Removes an element. If it is already selected = remove, if not = add. */
|
|
62
|
-
public toggle(element: E) {
|
|
63
|
-
if (this.has(element)) this.delete(element)
|
|
64
|
-
else this.add(element)
|
|
65
|
-
return element
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
public addAll = (elements: E[]) => elements.forEach((el) => this.add(el))
|
|
69
|
-
|
|
70
|
-
public deleteAll = (elements: E[]) => elements.forEach((el) => this.delete(el))
|
|
71
|
-
|
|
72
|
-
get elements() {
|
|
73
|
-
return Array.from(this.values())
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
get rects() {
|
|
77
|
-
if (this._rects) return this._rects
|
|
78
|
-
this._rects = new Map()
|
|
79
|
-
this.forEach((element) =>
|
|
80
|
-
this._rects?.set(element, element.getBoundingClientRect())
|
|
81
|
-
)
|
|
82
|
-
|
|
83
|
-
// since elements can be moved, we need to update the rects every X ms
|
|
84
|
-
if (this._timeout) clearTimeout(this._timeout)
|
|
85
|
-
this._timeout = setTimeout(
|
|
86
|
-
() => (this._rects = undefined),
|
|
87
|
-
this.Settings.refreshMemoryRate
|
|
88
|
-
)
|
|
89
|
-
|
|
90
|
-
return this._rects
|
|
91
|
-
}
|
|
92
|
-
}
|