dragselect 3.0.1 → 3.0.3
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 +5 -1
- package/dist/DragSelect.d.ts +877 -0
- package/dist/DragSelect.esm.js +2597 -0
- package/dist/DragSelect.js +2605 -0
- package/dist/ds.esm.min.js +1 -0
- package/dist/ds.min.js +1 -0
- package/package.json +9 -1
- package/deployment.md +0 -33
- package/jest-puppeteer.config.js +0 -16
- package/jest.config.js +0 -10
- package/rollup.config.mjs +0 -82
- package/src/DragSelect.ts +0 -425
- package/src/methods/addModificationObservers.ts +0 -51
- package/src/methods/canScroll.ts +0 -19
- package/src/methods/createSelectorAreaElement.ts +0 -8
- package/src/methods/createSelectorElement.ts +0 -13
- package/src/methods/debounce.ts +0 -31
- package/src/methods/ensureArray.ts +0 -13
- package/src/methods/getAllParentNodes.ts +0 -15
- package/src/methods/getAreaRect.ts +0 -27
- package/src/methods/getCurrentScroll.ts +0 -14
- package/src/methods/getDocumentScroll.ts +0 -5
- package/src/methods/getOverflowEdges.ts +0 -27
- package/src/methods/getPointerPos.ts +0 -9
- package/src/methods/getSelectionRect.ts +0 -29
- package/src/methods/getSelectorPosition.ts +0 -74
- package/src/methods/getStylePosition.spec.ts +0 -114
- package/src/methods/getStylePosition.ts +0 -83
- package/src/methods/handleElementPositionAttribute.ts +0 -15
- package/src/methods/handleKeyboardDragPosDifference.ts +0 -35
- package/src/methods/handleSelection.ts +0 -26
- package/src/methods/handleUnSelection.ts +0 -36
- package/src/methods/hydrateSettings.ts +0 -245
- package/src/methods/isCollision.ts +0 -66
- package/src/methods/limitDirection.ts +0 -36
- package/src/methods/moveElement.ts +0 -18
- package/src/methods/scrollElement.ts +0 -26
- package/src/methods/setStylePosition.ts +0 -16
- package/src/methods/subscriberAliases.ts +0 -240
- package/src/methods/updateElementStylePos.ts +0 -7
- package/src/methods/vect2.ts +0 -34
- package/src/modules/Area.ts +0 -169
- package/src/modules/Drag.ts +0 -236
- package/src/modules/DropZone.ts +0 -223
- package/src/modules/DropZones.ts +0 -151
- package/src/modules/Interaction.ts +0 -251
- package/src/modules/PubSub.ts +0 -111
- package/src/modules/SelectableSet.ts +0 -146
- package/src/modules/SelectedSet.ts +0 -92
- package/src/modules/Selection.ts +0 -133
- package/src/modules/Selector.ts +0 -81
- package/src/modules/SelectorArea.ts +0 -161
- package/src/stores/KeyStore.ts +0 -92
- package/src/stores/PointerStore.ts +0 -194
- package/src/stores/ScrollStore.ts +0 -87
- package/src/stores/SettingsStore.ts +0 -88
- package/src/types.ts +0 -158
- package/tsconfig.json +0 -105
package/src/modules/Selection.ts
DELETED
|
@@ -1,133 +0,0 @@
|
|
|
1
|
-
import DragSelect from '../DragSelect'
|
|
2
|
-
import PubSub from './PubSub'
|
|
3
|
-
import { DSBoundingRect, DSEvent, DSInputElement } from '../types'
|
|
4
|
-
import { DSSettings } from '../stores/SettingsStore'
|
|
5
|
-
import { handleSelection } from '../methods/handleSelection'
|
|
6
|
-
import { handleUnSelection } from '../methods/handleUnSelection'
|
|
7
|
-
import { isCollision } from '../methods/isCollision'
|
|
8
|
-
import { getSelectionRect } from '../methods/getSelectionRect'
|
|
9
|
-
|
|
10
|
-
export default class Selection<E extends DSInputElement> {
|
|
11
|
-
private _prevSelectedSet: Set<E> = new Set()
|
|
12
|
-
private _boundingRect?: DSBoundingRect
|
|
13
|
-
private _timeout?: NodeJS.Timeout
|
|
14
|
-
private DS: DragSelect<E>
|
|
15
|
-
private PS: PubSub<E>
|
|
16
|
-
private Settings: DSSettings<E>
|
|
17
|
-
|
|
18
|
-
constructor({ DS, PS }: { DS: DragSelect<E>; PS: PubSub<E> }) {
|
|
19
|
-
this.DS = DS
|
|
20
|
-
this.PS = PS
|
|
21
|
-
this.Settings = this.DS.stores.SettingsStore.s
|
|
22
|
-
this.PS.subscribe('Interaction:start', this.start)
|
|
23
|
-
this.PS.subscribe('Interaction:update', this.update)
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
/** Stores the previous selection (solves #9) */
|
|
27
|
-
private _storePrevious(event: DSEvent) {
|
|
28
|
-
const {
|
|
29
|
-
stores: { KeyStore },
|
|
30
|
-
SelectedSet,
|
|
31
|
-
} = this.DS
|
|
32
|
-
|
|
33
|
-
if (KeyStore.isMultiSelectKeyPressed(event))
|
|
34
|
-
this._prevSelectedSet = new Set(SelectedSet)
|
|
35
|
-
else this._prevSelectedSet = new Set()
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
private start = ({
|
|
39
|
-
event,
|
|
40
|
-
isDragging,
|
|
41
|
-
}: {
|
|
42
|
-
event: DSEvent
|
|
43
|
-
isDragging: boolean
|
|
44
|
-
}) => {
|
|
45
|
-
if (isDragging) return
|
|
46
|
-
this._storePrevious(event)
|
|
47
|
-
this._handleInsideSelection(true, event)
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
private update = ({ isDragging }: { isDragging?: boolean }) => {
|
|
51
|
-
if (isDragging || this.DS.continue) return
|
|
52
|
-
this._handleInsideSelection()
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
/** Checks if any selectable element is inside selection. */
|
|
56
|
-
private _handleInsideSelection = (force?: boolean, event?: DSEvent) => {
|
|
57
|
-
const { SelectableSet, SelectorArea, Selector } = this.DS
|
|
58
|
-
|
|
59
|
-
const multiSelectionToggle =
|
|
60
|
-
this.DS.stores.KeyStore.isMultiSelectKeyPressed(event) &&
|
|
61
|
-
this.Settings.multiSelectToggling
|
|
62
|
-
const selectionThreshold = this.Settings.selectionThreshold
|
|
63
|
-
|
|
64
|
-
const elRects = SelectableSet.rects
|
|
65
|
-
const selectorRect = Selector.rect
|
|
66
|
-
|
|
67
|
-
const select: Map<E, DSBoundingRect> = new Map()
|
|
68
|
-
const unselect: Map<E, DSBoundingRect> = new Map()
|
|
69
|
-
|
|
70
|
-
for (const [element, elementRect] of elRects) {
|
|
71
|
-
if (!SelectorArea.isInside(element, elementRect)) continue
|
|
72
|
-
if (isCollision(elementRect, selectorRect, selectionThreshold))
|
|
73
|
-
select.set(element, elementRect)
|
|
74
|
-
else unselect.set(element, elementRect)
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
if (this.DS.continue) return
|
|
78
|
-
|
|
79
|
-
// [PUBLICLY EXPOSED METHOD]
|
|
80
|
-
const { select: filteredSelect, unselect: filteredUnselect } =
|
|
81
|
-
this.filterSelected({ select, unselect, selectorRect })
|
|
82
|
-
|
|
83
|
-
filteredSelect.forEach((_, element) =>
|
|
84
|
-
handleSelection({
|
|
85
|
-
element,
|
|
86
|
-
force,
|
|
87
|
-
multiSelectionToggle,
|
|
88
|
-
SelectedSet: this.DS.SelectedSet,
|
|
89
|
-
hoverClassName: this.Settings.hoverClass,
|
|
90
|
-
})
|
|
91
|
-
)
|
|
92
|
-
filteredUnselect.forEach((_, element) =>
|
|
93
|
-
handleUnSelection({
|
|
94
|
-
element,
|
|
95
|
-
force,
|
|
96
|
-
SelectedSet: this.DS.SelectedSet,
|
|
97
|
-
hoverClassName: this.Settings.hoverClass,
|
|
98
|
-
PrevSelectedSet: this._prevSelectedSet,
|
|
99
|
-
})
|
|
100
|
-
)
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
get boundingRect() {
|
|
104
|
-
if (this._boundingRect) return this._boundingRect
|
|
105
|
-
this._boundingRect = getSelectionRect(this.DS.SelectedSet)
|
|
106
|
-
|
|
107
|
-
// since elements can be moved, we need to update the rects every X ms
|
|
108
|
-
if (this._timeout) clearTimeout(this._timeout)
|
|
109
|
-
this._timeout = setTimeout(
|
|
110
|
-
() => (this._boundingRect = undefined),
|
|
111
|
-
this.Settings.refreshMemoryRate
|
|
112
|
-
)
|
|
113
|
-
|
|
114
|
-
return this._boundingRect
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
////
|
|
118
|
-
// [PUBLICLY EXPOSED METHODS]
|
|
119
|
-
|
|
120
|
-
/**
|
|
121
|
-
* Can be overridden to apply further filtering logic after the items to select are identified but before they actually get selected
|
|
122
|
-
* Is expected to return the select / unselect maps in the same shape as passed in
|
|
123
|
-
*/
|
|
124
|
-
public filterSelected = ({
|
|
125
|
-
select,
|
|
126
|
-
unselect,
|
|
127
|
-
selectorRect,
|
|
128
|
-
}: {
|
|
129
|
-
select: Map<E, DSBoundingRect>
|
|
130
|
-
unselect: Map<E, DSBoundingRect>
|
|
131
|
-
selectorRect: DSBoundingRect
|
|
132
|
-
}) => ({ select, unselect })
|
|
133
|
-
}
|
package/src/modules/Selector.ts
DELETED
|
@@ -1,81 +0,0 @@
|
|
|
1
|
-
import DragSelect from "../DragSelect"
|
|
2
|
-
import { createSelectorElement } from "../methods/createSelectorElement";
|
|
3
|
-
import { getSelectorPosition } from "../methods/getSelectorPosition";
|
|
4
|
-
import updateElementStylePos from "../methods/updateElementStylePos";
|
|
5
|
-
import { vect2rect } from "../methods/vect2";
|
|
6
|
-
import { DSSettings } from "../stores/SettingsStore";
|
|
7
|
-
import { DSBoundingRect, DSInputElement } from "../types"
|
|
8
|
-
import PubSub from "./PubSub";
|
|
9
|
-
|
|
10
|
-
export default class Selector<E extends DSInputElement> {
|
|
11
|
-
private _rect?: DSBoundingRect
|
|
12
|
-
private DS: DragSelect<E>;
|
|
13
|
-
private PS: PubSub<E>;
|
|
14
|
-
private Settings: DSSettings<E>;
|
|
15
|
-
public HTMLNode: HTMLElement;
|
|
16
|
-
|
|
17
|
-
constructor({ DS, PS }: { DS: DragSelect<E>; PS: PubSub<E> }) {
|
|
18
|
-
this.DS = DS
|
|
19
|
-
this.PS = PS
|
|
20
|
-
this.Settings = this.DS.stores.SettingsStore.s
|
|
21
|
-
this.HTMLNode = this.Settings.selector // to make TS happy, will be replaced in `attachSelector`
|
|
22
|
-
|
|
23
|
-
this.PS.subscribe('Settings:updated:selectorClass', ({ settings }) => {
|
|
24
|
-
this.HTMLNode.classList.remove(settings['selectorClass:pre'])
|
|
25
|
-
this.HTMLNode.classList.add(settings.selectorClass)
|
|
26
|
-
})
|
|
27
|
-
this.PS.subscribe('Settings:updated:selector', this.attachSelector)
|
|
28
|
-
this.PS.subscribe('Settings:updated:customStyles', this.attachSelector)
|
|
29
|
-
this.attachSelector()
|
|
30
|
-
|
|
31
|
-
this.PS.subscribe('Interaction:start', this.start)
|
|
32
|
-
this.PS.subscribe('Interaction:update', this.update)
|
|
33
|
-
this.PS.subscribe('Interaction:end', this.stop)
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
private attachSelector = () => {
|
|
37
|
-
if (this.HTMLNode && this.DS.SelectorArea?.HTMLNode)
|
|
38
|
-
this.DS.SelectorArea.HTMLNode.removeChild(this.HTMLNode)
|
|
39
|
-
this.HTMLNode = this.Settings.selector || createSelectorElement(this.Settings.customStyles)
|
|
40
|
-
this.HTMLNode.classList.add(this.Settings.selectorClass)
|
|
41
|
-
if (this.HTMLNode && this.DS.SelectorArea?.HTMLNode)
|
|
42
|
-
this.DS.SelectorArea.HTMLNode.appendChild(this.HTMLNode)
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
private start = ({ isDragging }: { isDragging?: boolean }) => {
|
|
46
|
-
if (isDragging) return
|
|
47
|
-
const {
|
|
48
|
-
stores: { PointerStore },
|
|
49
|
-
} = this.DS
|
|
50
|
-
const pPos = PointerStore.initialValArea
|
|
51
|
-
updateElementStylePos(this.HTMLNode, vect2rect(pPos, 1))
|
|
52
|
-
this.HTMLNode.style.display = 'block'
|
|
53
|
-
this._rect = undefined
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
public stop = () => {
|
|
57
|
-
this.HTMLNode.style.width = '0'
|
|
58
|
-
this.HTMLNode.style.height = '0'
|
|
59
|
-
this.HTMLNode.style.display = 'none'
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
/** Moves the selection to the correct place */
|
|
63
|
-
private update = ({ isDragging }: { isDragging?: boolean }) => {
|
|
64
|
-
if (isDragging || this.DS.continue) return
|
|
65
|
-
const {
|
|
66
|
-
stores: { ScrollStore, PointerStore },
|
|
67
|
-
} = this.DS
|
|
68
|
-
const pos = getSelectorPosition({
|
|
69
|
-
scrollAmount: ScrollStore.scrollAmount,
|
|
70
|
-
initialPointerPos: PointerStore.initialValArea,
|
|
71
|
-
pointerPos: PointerStore.currentValArea,
|
|
72
|
-
})
|
|
73
|
-
updateElementStylePos(this.HTMLNode, pos)
|
|
74
|
-
this._rect = undefined
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
public get rect() {
|
|
78
|
-
if (this._rect) return this._rect
|
|
79
|
-
return (this._rect = this.HTMLNode.getBoundingClientRect())
|
|
80
|
-
}
|
|
81
|
-
}
|
|
@@ -1,161 +0,0 @@
|
|
|
1
|
-
import DragSelect from '../DragSelect'
|
|
2
|
-
import { createSelectorAreaElement } from '../methods/createSelectorAreaElement'
|
|
3
|
-
import { getOverflowEdges } from '../methods/getOverflowEdges'
|
|
4
|
-
import { isCollision } from '../methods/isCollision'
|
|
5
|
-
import { vect2rect } from '../methods/vect2'
|
|
6
|
-
import { DSSettings } from '../stores/SettingsStore'
|
|
7
|
-
import { DSBoundingRect, DSEdges, DSEvent, DSInputElement } from '../types'
|
|
8
|
-
import PubSub from './PubSub'
|
|
9
|
-
|
|
10
|
-
type AppendRemove = 'append' | 'remove'
|
|
11
|
-
|
|
12
|
-
export default class SelectorArea<E extends DSInputElement> {
|
|
13
|
-
private _scrollInterval?: NodeJS.Timeout
|
|
14
|
-
private _rect?: DSBoundingRect
|
|
15
|
-
private currentEdges: DSEdges = []
|
|
16
|
-
private DS: DragSelect<E>
|
|
17
|
-
private PS: PubSub<E>
|
|
18
|
-
private Settings: DSSettings<E>
|
|
19
|
-
public HTMLNode: HTMLElement
|
|
20
|
-
|
|
21
|
-
constructor({ DS, PS }: { DS: DragSelect<E>; PS: PubSub<E> }) {
|
|
22
|
-
this.DS = DS
|
|
23
|
-
this.PS = PS
|
|
24
|
-
this.Settings = this.DS.stores.SettingsStore.s
|
|
25
|
-
this.HTMLNode = createSelectorAreaElement()
|
|
26
|
-
|
|
27
|
-
this.PS.subscribe('Settings:updated:selectorAreaClass', ({ settings }) => {
|
|
28
|
-
this.HTMLNode.classList.remove(settings['selectorAreaClass:pre'])
|
|
29
|
-
this.HTMLNode.classList.add(settings['selectorAreaClass'])
|
|
30
|
-
})
|
|
31
|
-
this.HTMLNode.classList.add(this.Settings.selectorAreaClass)
|
|
32
|
-
|
|
33
|
-
this.PS.subscribe('Area:modified', this.updatePos)
|
|
34
|
-
this.PS.subscribe('Area:modified', this.updatePos)
|
|
35
|
-
this.PS.subscribe('Interaction:init', this.init)
|
|
36
|
-
this.PS.subscribe('Interaction:start', ({ isDraggingKeyboard }) =>
|
|
37
|
-
this.startAutoScroll({ isDraggingKeyboard })
|
|
38
|
-
)
|
|
39
|
-
this.PS.subscribe('Interaction:end', () => {
|
|
40
|
-
this.updatePos()
|
|
41
|
-
this.stopAutoScroll()
|
|
42
|
-
})
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
private init = () => {
|
|
46
|
-
this.applyElements('append')
|
|
47
|
-
this.updatePos()
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
/** Adding / Removing elements to document */
|
|
51
|
-
private applyElements = <K extends keyof AppendRemove>(
|
|
52
|
-
method: AppendRemove[K]
|
|
53
|
-
) => {
|
|
54
|
-
const docEl = document.body ? 'body' : 'documentElement'
|
|
55
|
-
const methodName = `${method}Child` as `${AppendRemove}Child`
|
|
56
|
-
this.HTMLNode[methodName](this.DS.Selector.HTMLNode)
|
|
57
|
-
document[docEl][methodName](this.HTMLNode)
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
/** Updates the selectorAreas positions to match the areas */
|
|
61
|
-
private updatePos = () => {
|
|
62
|
-
this._rect = undefined
|
|
63
|
-
const rect = this.DS.Area.rect
|
|
64
|
-
const border = this.DS.Area.computedBorder
|
|
65
|
-
const { style } = this.HTMLNode
|
|
66
|
-
const top = `${rect.top + border.top}px`
|
|
67
|
-
const left = `${rect.left + border.left}px`
|
|
68
|
-
const width = `${rect.width}px`
|
|
69
|
-
const height = `${rect.height}px`
|
|
70
|
-
if (style.top !== top) style.top = top
|
|
71
|
-
if (style.left !== left) style.left = left
|
|
72
|
-
if (style.width !== width) style.width = width
|
|
73
|
-
if (style.height !== height) style.height = height
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
public stop = (remove: boolean) => {
|
|
77
|
-
this.stopAutoScroll()
|
|
78
|
-
if (remove) this.applyElements('remove')
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
//////////////////////////////////////////////////////////////////////////////////////
|
|
82
|
-
// AutoScroll
|
|
83
|
-
|
|
84
|
-
private startAutoScroll = ({
|
|
85
|
-
isDraggingKeyboard,
|
|
86
|
-
}: {
|
|
87
|
-
isDraggingKeyboard?: boolean
|
|
88
|
-
}) => {
|
|
89
|
-
if (isDraggingKeyboard) return
|
|
90
|
-
this.currentEdges = []
|
|
91
|
-
this._scrollInterval = setInterval(() => this.handleAutoScroll(), 16)
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
/** Creates an interval that auto-scrolls while the cursor is near the edge */
|
|
95
|
-
private handleAutoScroll = () => {
|
|
96
|
-
if (this.DS.continue) return
|
|
97
|
-
const {
|
|
98
|
-
stores: { PointerStore },
|
|
99
|
-
Area,
|
|
100
|
-
} = this.DS
|
|
101
|
-
|
|
102
|
-
this.currentEdges = getOverflowEdges({
|
|
103
|
-
elementRect: vect2rect(PointerStore.currentVal),
|
|
104
|
-
containerRect: this.rect,
|
|
105
|
-
tolerance: this.Settings.overflowTolerance,
|
|
106
|
-
})
|
|
107
|
-
|
|
108
|
-
if (this.currentEdges.length)
|
|
109
|
-
Area.scroll(this.currentEdges, this.Settings.autoScrollSpeed)
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
private stopAutoScroll = () => {
|
|
113
|
-
this.currentEdges = []
|
|
114
|
-
clearInterval(this._scrollInterval)
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
//////////////////////////////////////////////////////////////////////////////////////
|
|
118
|
-
// Booleans
|
|
119
|
-
|
|
120
|
-
/**
|
|
121
|
-
* Checks if the element is either inside the Selector Area (as a reachable child or touching the area)
|
|
122
|
-
* @param elementRect - slight performance improvements when passed
|
|
123
|
-
*/
|
|
124
|
-
public isInside = (element: E, elementRect?: DSBoundingRect) => {
|
|
125
|
-
if (
|
|
126
|
-
this.DS.Area.HTMLNode.contains(element) &&
|
|
127
|
-
this.DS.stores.ScrollStore.canScroll
|
|
128
|
-
)
|
|
129
|
-
return true
|
|
130
|
-
return isCollision(
|
|
131
|
-
this.rect,
|
|
132
|
-
elementRect || element.getBoundingClientRect()
|
|
133
|
-
)
|
|
134
|
-
}
|
|
135
|
-
|
|
136
|
-
/** checks if the click was triggered on the area. */
|
|
137
|
-
public isClicked(event?: DSEvent) {
|
|
138
|
-
const {
|
|
139
|
-
stores: { PointerStore },
|
|
140
|
-
} = this.DS
|
|
141
|
-
|
|
142
|
-
const initialVal = event
|
|
143
|
-
? PointerStore.getPointerPosition(event)
|
|
144
|
-
: PointerStore.initialVal
|
|
145
|
-
|
|
146
|
-
return isCollision(
|
|
147
|
-
{
|
|
148
|
-
left: initialVal.x,
|
|
149
|
-
top: initialVal.y,
|
|
150
|
-
right: initialVal.x,
|
|
151
|
-
bottom: initialVal.y,
|
|
152
|
-
},
|
|
153
|
-
this.rect
|
|
154
|
-
)
|
|
155
|
-
}
|
|
156
|
-
|
|
157
|
-
public get rect() {
|
|
158
|
-
if (this._rect) return this._rect
|
|
159
|
-
return (this._rect = this.HTMLNode.getBoundingClientRect())
|
|
160
|
-
}
|
|
161
|
-
}
|
package/src/stores/KeyStore.ts
DELETED
|
@@ -1,92 +0,0 @@
|
|
|
1
|
-
import DragSelect from '../DragSelect'
|
|
2
|
-
import PubSub from '../modules/PubSub'
|
|
3
|
-
import { DSInputElement, Settings } from '../types'
|
|
4
|
-
|
|
5
|
-
export type DSKeyStorePublishEventNames = "KeyStore:down:pre"|"KeyStore:down"|"KeyStore:up:pre"|"KeyStore:up"
|
|
6
|
-
|
|
7
|
-
export type DSKeyStorePublishEventData = {
|
|
8
|
-
event: KeyboardEvent;
|
|
9
|
-
/** Pressed key (lowercase) */
|
|
10
|
-
key: string;
|
|
11
|
-
};
|
|
12
|
-
|
|
13
|
-
export type DSKeyStorePublish = {
|
|
14
|
-
[K in DSKeyStorePublishEventNames]: DSKeyStorePublishEventData
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
type KeyMapping = { [key in 'control'|'shift'|'meta']: 'ctrlKey'|'shiftKey'|'metaKey' }
|
|
18
|
-
|
|
19
|
-
export default class KeyStore<E extends DSInputElement> {
|
|
20
|
-
private _currentValues = new Set<string>()
|
|
21
|
-
private _keyMapping: KeyMapping = {
|
|
22
|
-
control: 'ctrlKey',
|
|
23
|
-
shift: 'shiftKey',
|
|
24
|
-
meta: 'metaKey',
|
|
25
|
-
}
|
|
26
|
-
private DS: DragSelect<E>
|
|
27
|
-
private PS: PubSub<E>
|
|
28
|
-
private settings: Required<Settings<E>>
|
|
29
|
-
|
|
30
|
-
/**
|
|
31
|
-
* @class KeyStore
|
|
32
|
-
* @constructor KeyStore
|
|
33
|
-
* @ignore
|
|
34
|
-
*/
|
|
35
|
-
constructor({ DS, PS }: { DS: DragSelect<E>, PS: PubSub<E> }) {
|
|
36
|
-
this.DS = DS
|
|
37
|
-
this.PS = PS
|
|
38
|
-
this.settings = this.DS.stores.SettingsStore.s
|
|
39
|
-
this.PS.subscribe('Interaction:init', this.init)
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
private init = () => {
|
|
43
|
-
document.addEventListener('keydown', this.keydown)
|
|
44
|
-
document.addEventListener('keyup', this.keyup)
|
|
45
|
-
window.addEventListener('blur', this.reset)
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
private keydown = (event: KeyboardEvent) => {
|
|
49
|
-
if(!event.key?.toLocaleLowerCase) return
|
|
50
|
-
const key = event.key.toLowerCase()
|
|
51
|
-
this.PS.publish('KeyStore:down:pre', { event, key })
|
|
52
|
-
this._currentValues.add(key)
|
|
53
|
-
this.PS.publish('KeyStore:down', { event, key })
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
private keyup = (event: KeyboardEvent) => {
|
|
57
|
-
if(!event.key?.toLocaleLowerCase) return
|
|
58
|
-
const key = event.key.toLowerCase()
|
|
59
|
-
this.PS.publish('KeyStore:up:pre', { event, key })
|
|
60
|
-
this._currentValues.delete(key)
|
|
61
|
-
this.PS.publish('KeyStore:up', { event, key })
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
public stop = () => {
|
|
65
|
-
document.removeEventListener('keydown', this.keydown)
|
|
66
|
-
document.removeEventListener('keyup', this.reset)
|
|
67
|
-
window.removeEventListener('blur', this.reset)
|
|
68
|
-
this.reset()
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
private reset = () => this._currentValues.clear()
|
|
72
|
-
|
|
73
|
-
public isMultiSelectKeyPressed(event?: KeyboardEvent|MouseEvent|PointerEvent|TouchEvent) {
|
|
74
|
-
if(this.settings.multiSelectMode) return true
|
|
75
|
-
|
|
76
|
-
const multiSelectKeys = this.settings.multiSelectKeys?.map(
|
|
77
|
-
(key) => key.toLocaleLowerCase() as keyof KeyMapping
|
|
78
|
-
) ?? []
|
|
79
|
-
|
|
80
|
-
if (this.currentValues.some((key) => multiSelectKeys.includes(key as keyof KeyMapping)))
|
|
81
|
-
return true
|
|
82
|
-
|
|
83
|
-
if (event && multiSelectKeys.some((key) => event[this._keyMapping[key]]))
|
|
84
|
-
return true
|
|
85
|
-
|
|
86
|
-
return false
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
public get currentValues() {
|
|
90
|
-
return Array.from(this._currentValues.values())
|
|
91
|
-
}
|
|
92
|
-
}
|
|
@@ -1,194 +0,0 @@
|
|
|
1
|
-
import DragSelect from '../DragSelect'
|
|
2
|
-
import PubSub from '../modules/PubSub'
|
|
3
|
-
import { DSEvent, DSInputElement, Settings, Vect2 } from '../types'
|
|
4
|
-
import { getPointerPos } from '../methods/getPointerPos'
|
|
5
|
-
import { rect2vect, calcVect } from '../methods/vect2'
|
|
6
|
-
import { InteractionEvent } from '../modules/Interaction'
|
|
7
|
-
|
|
8
|
-
export type DSPointerStorePublishEventNames =
|
|
9
|
-
| 'PointerStore:updated:pre'
|
|
10
|
-
| 'PointerStore:updated'
|
|
11
|
-
|
|
12
|
-
export type DSPointerStorePublishEventData = {
|
|
13
|
-
event: PointerEvent | MouseEvent | TouchEvent
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
export type DSPointerStorePublish = {
|
|
17
|
-
[K in DSPointerStorePublishEventNames]: DSPointerStorePublishEventData
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
export default class PointerStore<E extends DSInputElement> {
|
|
21
|
-
private _isMouseInteraction = false
|
|
22
|
-
// Position relative to area
|
|
23
|
-
private _initialValArea: Vect2 = { x: 0, y: 0 }
|
|
24
|
-
private _currentValArea: Vect2 = { x: 0, y: 0 }
|
|
25
|
-
private _lastValArea: Vect2 = { x: 0, y: 0 }
|
|
26
|
-
// General Pointer Position
|
|
27
|
-
private _initialVal: Vect2 = { x: 0, y: 0 }
|
|
28
|
-
private _currentVal: Vect2 = { x: 0, y: 0 }
|
|
29
|
-
private _lastVal: Vect2 = { x: 0, y: 0 }
|
|
30
|
-
private _lastTouch?: TouchEvent
|
|
31
|
-
|
|
32
|
-
private DS: DragSelect<E>
|
|
33
|
-
private PS: PubSub<E>
|
|
34
|
-
private settings: Required<Settings<E>>
|
|
35
|
-
|
|
36
|
-
constructor({ DS, PS }: { DS: DragSelect<E>; PS: PubSub<E> }) {
|
|
37
|
-
this.DS = DS
|
|
38
|
-
this.PS = PS
|
|
39
|
-
this.settings = this.DS.stores.SettingsStore.s
|
|
40
|
-
this.PS.subscribe('Interaction:init', this.init)
|
|
41
|
-
this.PS.subscribe('Interaction:start', ({ event }) => this.start(event))
|
|
42
|
-
this.PS.subscribe('Interaction:end', ({ event }) => this.reset(event))
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
private init = () => {
|
|
46
|
-
if (this.settings.usePointerEvents)
|
|
47
|
-
document.addEventListener('pointermove', this.update, { passive: false })
|
|
48
|
-
else document.addEventListener('mousemove', this.update)
|
|
49
|
-
|
|
50
|
-
document.addEventListener('touchmove', this.update, { passive: false })
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
public start(event?: DSEvent) {
|
|
54
|
-
if (!event) return
|
|
55
|
-
this._isMouseInteraction = true
|
|
56
|
-
this.currentVal = this.initialVal = this.getPointerPosition(event)
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
public getPointerPosition = (event?: DSEvent) =>
|
|
60
|
-
getPointerPos({
|
|
61
|
-
event: this._normalizedEvent(event),
|
|
62
|
-
})
|
|
63
|
-
|
|
64
|
-
private update = (event: Event) => {
|
|
65
|
-
// type Event to satisfy event listeners, but we know type is : event as InteractionEvent
|
|
66
|
-
if (!event) return
|
|
67
|
-
this.PS.publish('PointerStore:updated:pre', {
|
|
68
|
-
event: event as InteractionEvent,
|
|
69
|
-
})
|
|
70
|
-
this.currentVal = this.getPointerPosition(event as InteractionEvent)
|
|
71
|
-
if (!this._isMouseInteraction) return
|
|
72
|
-
this.PS.publish('PointerStore:updated', {
|
|
73
|
-
event: event as InteractionEvent,
|
|
74
|
-
})
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
public stop = () => {
|
|
78
|
-
// @TODO: fix pointer events mixing issue see [PR](https://github.com/ThibaultJanBeyer/DragSelect/pull/128#issuecomment-1154885289)
|
|
79
|
-
if (this.settings.usePointerEvents)
|
|
80
|
-
document.removeEventListener('pointermove', this.update, {
|
|
81
|
-
// @ts-ignore
|
|
82
|
-
passive: false,
|
|
83
|
-
})
|
|
84
|
-
else document.removeEventListener('mousemove', this.update)
|
|
85
|
-
|
|
86
|
-
document.removeEventListener('touchmove', this.update, {
|
|
87
|
-
// @ts-ignore
|
|
88
|
-
passive: false,
|
|
89
|
-
})
|
|
90
|
-
|
|
91
|
-
this.reset()
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
private reset = (event?: DSEvent) => {
|
|
95
|
-
this.currentVal = this.lastVal = this.getPointerPosition(event)
|
|
96
|
-
// debounce in order "onClick" to work
|
|
97
|
-
setTimeout(() => (this._isMouseInteraction = false), 100)
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
private _normalizedEvent(event?: DSEvent): MouseEvent | PointerEvent | Touch {
|
|
101
|
-
// null KeyboardEvents
|
|
102
|
-
if (!event || event instanceof KeyboardEvent)
|
|
103
|
-
return { clientX: 0, clientY: 0 } as MouseEvent
|
|
104
|
-
// touchend has not touches. so we take the last touch if a touchevent, we need to store the positions
|
|
105
|
-
if ('touches' in event) {
|
|
106
|
-
if (event.type !== 'touchend') this._lastTouch = event
|
|
107
|
-
// if a touchevent, return the last touch rather than the regular event
|
|
108
|
-
// as we need .touches[0] from that event
|
|
109
|
-
return this._lastTouch?.touches[0] || event.touches[0]
|
|
110
|
-
}
|
|
111
|
-
return event
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
/** First recorded pointer position within the area */
|
|
115
|
-
public get initialValArea() {
|
|
116
|
-
if (!this._initialValArea) return { x: 0, y: 0 }
|
|
117
|
-
return this._initialValArea
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
/** Current pointer position within the area */
|
|
121
|
-
public get currentValArea() {
|
|
122
|
-
if (!this._currentValArea) return { x: 0, y: 0 }
|
|
123
|
-
return this._currentValArea
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
/** Last recorded pointer position within the area */
|
|
127
|
-
public get lastValArea() {
|
|
128
|
-
if (!this._lastValArea) return { x: 0, y: 0 }
|
|
129
|
-
return this._lastValArea
|
|
130
|
-
}
|
|
131
|
-
|
|
132
|
-
/** First recorded pointer position */
|
|
133
|
-
public get initialVal() {
|
|
134
|
-
if (!this._initialVal) return { x: 0, y: 0 }
|
|
135
|
-
return this._initialVal
|
|
136
|
-
}
|
|
137
|
-
|
|
138
|
-
/** Current pointer position */
|
|
139
|
-
public get currentVal() {
|
|
140
|
-
if (!this._currentVal) return { x: 0, y: 0 }
|
|
141
|
-
return this._currentVal
|
|
142
|
-
}
|
|
143
|
-
|
|
144
|
-
/** Last recorded pointer position */
|
|
145
|
-
public get lastVal() {
|
|
146
|
-
if (!this._lastVal) return { x: 0, y: 0 }
|
|
147
|
-
return this._lastVal
|
|
148
|
-
}
|
|
149
|
-
|
|
150
|
-
public set initialVal(value) {
|
|
151
|
-
this._initialVal = value
|
|
152
|
-
this._initialValArea =
|
|
153
|
-
value &&
|
|
154
|
-
calcVect(
|
|
155
|
-
value,
|
|
156
|
-
'-',
|
|
157
|
-
calcVect(
|
|
158
|
-
rect2vect(this.DS.Area.rect),
|
|
159
|
-
'+',
|
|
160
|
-
rect2vect(this.DS.Area.computedBorder)
|
|
161
|
-
)
|
|
162
|
-
)
|
|
163
|
-
}
|
|
164
|
-
|
|
165
|
-
public set currentVal(value) {
|
|
166
|
-
this._currentVal = value
|
|
167
|
-
this._currentValArea =
|
|
168
|
-
value &&
|
|
169
|
-
calcVect(
|
|
170
|
-
value,
|
|
171
|
-
'-',
|
|
172
|
-
calcVect(
|
|
173
|
-
rect2vect(this.DS.Area.rect),
|
|
174
|
-
'+',
|
|
175
|
-
rect2vect(this.DS.Area.computedBorder)
|
|
176
|
-
)
|
|
177
|
-
)
|
|
178
|
-
}
|
|
179
|
-
|
|
180
|
-
public set lastVal(value) {
|
|
181
|
-
this._lastVal = value
|
|
182
|
-
this._lastValArea =
|
|
183
|
-
value &&
|
|
184
|
-
calcVect(
|
|
185
|
-
value,
|
|
186
|
-
'-',
|
|
187
|
-
calcVect(
|
|
188
|
-
rect2vect(this.DS.Area.rect),
|
|
189
|
-
'+',
|
|
190
|
-
rect2vect(this.DS.Area.computedBorder)
|
|
191
|
-
)
|
|
192
|
-
)
|
|
193
|
-
}
|
|
194
|
-
}
|