diff --git a/web/pdf_sidebar_resizer.js b/web/pdf_sidebar_resizer.js index 384be23f7..71d557ffe 100644 --- a/web/pdf_sidebar_resizer.js +++ b/web/pdf_sidebar_resizer.js @@ -13,7 +13,7 @@ * limitations under the License. */ -import { NullL10n } from './ui_utils'; +import { clamp, NullL10n } from './ui_utils'; const SIDEBAR_WIDTH_VAR = '--sidebar-width'; const SIDEBAR_MIN_WIDTH = 200; // pixels @@ -47,8 +47,10 @@ class PDFSidebarResizer { this.eventBus = eventBus; this.l10n = l10n; - if (typeof CSS === 'undefined' || typeof CSS.supports !== 'function' || - !CSS.supports(SIDEBAR_WIDTH_VAR, `calc(-1 * ${SIDEBAR_MIN_WIDTH}px)`)) { + if ((typeof PDFJSDev === 'undefined' || !PDFJSDev.test('MOZCENTRAL')) && + (typeof CSS === 'undefined' || typeof CSS.supports !== 'function' || + !CSS.supports(SIDEBAR_WIDTH_VAR, + `calc(-1 * ${SIDEBAR_MIN_WIDTH}px)`))) { console.warn('PDFSidebarResizer: ' + 'The browser does not support resizing of the sidebar.'); return; @@ -82,19 +84,14 @@ class PDFSidebarResizer { } // Prevent the sidebar from becoming too narrow, or from occupying more // than half of the available viewer width. - const maxWidth = Math.floor(this.outerContainerWidth / 2); - if (width > maxWidth) { - width = maxWidth; - } - if (width < SIDEBAR_MIN_WIDTH) { - width = SIDEBAR_MIN_WIDTH; - } + const newWidth = clamp(width, SIDEBAR_MIN_WIDTH, + Math.floor(this.outerContainerWidth / 2)); // Only update the UI when the sidebar width did in fact change. - if (width === this._width) { + if (newWidth === this._width) { return false; } - this._width = width; - this.doc.style.setProperty(SIDEBAR_WIDTH_VAR, `${width}px`); + this._width = newWidth; + this.doc.style.setProperty(SIDEBAR_WIDTH_VAR, `${newWidth}px`); return true; } @@ -154,30 +151,33 @@ class PDFSidebarResizer { this.eventBus.on('resize', (evt) => { // When the *entire* viewer is resized, such that it becomes narrower, // ensure that the sidebar doesn't end up being too wide. - if (evt && evt.source === window) { - // Always reset the cached width when the viewer is resized. - this._outerContainerWidth = null; - - if (this._width) { - // NOTE: If the sidebar is closed, we don't need to worry about - // visual glitches nor ensure that rendering is triggered. - if (this.sidebarOpen) { - this.outerContainer.classList.add(SIDEBAR_RESIZING_CLASS); - let updated = this._updateWidth(this._width); - - Promise.resolve().then(() => { - this.outerContainer.classList.remove(SIDEBAR_RESIZING_CLASS); - // Trigger rendering if the sidebar width changed, to avoid - // depending on the order in which 'resize' events are handled. - if (updated) { - this.eventBus.dispatch('resize', { source: this, }); - } - }); - } else { - this._updateWidth(this._width); - } - } + if (!evt || evt.source !== window) { + return; } + // Always reset the cached width when the viewer is resized. + this._outerContainerWidth = null; + + if (!this._width) { + // The sidebar hasn't been resized, hence no need to adjust its width. + return; + } + // NOTE: If the sidebar is closed, we don't need to worry about + // visual glitches nor ensure that rendering is triggered. + if (!this.sidebarOpen) { + this._updateWidth(this._width); + return; + } + this.outerContainer.classList.add(SIDEBAR_RESIZING_CLASS); + let updated = this._updateWidth(this._width); + + Promise.resolve().then(() => { + this.outerContainer.classList.remove(SIDEBAR_RESIZING_CLASS); + // Trigger rendering if the sidebar width changed, to avoid + // depending on the order in which 'resize' events are handled. + if (updated) { + this.eventBus.dispatch('resize', { source: this, }); + } + }); }); } } diff --git a/web/ui_utils.js b/web/ui_utils.js index e1235759a..cb7108a75 100644 --- a/web/ui_utils.js +++ b/web/ui_utils.js @@ -898,6 +898,7 @@ export { NullL10n, EventBus, getGlobalEventBus, + clamp, ProgressBar, getPDFFileNameFromURL, noContextMenuHandler,