2023-02-22 23:03:47 +01:00
|
|
|
import octopus_url from "../../static/generated/emoji/images-google-64/1f419.png";
|
2024-08-22 18:08:03 +02:00
|
|
|
import google_blob_sheet from "../generated/emoji/google-blob.webp";
|
|
|
|
import google_sheet from "../generated/emoji/google.webp";
|
|
|
|
import twitter_sheet from "../generated/emoji/twitter.webp";
|
2020-09-26 06:24:56 +02:00
|
|
|
|
2024-11-12 03:59:37 +01:00
|
|
|
import * as blueslip from "./blueslip.ts";
|
|
|
|
import {user_settings} from "./user_settings.ts";
|
2021-03-25 22:35:45 +01:00
|
|
|
|
2020-07-24 06:02:07 +02:00
|
|
|
import google_blob_css from "!style-loader?injectType=lazyStyleTag!css-loader!../generated/emoji-styles/google-blob-sprite.css";
|
|
|
|
import google_css from "!style-loader?injectType=lazyStyleTag!css-loader!../generated/emoji-styles/google-sprite.css";
|
|
|
|
import twitter_css from "!style-loader?injectType=lazyStyleTag!css-loader!../generated/emoji-styles/twitter-sprite.css";
|
|
|
|
|
2023-05-21 09:15:52 +02:00
|
|
|
type EmojiSet = {
|
|
|
|
css: {use: () => void; unuse: () => void};
|
|
|
|
sheet: string;
|
|
|
|
};
|
|
|
|
|
|
|
|
const emojisets = new Map<string, EmojiSet>([
|
2020-07-16 22:40:18 +02:00
|
|
|
["google", {css: google_css, sheet: google_sheet}],
|
|
|
|
["google-blob", {css: google_blob_css, sheet: google_blob_sheet}],
|
|
|
|
["twitter", {css: twitter_css, sheet: twitter_sheet}],
|
2020-02-21 00:20:39 +01:00
|
|
|
]);
|
|
|
|
|
2022-06-05 07:23:18 +02:00
|
|
|
// For `text` emoji set we fallback to `google` emoji set
|
2020-02-21 00:20:39 +01:00
|
|
|
// for displaying emojis in emoji picker and typeahead.
|
2023-05-21 09:15:52 +02:00
|
|
|
emojisets.set("text", emojisets.get("google")!);
|
2020-02-21 00:20:39 +01:00
|
|
|
|
2023-05-21 09:15:52 +02:00
|
|
|
let current_emojiset: EmojiSet | undefined;
|
2020-02-21 00:20:39 +01:00
|
|
|
|
2024-03-30 10:51:09 +01:00
|
|
|
async function fetch_emojiset(name: string, url: string): Promise<void> {
|
2024-04-17 08:30:05 +02:00
|
|
|
const MAX_RETRIES = 3;
|
|
|
|
const RETRY_DELAY = 10000; // 10 seconds
|
|
|
|
|
|
|
|
for (let attempt = 1; attempt <= MAX_RETRIES; attempt += 1) {
|
|
|
|
try {
|
|
|
|
const response = await fetch(url);
|
|
|
|
|
|
|
|
if (!response.ok) {
|
|
|
|
throw new Error(`HTTP error! status: ${response.status}`);
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the fetch is successful, resolve the promise and return
|
|
|
|
return;
|
|
|
|
} catch (error) {
|
|
|
|
if (!navigator.onLine) {
|
|
|
|
// If the user is offline, retry once they are online.
|
|
|
|
await new Promise<void>((resolve) => {
|
|
|
|
// We don't want to throw an error here since this is clearly something wrong with user's network.
|
|
|
|
blueslip.warn(
|
|
|
|
`Failed to load emojiset ${name} from ${url}. Retrying when online.`,
|
|
|
|
);
|
|
|
|
window.addEventListener(
|
|
|
|
"online",
|
|
|
|
() => {
|
|
|
|
resolve();
|
|
|
|
},
|
|
|
|
{once: true},
|
|
|
|
);
|
|
|
|
});
|
|
|
|
} else {
|
2024-03-30 10:51:09 +01:00
|
|
|
blueslip.warn(
|
2024-04-17 08:30:05 +02:00
|
|
|
`Failed to load emojiset ${name} from ${url}. Attempt ${attempt} of ${MAX_RETRIES}.`,
|
2024-03-30 10:51:09 +01:00
|
|
|
);
|
|
|
|
|
2024-04-17 08:30:05 +02:00
|
|
|
// If this was the last attempt, rethrow the error
|
|
|
|
if (attempt === MAX_RETRIES) {
|
|
|
|
blueslip.error(
|
|
|
|
`Failed to load emojiset ${name} from ${url} after ${MAX_RETRIES} attempts.`,
|
|
|
|
);
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wait before the next attempt
|
|
|
|
await new Promise((resolve) => setTimeout(resolve, RETRY_DELAY));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-03-30 10:51:09 +01:00
|
|
|
}
|
|
|
|
|
2023-05-21 09:15:52 +02:00
|
|
|
export async function select(name: string): Promise<void> {
|
2020-02-21 00:20:39 +01:00
|
|
|
const new_emojiset = emojisets.get(name);
|
|
|
|
if (new_emojiset === current_emojiset) {
|
|
|
|
return;
|
|
|
|
}
|
2023-05-21 09:15:52 +02:00
|
|
|
|
|
|
|
if (!new_emojiset) {
|
|
|
|
throw new Error("Unknown emojiset " + name);
|
|
|
|
}
|
|
|
|
|
2024-03-30 10:51:09 +01:00
|
|
|
await fetch_emojiset(name, new_emojiset.sheet);
|
|
|
|
|
2020-02-21 00:20:39 +01:00
|
|
|
if (current_emojiset) {
|
|
|
|
current_emojiset.css.unuse();
|
|
|
|
}
|
|
|
|
new_emojiset.css.use();
|
|
|
|
current_emojiset = new_emojiset;
|
|
|
|
}
|
|
|
|
|
2023-05-21 09:15:52 +02:00
|
|
|
export function initialize(): void {
|
|
|
|
void select(user_settings.emojiset);
|
2020-02-21 00:20:39 +01:00
|
|
|
|
|
|
|
// Load the octopus image in the background, so that the browser
|
|
|
|
// will cache it for later use. Note that we hardcode the octopus
|
|
|
|
// emoji to the old Google one because it's better.
|
|
|
|
//
|
|
|
|
// TODO: We should probably just make this work just like the Zulip emoji.
|
|
|
|
const octopus_image = new Image();
|
2020-09-26 06:24:56 +02:00
|
|
|
octopus_image.src = octopus_url;
|
2020-02-21 00:20:39 +01:00
|
|
|
}
|