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.

107 lines
3.1 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 { i18n } from "../lib/langPack";
import replaceContent from "../helpers/dom/replaceContent";
import { NULL_PEER_ID } from "../lib/mtproto/mtproto_config";
3 years ago
import limitSymbols from "../helpers/string/limitSymbols";
import setInnerHTML from "../helpers/dom/setInnerHTML";
2 years ago
import { AppManagers } from "../lib/appManagers/managers";
import wrapEmojiText from "../lib/richTextProcessor/wrapEmojiText";
import getPeerTitle from "./wrappers/getPeerTitle";
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
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);
//console.log('in the summer silence i was doing nothing', peerTitle, peerId);
if(peerTitle) {
peerTitle.update();
}
});
});
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;
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);
}
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;
}
if(this.peerId === undefined) {
this.peerId = NULL_PEER_ID;
}
3 years ago
if(this.peerId !== rootScope.myId || !this.dialog) {
2 years ago
const managers = this.managers ?? rootScope.managers;
setInnerHTML(this.element, await getPeerTitle(this.peerId, this.plainText, this.onlyFirstName, this.limitSymbols, managers));
3 years ago
} else {
replaceContent(this.element, i18n(this.onlyFirstName ? 'Saved' : 'SavedMessages'));
}
}
}