Skip to content

Commit 0ef295f

Browse files
committed
Include text from shape selector in thumbnail alt text
Include extracted document text from shape selectors in the alt text of thumbnails.
1 parent c89309d commit 0ef295f

File tree

6 files changed

+53
-10
lines changed

6 files changed

+53
-10
lines changed

src/sidebar/components/Annotation/Annotation.tsx

+6-1
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,12 @@ function Annotation({
124124
replyCount={replyCount}
125125
threadIsCollapsed={threadIsCollapsed}
126126
/>
127-
{targetShape && <AnnotationThumbnail tag={annotation.$tag} />}
127+
{targetShape && (
128+
<AnnotationThumbnail
129+
tag={annotation.$tag}
130+
textInImage={targetShape.text}
131+
/>
132+
)}
128133
{annotationQuote && (
129134
<AnnotationQuote
130135
quote={annotationQuote}

src/sidebar/components/Annotation/AnnotationThumbnail.tsx

+16-1
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,17 @@ function BitmapImage({ alt, bitmap, classes, scale = 1.0 }: BitmapImageProps) {
4444
export type AnnotationThumbnailProps = {
4545
tag: string;
4646
thumbnailService: ThumbnailService;
47+
48+
/**
49+
* Text contained in the thumbnail.
50+
*
51+
* This is used when generating alt text for the thumbnail.
52+
*/
53+
textInImage?: string;
4754
};
4855

4956
function AnnotationThumbnail({
57+
textInImage,
5058
tag,
5159
thumbnailService,
5260
}: AnnotationThumbnailProps) {
@@ -69,14 +77,21 @@ function AnnotationThumbnail({
6977
}
7078
}, [error, devicePixelRatio, tag, thumbnail, thumbnailService]);
7179

80+
let altText;
81+
if (textInImage) {
82+
altText = `Thumbnail. Contains text: ${textInImage}`;
83+
} else {
84+
altText = 'Thumbnail';
85+
}
86+
7287
return (
7388
<div
7489
className="flex flex-row justify-center"
7590
data-testid="thumbnail-container"
7691
>
7792
{thumbnail && (
7893
<BitmapImage
79-
alt="annotated content thumbnail"
94+
alt={altText}
8095
bitmap={thumbnail}
8196
classes="border rounded-md"
8297
scale={devicePixelRatio}

src/sidebar/components/Annotation/test/Annotation-test.js

+2
Original file line numberDiff line numberDiff line change
@@ -157,12 +157,14 @@ describe('Annotation', () => {
157157
right: 10,
158158
bottom: 0,
159159
},
160+
text: 'Some text',
160161
},
161162
];
162163
const wrapper = createComponent({ annotation });
163164
const thumbnail = wrapper.find('AnnotationThumbnail');
164165
assert.isTrue(thumbnail.exists());
165166
assert.equal(thumbnail.prop('tag'), annotation.$tag);
167+
assert.equal(thumbnail.prop('textInImage'), 'Some text');
166168
});
167169
});
168170

src/sidebar/components/Annotation/test/AnnotationThumbnail-test.js

+18
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,24 @@ describe('AnnotationThumbnail', () => {
3838
assert.isTrue(wrapper.exists('BitmapImage'));
3939
});
4040

41+
[
42+
{
43+
textInImage: undefined,
44+
expectedAlt: 'Thumbnail',
45+
},
46+
{
47+
textInImage: 'Foo bar',
48+
expectedAlt: 'Thumbnail. Contains text: Foo bar',
49+
},
50+
].forEach(({ textInImage, expectedAlt }) => {
51+
it('sets alt text for thumbnail', () => {
52+
fakeThumbnailService.get.returns(fakeThumbnail);
53+
const wrapper = createComponent({ textInImage });
54+
const image = wrapper.find('canvas');
55+
assert.equal(image.prop('aria-label'), expectedAlt);
56+
});
57+
});
58+
4159
it('requests thumbnail and then renders it if not cached', async () => {
4260
const wrapper = createComponent();
4361
assert.calledOnce(fakeThumbnailService.fetch);

src/sidebar/helpers/annotation-metadata.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -401,11 +401,11 @@ export function quote(annotation: APIAnnotationData): string | null {
401401
* This will return `null` if the annotation is associated with a text
402402
* selection instead of a shape.
403403
*/
404-
export function shape(annotation: APIAnnotationData): Shape | null {
404+
export function shape(annotation: APIAnnotationData): ShapeSelector | null {
405405
const shapeSelector = annotation.target[0]?.selector?.find(
406406
s => s.type === 'ShapeSelector',
407407
) as ShapeSelector | undefined;
408-
return shapeSelector?.shape ?? null;
408+
return shapeSelector ?? null;
409409
}
410410

411411
/**

src/sidebar/helpers/test/annotation-metadata-test.js

+9-6
Original file line numberDiff line numberDiff line change
@@ -726,15 +726,18 @@ describe('sidebar/helpers/annotation-metadata', () => {
726726
},
727727
],
728728
expected: {
729-
type: 'rect',
730-
left: 0,
731-
top: 10,
732-
right: 10,
733-
bottom: 0,
729+
type: 'ShapeSelector',
730+
shape: {
731+
type: 'rect',
732+
left: 0,
733+
top: 10,
734+
right: 10,
735+
bottom: 0,
736+
},
734737
},
735738
},
736739
].forEach(({ selectors, expected }) => {
737-
it('returns shape from shape selector', () => {
740+
it('returns shape selector', () => {
738741
const annotation = {
739742
target: [
740743
{

0 commit comments

Comments
 (0)