dragselect 2.7.3 → 3.0.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.
Files changed (144) hide show
  1. package/CHANGELOG.md +100 -22
  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 -3877
  58. package/dist/DragSelect.js +0 -3885
  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 -40
  98. package/dist/modules/SelectedSet.d.ts +0 -29
  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 -144
  136. package/src/modules/SelectedSet.js +0 -69
  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
@@ -0,0 +1,169 @@
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
+ }
@@ -0,0 +1,236 @@
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
+ }