2
2
<section >
3
3
<MonacoEditor
4
4
language =" json"
5
- :value = " jsonData"
5
+ v-model = " state. jsonData"
6
6
id =" json-list-monaco-editor"
7
7
file-name =" list.json"
8
8
file-description =" list of items to search"
13
13
<input
14
14
type =" text"
15
15
class =" search-section-input"
16
- v-model =" searchPattern"
16
+ v-model =" state. searchPattern"
17
17
@keyup =" onSearchPatternKeyUp"
18
18
placeholder =" Search..."
19
19
/>
20
20
</section >
21
21
22
22
<section >
23
23
<MonacoEditor
24
- language =" typescript "
25
- :value = " mainJsData"
24
+ language =" javascript "
25
+ v-model = " state. mainJsData"
26
26
id =" main-monaco-editor"
27
27
file-name =" main.js"
28
28
file-description =" entry module"
32
32
<section class =" monaco-editor-results" >
33
33
<MonacoEditor
34
34
language =" json"
35
- :value = " resultsData"
35
+ v-model = " state. resultsData"
36
36
id =" main-monaco-editor"
37
37
file-name =" Results:"
38
38
:file-description ="
39
- isNullish(count) || isNullish(searchTime)
39
+ isNullish(state. count) || isNullish(state. searchTime)
40
40
? ''
41
- : 'found ' + count + ' in ' + searchTime + 'ms'
41
+ : 'found ' + state. count + ' in ' + state. searchTime + 'ms'
42
42
"
43
43
readonly
44
44
></MonacoEditor >
47
47
48
48
<script setup lang="ts">
49
49
import { Stopwatch } from ' @sapphire/stopwatch'
50
- import { ref } from ' vue'
50
+ import { reactive } from ' vue'
51
+ import type FuseTypes from ' ../../../../dist/fuse'
51
52
import Fuse from ' ../../../../dist/fuse.esm.js'
52
53
import Books from ' ./books.js'
53
54
import MonacoEditor from ' ./MonacoEditor.vue'
54
55
55
- const searchPattern = ref (' ' )
56
- const fuseSearchOptions = ref ({
56
+ interface State {
57
+ searchPattern: string
58
+ jsonData: string
59
+ mainJsData: string
60
+ resultsData: string
61
+ count: number | null
62
+ searchTime: number | null
63
+ fuseSearchOptions: FuseTypes .IFuseOptions <never >
64
+ }
65
+
66
+ const defaultFuseSearchOptions: FuseTypes .IFuseOptions <never > = {
57
67
isCaseSensitive: false ,
58
68
includeScore: false ,
59
69
shouldSort: true ,
@@ -66,15 +76,21 @@ const fuseSearchOptions = ref({
66
76
useExtendedSearch: false ,
67
77
ignoreLocation: false ,
68
78
ignoreFieldNorm: false ,
69
- fieldNormWeight: 1
70
- })
79
+ fieldNormWeight: 1 ,
80
+ keys: [' title' , ' author.firstName' ]
81
+ }
71
82
72
- let codify = () => {
83
+ let codify = (
84
+ searchPattern : string ,
85
+ fuseSearchOptions : FuseTypes .IFuseOptions <never > = defaultFuseSearchOptions
86
+ ): string => {
73
87
return `
74
88
const Fuse = require('fuse.js');
75
89
76
90
const fuseOptions = {
77
- ${Object .entries (fuseSearchOptions ).map (([key , value ]) => ` \t // ${key }: ${value },` ).join (' \n ' )}
91
+ ${Object .entries (fuseSearchOptions )
92
+ .map (([key , value ]) => ` \t // ${key }: ${value },` )
93
+ .join (' \n ' )}
78
94
\t keys: [
79
95
\t\t "title",
80
96
\t\t "author.firstName"
@@ -84,105 +100,55 @@ ${Object.entries(fuseSearchOptions).map(([key, value]) => `\t// ${key}: ${value}
84
100
const fuse = new Fuse(list, fuseOptions);
85
101
86
102
// Change the pattern
87
- const pattern = "${searchPattern . value }"
103
+ const searchPattern = "${searchPattern }"
88
104
89
- return fuse.search(pattern ) `
105
+ return fuse.search(searchPattern ) `
90
106
}
91
107
108
+ const state = reactive <State >({
109
+ searchPattern: ' ' ,
110
+ jsonData: JSON .stringify (Books , null , 2 ),
111
+ mainJsData: codify (' ' ),
112
+ resultsData: JSON .stringify ({}, null , 2 ),
113
+ count: null ,
114
+ searchTime: null ,
115
+ fuseSearchOptions: defaultFuseSearchOptions
116
+ })
117
+
92
118
const isNullish = <T >(
93
119
value : null | undefined | T
94
120
): value is null | undefined => {
95
121
return value === null || value === undefined
96
122
}
97
123
98
- const jsonData = ref (JSON .stringify (Books , null , 2 ))
99
- const mainJsData = ref (codify ())
100
- const resultsData = ref (JSON .stringify ({}, null , 2 ))
101
- const count = ref <number | null >(null )
102
- const searchTime = ref <number | null >(null )
103
-
104
- console .log (' >>>>' , resultsData .value )
105
-
106
124
function onSearchPatternKeyUp() {
107
- mainJsData . value = codify ()
125
+ state . mainJsData = codify (state . searchPattern , state . fuseSearchOptions )
108
126
109
127
doFuseSearch ()
110
128
}
111
129
112
130
function doFuseSearch() {
113
131
try {
114
- const fuseOptions = {
115
- // isCaseSensitive: false,
116
- // includeScore: false,
117
- // shouldSort: true,
118
- // includeMatches: false,
119
- // findAllMatches: false,
120
- // minMatchCharLength: 1,
121
- // location: 0,
122
- // threshold: 0.6,
123
- // distance: 100,
124
- // useExtendedSearch: false,
125
- // ignoreLocation: false,
126
- // ignoreFieldNorm: false,
127
- // fieldNormWeight: 1,
132
+ const fuseOptions: FuseTypes .IFuseOptions <never > = {
133
+ ... state .fuseSearchOptions ,
128
134
keys: [' title' , ' author.firstName' ]
129
135
}
130
136
131
- const fuse = new Fuse (JSON .parse (jsonData . value ), fuseOptions )
137
+ const fuse = new Fuse (JSON .parse (state . jsonData ), state . fuseSearchOptions )
132
138
133
139
const stopwatch = new Stopwatch ()
134
140
135
- const result = fuse .search (searchPattern . value )
141
+ const result = fuse .search (state . searchPattern )
136
142
137
- count . value = result .length
138
- searchTime . value = Math .floor (stopwatch .stop ().duration )
139
- resultsData . value = JSON .stringify (result , null , 2 )
143
+ state . count = result .length
144
+ state . searchTime = Math .floor (stopwatch .stop ().duration )
145
+ state . resultsData = JSON .stringify (result , null , 2 )
140
146
} catch {
141
- count . value = null
142
- searchTime . value = null
143
- resultsData . value = JSON .stringify ({}, null , 2 )
147
+ state . count = null
148
+ state . searchTime = null
149
+ state . resultsData = JSON .stringify ({}, null , 2 )
144
150
}
145
151
}
146
-
147
- // function onCmCodeChange(newCode) {
148
- // code.value = newCode
149
- // try {
150
- // parse()
151
- // update()
152
- // } catch (err) {}
153
- // }
154
-
155
- // function onCmListChange(newCode) {
156
- // try {
157
- // list.value = eval(newCode)
158
- // listErrorMessage.value = null
159
- // hasErrors.value = !!codeErrorMessage.value
160
- // update()
161
- // } catch (err) {
162
- // listErrorMessage.value = err
163
- // hasErrors.value = true
164
- // outputHtml.value = ''
165
- // throw err
166
- // }
167
- // }
168
-
169
- // function update() {
170
- // if (hasErrors.value) {
171
- // return
172
- // }
173
- // const html = Prism.highlight(
174
- // JSON.stringify(result.value, null, 2),
175
- // Prism.languages.json,
176
- // 'json'
177
- // )
178
- // count.value = result.value.length
179
- // outputHtml.value = html
180
- // }
181
-
182
- // onMounted(() => {
183
- // // parse()
184
- // // update()
185
- // })
186
152
</script >
187
153
188
154
<style scoped lang="css">
0 commit comments