Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improve emoji loading #935

Merged
merged 4 commits into from
Jul 18, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
import data, { Emoji, EmojiMartData } from "@emoji-mart/data";
import { init, SearchIndex } from "emoji-mart";
import type { Emoji, EmojiMartData } from "@emoji-mart/data";
import { SearchIndex, init } from "emoji-mart";

import { BlockSchema, InlineContentSchema, StyleSchema } from "../../schema";
import { BlockNoteEditor } from "../../editor/BlockNoteEditor";
import { checkDefaultInlineContentTypeInSchema } from "../../blocks/defaultBlockTypeGuards";
import { BlockNoteEditor } from "../../editor/BlockNoteEditor";
import { BlockSchema, InlineContentSchema, StyleSchema } from "../../schema";
import { DefaultGridSuggestionItem } from "./DefaultGridSuggestionItem";

let dataInitialized = false;
const emojiMartData = data as EmojiMartData;
let data:
| Promise<{
default: EmojiMartData;
}>
| undefined;

export async function getDefaultEmojiPickerItems<
BSchema extends BlockSchema,
Expand All @@ -21,17 +24,22 @@ export async function getDefaultEmojiPickerItems<
return [];
}

if (!dataInitialized) {
dataInitialized = true;
await init({ emojiMartData });
if (!data) {
// use a dynamic import to encourage bundle-splitting
// and a smaller initial client bundle size
data = import("@emoji-mart/data", { assert: { type: "json" } }) as any;
const emojiMartData = (await data)!.default;
await init({ data: emojiMartData });
}

const emojiMartData = (await data)!.default;

const emojisToShow =
query.trim() === ""
? Object.values(emojiMartData.emojis)
: ((await SearchIndex.search(query)) as Emoji[]);

return emojisToShow.map((emoji: Emoji) => ({
return emojisToShow.map((emoji) => ({
id: emoji.skins[0].native,
onItemClick: () => editor.insertInlineContent(emoji.skins[0].native + " "),
}));
Expand Down
Loading