Skip to content

Commit 46c561c

Browse files
committed
feat: implementing reacting to option changes
1 parent fe0d33e commit 46c561c

2 files changed

Lines changed: 36 additions & 15 deletions

File tree

docs/.vuepress/components/Demo/Demo.vue

Lines changed: 35 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
id="main-monaco-editor"
2727
file-name="main.js"
2828
file-description="entry module"
29+
@update:modelValue="updateMainJsData"
2930
></MonacoEditor>
3031
</section>
3132

@@ -47,6 +48,7 @@
4748

4849
<script setup lang="ts">
4950
import { Stopwatch } from '@sapphire/stopwatch'
51+
import {isNullish, isObject, tryParseJSON} from '@sapphire/utilities'
5052
import { reactive } from 'vue'
5153
import type FuseTypes from '../../../../dist/fuse'
5254
import Fuse from '../../../../dist/fuse.esm.js'
@@ -59,7 +61,7 @@ interface State {
5961
mainJsData: string
6062
resultsData: string
6163
count: number | null
62-
searchTime: number | null
64+
searchTime: string | null
6365
fuseSearchOptions: FuseTypes.IFuseOptions<never>
6466
}
6567
@@ -76,14 +78,13 @@ const defaultFuseSearchOptions: FuseTypes.IFuseOptions<never> = {
7678
useExtendedSearch: false,
7779
ignoreLocation: false,
7880
ignoreFieldNorm: false,
79-
fieldNormWeight: 1,
80-
keys: ['title', 'author.firstName']
81+
fieldNormWeight: 1
8182
}
8283
83-
let codify = (
84+
function codify(
8485
searchPattern: string,
8586
fuseSearchOptions: FuseTypes.IFuseOptions<never> = defaultFuseSearchOptions
86-
): string => {
87+
): string {
8788
return `
8889
const Fuse = require('fuse.js');
8990
@@ -115,10 +116,29 @@ const state = reactive<State>({
115116
fuseSearchOptions: defaultFuseSearchOptions
116117
})
117118
118-
const isNullish = <T>(
119-
value: null | undefined | T
120-
): value is null | undefined => {
121-
return value === null || value === undefined
119+
function removeComments(str: string) {
120+
let lines = str.split('\n')
121+
lines = lines.filter((line) => !line.startsWith('//'))
122+
return lines.join('\n')
123+
}
124+
125+
function updateMainJsData(newValue: string) {
126+
const newFuseOptionsString = removeComments(
127+
newValue
128+
.replace(/const Fuse = require\('fuse\.js'\);\n\n/, '')
129+
.replace(/const fuse = new Fuse\(list, fuseOptions\);\n\n/, '')
130+
.replace(/\/\/ Change the pattern\n/, '')
131+
.replace(/const searchPattern = ".+"\n\n/, '')
132+
.replace(/return fuse\.search\(searchPattern\)/, '')
133+
.replace(/\n\n/, '')
134+
.replace(/\t/g, '')
135+
.replaceAll(';', '')
136+
.replace(/const fuseOptions = /, '')
137+
).replaceAll(/([a-zA-Z]+):/g, '"$1":')
138+
139+
const newFuseOptions = tryParseJSON(newFuseOptionsString)
140+
141+
doFuseSearch(isObject(newFuseOptions) ? newFuseOptions : undefined)
122142
}
123143
124144
function onSearchPatternKeyUp() {
@@ -127,21 +147,22 @@ function onSearchPatternKeyUp() {
127147
doFuseSearch()
128148
}
129149
130-
function doFuseSearch() {
150+
function doFuseSearch(fuseSearchOptions = state.fuseSearchOptions) {
131151
try {
132152
const fuseOptions: FuseTypes.IFuseOptions<never> = {
133-
...state.fuseSearchOptions,
134-
keys: ['title', 'author.firstName']
153+
keys: ['title', 'author.firstName'],
154+
...fuseSearchOptions
135155
}
136156
137-
const fuse = new Fuse(JSON.parse(state.jsonData), state.fuseSearchOptions)
157+
const fuse = new Fuse(JSON.parse(state.jsonData), fuseOptions)
138158
139159
const stopwatch = new Stopwatch()
140160
141161
const result = fuse.search(state.searchPattern)
162+
const searchTime = stopwatch.stop().duration
142163
143164
state.count = result.length
144-
state.searchTime = Math.floor(stopwatch.stop().duration)
165+
state.searchTime = searchTime.toFixed(1)
145166
state.resultsData = JSON.stringify(result, null, 2)
146167
} catch {
147168
state.count = null

docs/.vuepress/components/Demo/MonacoEditor.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<script setup lang="ts">
22
import * as Monaco from 'monaco-editor'
3-
import { computed, defineProps, onMounted, ref, watch } from 'vue'
3+
import { computed, onMounted, ref, watch } from 'vue'
44
import loader from '@monaco-editor/loader';
55
66
interface Emits {

0 commit comments

Comments
 (0)