Telegram Web K with changes to work inside I2P https://web.telegram.i2p/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

228 lines
6.7 KiB

3 years ago
/*
* https://github.com/morethanwords/tweb
* Copyright (C) 2019-2021 Eduard Kuzmenko
* https://github.com/morethanwords/tweb/blob/master/LICENSE
*/
import rootScope from "../lib/rootScope";
import { IS_SAFARI } from "../environment/userAgent";
3 years ago
import { MOUNT_CLASS_TO } from "../config/debug";
import isInDOM from "../helpers/dom/isInDOM";
3 years ago
import RLottiePlayer from "../lib/rlottie/rlottiePlayer";
import indexOfAndSplice from "../helpers/array/indexOfAndSplice";
import forEachReverse from "../helpers/array/forEachReverse";
3 years ago
export interface AnimationItem {
el: HTMLElement,
group: string,
animation: RLottiePlayer | HTMLVideoElement
};
export class AnimationIntersector {
3 years ago
private observer: IntersectionObserver;
3 years ago
private visible: Set<AnimationItem> = new Set();
3 years ago
private overrideIdleGroups: Set<string>;
3 years ago
private byGroups: {[group: string]: AnimationItem[]} = {};
private lockedGroups: {[group: string]: true} = {};
private onlyOnePlayableGroup: string = '';
private intersectionLockedGroups: {[group: string]: true} = {};
private videosLocked = false;
constructor() {
this.observer = new IntersectionObserver((entries) => {
3 years ago
// if(rootScope.idle.isIDLE) return;
3 years ago
for(const entry of entries) {
const target = entry.target;
for(const group in this.byGroups) {
if(this.intersectionLockedGroups[group]) {
continue;
}
const player = this.byGroups[group].find(p => p.el === target);
if(player) {
if(entry.isIntersecting) {
this.visible.add(player);
this.checkAnimation(player, false);
} else {
this.visible.delete(player);
this.checkAnimation(player, true);
if(player.animation instanceof RLottiePlayer/* && player.animation.cachingDelta === 2 */) {
//console.warn('will clear cache', player);
player.animation.clearCache();
}
}
break;
}
}
}
});
3 years ago
this.overrideIdleGroups = new Set();
rootScope.addEventListener('media_play', ({doc}) => {
3 years ago
if(doc.type === 'round') {
this.videosLocked = true;
this.checkAnimations();
}
});
rootScope.addEventListener('media_pause', () => {
3 years ago
if(this.videosLocked) {
this.videosLocked = false;
this.checkAnimations();
}
});
}
3 years ago
public setOverrideIdleGroup(group: string, override: boolean) {
if(override) this.overrideIdleGroups.add(group);
else this.overrideIdleGroups.delete(group);
}
3 years ago
public getAnimations(element: HTMLElement) {
const found: AnimationItem[] = [];
for(const group in this.byGroups) {
for(const player of this.byGroups[group]) {
if(player.el === element) {
found.push(player);
}
}
}
return found;
}
public removeAnimation(player: AnimationItem) {
//console.log('destroy animation');
const {el, animation} = player;
animation.remove();
if(animation instanceof HTMLVideoElement && IS_SAFARI) {
3 years ago
setTimeout(() => { // TODO: очистка по очереди, а не все вместе с этим таймаутом
animation.src = '';
animation.load();
}, 1e3);
}
3 years ago
const group = this.byGroups[player.group];
if(group) {
indexOfAndSplice(group, player);
if(!group.length) {
delete this.byGroups[player.group];
}
3 years ago
}
this.observer.unobserve(el);
this.visible.delete(player);
}
public addAnimation(animation: RLottiePlayer | HTMLVideoElement, group = '') {
const player = {
el: animation instanceof RLottiePlayer ? animation.el : animation,
animation: animation,
group
};
if(animation instanceof RLottiePlayer) {
if(!rootScope.settings.stickers.loop && animation.loop) {
animation.loop = rootScope.settings.stickers.loop;
}
}
(this.byGroups[group] ?? (this.byGroups[group] = [])).push(player);
this.observer.observe(player.el);
}
public checkAnimations(blurred?: boolean, group?: string, destroy = false) {
3 years ago
// if(rootScope.idle.isIDLE) return;
3 years ago
3 years ago
if(group !== undefined && !this.byGroups[group]) {
3 years ago
//console.warn('no animation group:', group);
return;
}
3 years ago
const groups = group !== undefined /* && false */ ? [group] : Object.keys(this.byGroups);
3 years ago
for(const group of groups) {
const animations = this.byGroups[group];
3 years ago
forEachReverse(animations, (player) => {
3 years ago
this.checkAnimation(player, blurred, destroy);
});
}
}
public checkAnimation(player: AnimationItem, blurred = false, destroy = false) {
const {el, animation, group} = player;
//return;
if((destroy || (!isInDOM(el) && !this.lockedGroups[group]))/* && false */) {
this.removeAnimation(player);
return;
}
if(blurred || (this.onlyOnePlayableGroup && this.onlyOnePlayableGroup !== group) || (animation instanceof HTMLVideoElement && this.videosLocked)) {
if(!animation.paused) {
//console.warn('pause animation:', animation);
animation.pause();
}
} else if(animation.paused &&
this.visible.has(player) &&
animation.autoplay &&
3 years ago
(!this.onlyOnePlayableGroup || this.onlyOnePlayableGroup === group) &&
(!rootScope.idle.isIDLE || this.overrideIdleGroups.has(player.group))
3 years ago
) {
//console.warn('play animation:', animation);
animation.play();
}
}
public setOnlyOnePlayableGroup(group: string) {
this.onlyOnePlayableGroup = group;
}
public lockGroup(group: string) {
this.lockedGroups[group] = true;
}
public unlockGroup(group: string) {
delete this.lockedGroups[group];
this.checkAnimations(undefined, group);
}
public refreshGroup(group: string) {
const animations = this.byGroups[group];
if(animations && animations.length) {
animations.forEach(animation => {
this.observer.unobserve(animation.el);
});
window.requestAnimationFrame(() => {
animations.forEach(animation => {
this.observer.observe(animation.el);
});
});
}
}
public lockIntersectionGroup(group: string) {
this.intersectionLockedGroups[group] = true;
}
public unlockIntersectionGroup(group: string) {
delete this.intersectionLockedGroups[group];
this.refreshGroup(group);
}
}
const animationIntersector = new AnimationIntersector();
if(MOUNT_CLASS_TO) {
MOUNT_CLASS_TO.animationIntersector = animationIntersector;
}
3 years ago
export default animationIntersector;