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,4 +1,4 @@
1
- import { afterEach, describe, it, expect, vi } from 'vitest'
1
+ import { afterEach, describe, it, expect } from 'vitest'
2
2
  import { ShadowContainer } from '../widget/shadow'
3
3
  import { DrawLayer } from './draw'
4
4
 
@@ -6,13 +6,17 @@ const SVG_SEL = 'svg[class="mtb-draw-svg"]'
6
6
 
7
7
  describe('DrawLayer', () => {
8
8
  let shadow: ShadowContainer
9
- let onDrawComplete: ReturnType<typeof vi.fn>
10
9
  let draw: DrawLayer
11
10
 
12
11
  function setup() {
13
12
  shadow = new ShadowContainer()
14
- onDrawComplete = vi.fn()
15
- draw = new DrawLayer(shadow, onDrawComplete)
13
+ draw = new DrawLayer(shadow)
14
+ }
15
+
16
+ function stroke(points: [number, number][]): void {
17
+ draw.startDraw(points[0][0], points[0][1])
18
+ for (let i = 1; i < points.length; i++) draw.continueDraw(points[i][0], points[i][1])
19
+ draw.commitStroke()
16
20
  }
17
21
 
18
22
  afterEach(() => {
@@ -30,56 +34,33 @@ describe('DrawLayer', () => {
30
34
  })
31
35
  })
32
36
 
33
- describe('startDraw', () => {
34
- it('creates a path element in the SVG', () => {
35
- setup()
36
- draw.startDraw(10, 20)
37
- const path = shadow.root.querySelector(`${SVG_SEL} path`)
38
- expect(path).toBeTruthy()
39
- expect(path!.tagName).toBe('path')
40
- })
41
-
42
- it('sets isCurrentlyDrawing to true', () => {
37
+ describe('startDraw / continueDraw', () => {
38
+ it('creates a path and tracks the current point count', () => {
43
39
  setup()
44
- expect(draw.isCurrentlyDrawing()).toBe(false)
45
40
  draw.startDraw(10, 20)
46
41
  expect(draw.isCurrentlyDrawing()).toBe(true)
47
- })
48
-
49
- it('initializes point count to 1', () => {
50
- setup()
51
- draw.startDraw(10, 20)
52
- expect(draw.getPointCount()).toBe(1)
53
- })
54
- })
55
-
56
- describe('continueDraw', () => {
57
- it('adds points and updates the path d attribute', () => {
58
- setup()
59
- draw.startDraw(10, 20)
42
+ expect(draw.getCurrentPointCount()).toBe(1)
60
43
  draw.continueDraw(30, 40)
61
44
  const path = shadow.root.querySelector(`${SVG_SEL} path`)!
62
45
  expect(path.getAttribute('d')).toContain('M 10 20')
63
46
  expect(path.getAttribute('d')).toContain('L 30 40')
64
- expect(draw.getPointCount()).toBe(2)
47
+ expect(draw.getCurrentPointCount()).toBe(2)
65
48
  })
66
49
 
67
- it('does nothing when not drawing', () => {
50
+ it('continueDraw does nothing when not drawing', () => {
68
51
  setup()
69
52
  draw.continueDraw(30, 40)
70
- expect(draw.getPointCount()).toBe(0)
53
+ expect(draw.getCurrentPointCount()).toBe(0)
71
54
  })
72
55
  })
73
56
 
74
- describe('endDraw', () => {
75
- it('calls onDrawComplete with a draw annotation when enough points', () => {
57
+ describe('commitStroke', () => {
58
+ it('commits the current stroke and makes it available for the annotation', () => {
76
59
  setup()
77
- draw.startDraw(0, 0)
78
- draw.continueDraw(10, 10)
79
- draw.endDraw()
80
-
81
- expect(onDrawComplete).toHaveBeenCalledOnce()
82
- const annotation = onDrawComplete.mock.calls[0][0]
60
+ stroke([[0, 0], [10, 10]])
61
+ expect(draw.isCurrentlyDrawing()).toBe(false)
62
+ expect(draw.hasStrokes()).toBe(true)
63
+ const annotation = draw.getAnnotation()!
83
64
  expect(annotation.type).toBe('draw')
84
65
  expect(annotation.drawPath).toContain('M 0 0')
85
66
  expect(annotation.drawPath).toContain('L 10 10')
@@ -87,59 +68,83 @@ describe('DrawLayer', () => {
87
68
  expect(annotation.y).toBe(5)
88
69
  })
89
70
 
90
- it('removes the path and does not call callback with fewer than 2 points', () => {
71
+ it('returns false when not drawing', () => {
91
72
  setup()
92
- draw.startDraw(10, 20)
93
- draw.endDraw()
73
+ expect(draw.commitStroke()).toBe(false)
74
+ })
75
+ })
94
76
 
95
- expect(onDrawComplete).not.toHaveBeenCalled()
96
- expect(draw.isCurrentlyDrawing()).toBe(false)
77
+ describe('undo / redo', () => {
78
+ it('undo removes the last committed stroke and redo restores it', () => {
79
+ setup()
80
+ stroke([[0, 0], [10, 10]])
81
+ stroke([[20, 20], [30, 30]])
82
+ expect(shadow.root.querySelectorAll(`${SVG_SEL} path`).length).toBe(2)
83
+
84
+ expect(draw.undo()).toBe(true)
85
+ expect(shadow.root.querySelectorAll(`${SVG_SEL} path`).length).toBe(1)
86
+ expect(draw.canRedo()).toBe(true)
87
+
88
+ expect(draw.redo()).toBe(true)
89
+ expect(shadow.root.querySelectorAll(`${SVG_SEL} path`).length).toBe(2)
90
+ expect(draw.canRedo()).toBe(false)
97
91
  })
98
92
 
99
- it('does nothing when not currently drawing', () => {
93
+ it('undo/redo are no-ops on empty stacks', () => {
100
94
  setup()
101
- draw.endDraw()
102
- expect(onDrawComplete).not.toHaveBeenCalled()
95
+ expect(draw.undo()).toBe(false)
96
+ expect(draw.redo()).toBe(false)
103
97
  })
104
98
 
105
- it('sets isCurrentlyDrawing to false after ending', () => {
99
+ it('committing a new stroke clears the redo stack', () => {
106
100
  setup()
107
- draw.startDraw(0, 0)
108
- draw.continueDraw(10, 10)
109
- draw.endDraw()
110
- expect(draw.isCurrentlyDrawing()).toBe(false)
101
+ stroke([[0, 0], [10, 10]])
102
+ draw.undo()
103
+ expect(draw.canRedo()).toBe(true)
104
+ stroke([[40, 40], [50, 50]])
105
+ expect(draw.canRedo()).toBe(false)
111
106
  })
112
107
  })
113
108
 
114
- describe('clearAll', () => {
115
- it('removes all SVG children and resets state', () => {
109
+ describe('getAnnotation', () => {
110
+ it('returns null when there are no committed strokes', () => {
116
111
  setup()
117
- draw.startDraw(0, 0)
118
- draw.continueDraw(10, 10)
119
- draw.clearAll()
112
+ expect(draw.getAnnotation()).toBeNull()
113
+ })
120
114
 
121
- expect(shadow.root.querySelector(SVG_SEL)!.childElementCount).toBe(0)
122
- expect(draw.isCurrentlyDrawing()).toBe(false)
123
- expect(draw.getPointCount()).toBe(0)
115
+ it('combines multiple strokes into one draw path with a joint center', () => {
116
+ setup()
117
+ stroke([[0, 0], [100, 0]])
118
+ stroke([[0, 100], [100, 100]])
119
+ const annotation = draw.getAnnotation()!
120
+ expect(annotation.drawPath!.match(/M /g)!.length).toBe(2)
121
+ expect(annotation.x).toBe(50)
122
+ expect(annotation.y).toBe(50)
124
123
  })
125
124
  })
126
125
 
127
126
  describe('cancelCurrentDraw', () => {
128
- it('removes the current path and resets drawing state', () => {
127
+ it('removes the in-progress path but keeps committed strokes', () => {
129
128
  setup()
130
- draw.startDraw(0, 0)
131
- draw.continueDraw(10, 10)
129
+ stroke([[0, 0], [10, 10]])
130
+ draw.startDraw(50, 50)
132
131
  draw.cancelCurrentDraw()
133
-
134
132
  expect(draw.isCurrentlyDrawing()).toBe(false)
135
- expect(draw.getPointCount()).toBe(0)
136
- const svg = shadow.root.querySelector(SVG_SEL)!
137
- expect(svg.querySelector('path')).toBeNull()
133
+ expect(draw.hasStrokes()).toBe(true)
134
+ expect(shadow.root.querySelectorAll(`${SVG_SEL} path`).length).toBe(1)
138
135
  })
136
+ })
139
137
 
140
- it('is safe to call when not drawing', () => {
138
+ describe('clearAll', () => {
139
+ it('removes all strokes and resets undo/redo', () => {
141
140
  setup()
142
- expect(() => draw.cancelCurrentDraw()).not.toThrow()
141
+ stroke([[0, 0], [10, 10]])
142
+ draw.undo()
143
+ draw.clearAll()
144
+ expect(shadow.root.querySelector(SVG_SEL)!.childElementCount).toBe(0)
145
+ expect(draw.hasStrokes()).toBe(false)
146
+ expect(draw.canUndo()).toBe(false)
147
+ expect(draw.canRedo()).toBe(false)
143
148
  })
144
149
  })
145
150
 
@@ -150,19 +155,4 @@ describe('DrawLayer', () => {
150
155
  expect(shadow.root.querySelector(SVG_SEL)).toBeNull()
151
156
  })
152
157
  })
153
-
154
- describe('getCenter calculation', () => {
155
- it('computes the center of the bounding box of all points', () => {
156
- setup()
157
- draw.startDraw(0, 0)
158
- draw.continueDraw(100, 0)
159
- draw.continueDraw(100, 100)
160
- draw.continueDraw(0, 100)
161
- draw.endDraw()
162
-
163
- const annotation = onDrawComplete.mock.calls[0][0]
164
- expect(annotation.x).toBe(50)
165
- expect(annotation.y).toBe(50)
166
- })
167
- })
168
158
  })
@@ -2,16 +2,23 @@ import type { ShadowContainer } from '../widget/shadow'
2
2
  import type { Annotation } from '../types'
3
3
  import { DRAW_STROKE } from '../styles'
4
4
 
5
+ type Point = [number, number]
6
+
7
+ interface Stroke {
8
+ path: SVGPathElement
9
+ d: string
10
+ points: Point[]
11
+ }
12
+
5
13
  export class DrawLayer {
6
14
  private svg: SVGSVGElement
7
- private path: SVGPathElement | null = null
8
- private points: [number, number][] = []
15
+ private currentPath: SVGPathElement | null = null
16
+ private currentPoints: Point[] = []
9
17
  private isDrawing = false
10
- private onDrawComplete: (annotation: Annotation) => void
11
-
12
- constructor(shadow: ShadowContainer, onDrawComplete: (annotation: Annotation) => void) {
13
- this.onDrawComplete = onDrawComplete
18
+ private committed: Stroke[] = []
19
+ private redoStack: { d: string; points: Point[] }[] = []
14
20
 
21
+ constructor(shadow: ShadowContainer) {
15
22
  this.svg = document.createElementNS('http://www.w3.org/2000/svg', 'svg')
16
23
  this.svg.setAttribute('class', 'mtb-draw-svg')
17
24
  shadow.append(this.svg)
@@ -19,90 +26,126 @@ export class DrawLayer {
19
26
 
20
27
  startDraw(x: number, y: number): void {
21
28
  this.isDrawing = true
22
- this.points = [[x, y]]
23
-
24
- this.path = document.createElementNS('http://www.w3.org/2000/svg', 'path')
25
- this.path.setAttribute('fill', 'none')
26
- this.path.setAttribute('stroke', DRAW_STROKE.color)
27
- this.path.setAttribute('stroke-width', DRAW_STROKE.width)
28
- this.path.setAttribute('stroke-linecap', DRAW_STROKE.linecap)
29
- this.path.setAttribute('stroke-linejoin', DRAW_STROKE.linejoin)
30
- this.path.setAttribute('style', `filter:${DRAW_STROKE.filter}`)
31
- this.svg.appendChild(this.path)
29
+ this.currentPoints = [[x, y]]
30
+ this.currentPath = this.createPath()
31
+ this.svg.appendChild(this.currentPath)
32
32
  }
33
33
 
34
34
  continueDraw(x: number, y: number): void {
35
- if (!this.isDrawing || !this.path) return
36
- this.points.push([x, y])
37
- this.path.setAttribute('d', this.buildPathD())
38
- }
39
-
40
- endDraw(): void {
41
- if (!this.isDrawing || this.points.length < 2) {
42
- this.isDrawing = false
43
- if (this.path) {
44
- this.path.remove()
45
- this.path = null
46
- }
47
- return
48
- }
35
+ if (!this.isDrawing || !this.currentPath) return
36
+ this.currentPoints.push([x, y])
37
+ this.currentPath.setAttribute('d', buildPathD(this.currentPoints))
38
+ }
39
+
40
+ // Commits the in-progress stroke to the undo stack and clears the redo stack.
41
+ // Callers decide whether the stroke has enough points before committing.
42
+ commitStroke(): boolean {
43
+ if (!this.isDrawing || !this.currentPath) return false
49
44
  this.isDrawing = false
45
+ this.committed.push({ path: this.currentPath, d: buildPathD(this.currentPoints), points: this.currentPoints })
46
+ this.redoStack = []
47
+ this.currentPath = null
48
+ this.currentPoints = []
49
+ return true
50
+ }
50
51
 
51
- const center = this.getCenter()
52
- const pathD = this.buildPathD()
52
+ cancelCurrentDraw(): void {
53
+ this.currentPath?.remove()
54
+ this.currentPath = null
55
+ this.isDrawing = false
56
+ this.currentPoints = []
57
+ }
53
58
 
54
- const annotation: Annotation = {
55
- type: 'draw',
56
- x: center[0],
57
- y: center[1],
58
- drawPath: pathD,
59
- }
59
+ undo(): boolean {
60
+ const stroke = this.committed.pop()
61
+ if (!stroke) return false
62
+ stroke.path.remove()
63
+ this.redoStack.push({ d: stroke.d, points: stroke.points })
64
+ return true
65
+ }
60
66
 
61
- this.onDrawComplete(annotation)
67
+ redo(): boolean {
68
+ const entry = this.redoStack.pop()
69
+ if (!entry) return false
70
+ const path = this.createPath()
71
+ path.setAttribute('d', entry.d)
72
+ this.svg.appendChild(path)
73
+ this.committed.push({ path, d: entry.d, points: entry.points })
74
+ return true
62
75
  }
63
76
 
64
- private buildPathD(): string {
65
- if (this.points.length === 0) return ''
66
- const [fx, fy] = this.points[0]
67
- let d = `M ${fx} ${fy}`
68
- for (let i = 1; i < this.points.length; i++) {
69
- const [x, y] = this.points[i]
70
- d += ` L ${x} ${y}`
71
- }
72
- return d
77
+ canUndo(): boolean {
78
+ return this.committed.length > 0
73
79
  }
74
80
 
75
- private getCenter(): [number, number] {
76
- const xs = this.points.map(p => p[0])
77
- const ys = this.points.map(p => p[1])
78
- const cx = (Math.min(...xs) + Math.max(...xs)) / 2
79
- const cy = (Math.min(...ys) + Math.max(...ys)) / 2
80
- return [cx, cy]
81
+ canRedo(): boolean {
82
+ return this.redoStack.length > 0
81
83
  }
82
84
 
83
- clearAll(): void {
84
- while (this.svg.firstChild) this.svg.removeChild(this.svg.firstChild)
85
- this.path = null
86
- this.isDrawing = false
87
- this.points = []
85
+ hasStrokes(): boolean {
86
+ return this.committed.length > 0
88
87
  }
89
88
 
90
- cancelCurrentDraw(): void {
91
- this.path?.remove()
92
- this.path = null
89
+ getAnnotation(): Annotation | null {
90
+ if (this.committed.length === 0) return null
91
+ const points = this.committed.flatMap(s => s.points)
92
+ const [cx, cy] = center(points)
93
+ return {
94
+ type: 'draw',
95
+ x: cx,
96
+ y: cy,
97
+ drawPath: this.committed.map(s => s.d).join(' '),
98
+ }
99
+ }
100
+
101
+ clearAll(): void {
102
+ while (this.svg.firstChild) this.svg.removeChild(this.svg.firstChild)
103
+ this.currentPath = null
93
104
  this.isDrawing = false
94
- this.points = []
105
+ this.currentPoints = []
106
+ this.committed = []
107
+ this.redoStack = []
95
108
  }
96
109
 
97
110
  isCurrentlyDrawing(): boolean {
98
111
  return this.isDrawing
99
112
  }
100
113
 
101
- getPointCount(): number {
102
- return this.points.length
114
+ getCurrentPointCount(): number {
115
+ return this.currentPoints.length
103
116
  }
104
117
 
105
118
  destroy(): void {
106
119
  this.svg.remove()
107
120
  }
121
+
122
+ private createPath(): SVGPathElement {
123
+ const path = document.createElementNS('http://www.w3.org/2000/svg', 'path')
124
+ path.setAttribute('fill', 'none')
125
+ path.setAttribute('stroke', DRAW_STROKE.color)
126
+ path.setAttribute('stroke-width', DRAW_STROKE.width)
127
+ path.setAttribute('stroke-linecap', DRAW_STROKE.linecap)
128
+ path.setAttribute('stroke-linejoin', DRAW_STROKE.linejoin)
129
+ path.setAttribute('style', `filter:${DRAW_STROKE.filter}`)
130
+ return path
131
+ }
132
+ }
133
+
134
+ function buildPathD(points: Point[]): string {
135
+ if (points.length === 0) return ''
136
+ const [fx, fy] = points[0]
137
+ let d = `M ${fx} ${fy}`
138
+ for (let i = 1; i < points.length; i++) {
139
+ const [x, y] = points[i]
140
+ d += ` L ${x} ${y}`
141
+ }
142
+ return d
143
+ }
144
+
145
+ function center(points: Point[]): Point {
146
+ const xs = points.map(p => p[0])
147
+ const ys = points.map(p => p[1])
148
+ const cx = (Math.min(...xs) + Math.max(...xs)) / 2
149
+ const cy = (Math.min(...ys) + Math.max(...ys)) / 2
150
+ return [cx, cy]
108
151
  }
@@ -1,5 +1,28 @@
1
1
  import { describe, it, expect } from 'vitest'
2
- import { getElementName, getCssSelector } from './highlighter'
2
+ import { ElementHighlighter, getElementName, getCssSelector } from './highlighter'
3
+ import { ShadowContainer } from '../widget/shadow'
4
+
5
+ describe('ElementHighlighter tooltip', () => {
6
+ it('shows a tooltip with the element name on highlight', () => {
7
+ const shadow = new ShadowContainer()
8
+ const highlighter = new ElementHighlighter(shadow)
9
+ const el = document.createElement('button')
10
+ el.textContent = 'Export PDF'
11
+ document.body.appendChild(el)
12
+
13
+ highlighter.highlight(el)
14
+ const tooltip = shadow.root.querySelector<HTMLDivElement>('.mtb-highlight-tooltip')!
15
+ expect(tooltip.textContent).toBe('Export PDF')
16
+ expect(tooltip.style.display).toBe('block')
17
+
18
+ highlighter.hide()
19
+ expect(tooltip.style.display).toBe('none')
20
+
21
+ highlighter.destroy()
22
+ document.body.removeChild(el)
23
+ shadow.destroy()
24
+ })
25
+ })
3
26
 
4
27
  describe('getElementName', () => {
5
28
  it('returns aria-label when present', () => {
@@ -2,11 +2,13 @@ import type { ShadowContainer } from '../widget/shadow'
2
2
 
3
3
  export class ElementHighlighter {
4
4
  private highlightEl: HTMLDivElement
5
+ private tooltipEl: HTMLDivElement
5
6
 
6
7
  constructor(shadow: ShadowContainer) {
7
8
  this.highlightEl = shadow.el<HTMLDivElement>('div', 'mtb-highlight')
9
+ this.tooltipEl = shadow.el<HTMLDivElement>('div', 'mtb-highlight-tooltip')
8
10
  this.hide()
9
- shadow.append(this.highlightEl)
11
+ shadow.append(this.highlightEl, this.tooltipEl)
10
12
  }
11
13
 
12
14
  highlight(el: Element): void {
@@ -16,14 +18,26 @@ export class ElementHighlighter {
16
18
  this.highlightEl.style.width = `${rect.width + 4}px`
17
19
  this.highlightEl.style.height = `${rect.height + 4}px`
18
20
  this.highlightEl.style.display = 'block'
21
+ this.showTooltip(el, rect)
22
+ }
23
+
24
+ private showTooltip(el: Element, rect: DOMRect): void {
25
+ this.tooltipEl.textContent = getElementName(el)
26
+ const below = rect.top < 30
27
+ this.tooltipEl.classList.toggle('below', below)
28
+ this.tooltipEl.style.left = `${rect.left + rect.width / 2}px`
29
+ this.tooltipEl.style.top = below ? `${rect.bottom + 8}px` : `${rect.top - 8}px`
30
+ this.tooltipEl.style.display = 'block'
19
31
  }
20
32
 
21
33
  hide(): void {
22
34
  this.highlightEl.style.display = 'none'
35
+ this.tooltipEl.style.display = 'none'
23
36
  }
24
37
 
25
38
  destroy(): void {
26
39
  this.highlightEl.remove()
40
+ this.tooltipEl.remove()
27
41
  }
28
42
  }
29
43
 
@@ -73,7 +87,7 @@ export function getCssSelector(el: Element): string {
73
87
  )
74
88
  if (siblings.length > 1) {
75
89
  const idx = siblings.indexOf(current) + 1
76
- selector += `:nth-child(${idx})`
90
+ selector += `:nth-of-type(${idx})`
77
91
  }
78
92
  }
79
93