2019-09-18 19:52:39 +02:00
|
|
|
import { MastodonEmoji } from "../types/Emojis";
|
|
|
|
import Mastodon from "megalodon";
|
2019-04-04 02:01:54 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Takes a given string and replaces emoji codes with their respective image tags.
|
|
|
|
* @param contents The string to replace with emojis
|
|
|
|
* @param emojis The set of emojis to replace the content with
|
|
|
|
* @param className The associated class for the string
|
|
|
|
* @returns String with image tags for emojis
|
|
|
|
*/
|
2019-09-18 19:52:39 +02:00
|
|
|
export function emojifyString(
|
|
|
|
contents: string,
|
|
|
|
emojis: [MastodonEmoji],
|
|
|
|
className?: any
|
|
|
|
): string {
|
2019-04-04 02:01:54 +02:00
|
|
|
let newContents: string = contents;
|
|
|
|
|
|
|
|
emojis.forEach((emoji: MastodonEmoji) => {
|
2019-09-18 19:52:39 +02:00
|
|
|
let filter = new RegExp(`:${emoji.shortcode}:`, "g");
|
|
|
|
newContents = newContents.replace(
|
|
|
|
filter,
|
|
|
|
`<img src=${emoji.static_url} ${
|
|
|
|
className ? `class="${className}"` : ""
|
|
|
|
}/>`
|
|
|
|
);
|
|
|
|
});
|
2019-04-04 02:01:54 +02:00
|
|
|
|
|
|
|
return newContents;
|
2019-04-05 22:29:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
export function collectEmojisFromServer() {
|
2019-09-18 19:52:39 +02:00
|
|
|
let client = new Mastodon(
|
|
|
|
localStorage.getItem("access_token") as string,
|
|
|
|
localStorage.getItem("baseurl") + "/api/v1"
|
|
|
|
);
|
|
|
|
let emojisPath = localStorage.getItem("emojis");
|
2019-04-05 22:29:11 +02:00
|
|
|
let emojis: any[] = [];
|
|
|
|
if (emojisPath === null) {
|
2019-09-18 19:52:39 +02:00
|
|
|
client
|
|
|
|
.get("/custom_emojis")
|
|
|
|
.then((resp: any) => {
|
|
|
|
resp.data.forEach((emoji: MastodonEmoji) => {
|
|
|
|
let customEmoji = {
|
|
|
|
name: emoji.shortcode,
|
|
|
|
emoticons: [""],
|
|
|
|
short_names: [emoji.shortcode],
|
|
|
|
imageUrl: emoji.static_url,
|
|
|
|
keywords: ["mastodon", "custom"]
|
|
|
|
};
|
|
|
|
emojis.push(customEmoji);
|
|
|
|
localStorage.setItem("emojis", JSON.stringify(emojis));
|
|
|
|
});
|
2019-04-05 22:29:11 +02:00
|
|
|
})
|
2019-09-18 19:52:39 +02:00
|
|
|
.catch((err: Error) => {
|
|
|
|
console.error(err.message);
|
|
|
|
});
|
2019-04-05 22:29:11 +02:00
|
|
|
}
|
2019-09-18 19:52:39 +02:00
|
|
|
}
|