mirror of
https://github.com/mozilla/pdf.js.git
synced 2025-04-20 15:18:08 +02:00
[Annotations] Add some aria-owns in the text layer to link to annotations (bug 1780375)
This patch doesn't structurally change the text layer: it just adds some aria-owns attributes to some spans. The aria-owns attribute expect to have an element id, hence it's why it adds back an id on the element rendering an annotation, but this id is built in using crypto.randomUUID to avoid any potential issues with the hash in the url. The elements in the annotation layer are moved into the DOM in order to have them in the same "order" as they visually are. The overall goal is to help screen readers to present to the user the annotations as they visually are and as they come in the text flow. It is clearly not perfect, but it should improve readability for some people with visual disabilities.
This commit is contained in:
parent
cef2ac99e5
commit
f316300113
23 changed files with 436 additions and 246 deletions
|
@ -21,6 +21,8 @@
|
|||
/** @typedef {import("../src/display/editor/tools.js").AnnotationEditorUIManager} AnnotationEditorUIManager */
|
||||
// eslint-disable-next-line max-len
|
||||
/** @typedef {import("../annotation_storage.js").AnnotationStorage} AnnotationStorage */
|
||||
// eslint-disable-next-line max-len
|
||||
/** @typedef {import("./text_accessibility.js").TextAccessibilityManager} TextAccessibilityManager */
|
||||
/** @typedef {import("./interfaces").IL10n} IL10n */
|
||||
|
||||
import { AnnotationEditorLayer } from "pdfjs-lib";
|
||||
|
@ -31,6 +33,7 @@ import { NullL10n } from "./l10n_utils.js";
|
|||
* @property {number} mode - Editor mode
|
||||
* @property {HTMLDivElement} pageDiv
|
||||
* @property {PDFPageProxy} pdfPage
|
||||
* @property {TextAccessibilityManager} accessibilityManager
|
||||
* @property {AnnotationStorage} annotationStorage
|
||||
* @property {IL10n} l10n - Localization service.
|
||||
* @property {AnnotationEditorUIManager} uiManager
|
||||
|
@ -46,6 +49,7 @@ class AnnotationEditorLayerBuilder {
|
|||
this.pageDiv = options.pageDiv;
|
||||
this.pdfPage = options.pdfPage;
|
||||
this.annotationStorage = options.annotationStorage || null;
|
||||
this.accessibilityManager = options.accessibilityManager;
|
||||
this.l10n = options.l10n || NullL10n;
|
||||
this.annotationEditorLayer = null;
|
||||
this.div = null;
|
||||
|
@ -83,6 +87,7 @@ class AnnotationEditorLayerBuilder {
|
|||
uiManager: this.#uiManager,
|
||||
div: this.div,
|
||||
annotationStorage: this.annotationStorage,
|
||||
accessibilityManager: this.accessibilityManager,
|
||||
pageIndex: this.pdfPage._pageIndex,
|
||||
l10n: this.l10n,
|
||||
viewport: clonedViewport,
|
||||
|
|
|
@ -210,7 +210,6 @@
|
|||
|
||||
.annotationLayer .popup {
|
||||
position: absolute;
|
||||
z-index: 200;
|
||||
max-width: calc(180px * var(--scale-factor));
|
||||
background-color: rgba(255, 255, 153, 1);
|
||||
box-shadow: 0 calc(2px * var(--scale-factor)) calc(5px * var(--scale-factor))
|
||||
|
|
|
@ -19,6 +19,8 @@
|
|||
/** @typedef {import("./interfaces").IDownloadManager} IDownloadManager */
|
||||
/** @typedef {import("./interfaces").IL10n} IL10n */
|
||||
/** @typedef {import("./interfaces").IPDFLinkService} IPDFLinkService */
|
||||
// eslint-disable-next-line max-len
|
||||
/** @typedef {import("./textaccessibility.js").TextAccessibilityManager} TextAccessibilityManager */
|
||||
|
||||
import { AnnotationLayer } from "pdfjs-lib";
|
||||
import { NullL10n } from "./l10n_utils.js";
|
||||
|
@ -40,6 +42,7 @@ import { NullL10n } from "./l10n_utils.js";
|
|||
* [fieldObjectsPromise]
|
||||
* @property {Object} [mouseState]
|
||||
* @property {Map<string, HTMLCanvasElement>} [annotationCanvasMap]
|
||||
* @property {TextAccessibilityManager} accessibilityManager
|
||||
*/
|
||||
|
||||
class AnnotationLayerBuilder {
|
||||
|
@ -60,6 +63,7 @@ class AnnotationLayerBuilder {
|
|||
fieldObjectsPromise = null,
|
||||
mouseState = null,
|
||||
annotationCanvasMap = null,
|
||||
accessibilityManager = null,
|
||||
}) {
|
||||
this.pageDiv = pageDiv;
|
||||
this.pdfPage = pdfPage;
|
||||
|
@ -74,6 +78,7 @@ class AnnotationLayerBuilder {
|
|||
this._fieldObjectsPromise = fieldObjectsPromise;
|
||||
this._mouseState = mouseState;
|
||||
this._annotationCanvasMap = annotationCanvasMap;
|
||||
this._accessibilityManager = accessibilityManager;
|
||||
|
||||
this.div = null;
|
||||
this._cancelled = false;
|
||||
|
@ -112,6 +117,7 @@ class AnnotationLayerBuilder {
|
|||
fieldObjects,
|
||||
mouseState: this._mouseState,
|
||||
annotationCanvasMap: this._annotationCanvasMap,
|
||||
accessibilityManager: this._accessibilityManager,
|
||||
};
|
||||
|
||||
if (this.div) {
|
||||
|
|
|
@ -30,6 +30,8 @@
|
|||
// eslint-disable-next-line max-len
|
||||
/** @typedef {import("./interfaces").IPDFTextLayerFactory} IPDFTextLayerFactory */
|
||||
/** @typedef {import("./interfaces").IPDFXfaLayerFactory} IPDFXfaLayerFactory */
|
||||
// eslint-disable-next-line max-len
|
||||
/** @typedef {import("./text_accessibility.js").TextAccessibilityManager} TextAccessibilityManager */
|
||||
|
||||
import {
|
||||
AnnotationEditorType,
|
||||
|
@ -1637,6 +1639,7 @@ class BaseViewer {
|
|||
* @property {boolean} [enhanceTextSelection]
|
||||
* @property {EventBus} eventBus
|
||||
* @property {TextHighlighter} highlighter
|
||||
* @property {TextAccessibilityManager} [accessibilityManager]
|
||||
*/
|
||||
|
||||
/**
|
||||
|
@ -1650,6 +1653,7 @@ class BaseViewer {
|
|||
enhanceTextSelection = false,
|
||||
eventBus,
|
||||
highlighter,
|
||||
accessibilityManager = null,
|
||||
}) {
|
||||
return new TextLayerBuilder({
|
||||
textLayerDiv,
|
||||
|
@ -1660,6 +1664,7 @@ class BaseViewer {
|
|||
? false
|
||||
: enhanceTextSelection,
|
||||
highlighter,
|
||||
accessibilityManager,
|
||||
});
|
||||
}
|
||||
|
||||
|
@ -1698,6 +1703,7 @@ class BaseViewer {
|
|||
* [fieldObjectsPromise]
|
||||
* @property {Map<string, HTMLCanvasElement>} [annotationCanvasMap] - Map some
|
||||
* annotation ids with canvases used to render them.
|
||||
* @property {TextAccessibilityManager} [accessibilityManager]
|
||||
*/
|
||||
|
||||
/**
|
||||
|
@ -1716,6 +1722,7 @@ class BaseViewer {
|
|||
mouseState = this._scriptingManager?.mouseState,
|
||||
fieldObjectsPromise = this.pdfDocument?.getFieldObjects(),
|
||||
annotationCanvasMap = null,
|
||||
accessibilityManager = null,
|
||||
}) {
|
||||
return new AnnotationLayerBuilder({
|
||||
pageDiv,
|
||||
|
@ -1731,6 +1738,7 @@ class BaseViewer {
|
|||
mouseState,
|
||||
fieldObjectsPromise,
|
||||
annotationCanvasMap,
|
||||
accessibilityManager,
|
||||
});
|
||||
}
|
||||
|
||||
|
@ -1741,6 +1749,7 @@ class BaseViewer {
|
|||
* @property {PDFPageProxy} pdfPage
|
||||
* @property {IL10n} l10n
|
||||
* @property {AnnotationStorage} [annotationStorage] - Storage for annotation
|
||||
* @property {TextAccessibilityManager} [accessibilityManager]
|
||||
* data in forms.
|
||||
*/
|
||||
|
||||
|
@ -1752,6 +1761,7 @@ class BaseViewer {
|
|||
uiManager = this.#annotationEditorUIManager,
|
||||
pageDiv,
|
||||
pdfPage,
|
||||
accessibilityManager = null,
|
||||
l10n,
|
||||
annotationStorage = this.pdfDocument?.annotationStorage,
|
||||
}) {
|
||||
|
@ -1760,6 +1770,7 @@ class BaseViewer {
|
|||
pageDiv,
|
||||
pdfPage,
|
||||
annotationStorage,
|
||||
accessibilityManager,
|
||||
l10n,
|
||||
});
|
||||
}
|
||||
|
|
|
@ -92,6 +92,10 @@
|
|||
box-sizing: border-box;
|
||||
}
|
||||
|
||||
#viewer.textLayer-visible .textLayer span[aria-owns] {
|
||||
background-color: rgba(255, 0, 0, 0.3);
|
||||
}
|
||||
|
||||
#viewer.textLayer-hover .textLayer span:hover {
|
||||
background-color: rgba(255, 255, 255, 1);
|
||||
color: rgba(0, 0, 0, 1);
|
||||
|
|
|
@ -30,6 +30,8 @@
|
|||
/** @typedef {import("./interfaces").IPDFTextLayerFactory} IPDFTextLayerFactory */
|
||||
/** @typedef {import("./interfaces").IPDFXfaLayerFactory} IPDFXfaLayerFactory */
|
||||
/** @typedef {import("./text_highlighter").TextHighlighter} TextHighlighter */
|
||||
// eslint-disable-next-line max-len
|
||||
/** @typedef {import("./text_accessibility.js").TextAccessibilityManager} TextAccessibilityManager */
|
||||
|
||||
import { AnnotationEditorLayerBuilder } from "./annotation_editor_layer_builder.js";
|
||||
import { AnnotationLayerBuilder } from "./annotation_layer_builder.js";
|
||||
|
@ -60,6 +62,7 @@ class DefaultAnnotationLayerFactory {
|
|||
* [fieldObjectsPromise]
|
||||
* @property {Map<string, HTMLCanvasElement>} [annotationCanvasMap] - Map some
|
||||
* annotation ids with canvases used to render them.
|
||||
* @property {TextAccessibilityManager} [accessibilityManager]
|
||||
*/
|
||||
|
||||
/**
|
||||
|
@ -78,6 +81,7 @@ class DefaultAnnotationLayerFactory {
|
|||
mouseState = null,
|
||||
fieldObjectsPromise = null,
|
||||
annotationCanvasMap = null,
|
||||
accessibilityManager = null,
|
||||
}) {
|
||||
return new AnnotationLayerBuilder({
|
||||
pageDiv,
|
||||
|
@ -92,6 +96,7 @@ class DefaultAnnotationLayerFactory {
|
|||
fieldObjectsPromise,
|
||||
mouseState,
|
||||
annotationCanvasMap,
|
||||
accessibilityManager,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
@ -107,6 +112,7 @@ class DefaultAnnotationEditorLayerFactory {
|
|||
* @property {PDFPageProxy} pdfPage
|
||||
* @property {IL10n} l10n
|
||||
* @property {AnnotationStorage} [annotationStorage] - Storage for annotation
|
||||
* @property {TextAccessibilityManager} [accessibilityManager]
|
||||
* data in forms.
|
||||
*/
|
||||
|
||||
|
@ -118,6 +124,7 @@ class DefaultAnnotationEditorLayerFactory {
|
|||
uiManager = null,
|
||||
pageDiv,
|
||||
pdfPage,
|
||||
accessibilityManager = null,
|
||||
l10n,
|
||||
annotationStorage = null,
|
||||
}) {
|
||||
|
@ -125,6 +132,7 @@ class DefaultAnnotationEditorLayerFactory {
|
|||
uiManager,
|
||||
pageDiv,
|
||||
pdfPage,
|
||||
accessibilityManager,
|
||||
l10n,
|
||||
annotationStorage,
|
||||
});
|
||||
|
@ -163,6 +171,7 @@ class DefaultTextLayerFactory {
|
|||
* @property {boolean} [enhanceTextSelection]
|
||||
* @property {EventBus} eventBus
|
||||
* @property {TextHighlighter} highlighter
|
||||
* @property {TextAccessibilityManager} [accessibilityManager]
|
||||
*/
|
||||
|
||||
/**
|
||||
|
@ -176,6 +185,7 @@ class DefaultTextLayerFactory {
|
|||
enhanceTextSelection = false,
|
||||
eventBus,
|
||||
highlighter,
|
||||
accessibilityManager = null,
|
||||
}) {
|
||||
return new TextLayerBuilder({
|
||||
textLayerDiv,
|
||||
|
@ -184,6 +194,7 @@ class DefaultTextLayerFactory {
|
|||
enhanceTextSelection,
|
||||
eventBus,
|
||||
highlighter,
|
||||
accessibilityManager,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
|
@ -29,6 +29,8 @@
|
|||
/** @typedef {import("./text_layer_builder").TextLayerBuilder} TextLayerBuilder */
|
||||
/** @typedef {import("./ui_utils").RenderingStates} RenderingStates */
|
||||
/** @typedef {import("./xfa_layer_builder").XfaLayerBuilder} XfaLayerBuilder */
|
||||
// eslint-disable-next-line max-len
|
||||
/** @typedef {import("./text_accessibility.js").TextAccessibilityManager} TextAccessibilityManager */
|
||||
|
||||
/**
|
||||
* @interface
|
||||
|
@ -162,6 +164,7 @@ class IPDFTextLayerFactory {
|
|||
* @property {boolean} [enhanceTextSelection]
|
||||
* @property {EventBus} eventBus
|
||||
* @property {TextHighlighter} highlighter
|
||||
* @property {TextAccessibilityManager} [accessibilityManager]
|
||||
*/
|
||||
|
||||
/**
|
||||
|
@ -175,6 +178,7 @@ class IPDFTextLayerFactory {
|
|||
enhanceTextSelection = false,
|
||||
eventBus,
|
||||
highlighter,
|
||||
accessibilityManager,
|
||||
}) {}
|
||||
}
|
||||
|
||||
|
@ -199,6 +203,7 @@ class IPDFAnnotationLayerFactory {
|
|||
* [fieldObjectsPromise]
|
||||
* @property {Map<string, HTMLCanvasElement>} [annotationCanvasMap] - Map some
|
||||
* annotation ids with canvases used to render them.
|
||||
* @property {TextAccessibilityManager} [accessibilityManager]
|
||||
*/
|
||||
|
||||
/**
|
||||
|
@ -217,6 +222,7 @@ class IPDFAnnotationLayerFactory {
|
|||
mouseState = null,
|
||||
fieldObjectsPromise = null,
|
||||
annotationCanvasMap = null,
|
||||
accessibilityManager = null,
|
||||
}) {}
|
||||
}
|
||||
|
||||
|
@ -231,6 +237,7 @@ class IPDFAnnotationEditorLayerFactory {
|
|||
* @property {PDFPageProxy} pdfPage
|
||||
* @property {IL10n} l10n
|
||||
* @property {AnnotationStorage} [annotationStorage] - Storage for annotation
|
||||
* @property {TextAccessibilityManager} [accessibilityManager]
|
||||
* data in forms.
|
||||
*/
|
||||
|
||||
|
@ -244,6 +251,7 @@ class IPDFAnnotationEditorLayerFactory {
|
|||
pdfPage,
|
||||
l10n,
|
||||
annotationStorage = null,
|
||||
accessibilityManager,
|
||||
}) {}
|
||||
}
|
||||
|
||||
|
|
|
@ -51,6 +51,7 @@ import {
|
|||
} from "./ui_utils.js";
|
||||
import { compatibilityParams } from "./app_options.js";
|
||||
import { NullL10n } from "./l10n_utils.js";
|
||||
import { TextAccessibilityManager } from "./text_accessibility.js";
|
||||
|
||||
/**
|
||||
* @typedef {Object} PDFPageViewOptions
|
||||
|
@ -697,6 +698,7 @@ class PDFPageView {
|
|||
|
||||
let textLayer = null;
|
||||
if (this.textLayerMode !== TextLayerMode.DISABLE && this.textLayerFactory) {
|
||||
this._accessibilityManager ||= new TextAccessibilityManager();
|
||||
const textLayerDiv = document.createElement("div");
|
||||
textLayerDiv.className = "textLayer";
|
||||
textLayerDiv.style.width = canvasWrapper.style.width;
|
||||
|
@ -716,6 +718,7 @@ class PDFPageView {
|
|||
this.textLayerMode === TextLayerMode.ENABLE_ENHANCE,
|
||||
eventBus: this.eventBus,
|
||||
highlighter: this.textHighlighter,
|
||||
accessibilityManager: this._accessibilityManager,
|
||||
});
|
||||
}
|
||||
this.textLayer = textLayer;
|
||||
|
@ -733,6 +736,7 @@ class PDFPageView {
|
|||
renderForms: this.#annotationMode === AnnotationMode.ENABLE_FORMS,
|
||||
l10n: this.l10n,
|
||||
annotationCanvasMap: this._annotationCanvasMap,
|
||||
accessibilityManager: this._accessibilityManager,
|
||||
});
|
||||
}
|
||||
|
||||
|
@ -824,6 +828,7 @@ class PDFPageView {
|
|||
pageDiv: div,
|
||||
pdfPage,
|
||||
l10n: this.l10n,
|
||||
accessibilityManager: this._accessibilityManager,
|
||||
}
|
||||
);
|
||||
this._renderAnnotationEditorLayer();
|
||||
|
|
246
web/text_accessibility.js
Normal file
246
web/text_accessibility.js
Normal file
|
@ -0,0 +1,246 @@
|
|||
/* Copyright 2022 Mozilla Foundation
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
import { binarySearchFirstItem } from "pdfjs-lib";
|
||||
|
||||
/**
|
||||
* This class aims to provide some methods:
|
||||
* - to reorder elements in the DOM with respect to the visual order;
|
||||
* - to create a link, using aria-owns, between spans in the textLayer and
|
||||
* annotations in the annotationLayer. The goal is to help to know
|
||||
* where the annotations are in the text flow.
|
||||
*/
|
||||
class TextAccessibilityManager {
|
||||
#enabled = false;
|
||||
|
||||
#textChildren = null;
|
||||
|
||||
#textNodes = new Map();
|
||||
|
||||
#waitingElements = new Map();
|
||||
|
||||
setTextMapping(textDivs) {
|
||||
this.#textChildren = textDivs;
|
||||
}
|
||||
|
||||
/**
|
||||
* Compare the positions of two elements, it must correspond to
|
||||
* the visual ordering.
|
||||
*
|
||||
* @param {HTMLElement} e1
|
||||
* @param {HTMLElement} e2
|
||||
* @returns {number}
|
||||
*/
|
||||
static #compareElementPositions(e1, e2) {
|
||||
const rect1 = e1.getBoundingClientRect();
|
||||
const rect2 = e2.getBoundingClientRect();
|
||||
|
||||
if (rect1.width === 0 && rect1.height === 0) {
|
||||
return +1;
|
||||
}
|
||||
|
||||
if (rect2.width === 0 && rect2.height === 0) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
const top1 = rect1.y;
|
||||
const bot1 = rect1.y + rect1.height;
|
||||
const mid1 = rect1.y + rect1.height / 2;
|
||||
|
||||
const top2 = rect2.y;
|
||||
const bot2 = rect2.y + rect2.height;
|
||||
const mid2 = rect2.y + rect2.height / 2;
|
||||
|
||||
if (mid1 <= top2 && mid2 >= bot1) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (mid2 <= top1 && mid1 >= bot2) {
|
||||
return +1;
|
||||
}
|
||||
|
||||
const centerX1 = rect1.x + rect1.width / 2;
|
||||
const centerX2 = rect2.x + rect2.width / 2;
|
||||
|
||||
return centerX1 - centerX2;
|
||||
}
|
||||
|
||||
/**
|
||||
* Function called when the text layer has finished rendering.
|
||||
*/
|
||||
enable() {
|
||||
if (this.#enabled) {
|
||||
throw new Error("TextAccessibilityManager is already enabled.");
|
||||
}
|
||||
if (!this.#textChildren) {
|
||||
throw new Error("Text divs and strings have not been set.");
|
||||
}
|
||||
|
||||
this.#enabled = true;
|
||||
this.#textChildren = this.#textChildren.slice();
|
||||
this.#textChildren.sort(TextAccessibilityManager.#compareElementPositions);
|
||||
|
||||
if (this.#textNodes.size > 0) {
|
||||
// Some links have been made before this manager has been disabled, hence
|
||||
// we restore them.
|
||||
const textChildren = this.#textChildren;
|
||||
for (const [id, nodeIndex] of this.#textNodes) {
|
||||
this.#addIdToAriaOwns(id, textChildren[nodeIndex]);
|
||||
}
|
||||
}
|
||||
|
||||
for (const [element, isRemovable] of this.#waitingElements) {
|
||||
this.addPointerInTextLayer(element, isRemovable);
|
||||
}
|
||||
this.#waitingElements.clear();
|
||||
}
|
||||
|
||||
disable() {
|
||||
if (!this.#enabled) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Don't clear this.#textNodes which is used to rebuild the aria-owns
|
||||
// in case it's re-enabled at some point.
|
||||
|
||||
this.#waitingElements.clear();
|
||||
this.#textChildren = null;
|
||||
this.#enabled = false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove an aria-owns id from a node in the text layer.
|
||||
* @param {HTMLElement} element
|
||||
*/
|
||||
removePointerInTextLayer(element) {
|
||||
if (!this.#enabled) {
|
||||
this.#waitingElements.delete(element);
|
||||
return;
|
||||
}
|
||||
|
||||
const children = this.#textChildren;
|
||||
if (!children || children.length === 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
const { id } = element;
|
||||
const nodeIndex = this.#textNodes.get(id);
|
||||
if (nodeIndex === undefined) {
|
||||
return;
|
||||
}
|
||||
|
||||
const node = children[nodeIndex];
|
||||
|
||||
this.#textNodes.delete(id);
|
||||
let owns = node.getAttribute("aria-owns");
|
||||
if (owns?.includes(id)) {
|
||||
owns = owns
|
||||
.split(" ")
|
||||
.filter(x => x !== id)
|
||||
.join(" ");
|
||||
if (owns) {
|
||||
node.setAttribute("aria-owns", owns);
|
||||
} else {
|
||||
node.removeAttribute("aria-owns");
|
||||
node.setAttribute("role", "presentation");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#addIdToAriaOwns(id, node) {
|
||||
const owns = node.getAttribute("aria-owns");
|
||||
if (!owns?.includes(id)) {
|
||||
node.setAttribute("aria-owns", owns ? `${owns} ${id}` : id);
|
||||
}
|
||||
node.removeAttribute("role");
|
||||
}
|
||||
|
||||
/**
|
||||
* Find the text node which is the nearest and add an aria-owns attribute
|
||||
* in order to correctly position this editor in the text flow.
|
||||
* @param {HTMLElement} element
|
||||
* @param {boolean} isRemovable
|
||||
*/
|
||||
addPointerInTextLayer(element, isRemovable) {
|
||||
const { id } = element;
|
||||
if (!id) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!this.#enabled) {
|
||||
// The text layer needs to be there, so we postpone the association.
|
||||
this.#waitingElements.set(element, isRemovable);
|
||||
return;
|
||||
}
|
||||
|
||||
if (isRemovable) {
|
||||
this.removePointerInTextLayer(element);
|
||||
}
|
||||
|
||||
const children = this.#textChildren;
|
||||
if (!children || children.length === 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
const index = binarySearchFirstItem(
|
||||
children,
|
||||
node =>
|
||||
TextAccessibilityManager.#compareElementPositions(element, node) < 0
|
||||
);
|
||||
|
||||
const nodeIndex = Math.max(0, index - 1);
|
||||
this.#addIdToAriaOwns(id, children[nodeIndex]);
|
||||
this.#textNodes.set(id, nodeIndex);
|
||||
}
|
||||
|
||||
/**
|
||||
* Move a div in the DOM in order to respect the visual order.
|
||||
* @param {HTMLDivElement} element
|
||||
*/
|
||||
moveElementInDOM(container, element, contentElement, isRemovable) {
|
||||
this.addPointerInTextLayer(contentElement, isRemovable);
|
||||
|
||||
if (!container.hasChildNodes()) {
|
||||
container.append(element);
|
||||
return;
|
||||
}
|
||||
|
||||
const children = Array.from(container.childNodes).filter(
|
||||
node => node !== element
|
||||
);
|
||||
|
||||
if (children.length === 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
const elementToCompare = contentElement || element;
|
||||
const index = binarySearchFirstItem(
|
||||
children,
|
||||
node =>
|
||||
TextAccessibilityManager.#compareElementPositions(
|
||||
elementToCompare,
|
||||
node
|
||||
) < 0
|
||||
);
|
||||
|
||||
if (index === 0) {
|
||||
children[0].before(element);
|
||||
} else {
|
||||
children[index - 1].after(element);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export { TextAccessibilityManager };
|
|
@ -17,6 +17,8 @@
|
|||
/** @typedef {import("../src/display/display_utils").PageViewport} PageViewport */
|
||||
/** @typedef {import("./event_utils").EventBus} EventBus */
|
||||
/** @typedef {import("./text_highlighter").TextHighlighter} TextHighlighter */
|
||||
// eslint-disable-next-line max-len
|
||||
/** @typedef {import("./text_accessibility.js").TextAccessibilityManager} TextAccessibilityManager */
|
||||
|
||||
import { renderTextLayer } from "pdfjs-lib";
|
||||
|
||||
|
@ -32,6 +34,7 @@ const EXPAND_DIVS_TIMEOUT = 300; // ms
|
|||
* highlighting text from the find controller.
|
||||
* @property {boolean} enhanceTextSelection - Option to turn on improved
|
||||
* text selection.
|
||||
* @property {TextAccessibilityManager} [accessibilityManager]
|
||||
*/
|
||||
|
||||
/**
|
||||
|
@ -47,6 +50,7 @@ class TextLayerBuilder {
|
|||
viewport,
|
||||
highlighter = null,
|
||||
enhanceTextSelection = false,
|
||||
accessibilityManager = null,
|
||||
}) {
|
||||
this.textLayerDiv = textLayerDiv;
|
||||
this.eventBus = eventBus;
|
||||
|
@ -60,6 +64,7 @@ class TextLayerBuilder {
|
|||
this.textLayerRenderTask = null;
|
||||
this.highlighter = highlighter;
|
||||
this.enhanceTextSelection = enhanceTextSelection;
|
||||
this.accessibilityManager = accessibilityManager;
|
||||
|
||||
this._bindMouse();
|
||||
}
|
||||
|
@ -97,6 +102,7 @@ class TextLayerBuilder {
|
|||
|
||||
this.textDivs.length = 0;
|
||||
this.highlighter?.setTextMapping(this.textDivs, this.textContentItemsStr);
|
||||
this.accessibilityManager?.setTextMapping(this.textDivs);
|
||||
|
||||
const textLayerFrag = document.createDocumentFragment();
|
||||
this.textLayerRenderTask = renderTextLayer({
|
||||
|
@ -114,6 +120,7 @@ class TextLayerBuilder {
|
|||
this.textLayerDiv.append(textLayerFrag);
|
||||
this._finishRendering();
|
||||
this.highlighter?.enable();
|
||||
this.accessibilityManager?.enable();
|
||||
},
|
||||
function (reason) {
|
||||
// Cancelled or failed to render text layer; skipping errors.
|
||||
|
@ -130,6 +137,7 @@ class TextLayerBuilder {
|
|||
this.textLayerRenderTask = null;
|
||||
}
|
||||
this.highlighter?.disable();
|
||||
this.accessibilityManager?.disable();
|
||||
}
|
||||
|
||||
setTextContentStream(readableStream) {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue