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
@@ -0,0 +1,51 @@
1
+ export type AddModificationObservers<T> = {
2
+ /** Adds modification listeners to DOMNode(s) */
3
+ (
4
+ nodes: Node[],
5
+ cb: T
6
+ ): {
7
+ observer: MutationObserver
8
+ callback: T
9
+ /** Removes event-listeners from the DOMNode(s) */
10
+ cleanup: () => void
11
+ }
12
+ }
13
+
14
+ export type MutationCallbackEvent =
15
+ | UIEvent
16
+ | Event
17
+ | MutationRecord[]
18
+ | ResizeObserverEntry[]
19
+ type ModificationListenerCallback = (event: MutationCallbackEvent) => any
20
+
21
+ export const addModificationObservers: AddModificationObservers<
22
+ ModificationListenerCallback
23
+ > = (nodes, cb) => {
24
+ const callback = cb
25
+
26
+ window.addEventListener('resize', callback)
27
+ window.addEventListener('scroll', callback)
28
+
29
+ const observer = new MutationObserver(callback)
30
+ const resizeObserver = new ResizeObserver(callback)
31
+
32
+ nodes.forEach((el, i) => {
33
+ observer.observe(el, {
34
+ childList: i !== 0,
35
+ attributes: true,
36
+ })
37
+ if (el instanceof Element) resizeObserver.observe(el)
38
+ })
39
+
40
+ /**
41
+ * Removes all observers
42
+ */
43
+ const cleanup = () => {
44
+ window.removeEventListener('resize', callback)
45
+ window.removeEventListener('scroll', callback)
46
+ observer.disconnect()
47
+ resizeObserver.disconnect()
48
+ }
49
+
50
+ return { observer, resizeObserver, callback, cleanup }
51
+ }
@@ -0,0 +1,19 @@
1
+ import { DSArea } from '../types'
2
+ import { getCurrentScroll } from './getCurrentScroll'
3
+
4
+ /** Checks whether the area can scroll or not */
5
+ export type CanScroll = {
6
+ (area: DSArea): boolean
7
+ }
8
+
9
+ export const canScroll: CanScroll = area => {
10
+ const scroll = getCurrentScroll(area)
11
+ if (scroll.x || scroll.y) return true
12
+
13
+ if (area instanceof Document) {
14
+ if (area.body) return Boolean(area.body.scrollTop = 1)
15
+ return Boolean(area.documentElement.scrollTop = 1)
16
+ }
17
+
18
+ return Boolean(area.scrollTop = 1)
19
+ }
@@ -1,11 +1,4 @@
1
- // @ts-check
2
- import '../types'
3
-
4
- /**
5
- * Creates the SelectorArea
6
- * @return {HTMLDivElement}
7
- */
8
- export default () => {
1
+ export const createSelectorAreaElement = () => {
9
2
  const node = document.createElement('div')
10
3
  node.style.position = 'fixed'
11
4
  node.style.overflow = 'hidden'
@@ -1,12 +1,4 @@
1
- // @ts-check
2
- import '../types'
3
-
4
- /**
5
- * Create the selector node
6
- * @param {boolean} customStyles
7
- * @return {HTMLElement}
8
- */
9
- export default (customStyles) => {
1
+ export const createSelectorElement = (customStyles: boolean) => {
10
2
  const selector = document.createElement('div')
11
3
 
12
4
  selector.style.position = 'absolute'
@@ -1,25 +1,19 @@
1
- // @ts-check
2
- import '../types'
3
-
4
1
  /**
5
2
  * Returns a function, that, as long as it continues to be invoked, will not
6
3
  * be triggered. The function will be called after it stops being called for
7
4
  * `wait` milliseconds. All credits to [Trey Huffine]{@link https://levelup.gitconnected.com/debounce-in-javascript-improve-your-applications-performance-5b01855e086}
8
- * @param {*} func
9
- * @param {number} wait
10
- * @returns {DSModificationCallback}
11
5
  */
12
- export default (func, wait) => {
13
- let timeout
6
+ export const debounce = (func: any, wait: number) => {
7
+ let timeout: number | undefined
14
8
 
15
9
  // This is the function that is returned and will be executed many times
16
10
  // We spread (...args) to capture any number of parameters we want to pass
17
- return (...args) => {
11
+ return (...args: any[]) => {
18
12
  // The callback function to be executed after
19
13
  // the debounce time has elapsed
20
14
  const later = () => {
21
15
  // null timeout to indicate the debounce ended
22
- timeout = null
16
+ timeout = undefined
23
17
 
24
18
  // Execute the callback
25
19
  func(...args)
@@ -32,6 +26,6 @@ export default (func, wait) => {
32
26
 
33
27
  // Restart the debounce waiting period.
34
28
  // setTimeout returns a truthy value (it differs in web vs Node)
35
- timeout = setTimeout(later, wait)
29
+ timeout = setTimeout(later, wait) as any as number
36
30
  }
37
31
  }
@@ -0,0 +1,13 @@
1
+ type EnsureArray = {
2
+ /**
3
+ * Transforms any list or single dom node to an array so user doesn’t have to care. Also removes duplicates.
4
+ * @param items a single item, a Node-list or any element group
5
+ */
6
+ <T>(items?: T | T[] | [T]): T[]
7
+ }
8
+
9
+ export const ensureArray: EnsureArray = input => {
10
+ if (!input) return []
11
+ if (!Array.isArray(input) && typeof (input as any)[Symbol.iterator] !== 'function') return [input]
12
+ return [...new Set([...(input as any)])]
13
+ }
@@ -0,0 +1,15 @@
1
+ import { DSArea, DSInputElement } from "../types"
2
+
3
+ export const getAllParentNodes = <E extends DSInputElement>(node: E|DSArea) => {
4
+ const traverse = (toWatch: ParentNode[], index = 0): ParentNode[] => {
5
+ const parent = toWatch[index]?.parentNode
6
+ if (parent) {
7
+ toWatch.push(parent)
8
+ index++
9
+ return traverse(toWatch, index)
10
+ }
11
+ return toWatch
12
+ }
13
+
14
+ return traverse([node])
15
+ }
@@ -1,14 +1,10 @@
1
- // @ts-check
2
- import '../types'
1
+ import { DSArea, DSBoundingRect } from '../types'
3
2
  /**
4
3
  * Returns the top/left/bottom/right/width/height
5
4
  * values of an area. If area is document then everything
6
5
  * except the sizes will be nulled.
7
- * @param {DSArea} area
8
- * @param {number} zoom
9
- * @returns {DSBoundingRect}
10
6
  */
11
- export default (area, zoom) => {
7
+ export const getAreaRect = (area: DSArea, zoom: number): DSBoundingRect => {
12
8
  if (area instanceof Document)
13
9
  return {
14
10
  top: 0,
@@ -0,0 +1,14 @@
1
+ import { DSArea, Vect2 } from '../types'
2
+ import { getDocumentScroll } from './getDocumentScroll'
3
+
4
+ export type GetCurrentScroll = {
5
+ (area: DSArea): Vect2
6
+ }
7
+
8
+ export const getCurrentScroll: GetCurrentScroll = area => {
9
+ if (!area || area instanceof Document) return getDocumentScroll()
10
+ return {
11
+ x: area.scrollLeft >= 0 ? area.scrollLeft : getDocumentScroll().x,
12
+ y: area.scrollTop >= 0 ? area.scrollTop : getDocumentScroll().y,
13
+ }
14
+ }
@@ -1,5 +1,5 @@
1
- // @ts-check
2
- export default () => ({
1
+
2
+ export const getDocumentScroll = () => ({
3
3
  y: document.body?.scrollTop || document.documentElement?.scrollTop || 0,
4
4
  x: document.body?.scrollLeft || document.documentElement?.scrollLeft || 0,
5
5
  })
@@ -1,27 +1,27 @@
1
- // @ts-check
2
- import '../types'
1
+ import { DSBoundingRect, DSEdges, Vect2 } from '../types'
2
+
3
+ type Props = {
4
+ elementRect: DSBoundingRect
5
+ containerRect: DSBoundingRect
6
+ tolerance?: Vect2
7
+ }
3
8
 
4
9
  /**
5
10
  * Returns the edges that an element is overflowing
6
- * @param {Object} p
7
- * @param {DSBoundingRect} p.elementRect
8
- * @param {DSBoundingRect} p.containerRect
9
- * @param {Vect2} [p.tolerance]
10
- * @returns {DSEdges}
11
11
  */
12
- export default ({
12
+ export const getOverflowEdges = ({
13
13
  elementRect,
14
14
  containerRect,
15
15
  tolerance = {
16
16
  x: 0,
17
17
  y: 0,
18
18
  },
19
- }) => {
20
- const edges = []
19
+ }: Props): DSEdges => {
20
+ const edges: DSEdges = []
21
21
  if (elementRect.top - tolerance.y < containerRect.top) edges.push('top')
22
22
  if (elementRect.left - tolerance.x < containerRect.left) edges.push('left')
23
23
  if (elementRect.bottom + tolerance.y > containerRect.bottom)
24
24
  edges.push('bottom')
25
25
  if (elementRect.right + tolerance.y > containerRect.right) edges.push('right')
26
- return /** @type {DSEdges} */ (edges)
26
+ return edges
27
27
  }
@@ -0,0 +1,9 @@
1
+ import { Vect2 } from "../types";
2
+
3
+ /**
4
+ * Returns cursor x, y position based on event object
5
+ */
6
+ export const getPointerPos = ({ event }: { event: { clientX: number, clientY: number }}): Vect2 => ({
7
+ x: event.clientX,
8
+ y: event.clientY,
9
+ })
@@ -0,0 +1,29 @@
1
+ import SelectableSet from '../modules/SelectableSet'
2
+ import SelectedSet from '../modules/SelectedSet'
3
+ import { DSBoundingRect, DSInputElement } from '../types'
4
+
5
+ // @TODO: calculate the difference in all directions based on the mouse position! (since the selection square ratio won’t change we don’t have to re-calculate and re-fetch the position of every element in the square during drag)
6
+
7
+ /** Returns the compound bounding rect of multiple elements */
8
+ export const getSelectionRect = <E extends DSInputElement>(SelectedSet: SelectedSet<E>): DSBoundingRect => {
9
+ const rect = {
10
+ top: Number.POSITIVE_INFINITY,
11
+ left: Number.POSITIVE_INFINITY,
12
+ bottom: Number.NEGATIVE_INFINITY,
13
+ right: Number.NEGATIVE_INFINITY,
14
+ width: Number.NEGATIVE_INFINITY,
15
+ height: Number.NEGATIVE_INFINITY,
16
+ }
17
+
18
+ SelectedSet.rects.forEach(elementRect => {
19
+ rect.top = Math.min(rect.top, elementRect.top || rect.top)
20
+ rect.left = Math.min(rect.left, elementRect.left || rect.left)
21
+ rect.bottom = Math.max(rect.bottom, elementRect.bottom || rect.bottom)
22
+ rect.right = Math.max(rect.right, elementRect.right || rect.right)
23
+ })
24
+
25
+ rect.height = rect.bottom - rect.top
26
+ rect.width = rect.right - rect.left
27
+
28
+ return rect
29
+ }
@@ -1,11 +1,13 @@
1
- // @ts-check
2
- import '../types'
3
- /**
4
- * Reliably returns the exact x,y,w,h positions of the selector element
5
- * @param {{ scrollAmount:Vect2, initialPointerPos:Vect2, pointerPos:Vect2 }} p
6
- * @returns {{left:number,top:number,width:number,height:number}}
7
- */
8
- export default ({ scrollAmount, initialPointerPos, pointerPos }) => {
1
+ import { DSBoundingRect, Vect2 } from "../types"
2
+
3
+ type Params = {
4
+ scrollAmount: Vect2
5
+ initialPointerPos: Vect2
6
+ pointerPos: Vect2
7
+ }
8
+
9
+ /** Reliably returns the exact x,y,w,h positions of the selector element */
10
+ export const getSelectorPosition = ({ scrollAmount, initialPointerPos, pointerPos }: Params) => {
9
11
  /** check for direction
10
12
  *
11
13
  * This is quite complicated, so also quite complicated to explain. Lemme’ try:
@@ -44,7 +46,7 @@ export default ({ scrollAmount, initialPointerPos, pointerPos }) => {
44
46
  *
45
47
  * I hope that makes sense. Try stuff out and play around with variables to get a hang of it.
46
48
  */
47
- const selectorPos = {}
49
+ const selectorPos: Partial<DSBoundingRect> = {}
48
50
 
49
51
  // right
50
52
  if (pointerPos.x > initialPointerPos.x - scrollAmount.x) {
@@ -2,7 +2,7 @@
2
2
  * @jest-environment jsdom
3
3
  */
4
4
 
5
- import getStylePosition from './getStylePosition'
5
+ import { getStylePosition } from './getStylePosition'
6
6
 
7
7
  describe('getStylePosition', () => {
8
8
  describe('top/left', () => {
@@ -10,7 +10,7 @@ describe('getStylePosition', () => {
10
10
  document.body.innerHTML = /*html*/ `
11
11
  <div id="test" style="top:1px; left:2px"></div>
12
12
  `
13
- const element = document.getElementById('test')
13
+ const element = document.getElementById('test') as HTMLElement
14
14
  const position = getStylePosition(element)
15
15
 
16
16
  expect(position).toMatchObject({
@@ -24,7 +24,7 @@ describe('getStylePosition', () => {
24
24
  <style>#test { top: 3px; left: 4px }</style>
25
25
  <div id="test"></div>
26
26
  `
27
- const element = document.getElementById('test')
27
+ const element = document.getElementById('test') as HTMLElement
28
28
  const position = getStylePosition(element)
29
29
 
30
30
  expect(position).toMatchObject({
@@ -35,7 +35,7 @@ describe('getStylePosition', () => {
35
35
 
36
36
  it('falls back to computed styles when not set', async () => {
37
37
  document.body.innerHTML = /*html*/ `<div id="test"></div>`
38
- const element = document.getElementById('test')
38
+ const element = document.getElementById('test') as HTMLElement
39
39
  const position = getStylePosition(element)
40
40
  expect(position).toMatchObject({
41
41
  x: 0,
@@ -49,7 +49,7 @@ describe('getStylePosition', () => {
49
49
  document.body.innerHTML = /*html*/ `
50
50
  <div id="test" style="transform: translate(5px, 6px) rotate(45deg)"></div>
51
51
  `
52
- const element = document.getElementById('test')
52
+ const element = document.getElementById('test') as HTMLElement
53
53
  const position = getStylePosition(element, true)
54
54
 
55
55
  expect(position).toMatchObject({
@@ -62,7 +62,7 @@ describe('getStylePosition', () => {
62
62
  document.body.innerHTML = /*html*/ `
63
63
  <div id="test" style="transform: translate3d(7px, 8px, 0) scale(1.5)"></div>
64
64
  `
65
- const element = document.getElementById('test')
65
+ const element = document.getElementById('test') as HTMLElement
66
66
  const position = getStylePosition(element, true)
67
67
 
68
68
  expect(position).toMatchObject({
@@ -77,7 +77,7 @@ describe('getStylePosition', () => {
77
77
  <style>#test { transform: matrix(1, 0.267949, 0.267949, 1, 9, 10) }</style>
78
78
  <div id="test"></div>
79
79
  `
80
- const element = document.getElementById('test')
80
+ const element = document.getElementById('test') as HTMLElement
81
81
  const position = getStylePosition(element, true)
82
82
 
83
83
  expect(position).toMatchObject({
@@ -92,7 +92,7 @@ describe('getStylePosition', () => {
92
92
  <style>#test { transform: matrix3d(1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 11, 12, 1, 1) }</style>
93
93
  <div id="test"></div>
94
94
  `
95
- const element = document.getElementById('test')
95
+ const element = document.getElementById('test') as HTMLElement
96
96
  const position = getStylePosition(element, true)
97
97
 
98
98
  expect(position).toMatchObject({
@@ -103,7 +103,7 @@ describe('getStylePosition', () => {
103
103
 
104
104
  it('falls back to computed styles when not set', async () => {
105
105
  document.body.innerHTML = /*html*/ `<div id="test"></div>`
106
- const element = document.getElementById('test')
106
+ const element = document.getElementById('test') as HTMLElement
107
107
  const position = getStylePosition(element, true)
108
108
  expect(position).toMatchObject({
109
109
  x: 0,
@@ -1,11 +1,6 @@
1
- // @ts-check
2
- import '../types'
1
+ import { DSInputElement, Vect2 } from "../types"
3
2
 
4
- /**
5
- * @param {DSElement} element
6
- * @return {Vect2}
7
- */
8
- const getComputedTranslatePositions = (element) => {
3
+ const getComputedTranslatePositions = <E extends DSInputElement>(element: E) => {
9
4
  const position = {
10
5
  x: 0,
11
6
  y: 0,
@@ -32,11 +27,7 @@ const getComputedTranslatePositions = (element) => {
32
27
  return position
33
28
  }
34
29
 
35
- /**
36
- * @param {DSElement} element
37
- * @return {Vect2}
38
- */
39
- const getTranslatedPositions = (element) => {
30
+ const getTranslatedPositions = <E extends DSInputElement>(element: E) => {
40
31
  const { transform } = element.style
41
32
 
42
33
  if (!transform || transform.indexOf('translate') < 0)
@@ -62,11 +53,7 @@ const getTranslatedPositions = (element) => {
62
53
  return position
63
54
  }
64
55
 
65
- /**
66
- * @param {DSElement} element
67
- * @return {Vect2}
68
- */
69
- const getTopLeftPosition = (element) => {
56
+ const getTopLeftPosition = <E extends DSInputElement>(element: E) => {
70
57
  const { style } = element
71
58
 
72
59
  const position = {
@@ -89,11 +76,8 @@ const getTopLeftPosition = (element) => {
89
76
  /**
90
77
  * Returns the X and Y coordinates based on styles
91
78
  * Can handle translate and top/left
92
- * @param {DSElement} element
93
- * @param {boolean} [useTranslate]
94
- * @return {Vect2}
95
79
  */
96
- export default (element, useTranslate) => {
80
+ export const getStylePosition = <E extends DSInputElement>(element: E, useTranslate?: boolean): Vect2 => {
97
81
  if (useTranslate) return getTranslatedPositions(element)
98
82
  return getTopLeftPosition(element)
99
83
  }
@@ -0,0 +1,15 @@
1
+ import { DSArea } from "../types"
2
+
3
+ type Props = {
4
+ computedStyle: CSSStyleDeclaration
5
+ node: DSArea
6
+ }
7
+
8
+ /** Fix: some elements have to have a special position attribute for calculations */
9
+ export const handleElementPositionAttribute = ({ computedStyle, node }: Props) => {
10
+ const { position } = computedStyle
11
+ const isPositioned =
12
+ position === 'absolute' || position === 'relative' || position === 'fixed'
13
+ if (!(node instanceof Document) && !isPositioned)
14
+ node.style.position = 'relative'
15
+ }
@@ -0,0 +1,35 @@
1
+ import { DSDragKeys, DSEdges, Vect2 } from "../types"
2
+
3
+ type Props = {
4
+ /** The keyboard key that was pressed */
5
+ key: string
6
+ shiftKey: boolean
7
+ keyboardDragSpeed: number
8
+ zoom: number
9
+ scrollDiff: Vect2
10
+ dragKeys?: DSDragKeys
11
+ }
12
+ export const handleKeyboardDragPosDifference = ({
13
+ shiftKey,
14
+ keyboardDragSpeed,
15
+ zoom,
16
+ key,
17
+ dragKeys,
18
+ scrollDiff,
19
+ }: Props): Vect2 => {
20
+ const posDirection = { x: 0, y: 0 }
21
+ const increase = shiftKey
22
+ ? keyboardDragSpeed * 4 * zoom
23
+ : keyboardDragSpeed * zoom
24
+
25
+ if (dragKeys?.left.includes(key))
26
+ posDirection.x = scrollDiff.x || -increase
27
+ if (dragKeys?.right.includes(key))
28
+ posDirection.x = scrollDiff.x || increase
29
+ if (dragKeys?.up.includes(key))
30
+ posDirection.y = scrollDiff.y || -increase
31
+ if (dragKeys?.down.includes(key))
32
+ posDirection.y = scrollDiff.y || increase
33
+
34
+ return posDirection
35
+ }
@@ -0,0 +1,26 @@
1
+ import SelectedSet from "../modules/SelectedSet"
2
+ import { DSInputElement } from "../types"
3
+
4
+ type Props<E extends DSInputElement> = {
5
+ element: E
6
+ force?: boolean
7
+ multiSelectionToggle: boolean
8
+ SelectedSet: SelectedSet<E>
9
+ hoverClassName: string
10
+ }
11
+
12
+ /** Logic when an element is selected */
13
+ export const handleSelection = <E extends DSInputElement>({
14
+ element,
15
+ force,
16
+ multiSelectionToggle,
17
+ SelectedSet,
18
+ hoverClassName,
19
+ }: Props<E>) => {
20
+ if (element.classList.contains(hoverClassName) && !force) return
21
+
22
+ if (!SelectedSet.has(element)) SelectedSet.add(element)
23
+ else if (multiSelectionToggle) SelectedSet.delete(element)
24
+
25
+ element.classList.add(hoverClassName)
26
+ }
@@ -1,23 +1,23 @@
1
- // @ts-check
2
- import '../types'
1
+ import SelectedSet from "../modules/SelectedSet"
2
+ import { DSInputElement } from "../types"
3
3
 
4
- /**
5
- * Logic when an element is de-selected
6
- * @param {Object} p
7
- * @param {DSElement} p.element
8
- * @param {boolean} p.force
9
- * @param {Set} p.SelectedSet
10
- * @param {Set} p.PrevSelectedSet
11
- * @param {string} p.hoverClassName
12
- */
13
- export default ({
4
+ type Props<E extends DSInputElement> = {
5
+ element: E
6
+ force?: boolean
7
+ SelectedSet: SelectedSet<E>
8
+ PrevSelectedSet: Set<E>
9
+ hoverClassName: string
10
+ }
11
+
12
+ /** Logic when an element is de-selected */
13
+ export const handleUnSelection = <E extends DSInputElement>({
14
14
  element,
15
15
  force,
16
16
  SelectedSet,
17
17
  PrevSelectedSet,
18
18
  hoverClassName,
19
- }) => {
20
- if (!element.classList.contains(hoverClassName) && !force) return false
19
+ }: Props<E>) => {
20
+ if (!element.classList.contains(hoverClassName) && !force) return
21
21
 
22
22
  const inSelection = SelectedSet.has(element)
23
23
  const inPrevSelection = PrevSelectedSet.has(element)