react-msaview 8.4.1 → 9.1.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 (193) hide show
  1. package/README.md +19 -10
  2. package/bundle/index.js +74 -74
  3. package/bundle/index.js.map +4 -4
  4. package/dist/colorSchemes.js +1 -3
  5. package/dist/colorSchemes.js.map +1 -1
  6. package/dist/columnCounts.d.ts +0 -1
  7. package/dist/columnCounts.js +1 -1
  8. package/dist/columnCounts.js.map +1 -1
  9. package/dist/components/Loading.js +5 -5
  10. package/dist/components/Loading.js.map +1 -1
  11. package/dist/components/MSAView.js +2 -2
  12. package/dist/components/MSAView.js.map +1 -1
  13. package/dist/components/MSAViewer.d.ts +1892 -7
  14. package/dist/components/MSAViewer.js +144 -202
  15. package/dist/components/MSAViewer.js.map +1 -1
  16. package/dist/components/ResizeHandles.js +4 -3
  17. package/dist/components/ResizeHandles.js.map +1 -1
  18. package/dist/components/Track.js +3 -2
  19. package/dist/components/Track.js.map +1 -1
  20. package/dist/components/controls.d.ts +11 -0
  21. package/dist/components/controls.js +48 -0
  22. package/dist/components/controls.js.map +1 -0
  23. package/dist/components/dialogs/AnnotationFileDialog.js +5 -16
  24. package/dist/components/dialogs/AnnotationFileDialog.js.map +1 -1
  25. package/dist/components/dialogs/ExportSVGDialog.js +5 -7
  26. package/dist/components/dialogs/ExportSVGDialog.js.map +1 -1
  27. package/dist/components/dialogs/HelpDialog.d.ts +6 -0
  28. package/dist/components/dialogs/HelpDialog.js +30 -0
  29. package/dist/components/dialogs/HelpDialog.js.map +1 -0
  30. package/dist/components/header/FileMenu.js +33 -7
  31. package/dist/components/header/FileMenu.js.map +1 -1
  32. package/dist/components/header/Header.js +4 -6
  33. package/dist/components/header/Header.js.map +1 -1
  34. package/dist/components/header/TreeSettingsMenu.d.ts +1 -1
  35. package/dist/components/header/TreeSettingsMenu.js +1 -32
  36. package/dist/components/header/TreeSettingsMenu.js.map +1 -1
  37. package/dist/components/header/ZoomControls.js +28 -44
  38. package/dist/components/header/ZoomControls.js.map +1 -1
  39. package/dist/components/header/settingsMenuItems.d.ts +1 -0
  40. package/dist/components/header/settingsMenuItems.js +48 -1
  41. package/dist/components/header/settingsMenuItems.js.map +1 -1
  42. package/dist/components/import/data/seq2.d.ts +0 -1
  43. package/dist/components/import/data/seq2.js +0 -17
  44. package/dist/components/import/data/seq2.js.map +1 -1
  45. package/dist/components/msa/MSACanvas.js +2 -1
  46. package/dist/components/msa/MSACanvas.js.map +1 -1
  47. package/dist/components/msa/MSACanvasBlock.js +2 -5
  48. package/dist/components/msa/MSACanvasBlock.js.map +1 -1
  49. package/dist/components/msa/drawFeatureSpans.d.ts +50 -11
  50. package/dist/components/msa/drawFeatureSpans.js +99 -56
  51. package/dist/components/msa/drawFeatureSpans.js.map +1 -1
  52. package/dist/components/msa/msaRaster.d.ts +14 -1
  53. package/dist/components/msa/msaRaster.js +19 -9
  54. package/dist/components/msa/msaRaster.js.map +1 -1
  55. package/dist/components/msa/renderBoxFeatureCanvasBlock.d.ts +9 -0
  56. package/dist/components/msa/renderBoxFeatureCanvasBlock.js +19 -8
  57. package/dist/components/msa/renderBoxFeatureCanvasBlock.js.map +1 -1
  58. package/dist/components/msa/renderHighlights.d.ts +2 -2
  59. package/dist/components/msa/renderHighlights.js +2 -3
  60. package/dist/components/msa/renderHighlights.js.map +1 -1
  61. package/dist/components/msa/renderMSAMouseover.js +2 -1
  62. package/dist/components/msa/renderMSAMouseover.js.map +1 -1
  63. package/dist/components/renderCtx.d.ts +1 -1
  64. package/dist/components/rowpanels/renderFeaturePanel.d.ts +5 -1
  65. package/dist/components/rowpanels/renderFeaturePanel.js +12 -5
  66. package/dist/components/rowpanels/renderFeaturePanel.js.map +1 -1
  67. package/dist/components/tracks/drawTracks.d.ts +1 -53
  68. package/dist/components/tracks/drawTracks.js +5 -5
  69. package/dist/components/tracks/drawTracks.js.map +1 -1
  70. package/dist/components/tree/CladeLabels.d.ts +1 -1
  71. package/dist/components/tree/CladeLabels.js +22 -34
  72. package/dist/components/tree/CladeLabels.js.map +1 -1
  73. package/dist/components/tree/TreeCanvas.js +4 -4
  74. package/dist/components/tree/TreeCanvas.js.map +1 -1
  75. package/dist/components/tree/TreeCanvasBlock.js +4 -4
  76. package/dist/components/tree/TreeCanvasBlock.js.map +1 -1
  77. package/dist/components/tree/TreeOverview.js +10 -5
  78. package/dist/components/tree/TreeOverview.js.map +1 -1
  79. package/dist/components/tree/TreePanel.js +6 -2
  80. package/dist/components/tree/TreePanel.js.map +1 -1
  81. package/dist/components/tree/TreeRuler.js +2 -2
  82. package/dist/components/tree/TreeRuler.js.map +1 -1
  83. package/dist/components/tree/cladeBrackets.d.ts +22 -17
  84. package/dist/components/tree/cladeBrackets.js +40 -9
  85. package/dist/components/tree/cladeBrackets.js.map +1 -1
  86. package/dist/components/tree/clickMap.d.ts +2 -0
  87. package/dist/components/tree/clickMap.js.map +1 -1
  88. package/dist/components/tree/ellipsize.d.ts +8 -0
  89. package/dist/components/tree/ellipsize.js +20 -0
  90. package/dist/components/tree/ellipsize.js.map +1 -0
  91. package/dist/components/tree/renderTreeCanvas.js +30 -32
  92. package/dist/components/tree/renderTreeCanvas.js.map +1 -1
  93. package/dist/components/tree/renderTreeMouseover.js +1 -1
  94. package/dist/components/tree/renderTreeMouseover.js.map +1 -1
  95. package/dist/components/tree/renderTreeOverview.js +4 -11
  96. package/dist/components/tree/renderTreeOverview.js.map +1 -1
  97. package/dist/constants.d.ts +6 -3
  98. package/dist/constants.js +16 -6
  99. package/dist/constants.js.map +1 -1
  100. package/dist/element.js +3 -6
  101. package/dist/element.js.map +1 -1
  102. package/dist/exportLayout.d.ts +39 -0
  103. package/dist/exportLayout.js +78 -0
  104. package/dist/exportLayout.js.map +1 -0
  105. package/dist/headlessRenderEnv.d.ts +12 -39
  106. package/dist/headlessRenderEnv.js +18 -12
  107. package/dist/headlessRenderEnv.js.map +1 -1
  108. package/dist/hierarchy.d.ts +5 -8
  109. package/dist/hierarchy.js +5 -79
  110. package/dist/hierarchy.js.map +1 -1
  111. package/dist/index.d.ts +3 -1
  112. package/dist/index.js +4 -1
  113. package/dist/index.js.map +1 -1
  114. package/dist/measureTextCanvas.d.ts +7 -0
  115. package/dist/measureTextCanvas.js +10 -8
  116. package/dist/measureTextCanvas.js.map +1 -1
  117. package/dist/model/treeModel.d.ts +14 -25
  118. package/dist/model/treeModel.js +16 -30
  119. package/dist/model/treeModel.js.map +1 -1
  120. package/dist/model.d.ts +108 -79
  121. package/dist/model.js +258 -209
  122. package/dist/model.js.map +1 -1
  123. package/dist/renderToSvg.d.ts +2 -7
  124. package/dist/renderToSvg.js +49 -135
  125. package/dist/renderToSvg.js.map +1 -1
  126. package/dist/svgTestUtil.d.ts +48 -48
  127. package/dist/tipLabels.d.ts +8 -0
  128. package/dist/tipLabels.js +46 -0
  129. package/dist/tipLabels.js.map +1 -0
  130. package/dist/types.d.ts +0 -1
  131. package/dist/version.d.ts +1 -1
  132. package/dist/version.js +1 -1
  133. package/dist/viewerSnapshot.d.ts +169 -0
  134. package/dist/viewerSnapshot.js +100 -0
  135. package/dist/viewerSnapshot.js.map +1 -0
  136. package/package.json +3 -3
  137. package/src/colorSchemes.ts +1 -4
  138. package/src/columnCounts.ts +1 -1
  139. package/src/components/Loading.tsx +19 -12
  140. package/src/components/MSAView.tsx +2 -2
  141. package/src/components/MSAViewer.tsx +276 -256
  142. package/src/components/ResizeHandles.tsx +10 -3
  143. package/src/components/Track.tsx +3 -2
  144. package/src/components/controls.ts +61 -0
  145. package/src/components/dialogs/AnnotationFileDialog.tsx +5 -21
  146. package/src/components/dialogs/ExportSVGDialog.tsx +8 -19
  147. package/src/components/dialogs/HelpDialog.tsx +79 -0
  148. package/src/components/header/FileMenu.tsx +33 -7
  149. package/src/components/header/Header.tsx +4 -6
  150. package/src/components/header/TreeSettingsMenu.tsx +2 -32
  151. package/src/components/header/ZoomControls.tsx +30 -66
  152. package/src/components/header/settingsMenuItems.ts +67 -0
  153. package/src/components/import/data/seq2.ts +0 -18
  154. package/src/components/msa/MSACanvas.tsx +2 -1
  155. package/src/components/msa/MSACanvasBlock.tsx +2 -6
  156. package/src/components/msa/drawFeatureSpans.ts +160 -79
  157. package/src/components/msa/msaRaster.ts +27 -9
  158. package/src/components/msa/renderBoxFeatureCanvasBlock.ts +39 -24
  159. package/src/components/msa/renderHighlights.ts +2 -3
  160. package/src/components/msa/renderMSAMouseover.ts +5 -1
  161. package/src/components/renderCtx.ts +0 -1
  162. package/src/components/rowpanels/renderFeaturePanel.ts +26 -7
  163. package/src/components/tracks/drawTracks.ts +5 -5
  164. package/src/components/tree/CladeLabels.tsx +30 -54
  165. package/src/components/tree/TreeCanvas.tsx +10 -4
  166. package/src/components/tree/TreeCanvasBlock.tsx +4 -4
  167. package/src/components/tree/TreeOverview.tsx +10 -5
  168. package/src/components/tree/TreePanel.tsx +8 -2
  169. package/src/components/tree/TreeRuler.tsx +7 -3
  170. package/src/components/tree/cladeBrackets.ts +59 -19
  171. package/src/components/tree/clickMap.ts +2 -0
  172. package/src/components/tree/ellipsize.ts +24 -0
  173. package/src/components/tree/renderTreeCanvas.ts +37 -36
  174. package/src/components/tree/renderTreeMouseover.ts +1 -1
  175. package/src/components/tree/renderTreeOverview.ts +7 -14
  176. package/src/constants.ts +17 -6
  177. package/src/element.ts +3 -7
  178. package/src/exportLayout.ts +134 -0
  179. package/src/headlessRenderEnv.ts +24 -11
  180. package/src/hierarchy.ts +5 -88
  181. package/src/index.ts +5 -5
  182. package/src/measureTextCanvas.ts +10 -8
  183. package/src/model/treeModel.ts +17 -34
  184. package/src/model.ts +296 -243
  185. package/src/renderToSvg.tsx +72 -209
  186. package/src/tipLabels.ts +51 -0
  187. package/src/types.ts +0 -1
  188. package/src/version.ts +1 -1
  189. package/src/viewerSnapshot.ts +147 -0
  190. package/dist/components/header/ZoomMenu.d.ts +0 -6
  191. package/dist/components/header/ZoomMenu.js +0 -37
  192. package/dist/components/header/ZoomMenu.js.map +0 -1
  193. package/src/components/header/ZoomMenu.tsx +0 -48
