dragselect 2.1.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.
@@ -38,8 +38,15 @@ export default class SelectorArea {
38
38
  private _overflowTolerance;
39
39
  DS: DragSelect;
40
40
  HTMLNode: HTMLDivElement;
41
+ start: () => void;
42
+ /**
43
+ * Adding / Removing elements to document
44
+ * @param {'append'|'remove'} method
45
+ */
46
+ applyElements: (method?: 'append' | 'remove') => void;
41
47
  /** Updates the selectorAreas positions to match the areas */
42
48
  updatePos: () => void;
49
+ stop: (remove: any) => void;
43
50
  startAutoScroll: () => void;
44
51
  /** Creates an interval that auto-scrolls while the cursor is near the edge */
45
52
  handleAutoScroll: () => void;
@@ -54,9 +61,10 @@ export default class SelectorArea {
54
61
  isInside: (element: DSElement, elementRect?: DSBoundingRect) => boolean;
55
62
  /**
56
63
  * checks if the click was triggered on the area.
64
+ * @param {DSEvent} [event]
57
65
  * @returns {boolean}
58
66
  */
59
- isClicked(): boolean;
67
+ isClicked(event?: DSEvent): boolean;
60
68
  get rect(): DSBoundingRect;
61
69
  }
62
70
  import DragSelect from "../DragSelect.js";
@@ -55,6 +55,8 @@ export default class PointerStore {
55
55
  set initialVal(arg: Vect2);
56
56
  /** First recorded pointer position */
57
57
  get initialVal(): Vect2;
58
+ /** @param {DSEvent} event */
59
+ getPointerPosition: (event: DSEvent) => Vect2;
58
60
  /** @param {DSEvent} [event] */
59
61
  update: (event?: DSEvent) => void;
60
62
  stop: () => void;
package/dist/types.d.ts CHANGED
@@ -50,6 +50,10 @@ type Settings = {
50
50
  * Whether an element is draggable from the start or needs to be selected first
51
51
  */
52
52
  immediateDrag?: boolean;
53
+ /**
54
+ * Whether or not the user can drag with the keyboard (we don't recommend disabling it)
55
+ */
56
+ keyboardDrag?: boolean;
53
57
  /**
54
58
  * The keys available to drag element using the keyboard.
55
59
  */
@@ -118,7 +122,7 @@ type CallbackObject = {
118
122
  /**
119
123
  * The respective event object
120
124
  */
121
- event?: MouseEvent | TouchEvent | Event;
125
+ event?: MouseEvent | TouchEvent | KeyboardEvent | Event;
122
126
  /**
123
127
  * The single item currently interacted with
124
128
  */
@@ -127,6 +131,14 @@ type CallbackObject = {
127
131
  * Whether the interaction is a drag or a select
128
132
  */
129
133
  isDragging?: boolean;
134
+ /**
135
+ * Whether or not the drag interaction is via keyboard
136
+ */
137
+ isDraggingKeyboard?: boolean;
138
+ /**
139
+ * Pressed key (lowercase)
140
+ */
141
+ key?: string;
130
142
  scroll_directions?: Array<'top' | 'bottom' | 'left' | 'right' | undefined>;
131
143
  scroll_multiplier?: number;
132
144
  };
@@ -174,10 +186,11 @@ type DSEvent = MouseEvent | TouchEvent;
174
186
  type DSMultiSelectKeys = string[];
175
187
  type DSEventNames = "dragstart" | "dragmove" | "autoscroll" | "elementselect" | "elementunselect" | "callback";
176
188
  type DSInternalEventNames = "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";
189
+ type DSInternalEventNamesPre = "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";
177
190
  /**
178
191
  * the name of the callback
179
192
  */
180
- type DSCallbackNames = "dragstart" | "dragmove" | "autoscroll" | "elementselect" | "elementunselect" | "callback" | "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";
193
+ type DSCallbackNames = "dragstart" | "dragmove" | "autoscroll" | "elementselect" | "elementunselect" | "callback" | "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" | "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";
181
194
  type DSBoundingRect = {
182
195
  top: number;
183
196
  left: number;
package/package.json CHANGED
@@ -1,28 +1,31 @@
1
1
  {
2
2
  "name": "dragselect",
3
- "version": "2.1.2",
3
+ "version": "2.3.0",
4
4
  "description": "easy javascript drag select functionality for your projects",
5
5
  "main": "./dist/DragSelect.js",
6
6
  "types": "./dist/DragSelect.d.ts",
7
7
  "scripts": {
8
+ "open": "open __tests__/quicktest.html -a 'Google Chrome'",
8
9
  "prestart": "npm i",
9
10
  "start": "npm run open & npm run watch",
10
- "open": "open __tests__/quicktest.html -a 'Google Chrome'",
11
11
  "watch": "concurrently --kill-others \"npm run checkjs:watch\" \"npm run rollup:watch\" \"npm run test:watch\"",
12
- "postcheckjs": "npm run docs && rm -rf docs",
12
+ "\n# TESTING:": "",
13
13
  "checkjs": "tsc --lib es2019,dom -t es5 --allowJs --checkJs --noEmit src/*.js",
14
- "checkjs:watch": "tsc -w --lib es2019,dom -t es5 --allowJs --checkJs --noEmit src/*.js",
15
- "types": "tsc src/DragSelect.js --lib es2019,dom -t es5 --declaration --allowJs --emitDeclarationOnly --outDir dist",
14
+ "checkjs:watch": "npm run checkjs -- -w",
15
+ "postcheckjs": "npm run docs && rm -rf docs",
16
+ "test": "npm run checkjs && jest --detectOpenHandles",
17
+ "test:watch": "npm run test -- --watch",
18
+ "\n# BUILDING:": "",
19
+ "build": "npm run rollup",
16
20
  "docs": "jsdoc -c jsdoc.json",
17
- "media": "cp -R .media docs/media",
18
21
  "premedia": "rm -r docs/media &",
19
- "rm": "rm -rf dist docs",
22
+ "media": "cp -R .media docs/media",
20
23
  "prerollup": "npm run types",
21
24
  "rollup": "rollup -c",
22
25
  "rollup:travis": "npm run rollup -- --travis",
23
26
  "rollup:watch": "npm run rollup -- -w",
24
- "test": "npm run checkjs && jest --detectOpenHandles",
25
- "test:watch": "jest --watch",
27
+ "types": "tsc src/DragSelect.js --lib es2019,dom -t es5 --declaration --allowJs --emitDeclarationOnly --outDir dist",
28
+ "\n## PROD:": "",
26
29
  "travis:build": "npm run rollup:travis && npm run media && npm run docs",
27
30
  "travis:test": "npm run rollup && npm run test"
28
31
  },
package/src/DragSelect.js CHANGED
@@ -55,12 +55,17 @@ import {
55
55
  SelectorArea,
56
56
  } from './modules'
57
57
  import { PointerStore, ScrollStore, KeyStore } from './stores'
58
- import { toArray, vect2 } from './methods'
58
+ import { toArray, vect2, subscriberAliases } from './methods'
59
59
 
60
60
  // Setup
61
61
  //////////////////////////////////////////////////////////////////////////////////////
62
62
 
63
63
  class DragSelect {
64
+ /**
65
+ * used to skip all current Selection and dragNdrop functionality
66
+ * @type {boolean}
67
+ */
68
+ continue = false
64
69
  /**
65
70
  * @class DragSelect
66
71
  * @constructor DragSelect
@@ -79,6 +84,7 @@ class DragSelect {
79
84
  selector = undefined,
80
85
  draggability = true,
81
86
  immediateDrag = true,
87
+ keyboardDrag = true,
82
88
  dragKeys,
83
89
  keyboardDragSpeed = 10,
84
90
  useTransform = true,
@@ -94,7 +100,7 @@ class DragSelect {
94
100
  onElementSelect,
95
101
  onElementUnselect,
96
102
  }) {
97
- this.PubSub = new PubSub()
103
+ this.PubSub = new PubSub({ DS: this })
98
104
  this.subscribe = this.PubSub.subscribe
99
105
  this.unsubscribe = this.PubSub.unsubscribe
100
106
  this.publish = this.PubSub.publish
@@ -153,6 +159,7 @@ class DragSelect {
153
159
  DS: this,
154
160
  draggability,
155
161
  useTransform,
162
+ keyboardDrag,
156
163
  dragKeys: Object.assign(
157
164
  {
158
165
  up: ['ArrowUp'],
@@ -174,53 +181,14 @@ class DragSelect {
174
181
  })
175
182
 
176
183
  // Subscriber Aliases
177
- this.subscribe('Selected:added', ({ items, item }) =>
178
- this.publish('elementselect', {
179
- items,
180
- item,
181
- isDragging: this.Interaction.isDragging,
182
- })
183
- )
184
- this.subscribe('Selected:removed', ({ items, item }) =>
185
- this.publish('elementunselect', {
186
- items,
187
- item,
188
- isDragging: this.Interaction.isDragging,
189
- })
190
- )
191
- this.subscribe('Interaction:update', ({ event, isDragging }) => {
192
- if (event)
193
- this.publish('dragmove', {
194
- items: this.getSelection(),
195
- event,
196
- isDragging,
197
- })
184
+ subscriberAliases({
185
+ subscribe: this.subscribe,
186
+ publish: this.publish,
187
+ SelectedSet: this.SelectedSet,
188
+ Interaction: this.Interaction,
198
189
  })
199
- this.subscribe(
200
- 'Area:scroll',
201
- ({ scroll_directions, scroll_multiplier }) => {
202
- this.publish('autoscroll', {
203
- items: this.getSelection(),
204
- scroll_directions,
205
- scroll_multiplier,
206
- isDragging: this.Interaction.isDragging,
207
- })
208
- }
209
- )
210
- this.subscribe('Interaction:start', ({ event, isDragging }) =>
211
- this.publish('dragstart', {
212
- items: this.getSelection(),
213
- event,
214
- isDragging,
215
- })
216
- )
217
- this.subscribe('Interaction:end', ({ event, isDragging }) =>
218
- this.publish('callback', {
219
- items: this.getSelection(),
220
- event,
221
- isDragging,
222
- })
223
- )
190
+
191
+ this.subscribe('Interaction:end', () => (this.continue = false))
224
192
 
225
193
  this.start()
226
194
  }
@@ -279,7 +247,10 @@ class DragSelect {
279
247
  * Initializes the functionality. Automatically triggered when created.
280
248
  * Also, reset the functionality after a teardown
281
249
  */
282
- start = () => this.Interaction.init()
250
+ start = () => {
251
+ this.stopped = false
252
+ this.Interaction.init()
253
+ }
283
254
  /**
284
255
  * Complete function teardown
285
256
  * Will teardown/stop the whole functionality
@@ -294,13 +265,22 @@ class DragSelect {
294
265
  this.Area.stop()
295
266
  this.Drag.stop()
296
267
  this.Selector.stop()
268
+ this.SelectorArea.stop(remove)
297
269
  this.stores.KeyStore.stop()
298
270
  this.stores.PointerStore.stop()
299
271
  this.stores.ScrollStore.stop()
300
272
 
301
273
  if (remove) this.SelectableSet.clear()
302
274
  if (fromSelection) this.SelectedSet.clear()
275
+
276
+ this.stopped = true
303
277
  }
278
+ /**
279
+ * Utility to override DragSelect internal functionality:
280
+ * Break will skip the selection or dragging functionality (until after the callback) but let everything continue to run.
281
+ * Useful utility to write your own functionality/move/dragNdrop based on DragSelect pointer positions.
282
+ */
283
+ break = () => (this.continue = true)
304
284
  /**
305
285
  * Returns the current selected nodes
306
286
  * @return {DSElements}
@@ -455,18 +435,28 @@ class DragSelect {
455
435
  */
456
436
  isMultiSelect = (event) => this.stores.KeyStore.isMultiSelectKeyPressed(event)
457
437
  /**
458
- * Returns the cursor position difference between start and now
438
+ * Utility method that returns the cursor position difference between start and now
459
439
  * @param {boolean} [usePreviousCursorDifference] if true, it will output the cursor position difference between the previous selection and now
440
+ * @param {boolean} [useAreaPositions] if true, it will use cursor positions relative to the area
460
441
  * @return {Vect2}
461
442
  * @deprecated
462
443
  */
463
- getCursorPositionDifference(usePreviousCursorDifference = false) {
444
+ getCursorPositionDifference(
445
+ usePreviousCursorDifference = false,
446
+ useAreaPositions = false
447
+ ) {
464
448
  console.warn(
465
449
  '[DragSelect] Using .getCursorPositionDifference is deprecated. Calculate yourself instead. i.e. `.getCurrentCursorPosition().x - .getInitialCursorPosition().x`'
466
450
  )
467
- const posA = this.getCurrentCursorPosition()
451
+ const posA = useAreaPositions
452
+ ? this.getCurrentCursorPositionArea()
453
+ : this.getCurrentCursorPosition()
468
454
  const posB = usePreviousCursorDifference
469
- ? this.getPreviousCursorPosition()
455
+ ? useAreaPositions
456
+ ? this.getPreviousCursorPositionArea()
457
+ : this.getPreviousCursorPosition()
458
+ : useAreaPositions
459
+ ? this.getInitialCursorPositionArea()
470
460
  : this.getInitialCursorPosition()
471
461
  return vect2.calc(posA, '-', posB)
472
462
  }
@@ -21,5 +21,6 @@ export { default as moveElement } from './moveElement'
21
21
  export { default as removeModificationObservers } from './removeModificationObservers'
22
22
  export { default as scrollElement } from './scrollElement'
23
23
  export { default as setStylePosition } from './setStylePosition'
24
+ export { default as subscriberAliases } from './subscriberAliases'
24
25
  export { default as toArray } from './toArray'
25
26
  export { default as updateElementStylePos } from './updateElementStylePos'
@@ -0,0 +1,52 @@
1
+ // @ts-check
2
+ import '../types'
3
+ import { Interaction, SelectedSet } from '../modules'
4
+
5
+ /**
6
+ * @typedef {function} DSSubscribe
7
+ * @param {DSCallbackNames} eventName
8
+ * @param {DSCallback} callback
9
+ * @returns {number} event id, can be used to unsubscribe more efficiently
10
+ */
11
+ /**
12
+ * @typedef {function} DSPublish
13
+ * @param {DSCallbackNames} eventName
14
+ * @param {CallbackObject} data passed to the subscription method
15
+ */
16
+
17
+ /**
18
+ * Maps internal events to external ones
19
+ *
20
+ * @param {Object} p
21
+ * @param {DSSubscribe} p.subscribe
22
+ * @param {DSPublish} p.publish
23
+ * @param {Interaction} p.Interaction
24
+ * @param {SelectedSet} p.SelectedSet
25
+ */
26
+ export default ({ subscribe, publish, Interaction, SelectedSet }) => {
27
+ const mapping = {
28
+ 'Selected:added': [{ name: 'elementselect' }],
29
+ 'Selected:removed': [{ name: 'elementunselect' }],
30
+ 'Area:scroll': [{ name: 'autoscroll' }], // scroll_directions, scroll_multiplier
31
+ 'Interaction:start': [{ name: 'dragstart' }], // event, isDraggingKeyboard
32
+ 'Interaction:update': [
33
+ { name: 'dragmove', condition: (data) => data.event },
34
+ ], // event, isDraggingKeyboard
35
+ 'Interaction:end': [{ name: 'callback' }], // event, isDraggingKeyboard
36
+ }
37
+
38
+ for (const [sub_name, pubs] of Object.entries(mapping))
39
+ ['pre', false].forEach((filler) =>
40
+ subscribe(filler ? `${sub_name}:${filler}` : sub_name, (data) =>
41
+ pubs.forEach(
42
+ (pub) =>
43
+ (!pub.condition || pub.condition(data)) &&
44
+ publish(filler ? `${filler}${pub.name}` : pub.name, {
45
+ items: SelectedSet.elements,
46
+ isDragging: Interaction.isDragging,
47
+ ...data,
48
+ })
49
+ )
50
+ )
51
+ )
52
+ }
@@ -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
  //////////////////////////////////////////////////////////////////////////////////////
@@ -40,6 +40,11 @@ export default class Drag {
40
40
  * @private
41
41
  */
42
42
  _dragKeysFlat
43
+ /**
44
+ * @type {boolean}
45
+ * @private
46
+ */
47
+ _keyboardDrag
43
48
  /**
44
49
  * @type {number}
45
50
  * @private
@@ -57,6 +62,7 @@ export default class Drag {
57
62
  * @param {boolean} p.draggability
58
63
  * @param {boolean} p.useTransform
59
64
  * @param {DSDragKeys} p.dragKeys
65
+ * @param {boolean} p.keyboardDrag
60
66
  * @param {number} p.keyboardDragSpeed
61
67
  * @param {number} p.zoom
62
68
  * @ignore
@@ -65,6 +71,7 @@ export default class Drag {
65
71
  DS,
66
72
  dragKeys,
67
73
  draggability,
74
+ keyboardDrag,
68
75
  keyboardDragSpeed,
69
76
  useTransform,
70
77
  zoom,
@@ -72,6 +79,7 @@ export default class Drag {
72
79
  this.DS = DS
73
80
  this._useTransform = useTransform
74
81
  this._keyboardDragSpeed = keyboardDragSpeed
82
+ this._keyboardDrag = keyboardDrag
75
83
  this._zoom = zoom
76
84
  this._draggability = draggability
77
85
 
@@ -96,16 +104,21 @@ export default class Drag {
96
104
  }
97
105
 
98
106
  keyboardDrag = ({ event, key }) => {
99
- console.log(event, key, this._draggability)
100
107
  if (
108
+ !this._keyboardDrag ||
101
109
  !this._dragKeysFlat.includes(key) ||
102
110
  !this.DS.SelectedSet.size ||
103
- !this._draggability
111
+ !this._draggability ||
112
+ this.DS.continue
104
113
  )
105
114
  return
106
115
 
107
- this._isKeyboard = true
108
- this.DS.publish('Interaction:start', { event, isDragging: true })
116
+ const publishData = {
117
+ event,
118
+ isDragging: true,
119
+ isDraggingKeyboard: true,
120
+ }
121
+ this.DS.publish(['Interaction:start:pre', 'Interaction:start'], publishData)
109
122
 
110
123
  this._elements = this.DS.getSelection()
111
124
  this.handleZIndex(true)
@@ -130,40 +143,49 @@ export default class Drag {
130
143
  })
131
144
  )
132
145
 
133
- this.DS.publish('Interaction:update', { event, isDragging: true })
134
- this._isKeyboard = false
146
+ this.DS.publish(['Interaction:update:pre', 'Interaction:update'], publishData)
135
147
  }
136
148
 
137
149
  keyboardEnd = ({ event, key }) => {
138
150
  if (
151
+ !this._keyboardDrag ||
139
152
  !this._dragKeysFlat.includes(key) ||
140
153
  !this.DS.SelectedSet.size ||
141
154
  !this._draggability
142
155
  )
143
156
  return
144
- this._isKeyboard = true
145
- this.DS.publish('Interaction:end', { event, isDragging: true })
146
- this._isKeyboard = false
157
+ const publishData = {
158
+ event,
159
+ isDragging: this._draggability,
160
+ isDraggingKeyboard: true,
161
+ }
162
+ this.DS.publish(['Interaction:end:pre', 'Interaction:end'], publishData)
147
163
  }
148
164
 
149
- start = ({ isDragging }) => {
150
- if (!isDragging || this._isKeyboard) return
165
+ start = ({ isDragging, isDraggingKeyboard }) => {
166
+ if (!isDragging || isDraggingKeyboard) return
151
167
  this._prevCursorPos = null
152
168
  this._prevScrollPos = null
153
169
  this._elements = this.DS.getSelection()
154
170
  this.handleZIndex(true)
155
171
  }
156
172
 
157
- stop = () => {
158
- if (this._isKeyboard) return
173
+ stop = (evt) => {
174
+ if (evt?.isKeyboard) return
159
175
  this._prevCursorPos = null
160
176
  this._prevScrollPos = null
161
177
  this.handleZIndex(false)
162
178
  this._elements = []
163
179
  }
164
180
 
165
- update = ({ isDragging }) => {
166
- if (!isDragging || !this._elements.length || this._isKeyboard) return
181
+ update = ({ isDragging, isDraggingKeyboard }) => {
182
+ if (
183
+ !isDragging ||
184
+ !this._elements.length ||
185
+ isDraggingKeyboard ||
186
+ this.DS.continue
187
+ )
188
+ return
167
189
 
168
190
  const posDirection = vect2.calc(this._cursorDiff, '+', this._scrollDiff)
169
191
 
@@ -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, {
@@ -52,12 +58,24 @@ export default class Interaction {
52
58
  this.DS.publish('Interaction:init', {})
53
59
  }
54
60
 
61
+ /**
62
+ * @param {DSEvent} event
63
+ */
55
64
  _canInteract(event) {
65
+ const isKeyboardClick =
66
+ /** @type {MouseEvent} */ (event).clientX === 0 &&
67
+ /** @type {MouseEvent} */ (event).clientY === 0 &&
68
+ /** @type {MouseEvent} */ (event).detail === 0 &&
69
+ event.target
70
+
56
71
  if (
57
- /* right-clicks */ event.button === 2 ||
58
- /* fix double-click issues */ this.isInteracting ||
59
- /* fix outside elements issue */ (event.target &&
60
- !this.DS.SelectorArea.isInside(event.target))
72
+ /** @type {MouseEvent} */ (event).button === 2 || // right-clicks
73
+ this.isInteracting || // fix double-click issues
74
+ (event.target &&
75
+ !this.DS.SelectorArea.isInside(
76
+ /** @type {DSElement} */ (event.target)
77
+ )) || //fix outside elements issue
78
+ (!isKeyboardClick && !this.DS.SelectorArea.isClicked(event)) // make sure the mouse click is inside the area
61
79
  )
62
80
  return false
63
81
 
@@ -67,7 +85,8 @@ export default class Interaction {
67
85
  /**
68
86
  * @param {DSEvent} event
69
87
  */
70
- start = (event) => {
88
+ start = (event) => this.DS.publish('Interaction:start:pre', { event, isDragging: this.isDragging })
89
+ _start = (event) => {
71
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
72
91
  if (!this._canInteract(event)) return
73
92
 
@@ -110,7 +129,7 @@ export default class Interaction {
110
129
  /**
111
130
  * Triggers when a node is actively selected: <button> nodes that are pressed via the keyboard.
112
131
  * Making DragSelect accessible for everyone!
113
- * @param {{ event:MouseEvent }} p
132
+ * @param {{ event:MouseEvent }} prop
114
133
  */
115
134
  onClick = ({ event }) => {
116
135
  if (!this._canInteract(event)) return
@@ -120,7 +139,6 @@ export default class Interaction {
120
139
  stores: { PointerStore, KeyStore },
121
140
  SelectableSet,
122
141
  SelectedSet,
123
- publish,
124
142
  } = this.DS
125
143
 
126
144
  PointerStore.start(event)
@@ -131,7 +149,7 @@ export default class Interaction {
131
149
  if (!KeyStore.isMultiSelectKeyPressed(event)) SelectedSet.clear()
132
150
  SelectedSet.toggle(node)
133
151
 
134
- 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)
135
153
  }
136
154
 
137
155
  stop = () => {
@@ -146,16 +164,18 @@ export default class Interaction {
146
164
  document.removeEventListener('touchend', this.reset)
147
165
  }
148
166
 
149
- update = ({ event, data }) => {
167
+ update = ({ event, scroll_directions, scroll_multiplier }) => {
150
168
  if (this.isInteracting)
151
- this.DS.publish('Interaction:update', {
169
+ this.DS.publish(['Interaction:update:pre', 'Interaction:update'], {
152
170
  event,
153
- data,
171
+ scroll_directions,
172
+ scroll_multiplier,
154
173
  isDragging: this.isDragging,
155
174
  })
156
175
  }
157
176
 
158
- reset = (event) => {
177
+ reset = (event) => this.DS.publish('Interaction:end:pre', { event, isDragging: this.isDragging })
178
+ _reset = (event) => {
159
179
  const isDragging = this.isDragging
160
180
  this.stop()
161
181
  this.init()