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.
- package/CHANGELOG.md +29 -1
- package/CODE_OF_CONDUCT.md +128 -0
- package/README.md +88 -15
- package/dist/DragSelect.d.ts +17 -4
- package/dist/DragSelect.es6m.js +466 -194
- package/dist/DragSelect.js +466 -194
- package/dist/ds.es6m.min.js +1 -1
- package/dist/ds.min.js +1 -1
- package/dist/methods/index.d.ts +1 -0
- package/dist/methods/subscriberAliases.d.ts +12 -0
- package/dist/modules/Area.d.ts +15 -9
- package/dist/modules/Drag.d.ts +13 -5
- package/dist/modules/Interaction.d.ts +11 -4
- package/dist/modules/PubSub.d.ts +10 -3
- package/dist/modules/SelectorArea.d.ts +9 -1
- package/dist/stores/PointerStore.d.ts +2 -0
- package/dist/types.d.ts +15 -2
- package/package.json +12 -9
- package/src/DragSelect.js +43 -53
- package/src/methods/index.js +1 -0
- package/src/methods/subscriberAliases.js +52 -0
- package/src/modules/Area.js +29 -17
- package/src/modules/Drag.js +37 -15
- package/src/modules/Interaction.js +33 -13
- package/src/modules/PubSub.js +35 -4
- package/src/modules/SelectableSet.js +2 -2
- package/src/modules/SelectedSet.js +12 -8
- package/src/modules/Selection.js +2 -1
- package/src/modules/Selector.js +1 -1
- package/src/modules/SelectorArea.js +30 -8
- package/src/stores/KeyStore.js +2 -0
- package/src/stores/PointerStore.js +9 -8
- package/src/types.js +6 -2
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,11 +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
|
|
43
|
-
* @param {CallbackObject
|
|
46
|
+
* @param {DSCallbackNames|DSCallbackNames[]} eventName
|
|
47
|
+
* @param {CallbackObject} data passed to the subscription method
|
|
44
48
|
*/
|
|
45
49
|
publish = (eventName, data) => {
|
|
46
|
-
if (
|
|
47
|
-
|
|
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
|
+
}
|
|
48
79
|
}
|
|
49
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
|
@@ -59,7 +59,7 @@ export default class Selection {
|
|
|
59
59
|
}
|
|
60
60
|
|
|
61
61
|
update = ({ isDragging }) => {
|
|
62
|
-
if (isDragging) return
|
|
62
|
+
if (isDragging || this.DS.continue) return
|
|
63
63
|
this._handleInsideSelection()
|
|
64
64
|
}
|
|
65
65
|
|
|
@@ -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,
|
package/src/modules/Selector.js
CHANGED
|
@@ -47,11 +47,9 @@ export default class SelectorArea {
|
|
|
47
47
|
this.DS = DS
|
|
48
48
|
|
|
49
49
|
this.HTMLNode = createSelectorAreaElement(selectorAreaClass)
|
|
50
|
-
this.HTMLNode.appendChild(this.DS.Selector.HTMLNode)
|
|
51
|
-
const docEl = document.body ? 'body' : 'documentElement'
|
|
52
|
-
document[docEl].appendChild(this.HTMLNode)
|
|
53
50
|
|
|
54
51
|
this.DS.subscribe('Area:modified', this.updatePos)
|
|
52
|
+
this.DS.subscribe('Interaction:init', this.start)
|
|
55
53
|
this.DS.subscribe('Interaction:start', this.startAutoScroll)
|
|
56
54
|
this.DS.subscribe('Interaction:end', () => {
|
|
57
55
|
this.updatePos()
|
|
@@ -59,6 +57,19 @@ export default class SelectorArea {
|
|
|
59
57
|
})
|
|
60
58
|
}
|
|
61
59
|
|
|
60
|
+
start = () => this.applyElements('append')
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* Adding / Removing elements to document
|
|
64
|
+
* @param {'append'|'remove'} method
|
|
65
|
+
*/
|
|
66
|
+
applyElements = (method = 'append') => {
|
|
67
|
+
const docEl = document.body ? 'body' : 'documentElement'
|
|
68
|
+
const methodName = `${method}Child`
|
|
69
|
+
this.HTMLNode[methodName](this.DS.Selector.HTMLNode)
|
|
70
|
+
document[docEl][methodName](this.HTMLNode)
|
|
71
|
+
}
|
|
72
|
+
|
|
62
73
|
/** Updates the selectorAreas positions to match the areas */
|
|
63
74
|
updatePos = () => {
|
|
64
75
|
this._rect = null
|
|
@@ -75,6 +86,11 @@ export default class SelectorArea {
|
|
|
75
86
|
if (style.height !== height) style.height = height
|
|
76
87
|
}
|
|
77
88
|
|
|
89
|
+
stop = (remove) => {
|
|
90
|
+
this.stopAutoScroll()
|
|
91
|
+
if (remove) this.applyElements('remove')
|
|
92
|
+
}
|
|
93
|
+
|
|
78
94
|
//////////////////////////////////////////////////////////////////////////////////////
|
|
79
95
|
// AutoScroll
|
|
80
96
|
|
|
@@ -85,6 +101,7 @@ export default class SelectorArea {
|
|
|
85
101
|
|
|
86
102
|
/** Creates an interval that auto-scrolls while the cursor is near the edge */
|
|
87
103
|
handleAutoScroll = () => {
|
|
104
|
+
if (this.DS.continue) return
|
|
88
105
|
const {
|
|
89
106
|
stores: { PointerStore },
|
|
90
107
|
Area,
|
|
@@ -129,19 +146,24 @@ export default class SelectorArea {
|
|
|
129
146
|
|
|
130
147
|
/**
|
|
131
148
|
* checks if the click was triggered on the area.
|
|
149
|
+
* @param {DSEvent} [event]
|
|
132
150
|
* @returns {boolean}
|
|
133
151
|
*/
|
|
134
|
-
isClicked() {
|
|
152
|
+
isClicked(event) {
|
|
135
153
|
const {
|
|
136
154
|
stores: { PointerStore },
|
|
137
155
|
} = this.DS
|
|
138
156
|
|
|
157
|
+
const initialVal = event
|
|
158
|
+
? PointerStore.getPointerPosition(event)
|
|
159
|
+
: PointerStore.initialVal
|
|
160
|
+
|
|
139
161
|
return isCollision(
|
|
140
162
|
{
|
|
141
|
-
left:
|
|
142
|
-
top:
|
|
143
|
-
right:
|
|
144
|
-
bottom:
|
|
163
|
+
left: initialVal.x,
|
|
164
|
+
top: initialVal.y,
|
|
165
|
+
right: initialVal.x,
|
|
166
|
+
bottom: initialVal.y,
|
|
145
167
|
},
|
|
146
168
|
this.rect
|
|
147
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
|
@@ -13,6 +13,7 @@
|
|
|
13
13
|
* @property {HTMLElement} [selector=HTMLElement] the square that will draw the selection
|
|
14
14
|
* @property {boolean} [draggability=true] When a user is dragging on an already selected element, the selection is dragged.
|
|
15
15
|
* @property {boolean} [immediateDrag=true] Whether an element is draggable from the start or needs to be selected first
|
|
16
|
+
* @property {boolean} [keyboardDrag=true] Whether or not the user can drag with the keyboard (we don't recommend disabling it)
|
|
16
17
|
* @property {DSDragKeys} [dragKeys={up:['ArrowUp'],down:['ArrowDown'],left:['ArrowLeft'],righ:['ArrowRight']}] The keys available to drag element using the keyboard.
|
|
17
18
|
* @property {number} [keyboardDragSpeed=10] The speed at which elements are dragged using the keyboard. In pixels per keydown.
|
|
18
19
|
* @property {boolean} [useTransform=true] Whether to use hardware accelerated css transforms when dragging or top/left instead
|
|
@@ -33,9 +34,11 @@
|
|
|
33
34
|
* The Object that is passed back to any callback method
|
|
34
35
|
* @typedef {Object} CallbackObject
|
|
35
36
|
* @property {Array<HTMLElement|SVGElement|any>} [items] The items currently selected
|
|
36
|
-
* @property {MouseEvent|TouchEvent|Event} [event] The respective event object
|
|
37
|
+
* @property {MouseEvent|TouchEvent|KeyboardEvent|Event} [event] The respective event object
|
|
37
38
|
* @property {HTMLElement|SVGElement|any} [item] The single item currently interacted with
|
|
38
39
|
* @property {boolean} [isDragging] Whether the interaction is a drag or a select
|
|
40
|
+
* @property {boolean} [isDraggingKeyboard] Whether or not the drag interaction is via keyboard
|
|
41
|
+
* @property {string} [key] Pressed key (lowercase)
|
|
39
42
|
* @property {Array.<'top'|'bottom'|'left'|'right'|undefined>} [scroll_directions]
|
|
40
43
|
* @property {number} [scroll_multiplier]
|
|
41
44
|
*/
|
|
@@ -58,7 +61,8 @@
|
|
|
58
61
|
|
|
59
62
|
/** @typedef {'dragmove'|'autoscroll'|'dragstart'|'elementselect'|'elementunselect'|'callback'} DSEventNames */
|
|
60
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 */
|
|
61
|
-
/** @typedef {
|
|
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 */
|
|
62
66
|
|
|
63
67
|
/** @typedef {{top:number,left:number,bottom:number,right:number,width:number,height:number}} DSBoundingRect */
|
|
64
68
|
/** @typedef {{up:string[],down:string[],left:string[],right:string[]}} DSDragKeys */
|