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.
Files changed (107) hide show
  1. package/README.md +10 -10
  2. package/dist/annotate/draw-note-bar.d.ts +29 -0
  3. package/dist/annotate/draw-note-bar.d.ts.map +1 -0
  4. package/dist/annotate/draw-note-bar.test.d.ts +2 -0
  5. package/dist/annotate/draw-note-bar.test.d.ts.map +1 -0
  6. package/dist/annotate/draw.d.ts +15 -9
  7. package/dist/annotate/draw.d.ts.map +1 -1
  8. package/dist/annotate/highlighter.d.ts +2 -0
  9. package/dist/annotate/highlighter.d.ts.map +1 -1
  10. package/dist/annotate/session.d.ts +16 -2
  11. package/dist/annotate/session.d.ts.map +1 -1
  12. package/dist/api/client.d.ts +8 -5
  13. package/dist/api/client.d.ts.map +1 -1
  14. package/dist/context/breadcrumbs.d.ts +26 -0
  15. package/dist/context/breadcrumbs.d.ts.map +1 -0
  16. package/dist/context/breadcrumbs.test.d.ts +2 -0
  17. package/dist/context/breadcrumbs.test.d.ts.map +1 -0
  18. package/dist/context/dom-utils.d.ts +3 -0
  19. package/dist/context/dom-utils.d.ts.map +1 -0
  20. package/dist/context/frustration.d.ts +0 -1
  21. package/dist/context/frustration.d.ts.map +1 -1
  22. package/dist/i18n/de.d.ts +24 -0
  23. package/dist/i18n/de.d.ts.map +1 -1
  24. package/dist/i18n/en.d.ts +24 -0
  25. package/dist/i18n/en.d.ts.map +1 -1
  26. package/dist/i18n/es.d.ts +24 -0
  27. package/dist/i18n/es.d.ts.map +1 -1
  28. package/dist/i18n/fr.d.ts +24 -0
  29. package/dist/i18n/fr.d.ts.map +1 -1
  30. package/dist/i18n/ja.d.ts +24 -0
  31. package/dist/i18n/ja.d.ts.map +1 -1
  32. package/dist/i18n/ko.d.ts +24 -0
  33. package/dist/i18n/ko.d.ts.map +1 -1
  34. package/dist/i18n/zh-CN.d.ts +24 -0
  35. package/dist/i18n/zh-CN.d.ts.map +1 -1
  36. package/dist/makethisbetter.cjs +50 -25
  37. package/dist/makethisbetter.cjs.map +1 -1
  38. package/dist/makethisbetter.esm.js +1132 -590
  39. package/dist/makethisbetter.esm.js.map +1 -1
  40. package/dist/makethisbetter.js +49 -24
  41. package/dist/makethisbetter.js.map +1 -1
  42. package/dist/popup/clarify.d.ts +28 -7
  43. package/dist/popup/clarify.d.ts.map +1 -1
  44. package/dist/popup/comment.d.ts +1 -0
  45. package/dist/popup/comment.d.ts.map +1 -1
  46. package/dist/record/replay.d.ts +7 -0
  47. package/dist/record/replay.d.ts.map +1 -0
  48. package/dist/screenshot/capture.d.ts +1 -0
  49. package/dist/screenshot/capture.d.ts.map +1 -1
  50. package/dist/styles.d.ts +1 -0
  51. package/dist/styles.d.ts.map +1 -1
  52. package/dist/types.d.ts +13 -0
  53. package/dist/types.d.ts.map +1 -1
  54. package/dist/widget/controller.d.ts +18 -0
  55. package/dist/widget/controller.d.ts.map +1 -1
  56. package/dist/widget/dim-overlay.d.ts +8 -0
  57. package/dist/widget/dim-overlay.d.ts.map +1 -0
  58. package/dist/widget/dim-overlay.test.d.ts +2 -0
  59. package/dist/widget/dim-overlay.test.d.ts.map +1 -0
  60. package/dist/widget/payload.d.ts +2 -2
  61. package/dist/widget/payload.d.ts.map +1 -1
  62. package/dist/widget/tab.d.ts +3 -0
  63. package/dist/widget/tab.d.ts.map +1 -1
  64. package/package.json +1 -1
  65. package/src/annotate/draw-note-bar.test.ts +90 -0
  66. package/src/annotate/draw-note-bar.ts +105 -0
  67. package/src/annotate/draw.test.ts +74 -84
  68. package/src/annotate/draw.ts +108 -65
  69. package/src/annotate/highlighter.test.ts +24 -1
  70. package/src/annotate/highlighter.ts +16 -2
  71. package/src/annotate/session.test.ts +112 -96
  72. package/src/annotate/session.ts +54 -10
  73. package/src/api/client.test.ts +69 -30
  74. package/src/api/client.ts +68 -20
  75. package/src/context/breadcrumbs.test.ts +154 -0
  76. package/src/context/breadcrumbs.ts +157 -0
  77. package/src/context/dom-utils.ts +16 -0
  78. package/src/context/frustration.ts +4 -8
  79. package/src/i18n/de.ts +34 -11
  80. package/src/i18n/en.ts +33 -10
  81. package/src/i18n/es.ts +34 -11
  82. package/src/i18n/fr.ts +33 -10
  83. package/src/i18n/index.test.ts +12 -0
  84. package/src/i18n/ja.ts +32 -9
  85. package/src/i18n/ko.ts +34 -11
  86. package/src/i18n/zh-CN.ts +35 -12
  87. package/src/popup/clarify.test.ts +132 -40
  88. package/src/popup/clarify.ts +154 -42
  89. package/src/popup/comment.test.ts +57 -0
  90. package/src/popup/comment.ts +15 -9
  91. package/src/record/replay.ts +115 -0
  92. package/src/record/session.test.ts +8 -1
  93. package/src/record/session.ts +1 -1
  94. package/src/screenshot/capture.test.ts +51 -23
  95. package/src/screenshot/capture.ts +101 -55
  96. package/src/styles/widget.css +347 -1
  97. package/src/styles.ts +2 -0
  98. package/src/types.ts +15 -0
  99. package/src/widget/controller.test.ts +476 -65
  100. package/src/widget/controller.ts +222 -40
  101. package/src/widget/dim-overlay.test.ts +27 -0
  102. package/src/widget/dim-overlay.ts +20 -0
  103. package/src/widget/payload.test.ts +26 -1
  104. package/src/widget/payload.ts +3 -1
  105. package/src/widget/pet.test.ts +2 -2
  106. package/src/widget/tab.test.ts +15 -0
  107. 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
