dragselect 2.7.4 → 3.0.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.
Files changed (144) hide show
  1. package/CHANGELOG.md +97 -23
  2. package/LICENSE +690 -0
  3. package/README.md +2 -1
  4. package/jest-puppeteer.config.js +13 -3
  5. package/jest.config.js +5 -0
  6. package/package.json +40 -46
  7. package/rollup.config.mjs +82 -0
  8. package/src/DragSelect.ts +425 -0
  9. package/src/methods/addModificationObservers.ts +51 -0
  10. package/src/methods/canScroll.ts +19 -0
  11. package/src/methods/{createSelectorAreaElement.js → createSelectorAreaElement.ts} +1 -8
  12. package/src/methods/{createSelectorElement.js → createSelectorElement.ts} +1 -9
  13. package/src/methods/{debounce.js → debounce.ts} +5 -11
  14. package/src/methods/ensureArray.ts +13 -0
  15. package/src/methods/getAllParentNodes.ts +15 -0
  16. package/src/methods/{getAreaRect.js → getAreaRect.ts} +2 -6
  17. package/src/methods/getCurrentScroll.ts +14 -0
  18. package/src/methods/{documentScroll.js → getDocumentScroll.ts} +2 -2
  19. package/src/methods/{getOverflowEdges.js → getOverflowEdges.ts} +11 -11
  20. package/src/methods/getPointerPos.ts +9 -0
  21. package/src/methods/getSelectionRect.ts +29 -0
  22. package/src/methods/{getSelectorPosition.js → getSelectorPosition.ts} +11 -9
  23. package/src/methods/{getStylePosition.spec.js → getStylePosition.spec.ts} +9 -9
  24. package/src/methods/{getStylePosition.js → getStylePosition.ts} +5 -21
  25. package/src/methods/handleElementPositionAttribute.ts +15 -0
  26. package/src/methods/handleKeyboardDragPosDifference.ts +35 -0
  27. package/src/methods/handleSelection.ts +26 -0
  28. package/src/methods/{handleUnSelection.js → handleUnSelection.ts} +14 -14
  29. package/src/methods/{hydrateSettings.js → hydrateSettings.ts} +15 -26
  30. package/src/methods/isCollision.ts +66 -0
  31. package/src/methods/limitDirection.ts +36 -0
  32. package/src/methods/moveElement.ts +18 -0
  33. package/src/methods/{scrollElement.js → scrollElement.ts} +4 -10
  34. package/src/methods/{setStylePosition.js → setStylePosition.ts} +3 -11
  35. package/src/methods/subscriberAliases.ts +240 -0
  36. package/src/methods/updateElementStylePos.ts +7 -0
  37. package/src/methods/vect2.ts +34 -0
  38. package/src/modules/Area.ts +169 -0
  39. package/src/modules/Drag.ts +236 -0
  40. package/src/modules/{DropZone.js → DropZone.ts} +80 -119
  41. package/src/modules/DropZones.ts +151 -0
  42. package/src/modules/Interaction.ts +251 -0
  43. package/src/modules/PubSub.ts +111 -0
  44. package/src/modules/SelectableSet.ts +146 -0
  45. package/src/modules/SelectedSet.ts +92 -0
  46. package/src/modules/{Selection.js → Selection.ts} +59 -44
  47. package/src/modules/Selector.ts +81 -0
  48. package/src/modules/{SelectorArea.js → SelectorArea.ts} +58 -71
  49. package/src/stores/KeyStore.ts +92 -0
  50. package/src/stores/PointerStore.ts +194 -0
  51. package/src/stores/ScrollStore.ts +87 -0
  52. package/src/stores/SettingsStore.ts +88 -0
  53. package/src/types.ts +158 -0
  54. package/tsconfig.json +105 -0
  55. package/bower.json +0 -24
  56. package/dist/DragSelect.d.ts +0 -200
  57. package/dist/DragSelect.es6m.js +0 -3886
  58. package/dist/DragSelect.js +0 -3894
  59. package/dist/ds.es6m.min.js +0 -1
  60. package/dist/ds.min.js +0 -1
  61. package/dist/methods/addModificationObservers.d.ts +0 -11
  62. package/dist/methods/canScroll.d.ts +0 -3
  63. package/dist/methods/createSelectorAreaElement.d.ts +0 -3
  64. package/dist/methods/createSelectorElement.d.ts +0 -3
  65. package/dist/methods/debounce.d.ts +0 -3
  66. package/dist/methods/documentScroll.d.ts +0 -6
  67. package/dist/methods/getAllParentNodes.d.ts +0 -3
  68. package/dist/methods/getAreaRect.d.ts +0 -3
  69. package/dist/methods/getBoundingClientRect.d.ts +0 -4
  70. package/dist/methods/getCurrentScroll.d.ts +0 -3
  71. package/dist/methods/getOverflowEdges.d.ts +0 -7
  72. package/dist/methods/getPointerPos.d.ts +0 -5
  73. package/dist/methods/getSelectorPosition.d.ts +0 -12
  74. package/dist/methods/getStylePosition.d.ts +0 -3
  75. package/dist/methods/handleElementOverflow.d.ts +0 -10
  76. package/dist/methods/handleElementPositionAttribute.d.ts +0 -6
  77. package/dist/methods/handleKeyboardDragPosDifference.d.ts +0 -13
  78. package/dist/methods/handleSelection.d.ts +0 -9
  79. package/dist/methods/handleUnSelection.d.ts +0 -9
  80. package/dist/methods/hydrateSettings.d.ts +0 -3
  81. package/dist/methods/index.d.ts +0 -30
  82. package/dist/methods/isCollision.d.ts +0 -13
  83. package/dist/methods/moveElement.d.ts +0 -8
  84. package/dist/methods/removeModificationObservers.d.ts +0 -3
  85. package/dist/methods/scrollElement.d.ts +0 -3
  86. package/dist/methods/setStylePosition.d.ts +0 -3
  87. package/dist/methods/subscriberAliases.d.ts +0 -14
  88. package/dist/methods/toArray.d.ts +0 -3
  89. package/dist/methods/updateElementStylePos.d.ts +0 -8
  90. package/dist/methods/vect2.d.ts +0 -8
  91. package/dist/modules/Area.d.ts +0 -80
  92. package/dist/modules/Drag.d.ts +0 -71
  93. package/dist/modules/DropZone.d.ts +0 -90
  94. package/dist/modules/DropZones.d.ts +0 -73
  95. package/dist/modules/Interaction.d.ts +0 -51
  96. package/dist/modules/PubSub.d.ts +0 -37
  97. package/dist/modules/SelectableSet.d.ts +0 -43
  98. package/dist/modules/SelectedSet.d.ts +0 -32
  99. package/dist/modules/Selection.d.ts +0 -54
  100. package/dist/modules/Selector.d.ts +0 -29
  101. package/dist/modules/SelectorArea.d.ts +0 -58
  102. package/dist/modules/index.d.ts +0 -11
  103. package/dist/stores/KeyStore.d.ts +0 -34
  104. package/dist/stores/PointerStore.d.ts +0 -83
  105. package/dist/stores/ScrollStore.d.ts +0 -38
  106. package/dist/stores/SettingsStore.d.ts +0 -39
  107. package/dist/stores/index.d.ts +0 -5
  108. package/dist/types.d.ts +0 -276
  109. package/jsconfig.json +0 -13
  110. package/rollup.config.js +0 -101
  111. package/src/DragSelect.js +0 -384
  112. package/src/methods/addModificationObservers.js +0 -38
  113. package/src/methods/canScroll.js +0 -20
  114. package/src/methods/getAllParentNodes.js +0 -17
  115. package/src/methods/getBoundingClientRect.js +0 -33
  116. package/src/methods/getCurrentScroll.js +0 -15
  117. package/src/methods/getPointerPos.js +0 -13
  118. package/src/methods/handleElementOverflow.js +0 -65
  119. package/src/methods/handleElementPositionAttribute.js +0 -16
  120. package/src/methods/handleKeyboardDragPosDifference.js +0 -57
  121. package/src/methods/handleSelection.js +0 -26
  122. package/src/methods/index.js +0 -29
  123. package/src/methods/isCollision.js +0 -58
  124. package/src/methods/moveElement.js +0 -39
  125. package/src/methods/removeModificationObservers.js +0 -13
  126. package/src/methods/subscriberAliases.js +0 -72
  127. package/src/methods/toArray.js +0 -18
  128. package/src/methods/updateElementStylePos.js +0 -15
  129. package/src/methods/vect2.js +0 -54
  130. package/src/modules/Area.js +0 -183
  131. package/src/modules/Drag.js +0 -257
  132. package/src/modules/DropZones.js +0 -161
  133. package/src/modules/Interaction.js +0 -208
  134. package/src/modules/PubSub.js +0 -78
  135. package/src/modules/SelectableSet.js +0 -149
  136. package/src/modules/SelectedSet.js +0 -72
  137. package/src/modules/Selector.js +0 -90
  138. package/src/modules/index.js +0 -10
  139. package/src/stores/KeyStore.js +0 -83
  140. package/src/stores/PointerStore.js +0 -218
  141. package/src/stores/ScrollStore.js +0 -92
  142. package/src/stores/SettingsStore.js +0 -66
  143. package/src/stores/index.js +0 -4
  144. package/src/types.js +0 -100
@@ -1,257 +0,0 @@
1
- // @ts-check
2
- import '../types'
3
- import DragSelect from '../DragSelect'
4
-
5
- import {
6
- getBoundingClientRect,
7
- handleKeyboardDragPosDifference,
8
- moveElement,
9
- vect2,
10
- } from '../methods'
11
-
12
- export default class Drag {
13
- /**
14
- * @type {Vect2}
15
- * @private
16
- */
17
- _prevCursorPos
18
-
19
- /**
20
- * @type {Vect2}
21
- * @private
22
- */
23
- _prevScrollPos
24
-
25
- /**
26
- * @type {DSElements}
27
- * @private
28
- */
29
- _elements = []
30
-
31
- /**
32
- * @type {DSDragKeys}
33
- * @private
34
- */
35
- _dragKeys
36
-
37
- /**
38
- * @type {string[]}
39
- * @private
40
- */
41
- _dragKeysFlat = []
42
-
43
- /**
44
- * @type {DSBoundingRect}
45
- * @private
46
- */
47
- _selectionRect
48
-
49
- /**
50
- * @constructor Drag
51
- * @param {{DS:DragSelect}} obj
52
- * @ignore
53
- */
54
- constructor({ DS }) {
55
- this.DS = DS
56
-
57
- // @ts-ignore: @todo: update to typescript
58
- this.DS.subscribe('Settings:updated:dragKeys', this.assignDragkeys)
59
- this.assignDragkeys()
60
- this.DS.subscribe('Interaction:start', this.start)
61
- this.DS.subscribe('Interaction:end', this.stop)
62
- this.DS.subscribe('Interaction:update', this.update)
63
- this.DS.subscribe('KeyStore:down', this.keyboardDrag)
64
- this.DS.subscribe('KeyStore:up', this.keyboardEnd)
65
- }
66
-
67
- assignDragkeys = () => {
68
- this._dragKeys = {
69
- up: this.DS.stores.SettingsStore.s.dragKeys.up.map((k) =>
70
- k.toLowerCase()
71
- ),
72
- down: this.DS.stores.SettingsStore.s.dragKeys.down.map((k) =>
73
- k.toLowerCase()
74
- ),
75
- left: this.DS.stores.SettingsStore.s.dragKeys.left.map((k) =>
76
- k.toLowerCase()
77
- ),
78
- right: this.DS.stores.SettingsStore.s.dragKeys.right.map((k) =>
79
- k.toLowerCase()
80
- ),
81
- }
82
- this._dragKeysFlat = [
83
- ...this._dragKeys.up,
84
- ...this._dragKeys.down,
85
- ...this._dragKeys.left,
86
- ...this._dragKeys.right,
87
- ]
88
- }
89
-
90
- keyboardDrag = ({ event, key }) => {
91
- const _key = key.toLowerCase()
92
- if (
93
- !this.DS.stores.SettingsStore.s.keyboardDrag ||
94
- !this._dragKeysFlat.includes(_key) ||
95
- !this.DS.SelectedSet.size ||
96
- !this.DS.stores.SettingsStore.s.draggability ||
97
- this.DS.continue
98
- )
99
- return
100
-
101
- const publishData = {
102
- event,
103
- isDragging: true,
104
- isDraggingKeyboard: true,
105
- }
106
- this.DS.publish(['Interaction:start:pre', 'Interaction:start'], publishData)
107
-
108
- this._elements = this.DS.getSelection()
109
- if (this.DS.stores.SettingsStore.s.dragAsBlock)
110
- this._selectionRect = getBoundingClientRect(this._elements, this.DS.SelectableSet)
111
- this.handleZIndex(true)
112
-
113
- let posDirection = handleKeyboardDragPosDifference({
114
- shiftKey: this.DS.stores.KeyStore.currentValues.includes('shift'),
115
- keyboardDragSpeed: this.DS.stores.SettingsStore.s.keyboardDragSpeed,
116
- zoom: this.DS.stores.SettingsStore.s.zoom,
117
- key: _key,
118
- scrollCallback: this.DS.Area.scroll,
119
- scrollDiff: this._scrollDiff,
120
- canScroll: this.DS.stores.ScrollStore.canScroll,
121
- dragKeys: this._dragKeys,
122
- })
123
-
124
- if (this.DS.stores.SettingsStore.s.dragAsBlock)
125
- posDirection = this.limitDirection(posDirection)
126
-
127
- this._elements.forEach((element) =>
128
- moveElement({
129
- element,
130
- posDirection,
131
- containerRect: this.DS.SelectorArea.rect,
132
- useTransform: this.DS.stores.SettingsStore.s.useTransform,
133
- })
134
- )
135
-
136
- this.DS.publish(
137
- ['Interaction:update:pre', 'Interaction:update'],
138
- publishData
139
- )
140
- }
141
-
142
- keyboardEnd = ({ event, key }) => {
143
- const _key = key.toLowerCase()
144
- if (
145
- !this.DS.stores.SettingsStore.s.keyboardDrag ||
146
- !this._dragKeysFlat.includes(_key) ||
147
- !this.DS.SelectedSet.size ||
148
- !this.DS.stores.SettingsStore.s.draggability
149
- )
150
- return
151
- const publishData = {
152
- event,
153
- isDragging: this.DS.stores.SettingsStore.s.draggability,
154
- isDraggingKeyboard: true,
155
- }
156
- this.DS.publish(['Interaction:end:pre', 'Interaction:end'], publishData)
157
- }
158
-
159
- start = ({ isDragging, isDraggingKeyboard }) => {
160
- if (!isDragging || isDraggingKeyboard) return
161
- this._prevCursorPos = null
162
- this._prevScrollPos = null
163
- this._elements = this.DS.getSelection()
164
- if (this.DS.stores.SettingsStore.s.dragAsBlock)
165
- this._selectionRect = getBoundingClientRect(this._elements, this.DS.SelectableSet)
166
- this.handleZIndex(true)
167
- }
168
-
169
- stop = (evt) => {
170
- if (evt?.isKeyboard) return
171
- this._prevCursorPos = null
172
- this._prevScrollPos = null
173
- this.handleZIndex(false)
174
- this._elements = []
175
- }
176
-
177
- update = ({ isDragging, isDraggingKeyboard }) => {
178
- if (
179
- !isDragging ||
180
- !this._elements.length ||
181
- isDraggingKeyboard ||
182
- this.DS.continue
183
- )
184
- return
185
-
186
- let posDirection = vect2.calc(this._cursorDiff, '+', this._scrollDiff)
187
- if (this.DS.stores.SettingsStore.s.dragAsBlock)
188
- posDirection = this.limitDirection(posDirection)
189
-
190
- this._elements.forEach((element) =>
191
- moveElement({
192
- element,
193
- posDirection,
194
- containerRect: this.DS.SelectorArea.rect,
195
- useTransform: this.DS.stores.SettingsStore.s.useTransform,
196
- })
197
- )
198
- }
199
-
200
- /**
201
- * Modify direction value so that the rect of draggable elements
202
- * does not exceed the boundaries of container rect
203
- * @param {Vect2} direction
204
- * @return {Vect2}
205
- */
206
- limitDirection = (direction) => {
207
- const containerRect = this.DS.SelectorArea.rect;
208
- const scrollAmount = this.DS.stores.ScrollStore.scrollAmount;
209
-
210
- const delta = {
211
- top: containerRect.top - this._selectionRect.top + scrollAmount.y,
212
- left: containerRect.left - this._selectionRect.left + scrollAmount.x,
213
- bottom: containerRect.bottom - this._selectionRect.bottom + scrollAmount.y,
214
- right: containerRect.right - this._selectionRect.right + scrollAmount.x,
215
- }
216
-
217
- if(direction.x === 0 && direction.y === 0) return direction
218
- if (direction.y < 0) direction.y = Math.max(direction.y, delta.top)
219
- if (direction.x < 0) direction.x = Math.max(direction.x, delta.left)
220
- if (direction.y > 0) direction.y = Math.min(direction.y, delta.bottom)
221
- if (direction.x > 0) direction.x = Math.min(direction.x, delta.right)
222
-
223
- this._selectionRect.top += direction.y;
224
- this._selectionRect.bottom += direction.y;
225
- this._selectionRect.left += direction.x;
226
- this._selectionRect.right += direction.x;
227
-
228
- return direction
229
- }
230
-
231
- handleZIndex = (add) => {
232
- this._elements.forEach(
233
- (element) =>
234
- (element.style.zIndex = `${
235
- (parseInt(element.style.zIndex) || 0) + add ? 9999 : -9998
236
- }`)
237
- )
238
- }
239
-
240
- get _cursorDiff() {
241
- const currentPointerVal = this.DS.stores.PointerStore.currentVal
242
- const cursorDiff = this._prevCursorPos
243
- ? vect2.calc(currentPointerVal, '-', this._prevCursorPos)
244
- : { x: 0, y: 0 }
245
- this._prevCursorPos = currentPointerVal
246
- return cursorDiff
247
- }
248
-
249
- get _scrollDiff() {
250
- const currentScrollVal = this.DS.stores.ScrollStore.currentVal
251
- const scrollDiff = this._prevScrollPos
252
- ? vect2.calc(currentScrollVal, '-', this._prevScrollPos)
253
- : { x: 0, y: 0 }
254
- this._prevScrollPos = currentScrollVal
255
- return scrollDiff
256
- }
257
- }
@@ -1,161 +0,0 @@
1
- // @ts-check
2
-
3
- import '../types'
4
- import DragSelect from '../DragSelect'
5
- import DropZone from './DropZone'
6
-
7
- import { isCollision } 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
- /**
18
- * Get the drop zone by the zone id
19
- * @type {Map<string, DropZone>}
20
- * @private
21
- */
22
- _zoneById = new Map()
23
-
24
- /**
25
- * Get the drop zones by one zone item
26
- * @type {Map<DSElement,DropZone[]>}
27
- * @private
28
- */
29
- _zonesByDroppable = new Map()
30
-
31
- /**
32
- * Get the drop zones by one zone item
33
- * @type {DropZone[]}
34
- * @private
35
- */
36
- _zones
37
-
38
- /**
39
- * @constructor Drag
40
- * @param {{DS:DragSelect}} obj
41
- * @ignore
42
- */
43
- constructor({ DS }) {
44
- this.DS = DS
45
- this.Settings = DS.stores.SettingsStore.s
46
-
47
- // @ts-ignore: @todo: update to typescript
48
- this.DS.subscribe('Settings:updated:dropZones', ({ settings }) =>
49
- this.setDropZones(settings)
50
- )
51
- this.setDropZones({
52
- dropZones: /** @type {DSDropZone[]} */ (this.DS.stores.SettingsStore.s
53
- .dropZones),
54
- })
55
-
56
- this.DS.subscribe('Interaction:end', this.stop)
57
- }
58
-
59
- /**
60
- * @param {Object} obj
61
- * @param {DSDropZone[]} [obj.dropZones]
62
- */
63
- setDropZones = ({ dropZones }) => {
64
- if (!dropZones) return
65
- if (this._zones) this._zones.forEach((zone) => zone.destroy())
66
-
67
- this._zones = dropZones.map(
68
- (zone) => new DropZone({ DS: this.DS, ...zone })
69
- )
70
- this._zones.forEach((zone) => {
71
- this._zoneByElement.set(zone.element, zone)
72
- this._zoneById.set(zone.id, zone)
73
- zone.droppables.forEach((droppable) => {
74
- const zones = this._zonesByDroppable.get(droppable)
75
- if (!zones?.length) return this._zonesByDroppable.set(droppable, [zone])
76
- // @ts-ignore
77
- this._zonesByDroppable.set(droppable, [...new Set([...zones, zone])])
78
- })
79
- })
80
- }
81
-
82
- _handleDrop = (target) => {
83
- this._zones.forEach((zone) => {
84
- if (zone === target) return
85
- zone.handleNoDrop()
86
- })
87
- if (!target) return
88
- target.handleDrop()
89
- }
90
-
91
- /**
92
- * @param {DSElements} elements
93
- * @param {Vect2} point
94
- * @returns {DropZone|undefined}
95
- */
96
- _getZoneByElementsFromPoint = (elements, { x, y }) => {
97
- for (let i = 0, il = elements.length; i < il; i++) {
98
- const zone = this._zoneByElement.get(elements[i])
99
- if (
100
- zone &&
101
- isCollision(
102
- zone.rect,
103
- { left: x, right: x, top: y, bottom: y },
104
- Math.min(this.Settings.dropTargetThreshold, 0.5)
105
- )
106
- ) {
107
- return zone
108
- }
109
- }
110
- }
111
-
112
- stop = ({ isDragging }) => {
113
- if (!isDragging) return
114
- const target = this.getTarget()
115
- this._handleDrop(target)
116
- }
117
-
118
- /// ///////////////////////////////////////////////////////////////////////////////////
119
- // Getters
120
-
121
- /**
122
- * @param {string} zoneId
123
- * @returns {DSElements|void}
124
- */
125
- getItemsDroppedById = (zoneId) => {
126
- const zone = this._zoneById.get(zoneId)
127
- if (!zone) return console.warn(`[DragSelect] No zone found (id: ${zoneId})`)
128
- return zone.itemsDropped
129
- }
130
-
131
- /**
132
- * @param {string} zoneId
133
- * @param {boolean} addClasses
134
- * @returns {DSElements|void}
135
- */
136
- getItemsInsideById = (zoneId, addClasses) => {
137
- const zone = this._zoneById.get(zoneId)
138
- if (!zone) return console.warn(`[DragSelect] No zone found (id: ${zoneId})`)
139
- const { itemsInside } = zone
140
- if (addClasses) zone.handleItemsInsideClasses()
141
- return itemsInside
142
- }
143
-
144
- /**
145
- * Returns first DropsZone under current pointer
146
- * @param {Vect2} [coordinates]
147
- * @returns {DropZone | undefined}
148
- */
149
- getTarget = (coordinates) => {
150
- if (!this._zones?.length) return
151
-
152
- const x = coordinates?.x || this.DS.stores.PointerStore.currentVal.x
153
- const y = coordinates?.y || this.DS.stores.PointerStore.currentVal.y
154
-
155
- const elements = document.elementsFromPoint(x, y)
156
- return this._getZoneByElementsFromPoint(
157
- /** @type {DSElements} */ (elements),
158
- { x, y }
159
- )
160
- }
161
- }
@@ -1,208 +0,0 @@
1
- // @ts-check
2
- import DragSelect from '../DragSelect'
3
- import '../types'
4
-
5
- export default class Interaction {
6
- /** @type {boolean} */
7
- isInteracting
8
-
9
- /** @type {boolean} */
10
- isDragging
11
-
12
- /**
13
- * @constructor Interaction
14
- * @param {{DS:DragSelect}} obj
15
- * @ignore
16
- */
17
- constructor({ DS }) {
18
- this.DS = DS
19
- this.Settings = DS.stores.SettingsStore.s
20
- // @ts-ignore: @todo: update to typescript
21
- this.DS.subscribe('Settings:updated:area', ({ settings }) => {
22
- this.stop(settings['area:pre'])
23
- this.init()
24
- })
25
- this.DS.subscribe('PointerStore:updated', this.update)
26
- this.DS.subscribe('Selectable:click', this.onClick)
27
- this.DS.subscribe('Selectable:pointer', ({ event }) => this.start(event))
28
- this.DS.subscribe('Interaction:start:pre', ({ event }) =>
29
- this._start(event)
30
- )
31
- this.DS.subscribe('Interaction:init:pre', this._init)
32
- this.DS.subscribe('Interaction:end:pre', ({ event }) => this._reset(event))
33
- this.DS.subscribe('Area:scroll', this.update)
34
- }
35
-
36
- init = () => this.DS.publish('Interaction:init:pre', {})
37
-
38
- _init = () => {
39
- this.stop()
40
-
41
- // @TODO: fix pointer events mixing issue see [PR](https://github.com/ThibaultJanBeyer/DragSelect/pull/128#issuecomment-1154885289)
42
- if (this.Settings.usePointerEvents)
43
- this.DS.Area.HTMLNode.addEventListener('pointerdown', this.start, {
44
- passive: false,
45
- })
46
- else this.DS.Area.HTMLNode.addEventListener('mousedown', this.start)
47
-
48
- this.DS.Area.HTMLNode.addEventListener('touchstart', this.start, {
49
- passive: false,
50
- })
51
- this.DS.publish('Interaction:init', {})
52
- }
53
-
54
- /**
55
- * @param {DSEvent} event
56
- */
57
- _canInteract(event) {
58
- const isKeyboardClick =
59
- /** @type {MouseEvent} */ (event).clientX === 0 &&
60
- /** @type {MouseEvent} */ (event).clientY === 0 &&
61
- /** @type {MouseEvent} */ (event).detail === 0 &&
62
- event.target
63
-
64
- if (
65
- /** @type {MouseEvent} */ (event).button === 2 || // right-clicks
66
- this.isInteracting || // fix double-click issues
67
- (event.target &&
68
- !this.DS.SelectorArea.isInside(
69
- /** @type {DSElement} */ (event.target)
70
- )) || // fix outside elements issue
71
- (!isKeyboardClick && !this.DS.SelectorArea.isClicked(event)) // make sure the mouse click is inside the area
72
- )
73
- return false
74
-
75
- return true
76
- }
77
-
78
- /**
79
- * @param {DSEvent} event
80
- */
81
- start = (event) =>
82
- this.DS.publish('Interaction:start:pre', {
83
- event,
84
- isDragging: this.isDragging,
85
- })
86
-
87
- _start = (event) => {
88
- 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
89
- if (!this._canInteract(event)) return
90
-
91
- this.isInteracting = true
92
- this.isDragging = this.isDragEvent(event)
93
-
94
- this.DS.publish('Interaction:start', { event, isDragging: this.isDragging })
95
-
96
- // @TODO: fix pointer events mixing issue see [PR](https://github.com/ThibaultJanBeyer/DragSelect/pull/128#issuecomment-1154885289)
97
- if (this.Settings.usePointerEvents) {
98
- document.addEventListener('pointerup', this.reset)
99
- document.addEventListener('pointercancel', this.reset)
100
- } else document.addEventListener('mouseup', this.reset)
101
-
102
- document.addEventListener('touchend', this.reset)
103
- }
104
-
105
- /**
106
- * Drag interaction
107
- * @param {DSEvent} event
108
- * @returns {boolean}
109
- */
110
- isDragEvent = (event) => {
111
- const clickedElement = /** @type {Element} */ (event.target).closest(
112
- `.${this.Settings.selectableClass}`
113
- )
114
-
115
- if (
116
- !this.Settings.draggability ||
117
- this.DS.stores.KeyStore.isMultiSelectKeyPressed(event) ||
118
- !clickedElement
119
- )
120
- return false
121
-
122
- if (this.Settings.immediateDrag) {
123
- if (!this.DS.SelectedSet.size)
124
- this.DS.SelectedSet.add(/** @type {DSElement} */ (clickedElement))
125
- else if (!this.DS.SelectedSet.has(clickedElement)) {
126
- this.DS.SelectedSet.clear()
127
- this.DS.SelectedSet.add(/** @type {DSElement} */ (clickedElement))
128
- }
129
- }
130
-
131
- if (this.DS.SelectedSet.has(clickedElement)) return true
132
-
133
- return false
134
- }
135
-
136
- /**
137
- * Triggers when a node is actively selected: <button> nodes that are pressed via the keyboard.
138
- * Making DragSelect accessible for everyone!
139
- * @param {{ event:MouseEvent }} prop
140
- */
141
- onClick = ({ event }) => {
142
- if (!this._canInteract(event)) return
143
- if (event.detail > 0) return // mouse interaction
144
-
145
- const {
146
- stores: { PointerStore, KeyStore },
147
- SelectableSet,
148
- SelectedSet,
149
- } = this.DS
150
-
151
- PointerStore.start(event)
152
-
153
- const node = /** @type {any} */ (event.target)
154
- if (!SelectableSet.has(node)) return
155
-
156
- if (!KeyStore.isMultiSelectKeyPressed(event)) SelectedSet.clear()
157
- SelectedSet.toggle(node)
158
-
159
- this.reset() // simulate mouse-up (that does not exist on keyboard)
160
- }
161
-
162
- stop = (area = this.DS.Area.HTMLNode) => {
163
- this.isInteracting = false
164
- this.isDragging = false
165
-
166
- // @TODO: fix pointer events mixing issue see [PR](https://github.com/ThibaultJanBeyer/DragSelect/pull/128#issuecomment-1154885289)
167
- if (this.Settings.usePointerEvents) {
168
- area.removeEventListener('pointerdown', this.start, {
169
- // @ts-ignore
170
- passive: false,
171
- })
172
- document.removeEventListener('pointerup', this.reset)
173
- document.removeEventListener('pointercancel', this.reset)
174
- } else {
175
- area.removeEventListener('mousedown', this.start)
176
- document.removeEventListener('mouseup', this.reset)
177
- }
178
-
179
- area.removeEventListener('touchstart', this.start, {
180
- // @ts-ignore
181
- passive: false,
182
- })
183
- document.removeEventListener('touchend', this.reset)
184
- }
185
-
186
- update = ({ event, scroll_directions, scroll_multiplier }) => {
187
- if (this.isInteracting)
188
- this.DS.publish(['Interaction:update:pre', 'Interaction:update'], {
189
- event,
190
- scroll_directions,
191
- scroll_multiplier,
192
- isDragging: this.isDragging,
193
- })
194
- }
195
-
196
- reset = (event) =>
197
- this.DS.publish('Interaction:end:pre', {
198
- event,
199
- isDragging: this.isDragging,
200
- })
201
-
202
- _reset = (event) => {
203
- const { isDragging } = this
204
- this.stop()
205
- this.init()
206
- this.DS.publish('Interaction:end', { event, isDragging })
207
- }
208
- }
@@ -1,78 +0,0 @@
1
- // @ts-check
2
- import '../types'
3
-
4
- export default class PubSub {
5
- subscribers = {}
6
-
7
- constructor({ DS }) {
8
- this.DS = DS
9
- }
10
-
11
- /**
12
- * Subscribe to an event
13
- * @memberof DragSelect#
14
- * @function subscribe
15
- * @param {DSCallbackNames} eventName
16
- * @param {DSCallback} callback
17
- * @returns {number} event id, can be used to unsubscribe more efficiently
18
- */
19
- subscribe = (eventName, callback) => {
20
- if (!Array.isArray(this.subscribers[eventName]))
21
- this.subscribers[eventName] = []
22
- this.subscribers[eventName].push(callback)
23
- return this.subscribers[eventName].length - 1
24
- }
25
-
26
- /**
27
- * Removes event subscription
28
- * @memberof DragSelect#
29
- * @function unsubscribe
30
- * @param {DSCallbackNames} eventName
31
- * @param {DSCallback} [callback] the callback method signature, has to be exactly the same as when subscribing. Consider using "id" instead.
32
- * @param {number} [id] event id returned when subscribed (more performant than callback search)
33
- */
34
- unsubscribe = (eventName, callback, id) => {
35
- if (id >= 0) this.subscribers[eventName].splice(id, 1)
36
- else if (callback)
37
- this.subscribers[eventName] = this.subscribers[eventName].filter(
38
- (cb) => cb !== callback
39
- )
40
- }
41
-
42
- /**
43
- * Publishes an event to all subscribers
44
- * @memberof DragSelect#
45
- * @function publish
46
- * @param {DSCallbackNames|DSCallbackNames[]} eventName
47
- * @param {CallbackObject} data passed to the subscription method
48
- */
49
- publish = (eventName, data) => {
50
- if (Array.isArray(eventName))
51
- eventName.forEach((name) => this._publish(name, data))
52
- else this._publish(eventName, data)
53
- }
54
-
55
- _publish = (eventName, data) => {
56
- const subscribers = this.subscribers[eventName]
57
- if (!Array.isArray(subscribers)) return
58
- if (eventName.includes(`:pre`)) this._handlePrePublish(subscribers, data)
59
- else this._handlePublish(subscribers, data)
60
- }
61
-
62
- // non-pre events are executed first in first out
63
- _handlePublish = (subscribers, data) => {
64
- for (let i = 0, il = subscribers.length; i < il; i++) {
65
- if (this.DS.stopped) return
66
- subscribers[i](data)
67
- }
68
- }
69
-
70
- // pre events are executed last in first out (so user callbacks are called before DS callbacks)
71
- _handlePrePublish = (subscribers, data) => {
72
- let i = subscribers.length
73
- while (i--) {
74
- if (this.DS.stopped) return
75
- subscribers[i](data)
76
- }
77
- }
78
- }