forked from hassellof/react-native-search-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
executable file
·205 lines (185 loc) · 6.26 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
import { NativeModules, NativeEventEmitter } from 'react-native';
import resolveAssetSource from 'react-native/Libraries/Image/resolveAssetSource';
const SearchApiManager = NativeModules.SearchApiManager;
const SPOTLIGHT_SEARCH_ITEM_TAPPED_EVENT = "spotlightSearchItemTapped";
const APP_HISTORY_SEARCH_ITEM_TAPPED = "appHistorySearchItemTapped";
/**
* `SearchApi` gives you a general interface to interact with the iOS Search API.
*
* ## Search item keys
*
* Dictionaries, passed to create spotlight and app history items have some common
* and some specific keys, here is the list of all possible keys.
*
* ### Common keys
*
* #### title: string
* Title of the item. Required for both item types.
*
* #### contentDescription: string
* Description of the item. Optional.
*
* #### keywords: Array<string>
* An array of keywords, assigned to the search item. Optional.
*
* #### thumbnail: string|object
* Image to be used as the thumbnail. Same as the `source` value of the `Image`
* view. Optional.
*
* ### Spotlight-specific keys
*
* #### uniqueIdentifier: string
* The unique identifier of the spotlight item, passed later on during
* the item opening event. Required.
*
* #### domain: string
* The domain for the spotlight item. Optional.
*
* ### App history-specific keys
*
* #### userInfo: Object
* A dictionary, passed later on during the item opening event. Required.
*
* #### eligibleForPublicIndexing: boolean
* A flag, that when set to `true` allows to add the item to the public index.
* Optional.
*
* #### expirationDate: Date
* Expiration date of the search item. Optional.
*
* #### webpageURL: string
* URL of the page, representing the same content on the app's website.
*/
class SearchApi extends NativeEventEmitter {
constructor() {
super(SearchApiManager);
}
/**
* Gets the initial spotlight item's identifier. Resoves to null
* in case the app was started otherwise.
*
* @NOTE A good place for calling this method is the component's
* `componentDidMount` override.
*/
getInitialSpotlightItem(): Promise {
return SearchApiManager.getInitialSpotlightItem();
}
/**
* Gets the initial app history item's user info dictionary. Resolves to null
* in case the app was started otherwise.
*
* @NOTE A good place for calling this method is the component's
* `componentDidMount` override.
*/
getInitialAppHistoryItem(): Promise {
return SearchApiManager.getInitialAppHistoryItem();
}
/**
* Registers for the spotlight item opening event.
*
* @NOTE A good place for calling this method is the component's
* `componentDidMount` override.
*
* @param listener A function that takes a single parameter
* of type `string`, containing the unique identifier of the
* spotlight item.
*/
addOnSpotlightItemOpenEventListener(listener: Function) {
this.addListener(SPOTLIGHT_SEARCH_ITEM_TAPPED_EVENT, listener);
}
/**
* Removes the spotlight item opening event listener.
*
* @NOTE A good place for calling this method is the component's
* `componentWillUnmount` override.
*
* @param listener The function, previously passed to
* `addOnSpotlightItemOpenEventListener`.
*/
removeOnSpotlightItemOpenEventListener(listener: Function) {
this.removeListener(SPOTLIGHT_SEARCH_ITEM_TAPPED_EVENT, listener);
}
/**
* Registers for the app history item opening event.
*
* @NOTE A good place for calling this method is the component's
* `componentDidMount` override.
*
* @param listener A function that takes a single parameter
* of type `Object`, containing the user info, passed when
* creating the search item.
*/
addOnAppHistoryItemOpenEventListener(listener: Function) {
this.addListener(APP_HISTORY_SEARCH_ITEM_TAPPED, listener);
}
/**
* Removes the app history item opening event listener.
*
* @NOTE A good place for calling this method is the component's
* `componentWillUnmount` override.
*
* @param listener The function, previously passed to
* `addOnAppHistoryItemOpenEventListener`.
*/
removeOnAppHistoryItemOpenEventListener(listener: Function) {
this.removeListener(APP_HISTORY_SEARCH_ITEM_TAPPED, listener);
}
/**
* Adds a new item to the spotlight index.
*
* @param item A dictionary with the item's parameters.
* See the comment above this class for more info.
*/
indexSpotlightItem(item: Object): Promise {
return this.indexSpotlightItems([item]);
}
/**
* Adds an array of new items to the spotlight index.
*
* @param items An array with new items to be added.
* See the comment above this class for more info.
*/
indexSpotlightItems(items: Array): Promise {
var copies = items.map(item => resolveItemThumbnail(item));
return SearchApiManager.indexItems(copies);
}
/**
* Deletes all items with specified identifiers from the
* spotlight index.
*
* @param identifiers An array of unique item identifiers.
*/
deleteSpotlightItemsWithIdentifiers(identifiers: Array): Promise {
return SearchApiManager.deleteItemsWithIdentifiers(identifiers);
}
/**
* Deletes all items in specified domains from the spotlight index.
*
* @param domains An array of spotlight item domains.
*/
deleteSpotlightItemsInDomains(domains: Array): Promise {
return SearchApiManager.deleteItemsInDomains(domains);
}
/**
* Clears up the spotlight index.
*/
deleteAllSpotlightItems(): Promise {
return SearchApiManager.deleteAllItems();
}
/**
* Creates a new search item, added to the app history.
*
* @param item A dictionary with the item's parameters.
* See the comment above this class for more info.
*/
indexAppHistoryItem(item: Object): Promise {
var itemCopy = resolveItemThumbnail(item);
return SearchApiManager.createUserActivity(itemCopy);
}
}
function resolveItemThumbnail(item: Object): Object {
var itemCopy = JSON.parse(JSON.stringify(item));
itemCopy.thumbnail = resolveAssetSource(item.thumbnail);
return itemCopy;
}
export default new SearchApi();