|
60 | 60 | </div> |
61 | 61 | </template> |
62 | 62 |
|
63 | | -<script> |
| 63 | +<script setup lang="ts"> |
| 64 | +import 'prismjs/components/prism-json' |
| 65 | +import { defineComponent, onMounted, ref, shallowRef } from 'vue' |
| 66 | +
|
64 | 67 | import Fuse from '../../../../dist/fuse.esm.js' |
65 | 68 | import Books from './books.js' |
66 | 69 |
|
67 | 70 | import Prism from 'prismjs' |
68 | | -import 'prismjs/components/prism-json' |
69 | 71 |
|
70 | | -import { codemirror } from 'vue-codemirror' |
71 | | -import 'codemirror/lib/codemirror.css' |
72 | | -import 'codemirror/mode/javascript/javascript.js' |
73 | | -import 'codemirror/theme/monokai.css' |
| 72 | +// import { javascript } from '@codemirror/lang-javascript' |
| 73 | +import { javascript } from '@codemirror/lang-javascript' |
| 74 | +import { oneDark } from '@codemirror/theme-one-dark' |
| 75 | +import { Codemirror } from 'vue-codemirror' |
74 | 76 |
|
75 | 77 | let keys = [] |
76 | 78 | for (const key in Fuse.config) { |
@@ -98,114 +100,129 @@ return fuse.search(pattern)` |
98 | 100 |
|
99 | 101 | // Purely here to make two-way binding possible: |
100 | 102 | // Extend Fuse such that we can expose the pattern that was |
101 | | -// modified direclty in the CodeMirror input. |
| 103 | +// modified directly in the CodeMirror input. |
102 | 104 | class DemoFuse extends Fuse { |
103 | | - constructor(list, options, index) { |
104 | | - super(list, options, index) |
105 | | - } |
106 | | - search(pattern, opts = { limit: false }) { |
| 105 | + public override search(pattern: any, opts = { limit: -1 }) { |
107 | 106 | const results = super.search(pattern, opts) |
108 | 107 | return { pattern, results } |
109 | 108 | } |
110 | 109 | } |
111 | 110 |
|
112 | | -export default { |
113 | | - name: 'Demo', |
| 111 | +let listJSON = ref(JSON.stringify(Books, null, 2)) |
| 112 | +let list = ref(Books) |
| 113 | +let code = ref(codify('')) |
| 114 | +let result = ref('') |
| 115 | +let outputHtml = ref('') |
| 116 | +let count = ref(0) |
| 117 | +let searchTime = ref<string | number>(0) |
| 118 | +let listErrorMessage = ref('') |
| 119 | +let codeErrorMessage = ref('') |
| 120 | +let hasErrors = ref(false) |
| 121 | +let pattern = ref('') |
| 122 | +let showCode = ref(true) |
| 123 | +let listOptions = ref({ |
| 124 | + tabSize: 2, |
| 125 | + mode: 'text/javascript', |
| 126 | + theme: 'default', |
| 127 | + lineNumbers: true, |
| 128 | + line: true |
| 129 | +}) |
| 130 | +let cmOptions = ref({ |
| 131 | + tabSize: 2, |
| 132 | + mode: 'text/javascript', |
| 133 | + theme: 'default', |
| 134 | + lineNumbers: true, |
| 135 | + line: true |
| 136 | +}) |
| 137 | +
|
| 138 | +function toggleCode() { |
| 139 | + showCode.value = !showCode.value |
| 140 | +} |
| 141 | +
|
| 142 | +function onCmCodeChange(newCode) { |
| 143 | + code.value = newCode |
| 144 | + try { |
| 145 | + parse() |
| 146 | + update() |
| 147 | + } catch (err) {} |
| 148 | +} |
| 149 | +
|
| 150 | +function onCmListChange(newCode) { |
| 151 | + try { |
| 152 | + list.value = eval(newCode) |
| 153 | + listErrorMessage.value = null |
| 154 | + hasErrors.value = !!codeErrorMessage.value |
| 155 | + update() |
| 156 | + } catch (err) { |
| 157 | + listErrorMessage.value = err |
| 158 | + hasErrors.value = true |
| 159 | + outputHtml.value = '' |
| 160 | + throw err |
| 161 | + } |
| 162 | +} |
| 163 | +
|
| 164 | +function parse() { |
| 165 | + try { |
| 166 | + let func = eval(`[function (Fuse, list){${this.code}}][0]`) |
| 167 | +
|
| 168 | + let start = new Date().getTime() |
| 169 | + const { pattern, results } = func(DemoFuse, this.list) |
| 170 | + result.value = results |
| 171 | + pattern.value = pattern |
| 172 | + let end = new Date().getTime() |
| 173 | + searchTime.value = end - start + ' ms' |
| 174 | + codeErrorMessage.value = null |
| 175 | + hasErrors.value = !!this.listErrorMessage |
| 176 | + } catch (err) { |
| 177 | + codeErrorMessage.value = err |
| 178 | + hasErrors.value = true |
| 179 | + outputHtml.value = '' |
| 180 | + throw err |
| 181 | + } |
| 182 | +} |
| 183 | +
|
| 184 | +function update() { |
| 185 | + if (hasErrors.value) { |
| 186 | + return |
| 187 | + } |
| 188 | + const html = Prism.highlight( |
| 189 | + JSON.stringify(this.result, null, 2), |
| 190 | + Prism.languages.json, |
| 191 | + 'json' |
| 192 | + ) |
| 193 | + count.value = this.result.length |
| 194 | + outputHtml.value = html |
| 195 | +} |
| 196 | +function onPatternKeyUp() { |
| 197 | + code.value = codify(this.pattern) |
| 198 | +} |
| 199 | +
|
| 200 | +onMounted(() => { |
| 201 | + parse() |
| 202 | + update() |
| 203 | +}) |
| 204 | +
|
| 205 | +defineComponent({ |
114 | 206 | components: { |
115 | | - codemirror |
| 207 | + Codemirror |
116 | 208 | }, |
117 | | - data: () => ({ |
118 | | - listJSON: JSON.stringify(Books, null, 2), |
119 | | - list: Books, |
120 | | - code: codify(''), |
121 | | - result: '', |
122 | | - outputHtml: '', |
123 | | - count: 0, |
124 | | - searchTime: 0, |
125 | | - listErrorMessage: '', |
126 | | - codeErrorMessage: '', |
127 | | - hasErrors: false, |
128 | | - pattern: '', |
129 | | - showCode: true, |
130 | | - listOptions: { |
131 | | - tabSize: 2, |
132 | | - mode: 'text/javascript', |
133 | | - theme: 'default', |
134 | | - lineNumbers: true, |
135 | | - line: true |
136 | | - }, |
137 | | - cmOptions: { |
138 | | - tabSize: 2, |
139 | | - mode: 'text/javascript', |
140 | | - theme: 'default', |
141 | | - lineNumbers: true, |
142 | | - line: true |
| 209 | + setup: () => { |
| 210 | + const extensions = [javascript(), oneDark] |
| 211 | +
|
| 212 | + // Codemirror EditorView instance ref |
| 213 | + const view = shallowRef() |
| 214 | + const handleReady = (payload) => { |
| 215 | + view.value = payload.view |
143 | 216 | } |
144 | | - }), |
145 | | - methods: { |
146 | | - toggleCode() { |
147 | | - this.showCode = !this.showCode |
148 | | - }, |
149 | | - onCmCodeChange(newCode) { |
150 | | - this.code = newCode |
151 | | - try { |
152 | | - this.parse() |
153 | | - this.update() |
154 | | - } catch (err) {} |
155 | | - }, |
156 | | - onCmListChange(newCode) { |
157 | | - try { |
158 | | - this.list = eval(newCode) |
159 | | - this.listErrorMessage = null |
160 | | - this.hasErrors = !!this.codeErrorMessage |
161 | | - this.update() |
162 | | - } catch (err) { |
163 | | - this.listErrorMessage = err |
164 | | - this.hasErrors = true |
165 | | - this.outputHtml = '' |
166 | | - throw err |
167 | | - } |
168 | | - }, |
169 | | - parse() { |
170 | | - try { |
171 | | - let func = eval(`[function (Fuse, list){${this.code}}][0]`) |
172 | 217 |
|
173 | | - let start = new Date().getTime() |
174 | | - const { pattern, results } = func(DemoFuse, this.list) |
175 | | - this.result = results |
176 | | - this.pattern = pattern |
177 | | - let end = new Date().getTime() |
178 | | - this.searchTime = end - start + ' ms' |
179 | | - this.codeErrorMessage = null |
180 | | - this.hasErrors = !!this.listErrorMessage |
181 | | - } catch (err) { |
182 | | - this.codeErrorMessage = err |
183 | | - this.hasErrors = true |
184 | | - this.outputHtml = '' |
185 | | - throw err |
186 | | - } |
187 | | - }, |
188 | | - update() { |
189 | | - if (this.hasErrors) { |
190 | | - return |
191 | | - } |
192 | | - const html = Prism.highlight( |
193 | | - JSON.stringify(this.result, null, 2), |
194 | | - Prism.languages.json, |
195 | | - 'json' |
196 | | - ) |
197 | | - this.count = this.result.length |
198 | | - this.outputHtml = html |
199 | | - }, |
200 | | - onPatternKeyUp() { |
201 | | - this.code = codify(this.pattern) |
| 218 | + return { |
| 219 | + code, |
| 220 | + extensions, |
| 221 | + handleReady, |
| 222 | + log: console.log |
202 | 223 | } |
203 | | - }, |
204 | | - mounted() { |
205 | | - this.parse() |
206 | | - this.update() |
207 | 224 | } |
208 | | -} |
| 225 | +}) |
209 | 226 | </script> |
210 | 227 |
|
211 | 228 | <style lang="css"> |
|
0 commit comments