Merge pull request #14219 from Snuffleupagus/getVisibleElements-ids

Let `getVisibleElements` return a Set containing the visible element `id`s
This commit is contained in:
Jonas Jenwald 2021-11-03 23:49:27 +01:00 committed by GitHub
commit 611627f5a1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 39 additions and 40 deletions

View File

@ -497,7 +497,8 @@ describe("ui_utils", function () {
// This is a reimplementation of getVisibleElements without the
// optimizations.
function slowGetVisibleElements(scroll, pages) {
const views = [];
const views = [],
ids = new Set();
const { scrollLeft, scrollTop } = scroll;
const scrollRight = scrollLeft + scroll.clientWidth;
const scrollBottom = scrollTop + scroll.clientHeight;
@ -535,9 +536,10 @@ describe("ui_utils", function () {
percent,
widthPercent: (fractionWidth * 100) | 0,
});
ids.add(view.id);
}
}
return { first: views[0], last: views[views.length - 1], views };
return { first: views[0], last: views[views.length - 1], views, ids };
}
// This function takes a fixed layout of pages and compares the system under
@ -699,6 +701,7 @@ describe("ui_utils", function () {
first: undefined,
last: undefined,
views: [],
ids: new Set(),
});
});
@ -715,6 +718,7 @@ describe("ui_utils", function () {
first: undefined,
last: undefined,
views: [],
ids: new Set(),
});
});

View File

@ -104,22 +104,19 @@ function PDFPageViewBuffer(size) {
data.shift().destroy();
}
};
/**
* After calling resize, the size of the buffer will be newSize. The optional
* parameter pagesToKeep is, if present, an array of pages to push to the back
* of the buffer, delaying their destruction. The size of pagesToKeep has no
* impact on the final size of the buffer; if pagesToKeep has length larger
* than newSize, some of those pages will be destroyed anyway.
* After calling resize, the size of the buffer will be `newSize`.
* The optional parameter `idsToKeep` is, if present, a Set of page-ids to
* push to the back of the buffer, delaying their destruction. The size of
* `idsToKeep` has no impact on the final size of the buffer; if `idsToKeep`
* is larger than `newSize`, some of those pages will be destroyed anyway.
*/
this.resize = function (newSize, pagesToKeep) {
this.resize = function (newSize, idsToKeep = null) {
size = newSize;
if (pagesToKeep) {
const pageIdsToKeep = new Set();
for (let i = 0, iMax = pagesToKeep.length; i < iMax; ++i) {
pageIdsToKeep.add(pagesToKeep[i].id);
}
if (idsToKeep) {
moveToEndOfArray(data, function (page) {
return pageIdsToKeep.has(page.id);
return idsToKeep.has(page.id);
});
}
while (data.length > size) {
@ -1145,7 +1142,7 @@ class BaseViewer {
return;
}
const newCacheSize = Math.max(DEFAULT_CACHE_SIZE, 2 * numVisiblePages + 1);
this._buffer.resize(newCacheSize, visiblePages);
this._buffer.resize(newCacheSize, visible.ids);
this.renderingQueue.renderHighestPriority(visible);
@ -1231,7 +1228,9 @@ class BaseViewer {
y: element.offsetTop + element.clientTop,
view: pageView,
};
return { first: view, last: view, views: [view] };
const ids = new Set([pageView.id]);
return { first: view, last: view, views: [view], ids };
}
_getVisiblePages() {
@ -1273,16 +1272,14 @@ class BaseViewer {
console.error(`isPageVisible: "${pageNumber}" is not a valid page.`);
return false;
}
return this._getVisiblePages().views.some(function (view) {
return view.id === pageNumber;
});
return this._getVisiblePages().ids.has(pageNumber);
}
/**
* @param {number} pageNumber
*/
isPageCached(pageNumber) {
if (!this.pdfDocument || !this._buffer) {
if (!this.pdfDocument) {
return false;
}
if (
@ -1296,9 +1293,6 @@ class BaseViewer {
return false;
}
const pageView = this._pages[pageNumber - 1];
if (!pageView) {
return false;
}
return this._buffer.has(pageView);
}

View File

@ -133,9 +133,13 @@ class PDFRenderingQueue {
// All the visible views have rendered; try to handle any "holes" in the
// page layout (can happen e.g. with spreadModes at higher zoom levels).
if (lastId - firstId + 1 > numVisible) {
const visibleIds = visible.ids;
for (let i = 1, ii = lastId - firstId; i < ii; i++) {
const holeId = scrolledDown ? firstId + i : lastId - i,
holeView = views[holeId - 1];
const holeId = scrolledDown ? firstId + i : lastId - i;
if (visibleIds.has(holeId)) {
continue;
}
const holeView = views[holeId - 1];
if (!this.isViewFinished(holeView)) {
return holeView;
}

View File

@ -99,26 +99,21 @@ class PDFThumbnailViewer {
// ... and add the highlight to the new thumbnail.
thumbnailView.div.classList.add(THUMBNAIL_SELECTED_CLASS);
}
const visibleThumbs = this._getVisibleThumbs();
const numVisibleThumbs = visibleThumbs.views.length;
const { first, last, views } = this._getVisibleThumbs();
// If the thumbnail isn't currently visible, scroll it into view.
if (numVisibleThumbs > 0) {
const first = visibleThumbs.first.id;
// Account for only one thumbnail being visible.
const last = numVisibleThumbs > 1 ? visibleThumbs.last.id : first;
if (views.length > 0) {
let shouldScroll = false;
if (pageNumber <= first || pageNumber >= last) {
if (pageNumber <= first.id || pageNumber >= last.id) {
shouldScroll = true;
} else {
visibleThumbs.views.some(function (view) {
if (view.id !== pageNumber) {
return false;
for (const { id, percent } of views) {
if (id !== pageNumber) {
continue;
}
shouldScroll = view.percent < 100;
return true;
});
shouldScroll = percent < 100;
break;
}
}
if (shouldScroll) {
scrollIntoView(thumbnailView.div, { top: THUMBNAIL_SCROLL_MARGIN });

View File

@ -473,6 +473,7 @@ function getVisibleElements({
}
const visible = [],
ids = new Set(),
numViews = views.length;
let firstVisibleElementInd = binarySearchFirstItem(
views,
@ -558,6 +559,7 @@ function getVisibleElements({
percent,
widthPercent: (fractionWidth * 100) | 0,
});
ids.add(view.id);
}
const first = visible[0],
@ -572,7 +574,7 @@ function getVisibleElements({
return a.id - b.id; // ensure stability
});
}
return { first, last, views: visible };
return { first, last, views: visible, ids };
}
/**