ts: Convert `emojisets` to TypeScript.

Declared types for '*.png' modules and '!style-loader?*' modules in
`assets.d.ts` and used them in `emojisets.ts`.
This commit is contained in:
Lalit 2023-05-21 12:45:52 +05:30 committed by Tim Abbott
parent d9716bc189
commit 7346ce98f3
3 changed files with 29 additions and 7 deletions

View File

@ -86,7 +86,7 @@ EXEMPT_FILES = make_set(
"web/src/dropdown_widget.js",
"web/src/echo.js",
"web/src/emoji_picker.js",
"web/src/emojisets.js",
"web/src/emojisets.ts",
"web/src/favicon.ts",
"web/src/feedback_widget.ts",
"web/src/flatpickr.js",

12
web/src/assets.d.ts vendored
View File

@ -7,3 +7,15 @@ declare module "*.ttf" {
const url: string;
export default url;
}
declare module "*.png" {
const url: string;
export default url;
}
// Declare the style loader for CSS files. This is used in the
// `import` statements in the `emojisets.ts` file.
declare module "!style-loader?*" {
const css: {use: () => void; unuse: () => void};
export default css;
}

View File

@ -10,7 +10,12 @@ import google_blob_css from "!style-loader?injectType=lazyStyleTag!css-loader!..
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";
const emojisets = new Map([
type EmojiSet = {
css: {use: () => void; unuse: () => void};
sheet: string;
};
const emojisets = new Map<string, EmojiSet>([
["google", {css: google_css, sheet: google_sheet}],
["google-blob", {css: google_blob_css, sheet: google_blob_sheet}],
["twitter", {css: twitter_css, sheet: twitter_sheet}],
@ -18,15 +23,20 @@ const emojisets = new Map([
// For `text` emoji set we fallback to `google` emoji set
// for displaying emojis in emoji picker and typeahead.
emojisets.set("text", emojisets.get("google"));
emojisets.set("text", emojisets.get("google")!);
let current_emojiset;
let current_emojiset: EmojiSet | undefined;
export async function select(name) {
export async function select(name: string): Promise<void> {
const new_emojiset = emojisets.get(name);
if (new_emojiset === current_emojiset) {
return;
}
if (!new_emojiset) {
throw new Error("Unknown emojiset " + name);
}
await new Promise((resolve, reject) => {
const sheet = new Image();
sheet.addEventListener("load", resolve);
@ -45,8 +55,8 @@ export async function select(name) {
current_emojiset = new_emojiset;
}
export function initialize() {
select(user_settings.emojiset);
export function initialize(): void {
void select(user_settings.emojiset);
// Load the octopus image in the background, so that the browser
// will cache it for later use. Note that we hardcode the octopus