diff --git a/app/components/Editor.vue b/app/components/Editor.vue index 2f71ad1..afe9598 100644 --- a/app/components/Editor.vue +++ b/app/components/Editor.vue @@ -210,7 +210,6 @@ defineEmits([ const { orientation, language } = toRefs(props); const { $bus, $shiki } = useNuxtApp(); -const { options: languageOptions } = useLanguages(); const root = ref(null); const monaco = ref(null); @@ -231,7 +230,7 @@ defineExpose({ focusToggle: () => hideButton.value?.$el?.focus({ preventScroll: true }), }); -const languages = computed(() => languageOptions($shiki.languages())); +const languages = computed(() => $shiki.languageOptions()); const landscape = computed(() => ['left', 'right'].includes(orientation.value)); const languageAlias = computed( diff --git a/app/components/ModalPreferences.vue b/app/components/ModalPreferences.vue index 5639303..40ea1dc 100644 --- a/app/components/ModalPreferences.vue +++ b/app/components/ModalPreferences.vue @@ -37,20 +37,14 @@ - + @@ -571,7 +565,6 @@ defineProps({ defineEmits(['update:modelValue']); const { $shiki } = useNuxtApp(); -const { options: languageOptions } = useLanguages(); const activeTab = ref('editor'); const isAutoColorScheme = ref(null); const customAspectRatioWidth = ref(''); @@ -581,7 +574,7 @@ const { types: socialTypes, positions: socialPositions } = useSocials(); const { fontFamilies } = useFonts(); -const languages = computed(() => languageOptions($shiki.languages())); +const languages = computed(() => $shiki.languageOptions()); const tabs = [ { value: 'editor', label: 'Editor', icon: CodeIcon }, diff --git a/app/components/SearchableSelect.vue b/app/components/SearchableSelect.vue index 35592f5..f407948 100644 --- a/app/components/SearchableSelect.vue +++ b/app/components/SearchableSelect.vue @@ -4,7 +4,7 @@ @@ -49,20 +49,20 @@
+ props.options.find((option) => option.value === props.modelValue) ?? + props.options.find((option) => option.aliases.includes(props.modelValue)) +); +const selectedLabel = computed(() => selectedOption.value?.label ?? props.modelValue); + const filteredOptions = computed(() => { const query = search.value.trim().toLowerCase(); @@ -110,18 +117,31 @@ const filteredOptions = computed(() => { return props.options; } - return props.options.filter((option) => option.toLowerCase().includes(query)); + const exact = []; + const partial = []; + + for (const option of props.options) { + const terms = [option.label, option.value, ...option.aliases].map((term) => + term.toLowerCase() + ); + + if (terms.includes(query)) { + exact.push(option); + } else if (terms.some((term) => term.includes(query))) { + partial.push(option); + } + } + + return [...exact, ...partial]; }); -function selectOption(option) { - emit('update:modelValue', option); +function selectOption(value) { + emit('update:modelValue', value); open.value = false; } function activateCurrentOption() { - activeOption.value = filteredOptions.value.includes(props.modelValue) - ? props.modelValue - : filteredOptions.value[0]; + activeOption.value = selectedOption.value?.value ?? filteredOptions.value[0]?.value; } function moveActiveOption(direction) { @@ -130,14 +150,16 @@ function moveActiveOption(direction) { return; } - const currentIndex = filteredOptions.value.indexOf(activeOption.value); + const currentIndex = filteredOptions.value.findIndex( + (option) => option.value === activeOption.value + ); const fallbackIndex = direction > 0 ? -1 : 0; const nextIndex = (currentIndex === -1 ? fallbackIndex : currentIndex) + direction; activeOption.value = filteredOptions.value[ (nextIndex + filteredOptions.value.length) % filteredOptions.value.length - ]; + ].value; } function handleSearchKeydown(event) { @@ -189,9 +211,9 @@ watch(filteredOptions, () => { return; } - if (!filteredOptions.value.includes(activeOption.value)) { - activateCurrentOption(); - } + activeOption.value = search.value.trim() + ? filteredOptions.value[0]?.value + : (selectedOption.value?.value ?? filteredOptions.value[0]?.value); }); watch(activeOption, async () => { diff --git a/app/composables/useLanguages.js b/app/composables/useLanguages.js index f137b8f..dbd5e36 100644 --- a/app/composables/useLanguages.js +++ b/app/composables/useLanguages.js @@ -1,22 +1,21 @@ -import { orderBy } from 'lodash'; +import { invert } from 'lodash'; const languageAliases = { bash: 'shellscript', dockerfile: 'docker', }; +const editorLanguageIds = invert(languageAliases); + export default function () { function options(languages = []) { - return orderBy( - languages.map((language) => { - return ( - { - docker: 'dockerfile', - shellscript: 'bash', - }[language] ?? language - ); - }) - ); + return languages + .map(({ id, name, aliases = [] }) => ({ + value: editorLanguageIds[id] ?? id, + label: name, + aliases: [id, ...aliases], + })) + .sort((a, b) => a.label.localeCompare(b.label, 'en')); } function highlightLanguage(language) { diff --git a/app/plugins/shiki.client.js b/app/plugins/shiki.client.js index 98fcd70..c4e8fb9 100644 --- a/app/plugins/shiki.client.js +++ b/app/plugins/shiki.client.js @@ -1,4 +1,5 @@ -import { ref } from 'vue'; +import { computed, ref } from 'vue'; +import useLanguages from '@/composables/useLanguages'; export default defineNuxtPlugin(() => { const worker = new Worker(new URL('../workers/shiki.worker.js', import.meta.url), { @@ -6,7 +7,9 @@ export default defineNuxtPlugin(() => { }); const pending = new Map(); - const allLanguageIds = ref([]); + const allLanguages = ref([]); + const { options } = useLanguages(); + const languageOptions = computed(() => options(allLanguages.value)); const allThemeIds = ref([]); let requestId = 0; let failure = null; @@ -50,7 +53,7 @@ export default defineNuxtPlugin(() => { } const ready = send('initialize').then(({ languages, themes }) => { - allLanguageIds.value = languages; + allLanguages.value = languages; allThemeIds.value = themes; }); @@ -58,7 +61,8 @@ export default defineNuxtPlugin(() => { provide: { shiki: { ready, - languages: () => allLanguageIds.value, + languages: () => allLanguages.value.map(({ id }) => id), + languageOptions: () => languageOptions.value, themes: () => allThemeIds.value, async tokenizeBlocks(payload) { await ready; diff --git a/app/workers/shiki.worker.js b/app/workers/shiki.worker.js index 49234c6..90c8563 100644 --- a/app/workers/shiki.worker.js +++ b/app/workers/shiki.worker.js @@ -15,8 +15,8 @@ async function initialize() { }); const languages = bundledLanguagesInfo - .map(({ id }) => id) - .filter((id) => !['php-html', 'html-derivative'].includes(id)); + .filter(({ id }) => !['php-html', 'html-derivative'].includes(id)) + .map(({ id, name, aliases = [] }) => ({ id, name, aliases })); const bundledThemes = bundledThemesInfo .map(({ id }) => id) diff --git a/tests/components/Editor.test.js b/tests/components/Editor.test.js index 9c73adf..fec630a 100644 --- a/tests/components/Editor.test.js +++ b/tests/components/Editor.test.js @@ -56,6 +56,9 @@ describe('Editor', () => { }); it('shows the language, settings, and add controls in a compact dock', async () => { + vi.spyOn(useNuxtApp().$shiki, 'languageOptions').mockReturnValue([ + { value: 'php', label: 'PHP', aliases: ['php'] }, + ]); const wrapper = mount(Editor, { props: { id: 'first', language: 'php' }, global: { stubs: { Monaco: MonacoStub }, directives: { tooltip: {} } }, @@ -64,7 +67,7 @@ describe('Editor', () => { const dock = wrapper.get('[aria-label="Editor tools"]'); expect(dock.findAll('button')).toHaveLength(3); - expect(dock.get('[aria-label="Code language"]').text()).toBe('php'); + expect(dock.get('[aria-label="Code language"]').text()).toBe('PHP'); expect(dock.get('[aria-label="Editor settings"]').exists()).toBe(true); expect(wrapper.find('.workspace-statusbar').exists()).toBe(false); @@ -73,10 +76,13 @@ describe('Editor', () => { wrapper.unmount(); }); - it('searches languages from the upward-opening dropdown', async () => { - vi.spyOn(useNuxtApp().$shiki, 'languages').mockReturnValue(['php', 'javascript']); + it('prioritizes an exact alias and emits the saved language id', async () => { + vi.spyOn(useNuxtApp().$shiki, 'languageOptions').mockReturnValue([ + { value: 'json', label: 'JSON', aliases: ['json'] }, + { value: 'javascript', label: 'JavaScript', aliases: ['js', 'mjs'] }, + ]); const wrapper = mount(Editor, { - props: { id: 'first', language: 'php' }, + props: { id: 'first', language: 'json' }, attachTo: document.body, global: { stubs: { Monaco: MonacoStub }, directives: { tooltip: {} } }, }); @@ -87,7 +93,10 @@ describe('Editor', () => { const search = new DOMWrapper( document.querySelector('[placeholder="Search languages..."]') ); - await search.setValue('javascript'); + await search.setValue('JS'); + expect( + document.querySelector('[data-active-searchable-select-option]').textContent.trim() + ).toBe('JavaScript'); await search.trigger('keydown', { key: 'Enter' }); await flushPromises(); diff --git a/tests/composables/useLanguages.test.js b/tests/composables/useLanguages.test.js index 7794c02..57d19bd 100644 --- a/tests/composables/useLanguages.test.js +++ b/tests/composables/useLanguages.test.js @@ -2,14 +2,21 @@ import { describe, expect, it } from 'vitest'; import useLanguages from '~/composables/useLanguages'; describe('useLanguages', () => { - it('exposes familiar markdown language ids instead of internal grammar ids', () => { + it('sorts display names while preserving editor ids and searchable grammar aliases', () => { const { options } = useLanguages(); - expect(options(['typescript', 'shellscript', 'php', 'docker'])).toEqual([ - 'bash', - 'dockerfile', - 'php', - 'typescript', + expect( + options([ + { id: 'shellscript', name: 'Shell', aliases: ['bash', 'sh', 'zsh'] }, + { id: 'csharp', name: 'C#', aliases: ['cs'] }, + { id: 'php', name: 'PHP' }, + { id: 'docker', name: 'Dockerfile', aliases: ['dockerfile'] }, + ]) + ).toEqual([ + { value: 'csharp', label: 'C#', aliases: ['csharp', 'cs'] }, + { value: 'dockerfile', label: 'Dockerfile', aliases: ['docker', 'dockerfile'] }, + { value: 'php', label: 'PHP', aliases: ['php'] }, + { value: 'bash', label: 'Shell', aliases: ['shellscript', 'bash', 'sh', 'zsh'] }, ]); }); diff --git a/tests/plugins/shiki.test.js b/tests/plugins/shiki.test.js index 16f0f31..043811b 100644 --- a/tests/plugins/shiki.test.js +++ b/tests/plugins/shiki.test.js @@ -16,11 +16,18 @@ describe('Shiki worker client', () => { } = shikiPlugin(); worker.dispatchEvent( new MessageEvent('message', { - data: { id: 1, result: { languages: ['php'], themes: ['github-dark'] } }, + data: { + id: 1, + result: { + languages: [{ id: 'php', name: 'PHP', aliases: [] }], + themes: ['github-dark'], + }, + }, }) ); await shiki.ready; expect(shiki.languages()).toEqual(['php']); + expect(shiki.languageOptions()).toEqual([{ value: 'php', label: 'PHP', aliases: ['php'] }]); const first = shiki.tokenizeBlocks({ theme: 'github-dark', blocks: [] }); const second = shiki.tokenizeBlocks({ theme: 'github-light', blocks: [] }); await Promise.resolve();