1
1
<script lang =" ts" >
2
- import SvelteSpotlight from ' svelte-spotlight/SvelteSpotlight.svelte' ;
3
- import { createEventDispatcher } from ' svelte' ;
4
- import type { SearchResult } from ' ../../types' ;
2
+ import SvelteSpotlight from ' svelte-spotlight/SvelteSpotlight.svelte' ;
3
+ import { createEventDispatcher } from ' svelte' ;
4
+ import type { SearchResult } from ' ../../types' ;
5
5
6
- export let query = " " ;
7
- export let results : SearchResult [] = [];
8
- export let isOpen = false ;
6
+ export let query = ' ' ;
7
+ export let results: SearchResult [] = [];
8
+ export let isOpen = false ;
9
9
10
- const dispatch = createEventDispatcher ();
10
+ const dispatch = createEventDispatcher ();
11
11
12
- $ : onOpenChange (isOpen );
13
- $ : onQueryChange (query );
12
+ $ : onOpenChange (isOpen );
13
+ $ : onQueryChange (query );
14
14
15
- function onOpenChange(newIsOpen : boolean ) {
16
- dispatch (' openchange' , { newIsOpen })
17
- }
15
+ function onOpenChange(newIsOpen : boolean ) {
16
+ dispatch (' openchange' , { newIsOpen });
17
+ }
18
18
19
- function onQueryChange(newQuery : string ) {
20
- dispatch (' querychange' , { newQuery });
21
- }
19
+ function onQueryChange(newQuery : string ) {
20
+ dispatch (' querychange' , { newQuery });
21
+ }
22
22
23
- function escape(text : string ) {
24
- return text .replace (/ </ g , ' <' ).replace (/ >/ g , ' >' );
25
- }
23
+ function escape(text : string ) {
24
+ return text .replace (/ </ g , ' <' ).replace (/ >/ g , ' >' );
25
+ }
26
26
27
- function excerpt(content : string , query : string ) {
28
- const index = content .toLowerCase ().indexOf (query .toLowerCase ());
29
- if (index === - 1 ) {
30
- return content .slice (0 , 100 );
31
- }
27
+ function excerpt(content : string , query : string ) {
28
+ const index = content .toLowerCase ().indexOf (query .toLowerCase ());
29
+ if (index === - 1 ) {
30
+ return content .slice (0 , 100 );
31
+ }
32
32
33
- const prefix = index > 20
34
- ? ` ...${content .slice (index - 15 , index )} `
35
- : content .slice (0 , index );
33
+ const prefix = index > 20 ? ` ...${content .slice (index - 15 , index )} ` : content .slice (0 , index );
36
34
37
- const suffix = content .slice (
38
- index + query .length ,
39
- index + query .length + (80 - (prefix .length + query .length ))
40
- );
35
+ const suffix = content .slice (
36
+ index + query .length ,
37
+ index + query .length + (80 - (prefix .length + query .length ))
38
+ );
41
39
42
- return (
43
- escape (prefix ) +
44
- ` <mark>${escape (content .slice (index , index + query .length ))}</mark> ` +
45
- escape (suffix )
46
- );
40
+ return (
41
+ escape (prefix ) +
42
+ ` <mark>${escape (content .slice (index , index + query .length ))}</mark> ` +
43
+ escape (suffix )
44
+ );
45
+ }
47
46
48
- }
49
-
50
- function onKeyboardSelect(event : CustomEvent <SearchResult >) {
51
- dispatch (' select' , { href: event .detail .href });
52
- }
47
+ function onKeyboardSelect(event : CustomEvent <SearchResult >) {
48
+ dispatch (' select' , { href: event .detail .href });
49
+ }
53
50
</script >
54
51
55
52
<SvelteSpotlight
56
- {results }
57
- bind:query
58
- bind:isOpen
59
- modalClass ={' w-[600px] max-w-[90%] bg-feDarkBlue-600 shadow-lg rounded-lg' }
60
- headerClass ={' py-3 px-10 border-b-2 border-gray-500 border-b-solid' }
61
- inputClass =" text-gray-50 focus:outline-none bg-transparent"
62
- contentClass =" py-3 [& _li]:py-2"
63
- resultIdKey =" href"
64
- on:select ={(event ) => {
65
- onKeyboardSelect (event );
66
- }}
53
+ {results }
54
+ bind:query
55
+ bind:isOpen
56
+ modalClass ={' w-[600px] max-w-[90%] bg-feDarkBlue-600 shadow-lg rounded-lg' }
57
+ headerClass ={' py-3 px-10 border-b-2 border-gray-500 border-b-solid' }
58
+ inputClass =" text-gray-50 focus:outline-none bg-transparent"
59
+ contentClass =" py-3 [& _li]:py-2"
60
+ resultIdKey =" href"
61
+ on:select ={(event ) => {
62
+ onKeyboardSelect (event );
63
+ }}
67
64
>
68
- <div
69
- slot =" result"
70
- let:selected
71
- let:result
72
- class ={` [&_mark]:bg-feLightBlue-600 cursor-pointer hover:bg-feDarkBlue-800 text-sm w-full ${selected ? " bg-feDarkBlue-500" : ' ' } ` }
73
- >
74
- <a href ={result .href }
75
- class =" ml-10"
76
- on:click ={_ => dispatch (' select' , { href: result .href })}
77
- >
78
- <strong >{@html excerpt (result .title , query )}</strong >
79
- {#if result .content }
80
- <div class ="mx-10 text-slate-500 text-sm truncate" >{@html excerpt (result .content , query )}</div >
81
- {/if }
82
- </a >
83
- </div >
65
+ <div
66
+ slot =" result"
67
+ let:selected
68
+ let:result
69
+ class ={` [&_mark]:bg-feLightBlue-600 cursor-pointer hover:bg-feDarkBlue-800 text-sm w-full ${
70
+ selected ? ' bg-feDarkBlue-500' : ' '
71
+ } ` }
72
+ >
73
+ <a href ={result .href } class ="ml-10" on:click ={(_ ) => dispatch (' select' , { href: result .href })}>
74
+ <strong >{@html excerpt (result .title , query )}</strong >
75
+ {#if result .content }
76
+ <div class =" mx-10 text-slate-500 text-sm truncate" >
77
+ {@html excerpt (result .content , query )}
78
+ </div >
79
+ {/if }
80
+ </a >
81
+ </div >
84
82
85
- <div slot =" noResults" class =" ml-10 text-slate-500 text-sm" >
86
- No results...
87
- </div >
88
- </SvelteSpotlight >
83
+ <div slot =" noResults" class =" ml-10 text-slate-500 text-sm" >No results...</div >
84
+ </SvelteSpotlight >
0 commit comments