1
1
import { getSuggestions } from './suggestions'
2
- import { isBlank , qs , qsAll } from '../helpers'
2
+ import { isBlank , qs } from '../helpers'
3
3
4
4
export const AUTOCOMPLETE_CONTAINER_SELECTOR = '.autocomplete'
5
5
export const AUTOCOMPLETE_SUGGESTION_LIST_SELECTOR = '.autocomplete-suggestions'
@@ -16,15 +16,6 @@ const state = {
16
16
*/
17
17
export function showAutocompleteList ( ) {
18
18
qs ( AUTOCOMPLETE_CONTAINER_SELECTOR ) . classList . add ( 'shown' )
19
-
20
- const buttons = qsAll ( '.autocomplete-suggestion-preview-indicator' )
21
- buttons . forEach ( button => {
22
- button . addEventListener ( 'click' , event => {
23
- event . preventDefault ( )
24
- event . stopPropagation ( )
25
- togglePreview ( )
26
- } )
27
- } )
28
19
}
29
20
30
21
/**
@@ -88,8 +79,12 @@ export function selectedAutocompleteSuggestion () {
88
79
* @param {Number } offset How much to move down (or up) the list.
89
80
*/
90
81
export function moveAutocompleteSelection ( offset ) {
91
- state . selectedIdx = newAutocompleteIndex ( offset )
82
+ setAutocompleteSelection ( newAutocompleteIndex ( offset ) )
83
+ }
92
84
85
+ export function setAutocompleteSelection ( index ) {
86
+ state . selectedIdx = index
87
+ const suggestionList = qs ( AUTOCOMPLETE_SUGGESTION_LIST_SELECTOR )
93
88
const selectedElement = qs ( `${ AUTOCOMPLETE_SUGGESTION_SELECTOR } .selected` )
94
89
const elementToSelect = qs ( `${ AUTOCOMPLETE_SUGGESTION_SELECTOR } [data-index="${ state . selectedIdx } "]` )
95
90
@@ -98,102 +93,69 @@ export function moveAutocompleteSelection (offset) {
98
93
}
99
94
100
95
if ( elementToSelect ) {
101
- elementToSelect . classList . add ( 'selected' )
102
- showPreview ( elementToSelect )
96
+ if ( state . previewOpen ) {
97
+ removePreview ( )
98
+ suggestionList . classList . add ( 'previewing' )
99
+ const newContainer = document . createElement ( 'div' )
100
+ newContainer . classList . add ( 'autocomplete-preview' )
101
+ const iframe = document . createElement ( 'iframe' )
102
+ const previewHref = elementToSelect . href . replace ( '.html' , '.html?preview=true' )
103
+ // The minimum permissions necessary for the iframe to run JavaScript and communicate with the parent window.
104
+ iframe . setAttribute ( 'sandbox' , 'allow-scripts allow-same-origin allow-popups' )
105
+ iframe . setAttribute ( 'src' , previewHref )
106
+ newContainer . replaceChildren ( iframe )
107
+ elementToSelect . parentNode . insertBefore ( newContainer , elementToSelect . nextSibling )
108
+ }
103
109
110
+ elementToSelect . classList . add ( 'selected' )
104
111
elementToSelect . scrollIntoView ( { block : 'nearest' } )
105
112
} else {
106
- const list = qs ( AUTOCOMPLETE_SUGGESTION_LIST_SELECTOR )
107
- if ( list ) { list . scrollTop = 0 }
113
+ if ( suggestionList ) { suggestionList . scrollTop = 0 }
108
114
}
109
115
}
110
116
111
117
/**
112
118
* Toggles the preview state of the autocomplete list
113
119
*/
114
- export function togglePreview ( ) {
115
- state . previewOpen = ! state . previewOpen
116
- const suggestionList = qs ( AUTOCOMPLETE_SUGGESTION_LIST_SELECTOR )
120
+ export function togglePreview ( elementToSelect ) {
117
121
if ( state . previewOpen ) {
118
- suggestionList . classList . add ( 'previewing' )
119
- const elementToSelect = qs ( `${ AUTOCOMPLETE_SUGGESTION_SELECTOR } [data-index="${ state . selectedIdx } "]` )
120
- showPreview ( elementToSelect )
122
+ hidePreview ( )
121
123
} else {
122
- suggestionList . classList . remove ( 'previewing' )
123
- }
124
- }
125
-
126
- function expandPreview ( ) {
127
- state . previewOpen = true
128
- const suggestionList = qs ( AUTOCOMPLETE_SUGGESTION_LIST_SELECTOR )
129
- if ( suggestionList ) {
130
- suggestionList . classList . add ( 'previewing' )
131
- }
132
- }
133
-
134
- function closePreview ( ) {
135
- state . previewOpen = false
136
- const suggestionList = qs ( AUTOCOMPLETE_SUGGESTION_LIST_SELECTOR )
137
- if ( suggestionList ) {
138
- suggestionList . classList . remove ( 'previewing' )
124
+ showPreview ( elementToSelect )
139
125
}
140
126
}
141
127
142
- export function removePreview ( ) {
128
+ /**
129
+ * Hides the preview state of the autocomplete list
130
+ */
131
+ export function hidePreview ( ) {
143
132
state . previewOpen = false
144
133
const suggestionList = qs ( AUTOCOMPLETE_SUGGESTION_LIST_SELECTOR )
145
-
146
- if ( suggestionList ) {
147
- suggestionList . classList . remove ( 'previewing' )
148
- }
149
-
150
- const container = previewContainer ( )
151
-
152
- if ( container ) {
153
- container . remove ( )
154
- }
134
+ suggestionList . classList . remove ( 'previewing' )
135
+ removePreview ( )
155
136
}
156
137
138
+ /**
139
+ * Shows the preview state of the autocomplete list
140
+ */
157
141
function showPreview ( elementToSelect ) {
158
- const container = previewContainer ( )
159
-
160
- if ( container ) {
161
- container . remove ( )
162
- } ;
163
-
142
+ state . previewOpen = true
164
143
const suggestionList = qs ( AUTOCOMPLETE_SUGGESTION_LIST_SELECTOR )
165
144
166
- if ( state . previewOpen && elementToSelect ) {
167
- suggestionList . classList . add ( 'previewing' )
168
- const newContainer = document . createElement ( 'div' )
169
- newContainer . classList . add ( 'autocomplete-preview' )
170
- const iframe = document . createElement ( 'iframe' )
171
- const previewHref = elementToSelect . href . replace ( '.html' , '.html?preview=true' )
172
- // The minimum permissions necessary for the iframe to run JavaScript and communicate with the parent window.
173
- iframe . setAttribute ( 'sandbox' , 'allow-scripts allow-same-origin allow-popups' )
174
- iframe . setAttribute ( 'src' , previewHref )
175
- newContainer . replaceChildren ( iframe )
176
- iframe . onload = ( ) => {
177
- if ( iframe . contentDocument ) {
178
- iframe . contentDocument . addEventListener ( 'keydown' , event => {
179
- if ( event . key === 'ArrowLeft' ) {
180
- closePreview ( )
181
- event . preventDefault ( )
182
- } else if ( event . key === 'ArrowRight' ) {
183
- expandPreview ( )
184
- event . preventDefault ( )
185
- }
186
- } )
187
- }
188
- }
189
- elementToSelect . parentNode . insertBefore ( newContainer , elementToSelect . nextSibling )
145
+ if ( elementToSelect ) {
146
+ elementToSelect = elementToSelect . closest ( AUTOCOMPLETE_SUGGESTION_SELECTOR )
190
147
} else {
191
- suggestionList . classList . remove ( 'previewing' )
148
+ elementToSelect = qs ( `${ AUTOCOMPLETE_SUGGESTION_SELECTOR } [data-index="${ state . selectedIdx } "]` )
149
+ }
150
+
151
+ if ( elementToSelect ) {
152
+ setAutocompleteSelection ( parseInt ( elementToSelect . dataset . index ) )
192
153
}
193
154
}
194
155
195
- function previewContainer ( ) {
196
- return qs ( '.autocomplete-preview' )
156
+ function removePreview ( ) {
157
+ let preview = qs ( '.autocomplete-preview' )
158
+ if ( preview ) { preview . remove ( ) }
197
159
}
198
160
199
161
function newAutocompleteIndex ( offset ) {
0 commit comments