Skip to content

Commit 3052e99

Browse files
Merge pull request #18013 from Snuffleupagus/SimpleLinkService-extends-PDFLinkService
Re-factor `SimpleLinkService` to extend `PDFLinkService`
2 parents 2b2ade7 + 8c3d571 commit 3052e99

File tree

1 file changed

+56
-159
lines changed

1 file changed

+56
-159
lines changed

web/pdf_link_service.js

+56-159
Original file line numberDiff line numberDiff line change
@@ -28,57 +28,6 @@ const LinkTarget = {
2828
TOP: 4,
2929
};
3030

31-
/**
32-
* @typedef {Object} ExternalLinkParameters
33-
* @property {string} url - An absolute URL.
34-
* @property {LinkTarget} [target] - The link target. The default value is
35-
* `LinkTarget.NONE`.
36-
* @property {string} [rel] - The link relationship. The default value is
37-
* `DEFAULT_LINK_REL`.
38-
* @property {boolean} [enabled] - Whether the link should be enabled. The
39-
* default value is true.
40-
*/
41-
42-
/**
43-
* Adds various attributes (href, title, target, rel) to hyperlinks.
44-
* @param {HTMLAnchorElement} link - The link element.
45-
* @param {ExternalLinkParameters} params
46-
*/
47-
function addLinkAttributes(link, { url, target, rel, enabled = true } = {}) {
48-
if (!url || typeof url !== "string") {
49-
throw new Error('A valid "url" parameter must provided.');
50-
}
51-
52-
if (enabled) {
53-
link.href = link.title = url;
54-
} else {
55-
link.href = "";
56-
link.title = `Disabled: ${url}`;
57-
link.onclick = () => false;
58-
}
59-
60-
let targetStr = ""; // LinkTarget.NONE
61-
switch (target) {
62-
case LinkTarget.NONE:
63-
break;
64-
case LinkTarget.SELF:
65-
targetStr = "_self";
66-
break;
67-
case LinkTarget.BLANK:
68-
targetStr = "_blank";
69-
break;
70-
case LinkTarget.PARENT:
71-
targetStr = "_parent";
72-
break;
73-
case LinkTarget.TOP:
74-
targetStr = "_top";
75-
break;
76-
}
77-
link.target = targetStr;
78-
79-
link.rel = typeof rel === "string" ? rel : DEFAULT_LINK_REL;
80-
}
81-
8231
/**
8332
* @typedef {Object} PDFLinkServiceOptions
8433
* @property {EventBus} eventBus - The application event bus.
@@ -98,6 +47,8 @@ function addLinkAttributes(link, { url, target, rel, enabled = true } = {}) {
9847
* @implements {IPDFLinkService}
9948
*/
10049
class PDFLinkService {
50+
externalLinkEnabled = true;
51+
10152
#pagesRefCache = new Map();
10253

10354
/**
@@ -112,7 +63,6 @@ class PDFLinkService {
11263
this.eventBus = eventBus;
11364
this.externalLinkTarget = externalLinkTarget;
11465
this.externalLinkRel = externalLinkRel;
115-
this.externalLinkEnabled = true;
11666
this._ignoreDestinationZoom = ignoreDestinationZoom;
11767

11868
this.baseUrl = null;
@@ -146,35 +96,39 @@ class PDFLinkService {
14696
* @type {number}
14797
*/
14898
get page() {
149-
return this.pdfViewer.currentPageNumber;
99+
return this.pdfDocument ? this.pdfViewer.currentPageNumber : 1;
150100
}
151101

152102
/**
153103
* @param {number} value
154104
*/
155105
set page(value) {
156-
this.pdfViewer.currentPageNumber = value;
106+
if (this.pdfDocument) {
107+
this.pdfViewer.currentPageNumber = value;
108+
}
157109
}
158110

159111
/**
160112
* @type {number}
161113
*/
162114
get rotation() {
163-
return this.pdfViewer.pagesRotation;
115+
return this.pdfDocument ? this.pdfViewer.pagesRotation : 0;
164116
}
165117

166118
/**
167119
* @param {number} value
168120
*/
169121
set rotation(value) {
170-
this.pdfViewer.pagesRotation = value;
122+
if (this.pdfDocument) {
123+
this.pdfViewer.pagesRotation = value;
124+
}
171125
}
172126

173127
/**
174128
* @type {boolean}
175129
*/
176130
get isInPresentationMode() {
177-
return this.pdfViewer.isInPresentationMode;
131+
return this.pdfDocument ? this.pdfViewer.isInPresentationMode : false;
178132
}
179133

180134
#goToDestinationHelper(rawDest, namedDest = null, explicitDest) {
@@ -294,18 +248,46 @@ class PDFLinkService {
294248
}
295249

296250
/**
297-
* Wrapper around the `addLinkAttributes` helper function.
251+
* Adds various attributes (href, title, target, rel) to hyperlinks.
298252
* @param {HTMLAnchorElement} link
299253
* @param {string} url
300254
* @param {boolean} [newWindow]
301255
*/
302256
addLinkAttributes(link, url, newWindow = false) {
303-
addLinkAttributes(link, {
304-
url,
305-
target: newWindow ? LinkTarget.BLANK : this.externalLinkTarget,
306-
rel: this.externalLinkRel,
307-
enabled: this.externalLinkEnabled,
308-
});
257+
if (!url || typeof url !== "string") {
258+
throw new Error('A valid "url" parameter must provided.');
259+
}
260+
const target = newWindow ? LinkTarget.BLANK : this.externalLinkTarget,
261+
rel = this.externalLinkRel;
262+
263+
if (this.externalLinkEnabled) {
264+
link.href = link.title = url;
265+
} else {
266+
link.href = "";
267+
link.title = `Disabled: ${url}`;
268+
link.onclick = () => false;
269+
}
270+
271+
let targetStr = ""; // LinkTarget.NONE
272+
switch (target) {
273+
case LinkTarget.NONE:
274+
break;
275+
case LinkTarget.SELF:
276+
targetStr = "_self";
277+
break;
278+
case LinkTarget.BLANK:
279+
targetStr = "_blank";
280+
break;
281+
case LinkTarget.PARENT:
282+
targetStr = "_parent";
283+
break;
284+
case LinkTarget.TOP:
285+
targetStr = "_top";
286+
break;
287+
}
288+
link.target = targetStr;
289+
290+
link.rel = typeof rel === "string" ? rel : DEFAULT_LINK_REL;
309291
}
310292

311293
/**
@@ -467,6 +449,9 @@ class PDFLinkService {
467449
* @param {string} action
468450
*/
469451
executeNamedAction(action) {
452+
if (!this.pdfDocument) {
453+
return;
454+
}
470455
// See PDF reference, table 8.45 - Named action
471456
switch (action) {
472457
case "GoBack":
@@ -507,9 +492,11 @@ class PDFLinkService {
507492
* @param {Object} action
508493
*/
509494
async executeSetOCGState(action) {
510-
const pdfDocument = this.pdfDocument;
511-
const optionalContentConfig =
512-
await this.pdfViewer.optionalContentConfigPromise;
495+
if (!this.pdfDocument) {
496+
return;
497+
}
498+
const pdfDocument = this.pdfDocument,
499+
optionalContentConfig = await this.pdfViewer.optionalContentConfigPromise;
513500

514501
if (pdfDocument !== this.pdfDocument) {
515502
return; // The document was closed while the optional content resolved.
@@ -603,98 +590,8 @@ class PDFLinkService {
603590
/**
604591
* @implements {IPDFLinkService}
605592
*/
606-
class SimpleLinkService {
607-
constructor() {
608-
this.externalLinkEnabled = true;
609-
}
610-
611-
/**
612-
* @type {number}
613-
*/
614-
get pagesCount() {
615-
return 0;
616-
}
617-
618-
/**
619-
* @type {number}
620-
*/
621-
get page() {
622-
return 0;
623-
}
624-
625-
/**
626-
* @param {number} value
627-
*/
628-
set page(value) {}
629-
630-
/**
631-
* @type {number}
632-
*/
633-
get rotation() {
634-
return 0;
635-
}
636-
637-
/**
638-
* @param {number} value
639-
*/
640-
set rotation(value) {}
641-
642-
/**
643-
* @type {boolean}
644-
*/
645-
get isInPresentationMode() {
646-
return false;
647-
}
648-
649-
/**
650-
* @param {string|Array} dest - The named, or explicit, PDF destination.
651-
*/
652-
async goToDestination(dest) {}
653-
654-
/**
655-
* @param {number|string} val - The page number, or page label.
656-
*/
657-
goToPage(val) {}
658-
659-
/**
660-
* @param {HTMLAnchorElement} link
661-
* @param {string} url
662-
* @param {boolean} [newWindow]
663-
*/
664-
addLinkAttributes(link, url, newWindow = false) {
665-
addLinkAttributes(link, { url, enabled: this.externalLinkEnabled });
666-
}
667-
668-
/**
669-
* @param dest - The PDF destination object.
670-
* @returns {string} The hyperlink to the PDF object.
671-
*/
672-
getDestinationHash(dest) {
673-
return "#";
674-
}
675-
676-
/**
677-
* @param hash - The PDF parameters/hash.
678-
* @returns {string} The hyperlink to the PDF object.
679-
*/
680-
getAnchorUrl(hash) {
681-
return "#";
682-
}
683-
684-
/**
685-
* @param {string} hash
686-
*/
687-
setHash(hash) {}
688-
689-
/**
690-
* @param {string} action
691-
*/
692-
executeNamedAction(action) {}
693-
694-
/**
695-
* @param {Object} action
696-
*/
697-
executeSetOCGState(action) {}
593+
class SimpleLinkService extends PDFLinkService {
594+
setDocument(pdfDocument, baseUrl = null) {}
698595

699596
/**
700597
* @param {number} pageNum - page number.

0 commit comments

Comments
 (0)