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
@@ -0,0 +1,194 @@
1
+ import DragSelect from '../DragSelect'
2
+ import PubSub from '../modules/PubSub'
3
+ import { DSEvent, DSInputElement, Settings, Vect2 } from '../types'
4
+ import { getPointerPos } from '../methods/getPointerPos'
5
+ import { rect2vect, calcVect } from '../methods/vect2'
6
+ import { InteractionEvent } from '../modules/Interaction'
7
+
8
+ export type DSPointerStorePublishEventNames =
9
+ | 'PointerStore:updated:pre'
10
+ | 'PointerStore:updated'
11
+
12
+ export type DSPointerStorePublishEventData = {
13
+ event: PointerEvent | MouseEvent | TouchEvent
14
+ }
15
+
16
+ export type DSPointerStorePublish = {
17
+ [K in DSPointerStorePublishEventNames]: DSPointerStorePublishEventData
18
+ }
19
+
20
+ export default class PointerStore<E extends DSInputElement> {
21
+ private _isMouseInteraction = false
22
+ // Position relative to area
23
+ private _initialValArea: Vect2 = { x: 0, y: 0 }
24
+ private _currentValArea: Vect2 = { x: 0, y: 0 }
25
+ private _lastValArea: Vect2 = { x: 0, y: 0 }
26
+ // General Pointer Position
27
+ private _initialVal: Vect2 = { x: 0, y: 0 }
28
+ private _currentVal: Vect2 = { x: 0, y: 0 }
29
+ private _lastVal: Vect2 = { x: 0, y: 0 }
30
+ private _lastTouch?: TouchEvent
31
+
32
+ private DS: DragSelect<E>
33
+ private PS: PubSub<E>
34
+ private settings: Required<Settings<E>>
35
+
36
+ constructor({ DS, PS }: { DS: DragSelect<E>; PS: PubSub<E> }) {
37
+ this.DS = DS
38
+ this.PS = PS
39
+ this.settings = this.DS.stores.SettingsStore.s
40
+ this.PS.subscribe('Interaction:init', this.init)
41
+ this.PS.subscribe('Interaction:start', ({ event }) => this.start(event))
42
+ this.PS.subscribe('Interaction:end', ({ event }) => this.reset(event))
43
+ }
44
+
45
+ private init = () => {
46
+ if (this.settings.usePointerEvents)
47
+ document.addEventListener('pointermove', this.update, { passive: false })
48
+ else document.addEventListener('mousemove', this.update)
49
+
50
+ document.addEventListener('touchmove', this.update, { passive: false })
51
+ }
52
+
53
+ public start(event?: DSEvent) {
54
+ if (!event) return
55
+ this._isMouseInteraction = true
56
+ this.currentVal = this.initialVal = this.getPointerPosition(event)
57
+ }
58
+
59
+ public getPointerPosition = (event?: DSEvent) =>
60
+ getPointerPos({
61
+ event: this._normalizedEvent(event),
62
+ })
63
+
64
+ private update = (event: Event) => {
65
+ // type Event to satisfy event listeners, but we know type is : event as InteractionEvent
66
+ if (!event) return
67
+ this.PS.publish('PointerStore:updated:pre', {
68
+ event: event as InteractionEvent,
69
+ })
70
+ this.currentVal = this.getPointerPosition(event as InteractionEvent)
71
+ if (!this._isMouseInteraction) return
72
+ this.PS.publish('PointerStore:updated', {
73
+ event: event as InteractionEvent,
74
+ })
75
+ }
76
+
77
+ public stop = () => {
78
+ // @TODO: fix pointer events mixing issue see [PR](https://github.com/ThibaultJanBeyer/DragSelect/pull/128#issuecomment-1154885289)
79
+ if (this.settings.usePointerEvents)
80
+ document.removeEventListener('pointermove', this.update, {
81
+ // @ts-ignore
82
+ passive: false,
83
+ })
84
+ else document.removeEventListener('mousemove', this.update)
85
+
86
+ document.removeEventListener('touchmove', this.update, {
87
+ // @ts-ignore
88
+ passive: false,
89
+ })
90
+
91
+ this.reset()
92
+ }
93
+
94
+ private reset = (event?: DSEvent) => {
95
+ this.currentVal = this.lastVal = this.getPointerPosition(event)
96
+ // debounce in order "onClick" to work
97
+ setTimeout(() => (this._isMouseInteraction = false), 100)
98
+ }
99
+
100
+ private _normalizedEvent(event?: DSEvent): MouseEvent | PointerEvent | Touch {
101
+ // null KeyboardEvents
102
+ if (!event || event instanceof KeyboardEvent)
103
+ return { clientX: 0, clientY: 0 } as MouseEvent
104
+ // touchend has not touches. so we take the last touch if a touchevent, we need to store the positions
105
+ if ('touches' in event) {
106
+ if (event.type !== 'touchend') this._lastTouch = event
107
+ // if a touchevent, return the last touch rather than the regular event
108
+ // as we need .touches[0] from that event
109
+ return this._lastTouch?.touches[0] || event.touches[0]
110
+ }
111
+ return event
112
+ }
113
+
114
+ /** First recorded pointer position within the area */
115
+ public get initialValArea() {
116
+ if (!this._initialValArea) return { x: 0, y: 0 }
117
+ return this._initialValArea
118
+ }
119
+
120
+ /** Current pointer position within the area */
121
+ public get currentValArea() {
122
+ if (!this._currentValArea) return { x: 0, y: 0 }
123
+ return this._currentValArea
124
+ }
125
+
126
+ /** Last recorded pointer position within the area */
127
+ public get lastValArea() {
128
+ if (!this._lastValArea) return { x: 0, y: 0 }
129
+ return this._lastValArea
130
+ }
131
+
132
+ /** First recorded pointer position */
133
+ public get initialVal() {
134
+ if (!this._initialVal) return { x: 0, y: 0 }
135
+ return this._initialVal
136
+ }
137
+
138
+ /** Current pointer position */
139
+ public get currentVal() {
140
+ if (!this._currentVal) return { x: 0, y: 0 }
141
+ return this._currentVal
142
+ }
143
+
144
+ /** Last recorded pointer position */
145
+ public get lastVal() {
146
+ if (!this._lastVal) return { x: 0, y: 0 }
147
+ return this._lastVal
148
+ }
149
+
150
+ public set initialVal(value) {
151
+ this._initialVal = value
152
+ this._initialValArea =
153
+ value &&
154
+ calcVect(
155
+ value,
156
+ '-',
157
+ calcVect(
158
+ rect2vect(this.DS.Area.rect),
159
+ '+',
160
+ rect2vect(this.DS.Area.computedBorder)
161
+ )
162
+ )
163
+ }
164
+
165
+ public set currentVal(value) {
166
+ this._currentVal = value
167
+ this._currentValArea =
168
+ value &&
169
+ calcVect(
170
+ value,
171
+ '-',
172
+ calcVect(
173
+ rect2vect(this.DS.Area.rect),
174
+ '+',
175
+ rect2vect(this.DS.Area.computedBorder)
176
+ )
177
+ )
178
+ }
179
+
180
+ public set lastVal(value) {
181
+ this._lastVal = value
182
+ this._lastValArea =
183
+ value &&
184
+ calcVect(
185
+ value,
186
+ '-',
187
+ calcVect(
188
+ rect2vect(this.DS.Area.rect),
189
+ '+',
190
+ rect2vect(this.DS.Area.computedBorder)
191
+ )
192
+ )
193
+ }
194
+ }
@@ -0,0 +1,87 @@
1
+ import DragSelect from '../DragSelect'
2
+ import PubSub from '../modules/PubSub'
3
+ import { DSInputElement, Settings, Vect2 } from '../types'
4
+ import { calcVect, num2vect } from '../methods/vect2'
5
+ import { canScroll } from '../methods/canScroll'
6
+ import { getCurrentScroll } from '../methods/getCurrentScroll'
7
+
8
+ export default class ScrollStore<E extends DSInputElement> {
9
+ private _initialVal: Vect2 = { x: 0, y: 0 }
10
+ private _currentVal: Vect2 = { x: 0, y: 0 }
11
+ private _canScroll?: boolean
12
+ private DS: DragSelect<E>
13
+ private PS: PubSub<E>
14
+ private Settings: Required<Settings<E>>
15
+
16
+ constructor({ DS, PS }: { DS: DragSelect<E>; PS: PubSub<E> }) {
17
+ this.DS = DS
18
+ this.PS = PS
19
+ this.Settings = this.DS.stores.SettingsStore.s
20
+ this.PS.subscribe('Area:modified', () => {
21
+ this.stop()
22
+ this.init()
23
+ })
24
+ this.PS.subscribe('Interaction:init', this.init)
25
+ this.PS.subscribe('Interaction:start', () => this.start())
26
+ this.PS.subscribe('Interaction:end', () => this.reset())
27
+ }
28
+
29
+ private init = () => this.addListeners()
30
+
31
+ private addListeners = () =>
32
+ this.DS.Area.HTMLNode.addEventListener('scroll', this.update)
33
+ private removeListeners = () =>
34
+ this.DS.Area.HTMLNode.removeEventListener('scroll', this.update)
35
+
36
+ private start = () => {
37
+ this._currentVal = this._initialVal = getCurrentScroll(
38
+ this.DS.Area.HTMLNode
39
+ )
40
+ }
41
+
42
+ private update = () =>
43
+ (this._currentVal = getCurrentScroll(this.DS.Area.HTMLNode))
44
+
45
+ public stop = () => {
46
+ this.reset()
47
+ this.removeListeners()
48
+ }
49
+
50
+ private reset = () => {
51
+ this._initialVal = { x: 0, y: 0 }
52
+ this._canScroll = undefined
53
+ }
54
+
55
+ public get canScroll() {
56
+ if (typeof this._canScroll === 'boolean') return this._canScroll
57
+ return (this._canScroll = canScroll(this.DS.Area.HTMLNode))
58
+ }
59
+
60
+ public get scrollAmount() {
61
+ const scrollDiff = calcVect(this.currentVal, '-', this.initialVal)
62
+
63
+ // if area is zoomed, the scroll values are skewed, we need to fix that manually :(
64
+ const zoom = num2vect(this.Settings.zoom)
65
+ const zoomScroll = calcVect(
66
+ calcVect(scrollDiff, '*', zoom),
67
+ '-',
68
+ scrollDiff
69
+ )
70
+
71
+ return {
72
+ x: scrollDiff.x + zoomScroll.x,
73
+ y: scrollDiff.y + zoomScroll.y,
74
+ }
75
+ }
76
+
77
+ private get initialVal() {
78
+ if (!this._initialVal) return { x: 0, y: 0 }
79
+ return this._initialVal
80
+ }
81
+
82
+ public get currentVal() {
83
+ if (!this._currentVal)
84
+ this._currentVal = getCurrentScroll(this.DS.Area.HTMLNode)
85
+ return this._currentVal
86
+ }
87
+ }
@@ -0,0 +1,88 @@
1
+ import { DSInputElement, Settings } from '../types'
2
+ import DragSelect from '../DragSelect'
3
+
4
+ import { hydrateSettings } from '../methods/hydrateSettings'
5
+ import PubSub from '../modules/PubSub'
6
+
7
+ type WithPostfix<T, Postfix extends string> = {
8
+ [K in keyof T as K | `${string & K}${Postfix}`]: T[K];
9
+ }
10
+
11
+ export type DSSettings<E extends DSInputElement> = WithPostfix<Required<Settings<E>>, ':pre'>
12
+
13
+ export type DSSettingsPublishEventNames = "Settings:updated:pre"|"Settings:updated"|`Settings:updated:${string}:pre`|`Settings:updated:${string}`
14
+
15
+ export type DSSettingsPublishEventData<E extends DSInputElement> = {
16
+ /** whether this is the initial settings call */
17
+ 'settings:init': boolean;
18
+ /** the settings being updates/manipulated/passed, holds all settings including the previous value. i.e. updating selectorClass will publish { settings: { ...settings, selectorClass: 'newVal', 'selectorClass:pre': 'oldVal' } } this is a deep cloned copy so manipulating it will have no effect */
19
+ settings: DSSettings<E>;
20
+ /** the new settings that are passed, not available on the initial settings call */
21
+ 'settings:new'?: Settings<E>;
22
+ };
23
+
24
+ export type DSSettingsPublish<E extends DSInputElement> = {
25
+ [K in DSSettingsPublishEventNames]: DSSettingsPublishEventData<E>;
26
+ }
27
+
28
+ export default class SettingsStore<E extends DSInputElement> {
29
+ private _settings: DSSettings<E> = {} as DSSettings<E>
30
+ /**
31
+ * Holds the settings and their previous value `:pre`
32
+ * @example {
33
+ * autoScrollSpeed: 3,
34
+ * 'autoScrollSpeed:pre': 5
35
+ * }
36
+ **/
37
+ s: DSSettings<E> = {} as DSSettings<E>
38
+ private PS: PubSub<E>
39
+
40
+ /**
41
+ * @class ScrollStore
42
+ * @constructor ScrollStore
43
+ * @param {{ DS:DragSelect, settings:Settings }} p
44
+ * @ignore
45
+ */
46
+ constructor({
47
+ PS,
48
+ settings
49
+ }: { PS: PubSub<E>, settings: Settings<E> }) {
50
+ this.PS = PS
51
+ this.update({ settings, init: true })
52
+ }
53
+
54
+ update = ({ settings, init }: { settings: Settings<E>, init?: boolean }) => {
55
+ this.PS.publish('Settings:updated:pre', {
56
+ settings: this._settings,
57
+ 'settings:init': Boolean(init),
58
+ 'settings:new': settings
59
+ })
60
+ this._update({ settings, init })
61
+ }
62
+
63
+ private _update = ({ settings = {}, init = false }: { settings?: Settings<E>, init?: boolean }) => {
64
+ const _settings = hydrateSettings(settings, init)
65
+
66
+ for (const [key, value] of Object.entries(_settings) as [keyof Settings<E>, DSSettings<E>[keyof Settings<E>]][]) {
67
+ (<K extends keyof Settings<E>>(key: K, value: DSSettings<E>[K]) => {
68
+ if (!(key in this._settings)) {
69
+ Object.defineProperty(this.s, key, {
70
+ get: () => this._settings[key],
71
+ set: (newValue) => this.update({ settings: { [key]: newValue } }),
72
+ })
73
+ }
74
+
75
+ this._settings[`${key}:pre` as K] = this._settings[key] // need to cast as K, see https://stackoverflow.com/a/76070928/3712591
76
+ this._settings[key] = value
77
+
78
+ const update = {
79
+ settings: this._settings,
80
+ 'settings:init': init,
81
+ 'settings:new': settings
82
+ }
83
+ this.PS.publish('Settings:updated', update)
84
+ this.PS.publish(`Settings:updated:${key}`, update)
85
+ })(key, value); // method used for typecasting, see https://stackoverflow.com/q/76036535/3712591
86
+ }
87
+ }
88
+ }
package/src/types.ts ADDED
@@ -0,0 +1,158 @@
1
+ import { DSPublicPublishAdditionalEventData, DSPublicPublishEventNames } from "./methods/subscriberAliases"
2
+ import { DSAreaPublishEventData, DSAreaPublishEventNames } from "./modules/Area"
3
+ import { DSInteractionPublishEventData, DSInteractionPublishEventNames } from "./modules/Interaction"
4
+ import { DSSelectablePublishEventData, DSSelectablePublishEventNames } from "./modules/SelectableSet"
5
+ import { DSSelectedPublishEventData, DSSelectedPublishEventNames } from "./modules/SelectedSet"
6
+ import { DSKeyStorePublishEventData, DSKeyStorePublishEventNames } from "./stores/KeyStore"
7
+ import { DSPointerStorePublishEventData, DSPointerStorePublishEventNames } from "./stores/PointerStore"
8
+ import { DSSettingsPublishEventData, DSSettingsPublishEventNames } from "./stores/SettingsStore"
9
+
10
+ export type Vect2 = {
11
+ x: number,
12
+ y: number
13
+ }
14
+
15
+ export type Settings<E extends DSInputElement> = {
16
+ /** area in which you can drag. If not provided it will be the whole document. Default Document */
17
+ area?: HTMLElement | SVGElement | Document
18
+ /** [=[]] the elements that can be selected. */
19
+ selectables?: E | E[]
20
+ /** [=5] Speed in which the area scrolls while selecting (if available). Unit is pixel per movement. */
21
+ autoScrollSpeed?: number
22
+ /** [={x:25,y:25}] Tolerance for autoScroll (how close one has to be near an edges for autoScroll to start). */
23
+ overflowTolerance?: Vect2
24
+ /** [=1] Zoom scale factor (in case of using CSS style transform: scale() which messes with real positions). Unit scale zoom. */
25
+ zoom?: number
26
+ /** [=false] if set to true, no styles (except for position absolute) will be applied by default */
27
+ customStyles?: boolean
28
+ /** [=false] Add newly selected elements to the selection instead of replacing them */
29
+ multiSelectMode?: boolean
30
+ /** [=true] Whether or not to toggle already active elements while multi-selecting */
31
+ multiSelectToggling?: boolean
32
+ /** [=['Control', 'Shift', 'Meta']] Keys that allows switching to the multi-select mode (see the multiSelectMode option). Any key value is possible ([see MDN docs](https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/key)). Note that the best support is given for <kbd>Control</kbd>, <kbd>Shift</kbd> and <kbd>Meta</kbd>. Provide an empty array `[]` if you want to turn off the functionality. */
33
+ multiSelectKeys?: DSMultiSelectKeys
34
+ /** [=HTMLElement] the square that will draw the selection */
35
+ selector?: HTMLElement
36
+ /** [=0] how much % of the element has to be selected to be considered selected (0 = just touching, 1 = inside the selection) */
37
+ selectionThreshold?: number
38
+ /** [=true] When a user is dragging on an already selected element, the selection is dragged. */
39
+ draggability?: boolean
40
+ /** [=true] Whether an element is draggable from the start or needs to be selected first */
41
+ immediateDrag?: boolean
42
+ /** [=true] Whether or not the user can drag with the keyboard (we don't recommend disabling it) */
43
+ keyboardDrag?: boolean
44
+ /** [={up:['ArrowUp'],down:['ArrowDown'],left:['ArrowLeft'],righ:['ArrowRight']}] The keys available to drag element using the keyboard. */
45
+ dragKeys?: DSDragKeys
46
+ /** [=10] The speed at which elements are dragged using the keyboard. In pixels per keydown. */
47
+ keyboardDragSpeed?: number
48
+ /** [=true] Whether to use hardware accelerated css transforms when dragging or top/left instead */
49
+ useTransform?: boolean
50
+ /** [=80] Refresh rate on memoization, higher numbers mean better performance but more lag if elements are moving, lower numbers mean less lag but worse performance. If none of your DOMNodes are moving, you can set it to a very high number to increase performance. Value in milliseconds. */
51
+ refreshMemoryRate?: number
52
+ /** [=[]] one or more drop-elements: where the selectables can be dropped into */
53
+ dropZones?: DSInputDropZone<E>[]
54
+ /** [=1] how much % of the item has to be inside the dropzone to be considered inside (0 = barely touching, 1 = completely inside) */
55
+ dropInsideThreshold?: number
56
+ /** [=0] how much % of the zone does the pointer has to be in to be considered a target (0 = anywhere in the zone, max: 0.5 = has to point at the center of the zone) */
57
+ dropTargetThreshold?: number
58
+ /** [=false] Whether to use Pointer Events to replace traditional Mouse or Touch Events. Useful for tools like Google Blockly. */
59
+ usePointerEvents?: boolean
60
+ /** [=ds-hover] the class assigned to the mouse hovered items */
61
+ hoverClass?: string
62
+ /** [=ds-selectable] the class assigned to the elements that can be selected */
63
+ selectableClass?: string
64
+ /** [=ds-selected] the class assigned to the selected items */
65
+ selectedClass?: string
66
+ /** [=ds-selector] the class assigned to the square selector helper */
67
+ selectorClass?: string
68
+ /** [=ds-selector-area] the class assigned to the square in which the selector resides. By default it's invisible */
69
+ selectorAreaClass?: string
70
+ /** [=ds-dropped-target] on an item corresponding the target dropzone. This is also the prefix for ds-dropped-target-${zone.id} */
71
+ droppedTargetClass?: string
72
+ /** [=ds-dropped-inside] on an item that is within its dropzone bounds after a drop. This is also the prefix for ds-dropped-inside-${zone.id} */
73
+ droppedInsideClass?: string
74
+ /** [=ds-droppable] on element that can be dropped into at least one container. This is also the prefix for ds-droppable-${zone.id} */
75
+ droppableClass?: string
76
+ /** [=ds-dropzone] on each dropZone */
77
+ dropZoneClass?: string
78
+ /** [=ds-dropzone-ready] on corresponding dropZone when element is dragged */
79
+ dropZoneReadyClass?: string
80
+ /** [=ds-dropzone-target] on dropZone that has elements from any successful target drop */
81
+ dropZoneTargetClass?: string
82
+ /** [=ds-dropzone-inside] on dropZone that has elements inside after any drop */
83
+ dropZoneInsideClass?: string
84
+ }
85
+
86
+ export type DSCallbackObject<E extends DSInputElement> =
87
+ Readonly<
88
+ Partial<
89
+ & DSSettingsPublishEventData<E>
90
+ & DSAreaPublishEventData<E>
91
+ & DSKeyStorePublishEventData
92
+ & DSPointerStorePublishEventData
93
+ & DSInteractionPublishEventData
94
+ & DSSelectablePublishEventData<E>
95
+ & DSSelectedPublishEventData<E>
96
+ & DSPublicPublishAdditionalEventData<E>
97
+ >
98
+ >
99
+
100
+ export type DSInputDropZone<E extends DSInputElement> = {
101
+ /** can be any unique identifier of type string */
102
+ id: string
103
+ /** the dropzone itself */
104
+ element: E // @TODO this would not be of type InputElement!
105
+ /** elements that can be dropped into that zone. This is optional, by default it will be all selectables */
106
+ droppables?: E[]
107
+ }
108
+
109
+ export type DSElementPos = {
110
+ x: number,
111
+ y: number,
112
+ w: number,
113
+ h: number,
114
+ r: number,
115
+ b: number
116
+ }
117
+
118
+ export type DSEdges = Array<'top'|'bottom'|'left'|'right'|undefined>
119
+ export type DSEdgesObj = {top:number,bottom:number,left:number,right:number}
120
+
121
+ /** area within which you can drag */
122
+ export type DSArea = HTMLElement|SVGElement|Document
123
+
124
+ /** area in which you can drag */
125
+ export type DSSelectorArea = HTMLElement
126
+
127
+ /** elements that can be selected */
128
+ export type DSInputElement = HTMLElement | SVGElement
129
+
130
+ /** element that can be selected */
131
+ // export type DSElement = HTMLElement|SVGElement
132
+
133
+ /** An array of keys that allows switching to the multi-select mode */
134
+ export type DSMultiSelectKeys = Array<'Shift'|'Control'|'Meta'|string>
135
+
136
+ export type DSEvent = KeyboardEvent|MouseEvent|PointerEvent|TouchEvent
137
+
138
+ export type DSInternalEventName = DSSettingsPublishEventNames|DSAreaPublishEventNames|DSKeyStorePublishEventNames|DSPointerStorePublishEventNames|DSInteractionPublishEventNames|DSSelectablePublishEventNames|DSSelectedPublishEventNames
139
+ export type DSCallbackName<E extends DSInputElement> = DSPublicPublishEventNames<E>|DSInternalEventName
140
+
141
+ export type DSBoundingRectBase = {
142
+ top: number,
143
+ left: number,
144
+ bottom: number,
145
+ right: number,
146
+ }
147
+
148
+ export interface DSBoundingRect extends DSBoundingRectBase {
149
+ width: number,
150
+ height: number
151
+ }
152
+
153
+ export type DSDragKeys = {
154
+ up: Array<string>,
155
+ down: Array<string>,
156
+ left: Array<string>,
157
+ right: Array<string>
158
+ }
package/tsconfig.json ADDED
@@ -0,0 +1,105 @@
1
+ {
2
+ "compilerOptions": {
3
+ /* Visit https://aka.ms/tsconfig to read more about this file */
4
+
5
+ /* Projects */
6
+ // "incremental": true, /* Save .tsbuildinfo files to allow for incremental compilation of projects. */
7
+ // "composite": true, /* Enable constraints that allow a TypeScript project to be used with project references. */
8
+ // "tsBuildInfoFile": "./.tsbuildinfo", /* Specify the path to .tsbuildinfo incremental compilation file. */
9
+ // "disableSourceOfProjectReferenceRedirect": true, /* Disable preferring source files instead of declaration files when referencing composite projects. */
10
+ // "disableSolutionSearching": true, /* Opt a project out of multi-project reference checking when editing. */
11
+ // "disableReferencedProjectLoad": true, /* Reduce the number of projects loaded automatically by TypeScript. */
12
+
13
+ /* Language and Environment */
14
+ "target": "ESNext", /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */
15
+ // "lib": [], /* Specify a set of bundled library declaration files that describe the target runtime environment. */
16
+ // "jsx": "preserve", /* Specify what JSX code is generated. */
17
+ // "experimentalDecorators": true, /* Enable experimental support for TC39 stage 2 draft decorators. */
18
+ // "emitDecoratorMetadata": true, /* Emit design-type metadata for decorated declarations in source files. */
19
+ // "jsxFactory": "", /* Specify the JSX factory function used when targeting React JSX emit, e.g. 'React.createElement' or 'h'. */
20
+ // "jsxFragmentFactory": "", /* Specify the JSX Fragment reference used for fragments when targeting React JSX emit e.g. 'React.Fragment' or 'Fragment'. */
21
+ // "jsxImportSource": "", /* Specify module specifier used to import the JSX factory functions when using 'jsx: react-jsx*'. */
22
+ // "reactNamespace": "", /* Specify the object invoked for 'createElement'. This only applies when targeting 'react' JSX emit. */
23
+ // "noLib": true, /* Disable including any library files, including the default lib.d.ts. */
24
+ // "useDefineForClassFields": true, /* Emit ECMAScript-standard-compliant class fields. */
25
+ // "moduleDetection": "auto", /* Control what method is used to detect module-format JS files. */
26
+
27
+ /* Modules */
28
+ "module": "esnext", /* Specify what module code is generated. */
29
+ // "rootDir": "./", /* Specify the root folder within your source files. */
30
+ // "moduleResolution": "node", /* Specify how TypeScript looks up a file from a given module specifier. */
31
+ // "baseUrl": "./", /* Specify the base directory to resolve non-relative module names. */
32
+ // "paths": {}, /* Specify a set of entries that re-map imports to additional lookup locations. */
33
+ // "rootDirs": [], /* Allow multiple folders to be treated as one when resolving modules. */
34
+ // "typeRoots": [], /* Specify multiple folders that act like './node_modules/@types'. */
35
+ "types": ["puppeteer", "jest-environment-puppeteer", "expect-puppeteer", "jest"], /* Specify type package names to be included without being referenced in a source file. */
36
+ // "allowUmdGlobalAccess": true, /* Allow accessing UMD globals from modules. */
37
+ // "moduleSuffixes": [], /* List of file name suffixes to search when resolving a module. */
38
+ // "resolveJsonModule": true, /* Enable importing .json files. */
39
+ // "noResolve": true, /* Disallow 'import's, 'require's or '<reference>'s from expanding the number of files TypeScript should add to a project. */
40
+
41
+ /* JavaScript Support */
42
+ "allowJs": true, /* Allow JavaScript files to be a part of your program. Use the 'checkJS' option to get errors from these files. */
43
+ "checkJs": false, /* Enable error reporting in type-checked JavaScript files. */
44
+ // "maxNodeModuleJsDepth": 1, /* Specify the maximum folder depth used for checking JavaScript files from 'node_modules'. Only applicable with 'allowJs'. */
45
+
46
+ /* Emit */
47
+ // "declaration": true, /* Generate .d.ts files from TypeScript and JavaScript files in your project. */
48
+ // "declarationMap": true, /* Create sourcemaps for d.ts files. */
49
+ // "emitDeclarationOnly": true, /* Only output d.ts files and not JavaScript files. */
50
+ // "sourceMap": true, /* Create source map files for emitted JavaScript files. */
51
+ // "outFile": "./", /* Specify a file that bundles all outputs into one JavaScript file. If 'declaration' is true, also designates a file that bundles all .d.ts output. */
52
+ "outDir": "./dist", /* Specify an output folder for all emitted files. */
53
+ // "removeComments": true, /* Disable emitting comments. */
54
+ // "noEmit": true, /* Disable emitting files from a compilation. */
55
+ // "importHelpers": true, /* Allow importing helper functions from tslib once per project, instead of including them per-file. */
56
+ // "importsNotUsedAsValues": "remove", /* Specify emit/checking behavior for imports that are only used for types. */
57
+ // "downlevelIteration": true, /* Emit more compliant, but verbose and less performant JavaScript for iteration. */
58
+ // "sourceRoot": "", /* Specify the root path for debuggers to find the reference source code. */
59
+ // "mapRoot": "", /* Specify the location where debugger should locate map files instead of generated locations. */
60
+ // "inlineSourceMap": true, /* Include sourcemap files inside the emitted JavaScript. */
61
+ // "inlineSources": true, /* Include source code in the sourcemaps inside the emitted JavaScript. */
62
+ // "emitBOM": true, /* Emit a UTF-8 Byte Order Mark (BOM) in the beginning of output files. */
63
+ // "newLine": "crlf", /* Set the newline character for emitting files. */
64
+ // "stripInternal": true, /* Disable emitting declarations that have '@internal' in their JSDoc comments. */
65
+ // "noEmitHelpers": true, /* Disable generating custom helper functions like '__extends' in compiled output. */
66
+ // "noEmitOnError": true, /* Disable emitting files if any type checking errors are reported. */
67
+ // "preserveConstEnums": true, /* Disable erasing 'const enum' declarations in generated code. */
68
+ // "declarationDir": "./", /* Specify the output directory for generated declaration files. */
69
+ // "preserveValueImports": true, /* Preserve unused imported values in the JavaScript output that would otherwise be removed. */
70
+
71
+ /* Interop Constraints */
72
+ // "isolatedModules": true, /* Ensure that each file can be safely transpiled without relying on other imports. */
73
+ // "allowSyntheticDefaultImports": true, /* Allow 'import x from y' when a module doesn't have a default export. */
74
+ "esModuleInterop": true, /* Emit additional JavaScript to ease support for importing CommonJS modules. This enables 'allowSyntheticDefaultImports' for type compatibility. */
75
+ // "preserveSymlinks": true, /* Disable resolving symlinks to their realpath. This correlates to the same flag in node. */
76
+ "forceConsistentCasingInFileNames": true, /* Ensure that casing is correct in imports. */
77
+
78
+ /* Type Checking */
79
+ "strict": true, /* Enable all strict type-checking options. */
80
+ // "noImplicitAny": true, /* Enable error reporting for expressions and declarations with an implied 'any' type. */
81
+ // "strictNullChecks": true, /* When type checking, take into account 'null' and 'undefined'. */
82
+ // "strictFunctionTypes": true, /* When assigning functions, check to ensure parameters and the return values are subtype-compatible. */
83
+ // "strictBindCallApply": true, /* Check that the arguments for 'bind', 'call', and 'apply' methods match the original function. */
84
+ // "strictPropertyInitialization": true, /* Check for class properties that are declared but not set in the constructor. */
85
+ // "noImplicitThis": true, /* Enable error reporting when 'this' is given the type 'any'. */
86
+ // "useUnknownInCatchVariables": true, /* Default catch clause variables as 'unknown' instead of 'any'. */
87
+ // "alwaysStrict": true, /* Ensure 'use strict' is always emitted. */
88
+ // "noUnusedLocals": true, /* Enable error reporting when local variables aren't read. */
89
+ // "noUnusedParameters": true, /* Raise an error when a function parameter isn't read. */
90
+ // "exactOptionalPropertyTypes": true, /* Interpret optional property types as written, rather than adding 'undefined'. */
91
+ // "noImplicitReturns": true, /* Enable error reporting for codepaths that do not explicitly return in a function. */
92
+ // "noFallthroughCasesInSwitch": true, /* Enable error reporting for fallthrough cases in switch statements. */
93
+ // "noUncheckedIndexedAccess": true, /* Add 'undefined' to a type when accessed using an index. */
94
+ // "noImplicitOverride": true, /* Ensure overriding members in derived classes are marked with an override modifier. */
95
+ // "noPropertyAccessFromIndexSignature": true, /* Enforces using indexed accessors for keys declared using an indexed type. */
96
+ // "allowUnusedLabels": true, /* Disable error reporting for unused labels. */
97
+ // "allowUnreachableCode": true, /* Disable error reporting for unreachable code. */
98
+
99
+ /* Completeness */
100
+ // "skipDefaultLibCheck": true, /* Skip type checking .d.ts files that are included with TypeScript. */
101
+ "skipLibCheck": true, /* Skip type checking all .d.ts files. */
102
+ },
103
+ "include": ["src/**/*"],
104
+ "exclude": ["dist/**/*"]
105
+ }
package/bower.json DELETED
@@ -1,24 +0,0 @@
1
- {
2
- "name": "dragselect",
3
- "description": "easy javascript drag select functionality to your projects",
4
- "main": "./dist/ds.min.js",
5
- "authors": [
6
- "Thibault Jan Beyer"
7
- ],
8
- "license": "MIT",
9
- "keywords": [
10
- "drag",
11
- "select",
12
- "plain",
13
- "js",
14
- "javascript",
15
- "vanillajs"
16
- ],
17
- "homepage": "https://github.com/ThibaultJanBeyer/DragSelect",
18
- "ignore": [
19
- "**/.*",
20
- "node_modules",
21
- "bower_components",
22
- "__tests__"
23
- ]
24
- }