mirror of
https://github.com/mozilla/pdf.js.git
synced 2025-04-20 15:18:08 +02:00
[api-minor] Move addLinkAttributes
and LinkTarget
into the viewer
As part of the changes/improvement in PR 14092, we're no longer using the `addLinkAttributes` directly in e.g. the AnnotationLayer-code. Given that the helper function is now *only* used in the viewer, hence it no longer seems necessary to expose it through the official API. *Please note:* It seems somewhat unlikely that third-party users were relying *directly* on the helper function, which is why it's not being exported as part of the viewer components. (If necessary, we can always change this later on.)
This commit is contained in:
parent
290cbc5232
commit
2d2b6463b8
5 changed files with 111 additions and 124 deletions
|
@ -46,7 +46,6 @@ import {
|
|||
GlobalWorkerOptions,
|
||||
InvalidPDFException,
|
||||
isPdfFile,
|
||||
LinkTarget,
|
||||
loadScript,
|
||||
MissingPDFException,
|
||||
OPS,
|
||||
|
@ -57,6 +56,7 @@ import {
|
|||
version,
|
||||
} from "pdfjs-lib";
|
||||
import { CursorTool, PDFCursorTools } from "./pdf_cursor_tools.js";
|
||||
import { LinkTarget, PDFLinkService } from "./pdf_link_service.js";
|
||||
import { OverlayManager } from "./overlay_manager.js";
|
||||
import { PasswordPrompt } from "./password_prompt.js";
|
||||
import { PDFAttachmentViewer } from "./pdf_attachment_viewer.js";
|
||||
|
@ -65,7 +65,6 @@ import { PDFFindBar } from "./pdf_find_bar.js";
|
|||
import { PDFFindController } from "./pdf_find_controller.js";
|
||||
import { PDFHistory } from "./pdf_history.js";
|
||||
import { PDFLayerViewer } from "./pdf_layer_viewer.js";
|
||||
import { PDFLinkService } from "./pdf_link_service.js";
|
||||
import { PDFOutlineViewer } from "./pdf_outline_viewer.js";
|
||||
import { PDFPresentationMode } from "./pdf_presentation_mode.js";
|
||||
import { PDFRenderingQueue } from "./pdf_rendering_queue.js";
|
||||
|
|
|
@ -16,8 +16,73 @@
|
|||
/** @typedef {import("./event_utils").EventBus} EventBus */
|
||||
/** @typedef {import("./interfaces").IPDFLinkService} IPDFLinkService */
|
||||
|
||||
import { addLinkAttributes, LinkTarget } from "pdfjs-lib";
|
||||
import { parseQueryString } from "./ui_utils.js";
|
||||
import { removeNullCharacters } from "pdfjs-lib";
|
||||
|
||||
const DEFAULT_LINK_REL = "noopener noreferrer nofollow";
|
||||
|
||||
const LinkTarget = {
|
||||
NONE: 0, // Default value.
|
||||
SELF: 1,
|
||||
BLANK: 2,
|
||||
PARENT: 3,
|
||||
TOP: 4,
|
||||
};
|
||||
|
||||
/**
|
||||
* @typedef ExternalLinkParameters
|
||||
* @typedef {Object} ExternalLinkParameters
|
||||
* @property {string} url - An absolute URL.
|
||||
* @property {LinkTarget} [target] - The link target. The default value is
|
||||
* `LinkTarget.NONE`.
|
||||
* @property {string} [rel] - The link relationship. The default value is
|
||||
* `DEFAULT_LINK_REL`.
|
||||
* @property {boolean} [enabled] - Whether the link should be enabled. The
|
||||
* default value is true.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Adds various attributes (href, title, target, rel) to hyperlinks.
|
||||
* @param {HTMLAnchorElement} link - The link element.
|
||||
* @param {ExternalLinkParameters} params
|
||||
*/
|
||||
function addLinkAttributes(link, { url, target, rel, enabled = true } = {}) {
|
||||
if (!url || typeof url !== "string") {
|
||||
throw new Error('A valid "url" parameter must provided.');
|
||||
}
|
||||
|
||||
const urlNullRemoved = removeNullCharacters(url);
|
||||
if (enabled) {
|
||||
link.href = link.title = urlNullRemoved;
|
||||
} else {
|
||||
link.href = "";
|
||||
link.title = `Disabled: ${urlNullRemoved}`;
|
||||
link.onclick = () => {
|
||||
return false;
|
||||
};
|
||||
}
|
||||
|
||||
let targetStr = ""; // LinkTarget.NONE
|
||||
switch (target) {
|
||||
case LinkTarget.NONE:
|
||||
break;
|
||||
case LinkTarget.SELF:
|
||||
targetStr = "_self";
|
||||
break;
|
||||
case LinkTarget.BLANK:
|
||||
targetStr = "_blank";
|
||||
break;
|
||||
case LinkTarget.PARENT:
|
||||
targetStr = "_parent";
|
||||
break;
|
||||
case LinkTarget.TOP:
|
||||
targetStr = "_top";
|
||||
break;
|
||||
}
|
||||
link.target = targetStr;
|
||||
|
||||
link.rel = typeof rel === "string" ? rel : DEFAULT_LINK_REL;
|
||||
}
|
||||
|
||||
/**
|
||||
* @typedef {Object} PDFLinkServiceOptions
|
||||
|
@ -230,7 +295,7 @@ class PDFLinkService {
|
|||
}
|
||||
|
||||
/**
|
||||
* Wrapper around the `addLinkAttributes`-function in the API.
|
||||
* Wrapper around the `addLinkAttributes` helper function.
|
||||
* @param {HTMLAnchorElement} link
|
||||
* @param {string} url
|
||||
* @param {boolean} [newWindow]
|
||||
|
@ -634,4 +699,4 @@ class SimpleLinkService {
|
|||
}
|
||||
}
|
||||
|
||||
export { PDFLinkService, SimpleLinkService };
|
||||
export { LinkTarget, PDFLinkService, SimpleLinkService };
|
||||
|
|
|
@ -19,7 +19,11 @@ import {
|
|||
DefaultTextLayerFactory,
|
||||
DefaultXfaLayerFactory,
|
||||
} from "./default_factory.js";
|
||||
import { PDFLinkService, SimpleLinkService } from "./pdf_link_service.js";
|
||||
import {
|
||||
LinkTarget,
|
||||
PDFLinkService,
|
||||
SimpleLinkService,
|
||||
} from "./pdf_link_service.js";
|
||||
import { PDFSinglePageViewer, PDFViewer } from "./pdf_viewer.js";
|
||||
import { AnnotationLayerBuilder } from "./annotation_layer_builder.js";
|
||||
import { DownloadManager } from "./download_manager.js";
|
||||
|
@ -49,6 +53,7 @@ export {
|
|||
DownloadManager,
|
||||
EventBus,
|
||||
GenericL10n,
|
||||
LinkTarget,
|
||||
NullL10n,
|
||||
PDFFindController,
|
||||
PDFHistory,
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue