@@ -40,19 +40,16 @@ class PageMenuButton extends Component<any, any> {
40
40
e . preventDefault ( ) ;
41
41
props . onClick ( ) ;
42
42
}
43
-
44
- return h ( 'li' , { } ,
45
- h ( 'a' , { href : '#' , onClick : onClick } , props . title )
46
- ) ;
43
+ return < li > < a href = "#" onClick = { onClick } > { props . title } </ a > </ li > ;
47
44
}
48
45
49
46
}
50
47
51
48
function addSearchPageMenuButton ( action : ( ) => void ) {
52
- var pageMenu = document . querySelector ( '#page-menu' ) as HTMLUListElement ;
53
- var dummy = document . createElement ( 'li' ) ;
49
+ const pageMenu = document . querySelector ( '#page-menu' ) as HTMLUListElement ;
50
+ const dummy = document . createElement ( 'li' ) ;
54
51
pageMenu . insertBefore ( dummy , pageMenu . firstChild ) ;
55
- preact . render ( h ( PageMenuButton , { onClick : action , title : "Quick Jump" } ) , pageMenu , dummy ) ;
52
+ preact . render ( < PageMenuButton onClick = { action } title = "Quick Jump" /> , pageMenu , dummy ) ;
56
53
}
57
54
58
55
// -------------------------------------------------------------------------- //
@@ -165,7 +162,7 @@ class QuickJump extends Component<QuickJumpProps, QuickJumpState> {
165
162
}
166
163
167
164
navigateLinks ( change : number ) {
168
- var newActiveLinkIndex = Math . max ( - 1 , Math . min ( this . linkIndex - 1 , this . state . activeLinkIndex + change ) ) ;
165
+ const newActiveLinkIndex = Math . max ( - 1 , Math . min ( this . linkIndex - 1 , this . state . activeLinkIndex + change ) ) ;
169
166
this . navigatedByKeyboard = true ;
170
167
this . setState ( { activeLinkIndex : newActiveLinkIndex } ) ;
171
168
}
@@ -176,19 +173,19 @@ class QuickJump extends Component<QuickJumpProps, QuickJumpState> {
176
173
}
177
174
178
175
updateResults ( ) {
179
- var searchString = ( this . input && this . input . value ) || '' ;
176
+ const searchString = ( this . input && this . input . value ) || '' ;
180
177
const results : FuseResult < DocItem > [ ] = this . state . fuse . search ( searchString ) ;
181
178
182
- var resultsByModule : { [ name : string ] : FuseResult < DocItem > [ ] } = { } ;
179
+ const resultsByModule : { [ name : string ] : FuseResult < DocItem > [ ] } = { } ;
183
180
184
181
results . forEach ( ( result ) => {
185
182
const moduleName = result . item . module ;
186
183
const resultsInModule = resultsByModule [ moduleName ] || ( resultsByModule [ moduleName ] = [ ] ) ;
187
184
resultsInModule . push ( result ) ;
188
185
} ) ;
189
186
190
- var moduleResults : ResultsInModule [ ] = [ ] ;
191
- for ( var moduleName in resultsByModule ) {
187
+ const moduleResults : ResultsInModule [ ] = [ ] ;
188
+ for ( const moduleName in resultsByModule ) {
192
189
const items = resultsByModule [ moduleName ] ;
193
190
let sumOfInverseScores = 0 ;
194
191
items . forEach ( ( item ) => { sumOfInverseScores += 1 / item . score ; } ) ;
@@ -202,8 +199,8 @@ class QuickJump extends Component<QuickJumpProps, QuickJumpState> {
202
199
203
200
componentDidUpdate ( ) {
204
201
if ( this . searchResults && this . activeLink && this . navigatedByKeyboard ) {
205
- var rect = this . activeLink . getClientRects ( ) [ 0 ] ;
206
- var searchResultsTop = this . searchResults . getClientRects ( ) [ 0 ] . top ;
202
+ const rect = this . activeLink . getClientRects ( ) [ 0 ] ;
203
+ const searchResultsTop = this . searchResults . getClientRects ( ) [ 0 ] . top ;
207
204
if ( rect . bottom > window . innerHeight ) {
208
205
this . searchResults . scrollTop += rect . bottom - window . innerHeight + 80 ;
209
206
} else if ( rect . top < searchResultsTop ) {
@@ -229,7 +226,7 @@ class QuickJump extends Component<QuickJumpProps, QuickJumpState> {
229
226
const stopPropagation = ( e : Event ) => { e . stopPropagation ( ) ; } ;
230
227
231
228
const onMouseOver = ( e : MouseEvent ) => {
232
- var target : null | Element = e . target as Element ;
229
+ let target : null | Element = e . target as Element ;
233
230
while ( target && typeof target . getAttribute === 'function' ) {
234
231
const linkIndexString = target . getAttribute ( 'data-link-index' ) ;
235
232
if ( typeof linkIndexString == 'string' ) {
@@ -243,37 +240,32 @@ class QuickJump extends Component<QuickJumpProps, QuickJumpState> {
243
240
244
241
const items = take ( 10 , state . moduleResults ) . map ( ( r ) => this . renderResultsInModule ( r ) ) ;
245
242
246
- return (
247
- h ( 'div' , { id : 'search' , class : state . isVisible ? '' : 'hidden' } ,
248
- h ( 'div' , { id : 'search-form' , onMouseDown : stopPropagation } ,
249
- h ( 'input' , {
250
- placeholder : "Search in package by name" ,
251
- ref : ( input ) => { this . input = input as undefined | HTMLInputElement ; } ,
252
- onFocus : this . show . bind ( this ) ,
253
- onClick : this . show . bind ( this ) ,
254
- onInput : this . updateResults . bind ( this )
255
- } )
256
- ) ,
257
- h ( 'div' , {
258
- id : 'search-results' ,
259
- ref : ( el ) => { this . searchResults = el ; } ,
260
- onMouseDown : stopPropagation , onMouseOver : onMouseOver
261
- } ,
262
- state . searchString === ''
263
- ? [ h ( IntroMsg , { } ) , h ( KeyboardShortcuts , { } ) ]
264
- : items . length == 0
265
- ? h ( NoResultsMsg , { searchString : state . searchString } )
266
- : h ( 'ul' , { } , items )
267
- )
268
- )
269
- ) ;
243
+ return < div id = "search" class = { state . isVisible ? '' : 'hidden' } >
244
+ < div id = "search-form" onMouseDown = { stopPropagation } >
245
+ < input
246
+ placeholder = "Search in package by name"
247
+ ref = { ( input ) => { this . input = input as undefined | HTMLInputElement ; } }
248
+ onFocus = { this . show . bind ( this ) }
249
+ onClick = { this . show . bind ( this ) }
250
+ onInput = { this . updateResults . bind ( this ) }
251
+ />
252
+ </ div >
253
+ < div id = "search-results" ref = { ( el ) => { this . searchResults = el ; } }
254
+ onMouseDown = { stopPropagation } onMouseOver = { onMouseOver } >
255
+ { state . searchString === ''
256
+ ? [ < IntroMsg /> , < KeyboardShortcuts /> ]
257
+ : items . length == 0
258
+ ? < NoResultsMsg searchString = { state . searchString } />
259
+ : < ul > { items } </ ul > }
260
+ </ div >
261
+ </ div > ;
270
262
}
271
263
272
264
renderResultsInModule ( resultsInModule : ResultsInModule ) : JSX . Element {
273
- var items = resultsInModule . items ;
274
- var moduleName = resultsInModule . module ;
275
- var showAll = this . state . expanded [ moduleName ] || items . length <= 10 ;
276
- var visibleItems = showAll ? items : take ( 8 , items ) ;
265
+ const items = resultsInModule . items ;
266
+ const moduleName = resultsInModule . module ;
267
+ const showAll = this . state . expanded [ moduleName ] || items . length <= 10 ;
268
+ const visibleItems = showAll ? items : take ( 8 , items ) ;
277
269
278
270
const expand = ( ) => {
279
271
const newExpanded = Object . assign ( { } , this . state . expanded ) ;
@@ -282,24 +274,24 @@ class QuickJump extends Component<QuickJumpProps, QuickJumpState> {
282
274
} ;
283
275
284
276
const renderItem = ( item : DocItem ) => {
285
- return h ( 'li' , { class : ' search-result' } ,
286
- this . navigationLink ( this . props . baseUrl + "/" + item . link , { } ,
287
- h ( DocHtml , { html : item . display_html } )
288
- )
289
- ) ;
277
+ return < li class = " search-result" >
278
+ { this . navigationLink ( this . props . baseUrl + "/" + item . link , { } ,
279
+ < DocHtml html = { item . display_html } />
280
+ ) }
281
+ </ li > ;
290
282
} ;
291
283
292
- return h ( 'li' , { class : ' search-module' } ,
293
- h ( 'h4' , { } , moduleName ) ,
294
- h ( 'ul' , { } ,
295
- visibleItems . map ( ( item ) => renderItem ( item . item ) ) ,
296
- showAll
284
+ return < li class = " search-module" >
285
+ < h4 > { moduleName } </ h4 >
286
+ < ul >
287
+ { visibleItems . map ( ( item ) => renderItem ( item . item ) ) }
288
+ { showAll
297
289
? [ ]
298
- : h ( 'li' , { class : ' more-results' } ,
299
- this . actionLink ( expand , { } , "show " + ( items . length - visibleItems . length ) + " more results from this module" )
300
- )
301
- )
302
- ) ;
290
+ : < li class = " more-results" >
291
+ { this . actionLink ( expand , { } , "show " + ( items . length - visibleItems . length ) + " more results from this module" ) }
292
+ </ li > }
293
+ </ ul >
294
+ </ li > ;
303
295
}
304
296
305
297
navigationLink ( href : string , attrs : JSX . HTMLAttributes & JSX . SVGAttributes & { [ propName : string ] : any } , ...children : ( JSX . Element | JSX . Element [ ] | string ) [ ] ) {
@@ -315,13 +307,13 @@ class QuickJump extends Component<QuickJumpProps, QuickJumpState> {
315
307
}
316
308
317
309
menuLink ( attrs : JSX . HTMLAttributes & JSX . SVGAttributes & { [ propName : string ] : any } , action : ( ) => void , ...children : ( JSX . Element | JSX . Element [ ] | string ) [ ] ) {
318
- var linkIndex = this . linkIndex ;
310
+ const linkIndex = this . linkIndex ;
319
311
if ( linkIndex === this . state . activeLinkIndex ) {
320
312
attrs [ 'class' ] = ( attrs [ 'class' ] ? attrs [ 'class' ] + ' ' : '' ) + 'active-link' ;
321
313
attrs . ref = ( link ?: Element ) => { if ( link ) this . activeLink = link as HTMLAnchorElement ; } ;
322
314
this . activeLinkAction = action ;
323
315
}
324
- var newAttrs = Object . assign ( { 'data-link-index' : linkIndex } , attrs ) ;
316
+ const newAttrs = Object . assign ( { 'data-link-index' : linkIndex } , attrs ) ;
325
317
this . linkIndex += 1 ;
326
318
return h ( 'a' , newAttrs , ...children ) ;
327
319
}
@@ -335,79 +327,73 @@ class DocHtml extends Component<{ html: string }, {}> {
335
327
}
336
328
337
329
render ( props : { html : string } ) {
338
- return h ( ' div' , { dangerouslySetInnerHTML : { __html : props . html } } ) ;
330
+ return < div dangerouslySetInnerHTML = { { __html : props . html } } /> ;
339
331
}
340
332
341
333
} ;
342
334
343
335
function KeyboardShortcuts ( ) {
344
- return h ( 'table' , { class : 'keyboard-shortcuts' } ,
345
- h ( 'tr' , { } ,
346
- h ( 'th' , { } , "Key" ) ,
347
- h ( 'th' , { } , "Shortcut" )
348
- ) ,
349
- h ( 'tr' , { } ,
350
- h ( 'td' , { } , h ( 'span' , { class : 'key' } , "s" ) ) ,
351
- h ( 'td' , { } , "Open this search box" )
352
- ) ,
353
- h ( 'tr' , { } ,
354
- h ( 'td' , { } , h ( 'span' , { class : 'key' } , "esc" ) ) ,
355
- h ( 'td' , { } , "Close this search box" )
356
- ) ,
357
- h ( 'tr' , { } ,
358
- h ( 'td' , { } ,
359
- h ( 'span' , { class : 'key' } , "↓" ) , ", " ,
360
- h ( 'span' , { class : 'key' } , "ctrl" ) , "+" ,
361
- h ( 'span' , { class : 'key' } , "j" )
362
- ) ,
363
- h ( 'td' , { } , "Move down in search results" )
364
- ) ,
365
- h ( 'tr' , { } ,
366
- h ( 'td' , { } ,
367
- h ( 'span' , { class : 'key' } , "↑" ) , ", " ,
368
- h ( 'span' , { class : 'key' } , "ctrl" ) , "+" ,
369
- h ( 'span' , { class : 'key' } , "k" )
370
- ) ,
371
- h ( 'td' , { } , "Move up in search results" )
372
- ) ,
373
- h ( 'tr' , { } ,
374
- h ( 'td' , { } , h ( 'span' , { class : 'key' } , "↵" ) ) ,
375
- h ( 'td' , { } , "Go to active search result" )
376
- )
377
- ) ;
336
+ return < table class = "keyboard-shortcuts" >
337
+ < tr >
338
+ < th > Key</ th >
339
+ < th > Shortcut</ th >
340
+ </ tr >
341
+ < tr >
342
+ < td > < span class = "key" > s</ span > </ td >
343
+ < td > Open this search box</ td >
344
+ </ tr >
345
+ < tr >
346
+ < td > < span class = "key" > esc</ span > </ td >
347
+ < td > Close this search box</ td >
348
+ </ tr >
349
+ < tr >
350
+ < td >
351
+ < span class = "key" > ↓</ span > ,
352
+ < span class = "key" > ctrl</ span > + < span class = "key" > j</ span >
353
+ </ td >
354
+ < td > Move down in search results</ td >
355
+ </ tr >
356
+ < tr >
357
+ < td >
358
+ < span class = "key" > ↑</ span > ,
359
+ < span class = "key" > ctrl</ span > + < span class = "key" > k</ span >
360
+ </ td >
361
+ < td > Move up in search results</ td >
362
+ </ tr >
363
+ < tr >
364
+ < td > < span class = "key" > ↵</ span > </ td >
365
+ < td > Go to active search result</ td >
366
+ </ tr >
367
+ </ table > ;
378
368
}
379
369
380
370
function IntroMsg ( ) {
381
- return h ( 'p' , { } ,
382
- "You can find any exported type, constructor, class, function or pattern defined in this package by (approximate) name."
383
- ) ;
371
+ return < p > You can find any exported type, constructor, class, function or pattern defined in this package by (approximate) name.</ p > ;
384
372
}
385
373
386
374
function NoResultsMsg ( props : { searchString : string } ) {
387
- var messages = [
388
- h ( 'p' , { } ,
389
- "Your search for '" + props . searchString + "' produced the following list of results: " ,
390
- h ( 'code' , { } , '[]' ) ,
391
- "."
392
- ) ,
393
- h ( 'p' , { } ,
394
- h ( 'code' , { } , 'Nothing' ) ,
395
- " matches your query for '" + props . searchString + "'."
396
- ) ,
397
- h ( 'p' , { } ,
398
- h ( 'code' , { } , 'Left "no matches for \'' + props . searchString + '\'" :: Either String (NonEmpty SearchResult)' )
399
- )
375
+ const messages = [
376
+ < p >
377
+ Your search for '{ props . searchString } ' produced the following list of results: < code > []</ code > .
378
+ </ p > ,
379
+ < p >
380
+ < code > Nothing</ code > matches your query for '{ props . searchString } '.
381
+ </ p > ,
382
+ < p >
383
+ < code >
384
+ Left "no matches for '{ props . searchString } '" :: Either String (NonEmpty SearchResult)
385
+ </ code >
386
+ </ p >
400
387
] ;
401
388
402
389
return messages [ ( props . searchString || 'a' ) . charCodeAt ( 0 ) % messages . length ] ;
403
390
}
404
391
405
392
export function init ( docBaseUrl ?: string , showHide ?: ( action : ( ) => void ) => void ) {
406
- const props = {
407
- baseUrl : docBaseUrl || "." ,
408
- showHideTrigger : showHide || addSearchPageMenuButton
409
- } ;
410
- preact . render ( h ( QuickJump , props ) , document . body ) ;
393
+ preact . render (
394
+ < QuickJump baseUrl = { docBaseUrl || "." } showHideTrigger = { showHide || addSearchPageMenuButton } /> ,
395
+ document . body
396
+ ) ;
411
397
}
412
398
413
399
// export to global object
0 commit comments