Skip to content

Commit

Permalink
fixup! Enable automatic URL linking
Browse files Browse the repository at this point in the history
  • Loading branch information
ryzokuken committed Jan 29, 2025
1 parent 2c22395 commit d648990
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 28 deletions.
7 changes: 7 additions & 0 deletions src/display/annotation_layer.js
Original file line number Diff line number Diff line change
Expand Up @@ -3264,6 +3264,13 @@ class AnnotationLayer {
this.#setAnnotationCanvasMap();
}

/**
* Add link annotations to the annotation layer.
*
* @param {AddLinkAnnotationsParameters} annotations
* @param {IPDFLinkService} linkService
* @memberof AnnotationLayer
*/
async addLinkAnnotations(annotations, linkService) {
const elementParams = {
data: null,
Expand Down
40 changes: 30 additions & 10 deletions web/annotation_layer_builder.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,13 @@ import { PresentationModeState } from "./ui_utils.js";
* @property {StructTreeLayerBuilder} [structTreeLayer]
*/

/**
* @typedef {Object} InjectLinkAnnotationsOptions
* @property {Array<Object>} inferredLinks
* @property {PageViewport} viewport
* @property {StructTreeLayerBuilder} [structTreeLayer]
*/

class AnnotationLayerBuilder {
#annotations = null;

Expand Down Expand Up @@ -135,6 +142,9 @@ class AnnotationLayerBuilder {

// Create an annotation layer div and render the annotations
// if there is at least one annotation.
const div = (this.div = document.createElement("div"));
div.className = "annotationLayer";
this.#onAppend?.(div);

if (annotations.length === 0) {
this.hide();
Expand Down Expand Up @@ -176,18 +186,14 @@ class AnnotationLayerBuilder {
}

#initAnnotationLayer(viewport, structTreeLayer) {
const div = (this.div = document.createElement("div"));
div.className = "annotationLayer";
this.#onAppend?.(div);

this.annotationLayer = new AnnotationLayer({
div,
div: this.div,
accessibilityManager: this._accessibilityManager,
annotationCanvasMap: this._annotationCanvasMap,
annotationEditorUIManager: this._annotationEditorUIManager,
page: this.pdfPage,
viewport: viewport.clone({ dontFlip: true }),
structTreeLayer: structTreeLayer || null,
structTreeLayer,
});
}

Expand All @@ -209,12 +215,25 @@ class AnnotationLayerBuilder {
return !!this.annotationLayer?.hasEditableAnnotations();
}

async injectLinkAnnotations(inferredLinks, viewport, structTreeLayer) {
const uniqueLinks = this.#annotations
/**
* @param {InjectLinkAnnotationsOptions} options
* @returns {Promise<void>} A promise that is resolved when the inferred links
* are added to the annotation layer.
*/
async injectLinkAnnotations({
inferredLinks,
viewport,
structTreeLayer = null,
}) {
if (this._cancelled) {
return;
}

const newLinks = this.#annotations
? this.#checkInferredLinks(inferredLinks)
: inferredLinks;

if (!uniqueLinks.length) {
if (!newLinks.length) {
return;
}

Expand All @@ -223,7 +242,8 @@ class AnnotationLayerBuilder {
setLayerDimensions(this.annotationLayer.div, viewport);
}

this.annotationLayer.addLinkAnnotations(uniqueLinks, this.linkService);
await this.annotationLayer.addLinkAnnotations(newLinks, this.linkService);
this.div.hidden = false;
}

#updatePresentationModeState(state) {
Expand Down
17 changes: 4 additions & 13 deletions web/autolinker.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,21 +75,12 @@ function calculateLinkPosition(range, pdfPageView) {
}

function createLinkAnnotation({ url, index, length }, pdfPageView, id) {
const convertedMatch = pdfPageView._textHighlighter._convertMatches(
[index],
[length]
)[0];
const highlighter = pdfPageView._textHighlighter;
const [{ begin, end }] = highlighter._convertMatches([index], [length]);

const range = new Range();
range.setStart(
pdfPageView._textHighlighter.textDivs[convertedMatch.begin.divIdx]
.firstChild,
convertedMatch.begin.offset
);
range.setEnd(
pdfPageView._textHighlighter.textDivs[convertedMatch.end.divIdx].firstChild,
convertedMatch.end.offset
);
range.setStart(highlighter.textDivs[begin.divIdx].firstChild, begin.offset);
range.setEnd(highlighter.textDivs[end.divIdx].firstChild, end.offset);

return {
id: `added_link_${id}`,
Expand Down
10 changes: 5 additions & 5 deletions web/pdf_page_view.js
Original file line number Diff line number Diff line change
Expand Up @@ -1112,11 +1112,11 @@ class PDFPageView {
await this.#renderAnnotationLayer();
if (this.#enableAutoLinking) {
await textLayerPromise;
this.annotationLayer.injectLinkAnnotations(
Autolinker.processLinks(this),
this.viewport,
this.structTreeLayer
);
this.annotationLayer.injectLinkAnnotations({
inferredLinks: Autolinker.processLinks(this),
viewport: this.viewport,
structTreeLayer: this.structTreeLayer,
});
}
}

Expand Down

0 comments on commit d648990

Please sign in to comment.