-
-
Notifications
You must be signed in to change notification settings - Fork 316
/
Copy pathgeneral.ts
53 lines (43 loc) · 1.67 KB
/
general.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import type { UseDragEvent } from '../composables'
import type { Dimensions, GraphNode, SnapGrid, XYPosition } from '../types'
import { clampPosition } from './graph'
export function isMouseEvent(event: MouseEvent | TouchEvent): event is MouseEvent {
return 'clientX' in event
}
export function isUseDragEvent(event: any): event is UseDragEvent {
return 'sourceEvent' in event
}
export function getEventPosition(event: MouseEvent | TouchEvent, bounds?: DOMRect) {
const isMouse = isMouseEvent(event)
const evtX = isMouse ? event.clientX : event.touches?.[0].clientX
const evtY = isMouse ? event.clientY : event.touches?.[0].clientY
return {
x: evtX - (bounds?.left ?? 0),
y: evtY - (bounds?.top ?? 0),
}
}
export const isMacOs = () => typeof navigator !== 'undefined' && navigator?.userAgent?.indexOf('Mac') >= 0
export function getNodeDimensions(node: GraphNode): { width: number; height: number } {
return {
width: node.dimensions?.width ?? node.width ?? 0,
height: node.dimensions?.height ?? node.height ?? 0,
}
}
export function snapPosition(position: XYPosition, snapGrid: SnapGrid = [1, 1]): XYPosition {
return {
x: snapGrid[0] * Math.round(position.x / snapGrid[0]),
y: snapGrid[1] * Math.round(position.y / snapGrid[1]),
}
}
export function clampPositionToParent(childPosition: XYPosition, childDimensions: Dimensions, parent: GraphNode) {
const { width: parentWidth, height: parentHeight } = getNodeDimensions(parent)
const { x: parentX, y: parentY } = parent.computedPosition
return clampPosition(
childPosition,
[
[parentX, parentY],
[parentX + parentWidth, parentY + parentHeight],
],
childDimensions,
)
}