Use fuzzysort to filter languages

This commit is contained in:
Jonatan Heyman 2024-07-27 11:26:10 +02:00
parent 2f7374cca9
commit 43652ccaa9

View File

@ -1,4 +1,5 @@
<script> <script>
import fuzzysort from 'fuzzysort'
import { LANGUAGES } from '../editor/languages.js' import { LANGUAGES } from '../editor/languages.js'
const items = LANGUAGES.map(l => { const items = LANGUAGES.map(l => {
@ -11,6 +12,10 @@
}) })
items.unshift({token: "auto", name:"Auto-detect"}) items.unshift({token: "auto", name:"Auto-detect"})
items.forEach((item, idx) => {
item.preparedName = fuzzysort.prepare(item.name)
})
export default { export default {
data() { data() {
return { return {
@ -26,8 +31,17 @@
computed: { computed: {
filteredItems() { filteredItems() {
return items.filter((lang) => { if (this.filter === "") {
return lang.name.toLowerCase().indexOf(this.filter.toLowerCase()) !== -1 return items
}
const searchResults = fuzzysort.go(this.filter, items, {
keys: ['name'],
})
return searchResults.map(result => {
return {
"token": result.obj.token,
"name": result[0].highlight("<b>", "</b>")
}
}) })
}, },
}, },
@ -96,9 +110,8 @@
:class="idx === selected ? 'selected' : ''" :class="idx === selected ? 'selected' : ''"
@click="selectItem(item.token)" @click="selectItem(item.token)"
ref="item" ref="item"
> v-html="item.name"
{{ item.name }} />
</li>
</ul> </ul>
</form> </form>
</div> </div>
@ -173,4 +186,6 @@
&.selected &.selected
background: #1b6540 background: #1b6540
color: rgba(255,255,255, 0.87) color: rgba(255,255,255, 0.87)
::v-deep b
font-weight: 700
</style> </style>