Skip to content

Commit ad31385

Browse files
authored
Merge pull request #20055 from calixteman/bug1975264
[Editor] When an annotation is added, announce it if the user is using a screen reader (bug 1975264)
2 parents 54c0d56 + c22a425 commit ad31385

19 files changed

+86
-20
lines changed

l10n/en-US/viewer.ftl

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -533,6 +533,14 @@ pdfjs-editor-alt-text-settings-show-dialog-button-label = Show alt text editor r
533533
pdfjs-editor-alt-text-settings-show-dialog-description = Helps you make sure all your images have alt text.
534534
pdfjs-editor-alt-text-settings-close-button = Close
535535
536+
## Accessibility labels (announced by screen readers) for objects added to the editor.
537+
538+
pdfjs-editor-highlight-added-alert = Highlight added
539+
pdfjs-editor-freetext-added-alert = Text added
540+
pdfjs-editor-ink-added-alert = Drawing added
541+
pdfjs-editor-stamp-added-alert = Image added
542+
pdfjs-editor-signature-added-alert = Signature added
543+
536544
## "Annotations removed" bar
537545

538546
pdfjs-editor-undo-bar-message-highlight = Highlight removed

src/display/editor/draw.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,9 @@ class DrawingEditor extends AnnotationEditor {
106106
#createDrawOutlines({ drawOutlines, drawId, drawingOptions }) {
107107
this.#drawOutlines = drawOutlines;
108108
this._drawingOptions ||= drawingOptions;
109+
if (!this.annotationElementId) {
110+
this._uiManager.a11yAlert(`pdfjs-editor-${this.editorType}-added-alert`);
111+
}
109112

110113
if (drawId >= 0) {
111114
this._drawId = drawId;

src/display/editor/editor.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,7 @@ class AnnotationEditor {
180180
this._willKeepAspectRatio = false;
181181
this._initialOptions.isCentered = parameters.isCentered;
182182
this._structTreeParentId = null;
183+
this.annotationElementId = parameters.annotationElementId || null;
183184

184185
const {
185186
rotation,
@@ -427,6 +428,9 @@ class AnnotationEditor {
427428
* Commit the data contained in this editor.
428429
*/
429430
commit() {
431+
if (!this.isInEditMode()) {
432+
return;
433+
}
430434
this.addToAnnotationStorage();
431435
}
432436

@@ -1641,6 +1645,7 @@ class AnnotationEditor {
16411645
parent,
16421646
id: parent.getNextId(),
16431647
uiManager,
1648+
annotationElementId: data.annotationElementId,
16441649
});
16451650
editor.rotation = data.rotation;
16461651
editor.#accessibilityData = data.accessibilityData;

src/display/editor/freetext.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,9 @@ class FreeTextEditor extends AnnotationEditor {
131131
FreeTextEditor._defaultColor ||
132132
AnnotationEditor._defaultLineColor;
133133
this.#fontSize = params.fontSize || FreeTextEditor._defaultFontSize;
134+
if (!this.annotationElementId) {
135+
this._uiManager.a11yAlert("pdfjs-editor-freetext-added-alert");
136+
}
134137
}
135138

136139
/** @inheritdoc */
@@ -782,6 +785,7 @@ class FreeTextEditor extends AnnotationEditor {
782785
pageIndex: pageNumber - 1,
783786
rect: rect.slice(0),
784787
rotation,
788+
annotationElementId: id,
785789
id,
786790
deleted: false,
787791
popupRef,
@@ -791,7 +795,6 @@ class FreeTextEditor extends AnnotationEditor {
791795
editor.#fontSize = data.fontSize;
792796
editor.#color = Util.makeHexColor(...data.color);
793797
editor.#content = FreeTextEditor.#deserializeContent(data.value);
794-
editor.annotationElementId = data.id || null;
795798
editor._initialData = initialData;
796799

797800
return editor;

src/display/editor/highlight.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,10 @@ class HighlightEditor extends AnnotationEditor {
126126
this.#addToDrawLayer();
127127
this.rotate(this.rotation);
128128
}
129+
130+
if (!this.annotationElementId) {
131+
this._uiManager.a11yAlert("pdfjs-editor-highlight-added-alert");
132+
}
129133
}
130134

131135
/** @inheritdoc */
@@ -876,6 +880,7 @@ class HighlightEditor extends AnnotationEditor {
876880
pageIndex: pageNumber - 1,
877881
rect: rect.slice(0),
878882
rotation,
883+
annotationElementId: id,
879884
id,
880885
deleted: false,
881886
popupRef,
@@ -904,6 +909,7 @@ class HighlightEditor extends AnnotationEditor {
904909
pageIndex: pageNumber - 1,
905910
rect: rect.slice(0),
906911
rotation,
912+
annotationElementId: id,
907913
id,
908914
deleted: false,
909915
popupRef,
@@ -918,7 +924,6 @@ class HighlightEditor extends AnnotationEditor {
918924
if (inkLists) {
919925
editor.#thickness = data.thickness;
920926
}
921-
editor.annotationElementId = data.id || null;
922927
editor._initialData = initialData;
923928

924929
const [pageWidth, pageHeight] = editor.pageDimensions;

src/display/editor/ink.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,14 +164,14 @@ class InkEditor extends DrawingEditor {
164164
pageIndex: pageNumber - 1,
165165
rect: rect.slice(0),
166166
rotation,
167+
annotationElementId: id,
167168
id,
168169
deleted: false,
169170
popupRef,
170171
};
171172
}
172173

173174
const editor = await super.deserialize(data, parent, uiManager);
174-
editor.annotationElementId = data.id || null;
175175
editor._initialData = initialData;
176176

177177
return editor;

src/display/editor/stamp.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -484,6 +484,9 @@ class StampEditor extends AnnotationEditor {
484484
if (this.#bitmapFileName) {
485485
this.div.setAttribute("aria-description", this.#bitmapFileName);
486486
}
487+
if (!this.annotationElementId) {
488+
this._uiManager.a11yAlert("pdfjs-editor-stamp-added-alert");
489+
}
487490
}
488491

489492
copyCanvas(maxDataDimension, maxPreviewDimension, createImageData = false) {
@@ -781,6 +784,7 @@ class StampEditor extends AnnotationEditor {
781784
pageIndex: pageNumber - 1,
782785
rect: rect.slice(0),
783786
rotation,
787+
annotationElementId: id,
784788
id,
785789
deleted: false,
786790
accessibilityData: {
@@ -812,7 +816,6 @@ class StampEditor extends AnnotationEditor {
812816
editor.width = (rect[2] - rect[0]) / parentWidth;
813817
editor.height = (rect[3] - rect[1]) / parentHeight;
814818

815-
editor.annotationElementId = data.id || null;
816819
if (accessibilityData) {
817820
editor.altTextData = accessibilityData;
818821
}

src/display/editor/tools.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -676,6 +676,8 @@ class AnnotationEditorUIManager {
676676

677677
#viewer = null;
678678

679+
#viewerAlert = null;
680+
679681
#updateModeCapability = null;
680682

681683
static TRANSLATE_SMALL = 1; // page units.
@@ -818,6 +820,7 @@ class AnnotationEditorUIManager {
818820
constructor(
819821
container,
820822
viewer,
823+
viewerAlert,
821824
altTextManager,
822825
signatureManager,
823826
eventBus,
@@ -834,6 +837,7 @@ class AnnotationEditorUIManager {
834837
const signal = (this._signal = this.#abortController.signal);
835838
this.#container = container;
836839
this.#viewer = viewer;
840+
this.#viewerAlert = viewerAlert;
837841
this.#altTextManager = altTextManager;
838842
this.#signatureManager = signatureManager;
839843
this._eventBus = eventBus;
@@ -1175,6 +1179,19 @@ class AnnotationEditorUIManager {
11751179
}
11761180
}
11771181

1182+
a11yAlert(messageId, args = null) {
1183+
const viewerAlert = this.#viewerAlert;
1184+
if (!viewerAlert) {
1185+
return;
1186+
}
1187+
viewerAlert.setAttribute("data-l10n-id", messageId);
1188+
if (args) {
1189+
viewerAlert.setAttribute("data-l10n-args", JSON.stringify(args));
1190+
} else {
1191+
viewerAlert.removeAttribute("data-l10n-args");
1192+
}
1193+
}
1194+
11781195
#selectionChange() {
11791196
const selection = document.getSelection();
11801197
if (!selection || selection.isCollapsed) {

test/integration/freetext_editor_spec.mjs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,9 @@ describe("FreeText Editor", () => {
110110
await waitForSelectedEditor(page, editorSelector);
111111
await waitForStorageEntries(page, 1);
112112

113+
const alert = await page.$eval("#viewer-alert", el => el.textContent);
114+
expect(alert).toEqual("Text added");
115+
113116
let content = await page.$eval(editorSelector, el =>
114117
el.innerText.trimEnd()
115118
);

test/integration/highlight_editor_spec.mjs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,9 @@ describe("Highlight Editor", () => {
7878

7979
await page.waitForSelector(`${getEditorSelector(0)}`);
8080

81+
const alert = await page.$eval("#viewer-alert", el => el.textContent);
82+
expect(alert).toEqual("Highlight added");
83+
8184
const oneToOne = Array.from(new Array(13).keys(), n => n + 2).concat(
8285
Array.from(new Array(13).keys(), n => 13 - n)
8386
);

0 commit comments

Comments
 (0)