Re-name and re-factor the PDFLinkService.navigateTo
method
This modernizes and improves the code, by using `async`/`await` and by extracting the helper function to its own method. To hopefully avoid confusion, given the next patch, the method is also re-named to `goToDestination` to make is slightly clearer what it actually does.
This commit is contained in:
parent
4b4ac8a13d
commit
8431cfe482
@ -344,7 +344,7 @@ class LinkAnnotationElement extends AnnotationElement {
|
|||||||
link.href = this.linkService.getDestinationHash(destination);
|
link.href = this.linkService.getDestinationHash(destination);
|
||||||
link.onclick = () => {
|
link.onclick = () => {
|
||||||
if (destination) {
|
if (destination) {
|
||||||
this.linkService.navigateTo(destination);
|
this.linkService.goToDestination(destination);
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
};
|
};
|
||||||
|
@ -54,9 +54,9 @@ class IPDFLinkService {
|
|||||||
set externalLinkEnabled(value) {}
|
set externalLinkEnabled(value) {}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param dest - The PDF destination object.
|
* @param {string|Array} dest - The named, or explicit, PDF destination.
|
||||||
*/
|
*/
|
||||||
navigateTo(dest) {}
|
async goToDestination(dest) {}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param dest - The PDF destination object.
|
* @param dest - The PDF destination object.
|
||||||
|
@ -647,7 +647,7 @@ class PDFHistory {
|
|||||||
this.linkService.rotation = destination.rotation;
|
this.linkService.rotation = destination.rotation;
|
||||||
}
|
}
|
||||||
if (destination.dest) {
|
if (destination.dest) {
|
||||||
this.linkService.navigateTo(destination.dest);
|
this.linkService.goToDestination(destination.dest);
|
||||||
} else if (destination.hash) {
|
} else if (destination.hash) {
|
||||||
this.linkService.setHash(destination.hash);
|
this.linkService.setHash(destination.hash);
|
||||||
} else if (destination.page) {
|
} else if (destination.page) {
|
||||||
@ -655,7 +655,7 @@ class PDFHistory {
|
|||||||
this.linkService.page = destination.page;
|
this.linkService.page = destination.page;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Since `PDFLinkService.navigateTo` is asynchronous, we thus defer the
|
// Since `PDFLinkService.goToDestination` is asynchronous, we thus defer the
|
||||||
// resetting of `this._popStateInProgress` slightly.
|
// resetting of `this._popStateInProgress` slightly.
|
||||||
Promise.resolve().then(() => {
|
Promise.resolve().then(() => {
|
||||||
this._popStateInProgress = false;
|
this._popStateInProgress = false;
|
||||||
|
@ -108,10 +108,19 @@ class PDFLinkService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param {string|Array} dest - The named, or explicit, PDF destination.
|
* @deprecated
|
||||||
*/
|
*/
|
||||||
navigateTo(dest) {
|
navigateTo(dest) {
|
||||||
const goToDestination = ({ namedDest, explicitDest }) => {
|
console.error(
|
||||||
|
"Deprecated method: `navigateTo`, use `goToDestination` instead."
|
||||||
|
);
|
||||||
|
this.goToDestination(dest);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @private
|
||||||
|
*/
|
||||||
|
_goToDestinationHelper(rawDest, namedDest = null, explicitDest) {
|
||||||
// Dest array looks like that: <page-ref> </XYZ|/FitXXX> <args..>
|
// Dest array looks like that: <page-ref> </XYZ|/FitXXX> <args..>
|
||||||
const destRef = explicitDest[0];
|
const destRef = explicitDest[0];
|
||||||
let pageNumber;
|
let pageNumber;
|
||||||
@ -126,12 +135,12 @@ class PDFLinkService {
|
|||||||
.getPageIndex(destRef)
|
.getPageIndex(destRef)
|
||||||
.then(pageIndex => {
|
.then(pageIndex => {
|
||||||
this.cachePageRef(pageIndex + 1, destRef);
|
this.cachePageRef(pageIndex + 1, destRef);
|
||||||
goToDestination({ namedDest, explicitDest });
|
this._goToDestinationHelper(rawDest, namedDest, explicitDest);
|
||||||
})
|
})
|
||||||
.catch(() => {
|
.catch(() => {
|
||||||
console.error(
|
console.error(
|
||||||
`PDFLinkService.navigateTo: "${destRef}" is not ` +
|
`PDFLinkService._goToDestinationHelper: "${destRef}" is not ` +
|
||||||
`a valid page reference, for dest="${dest}".`
|
`a valid page reference, for dest="${rawDest}".`
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
return;
|
return;
|
||||||
@ -140,15 +149,15 @@ class PDFLinkService {
|
|||||||
pageNumber = destRef + 1;
|
pageNumber = destRef + 1;
|
||||||
} else {
|
} else {
|
||||||
console.error(
|
console.error(
|
||||||
`PDFLinkService.navigateTo: "${destRef}" is not ` +
|
`PDFLinkService._goToDestinationHelper: "${destRef}" is not ` +
|
||||||
`a valid destination reference, for dest="${dest}".`
|
`a valid destination reference, for dest="${rawDest}".`
|
||||||
);
|
);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (!pageNumber || pageNumber < 1 || pageNumber > this.pagesCount) {
|
if (!pageNumber || pageNumber < 1 || pageNumber > this.pagesCount) {
|
||||||
console.error(
|
console.error(
|
||||||
`PDFLinkService.navigateTo: "${pageNumber}" is not ` +
|
`PDFLinkService._goToDestinationHelper: "${pageNumber}" is not ` +
|
||||||
`a valid page number, for dest="${dest}".`
|
`a valid page number, for dest="${rawDest}".`
|
||||||
);
|
);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@ -165,32 +174,30 @@ class PDFLinkService {
|
|||||||
destArray: explicitDest,
|
destArray: explicitDest,
|
||||||
ignoreDestinationZoom: this._ignoreDestinationZoom,
|
ignoreDestinationZoom: this._ignoreDestinationZoom,
|
||||||
});
|
});
|
||||||
};
|
|
||||||
|
|
||||||
new Promise((resolve, reject) => {
|
|
||||||
if (typeof dest === "string") {
|
|
||||||
this.pdfDocument.getDestination(dest).then(destArray => {
|
|
||||||
resolve({
|
|
||||||
namedDest: dest,
|
|
||||||
explicitDest: destArray,
|
|
||||||
});
|
|
||||||
});
|
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
resolve({
|
|
||||||
namedDest: "",
|
/**
|
||||||
explicitDest: dest,
|
* This method will, when available, also update the browser history.
|
||||||
});
|
*
|
||||||
}).then(data => {
|
* @param {string|Array} dest - The named, or explicit, PDF destination.
|
||||||
if (!Array.isArray(data.explicitDest)) {
|
*/
|
||||||
|
async goToDestination(dest) {
|
||||||
|
let namedDest, explicitDest;
|
||||||
|
if (typeof dest === "string") {
|
||||||
|
namedDest = dest;
|
||||||
|
explicitDest = await this.pdfDocument.getDestination(dest);
|
||||||
|
} else {
|
||||||
|
namedDest = null;
|
||||||
|
explicitDest = await dest;
|
||||||
|
}
|
||||||
|
if (!Array.isArray(explicitDest)) {
|
||||||
console.error(
|
console.error(
|
||||||
`PDFLinkService.navigateTo: "${data.explicitDest}" is` +
|
`PDFLinkService.goToDestination: "${explicitDest}" is not ` +
|
||||||
` not a valid destination array, for dest="${dest}".`
|
`a valid destination array, for dest="${dest}".`
|
||||||
);
|
);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
goToDestination(data);
|
this._goToDestinationHelper(dest, namedDest, explicitDest);
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -307,7 +314,7 @@ class PDFLinkService {
|
|||||||
// Ensure that this parameter is *always* handled last, in order to
|
// Ensure that this parameter is *always* handled last, in order to
|
||||||
// guarantee that it won't be overridden (e.g. by the "page" parameter).
|
// guarantee that it won't be overridden (e.g. by the "page" parameter).
|
||||||
if ("nameddest" in params) {
|
if ("nameddest" in params) {
|
||||||
this.navigateTo(params.nameddest);
|
this.goToDestination(params.nameddest);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// Named (or explicit) destination.
|
// Named (or explicit) destination.
|
||||||
@ -323,7 +330,7 @@ class PDFLinkService {
|
|||||||
} catch (ex) {}
|
} catch (ex) {}
|
||||||
|
|
||||||
if (typeof dest === "string" || isValidExplicitDestination(dest)) {
|
if (typeof dest === "string" || isValidExplicitDestination(dest)) {
|
||||||
this.navigateTo(dest);
|
this.goToDestination(dest);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
console.error(
|
console.error(
|
||||||
@ -394,6 +401,9 @@ class PDFLinkService {
|
|||||||
this._pagesRefCache[refStr] = pageNum;
|
this._pagesRefCache[refStr] = pageNum;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @private
|
||||||
|
*/
|
||||||
_cachedPageNumber(pageRef) {
|
_cachedPageNumber(pageRef) {
|
||||||
const refStr =
|
const refStr =
|
||||||
pageRef.gen === 0 ? `${pageRef.num}R` : `${pageRef.num}R${pageRef.gen}`;
|
pageRef.gen === 0 ? `${pageRef.num}R` : `${pageRef.num}R${pageRef.gen}`;
|
||||||
@ -510,9 +520,9 @@ class SimpleLinkService {
|
|||||||
set rotation(value) {}
|
set rotation(value) {}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param dest - The PDF destination object.
|
* @param {string|Array} dest - The named, or explicit, PDF destination.
|
||||||
*/
|
*/
|
||||||
navigateTo(dest) {}
|
async goToDestination(dest) {}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param dest - The PDF destination object.
|
* @param dest - The PDF destination object.
|
||||||
|
@ -73,7 +73,7 @@ class PDFOutlineViewer extends BaseTreeViewer {
|
|||||||
element.href = linkService.getDestinationHash(dest);
|
element.href = linkService.getDestinationHash(dest);
|
||||||
element.onclick = () => {
|
element.onclick = () => {
|
||||||
if (dest) {
|
if (dest) {
|
||||||
linkService.navigateTo(dest);
|
linkService.goToDestination(dest);
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
};
|
};
|
||||||
|
Loading…
Reference in New Issue
Block a user