@portabletext/plugin-emoji-picker 0.0.15 → 1.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,45 +1,16 @@
1
- import {emojis} from './emojis'
2
-
3
1
  /**
4
- * Proposed, but not required type, to represent an emoji match.
5
- *
6
- * @example
7
- * ```tsx
8
- * {
9
- * type: 'exact',
10
- * key: '😂-joy',
11
- * emoji: '😂',
12
- * keyword: 'joy',
13
- * }
14
- * ```
15
- * @example
16
- * ```tsx
17
- * {
18
- * type: 'partial',
19
- * key: '😹-joy-_cat',
20
- * emoji: '😹',
21
- * keyword: 'joy',
22
- * startSlice: '',
23
- * endSlice: '_cat',
24
- * }
25
- * ```
2
+ * The base type representing an emoji match.
26
3
  *
27
4
  * @beta
28
5
  */
29
- export type EmojiMatch =
6
+ export type BaseEmojiMatch =
30
7
  | {
31
8
  type: 'exact'
32
- key: string
33
9
  emoji: string
34
- keyword: string
35
10
  }
36
11
  | {
37
12
  type: 'partial'
38
- key: string
39
13
  emoji: string
40
- keyword: string
41
- startSlice: string
42
- endSlice: string
43
14
  }
44
15
 
45
16
  /**
@@ -47,75 +18,5 @@ export type EmojiMatch =
47
18
  *
48
19
  * @beta
49
20
  */
50
- export type MatchEmojis<TEmojiMatch = EmojiMatch> = (query: {
51
- keyword: string
52
- }) => ReadonlyArray<TEmojiMatch>
53
-
54
- /**
55
- * Proposed, but not required, default implementation of `MatchEmojis`.
56
- *
57
- * @beta
58
- */
59
- export const matchEmojis: MatchEmojis = createMatchEmojis({emojis})
60
-
61
- /**
62
- * Proposed, but not required, function to create a `MatchEmojis` function.
63
- *
64
- * @example
65
- * ```ts
66
- * const matchEmojis = createMatchEmojis({
67
- * emojis: {
68
- * '😂': ['joy'],
69
- * '😹': ['joy_cat'],
70
- * },
71
- * })
72
- * ```
73
- *
74
- * @beta
75
- */
76
- export function createMatchEmojis(config: {
77
- emojis: Record<string, ReadonlyArray<string>>
78
- }): MatchEmojis {
79
- return ({keyword}: {keyword: string}) => {
80
- const foundEmojis: Array<EmojiMatch> = []
81
-
82
- if (keyword.length < 1) {
83
- return foundEmojis
84
- }
85
-
86
- for (const emoji in config.emojis) {
87
- const emojiKeywords = config.emojis[emoji] ?? []
88
-
89
- for (const emojiKeyword of emojiKeywords) {
90
- const keywordIndex = emojiKeyword.indexOf(keyword)
91
-
92
- if (keywordIndex === -1) {
93
- continue
94
- }
95
-
96
- if (emojiKeyword === keyword) {
97
- foundEmojis.push({
98
- type: 'exact',
99
- key: `${emoji}-${keyword}`,
100
- emoji,
101
- keyword,
102
- })
103
- } else {
104
- const start = emojiKeyword.slice(0, keywordIndex)
105
- const end = emojiKeyword.slice(keywordIndex + keyword.length)
106
-
107
- foundEmojis.push({
108
- type: 'partial',
109
- key: `${emoji}-${start}${keyword}${end}`,
110
- emoji,
111
- keyword,
112
- startSlice: start,
113
- endSlice: end,
114
- })
115
- }
116
- }
117
- }
118
-
119
- return foundEmojis
120
- }
121
- }
21
+ export type MatchEmojis<TEmojiMatch extends BaseEmojiMatch = BaseEmojiMatch> =
22
+ (query: {keyword: string}) => ReadonlyArray<TEmojiMatch>
@@ -2,12 +2,12 @@ import {useEditor} from '@portabletext/editor'
2
2
  import {useActorRef, useSelector} from '@xstate/react'
3
3
  import {useCallback} from 'react'
4
4
  import {emojiPickerMachine} from './emoji-picker-machine'
5
- import type {EmojiMatch, MatchEmojis} from './match-emojis'
5
+ import type {BaseEmojiMatch, MatchEmojis} from './match-emojis'
6
6
 
7
7
  /**
8
8
  * @beta
9
9
  */
10
- export type EmojiPicker<TEmojiMatch = EmojiMatch> = {
10
+ export type EmojiPicker<TEmojiMatch extends BaseEmojiMatch = BaseEmojiMatch> = {
11
11
  /**
12
12
  * The matched keyword, including colons.
13
13
  *
@@ -105,7 +105,9 @@ export type EmojiPicker<TEmojiMatch = EmojiMatch> = {
105
105
  /**
106
106
  * @beta
107
107
  */
108
- export type EmojiPickerProps<TEmojiMatch = EmojiMatch> = {
108
+ export type EmojiPickerProps<
109
+ TEmojiMatch extends BaseEmojiMatch = BaseEmojiMatch,
110
+ > = {
109
111
  matchEmojis: MatchEmojis<TEmojiMatch>
110
112
  }
111
113
 
@@ -136,12 +138,12 @@ export type EmojiPickerProps<TEmojiMatch = EmojiMatch> = {
136
138
  *
137
139
  * @beta
138
140
  */
139
- export function useEmojiPicker<TEmojiMatch = EmojiMatch>(
140
- props: EmojiPickerProps<TEmojiMatch>,
141
- ): EmojiPicker<TEmojiMatch> {
141
+ export function useEmojiPicker<
142
+ TEmojiMatch extends BaseEmojiMatch = BaseEmojiMatch,
143
+ >(props: EmojiPickerProps<TEmojiMatch>): EmojiPicker<TEmojiMatch> {
142
144
  const editor = useEditor()
143
145
  const emojiPickerActor = useActorRef(emojiPickerMachine, {
144
- input: {editor, matchEmojis: props.matchEmojis as MatchEmojis<EmojiMatch>},
146
+ input: {editor, matchEmojis: props.matchEmojis},
145
147
  })
146
148
  const keyword = useSelector(
147
149
  emojiPickerActor,