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.

196 lines
6.1 KiB

2 years ago
/*
* https://github.com/morethanwords/tweb
* Copyright (C) 2019-2021 Eduard Kuzmenko
* https://github.com/morethanwords/tweb/blob/master/LICENSE
*/
import callbackify from "../../helpers/callbackify";
import { formatNumber } from "../../helpers/number";
import { MessagePeerReaction, ReactionCount } from "../../layer";
import appPeersManager from "../../lib/appManagers/appPeersManager";
2 years ago
import appReactionsManager from "../../lib/appManagers/appReactionsManager";
import RLottiePlayer from "../../lib/rlottie/rlottiePlayer";
import SetTransition from "../singleTransition";
import StackedAvatars from "../stackedAvatars";
import { wrapSticker, wrapStickerAnimation } from "../wrappers";
const CLASS_NAME = 'reaction';
const TAG_NAME = CLASS_NAME + '-element';
const REACTION_INLINE_SIZE = 14;
2 years ago
const REACTION_BLOCK_SIZE = 22;
2 years ago
export const REACTION_DISPLAY_INLINE_COUNTER_AT = 2;
export const REACTION_DISPLAY_BLOCK_COUNTER_AT = 4;
export type ReactionLayoutType = 'block' | 'inline';
export default class ReactionElement extends HTMLElement {
private type: ReactionLayoutType;
private counter: HTMLElement;
private stickerContainer: HTMLElement;
private stackedAvatars: StackedAvatars;
private canRenderAvatars: boolean;
private _reactionCount: ReactionCount;
constructor() {
super();
this.classList.add(CLASS_NAME);
}
public get reactionCount() {
return this._reactionCount;
}
public set reactionCount(reactionCount: ReactionCount) {
this._reactionCount = reactionCount;
}
public get count() {
return this.reactionCount.count;
}
public init(type: ReactionLayoutType) {
this.type = type;
this.classList.add(CLASS_NAME + '-' + type);
}
public setCanRenderAvatars(canRenderAvatars: boolean) {
this.canRenderAvatars = canRenderAvatars;
}
public render(doNotRenderSticker?: boolean) {
const hadStickerContainer = !!this.stickerContainer;
if(!hadStickerContainer) {
this.stickerContainer = document.createElement('div');
this.stickerContainer.classList.add(CLASS_NAME + '-sticker');
this.append(this.stickerContainer);
}
const reactionCount = this.reactionCount;
if(!doNotRenderSticker && !hadStickerContainer) {
const availableReaction = appReactionsManager.getReaction(reactionCount.reaction);
callbackify(availableReaction, (availableReaction) => {
if(!availableReaction.center_icon) {
this.stickerContainer.classList.add('is-static');
}
if(availableReaction.pFlags.inactive) {
this.classList.add('is-inactive');
}
2 years ago
const size = this.type === 'inline' ? REACTION_INLINE_SIZE : REACTION_BLOCK_SIZE;
wrapSticker({
div: this.stickerContainer,
doc: availableReaction.center_icon ?? availableReaction.static_icon,
2 years ago
width: size,
2 years ago
height: size,
static: true
2 years ago
});
});
}
}
public renderCounter() {
const reactionCount = this.reactionCount;
const displayOn = this.type === 'inline' ? REACTION_DISPLAY_INLINE_COUNTER_AT : REACTION_DISPLAY_BLOCK_COUNTER_AT;
if(reactionCount.count >= displayOn || (this.type === 'block' && !this.canRenderAvatars)) {
if(!this.counter) {
this.counter = document.createElement(this.type === 'inline' ? 'i' : 'span');
this.counter.classList.add(CLASS_NAME + '-counter');
}
const formatted = formatNumber(reactionCount.count);
if(this.counter.textContent !== formatted) {
this.counter.textContent = formatted;
}
if(!this.counter.parentElement) {
this.append(this.counter);
}
} else if(this.counter?.parentElement) {
this.counter.remove();
this.counter = undefined;
}
}
public renderAvatars(recentReactions: MessagePeerReaction[]) {
2 years ago
if(this.type === 'inline') {
return;
}
if(this.reactionCount.count >= REACTION_DISPLAY_BLOCK_COUNTER_AT || !this.canRenderAvatars) {
if(this.stackedAvatars) {
this.stackedAvatars.container.remove();
this.stackedAvatars = undefined;
}
return;
}
if(!this.stackedAvatars) {
this.stackedAvatars = new StackedAvatars({
2 years ago
avatarSize: 24
2 years ago
});
this.append(this.stackedAvatars.container);
}
this.stackedAvatars.render(recentReactions.map(reaction => appPeersManager.getPeerId(reaction.peer_id)));
2 years ago
}
public setIsChosen(isChosen = !!this.reactionCount.pFlags.chosen) {
if(this.type === 'inline') return;
const wasChosen = this.classList.contains('is-chosen') && !this.classList.contains('backwards');
if(wasChosen !== isChosen) {
SetTransition(this, 'is-chosen', isChosen, this.isConnected ? 300 : 0);
}
}
public fireAroundAnimation() {
callbackify(appReactionsManager.getReaction(this.reactionCount.reaction), (availableReaction) => {
2 years ago
const size = this.type === 'inline' ? REACTION_INLINE_SIZE + 14 : REACTION_BLOCK_SIZE + 18;
2 years ago
const div = document.createElement('div');
div.classList.add(CLASS_NAME + '-sticker-activate');
Promise.all([
wrapSticker({
div: div,
doc: availableReaction.center_icon,
width: size,
height: size,
withThumb: false,
needUpscale: true,
play: false,
skipRatio: 1,
group: 'none',
needFadeIn: false
}) as Promise<RLottiePlayer>,
wrapStickerAnimation({
doc: availableReaction.around_animation,
size: 80,
target: this.stickerContainer,
side: 'center',
skipRatio: 1,
play: false
}).stickerPromise
2 years ago
]).then(([iconPlayer, aroundPlayer]) => {
iconPlayer.addEventListener('enterFrame', (frameNo) => {
if(frameNo === iconPlayer.maxFrame) {
iconPlayer.remove();
2 years ago
div.remove();
}
});
2 years ago
iconPlayer.addEventListener('firstFrame', () => {
2 years ago
this.stickerContainer.prepend(div);
2 years ago
iconPlayer.play();
2 years ago
aroundPlayer.play();
}, {once: true});
});
});
}
}
customElements.define(TAG_NAME, ReactionElement);