dragselect 2.2.2 → 2.3.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.
@@ -11,6 +11,13 @@ import {
11
11
  handleElementPositionAttribute,
12
12
  } from '../methods'
13
13
 
14
+ /**
15
+ * @typedef {Object} AreaProps
16
+ * @property {DSArea} area
17
+ * @property {PubSub} PS
18
+ * @property {number} zoom
19
+ */
20
+
14
21
  export default class Area {
15
22
  /**
16
23
  * @type {DSModificationCallback}
@@ -55,23 +62,16 @@ export default class Area {
55
62
 
56
63
  /**
57
64
  * @constructor Area
58
- * @param {Object} obj
59
- * @param {DSArea} obj.area
60
- * @param {PubSub} obj.PS
61
- * @param {number} obj.zoom
65
+ * @param {AreaProps} settings
62
66
  * @ignore
63
67
  */
64
68
  constructor({ area, PS, zoom }) {
65
- this._node = area
66
69
  this._zoom = zoom
67
70
  this.PubSub = PS
68
-
69
- handleElementPositionAttribute({
70
- computedStyle: this.computedStyle,
71
- node: this._node,
72
- })
71
+ this.setArea(area)
73
72
 
74
73
  this._modificationCallback = debounce((event) => {
74
+ this.PubSub.publish('Area:modified:pre', { event, item: this })
75
75
  this.reset()
76
76
  this.PubSub.publish('Area:modified', { event, item: this })
77
77
  }, 60)
@@ -79,14 +79,24 @@ export default class Area {
79
79
  this._modificationCallback
80
80
  )
81
81
 
82
+ this.PubSub.subscribe('Interaction:init', this.start)
83
+ this.PubSub.subscribe('Interaction:end', this.reset)
84
+ }
85
+
86
+ /** @param {DSArea} area */
87
+ setArea = (area) => {
88
+ this._node = area
89
+ handleElementPositionAttribute({
90
+ computedStyle: this.computedStyle,
91
+ node: this._node,
92
+ })
93
+
82
94
  // first immediate debounce to update values after dom-update
83
95
  setTimeout(() => {
96
+ this.PubSub.publish('Area:modified:pre', { item: this })
84
97
  this.reset()
85
- this.PubSub.publish('Area:modified', { event, item: this })
98
+ this.PubSub.publish('Area:modified', { item: this })
86
99
  })
87
-
88
- this.PubSub.subscribe('Interaction:init', this.start)
89
- this.PubSub.subscribe('Interaction:end', this.reset)
90
100
  }
91
101
 
92
102
  start = () => {
@@ -121,11 +131,13 @@ export default class Area {
121
131
  * @param {number} multiplier
122
132
  */
123
133
  scroll = (directions, multiplier) => {
124
- scrollElement(this._node, directions, multiplier)
125
- this.PubSub.publish('Area:scroll', {
134
+ const data = {
126
135
  scroll_directions: directions,
127
136
  scroll_multiplier: multiplier,
128
- })
137
+ }
138
+ this.PubSub.publish('Area:scroll:pre', data)
139
+ scrollElement(this._node, directions, multiplier)
140
+ this.PubSub.publish('Area:scroll', data)
129
141
  }
130
142
 
131
143
  //////////////////////////////////////////////////////////////////////////////////////
@@ -113,11 +113,12 @@ export default class Drag {
113
113
  )
114
114
  return
115
115
 
116
- this.DS.publish('Interaction:start', {
116
+ const publishData = {
117
117
  event,
118
118
  isDragging: true,
119
119
  isDraggingKeyboard: true,
120
- })
120
+ }
121
+ this.DS.publish(['Interaction:start:pre', 'Interaction:start'], publishData)
121
122
 
122
123
  this._elements = this.DS.getSelection()
123
124
  this.handleZIndex(true)
@@ -142,11 +143,7 @@ export default class Drag {
142
143
  })
143
144
  )
144
145
 
145
- this.DS.publish('Interaction:update', {
146
- event,
147
- isDragging: true,
148
- isDraggingKeyboard: true,
149
- })
146
+ this.DS.publish(['Interaction:update:pre', 'Interaction:update'], publishData)
150
147
  }
