Skip to content

Commit

Permalink
Fix labels being cut-off in SVG features by rendering feature labels …
Browse files Browse the repository at this point in the history
…on main thread (#3088)
  • Loading branch information
cmdcolin committed Jul 13, 2022
1 parent 8adb316 commit 59d9713
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ function FeatureGlyph(props: {
shouldShowDescription: boolean
fontHeight: number
allowedWidthExpansion: number
exportSVG: unknown
displayModel: DisplayModel
selected?: boolean
reversed?: boolean
Expand Down
19 changes: 16 additions & 3 deletions plugins/svg/src/SvgFeatureRenderer/components/FeatureLabel.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React from 'react'
import React, { useEffect, useState } from 'react'
import { observer } from 'mobx-react'
import { isAlive, isStateTreeNode } from 'mobx-state-tree'
import { measureText, getViewParams, Feature, Region } from '@jbrowse/core/util'
Expand All @@ -12,6 +12,7 @@ export default observer(
region,
reversed,
bpPerPx,
exportSVG,
feature,
viewParams,
color = 'black',
Expand All @@ -32,6 +33,7 @@ export default observer(
reversed?: boolean
displayModel: DisplayModel
region: Region
exportSVG: unknown
viewParams: {
start: number
end: number
Expand All @@ -48,9 +50,20 @@ export default observer(

const viewLeft = reversed ? params.end : params.start

const [labelVisible, setLabelVisible] = useState(exportSVG)

// we use an effect to set the label visible because there can be a
// mismatch between the server and the client after hydration due to the
// floating labels. if we are exporting an SVG we allow it as is though and
// do not use the effetct
useEffect(() => {
setLabelVisible(true)
}, [])

if (isStateTreeNode(region) && !isAlive(region)) {
return null
}

const rstart = region.start
const rend = region.end
const fstart = feature.get('start')
Expand All @@ -74,12 +87,12 @@ export default observer(
x = params.offsetPx1
}

return (
return labelVisible ? (
<text x={x} y={y + fontHeight} fill={color} fontSize={fontHeight}>
{measuredTextWidth > totalWidth
? `${text.slice(0, totalWidth / (fontHeight * 0.6))}...`
: text}
</text>
)
) : null
},
)
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ function RenderedFeatureGlyph(props: {
layout: BaseLayout<unknown>
extraGlyphs: ExtraGlyphValidator[]
displayMode: string
exportSVG: unknown
displayModel: DisplayModel
viewParams: {
start: number
Expand Down Expand Up @@ -145,6 +146,7 @@ const RenderedFeatures = observer(
displayMode: string
displayModel: DisplayModel
region: Region
exportSVG: unknown
extraGlyphs: ExtraGlyphValidator[]
layout: BaseLayout<unknown>
viewParams: {
Expand Down

0 comments on commit 59d9713

Please sign in to comment.