@@ -10,27 +10,29 @@ import {
10
10
replaceWordWithEntity ,
11
11
} from 'stream-chat' ;
12
12
import mergeWith from 'lodash.mergewith' ;
13
- import type { EmojiSearchIndexResult } from '../../../components/MessageInput' ;
14
- import { init , SearchIndex } from 'emoji-mart' ;
15
- import data from '@emoji-mart/data' ;
13
+ import type {
14
+ EmojiSearchIndex ,
15
+ EmojiSearchIndexResult ,
16
+ } from '../../../components/MessageInput' ;
16
17
import type { TextComposerSuggestion } from 'stream-chat' ;
17
18
import type { MiddlewareParams } from 'stream-chat' ;
18
19
19
- init ( { data } ) ;
20
-
21
20
class EmojiSearchSource <
22
21
T extends TextComposerSuggestion < EmojiSearchIndexResult > ,
23
22
> extends BaseSearchSource < T > {
24
23
readonly type : SearchSourceType = 'emoji' ;
25
- constructor ( options ?: SearchSourceOptions ) {
24
+ private emojiSearchIndex : EmojiSearchIndex ;
25
+
26
+ constructor ( emojiSearchIndex : EmojiSearchIndex , options ?: SearchSourceOptions ) {
26
27
super ( options ) ;
28
+ this . emojiSearchIndex = emojiSearchIndex ;
27
29
}
28
30
29
31
async query ( searchQuery : string ) {
30
32
if ( searchQuery . length === 0 || searchQuery . charAt ( 0 ) . match ( / [ ^ a - z A - Z 0 - 9 + - ] / ) ) {
31
33
return { items : [ ] as T [ ] , next : null } ;
32
34
}
33
- const emojis : T [ ] = ( await SearchIndex . search ( searchQuery ) ) ?? [ ] ;
35
+ const emojis = ( await this . emojiSearchIndex . search ( searchQuery ) ) ?? [ ] ;
34
36
35
37
// emojiIndex.search sometimes returns undefined values, so filter those out first
36
38
return {
@@ -77,11 +79,11 @@ const DEFAULT_OPTIONS: TextComposerMiddlewareOptions = { minChars: 1, trigger: '
77
79
export const createTextComposerEmojiMiddleware = <
78
80
T extends EmojiSearchIndexResult = EmojiSearchIndexResult ,
79
81
> (
80
- searchSource : EmojiSearchSource < T > ,
82
+ emojiSearchIndex : EmojiSearchIndex ,
81
83
options ?: Partial < TextComposerMiddlewareOptions > ,
82
84
) => {
83
85
const finalOptions = mergeWith ( DEFAULT_OPTIONS , options ?? { } ) ;
84
- const emojiSearchSource = searchSource ?? new EmojiSearchSource ( ) ;
86
+ const emojiSearchSource = new EmojiSearchSource ( emojiSearchIndex ) ;
85
87
emojiSearchSource . activate ( ) ;
86
88
87
89
return {
0 commit comments