-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
user.js
54 lines (51 loc) · 1.15 KB
/
user.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
/**
* WordPress dependencies
*/
import apiFetch from '@wordpress/api-fetch';
/** @typedef {import('@wordpress/components').WPCompleter} WPCompleter */
/**
* A user mentions completer.
*
* @type {WPCompleter}
*/
export default {
name: 'users',
className: 'editor-autocompleters__user',
triggerPrefix: '@',
options( search ) {
let payload = '';
if ( search ) {
payload = '?search=' + encodeURIComponent( search );
}
return apiFetch( { path: '/wp/v2/users' + payload } );
},
isDebounced: true,
getOptionKeywords( user ) {
return [ user.slug, user.name ];
},
getOptionLabel( user ) {
const avatar =
user.avatar_urls && user.avatar_urls[ 24 ] ? (
<img
key="avatar"
className="editor-autocompleters__user-avatar"
alt=""
src={ user.avatar_urls[ 24 ] }
/>
) : (
<span className="editor-autocompleters__no-avatar"></span>
);
return [
avatar,
<span key="name" className="editor-autocompleters__user-name">
{ user.name }
</span>,
<span key="slug" className="editor-autocompleters__user-slug">
{ user.slug }
</span>,
];
},
getOptionCompletion( user ) {
return `@${ user.slug }`;
},
};