dragselect 2.4.2 → 2.4.4

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dragselect",
3
- "version": "2.4.2",
3
+ "version": "2.4.4",
4
4
  "description": "easy javascript drag select functionality for your projects",
5
5
  "main": "./dist/DragSelect.js",
6
6
  "types": "./dist/DragSelect.d.ts",
@@ -10,7 +10,7 @@
10
10
  "start": "npm run open & npm run watch",
11
11
  "watch": "concurrently --kill-others \"npm run checkjs:watch\" \"npm run rollup:watch\" \"npm run test:watch\"",
12
12
  "\n# TESTING:": "",
13
- "checkjs": "tsc --lib es2019,dom -t es5 --allowJs --checkJs --noEmit src/*.js",
13
+ "checkjs": "tsc --lib es2019,dom -t es5 --allowJs --skipLibCheck --checkJs --noEmit src/*.js",
14
14
  "checkjs:watch": "npm run checkjs -- -w",
15
15
  "postcheckjs": "npm run docs && rm -rf docs",
16
16
  "pretest": "npm run build",
@@ -23,12 +23,12 @@
23
23
  "media": "cp -R .media docs/media",
24
24
  "prerollup": "npm run types",
25
25
  "rollup": "rollup -c",
26
- "rollup:travis": "npm run rollup -- --travis",
26
+ "rollup:ci": "npm run rollup -- --ci",
27
27
  "rollup:watch": "npm run rollup -- -w",
28
- "types": "tsc src/DragSelect.js --lib es2019,dom -t es5 --declaration --allowJs --emitDeclarationOnly --outDir dist",
28
+ "types": "tsc src/DragSelect.js --lib es2019,dom -t es5 --declaration --allowJs --emitDeclarationOnly --skipLibCheck --outDir dist",
29
29
  "\n## PROD:": "",
30
- "travis:build": "npm run rollup:travis && npm run media && npm run docs",
31
- "travis:test": "npm run rollup && npm run test"
30
+ "ci:build": "npm run rollup:ci && npm run media && npm run docs",
31
+ "ci:test": "npm run test"
32
32
  },
33
33
  "devDependencies": {
34
34
  "@babel/cli": "^7.12.10",
package/rollup.config.js CHANGED
@@ -74,7 +74,7 @@ export default {
74
74
  {
75
75
  name: 'copy',
76
76
  writeBundle(options) {
77
- if (!process.argv.includes('--travis')) return
77
+ if (!process.argv.includes('--ci')) return
78
78
  if (!fs.existsSync('docs/')) fs.mkdirSync('docs')
79
79
  fs.copyFileSync(
80
80
  `.v1/${path.basename(options.file)}`,
package/src/DragSelect.js CHANGED
@@ -242,12 +242,15 @@ class DragSelect {
242
242
  * Add elements that can be selected. No node is added twice
243
243
  * @param {DSInputElements} elements dom element(s)
244
244
  * @param {boolean} [addToSelection] if elements should also be added to current selection
245
+ * @param {boolean} [triggerCallback] - if callback should be called
245
246
  * @return {DSInputElements} the added element(s)
246
247
  */
247
- addSelectables(elements, addToSelection = false) {
248
+ addSelectables(elements, addToSelection, triggerCallback) {
248
249
  const els = toArray(elements)
249
250
  this.SelectableSet.addAll(els)
250
251
  if (addToSelection) this.SelectedSet.addAll(els)
252
+ if (triggerCallback)
253
+ this.PubSub.publish('callback', { items: this.getSelection() })
251
254
  return elements
252
255
  }
253
256
  /**
@@ -277,11 +280,14 @@ class DragSelect {
277
280
  * Remove elements from the elements that can be selected.
278
281
  * @param {DSInputElements} elements – dom element(s)
279
282
  * @param {boolean} [removeFromSelection] if elements should also be removed from current selection
283
+ * @param {boolean} [triggerCallback] - if callback should be called
280
284
  * @return {DSInputElements} the removed element(s)
281
285
  */
282
- removeSelectables(elements, removeFromSelection = false) {
286
+ removeSelectables(elements, removeFromSelection, triggerCallback) {
283
287
  this.SelectableSet.deleteAll(toArray(elements))
284
288
  if (removeFromSelection) this.removeSelection(elements)
289
+ if (triggerCallback)
290
+ this.PubSub.publish('callback', { items: this.getSelection() })
285
291
  return elements
286
292
  }
287
293
  /** The starting/initial position of the cursor/selector @return {Vect2} */
@@ -4,7 +4,7 @@ import '../types'
4
4
  /**
5
5
  * Returns cursor x, y position based on event object
6
6
  * @param {Object} p
7
- * @param {MouseEvent|Touch} p.event
7
+ * @param {MouseEvent|Touch|PointerEvent} p.event
8
8
  * @return {Vect2} cursor X/Y position
9
9
  */
10
10
  export default ({ event }) => ({
@@ -101,6 +101,7 @@ export default (settings, withFallback) => ({
101
101
  ...hydrateHelper('dragKeys', settings.dragKeys, withFallback, { up: ['ArrowUp'], down: ['ArrowDown'], left: ['ArrowLeft'], right: ['ArrowRight'] }),
102
102
  ...hydrateHelper('keyboardDragSpeed', settings.keyboardDragSpeed, withFallback, 10),
103
103
  ...hydrateHelper('useTransform', settings.useTransform, withFallback, true),
104
+ ...hydrateHelper('usePointerEvents', settings.usePointerEvents, withFallback, false),
104
105
  ...hydrateHelper('hoverClass', settings.hoverClass, withFallback, 'ds-hover'),
105
106
  ...hydrateHelper('selectableClass', settings.selectableClass, withFallback, 'ds-selectable'),
106
107
  ...hydrateHelper('selectedClass', settings.selectedClass, withFallback, 'ds-selected'),
@@ -15,6 +15,7 @@ export default class Interaction {
15
15
  */
16
16
  constructor({ DS }) {
17
17
  this.DS = DS
18
+ this.Settings = DS.stores.SettingsStore.s
18
19
  // @ts-ignore: @todo: update to typescript
19
20
  this.DS.subscribe('Settings:updated:area', this.init)
20
21
  this.DS.subscribe('PointerStore:updated', this.update)
@@ -31,7 +32,14 @@ export default class Interaction {
31
32
  init = () => this.DS.publish('Interaction:init:pre', {})
32
33
  _init = () => {
33
34
  this.stop()
34
- this.DS.Area.HTMLNode.addEventListener('mousedown', this.start)
35
+
36
+ // @TODO: fix pointer events mixing issue see [PR](https://github.com/ThibaultJanBeyer/DragSelect/pull/128#issuecomment-1154885289)
37
+ if (this.Settings.usePointerEvents)
38
+ this.DS.Area.HTMLNode.addEventListener('pointerdown', this.start, {
39
+ passive: false,
40
+ })
41
+ else this.DS.Area.HTMLNode.addEventListener('mousedown', this.start)
42
+
35
43
  this.DS.Area.HTMLNode.addEventListener('touchstart', this.start, {
36
44
  passive: false,
37
45
  })
@@ -79,7 +87,12 @@ export default class Interaction {
79
87
 
80
88
  this.DS.publish('Interaction:start', { event, isDragging: this.isDragging })
81
89
 
82
- document.addEventListener('mouseup', this.reset)
90
+ // @TODO: fix pointer events mixing issue see [PR](https://github.com/ThibaultJanBeyer/DragSelect/pull/128#issuecomment-1154885289)
91
+ if (this.Settings.usePointerEvents) {
92
+ document.addEventListener('pointerup', this.reset)
93
+ document.addEventListener('pointercancel', this.reset)
94
+ } else document.addEventListener('mouseup', this.reset)
95
+
83
96
  document.addEventListener('touchend', this.reset)
84
97
  }
85
98
 
@@ -90,17 +103,17 @@ export default class Interaction {
90
103
  */
91
104
  isDragEvent = (event) => {
92
105
  const clickedElement = /** @type {Element} */ (event.target).closest(
93
- `.${this.DS.stores.SettingsStore.s.selectableClass}`
106
+ `.${this.Settings.selectableClass}`
94
107
  )
95
108
 
96
109
  if (
97
- !this.DS.stores.SettingsStore.s.draggability ||
110
+ !this.Settings.draggability ||
98
111
  this.DS.stores.KeyStore.isMultiSelectKeyPressed(event) ||
99
112
  !clickedElement
100
113
  )
101
114
  return false
102
115
 
103
- if (this.DS.stores.SettingsStore.s.immediateDrag) {
116
+ if (this.Settings.immediateDrag) {
104
117
  if (!this.DS.SelectedSet.size)
105
118
  this.DS.SelectedSet.add(/** @type {DSElement} */ (clickedElement))
106
119
  else if (!this.DS.SelectedSet.has(clickedElement)) {
@@ -144,12 +157,24 @@ export default class Interaction {
144
157
  stop = () => {
145
158
  this.isInteracting = false
146
159
  this.isDragging = false
147
- this.DS.Area.HTMLNode.removeEventListener('mousedown', this.start)
160
+
161
+ // @TODO: fix pointer events mixing issue see [PR](https://github.com/ThibaultJanBeyer/DragSelect/pull/128#issuecomment-1154885289)
162
+ if (this.Settings.usePointerEvents) {
163
+ this.DS.Area.HTMLNode.removeEventListener('pointerdown', this.start, {
164
+ // @ts-ignore
165
+ passive: false,
166
+ })
167
+ document.removeEventListener('pointerup', this.reset)
168
+ document.removeEventListener('pointercancel', this.reset)
169
+ } else {
170
+ this.DS.Area.HTMLNode.removeEventListener('mousedown', this.start)
171
+ document.removeEventListener('mouseup', this.reset)
172
+ }
173
+
148
174
  this.DS.Area.HTMLNode.removeEventListener('touchstart', this.start, {
149
175
  // @ts-ignore
150
176
  passive: false,
151
177
  })
152
- document.removeEventListener('mouseup', this.reset)
153
178
  document.removeEventListener('touchend', this.reset)
154
179
  }
155
180
 
@@ -13,6 +13,7 @@ export default class SelectableSet extends Set {
13
13
  constructor({ DS }) {
14
14
  super()
15
15
  this.DS = DS
16
+ this.Settings = DS.stores.SettingsStore.s
16
17
  this.DS.subscribe('Interaction:init', this.init)
17
18
  // @ts-ignore: @todo: update to typescript
18
19
  this.DS.PubSub.subscribe('Settings:updated:selectables', () => {
@@ -29,7 +30,7 @@ export default class SelectableSet extends Set {
29
30
  }
30
31
 
31
32
  init = () =>
32
- toArray(this.DS.stores.SettingsStore.s.selectables).forEach((el) =>
33
+ toArray(this.Settings.selectables).forEach((el) =>
33
34
  this.add(el)
34
35
  )
35
36
 
@@ -41,17 +42,24 @@ export default class SelectableSet extends Set {
41
42
  item: element,
42
43
  }
43
44
  this.DS.publish('Selectable:added:pre', publishData)
44
- element.classList.add(this.DS.stores.SettingsStore.s.selectableClass)
45
+ element.classList.add(this.Settings.selectableClass)
45
46
  element.addEventListener('click', this._onClick)
46
- element.addEventListener('mousedown', this._onPointer)
47
+
48
+ if (this.Settings.usePointerEvents)
49
+ element.addEventListener('pointerdown', this._onPointer, {
50
+ // @ts-ignore
51
+ passive: false,
52
+ })
53
+ else element.addEventListener('mousedown', this._onPointer)
54
+
47
55
  element.addEventListener('touchstart', this._onPointer, {
48
56
  // @ts-ignore
49
57
  passive: false,
50
58
  })
51
59
 
52
60
  if (
53
- this.DS.stores.SettingsStore.s.draggability &&
54
- !this.DS.stores.SettingsStore.s.useTransform
61
+ this.Settings.draggability &&
62
+ !this.Settings.useTransform
55
63
  )
56
64
  handleElementPositionAttribute({
57
65
  computedStyle: window.getComputedStyle(element),
@@ -70,10 +78,17 @@ export default class SelectableSet extends Set {
70
78
  item: element,
71
79
  }
72
80
  this.DS.publish('Selectable:removed:pre', publishData)
73
- element.classList.remove(this.DS.stores.SettingsStore.s.selectableClass)
74
- element.classList.remove(this.DS.stores.SettingsStore.s.hoverClass)
81
+ element.classList.remove(this.Settings.selectableClass)
82
+ element.classList.remove(this.Settings.hoverClass)
75
83
  element.removeEventListener('click', this._onClick)
76
- element.removeEventListener('mousedown', this._onPointer)
84
+
85
+ if (this.Settings.usePointerEvents)
86
+ element.removeEventListener('pointerdown', this._onPointer, {
87
+ // @ts-ignore
88
+ passive: false,
89
+ })
90
+ else element.removeEventListener('mousedown', this._onPointer)
91
+
77
92
  element.removeEventListener('touchstart', this._onPointer, {
78
93
  // @ts-ignore
79
94
  passive: false,
@@ -60,7 +60,7 @@ export default class KeyStore {
60
60
 
61
61
  reset = () => this._currentValues.clear()
62
62
 
63
- /** @param {KeyboardEvent|MouseEvent|TouchEvent} [event] */
63
+ /** @param {KeyboardEvent|MouseEvent|PointerEvent|TouchEvent} [event] */
64
64
  isMultiSelectKeyPressed(event) {
65
65
  if(this.DS.stores.SettingsStore.s.multiSelectMode) return true
66
66
  const multiSelectKeys = this.DS.stores.SettingsStore.s.multiSelectKeys.map(
@@ -53,13 +53,20 @@ export default class PointerStore {
53
53
  */
54
54
  constructor({ DS }) {
55
55
  this.DS = DS
56
+ this.Settings = DS.stores.SettingsStore.s
56
57
  this.DS.subscribe('Interaction:init', this.init)
57
58
  this.DS.subscribe('Interaction:start', ({ event }) => this.start(event))
58
59
  this.DS.subscribe('Interaction:end', ({ event }) => this.reset(event))
59
60
  }
60
61
 
61
62
  init = () => {
62
- document.addEventListener('mousemove', this.update)
63
+ if (this.Settings.usePointerEvents)
64
+ document.addEventListener('pointermove', this.update, {
65
+ // @ts-ignore
66
+ passive: false,
67
+ })
68
+ else document.addEventListener('mousemove', this.update)
69
+
63
70
  document.addEventListener('touchmove', this.update, {
64
71
  // @ts-ignore
65
72
  passive: false,
@@ -89,7 +96,14 @@ export default class PointerStore {
89
96
  }
90
97
 
91
98
  stop = () => {
92
- document.removeEventListener('mousemove', this.update)
99
+ // @TODO: fix pointer events mixing issue see [PR](https://github.com/ThibaultJanBeyer/DragSelect/pull/128#issuecomment-1154885289)
100
+ if (this.Settings.usePointerEvents)
101
+ document.removeEventListener('pointermove', this.update, {
102
+ // @ts-ignore
103
+ passive: false,
104
+ })
105
+ else document.removeEventListener('mousemove', this.update)
106
+
93
107
  document.removeEventListener('touchmove', this.update, {
94
108
  // @ts-ignore
95
109
  passive: false,
@@ -110,7 +124,7 @@ export default class PointerStore {
110
124
 
111
125
  /**
112
126
  * @param {DSEvent} event
113
- * @return {MouseEvent|Touch}
127
+ * @return {MouseEvent|PointerEvent|Touch}
114
128
  * @private
115
129
  */
116
130
  _normalizedEvent(event) {
package/src/types.js CHANGED
@@ -17,6 +17,7 @@
17
17
  * @property {DSDragKeys} [dragKeys={up:['ArrowUp'],down:['ArrowDown'],left:['ArrowLeft'],righ:['ArrowRight']}] The keys available to drag element using the keyboard.
18
18
  * @property {number} [keyboardDragSpeed=10] The speed at which elements are dragged using the keyboard. In pixels per keydown.
19
19
  * @property {boolean} [useTransform=true] Whether to use hardware accelerated css transforms when dragging or top/left instead
20
+ * @property {boolean} [usePointerEvents=false] Whether to use Pointer Events to replace traditional Mouse or Touch Events. Useful for tools like Google Blockly.
20
21
  * @property {string} [hoverClass=ds-hover] the class assigned to the mouse hovered items
21
22
  * @property {string} [selectableClass=ds-selectable] the class assigned to the elements that can be selected
22
23
  * @property {string} [selectedClass=ds-selected] the class assigned to the selected items
@@ -28,7 +29,7 @@
28
29
  * The Object that is passed back to any callback method
29
30
  * @typedef {Object} CallbackObject
30
31
  * @property {Array<HTMLElement|SVGElement|any>} [items] The items currently selected
31
- * @property {MouseEvent|TouchEvent|KeyboardEvent|Event} [event] The respective event object
32
+ * @property {MouseEvent|TouchEvent|PointerEvent|KeyboardEvent|Event} [event] The respective event object
32
33
  * @property {HTMLElement|SVGElement|any} [item] The single item currently interacted with
33
34
  * @property {boolean} [isDragging] Whether the interaction is a drag or a select
34
35
  * @property {boolean} [isDraggingKeyboard] Whether or not the drag interaction is via keyboard
@@ -51,7 +52,7 @@
51
52
  /** @typedef {Array.<HTMLElement|SVGElement> | HTMLElement | SVGElement} DSInputElements the elements that can be selected */
52
53
  /** @typedef {Array.<HTMLElement|SVGElement>} DSElements the elements that can be selected */
53
54
  /** @typedef {HTMLElement|SVGElement} DSElement a single element that can be selected */
54
- /** @typedef {MouseEvent|TouchEvent} DSEvent en event from a touch or mouse interaction */
55
+ /** @typedef {MouseEvent|TouchEvent|PointerEvent} DSEvent en event from a touch or mouse interaction */
55
56
  /** @typedef {Array.<'Shift'|'Control'|'Meta'|string>} DSMultiSelectKeys An array of keys that allows switching to the multi-select mode */
56
57
 
57
58
  /** @typedef {'dragmove'|'autoscroll'|'dragstart'|'elementselect'|'elementunselect'|'callback'} DSEventNames */