Merge pull request #11198 from Snuffleupagus/PDFSidebarResizer-fixes
[PDFSidebarResizer] Add a couple of (small) readability improvements in the code
This commit is contained in:
commit
0786363b7c
@ -13,7 +13,7 @@
|
|||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import { NullL10n } from './ui_utils';
|
import { clamp, NullL10n } from './ui_utils';
|
||||||
|
|
||||||
const SIDEBAR_WIDTH_VAR = '--sidebar-width';
|
const SIDEBAR_WIDTH_VAR = '--sidebar-width';
|
||||||
const SIDEBAR_MIN_WIDTH = 200; // pixels
|
const SIDEBAR_MIN_WIDTH = 200; // pixels
|
||||||
@ -47,8 +47,10 @@ class PDFSidebarResizer {
|
|||||||
this.eventBus = eventBus;
|
this.eventBus = eventBus;
|
||||||
this.l10n = l10n;
|
this.l10n = l10n;
|
||||||
|
|
||||||
if (typeof CSS === 'undefined' || typeof CSS.supports !== 'function' ||
|
if ((typeof PDFJSDev === 'undefined' || !PDFJSDev.test('MOZCENTRAL')) &&
|
||||||
!CSS.supports(SIDEBAR_WIDTH_VAR, `calc(-1 * ${SIDEBAR_MIN_WIDTH}px)`)) {
|
(typeof CSS === 'undefined' || typeof CSS.supports !== 'function' ||
|
||||||
|
!CSS.supports(SIDEBAR_WIDTH_VAR,
|
||||||
|
`calc(-1 * ${SIDEBAR_MIN_WIDTH}px)`))) {
|
||||||
console.warn('PDFSidebarResizer: ' +
|
console.warn('PDFSidebarResizer: ' +
|
||||||
'The browser does not support resizing of the sidebar.');
|
'The browser does not support resizing of the sidebar.');
|
||||||
return;
|
return;
|
||||||
@ -82,19 +84,14 @@ class PDFSidebarResizer {
|
|||||||
}
|
}
|
||||||
// Prevent the sidebar from becoming too narrow, or from occupying more
|
// Prevent the sidebar from becoming too narrow, or from occupying more
|
||||||
// than half of the available viewer width.
|
// than half of the available viewer width.
|
||||||
const maxWidth = Math.floor(this.outerContainerWidth / 2);
|
const newWidth = clamp(width, SIDEBAR_MIN_WIDTH,
|
||||||
if (width > maxWidth) {
|
Math.floor(this.outerContainerWidth / 2));
|
||||||
width = maxWidth;
|
|
||||||
}
|
|
||||||
if (width < SIDEBAR_MIN_WIDTH) {
|
|
||||||
width = SIDEBAR_MIN_WIDTH;
|
|
||||||
}
|
|
||||||
// Only update the UI when the sidebar width did in fact change.
|
// Only update the UI when the sidebar width did in fact change.
|
||||||
if (width === this._width) {
|
if (newWidth === this._width) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
this._width = width;
|
this._width = newWidth;
|
||||||
this.doc.style.setProperty(SIDEBAR_WIDTH_VAR, `${width}px`);
|
this.doc.style.setProperty(SIDEBAR_WIDTH_VAR, `${newWidth}px`);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -154,30 +151,33 @@ class PDFSidebarResizer {
|
|||||||
this.eventBus.on('resize', (evt) => {
|
this.eventBus.on('resize', (evt) => {
|
||||||
// When the *entire* viewer is resized, such that it becomes narrower,
|
// When the *entire* viewer is resized, such that it becomes narrower,
|
||||||
// ensure that the sidebar doesn't end up being too wide.
|
// ensure that the sidebar doesn't end up being too wide.
|
||||||
if (evt && evt.source === window) {
|
if (!evt || evt.source !== window) {
|
||||||
// Always reset the cached width when the viewer is resized.
|
return;
|
||||||
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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
// 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, });
|
||||||
|
}
|
||||||
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -898,6 +898,7 @@ export {
|
|||||||
NullL10n,
|
NullL10n,
|
||||||
EventBus,
|
EventBus,
|
||||||
getGlobalEventBus,
|
getGlobalEventBus,
|
||||||
|
clamp,
|
||||||
ProgressBar,
|
ProgressBar,
|
||||||
getPDFFileNameFromURL,
|
getPDFFileNameFromURL,
|
||||||
noContextMenuHandler,
|
noContextMenuHandler,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user