fix: fix emoji render problem if not found

pull/90/head
moonrailgun 2 years ago
parent 256cb43c93
commit deceb60da3

@ -14,7 +14,7 @@ interface Props {
export const Emoji: React.FC<Props> = React.memo((props) => { export const Emoji: React.FC<Props> = React.memo((props) => {
const code = props.emoji.startsWith(':') ? props.emoji : `:${props.emoji}:`; // 对旧版兼容 const code = props.emoji.startsWith(':') ? props.emoji : `:${props.emoji}:`; // 对旧版兼容
const { emojiSkin } = useMemo(() => { return useMemo(() => {
let id = ''; let id = '';
let skin = 0; let skin = 0;
const matches = code.match(SearchIndex.SHORTCODES_REGEX); const matches = code.match(SearchIndex.SHORTCODES_REGEX);
@ -29,22 +29,27 @@ export const Emoji: React.FC<Props> = React.memo((props) => {
// @ts-ignore // @ts-ignore
const emoji = SearchIndex.get(id); const emoji = SearchIndex.get(id);
const emojiSkin = emoji.skins[skin - 1] || emoji.skins[0]; if (!emoji) {
// not found emoji
return <span />;
}
return { const emojiSkin = emoji.skins[skin - 1] || emoji.skins[0];
emojiSkin,
};
}, [code]);
return ( return (
<span className="emoji-mart-emoji align-middle" data-emoji-set={'twitter'}> <span
className="emoji-mart-emoji align-middle"
data-emoji-set={'twitter'}
>
<span <span
style={{ style={{
display: 'block', display: 'block',
width: props.size ?? '16px', width: props.size ?? '16px',
height: props.size ?? '16px', height: props.size ?? '16px',
backgroundImage: `url(${spritesUrl})`, backgroundImage: `url(${spritesUrl})`,
backgroundSize: `${100 * Data.sheet.cols}% ${100 * Data.sheet.rows}%`, backgroundSize: `${100 * Data.sheet.cols}% ${
100 * Data.sheet.rows
}%`,
backgroundPosition: `${ backgroundPosition: `${
(100 / (Data.sheet.cols - 1)) * emojiSkin.x (100 / (Data.sheet.cols - 1)) * emojiSkin.x
}% ${(100 / (Data.sheet.rows - 1)) * emojiSkin.y}%`, }% ${(100 / (Data.sheet.rows - 1)) * emojiSkin.y}%`,
@ -52,5 +57,6 @@ export const Emoji: React.FC<Props> = React.memo((props) => {
/> />
</span> </span>
); );
}, [code]);
}); });
Emoji.displayName = 'Emoji'; Emoji.displayName = 'Emoji';

Loading…
Cancel
Save