- cacheBust: true,
36
+ skipFonts: true,
29
37
  skipAutoScale: false,
30
38
  }),
31
39
  )
32
40
  })
33
41
 
34
- it('mounts pin annotations during capture and removes them after capture', async () => {
35
- let captureText = ''
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
- captureText = document.querySelector<HTMLElement>('[data-mtb-capture-annotations]')?.textContent ?? ''
46
+ bodyChildrenDuringCapture = document.body.childElementCount
38
47
  return 'data:image/jpeg;base64,/9j/AA=='
39
48
  })
49
+ vi.stubGlobal('Image', ImmediatelyFailingImage)
40
50
 
41
- const annotation: Annotation = { type: 'pin', x: 40, y: 60 }
42
- await captureScreenshot([annotation])
51
+ await captureScreenshot([{ type: 'draw', x: 1, y: 2, drawPath: 'M 0 0 L 5 5' }])
43
52
 
44
- expect(captureText).toContain('1')
45
- expect(document.querySelector('[data-mtb-capture-annotations]')).toBeNull()
53
+ expect(bodyChildrenDuringCapture).toBe(1)
54
+ expect(document.body.childElementCount).toBe(1)
46
55
  })
47
56
 
48
- it('mounts draw annotations during capture', async () => {
49
- let drawPath: string | null = null
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 annotation: Annotation = { type: 'draw', x: 50, y: 50, drawPath: 'M 10 10 L 90 90' }
56
- await captureScreenshot([annotation])
60
+ const blob = await captureScreenshot([{ type: 'pin', x: 10, y: 20 }])
57
61
 
58
- expect(drawPath).toBe('M 10 10 L 90 90')
62
+ expect(blob).toBeInstanceOf(Blob)
63
+ expect(blob?.type).toBe('image/jpeg')
59
64
  })
