From c6f990997ddce71ef61439e91c5d1035dc148936 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E3=81=9F=E3=81=84=E3=81=A1=20=E3=81=B2?= Date: Tue, 24 Oct 2023 17:06:14 +0900 Subject: [PATCH] [Glitch] Rewrite `emoji_unicode_mapping_light` to TS MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Port 948281070335c61657783702ef7d19e49909bed5 to glitch-soc Co-authored-by: taichi.fukuda ひ Signed-off-by: Claire --- .../glitch/components/autosuggest_emoji.jsx | 2 +- .../flavours/glitch/features/emoji/emoji.js | 2 +- .../features/emoji/emoji_compressed.d.ts | 15 +++-- .../features/emoji/emoji_mart_data_light.ts | 15 +---- .../emoji/emoji_unicode_mapping_light.js | 37 ------------ .../emoji/emoji_unicode_mapping_light.ts | 60 +++++++++++++++++++ .../components/announcements.jsx | 2 +- 7 files changed, 76 insertions(+), 57 deletions(-) delete mode 100644 app/javascript/flavours/glitch/features/emoji/emoji_unicode_mapping_light.js create mode 100644 app/javascript/flavours/glitch/features/emoji/emoji_unicode_mapping_light.ts diff --git a/app/javascript/flavours/glitch/components/autosuggest_emoji.jsx b/app/javascript/flavours/glitch/components/autosuggest_emoji.jsx index 32a996fd7c..0e7014e6b1 100644 --- a/app/javascript/flavours/glitch/components/autosuggest_emoji.jsx +++ b/app/javascript/flavours/glitch/components/autosuggest_emoji.jsx @@ -1,7 +1,7 @@ import PropTypes from 'prop-types'; import { PureComponent } from 'react'; -import unicodeMapping from 'flavours/glitch/features/emoji/emoji_unicode_mapping_light'; +import { unicodeMapping } from 'flavours/glitch/features/emoji/emoji_unicode_mapping_light'; import { assetHost } from 'flavours/glitch/utils/config'; export default class AutosuggestEmoji extends PureComponent { diff --git a/app/javascript/flavours/glitch/features/emoji/emoji.js b/app/javascript/flavours/glitch/features/emoji/emoji.js index 465966f87c..b50447d406 100644 --- a/app/javascript/flavours/glitch/features/emoji/emoji.js +++ b/app/javascript/flavours/glitch/features/emoji/emoji.js @@ -3,7 +3,7 @@ import Trie from 'substring-trie'; import { autoPlayGif, useSystemEmojiFont } from 'flavours/glitch/initial_state'; import { assetHost } from 'flavours/glitch/utils/config'; -import unicodeMapping from './emoji_unicode_mapping_light'; +import { unicodeMapping } from './emoji_unicode_mapping_light'; const trie = new Trie(Object.keys(unicodeMapping)); diff --git a/app/javascript/flavours/glitch/features/emoji/emoji_compressed.d.ts b/app/javascript/flavours/glitch/features/emoji/emoji_compressed.d.ts index 96e0cc5eaf..dc009b26b0 100644 --- a/app/javascript/flavours/glitch/features/emoji/emoji_compressed.d.ts +++ b/app/javascript/flavours/glitch/features/emoji/emoji_compressed.d.ts @@ -13,15 +13,20 @@ export type Search = string; * This could be a potential area of refactoring or error handling. * The non-existence of 'skins' property is evident at [this location]{@link app/javascript/flavours/glitch/features/emoji/emoji_compressed.js:121}. */ -export type Skins = null; +type Skins = null; -export type FilenameData = string[] | string[][]; +type Filename = string; +type UnicodeFilename = string; +export type FilenameData = [ + filename: Filename, + unicodeFilename?: UnicodeFilename, +][]; export type ShortCodesToEmojiDataKey = | EmojiData['id'] | BaseEmoji['native'] | keyof NimbleEmojiIndex['emojis']; -export type SearchData = [ +type SearchData = [ BaseEmoji['native'], Emoji['short_names'], Search, @@ -32,9 +37,9 @@ export type ShortCodesToEmojiData = Record< ShortCodesToEmojiDataKey, [FilenameData, SearchData] >; -export type EmojisWithoutShortCodes = FilenameData[]; +type EmojisWithoutShortCodes = FilenameData; -export type EmojiCompressed = [ +type EmojiCompressed = [ ShortCodesToEmojiData, Skins, Category[], diff --git a/app/javascript/flavours/glitch/features/emoji/emoji_mart_data_light.ts b/app/javascript/flavours/glitch/features/emoji/emoji_mart_data_light.ts index 142605b4bc..ffca1f8b06 100644 --- a/app/javascript/flavours/glitch/features/emoji/emoji_mart_data_light.ts +++ b/app/javascript/flavours/glitch/features/emoji/emoji_mart_data_light.ts @@ -30,22 +30,13 @@ const emojis: Emojis = {}; // decompress Object.keys(shortCodesToEmojiData).forEach((shortCode) => { const [_filenameData, searchData] = shortCodesToEmojiData[shortCode]; - const native = searchData[0]; - let short_names = searchData[1]; - const search = searchData[2]; - let unified = searchData[3]; + const [native, short_names, search, unified] = searchData; - if (!unified) { - // unified name can be derived from unicodeToUnifiedName - unified = unicodeToUnifiedName(native); - } - - if (short_names) short_names = [shortCode].concat(short_names); emojis[shortCode] = { native, search, - short_names, - unified, + short_names: short_names ? [shortCode].concat(short_names) : undefined, + unified: unified ?? unicodeToUnifiedName(native), }; }); diff --git a/app/javascript/flavours/glitch/features/emoji/emoji_unicode_mapping_light.js b/app/javascript/flavours/glitch/features/emoji/emoji_unicode_mapping_light.js deleted file mode 100644 index 3e3834580d..0000000000 --- a/app/javascript/flavours/glitch/features/emoji/emoji_unicode_mapping_light.js +++ /dev/null @@ -1,37 +0,0 @@ -// A mapping of unicode strings to an object containing the filename -// (i.e. the svg filename) and a shortCode intended to be shown -// as a "title" attribute in an HTML element (aka tooltip). - -import emojiCompressed from './emoji_compressed'; -import { unicodeToFilename } from './unicode_to_filename'; - -const [ - shortCodesToEmojiData, - _skins, - _categories, - _short_names, - emojisWithoutShortCodes, -] = emojiCompressed; - -// decompress -const unicodeMapping = {}; - -function processEmojiMapData(emojiMapData, shortCode) { - let [ native, filename ] = emojiMapData; - if (!filename) { - // filename name can be derived from unicodeToFilename - filename = unicodeToFilename(native); - } - unicodeMapping[native] = { - shortCode: shortCode, - filename: filename, - }; -} - -Object.keys(shortCodesToEmojiData).forEach((shortCode) => { - let [ filenameData ] = shortCodesToEmojiData[shortCode]; - filenameData.forEach(emojiMapData => processEmojiMapData(emojiMapData, shortCode)); -}); -emojisWithoutShortCodes.forEach(emojiMapData => processEmojiMapData(emojiMapData)); - -export default unicodeMapping; diff --git a/app/javascript/flavours/glitch/features/emoji/emoji_unicode_mapping_light.ts b/app/javascript/flavours/glitch/features/emoji/emoji_unicode_mapping_light.ts new file mode 100644 index 0000000000..191419496f --- /dev/null +++ b/app/javascript/flavours/glitch/features/emoji/emoji_unicode_mapping_light.ts @@ -0,0 +1,60 @@ +// A mapping of unicode strings to an object containing the filename +// (i.e. the svg filename) and a shortCode intended to be shown +// as a "title" attribute in an HTML element (aka tooltip). + +import type { + FilenameData, + ShortCodesToEmojiDataKey, +} from './emoji_compressed'; +import emojiCompressed from './emoji_compressed'; +import { unicodeToFilename } from './unicode_to_filename'; + +type UnicodeMapping = { + [key in FilenameData[number][0]]: { + shortCode: ShortCodesToEmojiDataKey; + filename: FilenameData[number][number]; + }; +}; + +const [ + shortCodesToEmojiData, + _skins, + _categories, + _short_names, + emojisWithoutShortCodes, +] = emojiCompressed; + +// decompress +const unicodeMapping: UnicodeMapping = {}; + +function processEmojiMapData( + emojiMapData: FilenameData[number], + shortCode?: ShortCodesToEmojiDataKey, +) { + const [native, _filename] = emojiMapData; + let filename = emojiMapData[1]; + if (!filename) { + // filename name can be derived from unicodeToFilename + filename = unicodeToFilename(native); + } + unicodeMapping[native] = { + shortCode, + filename, + }; +} + +Object.keys(shortCodesToEmojiData).forEach( + (shortCode: ShortCodesToEmojiDataKey) => { + if (shortCode === undefined) return; + const [filenameData, _searchData] = shortCodesToEmojiData[shortCode]; + filenameData.forEach((emojiMapData) => { + processEmojiMapData(emojiMapData, shortCode); + }); + }, +); + +emojisWithoutShortCodes.forEach((emojiMapData) => { + processEmojiMapData(emojiMapData); +}); + +export { unicodeMapping }; diff --git a/app/javascript/flavours/glitch/features/getting_started/components/announcements.jsx b/app/javascript/flavours/glitch/features/getting_started/components/announcements.jsx index 1037e8de1b..31e3ceec7c 100644 --- a/app/javascript/flavours/glitch/features/getting_started/components/announcements.jsx +++ b/app/javascript/flavours/glitch/features/getting_started/components/announcements.jsx @@ -17,7 +17,7 @@ import { AnimatedNumber } from 'flavours/glitch/components/animated_number'; import { Icon } from 'flavours/glitch/components/icon'; import { IconButton } from 'flavours/glitch/components/icon_button'; import EmojiPickerDropdown from 'flavours/glitch/features/compose/containers/emoji_picker_dropdown_container'; -import unicodeMapping from 'flavours/glitch/features/emoji/emoji_unicode_mapping_light'; +import { unicodeMapping } from 'flavours/glitch/features/emoji/emoji_unicode_mapping_light'; import { autoPlayGif, reduceMotion, disableSwiping, mascot } from 'flavours/glitch/initial_state'; import { assetHost } from 'flavours/glitch/utils/config'; import { WithRouterPropTypes } from 'flavours/glitch/utils/react_router';