From 7346ce98f3c07f5a525496d82161440484a0927c Mon Sep 17 00:00:00 2001 From: Lalit Date: Sun, 21 May 2023 12:45:52 +0530 Subject: [PATCH] ts: Convert `emojisets` to TypeScript. Declared types for '*.png' modules and '!style-loader?*' modules in `assets.d.ts` and used them in `emojisets.ts`. --- tools/test-js-with-node | 2 +- web/src/assets.d.ts | 12 ++++++++++++ web/src/{emojisets.js => emojisets.ts} | 22 ++++++++++++++++------ 3 files changed, 29 insertions(+), 7 deletions(-) rename web/src/{emojisets.js => emojisets.ts} (82%) diff --git a/tools/test-js-with-node b/tools/test-js-with-node index 96085b18f0..a7e45e8125 100755 --- a/tools/test-js-with-node +++ b/tools/test-js-with-node @@ -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", diff --git a/web/src/assets.d.ts b/web/src/assets.d.ts index 4795ce2a64..53b34255fe 100644 --- a/web/src/assets.d.ts +++ b/web/src/assets.d.ts @@ -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; +} diff --git a/web/src/emojisets.js b/web/src/emojisets.ts similarity index 82% rename from web/src/emojisets.js rename to web/src/emojisets.ts index ca4dd6dcdc..5ce0d4f1d1 100644 --- a/web/src/emojisets.js +++ b/web/src/emojisets.ts @@ -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([ ["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 { 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