From 296a7b6eae55193a88376e02d846192c416a602a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=89=E5=92=B2=E6=99=BA=E5=AD=90?= Date: Tue, 3 Jan 2023 04:56:12 +0800 Subject: [PATCH] feat(publish): support search for language list --- components/publish/PublishWidget.vue | 40 ++++++++++++++++++++++------ 1 file changed, 32 insertions(+), 8 deletions(-) diff --git a/components/publish/PublishWidget.vue b/components/publish/PublishWidget.vue index 071cdfed..c029d0d0 100644 --- a/components/publish/PublishWidget.vue +++ b/components/publish/PublishWidget.vue @@ -4,6 +4,7 @@ import { fileOpen } from 'browser-fs-access' import { useDropZone } from '@vueuse/core' import { EditorContent } from '@tiptap/vue-3' import ISO6391 from 'iso-639-1' +import Fuse from 'fuse.js' import type { Draft } from '~/types' type FileUploadError = [filename: string, message: string] @@ -185,6 +186,29 @@ async function onDrop(files: File[] | null) { const { isOverDropZone } = useDropZone(dropZoneRef, onDrop) +const languageKeyword = $ref('') +const languageList: { + code: string | null + nativeName: string + name?: string +}[] = [{ + code: null, + nativeName: 'None', +}, ...ISO6391.getAllCodes().map(code => ({ + code, + nativeName: ISO6391.getNativeName(code), + name: ISO6391.getName(code), +}))] +const fuse = new Fuse(languageList, { + keys: ['code', 'nativeName', 'name'], + shouldSort: true, +}) +const languages = $computed(() => + languageKeyword.trim() + ? fuse.search(languageKeyword).map(r => r.item) + : languageList, +) + defineExpose({ focusEditor: () => { editor.value?.commands?.focus?.() @@ -336,24 +360,24 @@ defineExpose({