react-msaview 5.8.0 → 5.10.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.
- package/bundle/index.js +84 -84
- package/bundle/index.js.map +3 -3
- package/dist/components/ConservationTrack.js +21 -16
- package/dist/components/ConservationTrack.js.map +1 -1
- package/dist/components/MSAView.js +17 -23
- package/dist/components/MSAView.js.map +1 -1
- package/dist/components/TextTrack.js +26 -21
- package/dist/components/TextTrack.js.map +1 -1
- package/dist/components/header/settingsMenuItems.js +39 -72
- package/dist/components/header/settingsMenuItems.js.map +1 -1
- package/dist/components/msa/DomainLegend.js +4 -5
- package/dist/components/msa/DomainLegend.js.map +1 -1
- package/dist/components/msa/MSACanvasBlock.js +19 -16
- package/dist/components/msa/MSACanvasBlock.js.map +1 -1
- package/dist/components/msa/MSAMouseoverCanvas.js +13 -6
- package/dist/components/msa/MSAMouseoverCanvas.js.map +1 -1
- package/dist/components/msa/renderBoxFeatureCanvasBlock.js +9 -6
- package/dist/components/msa/renderBoxFeatureCanvasBlock.js.map +1 -1
- package/dist/components/msa/renderMSAMouseover.js +31 -31
- package/dist/components/msa/renderMSAMouseover.js.map +1 -1
- package/dist/components/overlayColors.d.ts +2 -0
- package/dist/components/overlayColors.js +5 -0
- package/dist/components/overlayColors.js.map +1 -1
- package/dist/components/tree/TreeCanvas.js +29 -24
- package/dist/components/tree/TreeCanvas.js.map +1 -1
- package/dist/components/tree/TreeCanvasBlock.js +19 -12
- package/dist/components/tree/TreeCanvasBlock.js.map +1 -1
- package/dist/components/tree/renderTreeCanvas.d.ts +0 -38
- package/dist/components/tree/renderTreeCanvas.js +17 -17
- package/dist/components/tree/renderTreeCanvas.js.map +1 -1
- package/dist/constants.d.ts +1 -0
- package/dist/constants.js +1 -0
- package/dist/constants.js.map +1 -1
- package/dist/model.d.ts +16 -1
- package/dist/model.js +99 -114
- package/dist/model.js.map +1 -1
- package/dist/neighborJoining.js +12 -19
- package/dist/neighborJoining.js.map +1 -1
- package/dist/parseAsn1.js +32 -43
- package/dist/parseAsn1.js.map +1 -1
- package/dist/renderToSvg.js +3 -0
- package/dist/renderToSvg.js.map +1 -1
- package/dist/seqPosToGlobalCol.d.ts +6 -19
- package/dist/seqPosToGlobalCol.js +6 -33
- package/dist/seqPosToGlobalCol.js.map +1 -1
- package/dist/useCanvasAutorun.d.ts +23 -1
- package/dist/useCanvasAutorun.js +20 -8
- package/dist/useCanvasAutorun.js.map +1 -1
- package/dist/version.d.ts +1 -1
- package/dist/version.js +1 -1
- package/dist/version.js.map +1 -1
- package/package.json +3 -3
- package/src/components/ConservationTrack.tsx +7 -5
- package/src/components/MSAView.tsx +24 -30
- package/src/components/TextTrack.tsx +7 -12
- package/src/components/header/settingsMenuItems.ts +40 -72
- package/src/components/msa/DomainLegend.tsx +4 -3
- package/src/components/msa/MSACanvasBlock.tsx +16 -21
- package/src/components/msa/MSAMouseoverCanvas.tsx +10 -6
- package/src/components/msa/renderBoxFeatureCanvasBlock.test.ts +51 -0
- package/src/components/msa/renderBoxFeatureCanvasBlock.ts +13 -8
- package/src/components/msa/renderMSAMouseover.ts +37 -32
- package/src/components/overlayColors.ts +6 -0
- package/src/components/tree/TreeCanvas.tsx +6 -4
- package/src/components/tree/TreeCanvasBlock.tsx +11 -12
- package/src/components/tree/renderTreeCanvas.ts +20 -17
- package/src/constants.ts +1 -0
- package/src/model.ts +114 -130
- package/src/modelFilehandleLoaders.test.ts +137 -0
- package/src/neighborJoining.test.ts +12 -0
- package/src/neighborJoining.ts +12 -20
- package/src/parseAsn1.ts +32 -40
- package/src/{renderDomainLegendSvg.test.tsx → renderDomainsSvg.test.tsx} +44 -20
- package/src/renderToSvg.tsx +3 -0
- package/src/seqPosToGlobalCol.test.ts +39 -51
- package/src/seqPosToGlobalCol.ts +6 -41
- package/src/stripDefaultSnapshot.test.ts +11 -0
- package/src/useCanvasAutorun.test.tsx +98 -0
- package/src/useCanvasAutorun.ts +30 -11
- package/src/version.ts +1 -1
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
// @vitest-environment jsdom
|
|
2
2
|
//
|
|
3
|
-
// Verifies the
|
|
4
|
-
//
|
|
5
|
-
// impgRender.test.tsx (svgcanvas needs
|
|
3
|
+
// Verifies the SVG export draws the domain overlay: the boxes themselves across
|
|
4
|
+
// the full alignment width, and the color-key legend as a reserved column on the
|
|
5
|
+
// right. Polyfills mirror impgRender.test.tsx (svgcanvas needs
|
|
6
|
+
// DOMMatrix/DOMPoint + a measureText).
|
|
6
7
|
import { createJBrowseTheme } from '@jbrowse/core/ui/theme'
|
|
7
8
|
import { enableStaticRendering } from 'mobx-react'
|
|
8
9
|
import { beforeAll, expect, test } from 'vitest'
|
|
@@ -76,13 +77,17 @@ ACDEFGHIKL
|
|
|
76
77
|
>seq2
|
|
77
78
|
ACDE-GHIKL`
|
|
78
79
|
|
|
79
|
-
function makeModel(
|
|
80
|
+
function makeModel({
|
|
81
|
+
data = msa,
|
|
82
|
+
start = 1,
|
|
83
|
+
end = 5,
|
|
84
|
+
}: { data?: string; start?: number; end?: number } = {}) {
|
|
80
85
|
const model = MSAModelF().create({
|
|
81
|
-
id: 'domain-
|
|
86
|
+
id: 'domain-svg-test',
|
|
82
87
|
type: 'MsaView',
|
|
83
88
|
height: 400,
|
|
84
89
|
msaFormat: 'fasta',
|
|
85
|
-
data: { msa },
|
|
90
|
+
data: { msa: data },
|
|
86
91
|
})
|
|
87
92
|
model.setWidth(800)
|
|
88
93
|
model.setDomains({
|
|
@@ -97,7 +102,7 @@ function makeModel() {
|
|
|
97
102
|
description: 'Protein kinase domain',
|
|
98
103
|
},
|
|
99
104
|
},
|
|
100
|
-
locations: [{ start
|
|
105
|
+
locations: [{ start, end }],
|
|
101
106
|
},
|
|
102
107
|
],
|
|
103
108
|
},
|
|
@@ -105,18 +110,21 @@ function makeModel() {
|
|
|
105
110
|
return model
|
|
106
111
|
}
|
|
107
112
|
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
expect(model.actuallyShowDomains).toBe(true)
|
|
111
|
-
expect(model.visibleDomainTypes.map(d => d.accession)).toEqual(['PF00069'])
|
|
112
|
-
|
|
113
|
-
const svg = await renderToSvg(model, {
|
|
113
|
+
function exportEntire(model: ReturnType<typeof makeModel>) {
|
|
114
|
+
return renderToSvg(model, {
|
|
114
115
|
theme: createJBrowseTheme(),
|
|
115
116
|
exportType: 'entire',
|
|
116
117
|
includeMinimap: false,
|
|
117
118
|
includeTracks: false,
|
|
118
119
|
})
|
|
119
|
-
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
test('domain legend is drawn in the svg export when domains are shown', async () => {
|
|
123
|
+
const model = makeModel()
|
|
124
|
+
expect(model.actuallyShowDomains).toBe(true)
|
|
125
|
+
expect(model.visibleDomainTypes.map(d => d.accession)).toEqual(['PF00069'])
|
|
126
|
+
|
|
127
|
+
expect(await exportEntire(model)).toContain('Kinase')
|
|
120
128
|
})
|
|
121
129
|
|
|
122
130
|
test('no legend column when domains are hidden', async () => {
|
|
@@ -124,11 +132,27 @@ test('no legend column when domains are hidden', async () => {
|
|
|
124
132
|
model.setShowDomains(false)
|
|
125
133
|
expect(model.actuallyShowDomains).toBe(false)
|
|
126
134
|
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
135
|
+
expect(await exportEntire(model)).not.toContain('Kinase')
|
|
136
|
+
})
|
|
137
|
+
|
|
138
|
+
test('a domain box past the on-screen block size still draws', async () => {
|
|
139
|
+
// the export renders the alignment in one pass, so the overlay has to be told
|
|
140
|
+
// how wide that pass is; falling back to the 500px on-screen block size culled
|
|
141
|
+
// every band to the left edge of a wide alignment
|
|
142
|
+
const row = 'ACDEFGHIKL'.repeat(20)
|
|
143
|
+
const model = makeModel({
|
|
144
|
+
data: `>seq1\n${row}\n>seq2\n${row}`,
|
|
145
|
+
start: 151,
|
|
146
|
+
end: 190,
|
|
132
147
|
})
|
|
133
|
-
|
|
148
|
+
const band = model.domainBands.get('seq1')![0]!
|
|
149
|
+
const x = band.startCol * model.colWidth
|
|
150
|
+
expect(x).toBeGreaterThan(model.blockSize)
|
|
151
|
+
|
|
152
|
+
const svg = await exportEntire(model)
|
|
153
|
+
const fill = model.fillPalette.PF00069!
|
|
154
|
+
const rects = [...svg.matchAll(/<rect[^>]*>/g)].map(m => m[0])
|
|
155
|
+
expect(
|
|
156
|
+
rects.some(r => r.includes(`fill="${fill}"`) && r.includes(`x="${x}"`)),
|
|
157
|
+
).toBe(true)
|
|
134
158
|
})
|
package/src/renderToSvg.tsx
CHANGED
|
@@ -251,6 +251,9 @@ function CoreRendering({
|
|
|
251
251
|
ctx: msaCtx,
|
|
252
252
|
offsetX,
|
|
253
253
|
offsetY,
|
|
254
|
+
// the overlay culls bands to this width; without it the export falls back
|
|
255
|
+
// to the on-screen 500px block size and drops every band past it
|
|
256
|
+
blockSizeXOverride: msaAreaWidth,
|
|
254
257
|
blockSizeYOverride: contentHeight,
|
|
255
258
|
highResScaleFactorOverride: 1,
|
|
256
259
|
})
|
|
@@ -1,71 +1,59 @@
|
|
|
1
|
+
// seqPos -> global column, exercised through the model method rather than the
|
|
2
|
+
// buildSeqPosIndex primitive behind it: the method is what
|
|
3
|
+
// jbrowse-plugin-protein3d calls (see the cross-repo note in model.ts), and it
|
|
4
|
+
// owns the past-the-end answers the index alone cannot give.
|
|
1
5
|
import { describe, expect, test } from 'vitest'
|
|
2
6
|
|
|
3
|
-
import
|
|
7
|
+
import MSAModelF from './model.ts'
|
|
8
|
+
|
|
9
|
+
function colsOf(row: string, positions: number[]) {
|
|
10
|
+
const model = MSAModelF().create({
|
|
11
|
+
type: 'MsaView',
|
|
12
|
+
data: { msa: `>r\n${row}` },
|
|
13
|
+
})
|
|
14
|
+
model.setWidth(800)
|
|
15
|
+
return positions.map(pos => model.seqPosToGlobalCol('r', pos))
|
|
16
|
+
}
|
|
4
17
|
|
|
5
18
|
describe('seqPosToGlobalCol', () => {
|
|
6
|
-
test('
|
|
7
|
-
|
|
8
|
-
expect(
|
|
9
|
-
expect(seqPosToGlobalCol({ row, seqPos: 3 })).toBe(3)
|
|
10
|
-
expect(seqPosToGlobalCol({ row, seqPos: 7 })).toBe(7)
|
|
11
|
-
expect(seqPosToGlobalCol({ row, seqPos: 8 })).toBe(8) // Past end
|
|
19
|
+
test('is the identity when the row has no gaps', () => {
|
|
20
|
+
// 8 residues, so seqPos 8 is one past the end
|
|
21
|
+
expect(colsOf('ATGCATGC', [0, 3, 7, 8])).toEqual([0, 3, 7, 8])
|
|
12
22
|
})
|
|
13
23
|
|
|
14
|
-
test('
|
|
15
|
-
const row = 'A-TG-CA-TGC'
|
|
24
|
+
test('skips gap columns', () => {
|
|
16
25
|
// Global: A(0) -(1) T(2) G(3) -(4) C(5) A(6) -(7) T(8) G(9) C(10)
|
|
17
26
|
// SeqPos: A(0) T(1) G(2) C(3) A(4) T(5) G(6) C(7)
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
expect(seqPosToGlobalCol({ row, seqPos: 2 })).toBe(3) // G
|
|
22
|
-
expect(seqPosToGlobalCol({ row, seqPos: 3 })).toBe(5) // C
|
|
23
|
-
expect(seqPosToGlobalCol({ row, seqPos: 4 })).toBe(6) // A
|
|
24
|
-
expect(seqPosToGlobalCol({ row, seqPos: 5 })).toBe(8) // T
|
|
25
|
-
expect(seqPosToGlobalCol({ row, seqPos: 6 })).toBe(9) // G
|
|
26
|
-
expect(seqPosToGlobalCol({ row, seqPos: 7 })).toBe(10) // C
|
|
27
|
-
expect(seqPosToGlobalCol({ row, seqPos: 8 })).toBe(11) // Past end
|
|
28
|
-
})
|
|
29
|
-
|
|
30
|
-
test('handles empty row', () => {
|
|
31
|
-
expect(seqPosToGlobalCol({ row: '', seqPos: 0 })).toBe(0)
|
|
27
|
+
expect(colsOf('A-TG-CA-TGC', [0, 1, 2, 3, 4, 5, 6, 7, 8])).toEqual([
|
|
28
|
+
0, 2, 3, 5, 6, 8, 9, 10, 11,
|
|
29
|
+
])
|
|
32
30
|
})
|
|
33
31
|
|
|
34
|
-
test('
|
|
35
|
-
const row = '---..--'
|
|
36
|
-
expect(seqPosToGlobalCol({ row, seqPos: 0 })).toBe(0)
|
|
37
|
-
expect(seqPosToGlobalCol({ row, seqPos: 1 })).toBe(7) // Past end
|
|
38
|
-
})
|
|
39
|
-
|
|
40
|
-
test('handles mixed gap characters (- and .)', () => {
|
|
41
|
-
const row = 'A-.G-C.'
|
|
32
|
+
test('counts both gap characters, - and .', () => {
|
|
42
33
|
// Global: A(0) -(1) .(2) G(3) -(4) C(5) .(6)
|
|
43
34
|
// SeqPos: A(0) G(1) C(2)
|
|
44
|
-
|
|
45
|
-
expect(seqPosToGlobalCol({ row, seqPos: 0 })).toBe(0) // A
|
|
46
|
-
expect(seqPosToGlobalCol({ row, seqPos: 1 })).toBe(3) // G
|
|
47
|
-
expect(seqPosToGlobalCol({ row, seqPos: 2 })).toBe(5) // C
|
|
48
|
-
expect(seqPosToGlobalCol({ row, seqPos: 3 })).toBe(7) // Past end
|
|
35
|
+
expect(colsOf('A-.G-C.', [0, 1, 2, 3])).toEqual([0, 3, 5, 7])
|
|
49
36
|
})
|
|
50
37
|
|
|
51
|
-
test('handles leading gaps', () => {
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
38
|
+
test('handles leading and trailing gaps', () => {
|
|
39
|
+
expect(colsOf('--ACG', [0, 1, 2])).toEqual([2, 3, 4])
|
|
40
|
+
expect(colsOf('ACG--', [0, 1, 2, 3])).toEqual([0, 1, 2, 5])
|
|
41
|
+
})
|
|
55
42
|
|
|
56
|
-
|
|
57
|
-
expect(
|
|
58
|
-
expect(seqPosToGlobalCol({ row, seqPos: 2 })).toBe(4) // G
|
|
43
|
+
test('an all-gap row reports position 0 at column 0', () => {
|
|
44
|
+
expect(colsOf('---..--', [0, 1])).toEqual([0, 7])
|
|
59
45
|
})
|
|
60
46
|
|
|
61
|
-
test('
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
// SeqPos: A(0) C(1) G(2)
|
|
47
|
+
test('an empty row reports column 0', () => {
|
|
48
|
+
expect(colsOf('', [0])).toEqual([0])
|
|
49
|
+
})
|
|
65
50
|
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
51
|
+
test('an unknown row reports column 0', () => {
|
|
52
|
+
const model = MSAModelF().create({
|
|
53
|
+
type: 'MsaView',
|
|
54
|
+
data: { msa: '>r\nACGT' },
|
|
55
|
+
})
|
|
56
|
+
model.setWidth(800)
|
|
57
|
+
expect(model.seqPosToGlobalCol('nope', 2)).toBe(0)
|
|
70
58
|
})
|
|
71
59
|
})
|
package/src/seqPosToGlobalCol.ts
CHANGED
|
@@ -1,25 +1,14 @@
|
|
|
1
|
-
import { isBlank } from './util.ts'
|
|
2
|
-
|
|
3
1
|
/**
|
|
4
|
-
*
|
|
5
|
-
*
|
|
2
|
+
* The global column of every ungapped sequence position in a row, i.e.
|
|
3
|
+
* index[seqPos] is the column holding the seqPos-th non-gap character.
|
|
6
4
|
*
|
|
7
|
-
*
|
|
8
|
-
*
|
|
9
|
-
*
|
|
5
|
+
* A row is indexed once and then answers seqPos -> column by lookup, which is
|
|
6
|
+
* what makes the domain overlay affordable: it resolves one position per feature
|
|
7
|
+
* per row on every redraw, and scanning the row for each would be quadratic.
|
|
10
8
|
*
|
|
11
9
|
* @example
|
|
12
10
|
* // Row: "A-TG-C" (A at 0, T at 2, G at 3, C at 5)
|
|
13
|
-
*
|
|
14
|
-
* seqPosToGlobalCol({ row: "A-TG-C", seqPos: 1 }) // → 2 (T)
|
|
15
|
-
* seqPosToGlobalCol({ row: "A-TG-C", seqPos: 2 }) // → 3 (G)
|
|
16
|
-
* seqPosToGlobalCol({ row: "A-TG-C", seqPos: 3 }) // → 5 (C)
|
|
17
|
-
*/
|
|
18
|
-
/**
|
|
19
|
-
* The global column of every ungapped sequence position in a row, i.e.
|
|
20
|
-
* index[seqPos] is the column holding the seqPos-th non-gap character. Amortizes
|
|
21
|
-
* repeated seqPosToGlobalCol lookups (the domain overlay does one per feature
|
|
22
|
-
* per row) into a single pass per row.
|
|
11
|
+
* buildSeqPosIndex('A-TG-C') // → [0, 2, 3, 5]
|
|
23
12
|
*/
|
|
24
13
|
export function buildSeqPosIndex(row: string) {
|
|
25
14
|
const index = new Int32Array(row.length)
|
|
@@ -32,27 +21,3 @@ export function buildSeqPosIndex(row: string) {
|
|
|
32
21
|
}
|
|
33
22
|
return index.subarray(0, n)
|
|
34
23
|
}
|
|
35
|
-
|
|
36
|
-
export function seqPosToGlobalCol({
|
|
37
|
-
row,
|
|
38
|
-
seqPos,
|
|
39
|
-
}: {
|
|
40
|
-
row: string
|
|
41
|
-
seqPos: number
|
|
42
|
-
}) {
|
|
43
|
-
let nonGapCount = 0
|
|
44
|
-
let globalCol = 0
|
|
45
|
-
// Find the seqPos-th non-gap character
|
|
46
|
-
while (globalCol < row.length) {
|
|
47
|
-
if (!isBlank(row[globalCol])) {
|
|
48
|
-
if (nonGapCount === seqPos) {
|
|
49
|
-
return globalCol
|
|
50
|
-
}
|
|
51
|
-
nonGapCount++
|
|
52
|
-
}
|
|
53
|
-
globalCol++
|
|
54
|
-
}
|
|
55
|
-
// If seqPos is 0 and we didn't find any non-gap character, return 0
|
|
56
|
-
// Otherwise return globalCol (which is row.length at this point)
|
|
57
|
-
return seqPos === 0 ? 0 : globalCol
|
|
58
|
-
}
|
|
@@ -74,3 +74,14 @@ test('inline gff survives the snapshot when no gffFilehandle can refetch it', ()
|
|
|
74
74
|
})
|
|
75
75
|
expect(getSnapshot(withFilehandle).data.gff).toBeUndefined()
|
|
76
76
|
})
|
|
77
|
+
|
|
78
|
+
// The legend floats over the top-right of the alignment, so collapsing it is a
|
|
79
|
+
// way of seeing the residues under it. As component state that choice was lost
|
|
80
|
+
// on every reload and could not be set by a session or a figure at all.
|
|
81
|
+
test('a collapsed domain legend survives into the snapshot', () => {
|
|
82
|
+
const model = MsaView.create({ type: 'MsaView' })
|
|
83
|
+
expect(getSnapshot(model).showDomainLegend).toBeUndefined()
|
|
84
|
+
model.setShowDomainLegend(false)
|
|
85
|
+
expect(getSnapshot(model).showDomainLegend).toBe(false)
|
|
86
|
+
expect(MsaView.create(getSnapshot(model)).showDomainLegend).toBe(false)
|
|
87
|
+
})
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
// @vitest-environment jsdom
|
|
2
|
+
//
|
|
3
|
+
// Assigning width/height to a canvas resets its bitmap, and React assigns them
|
|
4
|
+
// on every commit where they changed. A draw that ran during the mobx reaction
|
|
5
|
+
// -- before React commits -- is therefore wiped by the resize that follows it,
|
|
6
|
+
// which is what used to blank the tree on a tree-area drag and the consensus
|
|
7
|
+
// tracks on a vertical zoom. So the hook has to draw again after the resize.
|
|
8
|
+
import React, { act } from 'react'
|
|
9
|
+
|
|
10
|
+
import { observable, runInAction } from 'mobx'
|
|
11
|
+
import { observer } from 'mobx-react'
|
|
12
|
+
import { createRoot } from 'react-dom/client'
|
|
13
|
+
import { afterEach, beforeAll, beforeEach, expect, test } from 'vitest'
|
|
14
|
+
|
|
15
|
+
import { useCanvasAutorun } from './useCanvasAutorun.ts'
|
|
16
|
+
|
|
17
|
+
import type { Root } from 'react-dom/client'
|
|
18
|
+
|
|
19
|
+
Reflect.set(globalThis, 'IS_REACT_ACT_ENVIRONMENT', true)
|
|
20
|
+
|
|
21
|
+
beforeAll(() => {
|
|
22
|
+
HTMLCanvasElement.prototype.getContext = function (this: HTMLCanvasElement) {
|
|
23
|
+
return { canvas: this } as unknown as CanvasRenderingContext2D
|
|
24
|
+
} as unknown as typeof HTMLCanvasElement.prototype.getContext
|
|
25
|
+
})
|
|
26
|
+
|
|
27
|
+
const state = observable({ width: 100, color: 'red' })
|
|
28
|
+
// what each draw saw: the observables it renders from, and the size of the
|
|
29
|
+
// canvas it was drawing onto
|
|
30
|
+
let draws: { observed: number; color: string; canvas: number }[] = []
|
|
31
|
+
|
|
32
|
+
const Probe = observer(function () {
|
|
33
|
+
const ref = useCanvasAutorun({
|
|
34
|
+
draw: ctx => {
|
|
35
|
+
draws.push({
|
|
36
|
+
observed: state.width,
|
|
37
|
+
color: state.color,
|
|
38
|
+
canvas: ctx.canvas.width,
|
|
39
|
+
})
|
|
40
|
+
},
|
|
41
|
+
width: state.width,
|
|
42
|
+
height: 50,
|
|
43
|
+
deps: [],
|
|
44
|
+
})
|
|
45
|
+
return <canvas ref={ref} width={state.width} height={50} />
|
|
46
|
+
})
|
|
47
|
+
|
|
48
|
+
let container: HTMLElement
|
|
49
|
+
let root: Root
|
|
50
|
+
|
|
51
|
+
beforeEach(() => {
|
|
52
|
+
draws = []
|
|
53
|
+
runInAction(() => {
|
|
54
|
+
state.width = 100
|
|
55
|
+
state.color = 'red'
|
|
56
|
+
})
|
|
57
|
+
container = document.createElement('div')
|
|
58
|
+
document.body.appendChild(container)
|
|
59
|
+
root = createRoot(container)
|
|
60
|
+
act(() => {
|
|
61
|
+
root.render(<Probe />)
|
|
62
|
+
})
|
|
63
|
+
})
|
|
64
|
+
|
|
65
|
+
afterEach(() => {
|
|
66
|
+
act(() => {
|
|
67
|
+
root.unmount()
|
|
68
|
+
})
|
|
69
|
+
container.remove()
|
|
70
|
+
})
|
|
71
|
+
|
|
72
|
+
test('the first draw sees the canvas at its rendered size', () => {
|
|
73
|
+
expect(draws).toEqual([{ observed: 100, color: 'red', canvas: 100 }])
|
|
74
|
+
})
|
|
75
|
+
|
|
76
|
+
test('a resize is followed by a draw onto the resized canvas', () => {
|
|
77
|
+
act(() => {
|
|
78
|
+
runInAction(() => {
|
|
79
|
+
state.width = 300
|
|
80
|
+
})
|
|
81
|
+
})
|
|
82
|
+
|
|
83
|
+
expect(container.querySelector('canvas')!.width).toBe(300)
|
|
84
|
+
expect(draws.at(-1)).toEqual({ observed: 300, color: 'red', canvas: 300 })
|
|
85
|
+
})
|
|
86
|
+
|
|
87
|
+
test('a change that leaves the size alone draws exactly once', () => {
|
|
88
|
+
act(() => {
|
|
89
|
+
runInAction(() => {
|
|
90
|
+
state.color = 'blue'
|
|
91
|
+
})
|
|
92
|
+
})
|
|
93
|
+
|
|
94
|
+
expect(draws).toEqual([
|
|
95
|
+
{ observed: 100, color: 'red', canvas: 100 },
|
|
96
|
+
{ observed: 100, color: 'blue', canvas: 100 },
|
|
97
|
+
])
|
|
98
|
+
})
|
package/src/useCanvasAutorun.ts
CHANGED
|
@@ -4,18 +4,37 @@ import { autorun } from 'mobx'
|
|
|
4
4
|
|
|
5
5
|
import type React from 'react'
|
|
6
6
|
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
7
|
+
/**
|
|
8
|
+
* Wires a canvas ref to a mobx autorun: grabs the 2d context once the canvas
|
|
9
|
+
* mounts and re-runs `draw` whenever any observable it reads changes. Collapses
|
|
10
|
+
* the getContext/null-guard/autorun-dispose boilerplate repeated by every canvas
|
|
11
|
+
* block host.
|
|
12
|
+
*
|
|
13
|
+
* `width`/`height` are the canvas *backing store* size the caller renders onto
|
|
14
|
+
* the element (logical size times the high-res scale factor), and they are load
|
|
15
|
+
* bearing rather than informational. Assigning either resets the canvas bitmap,
|
|
16
|
+
* and React assigns them on every commit where they changed -- which lands
|
|
17
|
+
* *after* the mobx reaction that already redrew at the old size. Without a redraw
|
|
18
|
+
* on the far side of that commit the canvas is left blank: it is what blanked the
|
|
19
|
+
* tree on a tree-area resize and the consensus tracks on a vertical zoom.
|
|
20
|
+
*
|
|
21
|
+
* `deps` covers everything else the effect should re-subscribe on, matching a
|
|
22
|
+
* normal useEffect dep array.
|
|
23
|
+
*/
|
|
24
|
+
export function useCanvasAutorun({
|
|
25
|
+
draw,
|
|
26
|
+
width,
|
|
27
|
+
height,
|
|
28
|
+
deps,
|
|
29
|
+
}: {
|
|
30
|
+
draw: (ctx: CanvasRenderingContext2D) => void
|
|
31
|
+
width: number
|
|
32
|
+
height: number
|
|
33
|
+
deps: React.DependencyList
|
|
34
|
+
}) {
|
|
16
35
|
const ref = useRef<HTMLCanvasElement>(null)
|
|
17
36
|
// the autorun tracks its own observable reads, so `draw` is intentionally not
|
|
18
|
-
// a dependency; `deps`
|
|
37
|
+
// a dependency; `deps` plus the canvas size control when it re-subscribes
|
|
19
38
|
useEffect(() => {
|
|
20
39
|
const ctx = ref.current?.getContext('2d')
|
|
21
40
|
return ctx
|
|
@@ -24,6 +43,6 @@ export function useCanvasAutorun(
|
|
|
24
43
|
})
|
|
25
44
|
: undefined
|
|
26
45
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
27
|
-
}, deps)
|
|
46
|
+
}, [...deps, width, height])
|
|
28
47
|
return ref
|
|
29
48
|
}
|
package/src/version.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export const version = '5.
|
|
1
|
+
export const version = '5.10.0'
|