dragselect 2.7.4 → 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.
Files changed (144) hide show
  1. package/CHANGELOG.md +97 -23
  2. package/LICENSE +690 -0
  3. package/README.md +2 -1
  4. package/jest-puppeteer.config.js +13 -3
  5. package/jest.config.js +5 -0
  6. package/package.json +40 -46
  7. package/rollup.config.mjs +82 -0
  8. package/src/DragSelect.ts +425 -0
  9. package/src/methods/addModificationObservers.ts +51 -0
  10. package/src/methods/canScroll.ts +19 -0
  11. package/src/methods/{createSelectorAreaElement.js → createSelectorAreaElement.ts} +1 -8
  12. package/src/methods/{createSelectorElement.js → createSelectorElement.ts} +1 -9
  13. package/src/methods/{debounce.js → debounce.ts} +5 -11
  14. package/src/methods/ensureArray.ts +13 -0
  15. package/src/methods/getAllParentNodes.ts +15 -0
  16. package/src/methods/{getAreaRect.js → getAreaRect.ts} +2 -6
  17. package/src/methods/getCurrentScroll.ts +14 -0
  18. package/src/methods/{documentScroll.js → getDocumentScroll.ts} +2 -2
  19. package/src/methods/{getOverflowEdges.js → getOverflowEdges.ts} +11 -11
  20. package/src/methods/getPointerPos.ts +9 -0
  21. package/src/methods/getSelectionRect.ts +29 -0
  22. package/src/methods/{getSelectorPosition.js → getSelectorPosition.ts} +11 -9
  23. package/src/methods/{getStylePosition.spec.js → getStylePosition.spec.ts} +9 -9
  24. package/src/methods/{getStylePosition.js → getStylePosition.ts} +5 -21
  25. package/src/methods/handleElementPositionAttribute.ts +15 -0
  26. package/src/methods/handleKeyboardDragPosDifference.ts +35 -0
  27. package/src/methods/handleSelection.ts +26 -0
  28. package/src/methods/{handleUnSelection.js → handleUnSelection.ts} +14 -14
  29. package/src/methods/{hydrateSettings.js → hydrateSettings.ts} +15 -26
  30. package/src/methods/isCollision.ts +66 -0
  31. package/src/methods/limitDirection.ts +36 -0
  32. package/src/methods/moveElement.ts +18 -0
  33. package/src/methods/{scrollElement.js → scrollElement.ts} +4 -10
  34. package/src/methods/{setStylePosition.js → setStylePosition.ts} +3 -11
  35. package/src/methods/subscriberAliases.ts +240 -0
  36. package/src/methods/updateElementStylePos.ts +7 -0
  37. package/src/methods/vect2.ts +34 -0
  38. package/src/modules/Area.ts +169 -0
  39. package/src/modules/Drag.ts +236 -0
  40. package/src/modules/{DropZone.js → DropZone.ts} +80 -119
  41. package/src/modules/DropZones.ts +151 -0
  42. package/src/modules/Interaction.ts +251 -0
  43. package/src/modules/PubSub.ts +111 -0
  44. package/src/modules/SelectableSet.ts +146 -0
  45. package/src/modules/SelectedSet.ts +92 -0
  46. package/src/modules/{Selection.js → Selection.ts} +59 -44
  47. package/src/modules/Selector.ts +81 -0
  48. package/src/modules/{SelectorArea.js → SelectorArea.ts} +58 -71
  49. package/src/stores/KeyStore.ts +92 -0
  50. package/src/stores/PointerStore.ts +194 -0
  51. package/src/stores/ScrollStore.ts +87 -0
  52. package/src/stores/SettingsStore.ts +88 -0
  53. package/src/types.ts +158 -0
  54. package/tsconfig.json +105 -0
  55. package/bower.json +0 -24
  56. package/dist/DragSelect.d.ts +0 -200
  57. package/dist/DragSelect.es6m.js +0 -3886
  58. package/dist/DragSelect.js +0 -3894
  59. package/dist/ds.es6m.min.js +0 -1
  60. package/dist/ds.min.js +0 -1
  61. package/dist/methods/addModificationObservers.d.ts +0 -11
  62. package/dist/methods/canScroll.d.ts +0 -3
  63. package/dist/methods/createSelectorAreaElement.d.ts +0 -3
  64. package/dist/methods/createSelectorElement.d.ts +0 -3
  65. package/dist/methods/debounce.d.ts +0 -3
  66. package/dist/methods/documentScroll.d.ts +0 -6
  67. package/dist/methods/getAllParentNodes.d.ts +0 -3
  68. package/dist/methods/getAreaRect.d.ts +0 -3
  69. package/dist/methods/getBoundingClientRect.d.ts +0 -4
  70. package/dist/methods/getCurrentScroll.d.ts +0 -3
  71. package/dist/methods/getOverflowEdges.d.ts +0 -7
  72. package/dist/methods/getPointerPos.d.ts +0 -5
  73. package/dist/methods/getSelectorPosition.d.ts +0 -12
  74. package/dist/methods/getStylePosition.d.ts +0 -3
  75. package/dist/methods/handleElementOverflow.d.ts +0 -10
  76. package/dist/methods/handleElementPositionAttribute.d.ts +0 -6
  77. package/dist/methods/handleKeyboardDragPosDifference.d.ts +0 -13
  78. package/dist/methods/handleSelection.d.ts +0 -9
  79. package/dist/methods/handleUnSelection.d.ts +0 -9
  80. package/dist/methods/hydrateSettings.d.ts +0 -3
  81. package/dist/methods/index.d.ts +0 -30
  82. package/dist/methods/isCollision.d.ts +0 -13
  83. package/dist/methods/moveElement.d.ts +0 -8
  84. package/dist/methods/removeModificationObservers.d.ts +0 -3
  85. package/dist/methods/scrollElement.d.ts +0 -3
  86. package/dist/methods/setStylePosition.d.ts +0 -3
  87. package/dist/methods/subscriberAliases.d.ts +0 -14
  88. package/dist/methods/toArray.d.ts +0 -3
  89. package/dist/methods/updateElementStylePos.d.ts +0 -8
  90. package/dist/methods/vect2.d.ts +0 -8
  91. package/dist/modules/Area.d.ts +0 -80
  92. package/dist/modules/Drag.d.ts +0 -71
  93. package/dist/modules/DropZone.d.ts +0 -90
  94. package/dist/modules/DropZones.d.ts +0 -73
  95. package/dist/modules/Interaction.d.ts +0 -51
  96. package/dist/modules/PubSub.d.ts +0 -37
  97. package/dist/modules/SelectableSet.d.ts +0 -43
  98. package/dist/modules/SelectedSet.d.ts +0 -32
  99. package/dist/modules/Selection.d.ts +0 -54
  100. package/dist/modules/Selector.d.ts +0 -29
  101. package/dist/modules/SelectorArea.d.ts +0 -58
  102. package/dist/modules/index.d.ts +0 -11
  103. package/dist/stores/KeyStore.d.ts +0 -34
  104. package/dist/stores/PointerStore.d.ts +0 -83
  105. package/dist/stores/ScrollStore.d.ts +0 -38
  106. package/dist/stores/SettingsStore.d.ts +0 -39
  107. package/dist/stores/index.d.ts +0 -5
  108. package/dist/types.d.ts +0 -276
  109. package/jsconfig.json +0 -13
  110. package/rollup.config.js +0 -101
  111. package/src/DragSelect.js +0 -384
  112. package/src/methods/addModificationObservers.js +0 -38
  113. package/src/methods/canScroll.js +0 -20
  114. package/src/methods/getAllParentNodes.js +0 -17
  115. package/src/methods/getBoundingClientRect.js +0 -33
  116. package/src/methods/getCurrentScroll.js +0 -15
  117. package/src/methods/getPointerPos.js +0 -13
  118. package/src/methods/handleElementOverflow.js +0 -65
  119. package/src/methods/handleElementPositionAttribute.js +0 -16
  120. package/src/methods/handleKeyboardDragPosDifference.js +0 -57
  121. package/src/methods/handleSelection.js +0 -26
  122. package/src/methods/index.js +0 -29
  123. package/src/methods/isCollision.js +0 -58
  124. package/src/methods/moveElement.js +0 -39
  125. package/src/methods/removeModificationObservers.js +0 -13
  126. package/src/methods/subscriberAliases.js +0 -72
  127. package/src/methods/toArray.js +0 -18
  128. package/src/methods/updateElementStylePos.js +0 -15
  129. package/src/methods/vect2.js +0 -54
  130. package/src/modules/Area.js +0 -183
  131. package/src/modules/Drag.js +0 -257
  132. package/src/modules/DropZones.js +0 -161
  133. package/src/modules/Interaction.js +0 -208
  134. package/src/modules/PubSub.js +0 -78
  135. package/src/modules/SelectableSet.js +0 -149
  136. package/src/modules/SelectedSet.js +0 -72
  137. package/src/modules/Selector.js +0 -90
  138. package/src/modules/index.js +0 -10
  139. package/src/stores/KeyStore.js +0 -83
  140. package/src/stores/PointerStore.js +0 -218
  141. package/src/stores/ScrollStore.js +0 -92
  142. package/src/stores/SettingsStore.js +0 -66
  143. package/src/stores/index.js +0 -4
  144. package/src/types.js +0 -100
@@ -1,22 +1,13 @@
1
- /**
2
- * @param {string} key
3
- * @param {string} type
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
- * @param {string} key
14
- * @param {*} value
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', fallback)
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', fallback)
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', fallback)
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', fallback)
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', fallback)
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 default (settings, withFallback) => ({
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
- ...hydrateHelper('dragAsBlock', settings.dragAsBlock, withFallback, false),
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
- // @ts-check
2
- import '../types'
1
+ import { DSArea, DSEdges } from "../types"
3
2
 
4
- /**
5
- * Scroll the element in the specified direction
6
- * @param {DSArea} element
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
- // @ts-check
2
- import '../types'
1
+ import { DSInputElement, Vect2 } from '../types'
3
2
 
4
- /**
5
- * Sets the style position to the X and Y coordinates
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 })