autocomplete-gutenberg.js
3.58 KB
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
/**
* Register an AutocompleterControl that can be used in place of
* a TextControl when a dropdown list of optional choices are required
*
* @since 1.0.0
*
* @param object atts TextControl compatible attributes that would be passed to a TextControl,
* plus an 'options' property comprising of an array of values to display in
* the autocomplete dropdown
* {
* label: field.label,
* help: field.description,
* value: props.attributes[ attribute ],
* list: <unique string>
* options: [
* 'value',
* 'value2'
* ]
* }
* @param array options Autocomplete Choices
*
* @package WPZincDashboardWidget
* @author WP Zinc
*/
var WPZincAutocompleterControl = function( atts ) {
// Define some constants for the various items we'll use.
const el = window.wp.element.createElement;
const {
TextControl
} = window.wp.components;
// Build options.
var options = [];
for ( var i in atts.options ) {
options.push(
el(
'option',
{
value: atts.options[ i ]
},
atts.options[ i ]
)
)
}
// Unset options from attributes.
delete atts.options;
// Return the TextControl with a <datalist>, which will
// provide the autocomplete functionality required.
return [
el(
TextControl,
atts
),
el(
'datalist',
{
id: atts.list,
},
options
)
];
}
/**
* Append an Autocompleter to Gutenberg Blocks' Autocompleters,
* using the global `wpzinc_autocomplete` array.
*
* @since 1.0.0
*
* @param array completers Completers.
* @param string blockName Block Name.
*/
function wp_zinc_auto_complete_gutenberg_register( completers, blockName ) {
wpzinc_autocomplete_gutenberg.forEach(
function( autocompleter, i ) {
autocompleter.triggers.forEach(
function( trigger, j ) {
// Skip if this uses a remote data source.
if ( 'url' in trigger ) {
return;
}
// Add to Gutenberg's Autocompleters.
completers.push(
{
name: trigger.name,
triggerPrefix: trigger.trigger,
options: trigger.values,
/**
* How options should be matched
*/
getOptionKeywords: function( option ) {
return option.value;
},
/**
* Returns the option label to display in the autocomplete
* drop down.
*
* @since 1.0.0
*
* @param object option Autocomplete Option.
* @return string Label
*/
getOptionLabel: function( option ) {
return option.value;
},
/**
* Appends the returned content to the current block
* that the user is editing, when the user clicks
* the autocomplete option.
*
* @since 1.0.0
*
* @param object option Chosen Autocomplete Option.
* @return string Value
*/
getOptionCompletion: function( option ) {
return option.key;
},
}
);
}
);
}
);
return completers;
}
/**
* Registers our Autocomplete Gutenberg Block Filter
* for the Post Editor.
*
* @since 1.0.0
*/
if ( typeof wp !== 'undefined' && typeof wp.hooks !== 'undefined' ) {
wpzinc_autocomplete_gutenberg.forEach(
function( autocompleter, i ) {
autocompleter.triggers.forEach(
function( trigger, j ) {
wp.hooks.addFilter(
'editor.Autocomplete.completers',
'wp-zinc/autocompleters/' + trigger.name,
wp_zinc_auto_complete_gutenberg_register
);
}
);
}
);
}