dragselect 2.7.4 → 3.0.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 +97 -23
- package/LICENSE +690 -0
- package/README.md +2 -1
- package/jest-puppeteer.config.js +13 -3
- package/jest.config.js +5 -0
- package/package.json +40 -46
- package/rollup.config.mjs +82 -0
- package/src/DragSelect.ts +425 -0
- package/src/methods/addModificationObservers.ts +51 -0
- package/src/methods/canScroll.ts +19 -0
- package/src/methods/{createSelectorAreaElement.js → createSelectorAreaElement.ts} +1 -8
- package/src/methods/{createSelectorElement.js → createSelectorElement.ts} +1 -9
- package/src/methods/{debounce.js → debounce.ts} +5 -11
- package/src/methods/ensureArray.ts +13 -0
- package/src/methods/getAllParentNodes.ts +15 -0
- package/src/methods/{getAreaRect.js → getAreaRect.ts} +2 -6
- package/src/methods/getCurrentScroll.ts +14 -0
- package/src/methods/{documentScroll.js → getDocumentScroll.ts} +2 -2
- package/src/methods/{getOverflowEdges.js → getOverflowEdges.ts} +11 -11
- package/src/methods/getPointerPos.ts +9 -0
- package/src/methods/getSelectionRect.ts +29 -0
- package/src/methods/{getSelectorPosition.js → getSelectorPosition.ts} +11 -9
- package/src/methods/{getStylePosition.spec.js → getStylePosition.spec.ts} +9 -9
- package/src/methods/{getStylePosition.js → getStylePosition.ts} +5 -21
- package/src/methods/handleElementPositionAttribute.ts +15 -0
- package/src/methods/handleKeyboardDragPosDifference.ts +35 -0
- package/src/methods/handleSelection.ts +26 -0
- package/src/methods/{handleUnSelection.js → handleUnSelection.ts} +14 -14
- package/src/methods/{hydrateSettings.js → hydrateSettings.ts} +15 -26
- package/src/methods/isCollision.ts +66 -0
- package/src/methods/limitDirection.ts +36 -0
- package/src/methods/moveElement.ts +18 -0
- package/src/methods/{scrollElement.js → scrollElement.ts} +4 -10
- package/src/methods/{setStylePosition.js → setStylePosition.ts} +3 -11
- package/src/methods/subscriberAliases.ts +240 -0
- package/src/methods/updateElementStylePos.ts +7 -0
- package/src/methods/vect2.ts +34 -0
- package/src/modules/Area.ts +169 -0
- package/src/modules/Drag.ts +236 -0
- package/src/modules/{DropZone.js → DropZone.ts} +80 -119
- package/src/modules/DropZones.ts +151 -0
- package/src/modules/Interaction.ts +251 -0
- package/src/modules/PubSub.ts +111 -0
- package/src/modules/SelectableSet.ts +146 -0
- package/src/modules/SelectedSet.ts +92 -0
- package/src/modules/{Selection.js → Selection.ts} +59 -44
- package/src/modules/Selector.ts +81 -0
- package/src/modules/{SelectorArea.js → SelectorArea.ts} +58 -71
- package/src/stores/KeyStore.ts +92 -0
- package/src/stores/PointerStore.ts +194 -0
- package/src/stores/ScrollStore.ts +87 -0
- package/src/stores/SettingsStore.ts +88 -0
- package/src/types.ts +158 -0
- package/tsconfig.json +105 -0
- package/bower.json +0 -24
- package/dist/DragSelect.d.ts +0 -200
- package/dist/DragSelect.es6m.js +0 -3886
- package/dist/DragSelect.js +0 -3894
- package/dist/ds.es6m.min.js +0 -1
- package/dist/ds.min.js +0 -1
- package/dist/methods/addModificationObservers.d.ts +0 -11
- package/dist/methods/canScroll.d.ts +0 -3
- package/dist/methods/createSelectorAreaElement.d.ts +0 -3
- package/dist/methods/createSelectorElement.d.ts +0 -3
- package/dist/methods/debounce.d.ts +0 -3
- package/dist/methods/documentScroll.d.ts +0 -6
- package/dist/methods/getAllParentNodes.d.ts +0 -3
- package/dist/methods/getAreaRect.d.ts +0 -3
- package/dist/methods/getBoundingClientRect.d.ts +0 -4
- package/dist/methods/getCurrentScroll.d.ts +0 -3
- package/dist/methods/getOverflowEdges.d.ts +0 -7
- package/dist/methods/getPointerPos.d.ts +0 -5
- package/dist/methods/getSelectorPosition.d.ts +0 -12
- package/dist/methods/getStylePosition.d.ts +0 -3
- package/dist/methods/handleElementOverflow.d.ts +0 -10
- package/dist/methods/handleElementPositionAttribute.d.ts +0 -6
- package/dist/methods/handleKeyboardDragPosDifference.d.ts +0 -13
- package/dist/methods/handleSelection.d.ts +0 -9
- package/dist/methods/handleUnSelection.d.ts +0 -9
- package/dist/methods/hydrateSettings.d.ts +0 -3
- package/dist/methods/index.d.ts +0 -30
- package/dist/methods/isCollision.d.ts +0 -13
- package/dist/methods/moveElement.d.ts +0 -8
- package/dist/methods/removeModificationObservers.d.ts +0 -3
- package/dist/methods/scrollElement.d.ts +0 -3
- package/dist/methods/setStylePosition.d.ts +0 -3
- package/dist/methods/subscriberAliases.d.ts +0 -14
- package/dist/methods/toArray.d.ts +0 -3
- package/dist/methods/updateElementStylePos.d.ts +0 -8
- package/dist/methods/vect2.d.ts +0 -8
- package/dist/modules/Area.d.ts +0 -80
- package/dist/modules/Drag.d.ts +0 -71
- package/dist/modules/DropZone.d.ts +0 -90
- package/dist/modules/DropZones.d.ts +0 -73
- package/dist/modules/Interaction.d.ts +0 -51
- package/dist/modules/PubSub.d.ts +0 -37
- package/dist/modules/SelectableSet.d.ts +0 -43
- package/dist/modules/SelectedSet.d.ts +0 -32
- package/dist/modules/Selection.d.ts +0 -54
- package/dist/modules/Selector.d.ts +0 -29
- package/dist/modules/SelectorArea.d.ts +0 -58
- package/dist/modules/index.d.ts +0 -11
- package/dist/stores/KeyStore.d.ts +0 -34
- package/dist/stores/PointerStore.d.ts +0 -83
- package/dist/stores/ScrollStore.d.ts +0 -38
- package/dist/stores/SettingsStore.d.ts +0 -39
- package/dist/stores/index.d.ts +0 -5
- package/dist/types.d.ts +0 -276
- package/jsconfig.json +0 -13
- package/rollup.config.js +0 -101
- package/src/DragSelect.js +0 -384
- package/src/methods/addModificationObservers.js +0 -38
- package/src/methods/canScroll.js +0 -20
- package/src/methods/getAllParentNodes.js +0 -17
- package/src/methods/getBoundingClientRect.js +0 -33
- package/src/methods/getCurrentScroll.js +0 -15
- package/src/methods/getPointerPos.js +0 -13
- package/src/methods/handleElementOverflow.js +0 -65
- package/src/methods/handleElementPositionAttribute.js +0 -16
- package/src/methods/handleKeyboardDragPosDifference.js +0 -57
- package/src/methods/handleSelection.js +0 -26
- package/src/methods/index.js +0 -29
- package/src/methods/isCollision.js +0 -58
- package/src/methods/moveElement.js +0 -39
- package/src/methods/removeModificationObservers.js +0 -13
- package/src/methods/subscriberAliases.js +0 -72
- package/src/methods/toArray.js +0 -18
- package/src/methods/updateElementStylePos.js +0 -15
- package/src/methods/vect2.js +0 -54
- package/src/modules/Area.js +0 -183
- package/src/modules/Drag.js +0 -257
- package/src/modules/DropZones.js +0 -161
- package/src/modules/Interaction.js +0 -208
- package/src/modules/PubSub.js +0 -78
- package/src/modules/SelectableSet.js +0 -149
- package/src/modules/SelectedSet.js +0 -72
- package/src/modules/Selector.js +0 -90
- package/src/modules/index.js +0 -10
- package/src/stores/KeyStore.js +0 -83
- package/src/stores/PointerStore.js +0 -218
- package/src/stores/ScrollStore.js +0 -92
- package/src/stores/SettingsStore.js +0 -66
- package/src/stores/index.js +0 -4
- package/src/types.js +0 -100
|
@@ -1,34 +1,30 @@
|
|
|
1
|
-
// @ts-check
|
|
2
|
-
import '../types'
|
|
3
1
|
import DragSelect from '../DragSelect'
|
|
4
|
-
|
|
5
|
-
import {
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
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> }) {
|
|
20
19
|
this.DS = DS
|
|
20
|
+
this.PS = PS
|
|
21
21
|
this.Settings = this.DS.stores.SettingsStore.s
|
|
22
|
-
this.
|
|
23
|
-
this.
|
|
22
|
+
this.PS.subscribe('Interaction:start', this.start)
|
|
23
|
+
this.PS.subscribe('Interaction:update', this.update)
|
|
24
24
|
}
|
|
25
25
|
|
|
26
|
-
/**
|
|
27
|
-
|
|
28
|
-
* @param {DSEvent} event
|
|
29
|
-
* @private
|
|
30
|
-
* */
|
|
31
|
-
_storePrevious(event) {
|
|
26
|
+
/** Stores the previous selection (solves #9) */
|
|
27
|
+
private _storePrevious(event: DSEvent) {
|
|
32
28
|
const {
|
|
33
29
|
stores: { KeyStore },
|
|
34
30
|
SelectedSet,
|
|
@@ -39,25 +35,25 @@ export default class Selection {
|
|
|
39
35
|
else this._prevSelectedSet = new Set()
|
|
40
36
|
}
|
|
41
37
|
|
|
42
|
-
|
|
43
|
-
|
|
38
|
+
private start = ({
|
|
39
|
+
event,
|
|
40
|
+
isDragging,
|
|
41
|
+
}: {
|
|
42
|
+
event: DSEvent
|
|
43
|
+
isDragging: boolean
|
|
44
|
+
}) => {
|
|
44
45
|
if (isDragging) return
|
|
45
46
|
this._storePrevious(event)
|
|
46
47
|
this._handleInsideSelection(true, event)
|
|
47
48
|
}
|
|
48
49
|
|
|
49
|
-
update = ({ isDragging }) => {
|
|
50
|
+
private update = ({ isDragging }: { isDragging?: boolean }) => {
|
|
50
51
|
if (isDragging || this.DS.continue) return
|
|
51
52
|
this._handleInsideSelection()
|
|
52
53
|
}
|
|
53
54
|
|
|
54
|
-
/**
|
|
55
|
-
|
|
56
|
-
* @param {boolean} [force]
|
|
57
|
-
* @param {DSEvent} [event]
|
|
58
|
-
* @private
|
|
59
|
-
*/
|
|
60
|
-
_handleInsideSelection = (force, event) => {
|
|
55
|
+
/** Checks if any selectable element is inside selection. */
|
|
56
|
+
private _handleInsideSelection = (force?: boolean, event?: DSEvent) => {
|
|
61
57
|
const { SelectableSet, SelectorArea, Selector } = this.DS
|
|
62
58
|
|
|
63
59
|
const multiSelectionToggle =
|
|
@@ -65,14 +61,11 @@ export default class Selection {
|
|
|
65
61
|
this.Settings.multiSelectToggling
|
|
66
62
|
const selectionThreshold = this.Settings.selectionThreshold
|
|
67
63
|
|
|
68
|
-
/** @type {any} */
|
|
69
64
|
const elRects = SelectableSet.rects
|
|
70
65
|
const selectorRect = Selector.rect
|
|
71
66
|
|
|
72
|
-
|
|
73
|
-
const
|
|
74
|
-
/** @type {Map<DSElement,DSBoundingRect>} */
|
|
75
|
-
const unselect = new Map()
|
|
67
|
+
const select: Map<E, DSBoundingRect> = new Map()
|
|
68
|
+
const unselect: Map<E, DSBoundingRect> = new Map()
|
|
76
69
|
|
|
77
70
|
for (const [element, elementRect] of elRects) {
|
|
78
71
|
if (!SelectorArea.isInside(element, elementRect)) continue
|
|
@@ -83,6 +76,7 @@ export default class Selection {
|
|
|
83
76
|
|
|
84
77
|
if (this.DS.continue) return
|
|
85
78
|
|
|
79
|
+
// [PUBLICLY EXPOSED METHOD]
|
|
86
80
|
const { select: filteredSelect, unselect: filteredUnselect } =
|
|
87
81
|
this.filterSelected({ select, unselect, selectorRect })
|
|
88
82
|
|
|
@@ -106,13 +100,34 @@ export default class Selection {
|
|
|
106
100
|
)
|
|
107
101
|
}
|
|
108
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
|
+
////
|
|
109
118
|
// [PUBLICLY EXPOSED METHODS]
|
|
110
119
|
|
|
111
120
|
/**
|
|
112
121
|
* Can be overridden to apply further filtering logic after the items to select are identified but before they actually get selected
|
|
113
122
|
* Is expected to return the select / unselect maps in the same shape as passed in
|
|
114
|
-
* @param {{select:Map<DSElement,DSBoundingRect>, unselect:Map<DSElement,DSBoundingRect>, selectorRect:DSBoundingRect}} obj
|
|
115
|
-
* @returns {{select:Map<DSElement,DSBoundingRect>, unselect:Map<DSElement,DSBoundingRect>}}
|
|
116
123
|
*/
|
|
117
|
-
filterSelected = ({
|
|
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 })
|
|
118
133
|
}
|
|
@@ -0,0 +1,81 @@
|
|
|
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,76 +1,65 @@
|
|
|
1
|
-
// @ts-check
|
|
2
|
-
import '../types.js'
|
|
3
1
|
import DragSelect from '../DragSelect'
|
|
4
|
-
import {
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
} from '../
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
* @private
|
|
25
|
-
*/
|
|
26
|
-
currentEdges = []
|
|
27
|
-
|
|
28
|
-
/**
|
|
29
|
-
* @class SelectorArea
|
|
30
|
-
* @constructor SelectorArea
|
|
31
|
-
* @param {{ DS:DragSelect }} obj
|
|
32
|
-
* @ignore
|
|
33
|
-
*/
|
|
34
|
-
constructor({ DS }) {
|
|
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> }) {
|
|
35
22
|
this.DS = DS
|
|
36
|
-
|
|
23
|
+
this.PS = PS
|
|
24
|
+
this.Settings = this.DS.stores.SettingsStore.s
|
|
37
25
|
this.HTMLNode = createSelectorAreaElement()
|
|
38
|
-
|
|
39
|
-
this.
|
|
26
|
+
|
|
27
|
+
this.PS.subscribe('Settings:updated:selectorAreaClass', ({ settings }) => {
|
|
40
28
|
this.HTMLNode.classList.remove(settings['selectorAreaClass:pre'])
|
|
41
29
|
this.HTMLNode.classList.add(settings['selectorAreaClass'])
|
|
42
30
|
})
|
|
43
|
-
this.HTMLNode.classList.add(this.
|
|
31
|
+
this.HTMLNode.classList.add(this.Settings.selectorAreaClass)
|
|
44
32
|
|
|
45
|
-
this.
|
|
46
|
-
this.
|
|
47
|
-
this.
|
|
48
|
-
this.
|
|
49
|
-
|
|
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', () => {
|
|
50
40
|
this.updatePos()
|
|
51
41
|
this.stopAutoScroll()
|
|
52
42
|
})
|
|
53
43
|
}
|
|
54
44
|
|
|
55
|
-
|
|
45
|
+
private init = () => {
|
|
56
46
|
this.applyElements('append')
|
|
57
47
|
this.updatePos()
|
|
58
48
|
}
|
|
59
49
|
|
|
60
|
-
/**
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
applyElements = (method = 'append') => {
|
|
50
|
+
/** Adding / Removing elements to document */
|
|
51
|
+
private applyElements = <K extends keyof AppendRemove>(
|
|
52
|
+
method: AppendRemove[K]
|
|
53
|
+
) => {
|
|
65
54
|
const docEl = document.body ? 'body' : 'documentElement'
|
|
66
|
-
const methodName = `${method}Child`
|
|
55
|
+
const methodName = `${method}Child` as `${AppendRemove}Child`
|
|
67
56
|
this.HTMLNode[methodName](this.DS.Selector.HTMLNode)
|
|
68
57
|
document[docEl][methodName](this.HTMLNode)
|
|
69
58
|
}
|
|
70
59
|
|
|
71
60
|
/** Updates the selectorAreas positions to match the areas */
|
|
72
|
-
updatePos = () => {
|
|
73
|
-
this._rect =
|
|
61
|
+
private updatePos = () => {
|
|
62
|
+
this._rect = undefined
|
|
74
63
|
const rect = this.DS.Area.rect
|
|
75
64
|
const border = this.DS.Area.computedBorder
|
|
76
65
|
const { style } = this.HTMLNode
|
|
@@ -84,7 +73,7 @@ export default class SelectorArea {
|
|
|
84
73
|
if (style.height !== height) style.height = height
|
|
85
74
|
}
|
|
86
75
|
|
|
87
|
-
stop = (remove) => {
|
|
76
|
+
public stop = (remove: boolean) => {
|
|
88
77
|
this.stopAutoScroll()
|
|
89
78
|
if (remove) this.applyElements('remove')
|
|
90
79
|
}
|
|
@@ -92,13 +81,18 @@ export default class SelectorArea {
|
|
|
92
81
|
//////////////////////////////////////////////////////////////////////////////////////
|
|
93
82
|
// AutoScroll
|
|
94
83
|
|
|
95
|
-
startAutoScroll = (
|
|
84
|
+
private startAutoScroll = ({
|
|
85
|
+
isDraggingKeyboard,
|
|
86
|
+
}: {
|
|
87
|
+
isDraggingKeyboard?: boolean
|
|
88
|
+
}) => {
|
|
89
|
+
if (isDraggingKeyboard) return
|
|
96
90
|
this.currentEdges = []
|
|
97
91
|
this._scrollInterval = setInterval(() => this.handleAutoScroll(), 16)
|
|
98
92
|
}
|
|
99
93
|
|
|
100
94
|
/** Creates an interval that auto-scrolls while the cursor is near the edge */
|
|
101
|
-
handleAutoScroll = () => {
|
|
95
|
+
private handleAutoScroll = () => {
|
|
102
96
|
if (this.DS.continue) return
|
|
103
97
|
const {
|
|
104
98
|
stores: { PointerStore },
|
|
@@ -106,16 +100,16 @@ export default class SelectorArea {
|
|
|
106
100
|
} = this.DS
|
|
107
101
|
|
|
108
102
|
this.currentEdges = getOverflowEdges({
|
|
109
|
-
elementRect:
|
|
103
|
+
elementRect: vect2rect(PointerStore.currentVal),
|
|
110
104
|
containerRect: this.rect,
|
|
111
|
-
tolerance: this.
|
|
105
|
+
tolerance: this.Settings.overflowTolerance,
|
|
112
106
|
})
|
|
113
107
|
|
|
114
108
|
if (this.currentEdges.length)
|
|
115
|
-
Area.scroll(this.currentEdges, this.
|
|
109
|
+
Area.scroll(this.currentEdges, this.Settings.autoScrollSpeed)
|
|
116
110
|
}
|
|
117
111
|
|
|
118
|
-
stopAutoScroll = () => {
|
|
112
|
+
private stopAutoScroll = () => {
|
|
119
113
|
this.currentEdges = []
|
|
120
114
|
clearInterval(this._scrollInterval)
|
|
121
115
|
}
|
|
@@ -124,13 +118,10 @@ export default class SelectorArea {
|
|
|
124
118
|
// Booleans
|
|
125
119
|
|
|
126
120
|
/**
|
|
127
|
-
* Checks if the element is either inside the Selector Area
|
|
128
|
-
*
|
|
129
|
-
* @param {DSElement} element
|
|
130
|
-
* @param {DSBoundingRect} [elementRect] - slight performance improvements
|
|
131
|
-
* @returns {boolean}
|
|
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
|
|
132
123
|
*/
|
|
133
|
-
isInside = (element, elementRect) => {
|
|
124
|
+
public isInside = (element: E, elementRect?: DSBoundingRect) => {
|
|
134
125
|
if (
|
|
135
126
|
this.DS.Area.HTMLNode.contains(element) &&
|
|
136
127
|
this.DS.stores.ScrollStore.canScroll
|
|
@@ -142,12 +133,8 @@ export default class SelectorArea {
|
|
|
142
133
|
)
|
|
143
134
|
}
|
|
144
135
|
|
|
145
|
-
/**
|
|
146
|
-
|
|
147
|
-
* @param {DSEvent} [event]
|
|
148
|
-
* @returns {boolean}
|
|
149
|
-
*/
|
|
150
|
-
isClicked(event) {
|
|
136
|
+
/** checks if the click was triggered on the area. */
|
|
137
|
+
public isClicked(event?: DSEvent) {
|
|
151
138
|
const {
|
|
152
139
|
stores: { PointerStore },
|
|
153
140
|
} = this.DS
|
|
@@ -167,7 +154,7 @@ export default class SelectorArea {
|
|
|
167
154
|
)
|
|
168
155
|
}
|
|
169
156
|
|
|
170
|
-
get rect() {
|
|
157
|
+
public get rect() {
|
|
171
158
|
if (this._rect) return this._rect
|
|
172
159
|
return (this._rect = this.HTMLNode.getBoundingClientRect())
|
|
173
160
|
}
|
|
@@ -0,0 +1,92 @@
|
|
|
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
|
+
}
|