Merge pull request #14880 from Snuffleupagus/CSS--viewer-container-height

Introduce a `--viewer-container-height` CSS variable to simplify the code
This commit is contained in:
Tim van der Meij 2022-05-07 11:25:32 +02:00 committed by GitHub
commit 82353cf728
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 21 additions and 51 deletions

View File

@ -574,7 +574,6 @@ const PDFViewerApplication = {
this.secondaryToolbar = new SecondaryToolbar( this.secondaryToolbar = new SecondaryToolbar(
appConfig.secondaryToolbar, appConfig.secondaryToolbar,
container,
eventBus eventBus
); );
@ -2401,6 +2400,8 @@ function webViewerSpreadModeChanged(evt) {
function webViewerResize() { function webViewerResize() {
const { pdfDocument, pdfViewer } = PDFViewerApplication; const { pdfDocument, pdfViewer } = PDFViewerApplication;
pdfViewer.updateContainerHeightCss();
if (!pdfDocument) { if (!pdfDocument) {
return; return;
} }

View File

@ -300,6 +300,7 @@ class BaseViewer {
if (this.removePageBorders) { if (this.removePageBorders) {
this.viewer.classList.add("removePageBorders"); this.viewer.classList.add("removePageBorders");
} }
this.updateContainerHeightCss();
// Defer the dispatching of this event, to give other viewer components // Defer the dispatching of this event, to give other viewer components
// time to initialize *and* register 'baseviewerinit' event listeners. // time to initialize *and* register 'baseviewerinit' event listeners.
Promise.resolve().then(() => { Promise.resolve().then(() => {
@ -936,7 +937,6 @@ class BaseViewer {
if (this.isInPresentationMode) { if (this.isInPresentationMode) {
const dummyPage = document.createElement("div"); const dummyPage = document.createElement("div");
dummyPage.className = "dummyPage"; dummyPage.className = "dummyPage";
dummyPage.style.height = `${this.container.clientHeight}px`;
spread.appendChild(dummyPage); spread.appendChild(dummyPage);
} }
@ -996,14 +996,6 @@ class BaseViewer {
* only because of limited numerical precision. * only because of limited numerical precision.
*/ */
#isSameScale(newScale) { #isSameScale(newScale) {
if (
this.isInPresentationMode &&
this.container.clientHeight !== this.#previousContainerHeight
) {
// Ensure that the current page remains centered vertically if/when
// the window is resized while PresentationMode is active.
return false;
}
return ( return (
newScale === this._currentScale || newScale === this._currentScale ||
Math.abs(newScale - this._currentScale) < 1e-15 Math.abs(newScale - this._currentScale) < 1e-15
@ -1064,8 +1056,7 @@ class BaseViewer {
if (this.defaultRenderingQueue) { if (this.defaultRenderingQueue) {
this.update(); this.update();
} }
this.updateContainerHeightCss();
this.#previousContainerHeight = this.container.clientHeight;
} }
/** /**
@ -1098,8 +1089,7 @@ class BaseViewer {
hPadding = vPadding = 4; hPadding = vPadding = 4;
} else if (this.removePageBorders) { } else if (this.removePageBorders) {
hPadding = vPadding = 0; hPadding = vPadding = 0;
} } else if (this._scrollMode === ScrollMode.HORIZONTAL) {
if (this._scrollMode === ScrollMode.HORIZONTAL) {
[hPadding, vPadding] = [vPadding, hPadding]; // Swap the padding values. [hPadding, vPadding] = [vPadding, hPadding]; // Swap the padding values.
} }
const pageWidthScale = const pageWidthScale =
@ -2070,6 +2060,16 @@ class BaseViewer {
} while (--steps > 0 && newScale > MIN_SCALE); } while (--steps > 0 && newScale > MIN_SCALE);
this.currentScaleValue = newScale; this.currentScaleValue = newScale;
} }
updateContainerHeightCss() {
const height = this.container.clientHeight;
if (height !== this.#previousContainerHeight) {
this.#previousContainerHeight = height;
this._doc.style.setProperty("--viewer-container-height", `${height}px`);
}
}
} }
export { BaseViewer, PagesCountLimit, PDFPageViewBuffer }; export { BaseViewer, PagesCountLimit, PDFPageViewBuffer };

View File

@ -17,6 +17,7 @@
@import url(xfa_layer_builder.css); @import url(xfa_layer_builder.css);
:root { :root {
--viewer-container-height: 0;
--pdfViewer-padding-bottom: 0; --pdfViewer-padding-bottom: 0;
--page-margin: 1px auto -8px; --page-margin: 1px auto -8px;
--page-border: 9px solid transparent; --page-border: 9px solid transparent;
@ -57,7 +58,7 @@
.pdfViewer .dummyPage { .pdfViewer .dummyPage {
position: relative; position: relative;
width: 0; width: 0;
/* The height is set via JS, see `BaseViewer.#ensurePageViewVisible`. */ height: var(--viewer-container-height);
} }
.pdfViewer.removePageBorders .page { .pdfViewer.removePageBorders .page {

View File

@ -13,7 +13,7 @@
* limitations under the License. * limitations under the License.
*/ */
import { SCROLLBAR_PADDING, ScrollMode, SpreadMode } from "./ui_utils.js"; import { ScrollMode, SpreadMode } from "./ui_utils.js";
import { CursorTool } from "./pdf_cursor_tools.js"; import { CursorTool } from "./pdf_cursor_tools.js";
import { PagesCountLimit } from "./base_viewer.js"; import { PagesCountLimit } from "./base_viewer.js";
@ -22,9 +22,6 @@ import { PagesCountLimit } from "./base_viewer.js";
* @property {HTMLDivElement} toolbar - Container for the secondary toolbar. * @property {HTMLDivElement} toolbar - Container for the secondary toolbar.
* @property {HTMLButtonElement} toggleButton - Button to toggle the visibility * @property {HTMLButtonElement} toggleButton - Button to toggle the visibility
* of the secondary toolbar. * of the secondary toolbar.
* @property {HTMLDivElement} toolbarButtonContainer - Container where all the
* toolbar buttons are placed. The maximum height of the toolbar is controlled
* dynamically by adjusting the 'max-height' CSS property of this DOM element.
* @property {HTMLButtonElement} presentationModeButton - Button for entering * @property {HTMLButtonElement} presentationModeButton - Button for entering
* presentation mode. * presentation mode.
* @property {HTMLButtonElement} openFileButton - Button to open a file. * @property {HTMLButtonElement} openFileButton - Button to open a file.
@ -52,13 +49,11 @@ import { PagesCountLimit } from "./base_viewer.js";
class SecondaryToolbar { class SecondaryToolbar {
/** /**
* @param {SecondaryToolbarOptions} options * @param {SecondaryToolbarOptions} options
* @param {HTMLDivElement} mainContainer
* @param {EventBus} eventBus * @param {EventBus} eventBus
*/ */
constructor(options, mainContainer, eventBus) { constructor(options, eventBus) {
this.toolbar = options.toolbar; this.toolbar = options.toolbar;
this.toggleButton = options.toggleButton; this.toggleButton = options.toggleButton;
this.toolbarButtonContainer = options.toolbarButtonContainer;
this.buttons = [ this.buttons = [
{ {
element: options.presentationModeButton, element: options.presentationModeButton,
@ -154,12 +149,8 @@ class SecondaryToolbar {
pageRotateCcw: options.pageRotateCcwButton, pageRotateCcw: options.pageRotateCcwButton,
}; };
this.mainContainer = mainContainer;
this.eventBus = eventBus; this.eventBus = eventBus;
this.opened = false; this.opened = false;
this.containerHeight = null;
this.previousContainerHeight = null;
this.reset(); this.reset();
@ -169,9 +160,6 @@ class SecondaryToolbar {
this.#bindCursorToolsListener(options); this.#bindCursorToolsListener(options);
this.#bindScrollModeListener(options); this.#bindScrollModeListener(options);
this.#bindSpreadModeListener(options); this.#bindSpreadModeListener(options);
// Bind the event listener for adjusting the 'max-height' of the toolbar.
this.eventBus._on("resize", this.#setMaxHeight.bind(this));
} }
/** /**
@ -322,8 +310,6 @@ class SecondaryToolbar {
return; return;
} }
this.opened = true; this.opened = true;
this.#setMaxHeight();
this.toggleButton.classList.add("toggled"); this.toggleButton.classList.add("toggled");
this.toggleButton.setAttribute("aria-expanded", "true"); this.toggleButton.setAttribute("aria-expanded", "true");
this.toolbar.classList.remove("hidden"); this.toolbar.classList.remove("hidden");
@ -346,22 +332,6 @@ class SecondaryToolbar {
this.open(); this.open();
} }
} }
#setMaxHeight() {
if (!this.opened) {
return; // Only adjust the 'max-height' if the toolbar is visible.
}
this.containerHeight = this.mainContainer.clientHeight;
if (this.containerHeight === this.previousContainerHeight) {
return;
}
this.toolbarButtonContainer.style.maxHeight = `${
this.containerHeight - SCROLLBAR_PADDING
}px`;
this.previousContainerHeight = this.containerHeight;
}
} }
export { SecondaryToolbar }; export { SecondaryToolbar };

View File

@ -482,7 +482,8 @@ select {
#secondaryToolbarButtonContainer { #secondaryToolbarButtonContainer {
max-width: 220px; max-width: 220px;
max-height: 400px; min-height: 26px;
max-height: calc(var(--viewer-container-height) - 40px);
overflow-y: auto; overflow-y: auto;
margin-bottom: -4px; margin-bottom: -4px;
} }

View File

@ -100,9 +100,6 @@ function getViewerConfiguration() {
secondaryToolbar: { secondaryToolbar: {
toolbar: document.getElementById("secondaryToolbar"), toolbar: document.getElementById("secondaryToolbar"),
toggleButton: document.getElementById("secondaryToolbarToggle"), toggleButton: document.getElementById("secondaryToolbarToggle"),
toolbarButtonContainer: document.getElementById(
"secondaryToolbarButtonContainer"
),
presentationModeButton: document.getElementById( presentationModeButton: document.getElementById(
"secondaryPresentationMode" "secondaryPresentationMode"
), ),