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.

171 lines
5.6 KiB

import rootScope from "../../lib/rootScope";
import { generatePathData } from "../../helpers/dom";
4 years ago
type BubbleGroup = {timestamp: number, fromId: number, mid: number, group: HTMLDivElement[]};
4 years ago
export default class BubbleGroups {
bubblesByGroups: Array<BubbleGroup> = []; // map to group
4 years ago
groups: Array<HTMLDivElement[]> = [];
//updateRAFs: Map<HTMLDivElement[], number> = new Map();
newGroupDiff = 120;
removeBubble(bubble: HTMLDivElement, mid: number) {
let details = this.bubblesByGroups.findAndSplice(g => g.mid == mid);
if(details && details.group.length) {
details.group.findAndSplice(d => d == bubble);
if(!details.group.length) {
this.groups.findAndSplice(g => g == details.group);
} else {
this.updateGroup(details.group);
4 years ago
}
}
}
addBubble(bubble: HTMLDivElement, message: any, reverse: boolean) {
let timestamp = message.date;
let fromId = message.fromId;
4 years ago
let group: HTMLDivElement[];
// fix for saved messages forward to self
if(fromId == rootScope.myId && message.peerId == rootScope.myId && message.fwdFromId == fromId) {
fromId = -fromId;
}
4 years ago
// try to find added
//this.removeBubble(message.mid);
if(this.bubblesByGroups.length) {
if(reverse) {
let g = this.bubblesByGroups[0];
if(g.fromId == fromId && (g.timestamp - timestamp) < this.newGroupDiff) {
4 years ago
group = g.group;
group.unshift(bubble);
} else {
this.groups.unshift(group = [bubble]);
}
} else {
let g = this.bubblesByGroups[this.bubblesByGroups.length - 1];
if(g.fromId == fromId && (timestamp - g.timestamp) < this.newGroupDiff) {
4 years ago
group = g.group;
group.push(bubble);
} else {
this.groups.push(group = [bubble]);
}
}
} else {
this.groups.push(group = [bubble]);
}
//console.log('[BUBBLE]: addBubble', bubble, message.mid, fromId, reverse, group);
4 years ago
this.bubblesByGroups[reverse ? 'unshift' : 'push']({timestamp, fromId, mid: message.mid, group});
this.updateGroup(group);
4 years ago
}
setClipIfNeeded(bubble: HTMLDivElement, remove = false) {
//console.log('setClipIfNeeded', bubble, remove);
const className = bubble.className;
if(className.includes('is-message-empty')/* && !className.includes('is-reply') */
&& (className.includes('photo') || className.includes('video'))) {
4 years ago
let container = bubble.querySelector('.bubble__media-container') as SVGSVGElement;
//console.log('setClipIfNeeded', bubble, remove, container);
if(!container) return;
try {
Array.from(container.children).forEach(object => {
if(object instanceof SVGDefsElement) return;
4 years ago
if(remove) {
object.removeAttributeNS(null, 'clip-path');
4 years ago
} else {
let clipId = container.dataset.clipId;
let path = container.firstElementChild.firstElementChild.lastElementChild as SVGPathElement;
let width = +object.getAttributeNS(null, 'width');
let height = +object.getAttributeNS(null, 'height');
let isOut = className.includes('is-out');
let isReply = className.includes('is-reply');
let d = '';
//console.log('setClipIfNeeded', object, width, height, isOut);
let tr: number, tl: number;
if(className.includes('forwarded') || isReply) {
tr = tl = 0;
} else if(isOut) {
tr = className.includes('is-group-first') ? 12 : 6;
tl = 12;
} else {
tr = 12;
tl = className.includes('is-group-first') ? 12 : 6;
}
if(isOut) {
d = generatePathData(0, 0, width - 9, height, tl, tr, 0, 12);
} else {
d = generatePathData(9, 0, width - 9, height, tl, tr, 12, 0);
}
path.setAttributeNS(null, 'd', d);
object.setAttributeNS(null, 'clip-path', 'url(#' + clipId + ')');
4 years ago
}
});
} catch(err) {}
4 years ago
}
}
updateGroup(group: HTMLDivElement[]) {
4 years ago
/* if(this.updateRAFs.has(group)) {
window.cancelAnimationFrame(this.updateRAFs.get(group));
this.updateRAFs.delete(group);
} */
//this.updateRAFs.set(group, window.requestAnimationFrame(() => {
//this.updateRAFs.delete(group);
if(!group.length) {
return;
}
let first = group[0];
//console.log('[BUBBLE]: updateGroup', group, first);
4 years ago
if(group.length == 1) {
first.classList.add('is-group-first', 'is-group-last');
this.setClipIfNeeded(first);
return;
} else {
first.classList.remove('is-group-last');
first.classList.add('is-group-first');
this.setClipIfNeeded(first, true);
}
let length = group.length - 1;
for(let i = 1; i < length; ++i) {
let bubble = group[i];
bubble.classList.remove('is-group-last', 'is-group-first');
this.setClipIfNeeded(bubble, true);
}
let last = group[group.length - 1];
last.classList.remove('is-group-first');
last.classList.add('is-group-last');
this.setClipIfNeeded(last);
//}));
}
updateGroupByMessageId(mid: number) {
4 years ago
let details = this.bubblesByGroups.find(g => g.mid == mid);
if(details) {
this.updateGroup(details.group);
}
}
cleanup() {
this.bubblesByGroups = [];
this.groups = [];
4 years ago
/* for(let value of this.updateRAFs.values()) {
window.cancelAnimationFrame(value);
}
this.updateRAFs.clear(); */
}
}