-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathInteraction.tsx
175 lines (167 loc) · 5.76 KB
/
Interaction.tsx
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
import React from 'react'
import { ScaleLinear } from 'd3-scale'
import { Domain } from '../../model'
import { MouseAwareSvg, SvgCoordinates } from './MouseAwareSvg'
import { MouseCursor } from './MouseCursor'
import { useZoom } from '../../hooks/useZoom'
import { ZoomLevels, getDomainSpan } from '../../shared/ZoomScale'
import { InteractionHandling } from './InteractionHandling'
import { useTrimming } from './trimmer/useTrimming'
import { TrimRange } from './trimmer/TrimRange'
import { Trimmer } from './trimmer/Trimmer'
import { InteractionModeType, UserInteraction } from './model'
export interface InteractionProps {
width: number
height: number
domain: Domain
maxDomain: Domain
maxDomainStart: number
maxDomainEnd: number
isDomainChangePossible: boolean
timeScale: ScaleLinear<number, number>
zoomLevels: ReadonlyArray<ZoomLevels>
isTrimming: boolean
trimRange?: Domain
isAnimationInProgress: boolean
isNoEventSelected: boolean
enabledInteractions?: ReadonlyArray<UserInteraction>
onDomainChange: (domain: Domain, animated: boolean) => void
dateFormat: (ms: number) => string
onCursorMove?: (millisAtCursor?: number, startMillis?: number, endMillis?: number) => void
onTrimRangeChange?: (startMillis: number, endMillis: number) => void
onInteractionEnd?: () => void
}
export const Interaction = ({
width,
height,
domain,
maxDomain,
maxDomainStart,
maxDomainEnd,
isDomainChangePossible,
timeScale,
zoomLevels,
isTrimming,
trimRange,
isAnimationInProgress,
isNoEventSelected,
enabledInteractions,
onDomainChange,
dateFormat,
onCursorMove,
onTrimRangeChange,
onInteractionEnd,
}: InteractionProps) => {
const {
zoomWidth,
nextSmallerZoomScale,
isZoomInPossible,
isZoomOutPossible,
onZoomIn,
onZoomOut,
onZoomReset,
onZoomInCustom,
onZoomInCustomInProgress,
} = useZoom({
domain,
maxDomainStart,
maxDomainEnd,
zoomLevels,
isDomainChangePossible,
timeScale,
onDomainChange,
onCursorMove,
})
const [onTrimStart, onTrimEnd] = useTrimming(maxDomain, timeScale, onTrimRangeChange, trimRange)
return (
<MouseAwareSvg width={width} height={height}>
{(mousePosition: SvgCoordinates) => {
const timeAtCursor = timeScale.invert(mousePosition.x)
const onScrub = () => {
if (onCursorMove) {
onCursorMove(timeAtCursor, ...getDomainSpan(maxDomainStart, maxDomainEnd, timeAtCursor, zoomWidth))
}
}
const onPan = (pixelDelta: number) => {
if (isDomainChangePossible) {
const [domainMin, domainMax] = domain
const [rangeMin, rangeMax] = timeScale.range()
const domainDelta = (pixelDelta / (rangeMax - rangeMin)) * (domainMax - domainMin)
const [newDomainMin, newDomainMax] = [domainMin + domainDelta, domainMax + domainDelta]
if (newDomainMin > maxDomainStart && newDomainMax < maxDomainEnd) {
onDomainChange([newDomainMin, newDomainMax], false)
}
}
}
return (
<InteractionHandling
width={width}
height={height}
mousePosition={mousePosition}
enabledInteractions={enabledInteractions}
isAnimationInProgress={isAnimationInProgress}
isZoomInPossible={isZoomInPossible}
isZoomOutPossible={isZoomOutPossible}
isTrimming={isTrimming}
onHover={onScrub}
onZoomIn={() => {
onZoomIn(timeAtCursor)
}}
onZoomOut={() => {
onZoomOut(timeAtCursor)
}}
onZoomInCustom={onZoomInCustom}
onZoomInCustomInProgress={onZoomInCustomInProgress}
onZoomReset={onZoomReset}
onPan={onPan}
onTrimStart={onTrimStart}
onTrimEnd={onTrimEnd}
onInteractionEnd={onInteractionEnd}
>
{(cursor, interactionMode, activeInteractions, setTrimHoverMode) => {
return (
<g>
{isNoEventSelected && interactionMode.type !== InteractionModeType.Trim ? (
<MouseCursor
mousePosition={mousePosition.x}
cursorLabel={dateFormat(timeAtCursor)}
cursor={cursor}
interactionMode={interactionMode}
zoomRangeStart={timeScale(timeAtCursor - zoomWidth / 2)!}
zoomRangeEnd={timeScale(timeAtCursor + zoomWidth / 2)!}
zoomScale={nextSmallerZoomScale}
isZoomInPossible={isZoomInPossible}
enabledInteractions={activeInteractions}
/>
) : (
<g />
)}
{trimRange && (
<TrimRange
startX={timeScale(trimRange[0])!}
endX={timeScale(trimRange[1])!}
height={height}
width={width}
/>
)}
{interactionMode.type === InteractionModeType.Trim && timeScale && (
<Trimmer
startX={trimRange ? trimRange[0] : maxDomain[0]}
endX={trimRange ? trimRange[1] : maxDomain[1]}
height={height}
width={width}
timeScale={timeScale}
setTrimMode={setTrimHoverMode}
dateFormat={dateFormat}
highlightActiveArea={interactionMode.variant === 'trim pan end'}
/>
)}
</g>
)
}}
</InteractionHandling>
)
}}
</MouseAwareSvg>
)
}