mirror of
https://github.com/squidfunk/mkdocs-material.git
synced 2026-07-29 09:12:35 -04:00
Reduced pressure on browser with ResizeObserver
This commit is contained in:
@@ -24,3 +24,4 @@ export * from "./_"
|
||||
export * from "./focus"
|
||||
export * from "./offset"
|
||||
export * from "./select"
|
||||
export * from "./size"
|
||||
|
||||
79
src/assets/javascripts/browser/element/size/index.ts
Normal file
79
src/assets/javascripts/browser/element/size/index.ts
Normal file
@@ -0,0 +1,79 @@
|
||||
/*
|
||||
* Copyright (c) 2016-2020 Martin Donath <martin.donath@squidfunk.com>
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to
|
||||
* deal in the Software without restriction, including without limitation the
|
||||
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
|
||||
* sell copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
|
||||
* IN THE SOFTWARE.
|
||||
*/
|
||||
|
||||
import { Observable, fromEventPattern } from "rxjs"
|
||||
import { shareReplay, startWith } from "rxjs/operators"
|
||||
|
||||
/* ----------------------------------------------------------------------------
|
||||
* Types
|
||||
* ------------------------------------------------------------------------- */
|
||||
|
||||
/**
|
||||
* Element offset
|
||||
*/
|
||||
export interface ElementSize {
|
||||
width: number /* Element width */
|
||||
height: number /* Element height */
|
||||
}
|
||||
|
||||
/* ----------------------------------------------------------------------------
|
||||
* Functions
|
||||
* ------------------------------------------------------------------------- */
|
||||
|
||||
/**
|
||||
* Retrieve element size
|
||||
*
|
||||
* @param el - Element
|
||||
*
|
||||
* @return Element size
|
||||
*/
|
||||
export function getElementSize(el: HTMLElement): ElementSize {
|
||||
return {
|
||||
width: el.offsetWidth,
|
||||
height: el.offsetHeight
|
||||
}
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------------- */
|
||||
|
||||
/**
|
||||
* Watch element size
|
||||
*
|
||||
* @param el - Element
|
||||
*
|
||||
* @return Element size observable
|
||||
*/
|
||||
export function watchElementSize(
|
||||
el: HTMLElement
|
||||
): Observable<ElementSize> {
|
||||
return fromEventPattern<ElementSize>(next => {
|
||||
new ResizeObserver(([{ contentRect }]) => next({
|
||||
width: Math.round(contentRect.width),
|
||||
height: Math.round(contentRect.height)
|
||||
}))
|
||||
.observe(el, { box: "border-box" }) // TODO: centralize, .observe and .unobserve
|
||||
})
|
||||
.pipe(
|
||||
startWith(getElementSize(el)),
|
||||
shareReplay(1)
|
||||
)
|
||||
}
|
||||
@@ -88,11 +88,11 @@ export function watchWorker<T extends WorkerMessage>(
|
||||
): Observable<T> {
|
||||
|
||||
/* Intercept messages from worker-like objects */
|
||||
const rx$ = fromEventPattern<Event>(next =>
|
||||
const rx$ = fromEventPattern<MessageEvent>(next =>
|
||||
worker.addEventListener("message", next)
|
||||
)
|
||||
.pipe(
|
||||
pluck<Event, T>("data")
|
||||
pluck<MessageEvent, T>("data")
|
||||
)
|
||||
|
||||
/* Send and receive messages, return hot observable */
|
||||
|
||||
Reference in New Issue
Block a user