[PDFSidebarResizer] Refactor the clamping in _updateWidth

Rather than manually clamping the `width` here, we can just `export` an already existing helper function from `ui_utils.js` instead.

(Hopefully it will eventually be possible to replace this helper function with a native `Math.clamp` function, given that there exists a "Stage 1 Proposal" for adding such a thing to the ECMAScript specification.)
This commit is contained in:
Jonas Jenwald 2019-10-03 12:03:45 +02:00
parent c1cfe2881b
commit 9e4552d792
2 changed files with 7 additions and 11 deletions

View File

@ -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
@ -84,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;
} }

View File

@ -898,6 +898,7 @@ export {
NullL10n, NullL10n,
EventBus, EventBus,
getGlobalEventBus, getGlobalEventBus,
clamp,
ProgressBar, ProgressBar,
getPDFFileNameFromURL, getPDFFileNameFromURL,
noContextMenuHandler, noContextMenuHandler,