dragselect 2.7.3 → 3.0.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 +100 -22
- 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 -3877
- package/dist/DragSelect.js +0 -3885
- 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 -40
- package/dist/modules/SelectedSet.d.ts +0 -29
- 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 -144
- package/src/modules/SelectedSet.js +0 -69
- 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,22 +1,13 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
* @param {*} fallback
|
|
5
|
-
* @returns {void}
|
|
6
|
-
*/
|
|
7
|
-
const wrongTypeWarn = (key, type, fallback) =>
|
|
1
|
+
import { DSInputElement, Settings } from "../types"
|
|
2
|
+
|
|
3
|
+
const wrongTypeWarn = (key: string, type: string) =>
|
|
8
4
|
console.warn(
|
|
9
5
|
`[DragSelect] TypeIssue: setting "${key}" is not of type "${type}".`
|
|
10
6
|
)
|
|
11
7
|
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
* @param {boolean} withFallback
|
|
16
|
-
* @param {*} fallback
|
|
17
|
-
* @returns {object}
|
|
18
|
-
*/
|
|
19
|
-
const hydrateHelper = (key, value, withFallback, fallback) => {
|
|
8
|
+
const hydrateHelper = <K extends string, V>(key: K, value: V, withFallback?: boolean, fallback?: V): {
|
|
9
|
+
[key in K]: V;
|
|
10
|
+
} | {} => {
|
|
20
11
|
// no value available
|
|
21
12
|
if (value === undefined) return withFallback ? { [key]: fallback } : {}
|
|
22
13
|
// force unsetting of a value
|
|
@@ -32,7 +23,7 @@ const hydrateHelper = (key, value, withFallback, fallback) => {
|
|
|
32
23
|
isAvailable = typeof value === 'string' || value instanceof String
|
|
33
24
|
if (expectedString && !isAvailable) {
|
|
34
25
|
forceFallback = true
|
|
35
|
-
wrongTypeWarn(key, 'string'
|
|
26
|
+
wrongTypeWarn(key, 'string')
|
|
36
27
|
}
|
|
37
28
|
|
|
38
29
|
// TypeCheck [Number]
|
|
@@ -41,7 +32,7 @@ const hydrateHelper = (key, value, withFallback, fallback) => {
|
|
|
41
32
|
isAvailable = !Number.isNaN(value) && typeof value === 'number'
|
|
42
33
|
if (expectedNumber && !isAvailable) {
|
|
43
34
|
forceFallback = true
|
|
44
|
-
wrongTypeWarn(key, 'number'
|
|
35
|
+
wrongTypeWarn(key, 'number')
|
|
45
36
|
}
|
|
46
37
|
|
|
47
38
|
// TypeCheck [Object]
|
|
@@ -51,7 +42,7 @@ const hydrateHelper = (key, value, withFallback, fallback) => {
|
|
|
51
42
|
isAvailable = Object.prototype.toString.call(value) === '[object Object]'
|
|
52
43
|
if (expectedObject && !isAvailable) {
|
|
53
44
|
forceFallback = true
|
|
54
|
-
wrongTypeWarn(key, 'object'
|
|
45
|
+
wrongTypeWarn(key, 'object')
|
|
55
46
|
}
|
|
56
47
|
|
|
57
48
|
// TypeCheck [Boolean]
|
|
@@ -59,7 +50,7 @@ const hydrateHelper = (key, value, withFallback, fallback) => {
|
|
|
59
50
|
if (expectedBoolean) isAvailable = typeof value === 'boolean'
|
|
60
51
|
if (expectedBoolean && !isAvailable) {
|
|
61
52
|
forceFallback = true
|
|
62
|
-
wrongTypeWarn(key, 'boolean'
|
|
53
|
+
wrongTypeWarn(key, 'boolean')
|
|
63
54
|
}
|
|
64
55
|
|
|
65
56
|
// TypeCheck [Array]
|
|
@@ -67,14 +58,14 @@ const hydrateHelper = (key, value, withFallback, fallback) => {
|
|
|
67
58
|
if (expectedArray) isAvailable = Array.isArray(value)
|
|
68
59
|
if (expectedArray && !isAvailable) {
|
|
69
60
|
forceFallback = true
|
|
70
|
-
wrongTypeWarn(key, 'array'
|
|
61
|
+
wrongTypeWarn(key, 'array')
|
|
71
62
|
}
|
|
72
63
|
|
|
73
64
|
const isFallback = forceFallback || withFallback
|
|
74
65
|
|
|
75
66
|
// Special rule for [dragKeys]
|
|
76
67
|
if (key === 'dragKeys' && isAvailable)
|
|
77
|
-
return { [key]: Object.assign(fallback, value) }
|
|
68
|
+
return { [key]: Object.assign(fallback as {}, value) }
|
|
78
69
|
if (key === 'dragKeys' && !isAvailable)
|
|
79
70
|
return isFallback ? { [key]: fallback } : {}
|
|
80
71
|
|
|
@@ -82,6 +73,7 @@ const hydrateHelper = (key, value, withFallback, fallback) => {
|
|
|
82
73
|
if (
|
|
83
74
|
key === 'dropZones' &&
|
|
84
75
|
isAvailable &&
|
|
76
|
+
Array.isArray(value) &&
|
|
85
77
|
new Set(value.map((v) => v.id)).size !== value.length
|
|
86
78
|
)
|
|
87
79
|
console.warn(
|
|
@@ -100,10 +92,8 @@ const hydrateHelper = (key, value, withFallback, fallback) => {
|
|
|
100
92
|
* - if "withfallback" is true, it will return the object with all settings:
|
|
101
93
|
* - - if not provided from the settings object (or wrong type), the fallback will be used
|
|
102
94
|
* (the fallback value for each setting is the last prop of the hydrateHelper)
|
|
103
|
-
* @param {Settings} settings
|
|
104
|
-
* @param {boolean} withFallback
|
|
105
95
|
*/
|
|
106
|
-
export
|
|
96
|
+
export const hydrateSettings = <E extends DSInputElement>(settings: Settings<E>, withFallback: boolean): Required<Settings<E>> => ({
|
|
107
97
|
...hydrateHelper('area', settings.area, withFallback, document),
|
|
108
98
|
...hydrateHelper('selectables', settings.selectables, withFallback, null),
|
|
109
99
|
...hydrateHelper(
|
|
@@ -252,5 +242,4 @@ export default (settings, withFallback) => ({
|
|
|
252
242
|
withFallback,
|
|
253
243
|
'ds-dropzone-inside'
|
|
254
244
|
),
|
|
255
|
-
|
|
256
|
-
})
|
|
245
|
+
} as Required<Settings<E>>)
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
// @ts-check
|
|
2
|
+
import { type DSBoundingRectBase } from '../types'
|
|
3
|
+
|
|
4
|
+
// [PUBLICLY EXPOSED METHOD]
|
|
5
|
+
|
|
6
|
+
export type IsCollision = {
|
|
7
|
+
/**
|
|
8
|
+
* Axis-Aligned Bounding Box Collision Detection.
|
|
9
|
+
* Imagine following Example:
|
|
10
|
+
*
|
|
11
|
+
*
|
|
12
|
+
* b01
|
|
13
|
+
* a01[1]a02
|
|
14
|
+
* b02 b11
|
|
15
|
+
* a11[2]a12
|
|
16
|
+
* b12
|
|
17
|
+
*
|
|
18
|
+
*
|
|
19
|
+
* to check if those two boxes collide we do this AABB calculation:
|
|
20
|
+
* 1. a01 < a12 (left border pos box1 smaller than right border pos box2)
|
|
21
|
+
* 2. a02 > a11 (right border pos box1 larger than left border pos box2)
|
|
22
|
+
* 3. b01 < b12 (top border pos box1 smaller than bottom border pos box2)
|
|
23
|
+
* 4. b02 > b11 (bottom border pos box1 larger than top border pos box2)
|
|
24
|
+
* {@link https://en.wikipedia.org/wiki/Minimum_bounding_box#Axis-aligned_minimum_bounding_box Wikipedia}
|
|
25
|
+
* {@link https://developer.mozilla.org/en-US/docs/Games/Techniques/2D_collision_detection MDN}
|
|
26
|
+
|
|
27
|
+
* @param percent
|
|
28
|
+
* 1 = the element has to be completely inside the other element
|
|
29
|
+
* 0.8 = the element has to be 80% inside the other element
|
|
30
|
+
* 0.5 = the element has to be 50% inside the other element
|
|
31
|
+
* 0.2 = the element has to be 20% inside the other element
|
|
32
|
+
* 0 = the element only has to touch the other element
|
|
33
|
+
*/
|
|
34
|
+
(
|
|
35
|
+
el1?: DSBoundingRectBase,
|
|
36
|
+
el2?: DSBoundingRectBase,
|
|
37
|
+
percent?: number
|
|
38
|
+
): boolean
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export const isCollision: IsCollision = (el1, el2, percent = 0) => {
|
|
42
|
+
if (!el1 || !el2) return false
|
|
43
|
+
|
|
44
|
+
let element1 = el1
|
|
45
|
+
|
|
46
|
+
if (percent > 0) {
|
|
47
|
+
const widthPoint = (el1.right - el1.left) * percent
|
|
48
|
+
const heightPoint = (el1.bottom - el1.top) * percent
|
|
49
|
+
element1 = {
|
|
50
|
+
left: el1.left + widthPoint,
|
|
51
|
+
right: el1.right - widthPoint,
|
|
52
|
+
top: el1.top + heightPoint,
|
|
53
|
+
bottom: el1.bottom - heightPoint,
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
if (
|
|
58
|
+
element1.left < el2.right && // 1.
|
|
59
|
+
element1.right > el2.left && // 2.
|
|
60
|
+
element1.top < el2.bottom && // 3.
|
|
61
|
+
element1.bottom > el2.top // 4.
|
|
62
|
+
)
|
|
63
|
+
return true
|
|
64
|
+
// collision detected!
|
|
65
|
+
return false
|
|
66
|
+
}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import SelectableSet from '../modules/SelectableSet'
|
|
2
|
+
import SelectedSet from '../modules/SelectedSet'
|
|
3
|
+
import { DSBoundingRect, DSInputElement, Vect2 } from '../types'
|
|
4
|
+
|
|
5
|
+
type Props = {
|
|
6
|
+
direction: Vect2
|
|
7
|
+
containerRect: DSBoundingRect
|
|
8
|
+
selectionRect: DSBoundingRect
|
|
9
|
+
scrollAmount: Vect2
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Modify direction value so that the rect of draggable elements
|
|
14
|
+
* does not exceed the boundaries of container rect
|
|
15
|
+
*/
|
|
16
|
+
export const limitDirection = ({ containerRect, selectionRect, direction, scrollAmount }: Props) => {
|
|
17
|
+
const delta = {
|
|
18
|
+
top: containerRect.top - selectionRect.top + scrollAmount.y,
|
|
19
|
+
left: containerRect.left - selectionRect.left + scrollAmount.x,
|
|
20
|
+
bottom: containerRect.bottom - selectionRect.bottom + scrollAmount.y,
|
|
21
|
+
right: containerRect.right - selectionRect.right + scrollAmount.x,
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
if(direction.x === 0 && direction.y === 0) return direction
|
|
25
|
+
if (direction.y < 0) direction.y = Math.max(direction.y, delta.top)
|
|
26
|
+
if (direction.x < 0) direction.x = Math.max(direction.x, delta.left)
|
|
27
|
+
if (direction.y > 0) direction.y = Math.min(direction.y, delta.bottom)
|
|
28
|
+
if (direction.x > 0) direction.x = Math.min(direction.x, delta.right)
|
|
29
|
+
|
|
30
|
+
selectionRect.top += direction.y;
|
|
31
|
+
selectionRect.bottom += direction.y;
|
|
32
|
+
selectionRect.left += direction.x;
|
|
33
|
+
selectionRect.right += direction.x;
|
|
34
|
+
|
|
35
|
+
return direction
|
|
36
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { DSBoundingRect, DSInputElement, Vect2 } from "../types"
|
|
2
|
+
import { getStylePosition } from "./getStylePosition"
|
|
3
|
+
import { setStylePosition } from "./setStylePosition"
|
|
4
|
+
import { calcVect } from "./vect2"
|
|
5
|
+
|
|
6
|
+
type Props<E extends DSInputElement> = {
|
|
7
|
+
element: E
|
|
8
|
+
posDirection: Vect2
|
|
9
|
+
containerRect: DSBoundingRect
|
|
10
|
+
useTransform: boolean
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
/** Moves the element in a posDirection */
|
|
14
|
+
export const moveElement = <E extends DSInputElement>({ element, posDirection, useTransform }: Props<E>) => {
|
|
15
|
+
const elementPos = getStylePosition(element, useTransform)
|
|
16
|
+
const newPos = calcVect(elementPos, '+', posDirection)
|
|
17
|
+
setStylePosition(element, newPos, useTransform)
|
|
18
|
+
}
|
|
@@ -1,14 +1,8 @@
|
|
|
1
|
-
|
|
2
|
-
import '../types'
|
|
1
|
+
import { DSArea, DSEdges } from "../types"
|
|
3
2
|
|
|
4
|
-
/**
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
* @param {Array.<'top'|'bottom'|'left'|'right'|undefined>} directions
|
|
8
|
-
* @param {number} multiplier
|
|
9
|
-
*/
|
|
10
|
-
export default (element, directions, multiplier) => {
|
|
11
|
-
if (!directions.length) return
|
|
3
|
+
/** Scroll the element in the specified direction */
|
|
4
|
+
export const scrollElement = (element?: DSArea, directions?: DSEdges, multiplier: number = 1) => {
|
|
5
|
+
if (!directions?.length || !element) return
|
|
12
6
|
|
|
13
7
|
const docEl =
|
|
14
8
|
document &&
|
|
@@ -1,15 +1,7 @@
|
|
|
1
|
-
|
|
2
|
-
import '../types'
|
|
1
|
+
import { DSInputElement, Vect2 } from '../types'
|
|
3
2
|
|
|
4
|
-
/**
|
|
5
|
-
|
|
6
|
-
* Can handle translate and top/left
|
|
7
|
-
* @param {DSElement} element
|
|
8
|
-
* @param {Vect2} values
|
|
9
|
-
* @param {boolean} [useTranslate]
|
|
10
|
-
* @return {DSElement}
|
|
11
|
-
*/
|
|
12
|
-
export default (element, values, useTranslate) => {
|
|
3
|
+
/** Sets the style position to the X and Y coordinates. Can handle translate and top/left */
|
|
4
|
+
export const setStylePosition = <E extends DSInputElement>(element: E, values: Vect2, useTranslate: boolean) => {
|
|
13
5
|
if (useTranslate) {
|
|
14
6
|
const prevTransform = element.style.transform
|
|
15
7
|
element.style.transform = `translate3d(${values.x}px,${
|
|
@@ -0,0 +1,240 @@
|
|
|
1
|
+
import DragSelect from '../DragSelect'
|
|
2
|
+
import PubSub, { DSPublishMappings } from '../modules/PubSub'
|
|
3
|
+
import { DSAreaPublishEventData } from '../modules/Area'
|
|
4
|
+
import { DSDropZone } from '../modules/DropZone'
|
|
5
|
+
import { DSInteractionPublishEventData } from '../modules/Interaction'
|
|
6
|
+
import { DSSelectedPublishEventData } from '../modules/SelectedSet'
|
|
7
|
+
import { DSInputElement } from '../types'
|
|
8
|
+
|
|
9
|
+
export type DSPublicPublishEventNames<E extends DSInputElement> =
|
|
10
|
+
keyof DSPublicPublish<E>
|
|
11
|
+
|
|
12
|
+
export type DSPublicPublishAdditionalEventData<E extends DSInputElement> = {
|
|
13
|
+
/** The dropZone element that the element was dropped into (or the mouse is currently hovering over) */
|
|
14
|
+
dropTarget?: DSDropZone<E>
|
|
15
|
+
/** Selected Elements */
|
|
16
|
+
items: E[]
|
|
17
|
+
/** Whether the user is dragging or selecting */
|
|
18
|
+
isDragging: boolean
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
type SelectionCB<E extends DSInputElement> = DSSelectedPublishEventData<E> & {
|
|
22
|
+
/** Whether the user is dragging or selecting */
|
|
23
|
+
isDragging: boolean
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
type ScrollCB<E extends DSInputElement> = Pick<
|
|
27
|
+
DSAreaPublishEventData<E>,
|
|
28
|
+
'scroll_directions' | 'scroll_multiplier'
|
|
29
|
+
> & {
|
|
30
|
+
/** Selected Elements */
|
|
31
|
+
items: E[]
|
|
32
|
+
/** Whether the user is dragging or selecting */
|
|
33
|
+
isDragging: boolean
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
type InteractionCB = DSInteractionPublishEventData
|
|
37
|
+
type InteractionEndCB<E extends DSInputElement> =
|
|
38
|
+
Partial<DSInteractionPublishEventData> & DSPublicPublishAdditionalEventData<E>
|
|
39
|
+
|
|
40
|
+
export const deprecatedNamesMap = {
|
|
41
|
+
elementselect: 'DS:select',
|
|
42
|
+
elementunselect: 'DS:unselect',
|
|
43
|
+
autoscroll: 'DS:scroll',
|
|
44
|
+
dragstart: 'DS:start',
|
|
45
|
+
dragmove: 'DS:update',
|
|
46
|
+
callback: 'DS:end',
|
|
47
|
+
preelementselect: 'DS:select:pre',
|
|
48
|
+
preelementunselect: 'DS:unselect:pre',
|
|
49
|
+
preautoscroll: 'DS:scroll:pre',
|
|
50
|
+
predragstart: 'DS:start:pre',
|
|
51
|
+
predragmove: 'DS:update:pre',
|
|
52
|
+
precallback: 'DS:end:pre',
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
export type DSPublicPublish<E extends DSInputElement> = {
|
|
56
|
+
// Selection Events
|
|
57
|
+
preelementselect: SelectionCB<E>
|
|
58
|
+
elementselect: SelectionCB<E>
|
|
59
|
+
'DS:select:pre': SelectionCB<E>
|
|
60
|
+
'DS:select': SelectionCB<E>
|
|
61
|
+
preelementunselect: SelectionCB<E>
|
|
62
|
+
elementunselect: SelectionCB<E>
|
|
63
|
+
'DS:unselect:pre': SelectionCB<E>
|
|
64
|
+
'DS:unselect': SelectionCB<E>
|
|
65
|
+
// Selectables Events
|
|
66
|
+
'DS:added:pre': SelectionCB<E>
|
|
67
|
+
'DS:added': SelectionCB<E>
|
|
68
|
+
'DS:removed:pre': SelectionCB<E>
|
|
69
|
+
'DS:removed': SelectionCB<E>
|
|
70
|
+
// Scroll Events
|
|
71
|
+
preautoscroll: ScrollCB<E>
|
|
72
|
+
autoscroll: ScrollCB<E>
|
|
73
|
+
'DS:scroll': ScrollCB<E>
|
|
74
|
+
'DS:scroll:pre': ScrollCB<E>
|
|
75
|
+
// Interaction Start Events
|
|
76
|
+
predragstart: InteractionCB
|
|
77
|
+
dragstart: InteractionCB
|
|
78
|
+
'DS:start': InteractionCB
|
|
79
|
+
'DS:start:pre': InteractionCB
|
|
80
|
+
// Interaction Update Events
|
|
81
|
+
predragmove: InteractionCB
|
|
82
|
+
dragmove: InteractionCB
|
|
83
|
+
'DS:update': InteractionCB
|
|
84
|
+
'DS:update:pre': InteractionCB
|
|
85
|
+
// Interaction End Events
|
|
86
|
+
precallback: InteractionEndCB<E>
|
|
87
|
+
callback: InteractionEndCB<E>
|
|
88
|
+
'DS:end': InteractionEndCB<E>
|
|
89
|
+
'DS:end:pre': InteractionEndCB<E>
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
type UsedPublishNames =
|
|
93
|
+
| 'Selected:added'
|
|
94
|
+
| 'Selected:removed'
|
|
95
|
+
| 'Selectable:added'
|
|
96
|
+
| 'Selectable:removed'
|
|
97
|
+
| 'Area:scroll'
|
|
98
|
+
| 'Interaction:start'
|
|
99
|
+
| 'Interaction:update'
|
|
100
|
+
| 'Interaction:end'
|
|
101
|
+
|
|
102
|
+
type UsedPublishMappings<E extends DSInputElement> = Pick<
|
|
103
|
+
DSPublishMappings<E>,
|
|
104
|
+
UsedPublishNames
|
|
105
|
+
>
|
|
106
|
+
|
|
107
|
+
type Condition<T, E extends DSInputElement> = (
|
|
108
|
+
data: T,
|
|
109
|
+
DS: DragSelect<E>
|
|
110
|
+
) => T | null
|
|
111
|
+
|
|
112
|
+
type MappingObj<
|
|
113
|
+
E extends DSInputElement,
|
|
114
|
+
K extends keyof UsedPublishMappings<E>,
|
|
115
|
+
> = {
|
|
116
|
+
name: DSPublicPublishEventNames<E>
|
|
117
|
+
condition?: Condition<UsedPublishMappings<E>[K], E>
|
|
118
|
+
extraData?: Condition<UsedPublishMappings<E>[K], E>
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
type DSMappings<E extends DSInputElement> = {
|
|
122
|
+
[K in keyof UsedPublishMappings<E>]: MappingObj<E, K>[]
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
const endExtraData = <E extends DSInputElement>(
|
|
126
|
+
data: UsedPublishMappings<E>['Interaction:end'],
|
|
127
|
+
DS: DragSelect<E>
|
|
128
|
+
) => {
|
|
129
|
+
const target = DS.DropZones.getTarget(data)
|
|
130
|
+
return {
|
|
131
|
+
...data,
|
|
132
|
+
...(target ? { dropTarget: target.toObject() } : {}),
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
/** Maps internal events to external ones */
|
|
137
|
+
export const subscriberAliases = <E extends DSInputElement>({
|
|
138
|
+
PS,
|
|
139
|
+
DS,
|
|
140
|
+
}: {
|
|
141
|
+
PS: PubSub<E>
|
|
142
|
+
DS: DragSelect<E>
|
|
143
|
+
}) => {
|
|
144
|
+
const mapping: DSMappings<E> = {
|
|
145
|
+
'Selected:added': [
|
|
146
|
+
{ name: 'preelementselect' },
|
|
147
|
+
{ name: 'elementselect' },
|
|
148
|
+
{ name: 'DS:select:pre' },
|
|
149
|
+
{ name: 'DS:select' },
|
|
150
|
+
],
|
|
151
|
+
'Selected:removed': [
|
|
152
|
+
{ name: 'preelementunselect' },
|
|
153
|
+
{ name: 'elementunselect' },
|
|
154
|
+
{ name: 'DS:unselect:pre' },
|
|
155
|
+
{ name: 'DS:unselect' },
|
|
156
|
+
],
|
|
157
|
+
'Selectable:added': [{ name: 'DS:added:pre' }, { name: 'DS:added' }],
|
|
158
|
+
'Selectable:removed': [{ name: 'DS:removed:pre' }, { name: 'DS:removed' }],
|
|
159
|
+
'Area:scroll': [
|
|
160
|
+
{ name: 'preautoscroll' },
|
|
161
|
+
{ name: 'autoscroll' },
|
|
162
|
+
{ name: 'DS:scroll:pre' },
|
|
163
|
+
{ name: 'DS:scroll' },
|
|
164
|
+
],
|
|
165
|
+
'Interaction:start': [
|
|
166
|
+
{ name: 'predragstart' },
|
|
167
|
+
{ name: 'dragstart' },
|
|
168
|
+
{ name: 'DS:start:pre' },
|
|
169
|
+
{ name: 'DS:start' },
|
|
170
|
+
],
|
|
171
|
+
'Interaction:update': [
|
|
172
|
+
{ name: 'predragmove', condition: (data) => (data.event ? data : null) },
|
|
173
|
+
{ name: 'dragmove', condition: (data) => (data.event ? data : null) },
|
|
174
|
+
{
|
|
175
|
+
name: 'DS:update:pre',
|
|
176
|
+
condition: (data) => (data.event ? data : null),
|
|
177
|
+
},
|
|
178
|
+
{ name: 'DS:update', condition: (data) => (data.event ? data : null) },
|
|
179
|
+
],
|
|
180
|
+
'Interaction:end': [
|
|
181
|
+
{ name: 'precallback', extraData: (data, DS) => endExtraData(data, DS) },
|
|
182
|
+
{ name: 'callback', extraData: (data, DS) => endExtraData(data, DS) },
|
|
183
|
+
{ name: 'DS:end:pre', extraData: (data, DS) => endExtraData(data, DS) },
|
|
184
|
+
{ name: 'DS:end', extraData: (data, DS) => endExtraData(data, DS) },
|
|
185
|
+
],
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
for (const [sub_name, sub_pubs] of Object.entries(mapping) as [
|
|
189
|
+
keyof UsedPublishMappings<E>,
|
|
190
|
+
DSMappings<E>[keyof UsedPublishMappings<E>],
|
|
191
|
+
][])
|
|
192
|
+
addSubscribers({ sub_name, sub_pubs, DS, PS })
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
const addSubscribers = <
|
|
196
|
+
E extends DSInputElement,
|
|
197
|
+
K extends keyof UsedPublishMappings<E>,
|
|
198
|
+
>({
|
|
199
|
+
sub_name,
|
|
200
|
+
DS,
|
|
201
|
+
PS,
|
|
202
|
+
sub_pubs,
|
|
203
|
+
}: {
|
|
204
|
+
sub_name: K
|
|
205
|
+
sub_pubs: DSMappings<E>[K]
|
|
206
|
+
DS: DragSelect<E>
|
|
207
|
+
PS: PubSub<E>
|
|
208
|
+
}) => {
|
|
209
|
+
// Subscribe to the internal event
|
|
210
|
+
PS.subscribe(sub_name, (data) =>
|
|
211
|
+
// publish to each of the mapped ones
|
|
212
|
+
sub_pubs.forEach((sub_pub) => publish({ sub_pub, data, DS }))
|
|
213
|
+
)
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
const publish = <
|
|
217
|
+
E extends DSInputElement,
|
|
218
|
+
K extends keyof UsedPublishMappings<E>,
|
|
219
|
+
>({
|
|
220
|
+
sub_pub,
|
|
221
|
+
data,
|
|
222
|
+
DS,
|
|
223
|
+
}: {
|
|
224
|
+
sub_pub: MappingObj<E, K>
|
|
225
|
+
data: DSPublishMappings<E>[K]
|
|
226
|
+
DS: DragSelect<E>
|
|
227
|
+
}) => {
|
|
228
|
+
// If the events condition is met, publish the external event
|
|
229
|
+
const cleanedData = !sub_pub.condition ? data : sub_pub.condition(data, DS)
|
|
230
|
+
if (cleanedData) {
|
|
231
|
+
const extra = (sub_pub.extraData && sub_pub.extraData(data, DS)) || {}
|
|
232
|
+
DS.publish(sub_pub.name, {
|
|
233
|
+
// add extra data as needed
|
|
234
|
+
items: DS.SelectedSet.elements,
|
|
235
|
+
isDragging: DS.Interaction.isDragging,
|
|
236
|
+
...cleanedData,
|
|
237
|
+
...extra,
|
|
238
|
+
})
|
|
239
|
+
}
|
|
240
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
/** Updates element style left, top, width, height values according to pos input object */
|
|
2
|
+
export default (element: HTMLElement, pos: { left?: number; top?:number; width?:number; height?:number }) => {
|
|
3
|
+
if(pos.left) element.style.left = `${pos.left}px`
|
|
4
|
+
if(pos.top) element.style.top = `${pos.top}px`
|
|
5
|
+
if(pos.width) element.style.width = `${pos.width}px`
|
|
6
|
+
if(pos.height) element.style.height = `${pos.height}px`
|
|
7
|
+
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { DSBoundingRect, DSBoundingRectBase, Vect2 } from "../types"
|
|
2
|
+
|
|
3
|
+
type Operator = '+'|'-'|'*'|'/'
|
|
4
|
+
export const calcVect = ({ x: x1, y: y1 }: Vect2, operator: Operator, { x: x2, y: y2 }: Vect2): Vect2 => {
|
|
5
|
+
const calculations = {
|
|
6
|
+
'+': {
|
|
7
|
+
x: x1 + x2,
|
|
8
|
+
y: y1 + y2,
|
|
9
|
+
},
|
|
10
|
+
'-': {
|
|
11
|
+
x: x1 - x2,
|
|
12
|
+
y: y1 - y2,
|
|
13
|
+
},
|
|
14
|
+
'*': {
|
|
15
|
+
x: x1 * x2,
|
|
16
|
+
y: y1 * y2,
|
|
17
|
+
},
|
|
18
|
+
'/': {
|
|
19
|
+
x: x1 / x2,
|
|
20
|
+
y: y1 / y2,
|
|
21
|
+
},
|
|
22
|
+
}
|
|
23
|
+
return calculations[operator]
|
|
24
|
+
}
|
|
25
|
+
export const rect2vect = (rect: DSBoundingRectBase|DSBoundingRect): Vect2 => ({ x: rect.left, y: rect.top })
|
|
26
|
+
export const vect2rect = (vect: Vect2, dimension: number = 0): DSBoundingRect => ({
|
|
27
|
+
left: vect.x,
|
|
28
|
+
top: vect.y,
|
|
29
|
+
right: vect.x,
|
|
30
|
+
bottom: vect.y,
|
|
31
|
+
width: dimension,
|
|
32
|
+
height: dimension,
|
|
33
|
+
})
|
|
34
|
+
export const num2vect = (n: number): Vect2 => ({ x: n, y: n })
|