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.

129 lines
3.9 KiB

3 years ago
/*
* https://github.com/morethanwords/tweb
* Copyright (C) 2019-2021 Eduard Kuzmenko
* https://github.com/morethanwords/tweb/blob/master/LICENSE
*/
2 years ago
import rootScope from '../lib/rootScope';
import {i18n} from '../lib/langPack';
import replaceContent from '../helpers/dom/replaceContent';
import {NULL_PEER_ID} from '../lib/mtproto/mtproto_config';
import limitSymbols from '../helpers/string/limitSymbols';
import setInnerHTML from '../helpers/dom/setInnerHTML';
import {AppManagers} from '../lib/appManagers/managers';
import wrapEmojiText from '../lib/richTextProcessor/wrapEmojiText';
import getPeerTitle from './wrappers/getPeerTitle';
import generateTitleIcons from './generateTitleIcons';
3 years ago
export type PeerTitleOptions = {
peerId?: PeerId,
fromName?: string,
3 years ago
plainText?: boolean,
onlyFirstName?: boolean,
dialog?: boolean,
2 years ago
limitSymbols?: number,
managers?: AppManagers,
2 years ago
withIcons?: boolean,
withPremiumIcon?: boolean
3 years ago
};
const weakMap: WeakMap<HTMLElement, PeerTitle> = new WeakMap();
rootScope.addEventListener('peer_title_edit', (peerId) => {
const elements = Array.from(document.querySelectorAll(`.peer-title[data-peer-id="${peerId}"]`)) as HTMLElement[];
elements.forEach((element) => {
3 years ago
const peerTitle = weakMap.get(element);
2 years ago
peerTitle?.update();
3 years ago
});
});
export default class PeerTitle {
public element: HTMLElement;
3 years ago
public peerId: PeerId;
2 years ago
private fromName: string;
private plainText = false;
private onlyFirstName = false;
private dialog = false;
private limitSymbols: number;
private managers: AppManagers;
private hasInner: boolean;
private withIcons: boolean;
2 years ago
private withPremiumIcon: boolean;
3 years ago
constructor(options?: PeerTitleOptions) {
3 years ago
this.element = document.createElement('span');
this.element.classList.add('peer-title');
this.element.setAttribute('dir', 'auto');
if(options) {
this.update(options);
}
2 years ago
3 years ago
weakMap.set(this.element, this);
}
public setOptions(options?: PeerTitleOptions) {
if(!options) {
return;
}
2 years ago
for(const i in options) {
// @ts-ignore
const value = options[i];
2 years ago
if(typeof(value) !== 'object') {
3 years ago
// @ts-ignore
this.element.dataset[i] = value ? '' + (typeof(value) === 'boolean' ? +value : value) : '0';
3 years ago
}
// @ts-ignore
this[i] = value;
3 years ago
}
}
public async update(options?: PeerTitleOptions) {
this.setOptions(options);
3 years ago
let fromName = this.fromName;
if(fromName !== undefined) {
if(this.limitSymbols !== undefined) {
fromName = limitSymbols(fromName, this.limitSymbols, this.limitSymbols);
}
2 years ago
setInnerHTML(this.element, wrapEmojiText(fromName));
return;
}
this.peerId ??= NULL_PEER_ID;
let hasInner: boolean;
3 years ago
if(this.peerId !== rootScope.myId || !this.dialog) {
2 years ago
const managers = this.managers ?? rootScope.managers;
const [title, icons] = await Promise.all([
getPeerTitle(this.peerId, this.plainText, this.onlyFirstName, this.limitSymbols, managers),
2 years ago
(this.withIcons && generateTitleIcons(this.peerId)) || (this.withPremiumIcon && generateTitleIcons(this.peerId, true, true))
]);
if(icons?.length) {
const inner = document.createElement('span');
inner.classList.add('peer-title-inner');
hasInner = true;
setInnerHTML(inner, title);
2 years ago
const fragment = document.createDocumentFragment();
fragment.append(inner, ...icons);
setInnerHTML(this.element, fragment);
} else {
setInnerHTML(this.element, title);
}
3 years ago
} else {
replaceContent(this.element, i18n(this.onlyFirstName ? 'Saved' : 'SavedMessages'));
}
if(this.hasInner !== hasInner) {
this.hasInner = hasInner;
this.element.classList.toggle('with-icons', hasInner);
}
3 years ago
}
}