dragselect 2.5.1 → 2.5.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.
@@ -24,7 +24,7 @@ export default class DropZone {
24
24
  /**
25
25
  * @type {DSElements}
26
26
  */
27
- droppables
27
+ _droppables
28
28
 
29
29
  /**
30
30
  * @type {DOMRect}
@@ -62,7 +62,7 @@ export default class DropZone {
62
62
  * @param {DragSelect} obj.DS
63
63
  * @param {string} obj.id
64
64
  * @param {DSElement} obj.element
65
- * @param {DSInputElements} obj.droppables
65
+ * @param {DSInputElements} [obj.droppables]
66
66
  * @ignore
67
67
  */
68
68
  constructor({ DS, id, element, droppables }) {
@@ -71,7 +71,7 @@ export default class DropZone {
71
71
 
72
72
  this.id = id
73
73
  this.element = element
74
- this.droppables = toArray(droppables)
74
+ if (droppables) this.droppables = toArray(droppables)
75
75
  this.element.classList.add(`${this.Settings.dropZoneClass}`)
76
76
 
77
77
  // @ts-ignore: @todo: update to typescript
@@ -249,4 +249,13 @@ export default class DropZone {
249
249
  if (this._parentNodes) return this._parentNodes
250
250
  return (this._parentNodes = getAllParentNodes(this.element))
251
251
  }
252
+
253
+ get droppables() {
254
+ if (this._droppables) return this._droppables
255
+ return this.DS.SelectableSet.elements
256
+ }
257
+
258
+ set droppables(value) {
259
+ this._droppables = value
260
+ }
252
261
  }
@@ -4,7 +4,7 @@ import '../types'
4
4
  import DragSelect from '../DragSelect'
5
5
  import DropZone from './DropZone'
6
6
 
7
- import { isCollision, toArray } from '../methods'
7
+ import { isCollision } from '../methods'
8
8
 
9
9
  export default class DropZones {
10
10
  /**
@@ -45,7 +45,9 @@ export default class DropZones {
45
45
  this.Settings = DS.stores.SettingsStore.s
46
46
 
47
47
  // @ts-ignore: @todo: update to typescript
48
- this.DS.subscribe('Settings:updated:dropZones', this.setDropZones)
48
+ this.DS.subscribe('Settings:updated:dropZones', ({ settings }) =>
49
+ this.setDropZones(settings)
50
+ )
49
51
  this.setDropZones({
50
52
  dropZones: /** @type {DSDropZone[]} */ (this.DS.stores.SettingsStore.s
51
53
  .dropZones),
@@ -5,6 +5,7 @@ import '../types'
5
5
  export default class Interaction {
6
6
  /** @type {boolean} */
7
7
  isInteracting
8
+
8
9
  /** @type {boolean} */
9
10
  isDragging
10
11
 
@@ -30,9 +31,10 @@ export default class Interaction {
30
31
  }
31
32
 
32
33
  init = () => this.DS.publish('Interaction:init:pre', {})
34
+
33
35
  _init = () => {
34
36
  this.stop()
35
-
37
+
36
38
  // @TODO: fix pointer events mixing issue see [PR](https://github.com/ThibaultJanBeyer/DragSelect/pull/128#issuecomment-1154885289)
37
39
  if (this.Settings.usePointerEvents)
38
40
  this.DS.Area.HTMLNode.addEventListener('pointerdown', this.start, {
@@ -62,7 +64,7 @@ export default class Interaction {
62
64
  (event.target &&
63
65
  !this.DS.SelectorArea.isInside(
64
66
  /** @type {DSElement} */ (event.target)
65
- )) || //fix outside elements issue
67
+ )) || // fix outside elements issue
66
68
  (!isKeyboardClick && !this.DS.SelectorArea.isClicked(event)) // make sure the mouse click is inside the area
67
69
  )
68
70
  return false
@@ -78,6 +80,7 @@ export default class Interaction {
78
80
  event,
79
81
  isDragging: this.isDragging,
80
82
  })
83
+
81
84
  _start = (event) => {
82
85
  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
83
86
  if (!this._canInteract(event)) return
@@ -147,8 +150,7 @@ export default class Interaction {
147
150
  const node = /** @type {any} */ (event.target)
148
151
  if (!SelectableSet.has(node)) return
149
152
 
150
- if (!KeyStore.isMultiSelectKeyPressed(event))
151
- SelectedSet.clear()
153
+ if (!KeyStore.isMultiSelectKeyPressed(event)) SelectedSet.clear()
152
154
  SelectedSet.toggle(node)
153
155
 
154
156
  this.reset() // simulate mouse-up (that does not exist on keyboard)
@@ -193,8 +195,9 @@ export default class Interaction {
193
195
  event,
194
196
  isDragging: this.isDragging,
195
197
  })
198
+
196
199
  _reset = (event) => {
197
- const isDragging = this.isDragging
200
+ const { isDragging } = this
198
201
  this.stop()
199
202
  this.init()
200
203
  this.DS.publish('Interaction:end', { event, isDragging })
@@ -48,17 +48,15 @@ export default class PubSub {
48
48
  */
49
49
  publish = (eventName, data) => {
50
50
  if (Array.isArray(eventName))
51
- eventName.forEach(name => this._publish(name, data))
52
- else
53
- this._publish(eventName, data)
51
+ eventName.forEach((name) => this._publish(name, data))
52
+ else this._publish(eventName, data)
54
53
  }
54
+
55
55
  _publish = (eventName, data) => {
56
56
  const subscribers = this.subscribers[eventName]
57
57
  if (!Array.isArray(subscribers)) return
58
- if (eventName.includes(`:pre`))
59
- this._handlePrePublish(subscribers, data)
60
- else
61
- this._handlePublish(subscribers, data)
58
+ if (eventName.includes(`:pre`)) this._handlePrePublish(subscribers, data)
59
+ else this._handlePublish(subscribers, data)
62
60
  }
63
61
 
64
62
  // non-pre events are executed first in first out
@@ -10,11 +10,13 @@ export default class SelectableSet extends Set {
10
10
  * @private
11
11
  */
12
12
  _rects
13
+
13
14
  /**
14
15
  * @type {NodeJS.Timeout}
15
16
  * @private
16
17
  */
17
18
  _timeout
19
+
18
20
  /**
19
21
  * @constructor SelectableSet
20
22
  * @param {{DS:DragSelect}} obj
@@ -34,15 +36,12 @@ export default class SelectableSet extends Set {
34
36
  this.DS.subscribe('Settings:updated:selectableClass', ({ settings }) => {
35
37
  this.forEach((el) => {
36
38
  el.classList.remove(settings['selectableClass:pre'])
37
- el.classList.add(settings['selectableClass'])
39
+ el.classList.add(settings.selectableClass)
38
40
  })
39
41
  })
40
42
  }
41
43
 
42
- init = () =>
43
- toArray(this.Settings.selectables).forEach((el) =>
44
- this.add(el)
45
- )
44
+ init = () => toArray(this.Settings.selectables).forEach((el) => this.add(el))
46
45
 
47
46
  /** @param {DSElement} element */
48
47
  add(element) {
@@ -54,23 +53,20 @@ export default class SelectableSet extends Set {
54
53
  this.DS.publish('Selectable:added:pre', publishData)
55
54
  element.classList.add(this.Settings.selectableClass)
56
55
  element.addEventListener('click', this._onClick)
57
-
56
+
58
57
  if (this.Settings.usePointerEvents)
59
58
  element.addEventListener('pointerdown', this._onPointer, {
60
59
  // @ts-ignore
61
60
  passive: false,
62
61
  })
63
62
  else element.addEventListener('mousedown', this._onPointer)
64
-
63
+
65
64
  element.addEventListener('touchstart', this._onPointer, {
66
65
  // @ts-ignore
67
66
  passive: false,
68
67
  })
69
68
 
70
- if (
71
- this.Settings.draggability &&
72
- !this.Settings.useTransform
73
- )
69
+ if (this.Settings.draggability && !this.Settings.useTransform)
74
70
  handleElementPositionAttribute({
75
71
  computedStyle: window.getComputedStyle(element),
76
72
  node: element,
@@ -111,13 +107,16 @@ export default class SelectableSet extends Set {
111
107
 
112
108
  _onClick = (event) =>
113
109
  this.DS.publish(['Selectable:click:pre', 'Selectable:click'], { event })
110
+
114
111
  _onPointer = (event) =>
115
112
  this.DS.publish(['Selectable:pointer:pre', 'Selectable:pointer'], { event })
116
113
 
117
114
  /** @param {DSElements} elements */
118
115
  addAll = (elements) => elements.forEach((el) => this.add(el))
116
+
119
117
  /** @param {DSElements} elements */
120
118
  deleteAll = (elements) => elements.forEach((el) => this.delete(el))
119
+
121
120
  /** @return {DSElements} */
122
121
  get elements() {
123
122
  return Array.from(this.values())
@@ -126,11 +125,16 @@ export default class SelectableSet extends Set {
126
125
  get rects() {
127
126
  if (this._rects) return this._rects
128
127
  this._rects = new Map()
129
- this.forEach((element) => this._rects.set(element, element.getBoundingClientRect()))
128
+ this.forEach((element) =>
129
+ this._rects.set(element, element.getBoundingClientRect())
130
+ )
130
131
 
131
132
  // since elements can be moved, we need to update the rects every X ms
132
133
  if (this._timeout) clearTimeout(this._timeout)
133
- this._timeout = setTimeout(() => this._rects = null, this.Settings.refreshMemoryRate)
134
+ this._timeout = setTimeout(
135
+ () => (this._rects = null),
136
+ this.Settings.refreshMemoryRate
137
+ )
134
138
 
135
139
  return this._rects
136
140
  }
@@ -58,8 +58,10 @@ export default class SelectedSet extends Set {
58
58
 
59
59
  /** @param {DSElements} elements */
60
60
  addAll = (elements) => elements.forEach((el) => this.add(el))
61
+
61
62
  /** @param {DSElements} elements */
62
63
  deleteAll = (elements) => elements.forEach((el) => this.delete(el))
64
+
63
65
  /** @return {DSElements} */
64
66
  get elements() {
65
67
  return Array.from(this.values())
@@ -66,14 +66,15 @@ export default class Selection {
66
66
  const select = []
67
67
  const unselect = []
68
68
 
69
- for(const [element, rect] of elRects) {
69
+ for (const [element, rect] of elRects) {
70
70
  if (!SelectorArea.isInside(element, rect)) continue
71
- if (isCollision(rect, Selector.rect, this.Settings.selectionThreshold)) select.push(element)
71
+ if (isCollision(rect, Selector.rect, this.Settings.selectionThreshold))
72
+ select.push(element)
72
73
  else unselect.push(element)
73
74
  }
74
75
 
75
76
  const multiSelectionToggle =
76
- (this.DS.stores.KeyStore.isMultiSelectKeyPressed(event)) &&
77
+ this.DS.stores.KeyStore.isMultiSelectKeyPressed(event) &&
77
78
  this.Settings.multiSelectToggling
78
79
 
79
80
  if (this.DS.continue) return
@@ -1,5 +1,5 @@
1
1
  // @ts-check
2
- import '../types.js'
2
+ import '../types'
3
3
  import DragSelect from '../DragSelect'
4
4
 
5
5
  import {
@@ -27,7 +27,7 @@ export default class Selector {
27
27
  // @ts-ignore: @todo: update to typescript
28
28
  this.DS.subscribe('Settings:updated:selectorClass', ({ settings }) => {
29
29
  this.HTMLNode.classList.remove(settings['selectorClass:pre'])
30
- this.HTMLNode.classList.add(settings['selectorClass'])
30
+ this.HTMLNode.classList.add(settings.selectorClass)
31
31
  })
32
32
  // @ts-ignore: @todo: update to typescript
33
33
  this.DS.subscribe('Settings:updated:selector', this.attachSelector)
@@ -37,7 +37,7 @@ export default class Selector {
37
37
  this.DS.subscribe('Interaction:update', this.update)
38
38
  this.DS.subscribe('Interaction:end', this.stop)
39
39
  }
40
-
40
+
41
41
  attachSelector = () => {
42
42
  if (this.HTMLNode && this.DS.SelectorArea?.HTMLNode)
43
43
  this.DS.SelectorArea.HTMLNode.removeChild(this.HTMLNode)
package/src/types.js CHANGED
@@ -58,9 +58,9 @@
58
58
 
59
59
  /**
60
60
  * @typedef {Object} DSInputDropZone
61
- * @property {string} id
62
- * @property {DSElement} element
63
- * @property {DSInputElements} droppables
61
+ * @property {string} id can be any unique identifier of type string
62
+ * @property {DSElement} element is the dropzone itself
63
+ * @property {DSInputElements} [droppables] the elements that can be dropped into that zone. This is optional, by default it will be all selectables
64
64
  */
65
65
  /**
66
66
  * @typedef {Object} DSDropZone