mirror of
https://github.com/mozilla/pdf.js.git
synced 2025-04-20 23:28:06 +02:00
Move the externalLinkTarget
and externalLinkRel
options to PDFLinkService
options
This removes the `PDFJS.externalLinkTarget`/`PDFJS.externalLinkRel` dependency from the viewer components, but please note that as a *temporary* solution the default viewer still uses it.
This commit is contained in:
parent
c45c394364
commit
3a6f6d23d6
8 changed files with 62 additions and 120 deletions
11
web/app.js
11
web/app.js
|
@ -22,7 +22,7 @@ import {
|
|||
} from './ui_utils';
|
||||
import {
|
||||
build, createBlob, getDocument, getFilenameFromUrl, InvalidPDFException,
|
||||
MissingPDFException, OPS, PDFJS, PDFWorker, shadow,
|
||||
LinkTarget, MissingPDFException, OPS, PDFJS, PDFWorker, shadow,
|
||||
UnexpectedResponseException, UNSUPPORTED_FEATURES, version
|
||||
} from 'pdfjs-lib';
|
||||
import { CursorTool, PDFCursorTools } from './pdf_cursor_tools';
|
||||
|
@ -184,10 +184,11 @@ let PDFViewerApplication = {
|
|||
this.eventBus.dispatch('localized');
|
||||
});
|
||||
|
||||
if (this.isViewerEmbedded && !PDFJS.isExternalLinkTargetSet()) {
|
||||
if (this.isViewerEmbedded &&
|
||||
PDFJS.externalLinkTarget === LinkTarget.NONE) {
|
||||
// Prevent external links from "replacing" the viewer,
|
||||
// when it's embedded in e.g. an iframe or an object.
|
||||
PDFJS.externalLinkTarget = PDFJS.LinkTarget.TOP;
|
||||
PDFJS.externalLinkTarget = LinkTarget.TOP;
|
||||
}
|
||||
|
||||
this.initialized = true;
|
||||
|
@ -250,7 +251,7 @@ let PDFViewerApplication = {
|
|||
PDFJS.useOnlyCssZoom = value;
|
||||
}),
|
||||
preferences.get('externalLinkTarget').then(function resolved(value) {
|
||||
if (PDFJS.isExternalLinkTargetSet()) {
|
||||
if (PDFJS.externalLinkTarget !== LinkTarget.NONE) {
|
||||
return;
|
||||
}
|
||||
PDFJS.externalLinkTarget = value;
|
||||
|
@ -378,6 +379,8 @@ let PDFViewerApplication = {
|
|||
|
||||
let pdfLinkService = new PDFLinkService({
|
||||
eventBus,
|
||||
externalLinkTarget: PDFJS.externalLinkTarget,
|
||||
externalLinkRel: PDFJS.externalLinkRel,
|
||||
});
|
||||
this.pdfLinkService = pdfLinkService;
|
||||
|
||||
|
|
|
@ -19,6 +19,11 @@ import { parseQueryString } from './ui_utils';
|
|||
/**
|
||||
* @typedef {Object} PDFLinkServiceOptions
|
||||
* @property {EventBus} eventBus - The application event bus.
|
||||
* @property {number} externalLinkTarget - (optional) Specifies the `target`
|
||||
* attribute for external links. Must use one of the values from {LinkTarget}.
|
||||
* Defaults to using no target.
|
||||
* @property {string} externalLinkRel - (optional) Specifies the `rel` attribute
|
||||
* for external links. Defaults to stripping the referrer.
|
||||
*/
|
||||
|
||||
/**
|
||||
|
@ -30,8 +35,12 @@ class PDFLinkService {
|
|||
/**
|
||||
* @param {PDFLinkServiceOptions} options
|
||||
*/
|
||||
constructor({ eventBus, } = {}) {
|
||||
constructor({ eventBus, externalLinkTarget = null,
|
||||
externalLinkRel = null, } = {}) {
|
||||
this.eventBus = eventBus || getGlobalEventBus();
|
||||
this.externalLinkTarget = externalLinkTarget;
|
||||
this.externalLinkRel = externalLinkRel;
|
||||
|
||||
this.baseUrl = null;
|
||||
this.pdfDocument = null;
|
||||
this.pdfViewer = null;
|
||||
|
@ -409,6 +418,11 @@ function isValidExplicitDestination(dest) {
|
|||
}
|
||||
|
||||
class SimpleLinkService {
|
||||
constructor() {
|
||||
this.externalLinkTarget = null;
|
||||
this.externalLinkRel = null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @returns {number}
|
||||
*/
|
||||
|
|
|
@ -13,9 +13,7 @@
|
|||
* limitations under the License.
|
||||
*/
|
||||
|
||||
import {
|
||||
addLinkAttributes, PDFJS, removeNullCharacters
|
||||
} from 'pdfjs-lib';
|
||||
import { addLinkAttributes, LinkTarget, removeNullCharacters } from 'pdfjs-lib';
|
||||
|
||||
const DEFAULT_TITLE = '\u2013';
|
||||
|
||||
|
@ -68,20 +66,22 @@ class PDFOutlineViewer {
|
|||
/**
|
||||
* @private
|
||||
*/
|
||||
_bindLink(element, item) {
|
||||
if (item.url) {
|
||||
_bindLink(element, { url, newWindow, dest, }) {
|
||||
let { linkService, } = this;
|
||||
|
||||
if (url) {
|
||||
addLinkAttributes(element, {
|
||||
url: item.url,
|
||||
target: (item.newWindow ? PDFJS.LinkTarget.BLANK : undefined),
|
||||
url,
|
||||
target: (newWindow ? LinkTarget.BLANK : linkService.externalLinkTarget),
|
||||
rel: linkService.externalLinkRel,
|
||||
});
|
||||
return;
|
||||
}
|
||||
let destination = item.dest;
|
||||
|
||||
element.href = this.linkService.getDestinationHash(destination);
|
||||
element.href = this.linkService.getDestinationHash(dest);
|
||||
element.onclick = () => {
|
||||
if (destination) {
|
||||
this.linkService.navigateTo(destination);
|
||||
if (dest) {
|
||||
this.linkService.navigateTo(dest);
|
||||
}
|
||||
return false;
|
||||
};
|
||||
|
@ -90,12 +90,12 @@ class PDFOutlineViewer {
|
|||
/**
|
||||
* @private
|
||||
*/
|
||||
_setStyles(element, item) {
|
||||
_setStyles(element, { bold, italic, }) {
|
||||
let styleStr = '';
|
||||
if (item.bold) {
|
||||
if (bold) {
|
||||
styleStr += 'font-weight: bold;';
|
||||
}
|
||||
if (item.italic) {
|
||||
if (italic) {
|
||||
styleStr += 'font-style: italic;';
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue