11import { getSuggestions } from './suggestions'
2- import { isBlank , qs , qsAll } from '../helpers'
2+ import { isBlank , qs } from '../helpers'
33
44export const AUTOCOMPLETE_CONTAINER_SELECTOR = '.autocomplete'
55export const AUTOCOMPLETE_SUGGESTION_LIST_SELECTOR = '.autocomplete-suggestions'
@@ -16,15 +16,6 @@ const state = {
1616 */
1717export function showAutocompleteList ( ) {
1818 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- } )
2819}
2920
3021/**
@@ -88,8 +79,12 @@ export function selectedAutocompleteSuggestion () {
8879 * @param {Number } offset How much to move down (or up) the list.
8980 */
9081export function moveAutocompleteSelection ( offset ) {
91- state . selectedIdx = newAutocompleteIndex ( offset )
82+ setAutocompleteSelection ( newAutocompleteIndex ( offset ) )
83+ }
9284
85+ export function setAutocompleteSelection ( index ) {
86+ state . selectedIdx = index
87+ const suggestionList = qs ( AUTOCOMPLETE_SUGGESTION_LIST_SELECTOR )
9388 const selectedElement = qs ( `${ AUTOCOMPLETE_SUGGESTION_SELECTOR } .selected` )
9489 const elementToSelect = qs ( `${ AUTOCOMPLETE_SUGGESTION_SELECTOR } [data-index="${ state . selectedIdx } "]` )
9590
@@ -98,102 +93,69 @@ export function moveAutocompleteSelection (offset) {
9893 }
9994
10095 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+ }
103109
110+ elementToSelect . classList . add ( 'selected' )
104111 elementToSelect . scrollIntoView ( { block : 'nearest' } )
105112 } else {
106- const list = qs ( AUTOCOMPLETE_SUGGESTION_LIST_SELECTOR )
107- if ( list ) { list . scrollTop = 0 }
113+ if ( suggestionList ) { suggestionList . scrollTop = 0 }
108114 }
109115}
110116
111117/**
112118 * Toggles the preview state of the autocomplete list
113119 */
114- export function togglePreview ( ) {
115- state . previewOpen = ! state . previewOpen
116- const suggestionList = qs ( AUTOCOMPLETE_SUGGESTION_LIST_SELECTOR )
120+ export function togglePreview ( elementToSelect ) {
117121 if ( state . previewOpen ) {
118- suggestionList . classList . add ( 'previewing' )
119- const elementToSelect = qs ( `${ AUTOCOMPLETE_SUGGESTION_SELECTOR } [data-index="${ state . selectedIdx } "]` )
120- showPreview ( elementToSelect )
122+ hidePreview ( )
121123 } 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 )
139125 }
140126}
141127
142- export function removePreview ( ) {
128+ /**
129+ * Hides the preview state of the autocomplete list
130+ */
131+ export function hidePreview ( ) {
143132 state . previewOpen = false
144133 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 ( )
155136}
156137
138+ /**
139+ * Shows the preview state of the autocomplete list
140+ */
157141function showPreview ( elementToSelect ) {
158- const container = previewContainer ( )
159-
160- if ( container ) {
161- container . remove ( )
162- } ;
163-
142+ state . previewOpen = true
164143 const suggestionList = qs ( AUTOCOMPLETE_SUGGESTION_LIST_SELECTOR )
165144
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 )
190147 } 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 ) )
192153 }
193154}
194155
195- function previewContainer ( ) {
196- return qs ( '.autocomplete-preview' )
156+ function removePreview ( ) {
157+ let preview = qs ( '.autocomplete-preview' )
158+ if ( preview ) { preview . remove ( ) }
197159}
198160
199161function newAutocompleteIndex ( offset ) {
0 commit comments