151
148
 
152
149
  keyboardEnd = ({ event, key }) => {
@@ -157,11 +154,12 @@ export default class Drag {
157
154
  !this._draggability
158
155
  )
159
156
  return
160
- this.DS.publish('Interaction:end', {
157
+ const publishData = {
161
158
  event,
162
159
  isDragging: this._draggability,
163
160
  isDraggingKeyboard: true,
164
- })
161
+ }
162
+ this.DS.publish(['Interaction:end:pre', 'Interaction:end'], publishData)
165
163
  }
166
164
 
167
165
  start = ({ isDragging, isDraggingKeyboard }) => {
@@ -40,10 +40,16 @@ export default class Interaction {
40
40
  this.DS.subscribe('PointerStore:updated', this.update)
41
41
  this.DS.subscribe('Selectable:click', this.onClick)
42
42
  this.DS.subscribe('Selectable:pointer', ({ event }) => this.start(event))
43
+ this.DS.subscribe('Interaction:start:pre', ({ event }) =>
44
+ this._start(event)
45
+ )
46
+ this.DS.subscribe('Interaction:init:pre', this._init)
47
+ this.DS.subscribe('Interaction:end:pre', ({ event }) => this._reset(event))
43
48
  this.DS.subscribe('Area:scroll', this.update)
44
49
  }
45
50
 
46
- init = () => {
51
+ init = () => this.DS.publish('Interaction:init:pre', {})
52
+ _init = () => {
47
53
  this.stop()
48
54
  this._areaElement.addEventListener('mousedown', this.start)
49
55
  this._areaElement.addEventListener('touchstart', this.start, {
@@ -79,7 +85,8 @@ export default class Interaction {
79
85
  /**
80
86
  * @param {DSEvent} event
81
87
  */
82
- start = (event) => {
88
+ start = (event) => this.DS.publish('Interaction:start:pre', { event, isDragging: this.isDragging })
89
+ _start = (event) => {
83
90
  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
84
91
  if (!this._canInteract(event)) return
85
92
 
@@ -122,7 +129,7 @@ export default class Interaction {
122
129
  /**
123
130
  * Triggers when a node is actively selected: <button> nodes that are pressed via the keyboard.
124
131
  * Making DragSelect accessible for everyone!
125
- * @param {{ event:MouseEvent }} p
132
+ * @param {{ event:MouseEvent }} prop
126
133
  */
127
134
  onClick = ({ event }) => {
128
135
  if (!this._canInteract(event)) return
@@ -132,7 +139,6 @@ export default class Interaction {
132
139
  stores: { PointerStore, KeyStore },
133
140
  SelectableSet,
134
141
  SelectedSet,
135
- publish,
136
142
  } = this.DS
137
143
 
138
144
  PointerStore.start(event)
@@ -143,7 +149,7 @@ export default class Interaction {
143
149
  if (!KeyStore.isMultiSelectKeyPressed(event)) SelectedSet.clear()
144
150
  SelectedSet.toggle(node)
145
151
 
146
- publish('Interaction:end', { event, isDragging: this.isDragging }) // simulate mouse-up (that does not exist on keyboard)
152
+ this.reset() // simulate mouse-up (that does not exist on keyboard)
147
153
  }
148
154
 
149
155
  stop = () => {
@@ -160,7 +166,7 @@ export default class Interaction {
160
166
 
161
167
  update = ({ event, scroll_directions, scroll_multiplier }) => {
162
168
  if (this.isInteracting)
163
- this.DS.publish('Interaction:update', {
169
+ this.DS.publish(['Interaction:update:pre', 'Interaction:update'], {
164
170
  event,
165
171
  scroll_directions,
166
172
  scroll_multiplier,
@@ -168,7 +174,8 @@ export default class Interaction {
168
174
  })
169
175
  }
170
176
 
171
- reset = (event) => {
177
+ reset = (event) => this.DS.publish('Interaction:end:pre', { event, isDragging: this.isDragging })
178
+ _reset = (event) => {
172
179
  const isDragging = this.isDragging
173
180
  this.stop()
174
181
  this.init()
@@ -4,6 +4,10 @@ import '../types'
4
4
  export default class PubSub {
5
5
  subscribers = {}
6
6
 
7
+ constructor({ DS }) {
8
+ this.DS = DS
9
+ }
10
+
7
11
  /**
8
12
  * Subscribe to an event
9
13
  * @memberof DragSelect#
@@ -39,12 +43,38 @@ export default class PubSub {
39
43
  * Publishes an event to all subscribers
40
44
  * @memberof DragSelect#
41
45
  * @function publish
42
- * @param {DSCallbackNames} eventName
46
+ * @param {DSCallbackNames|DSCallbackNames[]} eventName
43
47
  * @param {CallbackObject} data passed to the subscription method
44
48
  */
45
49
  publish = (eventName, data) => {
46
- if (!Array.isArray(this.subscribers[eventName])) return
47
- this.subscribers[`${eventName}:pre`]?.forEach((callback) => callback(data))
48
- this.subscribers[eventName]?.forEach((callback) => callback(data))
50
+ if (Array.isArray(eventName))
51
+ eventName.forEach(name => this._publish(name, data))
52
+ else
53
+ this._publish(eventName, data)
54
+ }
55
+ _publish = (eventName, data) => {
56
+ const subscribers = this.subscribers[eventName]
57
+ if (!Array.isArray(subscribers)) return
58
+ if (eventName.includes(`:pre`))
59
+ this._handlePrePublish(subscribers, data)
60
+ else
61
+ this._handlePublish(subscribers, data)
62
+ }
63
+
64
+ // non-pre events are executed first in first out
65
+ _handlePublish = (subscribers, data) => {
66
+ for (let i = 0, il = subscribers.length; i < il; i++) {
67
+ if (this.DS.stopped) return
68
+ subscribers[i](data)
69
+ }
70
+ }
71
+
72
+ // pre events are executed last in first out (so user callbacks are called before DS callbacks)
73
+ _handlePrePublish = (subscribers, data) => {
74
+ let i = subscribers.length
75
+ while (i--) {
76
+ if (this.DS.stopped) return
77
+ subscribers[i](data)
78
+ }
49
79
  }
50
80
  }
@@ -97,8 +97,8 @@ export default class SelectableSet extends Set {
97
97
 
98
98
  clear = () => this.forEach((el) => this.delete(el))
99
99
 
100
- _onClick = (event) => this.DS.publish('Selectable:click', { event })
101
- _onPointer = (event) => this.DS.publish('Selectable:pointer', { event })
100
+ _onClick = (event) => this.DS.publish(['Selectable:click:pre', 'Selectable:click'], { event })
101
+ _onPointer = (event) => this.DS.publish(['Selectable:pointer:pre', 'Selectable:pointer'], { event })
102
102
 
103
103
  /** @param {DSElements} elements */
104
104
  addAll = (elements) => elements.forEach((el) => this.add(el))
@@ -25,26 +25,30 @@ export default class SelectedSet extends Set {
25
25
  /** @param {DSElement} element */
26
26
  add(element) {
27
27
  if (super.has(element)) return
28
- super.add(element)
29
- element.classList.add(this._className)
30
- this.DS.publish('Selected:added', {
28
+ const publishData = {
31
29
  items: this.elements,
32
30
  item: element,
33
- })
31
+ }
32
+ this.DS.publish('Selected:added:pre', publishData)
33
+ super.add(element)
34
+ element.classList.add(this._className)
34
35
  element.style.zIndex = `${(parseInt(element.style.zIndex) || 0) + 1}`
36
+ this.DS.publish('Selected:added', publishData)
35
37
  return this
36
38
  }
37
39
 
38
40
  /** @param {DSElement} element */
39
41
  delete(element) {
40
42
  if (!super.has(element)) return
41
- const deleted = super.delete(element)
42
- element.classList.remove(this._className)
43
- this.DS.publish('Selected:removed', {
43
+ const publishData = {
44
44
  items: this.elements,
45
45
  item: element,
46
- })
46
+ }
47
+ this.DS.publish('Selected:removed:pre', publishData)
48
+ const deleted = super.delete(element)
49
+ element.classList.remove(this._className)
47
50
  element.style.zIndex = `${(parseInt(element.style.zIndex) || 0) - 1}`
51
+ this.DS.publish('Selected:removed', publishData)
48
52
  return deleted
49
53
  }
50
54
 
@@ -91,6 +91,7 @@ export default class Selection {
91
91
  this.DS.stores.KeyStore.isMultiSelectKeyPressed(event) &&
92
92
  this._multiSelectToggling
93
93
 
94
+ if (this.DS.continue) return
94
95
  select.forEach((element) =>
95
96
  handleSelection({
96
97
  element,
@@ -68,6 +68,7 @@ export default class KeyStore {
68
68
  /** @param {KeyboardEvent} event */
69
69
  keydown = (event) => {
70
70
  const key = event.key.toLowerCase()
71
+ this.DS.publish('KeyStore:down:pre', { event, key })
71
72
  this._currentValues.add(key)
72
73
  this.DS.publish('KeyStore:down', { event, key })
73
74
  }
@@ -75,6 +76,7 @@ export default class KeyStore {
75
76
  /** @param {KeyboardEvent} event */
76
77
  keyup = (event) => {
77
78
  const key = event.key.toLowerCase()
79
+ this.DS.publish('KeyStore:up:pre', { event, key })
78
80
  this._currentValues.delete(key)
79
81
  this.DS.publish('KeyStore:up', { event, key })
80
82
  }
@@ -82,6 +82,7 @@ export default class PointerStore {
82
82
  /** @param {DSEvent} [event] */
83
83
  update = (event) => {
84
84
  if (!event) return
85
+ this.DS.publish('PointerStore:updated:pre', { event })
85
86
  this.currentVal = this.getPointerPosition(event)
86
87
  if (!this._isMouseInteraction) return
87
88
  this.DS.publish('PointerStore:updated', { event })
package/src/types.js CHANGED
@@ -61,8 +61,8 @@
61
61
 
62
62
  /** @typedef {'dragmove'|'autoscroll'|'dragstart'|'elementselect'|'elementunselect'|'callback'} DSEventNames */
63
63
  /** @typedef {'Interaction:init'|'Interaction:start'|'Interaction:end'|'Interaction:update'|'Area:modified'|'Area:scroll'|'PointerStore:updated'|'Selected:added'|'Selected:removed'|'Selectable:click'|'Selectable:pointer'|'KeyStore:down'|'KeyStore:up'} DSInternalEventNames */
64
- /** @typedef {'Interaction:init:pre'|'Interaction:start:pre'|'Interaction:end:pre'|'Interaction:update:pre'|'Area:modified:pre'|'Area:scroll:pre'|'PointerStore:updated:pre'|'Selected:added:pre'|'Selected:removed:pre'|'Selectable:click:pre'|'Selectable:pointer:pre'|'KeyStore:down:pre'|'KeyStore:up:pre'|'Drag:keyboardDrag:pre'} DSInternalEventNamesPre */
65
- /** @typedef {DSEventNames|DSInternalEventNames} DSCallbackNames the name of the callback */
64
+ /** @typedef {'Interaction:init:pre'|'Interaction:start:pre'|'Interaction:end:pre'|'Interaction:update:pre'|'Area:modified:pre'|'Area:scroll:pre'|'PointerStore:updated:pre'|'Selected:added:pre'|'Selected:removed:pre'|'Selectable:click:pre'|'Selectable:pointer:pre'|'KeyStore:down:pre'|'KeyStore:up:pre'} DSInternalEventNamesPre */
65
+ /** @typedef {DSEventNames|DSInternalEventNames|DSInternalEventNamesPre} DSCallbackNames the name of the callback */
66
66
 
67
67
  /** @typedef {{top:number,left:number,bottom:number,right:number,width:number,height:number}} DSBoundingRect */
68
68
  /** @typedef {{up:string[],down:string[],left:string[],right:string[]}} DSDragKeys */