makethisbetter 1.5.4 → 1.7.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/README.md +10 -10
- package/dist/annotate/draw-note-bar.d.ts +29 -0
- package/dist/annotate/draw-note-bar.d.ts.map +1 -0
- package/dist/annotate/draw-note-bar.test.d.ts +2 -0
- package/dist/annotate/draw-note-bar.test.d.ts.map +1 -0
- package/dist/annotate/draw.d.ts +15 -9
- package/dist/annotate/draw.d.ts.map +1 -1
- package/dist/annotate/highlighter.d.ts +2 -0
- package/dist/annotate/highlighter.d.ts.map +1 -1
- package/dist/annotate/session.d.ts +16 -2
- package/dist/annotate/session.d.ts.map +1 -1
- package/dist/api/client.d.ts +8 -5
- package/dist/api/client.d.ts.map +1 -1
- package/dist/context/breadcrumbs.d.ts +26 -0
- package/dist/context/breadcrumbs.d.ts.map +1 -0
- package/dist/context/breadcrumbs.test.d.ts +2 -0
- package/dist/context/breadcrumbs.test.d.ts.map +1 -0
- package/dist/context/dom-utils.d.ts +3 -0
- package/dist/context/dom-utils.d.ts.map +1 -0
- package/dist/context/frustration.d.ts +0 -1
- package/dist/context/frustration.d.ts.map +1 -1
- package/dist/i18n/de.d.ts +24 -0
- package/dist/i18n/de.d.ts.map +1 -1
- package/dist/i18n/en.d.ts +24 -0
- package/dist/i18n/en.d.ts.map +1 -1
- package/dist/i18n/es.d.ts +24 -0
- package/dist/i18n/es.d.ts.map +1 -1
- package/dist/i18n/fr.d.ts +24 -0
- package/dist/i18n/fr.d.ts.map +1 -1
- package/dist/i18n/ja.d.ts +24 -0
- package/dist/i18n/ja.d.ts.map +1 -1
- package/dist/i18n/ko.d.ts +24 -0
- package/dist/i18n/ko.d.ts.map +1 -1
- package/dist/i18n/zh-CN.d.ts +24 -0
- package/dist/i18n/zh-CN.d.ts.map +1 -1
- package/dist/makethisbetter.cjs +50 -25
- package/dist/makethisbetter.cjs.map +1 -1
- package/dist/makethisbetter.esm.js +1132 -590
- package/dist/makethisbetter.esm.js.map +1 -1
- package/dist/makethisbetter.js +49 -24
- package/dist/makethisbetter.js.map +1 -1
- package/dist/popup/clarify.d.ts +28 -7
- package/dist/popup/clarify.d.ts.map +1 -1
- package/dist/popup/comment.d.ts +1 -0
- package/dist/popup/comment.d.ts.map +1 -1
- package/dist/record/replay.d.ts +7 -0
- package/dist/record/replay.d.ts.map +1 -0
- package/dist/screenshot/capture.d.ts +1 -0
- package/dist/screenshot/capture.d.ts.map +1 -1
- package/dist/styles.d.ts +1 -0
- package/dist/styles.d.ts.map +1 -1
- package/dist/types.d.ts +13 -0
- package/dist/types.d.ts.map +1 -1
- package/dist/widget/controller.d.ts +18 -0
- package/dist/widget/controller.d.ts.map +1 -1
- package/dist/widget/dim-overlay.d.ts +8 -0
- package/dist/widget/dim-overlay.d.ts.map +1 -0
- package/dist/widget/dim-overlay.test.d.ts +2 -0
- package/dist/widget/dim-overlay.test.d.ts.map +1 -0
- package/dist/widget/payload.d.ts +2 -2
- package/dist/widget/payload.d.ts.map +1 -1
- package/dist/widget/tab.d.ts +3 -0
- package/dist/widget/tab.d.ts.map +1 -1
- package/package.json +1 -1
- package/src/annotate/draw-note-bar.test.ts +90 -0
- package/src/annotate/draw-note-bar.ts +105 -0
- package/src/annotate/draw.test.ts +74 -84
- package/src/annotate/draw.ts +108 -65
- package/src/annotate/highlighter.test.ts +24 -1
- package/src/annotate/highlighter.ts +16 -2
- package/src/annotate/session.test.ts +112 -96
- package/src/annotate/session.ts +54 -10
- package/src/api/client.test.ts +69 -30
- package/src/api/client.ts +68 -20
- package/src/context/breadcrumbs.test.ts +154 -0
- package/src/context/breadcrumbs.ts +157 -0
- package/src/context/dom-utils.ts +16 -0
- package/src/context/frustration.ts +4 -8
- package/src/i18n/de.ts +34 -11
- package/src/i18n/en.ts +33 -10
- package/src/i18n/es.ts +34 -11
- package/src/i18n/fr.ts +33 -10
- package/src/i18n/index.test.ts +12 -0
- package/src/i18n/ja.ts +32 -9
- package/src/i18n/ko.ts +34 -11
- package/src/i18n/zh-CN.ts +35 -12
- package/src/popup/clarify.test.ts +132 -40
- package/src/popup/clarify.ts +154 -42
- package/src/popup/comment.test.ts +57 -0
- package/src/popup/comment.ts +15 -9
- package/src/record/replay.ts +115 -0
- package/src/record/session.test.ts +8 -1
- package/src/record/session.ts +1 -1
- package/src/screenshot/capture.test.ts +51 -23
- package/src/screenshot/capture.ts +101 -55
- package/src/styles/widget.css +347 -1
- package/src/styles.ts +2 -0
- package/src/types.ts +15 -0
- package/src/widget/controller.test.ts +476 -65
- package/src/widget/controller.ts +222 -40
- package/src/widget/dim-overlay.test.ts +27 -0
- package/src/widget/dim-overlay.ts +20 -0
- package/src/widget/payload.test.ts +26 -1
- package/src/widget/payload.ts +3 -1
- package/src/widget/pet.test.ts +2 -2
- package/src/widget/tab.test.ts +15 -0
- package/src/widget/tab.ts +11 -3
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import { afterEach, describe, expect, it, vi } from 'vitest'
|
|
2
2
|
import { toJpeg } from 'html-to-image'
|
|
3
|
-
import { captureScreenshot } from './capture'
|
|
4
|
-
import type { Annotation } from '../types'
|
|
3
|
+
import { captureScreenshot, parsePathPolylines } from './capture'
|
|
5
4
|
|
|
6
5
|
vi.mock('html-to-image', () => ({
|
|
7
6
|
toJpeg: vi.fn(async () => 'data:image/jpeg;base64,/9j/AA=='),
|
|
@@ -9,10 +8,19 @@ vi.mock('html-to-image', () => ({
|
|
|
9
8
|
|
|
10
9
|
const toJpegMock = vi.mocked(toJpeg)
|
|
11
10
|
|
|
11
|
+
class ImmediatelyFailingImage {
|
|
12
|
+
onload: (() => void) | null = null
|
|
13
|
+
onerror: (() => void) | null = null
|
|
14
|
+
set src(_value: string) {
|
|
15
|
+
queueMicrotask(() => this.onerror?.())
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
|
|
12
19
|
describe('captureScreenshot', () => {
|
|
13
20
|
afterEach(() => {
|
|
14
21
|
document.body.innerHTML = ''
|
|
15
22
|
toJpegMock.mockClear()
|
|
23
|
+
vi.unstubAllGlobals()
|
|
16
24
|
})
|
|
17
25
|
|
|
18
26
|
it('captures a compressed jpeg blob', async () => {
|
|
@@ -25,46 +33,66 @@ describe('captureScreenshot', () => {
|
|
|
25
33
|
expect.objectContaining({
|
|
26
34
|
quality: 0.85,
|
|
27
35
|
pixelRatio: 1,
|
|
28
|
-
|
|
36
|
+
skipFonts: true,
|
|
29
37
|
skipAutoScale: false,
|
|
30
38
|
}),
|
|
31
39
|
)
|
|
32
40
|
})
|
|
33
41
|
|
|
34
|
-
it('
|
|
35
|
-
let
|
|
42
|
+
it('does not mount anything into the live page while capturing', async () => {
|
|
43
|
+
let bodyChildrenDuringCapture = -1
|
|
44
|
+
document.body.innerHTML = '<main>content</main>'
|
|
36
45
|
toJpegMock.mockImplementationOnce(async () => {
|
|
37
|
-
|
|
46
|
+
bodyChildrenDuringCapture = document.body.childElementCount
|
|
38
47
|
return 'data:image/jpeg;base64,/9j/AA=='
|
|
39
48
|
})
|
|
49
|
+
vi.stubGlobal('Image', ImmediatelyFailingImage)
|
|
40
50
|
|
|
41
|
-
|
|
42
|
-
await captureScreenshot([annotation])
|
|
51
|
+
await captureScreenshot([{ type: 'draw', x: 1, y: 2, drawPath: 'M 0 0 L 5 5' }])
|
|
43
52
|
|
|
44
|
-
expect(
|
|
45
|
-
expect(document.
|
|
53
|
+
expect(bodyChildrenDuringCapture).toBe(1)
|
|
54
|
+
expect(document.body.childElementCount).toBe(1)
|
|
46
55
|
})
|
|
47
56
|
|
|
48
|
-
it('
|
|
49
|
-
|
|
50
|
-
toJpegMock.mockImplementationOnce(async () => {
|
|
51
|
-
drawPath = document.querySelector('[data-mtb-capture-annotations] path')?.getAttribute('d') ?? null
|
|
52
|
-
return 'data:image/jpeg;base64,/9j/AA=='
|
|
53
|
-
})
|
|
57
|
+
it('falls back to the plain screenshot when the bitmap cannot be loaded for baking', async () => {
|
|
58
|
+
vi.stubGlobal('Image', ImmediatelyFailingImage)
|
|
54
59
|
|
|
55
|
-
const
|
|
56
|
-
await captureScreenshot([annotation])
|
|
60
|
+
const blob = await captureScreenshot([{ type: 'pin', x: 10, y: 20 }])
|
|
57
61
|
|
|
58
|
-
expect(
|
|
62
|
+
expect(blob).toBeInstanceOf(Blob)
|
|
63
|
+
expect(blob?.type).toBe('image/jpeg')
|
|
59
64
|
})
|
|
60
65
|
|
|
61
|
-
it('returns null
|
|
66
|
+
it('returns null when the capture itself fails', async () => {
|
|
62
67
|
toJpegMock.mockRejectedValueOnce(new Error('capture failed'))
|
|
63
68
|
|
|
64
|
-
const
|
|
65
|
-
const blob = await captureScreenshot([annotation])
|
|
69
|
+
const blob = await captureScreenshot()
|
|
66
70
|
|
|
67
71
|
expect(blob).toBeNull()
|
|
68
|
-
|
|
72
|
+
})
|
|
73
|
+
})
|
|
74
|
+
|
|
75
|
+
describe('parsePathPolylines', () => {
|
|
76
|
+
it('parses a single M/L polyline', () => {
|
|
77
|
+
expect(parsePathPolylines('M 10 20 L 30 40 L 50 60')).toEqual([
|
|
78
|
+
[[10, 20], [30, 40], [50, 60]],
|
|
79
|
+
])
|
|
80
|
+
})
|
|
81
|
+
|
|
82
|
+
it('splits multiple strokes on M commands', () => {
|
|
83
|
+
expect(parsePathPolylines('M 1 2 L 3 4 M 5 6 L 7 8')).toEqual([
|
|
84
|
+
[[1, 2], [3, 4]],
|
|
85
|
+
[[5, 6], [7, 8]],
|
|
86
|
+
])
|
|
87
|
+
})
|
|
88
|
+
|
|
89
|
+
it('handles decimals and negatives and ignores junk', () => {
|
|
90
|
+
expect(parsePathPolylines('M 1.5 -2.25 L -3 4.75 Z garbage')).toEqual([
|
|
91
|
+
[[1.5, -2.25], [-3, 4.75]],
|
|
92
|
+
])
|
|
93
|
+
})
|
|
94
|
+
|
|
95
|
+
it('returns no polylines for an empty path', () => {
|
|
96
|
+
expect(parsePathPolylines('')).toEqual([])
|
|
69
97
|
})
|
|
70
98
|
})
|
|
@@ -1,93 +1,139 @@
|
|
|
1
1
|
import { toJpeg } from 'html-to-image'
|
|
2
2
|
import type { Annotation } from '../types'
|
|
3
|
+
import { BRAND_COLOR, FONT_STACK } from '../styles'
|
|
3
4
|
|
|
4
5
|
// Capture at CSS pixels (pixelRatio 1) as JPEG to keep the upload small: on
|
|
5
6
|
// retina screens the default devicePixelRatio would quadruple the pixel count,
|
|
6
7
|
// and the model downscales past ~1568px anyway.
|
|
8
|
+
//
|
|
9
|
+
// Annotations are baked onto the captured bitmap with canvas 2D afterwards —
|
|
10
|
+
// never mounted into the live page — so the capture leaves the page visually
|
|
11
|
+
// untouched no matter how long the DOM snapshot takes.
|
|
7
12
|
export async function captureScreenshot(annotations: Annotation[] = []): Promise<Blob | null> {
|
|
8
|
-
const annotationLayer = mountCaptureAnnotations(annotations)
|
|
9
|
-
|
|
10
13
|
try {
|
|
11
14
|
const dataUrl = await toJpeg(document.body, {
|
|
12
15
|
quality: 0.85,
|
|
13
16
|
pixelRatio: 1,
|
|
14
17
|
backgroundColor: '#ffffff',
|
|
15
|
-
|
|
18
|
+
skipFonts: true,
|
|
16
19
|
skipAutoScale: false,
|
|
17
20
|
filter: (node) => {
|
|
18
21
|
if (node instanceof HTMLElement && node.id === 'mtb-widget-host') return false
|
|
19
22
|
return true
|
|
20
23
|
},
|
|
21
24
|
})
|
|
25
|
+
if (annotations.length > 0) {
|
|
26
|
+
const annotated = await bakeAnnotations(dataUrl, annotations)
|
|
27
|
+
if (annotated) return annotated
|
|
28
|
+
}
|
|
22
29
|
return dataUrlToBlob(dataUrl)
|
|
23
30
|
} catch {
|
|
24
31
|
return null
|
|
25
|
-
} finally {
|
|
26
|
-
annotationLayer?.remove()
|
|
27
32
|
}
|
|
28
33
|
}
|
|
29
34
|
|
|
30
|
-
function
|
|
31
|
-
|
|
35
|
+
async function bakeAnnotations(dataUrl: string, annotations: Annotation[]): Promise<Blob | null> {
|
|
36
|
+
const canvas = document.createElement('canvas')
|
|
37
|
+
const ctx = canvas.getContext('2d')
|
|
38
|
+
if (!ctx) return null
|
|
39
|
+
|
|
40
|
+
const image = await loadImage(dataUrl)
|
|
41
|
+
if (!image) return null
|
|
32
42
|
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
43
|
+
canvas.width = image.naturalWidth || window.innerWidth
|
|
44
|
+
canvas.height = image.naturalHeight || window.innerHeight
|
|
45
|
+
ctx.drawImage(image, 0, 0)
|
|
46
|
+
const scaleX = canvas.width / window.innerWidth
|
|
47
|
+
const scaleY = canvas.height / window.innerHeight
|
|
37
48
|
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
49
|
+
let pinIndex = 0
|
|
50
|
+
for (const annotation of annotations) {
|
|
51
|
+
if (annotation.type === 'draw' && annotation.drawPath) {
|
|
52
|
+
drawStroke(ctx, annotation.drawPath, scaleX, scaleY)
|
|
53
|
+
} else if (annotation.type === 'pin') {
|
|
54
|
+
drawPin(ctx, annotation.x * scaleX, annotation.y * scaleY, ++pinIndex)
|
|
43
55
|
}
|
|
44
56
|
}
|
|
45
57
|
|
|
46
|
-
|
|
47
|
-
return layer
|
|
58
|
+
return canvasToJpegBlob(canvas)
|
|
48
59
|
}
|
|
49
60
|
|
|
50
|
-
function
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
'box-shadow:0 2px 8px rgba(5,150,105,0.45)',
|
|
68
|
-
'transform:translate(-50%,-50%)',
|
|
69
|
-
].join(';')
|
|
70
|
-
return pin
|
|
61
|
+
function drawStroke(ctx: CanvasRenderingContext2D, drawPath: string, scaleX: number, scaleY: number): void {
|
|
62
|
+
ctx.save()
|
|
63
|
+
ctx.strokeStyle = BRAND_COLOR
|
|
64
|
+
ctx.lineWidth = 3.5
|
|
65
|
+
ctx.lineCap = 'round'
|
|
66
|
+
ctx.lineJoin = 'round'
|
|
67
|
+
ctx.shadowColor = 'rgba(5,150,105,0.35)'
|
|
68
|
+
ctx.shadowOffsetY = 1
|
|
69
|
+
ctx.shadowBlur = 2
|
|
70
|
+
for (const points of parsePathPolylines(drawPath)) {
|
|
71
|
+
if (points.length === 0) continue
|
|
72
|
+
ctx.beginPath()
|
|
73
|
+
ctx.moveTo(points[0][0] * scaleX, points[0][1] * scaleY)
|
|
74
|
+
for (let i = 1; i < points.length; i++) ctx.lineTo(points[i][0] * scaleX, points[i][1] * scaleY)
|
|
75
|
+
ctx.stroke()
|
|
76
|
+
}
|
|
77
|
+
ctx.restore()
|
|
71
78
|
}
|
|
72
79
|
|
|
73
|
-
function
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
80
|
+
function drawPin(ctx: CanvasRenderingContext2D, x: number, y: number, index: number): void {
|
|
81
|
+
ctx.save()
|
|
82
|
+
ctx.shadowColor = 'rgba(5,150,105,0.45)'
|
|
83
|
+
ctx.shadowOffsetY = 2
|
|
84
|
+
ctx.shadowBlur = 8
|
|
85
|
+
ctx.fillStyle = BRAND_COLOR
|
|
86
|
+
ctx.beginPath()
|
|
87
|
+
ctx.arc(x, y, 11, 0, Math.PI * 2)
|
|
88
|
+
ctx.fill()
|
|
89
|
+
ctx.shadowColor = 'transparent'
|
|
90
|
+
ctx.lineWidth = 2
|
|
91
|
+
ctx.strokeStyle = '#ffffff'
|
|
92
|
+
ctx.stroke()
|
|
93
|
+
ctx.fillStyle = '#ffffff'
|
|
94
|
+
ctx.font = `700 10px ${FONT_STACK}`
|
|
95
|
+
ctx.textAlign = 'center'
|
|
96
|
+
ctx.textBaseline = 'middle'
|
|
97
|
+
ctx.fillText(String(index), x, y)
|
|
98
|
+
ctx.restore()
|
|
99
|
+
}
|
|
79
100
|
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
101
|
+
// The draw layer only ever emits M/L polyline commands (see buildPathD in
|
|
102
|
+
// annotate/draw.ts); anything else in the string is ignored.
|
|
103
|
+
export function parsePathPolylines(drawPath: string): Array<Array<[number, number]>> {
|
|
104
|
+
const polylines: Array<Array<[number, number]>> = []
|
|
105
|
+
let current: Array<[number, number]> | null = null
|
|
106
|
+
const tokens = drawPath.match(/[ML]\s*[-\d.]+\s+[-\d.]+/g) ?? []
|
|
107
|
+
for (const token of tokens) {
|
|
108
|
+
const [x, y] = token.slice(1).trim().split(/\s+/).map(Number)
|
|
109
|
+
if (Number.isNaN(x) || Number.isNaN(y)) continue
|
|
110
|
+
if (token[0] === 'M') {
|
|
111
|
+
current = [[x, y]]
|
|
112
|
+
polylines.push(current)
|
|
113
|
+
} else if (current) {
|
|
114
|
+
current.push([x, y])
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
return polylines
|
|
118
|
+
}
|
|
89
119
|
|
|
90
|
-
|
|
120
|
+
function loadImage(dataUrl: string): Promise<HTMLImageElement | null> {
|
|
121
|
+
return new Promise(resolve => {
|
|
122
|
+
const image = new Image()
|
|
123
|
+
image.onload = () => resolve(image)
|
|
124
|
+
image.onerror = () => resolve(null)
|
|
125
|
+
image.src = dataUrl
|
|
126
|
+
})
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
function canvasToJpegBlob(canvas: HTMLCanvasElement): Promise<Blob | null> {
|
|
130
|
+
return new Promise(resolve => {
|
|
131
|
+
try {
|
|
132
|
+
canvas.toBlob(blob => resolve(blob), 'image/jpeg', 0.85)
|
|
133
|
+
} catch {
|
|
134
|
+
resolve(null)
|
|
135
|
+
}
|
|
136
|
+
})
|
|
91
137
|
}
|
|
92
138
|
|
|
93
139
|
function dataUrlToBlob(dataUrl: string): Blob {
|