optimized charts rendering
This commit is contained in:
@@ -0,0 +1,58 @@
|
||||
import { Directive, ElementRef, inject } from '@angular/core';
|
||||
|
||||
const PASSIVE_WHEEL_SELECTOR = '[data-passive-wheel]';
|
||||
let wheelPatchInstalled = false;
|
||||
|
||||
/**
|
||||
* Chrome flags non-passive `wheel`/`mousewheel` listeners as scroll-blocking.
|
||||
* zrender (ECharts) mounts them on the chart's inner viewport without listener
|
||||
* options. The hierarchy tree only uses `roam: 'move'` and never calls
|
||||
* `preventDefault()` on wheel events, so marking them passive is safe.
|
||||
*
|
||||
* The patch is global but only affects wheel listeners inside an element that
|
||||
* carries this directive.
|
||||
*/
|
||||
function installPassiveWheelPatch(): void {
|
||||
if (wheelPatchInstalled) return;
|
||||
wheelPatchInstalled = true;
|
||||
|
||||
const nativeAddEventListener = EventTarget.prototype.addEventListener;
|
||||
|
||||
EventTarget.prototype.addEventListener = function (
|
||||
this: EventTarget,
|
||||
type: string,
|
||||
listener: EventListenerOrEventListenerObject | null,
|
||||
options?: boolean | AddEventListenerOptions,
|
||||
): void {
|
||||
const isWheelEvent = type === 'wheel' || type === 'mousewheel';
|
||||
const hasOptions = options !== undefined && typeof options !== 'boolean';
|
||||
|
||||
if (
|
||||
isWheelEvent &&
|
||||
!hasOptions &&
|
||||
this instanceof Element &&
|
||||
this.closest(PASSIVE_WHEEL_SELECTOR)
|
||||
) {
|
||||
nativeAddEventListener.call(this, type, listener, {
|
||||
capture: options === true,
|
||||
passive: true,
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
nativeAddEventListener.call(this, type, listener, options);
|
||||
};
|
||||
}
|
||||
|
||||
@Directive({
|
||||
selector: '[appPassiveWheel]',
|
||||
standalone: true,
|
||||
})
|
||||
export class PassiveWheelDirective {
|
||||
private readonly host = inject<ElementRef<HTMLElement>>(ElementRef);
|
||||
|
||||
constructor() {
|
||||
this.host.nativeElement.setAttribute('data-passive-wheel', '');
|
||||
installPassiveWheelPatch();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user