mirror of
https://github.com/outline/outline.git
synced 2026-11-27 16:08:58 +03:00
feat: Show frequently used emojis in editor emoji menu (#14722)
* feat: Show frequently used emojis in editor emoji menu when no search query * fix: Interleave custom emojis and insert default skin tone in recent emojis
This commit is contained in:
1 parent
c7434dcc26
commit
bb25530e7d
1 file changed
+94
-26
@@ -1,16 +1,22 @@
|
||||
import { compact, zip } from "es-toolkit";
|
||||
import { capitalize } from "es-toolkit/compat";
|
||||
import { observer } from "mobx-react";
|
||||
import { useCallback, useMemo, useEffect } from "react";
|
||||
import { emojiMartToGemoji, snakeCase } from "@shared/editor/lib/emoji";
|
||||
import { search as emojiSearch } from "@shared/utils/emoji";
|
||||
import { getEmojis, search as emojiSearch } from "@shared/utils/emoji";
|
||||
import EmojiMenuItem from "./EmojiMenuItem";
|
||||
import type { Props as SuggestionsMenuProps } from "./SuggestionsMenu";
|
||||
import SuggestionsMenu from "./SuggestionsMenu";
|
||||
import useStores from "~/hooks/useStores";
|
||||
import { determineIconType } from "@shared/utils/icon";
|
||||
import { IconType } from "@shared/types";
|
||||
import { EmojiSkinTone, IconType } from "@shared/types";
|
||||
import type { Emoji as EmojiData } from "@shared/types";
|
||||
import type EmojiModel from "~/models/Emoji";
|
||||
import type { MenuItem } from "@shared/editor/types";
|
||||
import { iconFrequencies } from "~/components/IconPicker/utils";
|
||||
|
||||
type Emoji = {
|
||||
id: string;
|
||||
name: string;
|
||||
title: string;
|
||||
emoji: string;
|
||||
@@ -27,36 +33,51 @@ const EmojiMenu = (props: Props) => {
|
||||
const { emojis } = useStores();
|
||||
const { search = "" } = props;
|
||||
|
||||
// Read on open so the list reflects emojis chosen since the menu last showed.
|
||||
const recent = useMemo<RecentEmojiIds | undefined>(
|
||||
() =>
|
||||
props.isActive
|
||||
? {
|
||||
emojis: iconFrequencies[IconType.Emoji].frequent,
|
||||
custom: iconFrequencies[IconType.Custom].frequent,
|
||||
}
|
||||
: undefined,
|
||||
[props.isActive]
|
||||
);
|
||||
|
||||
useEffect(() => {
|
||||
if (search) {
|
||||
void emojis.fetchPage({ query: search });
|
||||
return;
|
||||
}
|
||||
}, [emojis, search]);
|
||||
|
||||
const items = useMemo(
|
||||
() =>
|
||||
emojiSearch({ customEmojis: emojis.orderedData, query: search })
|
||||
.map((item) => {
|
||||
// We snake_case the shortcode for backwards compatibility with gemoji to
|
||||
// avoid multiple formats being written into documents.
|
||||
const id = emojiMartToGemoji[item.id] || item.id;
|
||||
const type = determineIconType(id);
|
||||
const value = type === IconType.Custom ? id : snakeCase(id);
|
||||
const emoji = item.value;
|
||||
recent?.custom.forEach((id) => {
|
||||
void emojis.fetch(id).catch(() => undefined);
|
||||
});
|
||||
}, [emojis, search, recent]);
|
||||
|
||||
return {
|
||||
name: "emoji",
|
||||
title: emoji,
|
||||
description:
|
||||
type === IconType.Custom
|
||||
? item.name
|
||||
: capitalize(item.name.toLowerCase()),
|
||||
emoji,
|
||||
attrs: { "data-name": value },
|
||||
};
|
||||
})
|
||||
.slice(0, 15),
|
||||
[search, emojis.orderedData]
|
||||
const items = useMemo(() => {
|
||||
const recentEmojis =
|
||||
search || !recent ? [] : getRecentEmojis(recent, emojis.orderedData);
|
||||
const results = recentEmojis.length
|
||||
? recentEmojis
|
||||
: emojiSearch({ customEmojis: emojis.orderedData, query: search });
|
||||
|
||||
return results.slice(0, 15).map(toMenuItem);
|
||||
}, [search, recent, emojis.orderedData]);
|
||||
|
||||
const handleSelect = useCallback(
|
||||
(item: MenuItem) => {
|
||||
const emoji = items.find((i) => i === item);
|
||||
if (!emoji) {
|
||||
return;
|
||||
}
|
||||
const type = determineIconType(emoji.id);
|
||||
iconFrequencies[
|
||||
type === IconType.Custom ? IconType.Custom : IconType.Emoji
|
||||
].track(emoji.id);
|
||||
},
|
||||
[items]
|
||||
);
|
||||
|
||||
const renderMenuItem = useCallback<
|
||||
@@ -72,10 +93,57 @@ const EmojiMenu = (props: Props) => {
|
||||
<SuggestionsMenu
|
||||
{...props}
|
||||
filterable={false}
|
||||
onSelect={handleSelect}
|
||||
renderMenuItem={renderMenuItem}
|
||||
items={items}
|
||||
/>
|
||||
);
|
||||
};
|
||||
|
||||
interface RecentEmojiIds {
|
||||
emojis: string[];
|
||||
custom: string[];
|
||||
}
|
||||
|
||||
/**
|
||||
* Resolve recent emoji ids, interleaving standard and custom emojis so both
|
||||
* appear within the result limit, and skipping custom emojis that are not loaded.
|
||||
*/
|
||||
const getRecentEmojis = (
|
||||
recent: RecentEmojiIds,
|
||||
customEmojis: EmojiModel[]
|
||||
): EmojiData[] => {
|
||||
// Emoji nodes store only the shortcode, so skin tones would not be inserted.
|
||||
const standard = getEmojis({
|
||||
ids: recent.emojis,
|
||||
skinTone: EmojiSkinTone.Default,
|
||||
});
|
||||
const custom = recent.custom.flatMap((id) => {
|
||||
const emoji = customEmojis.find((e) => e.id === id);
|
||||
return emoji ? [{ id: emoji.id, name: emoji.name, value: emoji.id }] : [];
|
||||
});
|
||||
|
||||
return compact(zip(standard, custom).flat());
|
||||
};
|
||||
|
||||
const toMenuItem = (item: EmojiData): Emoji => {
|
||||
// We snake_case the shortcode for backwards compatibility with gemoji to
|
||||
// avoid multiple formats being written into documents.
|
||||
const id = emojiMartToGemoji[item.id] || item.id;
|
||||
const type = determineIconType(id);
|
||||
const value = type === IconType.Custom ? id : snakeCase(id);
|
||||
|
||||
return {
|
||||
id: item.id,
|
||||
name: "emoji",
|
||||
title: item.value,
|
||||
description:
|
||||
type === IconType.Custom
|
||||
? item.name
|
||||
: capitalize(item.name.toLowerCase()),
|
||||
emoji: item.value,
|
||||
attrs: { "data-name": value },
|
||||
};
|
||||
};
|
||||
|
||||
export default observer(EmojiMenu);
|
||||
Reference in new issue
Block a user