package/src/constants.ts CHANGED
@@ -55,9 +55,14 @@ export const minLetterColWidth = 5
55
55
  // sub-row band or a features panel over small rows
56
56
  export const minFeatureLabelHeight = 9
57
57
 
58
- // Tree labels are measured once at this size and scaled to the current font
59
- // size, so a vertical zoom never re-measures the tree.
60
- export const labelReferenceFontSize = 16
58
+ export function fontSizeFor(rowHeight: number) {
59
+ return Math.min(Math.max(6, rowHeight - 3), 18)
60
+ }
61
+
62
+ // The fitted tree area measures the tip labels and the clade labels at the
63
+ // font a default row draws, so a vertical zoom leaves the alignment in place.
64
+ // A larger font ellipsizes the labels.
65
+ export const labelFitFontSize = fontSizeFor(defaultRowHeight)
61
66
 
62
67
  // Zoom limits, px per cell. maxCellSize caps both smooth zoom and the stepwise
63
68
  // zoom-in buttons (which otherwise grow unbounded). minColWidth/minRowHeight are
@@ -74,8 +79,14 @@ export const defaultShowColumnStats = true
74
79
  // Tree model defaults
75
80
  export const defaultDrawLabels = true
76
81
  export const defaultLabelsAlignRight = false
77
- export const defaultTreeAreaWidth = 400
78
- export const defaultTreeWidth = 300
82
+ // The fitted tree area gives the tree pxPerTreeDepth per level of its depth,
83
+ // between minTreeWidth and maxFitTreeWidth, beside the widest label, and takes
84
+ // at most maxTreeAreaShare of a view with an alignment. Where the share binds,
85
+ // the tree keeps minTreeWidth and the labels ellipsize.
86
+ export const minTreeWidth = 70
87
+ export const maxFitTreeWidth = 160
88
+ export const pxPerTreeDepth = 10
89
+ export const maxTreeAreaShare = 0.3
79
90
  export const defaultShowBranchLen = true
