Sengi-Windows-MacOS-Linux/src/app/tools/emoji.tools.ts

55 lines
1.5 KiB
TypeScript
Raw Normal View History

2019-04-25 22:52:14 +02:00
import { Emoji } from "../services/models/mastodon.interfaces";
import * as EmojiOne from "emojione";
2019-04-14 05:04:19 +02:00
export class EmojiConverter {
2019-04-25 22:52:14 +02:00
applyEmojis(emojis: Emoji[], text: string, type: EmojiTypeEnum): string {
//const instanceUrl = 'https://' + url.split('https://')[1].split('/')[0];
if(!text) return text;
2019-04-14 05:04:19 +02:00
2019-04-25 22:52:14 +02:00
let className = "emoji-small";
if (type === EmojiTypeEnum.medium) {
className = "emoji-medium";
}
2019-04-14 05:04:19 +02:00
2019-04-25 22:52:14 +02:00
if (emojis) {
emojis.forEach(emoji => {
text = text.replace(
`:${emoji.shortcode}:`,
`<img class="${className}" src="${emoji.url}" title=":${
emoji.shortcode
}:" alt=":${emoji.shortcode}:" />`
);
});
}
2019-04-14 05:04:19 +02:00
2019-04-25 22:52:14 +02:00
text = EmojiOne.toImage(text);
2019-04-18 05:25:54 +02:00
2019-04-25 22:52:14 +02:00
while (text.includes('class="emojione"')) {
text = text.replace('class="emojione"', `class="emojione ${className}"`);
}
2019-04-14 05:04:19 +02:00
2019-04-25 22:52:14 +02:00
//FIXME: clean up this mess...
// while (text.includes('https://cdn.jsdelivr.net/emojione/assets/4.5/png/32/')) {
// text = text.replace('https://cdn.jsdelivr.net/emojione/assets/4.5/png/32/', instanceUrl + '/emoji/');
// text = text.replace('.png', '.svg');
// }
while (
text.includes("https://cdn.jsdelivr.net/emojione/assets/4.5/png/32/")
) {
text = text.replace(
"https://cdn.jsdelivr.net/emojione/assets/4.5/png/32/",
"assets/emoji/72x72/"
);
// text = text.replace('.png', '.svg');
2019-04-14 05:04:19 +02:00
}
2019-04-25 22:52:14 +02:00
return text;
}
2019-04-14 05:04:19 +02:00
}
export enum EmojiTypeEnum {
2019-04-25 22:52:14 +02:00
small,
medium
}