60
65
 
61
- it('returns null and cleans up if capture fails', async () => {
66
+ it('returns null when the capture itself fails', async () => {
62
67
  toJpegMock.mockRejectedValueOnce(new Error('capture failed'))
63
68
 
64
- const annotation: Annotation = { type: 'pin', x: 40, y: 60 }
65
- const blob = await captureScreenshot([annotation])
69
+ const blob = await captureScreenshot()
66
70
 
67
71
  expect(blob).toBeNull()
68
- expect(document.querySelector('[data-mtb-capture-annotations]')).toBeNull()
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
- cacheBust: true,
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 mountCaptureAnnotations(annotations: Annotation[]): HTMLDivElement | null {
31
- if (annotations.length === 0) return null
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
- const layer = document.createElement('div')
34
- layer.setAttribute('data-mtb-capture-annotations', 'true')
35
- layer.setAttribute('aria-hidden', 'true')
36
- layer.style.cssText = 'position:fixed;inset:0;pointer-events:none;z-index:2147483645;'
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
- for (const [index, annotation] of annotations.entries()) {
39
- if (annotation.type === 'pin') {
40
- layer.appendChild(buildCapturePin(annotation, index + 1))
41
- } else if (annotation.type === 'draw' && annotation.drawPath) {
42
- layer.appendChild(buildCaptureDraw(annotation.drawPath))
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
- document.body.appendChild(layer)
47
- return layer
58
+ return canvasToJpegBlob(canvas)
48
59
  }
49
60
 
50
- function buildCapturePin(annotation: Annotation, index: number): HTMLDivElement {
51
- const pin = document.createElement('div')
52
- pin.textContent = String(index)
53
- pin.style.cssText = [
54
- 'position:fixed',
55
- `left:${annotation.x}px`,
56
- `top:${annotation.y}px`,
57
- 'width:22px',
58
- 'height:22px',
59
- 'border-radius:50% 50% 50% 2px',
60
- 'background:#059669',
61
- 'border:2px solid #fff',
62
- 'display:flex',
63
- 'align-items:center',
64
- 'justify-content:center',
65
- 'font:700 10px -apple-system,BlinkMacSystemFont,"Segoe UI",Roboto,sans-serif',
66
- 'color:#fff',
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 buildCaptureDraw(drawPath: string): SVGSVGElement {
74
- const svg = document.createElementNS('http://www.w3.org/2000/svg', 'svg')
75
- svg.setAttribute('width', '100%')
76
- svg.setAttribute('height', '100%')
77
- svg.setAttribute('viewBox', `0 0 ${window.innerWidth} ${window.innerHeight}`)
78
- svg.style.cssText = 'position:fixed;inset:0;width:100%;height:100%;overflow:visible;'
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
- const path = document.createElementNS('http://www.w3.org/2000/svg', 'path')
81
- path.setAttribute('d', drawPath)
82
- path.setAttribute('fill', 'none')
83
- path.setAttribute('stroke', '#059669')
84
- path.setAttribute('stroke-width', '3.5')
85
- path.setAttribute('stroke-linecap', 'round')
86
- path.setAttribute('stroke-linejoin', 'round')
87
- path.setAttribute('filter', 'drop-shadow(0 1px 2px rgba(5,150,105,0.35))')
88
- svg.appendChild(path)
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
- return svg
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 {