80
91
  export const treeOrders = [
81
92
  'branchLength',
@@ -93,7 +104,7 @@ export const defaultOverviewHeight = 120
93
104
  // Neighbor joining does not run above this many rows. The distance matrix is
94
105
  // O(n^2*L) and the join loop O(n^3), both on the main thread with no progress
95
106
  // and no cancel: 400 rows measured 1.5s, 800 rows 10s, and 1600 would be about
96
- // seventy. See agent-docs/ideas/neighbor-joining-scaling.md.
107
+ // seventy. See agent-docs/reference/neighbor-joining-scaling.md.
97
108
  export const maxNeighborJoiningRows = 500
98
109
 
99
110
  // The opacity a `rowTint` encoding draws its scale's color at, so the residues
package/src/element.ts CHANGED
@@ -49,10 +49,6 @@ const ownAttributes = [
49
49
  // the attributes a <nightingale-manager> writes on the elements it holds
50
50
  const nightingaleAttributes = ['display-start', 'display-end', 'highlight']
51
51
 
52
- function location(uri: string | null) {
53
- return uri ? { uri, locationType: 'UriLocation' as const } : undefined
54
- }
55
-
56
52
  function numberAttribute(value: string | null | undefined) {
57
53
  const n = value ? Number(value) : Number.NaN
58
54
  return Number.isFinite(n) ? n : undefined
@@ -188,9 +184,9 @@ export function defineMsaElement(tagName = 'jbrowse-msa') {
188
184
  const theme = this.getAttribute('theme')
189
185
  return {
190
186
  ...this.#data,
191
- msaFilehandle: location(this.getAttribute('msa-url')),
192
- treeFilehandle: location(this.getAttribute('tree-url')),
193
- gffFilehandle: location(this.getAttribute('gff-url')),
187
+ msaUrl: this.getAttribute('msa-url') ?? undefined,
188
+ treeUrl: this.getAttribute('tree-url') ?? undefined,
189
+ gffUrl: this.getAttribute('gff-url') ?? undefined,
194
190
  colorScheme:
195
191
  this.#data.colorScheme ??
196
192
  this.getAttribute('color-scheme') ??
@@ -0,0 +1,134 @@
1
+ import { legendRows } from './components/msa/legendRows.ts'
2
+ import { treeScaleBarHeight } from './constants.ts'
3
+
4
+ import type { LegendRow } from './components/msa/legendRows.ts'
5
+ import type { MsaViewModel } from './model.ts'
6
+ import type { Theme } from '@mui/material'
7
+
8
+ export interface ExportSvgOptions {
9
+ theme: Theme
10
+ includeMinimap?: boolean
11
+ includeTracks?: boolean
12
+ exportType: 'entire' | 'viewport'
13
+ }
14
+
15
+ // legend geometry, shared by the width calculation and the renderer so the
16
+ // reserved column on the right exactly fits the drawn legends
17
+ export const LEGEND_PAD = 8
18
+ export const LEGEND_ROW_H = 16
19
+ export const LEGEND_SWATCH = 12
20
+ export const LEGEND_FONT = 12
21
+ export const LEGEND_CHAR_W = 7
22
+
23
+ export const LEGEND_MAX_W = 360
24
+ export const LEGEND_TEXT_X = LEGEND_PAD + LEGEND_SWATCH + 6
25
+
26
+ export function getLegendWidth(rows: LegendRow[]) {
27
+ const maxLen = rows.reduce((a, r) => Math.max(a, r.label.length), 0)
28
+ const w = LEGEND_TEXT_X + LEGEND_PAD + maxLen * LEGEND_CHAR_W
29
+ return Math.min(LEGEND_MAX_W, Math.max(120, w))
30
+ }
31
+
32
+ export function legendHeight(rows: LegendRow[]) {
33
+ return LEGEND_PAD * 2 + rows.length * LEGEND_ROW_H
34
+ }
35
+
36
+ // resolved sizes/offsets (in svg user units) for the chosen export, shared by
37
+ // every layer; 'entire' renders the whole alignment unscrolled, 'viewport'
38
+ // mirrors the live scroll position
39
+ export interface Layout {
40
+ width: number
41
+ msaAreaWidth: number
42
+ height: number
43
+ contentHeight: number
44
+ trackHeight: number
45
+ // the band the minimap, the row panel headers and the tree overview stacked
46
+ // on the scale bar share across the top
47
+ topHeight: number
48
+ offsetX: number
49
+ offsetY: number
50
+ includeMinimap: boolean
51
+ legendWidth: number
52
+ }
53
+
54
+ export function getLayout(model: MsaViewModel, opts: ExportSvgOptions): Layout {
55
+ const {
56
+ scrollX,
57
+ scrollY,
58
+ totalWidth,
59
+ totalHeight,
60
+ effectiveTreeAreaWidth,
61
+ rowPanelsWidth,
62
+ rowPanelsHeaderHeight,
63
+ totalTrackAreaHeight,
64
+ minimapHeight,
65
+ msaAreaHeight,
66
+ msaCanvasWidth,
67
+ showHorizontalScrollbar,
68
+ treeOverviewHeight,
69
+ treeScaleBar,
70
+ } = model
71
+ const trackHeight = opts.includeTracks ? totalTrackAreaHeight : 0
72
+ // the minimap reflects the live viewport scroll position, so it's only
73
+ // meaningful for a viewport export, never for the entire alignment -- and
74
+ // only when the live view shows one at all, since a minimap over an alignment
75
+ // that already fits marks a viewport wider than the bar
76
+ const includeMinimap =
77
+ opts.exportType === 'viewport' &&
78
+ !!opts.includeMinimap &&
79
+ showHorizontalScrollbar
80
+ const rows = legendRows(model.legends)
81
+ const legendWidth = rows.length > 0 ? getLegendWidth(rows) : 0
82
+ const topHeight = Math.max(
83
+ includeMinimap ? minimapHeight : 0,
84
+ rowPanelsHeaderHeight,
85
+ treeOverviewHeight + (treeScaleBar ? treeScaleBarHeight : 0),
86
+ )
87
+ const gutterWidth = effectiveTreeAreaWidth + rowPanelsWidth
88
+
89
+ // width stays content-only; the legend occupies an extra column added at the
90
+ // svg root in MsaSvg. The viewport export takes the alignment canvas's own
91
+ // size rather than the widget's -- the widget box also covers the header, the
92
+ // resize handle and the scrollbars, and rendering that box exports rows and
93
+ // columns the scrollbars hide on screen
94
+ return opts.exportType === 'entire'
95
+ ? {
96
+ width: totalWidth + gutterWidth,
97
+ msaAreaWidth: totalWidth,
98
+ height: totalHeight + trackHeight + topHeight,
99
+ contentHeight: totalHeight,
100
+ trackHeight,
101
+ topHeight,
102
+ offsetX: 0,
103
+ offsetY: 0,
104
+ includeMinimap,
105
+ legendWidth,
106
+ }
107
+ : {
108
+ width: msaCanvasWidth + gutterWidth,
109
+ msaAreaWidth: msaCanvasWidth,
110
+ height: msaAreaHeight + topHeight + trackHeight,
111
+ contentHeight: msaAreaHeight,
112
+ trackHeight,
113
+ topHeight,
114
+ offsetX: -scrollX,
115
+ offsetY: -scrollY,
116
+ includeMinimap,
117
+ legendWidth,
118
+ }
119
+ }
120
+
121
+ /**
122
+ * the exported page's size: the layout plus the legend column, and as tall as
123
+ * the legend where the key outgrows a short alignment
124
+ */
125
+ export function exportPageSize(model: MsaViewModel, layout: Layout) {
126
+ const { width, height, legendWidth, topHeight } = layout
127
+ return {
128
+ width: width + legendWidth,
129
+ height:
130
+ legendWidth > 0
131
+ ? Math.max(height, topHeight + legendHeight(legendRows(model.legends)))
132
+ : height,
133
+ }
134
+ }
@@ -12,7 +12,9 @@
12
12
  * leaves the rest undefined -- every emitted transform then carries `NaN` in its
13
13
  * x components, which no assertion on a drawn x-position can survive.
14
14
  */
15
- export class HeadlessDOMMatrix {
15
+ import { CHAR_WIDTH_RATIO } from './measureTextCanvas.ts'
16
+
17
+ class HeadlessDOMMatrix {
16
18
  a: number
17
19
  b: number
18
20
  c: number
@@ -50,7 +52,7 @@ export class HeadlessDOMMatrix {
50
52
  }
51
53
  }
52
54
 
53
- export class HeadlessDOMPoint {
55
+ class HeadlessDOMPoint {
54
56
  constructor(
55
57
  public x = 0,
56
58
  public y = 0,
@@ -69,10 +71,11 @@ export class HeadlessDOMPoint {
69
71
  * its own jsdom passes that window instead, because Node has no global
70
72
  * HTMLCanvasElement to patch.
71
73
  *
72
- * `measureText` reports 0.6em per character, close enough to a real sans-serif
73
- * average for layout and, more usefully, exactly predictable: a caller can
74
- * compute the width a renderer will see rather than hardcoding a number
75
- * measured from one machine's fonts.
74
+ * Without `measureContext`, `measureText` reports 0.6em per character, close
75
+ * enough to a real sans-serif average for layout and exactly predictable: a
76
+ * test can compute the width a renderer will see. A caller with a real canvas,
77
+ * such as react-msaview-cli with @napi-rs/canvas, passes one of its 2D
78
+ * contexts as `measureContext` to measure with the installed fonts.
76
79
  *
77
80
  * `contextExtras` adds to the context a test hands out -- the raster test
78
81
  * supplies the ImageData methods that make the export take its image path.
@@ -80,6 +83,7 @@ export class HeadlessDOMPoint {
80
83
  export function installHeadlessRenderEnv(
81
84
  win: unknown = globalThis,
82
85
  contextExtras: Partial<CanvasRenderingContext2D> = {},
86
+ measureContext?: MeasureContext,
83
87
  ) {
84
88
  const g = globalThis as Record<string, unknown>
85
89
  g.DOMMatrix = HeadlessDOMMatrix
@@ -96,14 +100,23 @@ export function installHeadlessRenderEnv(
96
100
  set font(v: string) {
97
101
  font = v
98
102
  },
99
- measureText: (t: string) => ({
100
- width: t.length * (Number.parseFloat(font) || 10) * CHAR_WIDTH_RATIO,
101
- }),
103
+ measureText: (t: string) => {
104
+ if (measureContext) {
105
+ measureContext.font = font
106
+ return { width: measureContext.measureText(t).width }
107
+ }
108
+ return {
109
+ width:
110
+ t.length * (Number.parseFloat(font) || 10) * CHAR_WIDTH_RATIO,
111
+ }
112
+ },
102
113
  },
103
114
  contextExtras,
104
115
  ) as unknown as CanvasRenderingContext2D
105
116
  } as unknown as typeof HTMLCanvasElement.prototype.getContext
106
117
  }
107
118
 
108
- /** the per-character width factor `measureText` above reports */
109
- export const CHAR_WIDTH_RATIO = 0.6
119
+ export interface MeasureContext {
120
+ font: string
121
+ measureText(text: string): { width: number }
122
+ }
package/src/hierarchy.ts CHANGED
@@ -61,56 +61,6 @@ export function hierarchy<T>(
61
61
  return coreHierarchy(data, childrenAccessor)
62
62
  }
63
63
 
64
- export function clusterLayout<T>(
65
- root: HierarchyNode<T>,
66
- sizeX: number,
67
- sizeY: number,
68
- ) {
69
- const leafNodes = leaves(root)
70
- const n = leafNodes.length
71
- const step = sizeX / n
72
-
73
- for (let i = 0; i < n; i++) {
74
- leafNodes[i]!.x = (i + 0.5) * step
75
- }
76
-
77
- // x of an internal node is the mean of its children's x, so process in
78
- // post-order (children before parents). The subtree span comes from the same
79
- // pass, for the block culls in the tree renderer.
80
- const nodes = descendants(root)
81
- for (let i = nodes.length - 1; i >= 0; i--) {
82
- const node = nodes[i]!
83
- if (node.children) {
84
- let sum = 0
85
- let min = Infinity
86
- let max = -Infinity
87
- for (const child of node.children) {
88
- sum += child.x!
89
- min = Math.min(min, child.xMin!)
90
- max = Math.max(max, child.xMax!)
91
- }
92
- node.x = sum / node.children.length
93
- node.xMin = min
94
- node.xMax = max
95
- } else {
96
- node.xMin = node.x
97
- node.xMax = node.x
98
- }
99
- }
100
-
101
- const rootHeight = root.height
102
- const stack = [{ node: root, depth: 0 }]
103
- while (stack.length > 0) {
104
- const { node, depth } = stack.pop()!
105
- node.y = rootHeight === 0 ? sizeY : (depth / rootHeight) * sizeY
106
- if (node.children) {
107
- for (const child of node.children) {
108
- stack.push({ node: child, depth: depth + 1 })
109
- }
110
- }
111
- }
112
- }
113
-
114
64
  export interface TreeScale {
115
65
  readonly rowHeight: number
116
66
  readonly treeWidth: number
@@ -186,10 +136,10 @@ export class LaidOutNode<T> implements HierarchyNode<T> {
186
136
  }
187
137
 
188
138
  /**
189
- * The layout `clusterLayout` and `setBrLength` write, on new nodes: tips one
190
- * row apart, a parent at the mean of its children, and branch length measured
191
- * from the root, whose own branch is not drawn. A collapsed node records the
192
- * farthest tip of its hidden subtree, where the base of its triangle goes.
139
+ * The tree laid out on new nodes: tips one row apart, a parent at the mean of
140
+ * its children, and branch length measured from the root, whose own branch is
141
+ * not drawn. A collapsed node records the farthest tip of its hidden subtree,
142
+ * where the base of its triangle goes.
193
143
  */
194
144
  export function layoutTree<T extends { length?: number }>(
195
145
  root: HierarchyNode<T>,
@@ -329,39 +279,6 @@ export function calcDepthToLeaf<T>(node: HierarchyNode<T>): number {
329
279
  return node.depthToLeaf!
330
280
  }
331
281
 
332
- // Max root-to-leaf sum of branch lengths within the subtree at d. Negative
333
- // lengths are floored at zero, the same as setBrLength does when it lays the
334
- // tree out, so the extent this reports is the one that gets drawn
335
- export function maxLength(d: HierarchyNode): number {
336
- const nodes = descendants(d)
337
- const pathLen = new Map<HierarchyNode, number>()
338
- for (let i = nodes.length - 1; i >= 0; i--) {
339
- const n = nodes[i]!
340
- let childMax = 0
341
- if (n.children) {
342
- for (const child of n.children) {
343
- childMax = Math.max(childMax, pathLen.get(child)!)
344
- }
345
- }
346
- pathLen.set(n, Math.max(n.data.length || 0, 0) + childMax)
347
- }
348
- return pathLen.get(d)!
349
- }
350
-
351
- export function setBrLength(d: HierarchyNode, y0: number, k: number) {
352
- const stack = [{ node: d, y0 }]
353
- while (stack.length > 0) {
354
- const { node, y0: acc } = stack.pop()!
355
- const next = acc + Math.max(node.data.length || 0, 0)
356
- node.len = next * k
357
- if (node.children) {
358
- for (const child of node.children) {
359
- stack.push({ node: child, y0: next })
360
- }
361
- }
362
- }
363
- }
364
-
365
282
  /**
366
283
  * Every leaf by name, with a name two leaves share mapping to undefined: a tip
367
284
  * set naming it cannot say which tip it means.
@@ -416,7 +333,7 @@ export function mrca<T extends { name: string }>(
416
333
 
417
334
  /**
418
335
  * The deepest node whose subtree covers a run of rows, given in the row-space
419
- * coordinates clusterLayout writes to xMin/xMax. A subtree's leaves are one
336
+ * coordinates layoutTree writes to xMin/xMax. A subtree's leaves are one
420
337
  * contiguous run, so at most one child per level covers the run and the descent
421
338
  * visits the tree's depth rather than its nodes.
422
339
  */
package/src/index.ts CHANGED
@@ -11,10 +11,8 @@ export { renderToSvg } from './renderToSvg.tsx'
11
11
  export type { ExportSvgOptions } from './renderToSvg.tsx'
12
12
  // renderToSvg outside a browser needs DOM bits jsdom omits; react-msaview-cli
13
13
  // and the README figure generator both drive it that way
14
- export {
15
- CHAR_WIDTH_RATIO,
16
- installHeadlessRenderEnv,
17
- } from './headlessRenderEnv.ts'
14
+ export { installHeadlessRenderEnv } from './headlessRenderEnv.ts'
15
+ export { CHAR_WIDTH_RATIO } from './measureTextCanvas.ts'
18
16
  // the row count above which calculateNeighborJoiningTreeFromMSA throws, for
19
17
  // hosts gating their own menu item
20
18
  export { maxNeighborJoiningRows } from './constants.ts'
@@ -29,6 +27,8 @@ export type {
29
27
  export { default as MSAView } from './components/Loading.tsx'
30
28
  export { default as MSAViewer } from './components/MSAViewer.tsx'
31
29
  export type { MSAViewerProps } from './components/MSAViewer.tsx'
30
+ // the snapshot MSAViewer builds from its props, for the CLI's --props
31
+ export { resolveTheme, viewerSnapshot } from './viewerSnapshot.ts'
32
32
  export { mount } from './mount.tsx'
33
33
  export { defineMsaElement } from './element.ts'
34
34
  export { useMsaSvgFigure } from './useMsaSvgFigure.ts'
@@ -37,7 +37,7 @@ export type { MountedViewer } from './mount.tsx'
37
37
  export { type MsaViewModel, default as MSAModelF } from './model.ts'
38
38
  export type { MSAFormat, MSAParserType } from 'msa-parsers'
39
39
  export type { HierarchyNode, LaidOutNode, TreeScale } from './hierarchy.ts'
40
- // InterProScanResults is the EBI wire format that setDomains still accepts;
40
+ // InterProScanResults is the EBI wire format interProScanToAnnotations reads;
41
41
  // Annotation is the source-agnostic shape everything else is written against
42
42
  export type { InterProScanResults } from 'msa-parsers'
43
43
  // ColumnCounts, ColumnStats, DomainBand and TidyDomainAnnotation surface in the
@@ -1,7 +1,7 @@
1
1
  import { setFontSize } from './setFontSize.ts'
2
2
 
3
- // Cached across calls: labelWidthMap measures every leaf label on every layout,
4
- // and both createElement and getContext are far more expensive than measureText.
3
+ // Cached across calls, since both createElement and getContext are far more
4
+ // expensive than measureText.
5
5
  // `null` records that this environment has no 2d context, so the lookup is not
6
6
  // retried on every label.
7
7
  let ctxHandle: CanvasRenderingContext2D | null | undefined
@@ -16,16 +16,18 @@ function getCtx() {
16
16
  return ctxHandle
17
17
  }
18
18
 
19
- // Rough average glyph width as a fraction of the font size for the sans-serif
20
- // stack the tree labels use. Only reached where there is no canvas to measure
21
- // with (jsdom, SSR); estimating the label gutter beats throwing out of the
22
- // layout getters that read this on every render.
23
- const fallbackGlyphWidthRatio = 0.6
19
+ /**
20
+ * Rough average glyph width as a fraction of the font size for the sans-serif
21
+ * stack the labels use. measureTextCanvas falls back on it where there is no
22
+ * canvas to measure with (jsdom, SSR), and the headless render environment's
23
+ * `measureText` reports it.
24
+ */
25
+ export const CHAR_WIDTH_RATIO = 0.6
24
26
 
25
27
  export function measureTextCanvas(text: string, fontSize: number) {
26
28
  const ctx = getCtx()
27
29
  if (!ctx) {
28
- return text.length * fontSize * fallbackGlyphWidthRatio
30
+ return text.length * fontSize * CHAR_WIDTH_RATIO
29
31
  }
30
32
  setFontSize(ctx, fontSize)
31
33
  return ctx.measureText(text).width
@@ -10,8 +10,6 @@ import {
10
10
  defaultShowBranchLen,
11
11
  defaultTreeOrder,
12
12
  defaultShowTreeOverview,
13
- defaultTreeAreaWidth,
14
- defaultTreeWidth,
15
13
  treeOrders,
16
14
  } from '../constants.ts'
17
15
  import { stripDefault } from '../stripDefault.ts'
@@ -36,16 +34,19 @@ export function TreeModelF() {
36
34
 
37
35
  /**
38
36
  * #property
39
- * width of the area the tree is drawn in, px
37
+ * width of the area the tree and its labels draw in, px. Undefined fits
38
+ * the area to the labels and the tree; `effectiveTreeAreaWidth` reads
39
+ * the width either way
40
40
  */
41
- treeAreaWidth: stripDefault(types.number, defaultTreeAreaWidth),
41
+ treeAreaWidth: types.maybe(types.number),
42
42
 
43
43
  /**
44
44
  * #property
45
- * width of the tree within the treeArea, px. automatically synced to
46
- * fit within treeAreaWidth
45
+ * width of the tree within the tree area, px. Undefined gives the tree
46
+ * the area less the labels; `effectiveTreeWidth` reads the width either
47
+ * way
47
48
  */
48
- treeWidth: stripDefault(types.number, defaultTreeWidth),
49
+ treeWidth: types.maybe(types.number),
49
50
 
50
51
  /**
51
52
  * #property
@@ -97,29 +98,24 @@ export function TreeModelF() {
97
98
  * height of the tree overview band, px
98
99
  */
99
100
  overviewHeight: stripDefault(types.number, defaultOverviewHeight),
100
-
101
- /**
102
- * #property
103
- * auto-size treeAreaWidth to fit the row labels (plus the tree, if drawn)
104
- * instead of using a fixed width. useful when there is no tree, so the
105
- * label gutter isn't padded out to the default 400px
106
- */
107
- autoTreeAreaWidth: stripDefault(types.boolean, false),
108
101
  })
109
102
  .actions(self => ({
110
103
  /**
111
104
  * #action
112
- * set tree area width (px)
105
+ * set the tree area width, px, or undefined to fit it to the labels and
106
+ * the tree. The tree then fills the area less the labels
113
107
  */
114
- setTreeAreaWidth(n: number) {
115
- self.treeAreaWidth = Math.round(n)
108
+ setTreeAreaWidth(n: number | undefined) {
109
+ self.treeAreaWidth = n === undefined ? undefined : Math.round(n)
110
+ self.treeWidth = undefined
116
111
  },
117
112
  /**
118
113
  * #action
119
- * set tree width (px)
114
+ * set the tree width, px, or undefined to fill the tree area less the
115
+ * labels
120
116
  */
121
- setTreeWidth(n: number) {
122
- self.treeWidth = Math.round(n)
117
+ setTreeWidth(n: number | undefined) {
118
+ self.treeWidth = n === undefined ? undefined : Math.round(n)
123
119
  },
124
120
 
125
121
  /**
@@ -134,12 +130,6 @@ export function TreeModelF() {
134
130
  setDrawTree(arg: boolean) {
135
131
  self.drawTree = arg
136
132
  },
137
- /**
138
- * #action
139
- */
140
- setAutoTreeAreaWidth(arg: boolean) {
141
- self.autoTreeAreaWidth = arg
142
- },
143
133
 
144
134
  /**
145
135
  * #action
@@ -173,13 +163,6 @@ export function TreeModelF() {
173
163
  setShowTreeOverview(arg: boolean) {
174
164
  self.showTreeOverview = arg
175
165
  },
176
- /**
177
- * #action
178
- * set the height of the tree overview band (px)
179
- */
180
- setOverviewHeight(n: number) {
181
- self.overviewHeight = Math.round(n)
182
- },
183
166
  /**
184
167
  * #action
185
168
  */