-
Notifications
You must be signed in to change notification settings - Fork 64
/
Copy pathSvgFeatureRendering.tsx
359 lines (331 loc) · 8.83 KB
/
SvgFeatureRendering.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
import React, { useEffect, useRef, useState, useCallback } from 'react'
import { observer } from 'mobx-react'
import {
AnyConfigurationModel,
readConfObject,
} from '@jbrowse/core/configuration'
import { bpToPx, measureText, Region, Feature } from '@jbrowse/core/util'
import { BaseLayout, SceneGraph } from '@jbrowse/core/util/layouts'
// locals
import FeatureGlyph from './FeatureGlyph'
import SvgOverlay from './SvgOverlay'
import {
chooseGlyphComponent,
layOut,
ExtraGlyphValidator,
DisplayModel,
} from './util'
// used to make features have a little padding for their labels
const xPadding = 3
const yPadding = 5
// used so that user can click-away-from-feature below the laid out features
// (issue #1248)
const svgHeightPadding = 100
function RenderedFeatureGlyph(props: {
feature: Feature
bpPerPx: number
region: Region
config: AnyConfigurationModel
layout: BaseLayout<unknown>
extraGlyphs?: ExtraGlyphValidator[]
displayMode: string
exportSVG?: unknown
displayModel?: DisplayModel
detectRerender?: () => void
viewParams: {
start: number
end: number
offsetPx: number
offsetPx1: number
}
[key: string]: unknown
}) {
const {
feature,
detectRerender,
bpPerPx,
region,
config,
displayMode,
layout,
extraGlyphs,
} = props
// used for unit testing, difficult to mock out so it is in actual source code
detectRerender?.()
const { reversed } = region
const start = feature.get(reversed ? 'end' : 'start')
const startPx = bpToPx(start, region, bpPerPx)
const labelAllowed = displayMode !== 'collapsed'
const rootLayout = new SceneGraph('root', 0, 0, 0, 0)
const GlyphComponent = chooseGlyphComponent(feature, extraGlyphs)
const featureLayout = (GlyphComponent.layOut || layOut)({
layout: rootLayout,
feature,
bpPerPx,
reversed,
config,
extraGlyphs,
})
let shouldShowName = false
let shouldShowDescription = false
let name = ''
let description = ''
let fontHeight = 0
let expansion = 0
if (labelAllowed) {
const showLabels = readConfObject(config, 'showLabels')
const showDescriptions = readConfObject(config, 'showDescriptions')
fontHeight = readConfObject(config, ['labels', 'fontSize'], { feature })
expansion = readConfObject(config, 'maxFeatureGlyphExpansion') || 0
name = String(readConfObject(config, ['labels', 'name'], { feature }) || '')
shouldShowName = /\S/.test(name) && showLabels
const getWidth = (text: string) => {
const glyphWidth = rootLayout.width + expansion
const textWidth = measureText(text, fontHeight)
return Math.round(Math.min(textWidth, glyphWidth))
}
description = String(
readConfObject(config, ['labels', 'description'], { feature }) || '',
)
shouldShowDescription = /\S/.test(description) && showDescriptions
if (shouldShowName) {
rootLayout.addChild(
'nameLabel',
0,
featureLayout.bottom,
getWidth(name),
fontHeight,
)
}
if (shouldShowDescription) {
const aboveLayout = shouldShowName
? rootLayout.getSubRecord('nameLabel')
: featureLayout
rootLayout.addChild(
'descriptionLabel',
0,
aboveLayout.bottom,
getWidth(description),
fontHeight,
)
}
}
const topPx = layout.addRect(
feature.id(),
feature.get('start'),
feature.get('start') + rootLayout.width * bpPerPx + xPadding * bpPerPx,
rootLayout.height + yPadding,
)
if (topPx === null) {
return null
}
rootLayout.move(startPx, topPx)
return (
<FeatureGlyph
rootLayout={rootLayout}
name={name}
shouldShowName={shouldShowName}
description={description}
shouldShowDescription={shouldShowDescription}
fontHeight={fontHeight}
allowedWidthExpansion={expansion}
reversed={region.reversed}
topLevel={true}
{...props}
/>
)
}
const RenderedFeatures = observer(
(props: {
features?: Map<string, Feature>
isFeatureDisplayed?: (f: Feature) => boolean
bpPerPx: number
config: AnyConfigurationModel
displayMode: string
displayModel?: DisplayModel
region: Region
exportSVG?: unknown
extraGlyphs?: ExtraGlyphValidator[]
layout: BaseLayout<unknown>
viewParams: {
start: number
end: number
offsetPx: number
offsetPx1: number
}
[key: string]: unknown
}) => {
const { features = new Map(), isFeatureDisplayed } = props
return (
<>
{[...features.values()]
.filter(feature =>
isFeatureDisplayed ? isFeatureDisplayed(feature) : true,
)
.map(feature => (
<RenderedFeatureGlyph
key={feature.id()}
feature={feature}
{...props}
/>
))}
</>
)
},
)
const SvgFeatureRendering = observer(function SvgFeatureRendering(props: {
layout: BaseLayout<unknown>
blockKey: string
regions: Region[]
bpPerPx: number
detectRerender?: () => void
config: AnyConfigurationModel
features: Map<string, Feature>
displayModel?: DisplayModel
exportSVG?: boolean
viewParams: {
start: number
end: number
offsetPx: number
offsetPx1: number
}
featureDisplayHandler?: (f: Feature) => boolean
extraGlyphs?: ExtraGlyphValidator[]
onMouseOut?: React.MouseEventHandler
onMouseDown?: React.MouseEventHandler
onMouseLeave?: React.MouseEventHandler
onMouseEnter?: React.MouseEventHandler
onMouseOver?: React.MouseEventHandler
onMouseMove?: (event: React.MouseEvent, featureId?: string) => void
onMouseUp?: React.MouseEventHandler
onClick?: React.MouseEventHandler
}) {
const {
layout,
blockKey,
regions = [],
bpPerPx,
config,
displayModel = {},
exportSVG,
featureDisplayHandler,
onMouseOut,
onMouseDown,
onMouseLeave,
onMouseEnter,
onMouseOver,
onMouseMove,
onMouseUp,
onClick,
} = props
const [region] = regions
const width = (region.end - region.start) / bpPerPx
const displayMode = readConfObject(config, 'displayMode') as string
const ref = useRef<SVGSVGElement>(null)
const [mouseIsDown, setMouseIsDown] = useState(false)
const [height, setHeight] = useState(0)
const [movedDuringLastMouseDown, setMovedDuringLastMouseDown] =
useState(false)
const mouseDown = useCallback(
(event: React.MouseEvent) => {
setMouseIsDown(true)
setMovedDuringLastMouseDown(false)
return onMouseDown?.(event)
},
[onMouseDown],
)
const mouseUp = useCallback(
(event: React.MouseEvent) => {
setMouseIsDown(false)
return onMouseUp?.(event)
},
[onMouseUp],
)
const mouseMove = useCallback(
(event: React.MouseEvent) => {
if (!ref.current) {
return
}
if (mouseIsDown) {
setMovedDuringLastMouseDown(true)
}
const { left, top } = ref.current.getBoundingClientRect()
const offsetX = event.clientX - left
const offsetY = event.clientY - top
const px = region.reversed ? width - offsetX : offsetX
const clientBp = region.start + bpPerPx * px
const featureIdCurrentlyUnderMouse = displayModel.getFeatureOverlapping?.(
blockKey,
clientBp,
offsetY,
)
if (onMouseMove) {
onMouseMove(event, featureIdCurrentlyUnderMouse)
}
},
[
blockKey,
bpPerPx,
mouseIsDown,
onMouseMove,
region.reversed,
region.start,
displayModel,
width,
],
)
const click = useCallback(
(event: React.MouseEvent) => {
// don't select a feature if we are clicking and dragging
if (movedDuringLastMouseDown) {
return
}
onClick?.(event)
},
[movedDuringLastMouseDown, onClick],
)
useEffect(() => {
setHeight(layout.getTotalHeight())
}, [layout])
return exportSVG ? (
<RenderedFeatures
displayMode={displayMode}
isFeatureDisplayed={featureDisplayHandler}
region={region}
{...props}
/>
) : (
<svg
ref={ref}
data-testid="svgfeatures"
width={width}
height={height + svgHeightPadding}
style={{
// use block because svg by default is inline, which adds a margin
display: 'block',
}}
onMouseDown={mouseDown}
onMouseUp={mouseUp}
onMouseEnter={onMouseEnter}
onMouseLeave={onMouseLeave}
onMouseOver={onMouseOver}
onMouseOut={onMouseOut}
onMouseMove={mouseMove}
onClick={click}
>
<RenderedFeatures
displayMode={displayMode}
region={region}
movedDuringLastMouseDown={movedDuringLastMouseDown}
isFeatureDisplayed={featureDisplayHandler}
{...props}
/>
<SvgOverlay
{...props}
region={region}
movedDuringLastMouseDown={movedDuringLastMouseDown}
/>
</svg>
)
})
export default SvgFeatureRendering