-
-
Notifications
You must be signed in to change notification settings - Fork 531
fix: import emoji-mart dynamically like (#950) #1491
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
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,13 +9,42 @@ import { | |
} from "../../schema/index.js"; | ||
import { DefaultGridSuggestionItem } from "./DefaultGridSuggestionItem.js"; | ||
|
||
let data: | ||
// Temporary fix for https://github.com/missive/emoji-mart/pull/929 | ||
let emojiLoadingPromise: | ||
| Promise<{ | ||
default: EmojiMartData; | ||
emojiMart: typeof import("emoji-mart"); | ||
emojiData: EmojiMartData; | ||
}> | ||
| undefined; | ||
|
||
let emojiMart: typeof import("emoji-mart") | undefined; | ||
async function loadEmojiMart() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Decided to make both of these the same, and also made them Node.js safe |
||
if (emojiLoadingPromise) { | ||
return emojiLoadingPromise; | ||
} | ||
|
||
emojiLoadingPromise = (async () => { | ||
// load dynamically because emoji-mart doesn't specify type: module and breaks in nodejs | ||
const [emojiMartModule, emojiDataModule] = await Promise.all([ | ||
import("emoji-mart"), | ||
// use a dynamic import to encourage bundle-splitting | ||
// and a smaller initial client bundle size | ||
import("@emoji-mart/data"), | ||
]); | ||
|
||
const emojiMart = | ||
"default" in emojiMartModule ? emojiMartModule.default : emojiMartModule; | ||
const emojiData = | ||
"default" in emojiDataModule | ||
? (emojiDataModule.default as EmojiMartData) | ||
: (emojiDataModule as EmojiMartData); | ||
|
||
await emojiMart.init({ data: emojiData }); | ||
|
||
return { emojiMart, emojiData }; | ||
})(); | ||
|
||
return emojiLoadingPromise; | ||
} | ||
|
||
export async function getDefaultEmojiPickerItems< | ||
BSchema extends BlockSchema, | ||
|
@@ -29,23 +58,11 @@ export async function getDefaultEmojiPickerItems< | |
return []; | ||
} | ||
|
||
if (!data) { | ||
// use a dynamic import to encourage bundle-splitting | ||
// and a smaller initial client bundle size | ||
|
||
data = import("@emoji-mart/data") as any; | ||
|
||
// load dynamically because emoji-mart doesn't specify type: module and breaks in nodejs | ||
emojiMart = await import("emoji-mart"); | ||
const emojiMartData = (await data)!.default; | ||
await emojiMart.init({ data: emojiMartData }); | ||
} | ||
|
||
const emojiMartData = (await data)!.default; | ||
const { emojiData, emojiMart } = await loadEmojiMart(); | ||
|
||
const emojisToShow = | ||
query.trim() === "" | ||
? Object.values(emojiMartData.emojis) | ||
? Object.values(emojiData.emojis) | ||
: ((await emojiMart!.SearchIndex.search(query)) as Emoji[]); | ||
|
||
return emojisToShow.map((emoji) => ({ | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fyi, the reason data was global and could be a promise would be to prevent duplicate loads when
loadEmojiMart
is called twice in a row.Didn't help readability and it might be a premature optimization so I'm ok with the new version as long as we don't see this causing any issues
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, I can de-dupe easily. I'll pull that in.