Vanessa 2023-03-20 15:56:24 +08:00
parent 7ad5cdd067
commit 23f2af3501
57 changed files with 83865 additions and 2979 deletions

View file

@ -13,8 +13,6 @@
* limitations under the License.
*/
/** @typedef {import("../src/display/api").PDFPageProxy} PDFPageProxy */
const PDF_ROLE_TO_HTML_ROLE = {
// Document level structure types
Document: null, // There's a "document" role, but it doesn't make sense here.
@ -73,24 +71,35 @@ const PDF_ROLE_TO_HTML_ROLE = {
const HEADING_PATTERN = /^H(\d+)$/;
/**
* @typedef {Object} StructTreeLayerBuilderOptions
* @property {PDFPageProxy} pdfPage
*/
class StructTreeLayerBuilder {
/**
* @param {StructTreeLayerBuilderOptions} options
*/
constructor({ pdfPage }) {
this.pdfPage = pdfPage;
#treeDom = undefined;
get renderingDone() {
return this.#treeDom !== undefined;
}
render(structTree) {
return this._walk(structTree);
if (this.#treeDom !== undefined) {
return this.#treeDom;
}
const treeDom = this.#walk(structTree);
treeDom?.classList.add("structTree");
return (this.#treeDom = treeDom);
}
_setAttributes(structElement, htmlElement) {
hide() {
if (this.#treeDom && !this.#treeDom.hidden) {
this.#treeDom.hidden = true;
}
}
show() {
if (this.#treeDom?.hidden) {
this.#treeDom.hidden = false;
}
}
#setAttributes(structElement, htmlElement) {
if (structElement.alt !== undefined) {
htmlElement.setAttribute("aria-label", structElement.alt);
}
@ -102,7 +111,7 @@ class StructTreeLayerBuilder {
}
}
_walk(node) {
#walk(node) {
if (!node) {
return null;
}
@ -119,16 +128,16 @@ class StructTreeLayerBuilder {
}
}
this._setAttributes(node, element);
this.#setAttributes(node, element);
if (node.children) {
if (node.children.length === 1 && "id" in node.children[0]) {
// Often there is only one content node so just set the values on the
// parent node to avoid creating an extra span.
this._setAttributes(node.children[0], element);
this.#setAttributes(node.children[0], element);
} else {
for (const kid of node.children) {
element.append(this._walk(kid));
element.append(this.#walk(kid));
}
}
}