dragselect 2.2.1 → 2.3.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.
- package/CHANGELOG.md +18 -0
- package/CODE_OF_CONDUCT.md +128 -0
- package/README.md +17 -9
- package/dist/DragSelect.d.ts +2 -1
- package/dist/DragSelect.es6m.js +221 -99
- package/dist/DragSelect.js +221 -99
- package/dist/ds.es6m.min.js +1 -1
- package/dist/ds.min.js +1 -1
- package/dist/modules/Area.d.ts +15 -9
- package/dist/modules/Interaction.d.ts +18 -5
- package/dist/modules/PubSub.d.ts +9 -2
- package/dist/modules/SelectorArea.d.ts +2 -1
- package/dist/stores/PointerStore.d.ts +2 -0
- package/dist/types.d.ts +2 -2
- package/package.json +2 -1
- package/src/DragSelect.js +8 -2
- package/src/methods/subscriberAliases.js +0 -1
- package/src/modules/Area.js +29 -17
- package/src/modules/Drag.js +7 -9
- package/src/modules/Interaction.js +61 -18
- package/src/modules/PubSub.js +34 -4
- package/src/modules/SelectableSet.js +2 -2
- package/src/modules/SelectedSet.js +12 -8
- package/src/modules/Selection.js +1 -0
- package/src/modules/SelectorArea.js +10 -5
- package/src/stores/KeyStore.js +2 -0
- package/src/stores/PointerStore.js +9 -8
- package/src/types.js +2 -2
package/dist/types.d.ts
CHANGED
|
@@ -186,11 +186,11 @@ type DSEvent = MouseEvent | TouchEvent;
|
|
|
186
186
|
type DSMultiSelectKeys = string[];
|
|
187
187
|
type DSEventNames = "dragstart" | "dragmove" | "autoscroll" | "elementselect" | "elementunselect" | "callback";
|
|
188
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"
|
|
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";
|
|
190
190
|
/**
|
|
191
191
|
* the name of the callback
|
|
192
192
|
*/
|
|
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";
|
|
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";
|
|
194
194
|
type DSBoundingRect = {
|
|
195
195
|
top: number;
|
|
196
196
|
left: number;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "dragselect",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.3.1",
|
|
4
4
|
"description": "easy javascript drag select functionality for your projects",
|
|
5
5
|
"main": "./dist/DragSelect.js",
|
|
6
6
|
"types": "./dist/DragSelect.d.ts",
|
|
@@ -13,6 +13,7 @@
|
|
|
13
13
|
"checkjs": "tsc --lib es2019,dom -t es5 --allowJs --checkJs --noEmit src/*.js",
|
|
14
14
|
"checkjs:watch": "npm run checkjs -- -w",
|
|
15
15
|
"postcheckjs": "npm run docs && rm -rf docs",
|
|
16
|
+
"pretest": "npm run build",
|
|
16
17
|
"test": "npm run checkjs && jest --detectOpenHandles",
|
|
17
18
|
"test:watch": "npm run test -- --watch",
|
|
18
19
|
"\n# BUILDING:": "",
|
package/src/DragSelect.js
CHANGED
|
@@ -100,7 +100,7 @@ class DragSelect {
|
|
|
100
100
|
onElementSelect,
|
|
101
101
|
onElementUnselect,
|
|
102
102
|
}) {
|
|
103
|
-
this.PubSub = new PubSub()
|
|
103
|
+
this.PubSub = new PubSub({ DS: this })
|
|
104
104
|
this.subscribe = this.PubSub.subscribe
|
|
105
105
|
this.unsubscribe = this.PubSub.unsubscribe
|
|
106
106
|
this.publish = this.PubSub.publish
|
|
@@ -178,6 +178,7 @@ class DragSelect {
|
|
|
178
178
|
DS: this,
|
|
179
179
|
draggability,
|
|
180
180
|
immediateDrag,
|
|
181
|
+
selectableClass,
|
|
181
182
|
})
|
|
182
183
|
|
|
183
184
|
// Subscriber Aliases
|
|
@@ -247,7 +248,10 @@ class DragSelect {
|
|
|
247
248
|
* Initializes the functionality. Automatically triggered when created.
|
|
248
249
|
* Also, reset the functionality after a teardown
|
|
249
250
|
*/
|
|
250
|
-
start = () =>
|
|
251
|
+
start = () => {
|
|
252
|
+
this.stopped = false
|
|
253
|
+
this.Interaction.init()
|
|
254
|
+
}
|
|
251
255
|
/**
|
|
252
256
|
* Complete function teardown
|
|
253
257
|
* Will teardown/stop the whole functionality
|
|
@@ -269,6 +273,8 @@ class DragSelect {
|
|
|
269
273
|
|
|
270
274
|
if (remove) this.SelectableSet.clear()
|
|
271
275
|
if (fromSelection) this.SelectedSet.clear()
|
|
276
|
+
|
|
277
|
+
this.stopped = true
|
|
272
278
|
}
|
|
273
279
|
/**
|
|
274
280
|
* Utility to override DragSelect internal functionality:
|
|
@@ -33,7 +33,6 @@ export default ({ subscribe, publish, Interaction, SelectedSet }) => {
|
|
|
33
33
|
{ name: 'dragmove', condition: (data) => data.event },
|
|
34
34
|
], // event, isDraggingKeyboard
|
|
35
35
|
'Interaction:end': [{ name: 'callback' }], // event, isDraggingKeyboard
|
|
36
|
-
'Drag:keyboardDrag': [{ name: 'dragstart' }, { name: 'dragmove' }], // event, isDraggingKeyboard
|
|
37
36
|
}
|
|
38
37
|
|
|
39
38
|
for (const [sub_name, pubs] of Object.entries(mapping))
|
package/src/modules/Area.js
CHANGED
|
@@ -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 {
|
|
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', {
|
|
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
|
-
|
|
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
|
//////////////////////////////////////////////////////////////////////////////////////
|
package/src/modules/Drag.js
CHANGED
|
@@ -113,11 +113,12 @@ export default class Drag {
|
|
|
113
113
|
)
|
|
114
114
|
return
|
|
115
115
|
|
|
116
|
-
|
|
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
|
-
|
|
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 }) => {
|
|
@@ -18,6 +18,11 @@ export default class Interaction {
|
|
|
18
18
|
* @private
|
|
19
19
|
* */
|
|
20
20
|
_immediateDrag
|
|
21
|
+
/**
|
|
22
|
+
* @type {string}
|
|
23
|
+
* @private
|
|
24
|
+
* */
|
|
25
|
+
_selectableClass
|
|
21
26
|
/** @type {boolean} */
|
|
22
27
|
isInteracting
|
|
23
28
|
/** @type {boolean} */
|
|
@@ -26,24 +31,38 @@ export default class Interaction {
|
|
|
26
31
|
/**
|
|
27
32
|
* @constructor Interaction
|
|
28
33
|
* @param {Object} obj
|
|
34
|
+
* @param {DragSelect} obj.DS
|
|
29
35
|
* @param {DSArea} obj.areaElement
|
|
30
36
|
* @param {boolean} obj.draggability
|
|
31
37
|
* @param {boolean} obj.immediateDrag
|
|
32
|
-
* @param {
|
|
38
|
+
* @param {string} obj.selectableClass
|
|
33
39
|
* @ignore
|
|
34
40
|
*/
|
|
35
|
-
constructor({
|
|
41
|
+
constructor({
|
|
42
|
+
DS,
|
|
43
|
+
areaElement,
|
|
44
|
+
draggability,
|
|
45
|
+
immediateDrag,
|
|
46
|
+
selectableClass,
|
|
47
|
+
}) {
|
|
36
48
|
this._areaElement = areaElement
|
|
37
49
|
this._draggability = draggability
|
|
38
50
|
this._immediateDrag = immediateDrag
|
|
51
|
+
this._selectableClass = selectableClass
|
|
39
52
|
this.DS = DS
|
|
40
53
|
this.DS.subscribe('PointerStore:updated', this.update)
|
|
41
54
|
this.DS.subscribe('Selectable:click', this.onClick)
|
|
42
55
|
this.DS.subscribe('Selectable:pointer', ({ event }) => this.start(event))
|
|
56
|
+
this.DS.subscribe('Interaction:start:pre', ({ event }) =>
|
|
57
|
+
this._start(event)
|
|
58
|
+
)
|
|
59
|
+
this.DS.subscribe('Interaction:init:pre', this._init)
|
|
60
|
+
this.DS.subscribe('Interaction:end:pre', ({ event }) => this._reset(event))
|
|
43
61
|
this.DS.subscribe('Area:scroll', this.update)
|
|
44
62
|
}
|
|
45
63
|
|
|
46
|
-
init = () => {
|
|
64
|
+
init = () => this.DS.publish('Interaction:init:pre', {})
|
|
65
|
+
_init = () => {
|
|
47
66
|
this.stop()
|
|
48
67
|
this._areaElement.addEventListener('mousedown', this.start)
|
|
49
68
|
this._areaElement.addEventListener('touchstart', this.start, {
|
|
@@ -52,12 +71,24 @@ export default class Interaction {
|
|
|
52
71
|
this.DS.publish('Interaction:init', {})
|
|
53
72
|
}
|
|
54
73
|
|
|
74
|
+
/**
|
|
75
|
+
* @param {DSEvent} event
|
|
76
|
+
*/
|
|
55
77
|
_canInteract(event) {
|
|
78
|
+
const isKeyboardClick =
|
|
79
|
+
/** @type {MouseEvent} */ (event).clientX === 0 &&
|
|
80
|
+
/** @type {MouseEvent} */ (event).clientY === 0 &&
|
|
81
|
+
/** @type {MouseEvent} */ (event).detail === 0 &&
|
|
82
|
+
event.target
|
|
83
|
+
|
|
56
84
|
if (
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
!this.DS.SelectorArea.isInside(
|
|
85
|
+
/** @type {MouseEvent} */ (event).button === 2 || // right-clicks
|
|
86
|
+
this.isInteracting || // fix double-click issues
|
|
87
|
+
(event.target &&
|
|
88
|
+
!this.DS.SelectorArea.isInside(
|
|
89
|
+
/** @type {DSElement} */ (event.target)
|
|
90
|
+
)) || //fix outside elements issue
|
|
91
|
+
(!isKeyboardClick && !this.DS.SelectorArea.isClicked(event)) // make sure the mouse click is inside the area
|
|
61
92
|
)
|
|
62
93
|
return false
|
|
63
94
|
|
|
@@ -67,7 +98,12 @@ export default class Interaction {
|
|
|
67
98
|
/**
|
|
68
99
|
* @param {DSEvent} event
|
|
69
100
|
*/
|
|
70
|
-
start = (event) =>
|
|
101
|
+
start = (event) =>
|
|
102
|
+
this.DS.publish('Interaction:start:pre', {
|
|
103
|
+
event,
|
|
104
|
+
isDragging: this.isDragging,
|
|
105
|
+
})
|
|
106
|
+
_start = (event) => {
|
|
71
107
|
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
108
|
if (!this._canInteract(event)) return
|
|
73
109
|
|
|
@@ -86,23 +122,26 @@ export default class Interaction {
|
|
|
86
122
|
* @returns {boolean}
|
|
87
123
|
*/
|
|
88
124
|
isDragEvent = (event) => {
|
|
125
|
+
const clickedElement = /** @type {Element} */ (event.target).closest(
|
|
126
|
+
`.${this._selectableClass}`
|
|
127
|
+
)
|
|
89
128
|
if (
|
|
90
129
|
!this._draggability ||
|
|
91
130
|
this.DS.stores.KeyStore.isMultiSelectKeyPressed(event) ||
|
|
92
|
-
!
|
|
131
|
+
!clickedElement
|
|
93
132
|
)
|
|
94
133
|
return false
|
|
95
134
|
|
|
96
135
|
if (this._immediateDrag) {
|
|
97
136
|
if (!this.DS.SelectedSet.size)
|
|
98
|
-
this.DS.SelectedSet.add(/** @type {DSElement} */ (
|
|
99
|
-
else if (!this.DS.SelectedSet.has(
|
|
137
|
+
this.DS.SelectedSet.add(/** @type {DSElement} */ (clickedElement))
|
|
138
|
+
else if (!this.DS.SelectedSet.has(clickedElement)) {
|
|
100
139
|
this.DS.SelectedSet.clear()
|
|
101
|
-
this.DS.SelectedSet.add(/** @type {DSElement} */ (
|
|
140
|
+
this.DS.SelectedSet.add(/** @type {DSElement} */ (clickedElement))
|
|
102
141
|
}
|
|
103
142
|
}
|
|
104
143
|
|
|
105
|
-
if (this.DS.SelectedSet.has(
|
|
144
|
+
if (this.DS.SelectedSet.has(clickedElement)) return true
|
|
106
145
|
|
|
107
146
|
return false
|
|
108
147
|
}
|
|
@@ -110,7 +149,7 @@ export default class Interaction {
|
|
|
110
149
|
/**
|
|
111
150
|
* Triggers when a node is actively selected: <button> nodes that are pressed via the keyboard.
|
|
112
151
|
* Making DragSelect accessible for everyone!
|
|
113
|
-
* @param {{ event:MouseEvent }}
|
|
152
|
+
* @param {{ event:MouseEvent }} prop
|
|
114
153
|
*/
|
|
115
154
|
onClick = ({ event }) => {
|
|
116
155
|
if (!this._canInteract(event)) return
|
|
@@ -120,7 +159,6 @@ export default class Interaction {
|
|
|
120
159
|
stores: { PointerStore, KeyStore },
|
|
121
160
|
SelectableSet,
|
|
122
161
|
SelectedSet,
|
|
123
|
-
publish,
|
|
124
162
|
} = this.DS
|
|
125
163
|
|
|
126
164
|
PointerStore.start(event)
|
|
@@ -131,7 +169,7 @@ export default class Interaction {
|
|
|
131
169
|
if (!KeyStore.isMultiSelectKeyPressed(event)) SelectedSet.clear()
|
|
132
170
|
SelectedSet.toggle(node)
|
|
133
171
|
|
|
134
|
-
|
|
172
|
+
this.reset() // simulate mouse-up (that does not exist on keyboard)
|
|
135
173
|
}
|
|
136
174
|
|
|
137
175
|
stop = () => {
|
|
@@ -148,7 +186,7 @@ export default class Interaction {
|
|
|
148
186
|
|
|
149
187
|
update = ({ event, scroll_directions, scroll_multiplier }) => {
|
|
150
188
|
if (this.isInteracting)
|
|
151
|
-
this.DS.publish('Interaction:update', {
|
|
189
|
+
this.DS.publish(['Interaction:update:pre', 'Interaction:update'], {
|
|
152
190
|
event,
|
|
153
191
|
scroll_directions,
|
|
154
192
|
scroll_multiplier,
|
|
@@ -156,7 +194,12 @@ export default class Interaction {
|
|
|
156
194
|
})
|
|
157
195
|
}
|
|
158
196
|
|
|
159
|
-
reset = (event) =>
|
|
197
|
+
reset = (event) =>
|
|
198
|
+
this.DS.publish('Interaction:end:pre', {
|
|
199
|
+
event,
|
|
200
|
+
isDragging: this.isDragging,
|
|
201
|
+
})
|
|
202
|
+
_reset = (event) => {
|
|
160
203
|
const isDragging = this.isDragging
|
|
161
204
|
this.stop()
|
|
162
205
|
this.init()
|
package/src/modules/PubSub.js
CHANGED
|
@@ -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 (
|
|
47
|
-
|
|
48
|
-
|
|
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
|
-
|
|
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
|
|
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
|
|
package/src/modules/Selection.js
CHANGED
|
@@ -146,19 +146,24 @@ export default class SelectorArea {
|
|
|
146
146
|
|
|
147
147
|
/**
|
|
148
148
|
* checks if the click was triggered on the area.
|
|
149
|
+
* @param {DSEvent} [event]
|
|
149
150
|
* @returns {boolean}
|
|
150
151
|
*/
|
|
151
|
-
isClicked() {
|
|
152
|
+
isClicked(event) {
|
|
152
153
|
const {
|
|
153
154
|
stores: { PointerStore },
|
|
154
155
|
} = this.DS
|
|
155
156
|
|
|
157
|
+
const initialVal = event
|
|
158
|
+
? PointerStore.getPointerPosition(event)
|
|
159
|
+
: PointerStore.initialVal
|
|
160
|
+
|
|
156
161
|
return isCollision(
|
|
157
162
|
{
|
|
158
|
-
left:
|
|
159
|
-
top:
|
|
160
|
-
right:
|
|
161
|
-
bottom:
|
|
163
|
+
left: initialVal.x,
|
|
164
|
+
top: initialVal.y,
|
|
165
|
+
right: initialVal.x,
|
|
166
|
+
bottom: initialVal.y,
|
|
162
167
|
},
|
|
163
168
|
this.rect
|
|
164
169
|
)
|
package/src/stores/KeyStore.js
CHANGED
|
@@ -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
|
}
|
|
@@ -70,17 +70,20 @@ export default class PointerStore {
|
|
|
70
70
|
start(event) {
|
|
71
71
|
if (!event) return
|
|
72
72
|
this._isMouseInteraction = true
|
|
73
|
-
this.currentVal = this.initialVal =
|
|
73
|
+
this.currentVal = this.initialVal = this.getPointerPosition(event)
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/** @param {DSEvent} event */
|
|
77
|
+
getPointerPosition = (event) =>
|
|
78
|
+
getPointerPos({
|
|
74
79
|
event: this._normalizedEvent(event),
|
|
75
80
|
})
|
|
76
|
-
}
|
|
77
81
|
|
|
78
82
|
/** @param {DSEvent} [event] */
|
|
79
83
|
update = (event) => {
|
|
80
84
|
if (!event) return
|
|
81
|
-
this.
|
|
82
|
-
|
|
83
|
-
})
|
|
85
|
+
this.DS.publish('PointerStore:updated:pre', { event })
|
|
86
|
+
this.currentVal = this.getPointerPosition(event)
|
|
84
87
|
if (!this._isMouseInteraction) return
|
|
85
88
|
this.DS.publish('PointerStore:updated', { event })
|
|
86
89
|
}
|
|
@@ -99,9 +102,7 @@ export default class PointerStore {
|
|
|
99
102
|
reset = (event) => {
|
|
100
103
|
if (!event) return
|
|
101
104
|
|
|
102
|
-
this.currentVal = this.lastVal =
|
|
103
|
-
event: this._normalizedEvent(event),
|
|
104
|
-
})
|
|
105
|
+
this.currentVal = this.lastVal = this.getPointerPosition(event)
|
|
105
106
|
|
|
106
107
|
this.stop()
|
|
107
108
|
this.init()
|
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'
|
|
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 */
|