From 21c30dd60f2bfa83bbf56099f403def65baa3b42 Mon Sep 17 00:00:00 2001 From: arashsheyda Date: Thu, 25 May 2023 11:37:46 +0300 Subject: [PATCH 1/4] feat: command-palette group --- .../client/components/CommandPalette.vue | 48 +++++++++++++------ .../client/composables/state-commands.ts | 16 +++++++ .../client/pages/modules/analyze-build.vue | 2 +- .../devtools/client/pages/modules/modules.vue | 2 +- 4 files changed, 52 insertions(+), 16 deletions(-) diff --git a/packages/devtools/client/components/CommandPalette.vue b/packages/devtools/client/components/CommandPalette.vue index 916f5fc154..6dfb964c3c 100644 --- a/packages/devtools/client/components/CommandPalette.vue +++ b/packages/devtools/client/components/CommandPalette.vue @@ -6,32 +6,40 @@ const search = ref('') const items = useCommands() -const fuse = computed(() => new Fuse(items.value, { +const groups = computed(() => ['all', ...new Set(items.value.map(i => i.id.split(':')[0]))]) +const currentGroup = ref(groups.value[0]) +const groupItems = computed(() => items.value.filter(i => i.id.startsWith(currentGroup.value) || currentGroup.value === 'all')) + +const fuse = computed(() => new Fuse(groupItems.value, { keys: [ + 'id', 'title', ], - threshold: 0.3, + distance: 50, })) const filtered = computed(() => { const result = search.value ? fuse.value.search(search.value).map(i => i.item) - : (items.value || []) + : (groupItems.value || []) return result }) -const elements = ref([]) const selectedIndex = ref(0) watch(search, () => { selectedIndex.value = 0 + scrollToITem() }) function moveSelected(delta: number) { selectedIndex.value = ((selectedIndex.value + delta) + filtered.value.length) % filtered.value.length + scrollToITem() +} - const item = elements.value[selectedIndex.value] - item.scrollIntoView({ +function scrollToITem() { + const item = document.getElementById(filtered.value[selectedIndex.value]?.id) + item?.scrollIntoView({ block: 'center', }) } @@ -68,17 +76,27 @@ useEventListener('keydown', (e) => {
- +
+ + + + +
diff --git a/packages/devtools/client/composables/state-commands.ts b/packages/devtools/client/composables/state-commands.ts index f616dae8a1..eaccc54a8f 100644 --- a/packages/devtools/client/composables/state-commands.ts +++ b/packages/devtools/client/composables/state-commands.ts @@ -12,6 +12,7 @@ const registeredCommands = reactive(new Map toValue(i)), + ...docs.value ?? [], ] }) } @@ -58,3 +60,17 @@ export function registerCommands(getter: MaybeRefOrGetter) { registeredCommands.delete(id) }) } + +export function useNuxtDocs() { + return useAsyncState('getNuxtDocs', async () => { + // TODO: change url when possible + const list = await $fetch('https://cdn.jsdelivr.net/gh/arashsheyda/nuxt@docs-list/docs-list.json') + return list.map(i => ({ + ...i, + icon: 'carbon-document', + action: () => { + window.open(i.path, '_blank') + }, + })) + }) +} diff --git a/packages/devtools/client/pages/modules/analyze-build.vue b/packages/devtools/client/pages/modules/analyze-build.vue index 920b602846..c6e4bfc79e 100644 --- a/packages/devtools/client/pages/modules/analyze-build.vue +++ b/packages/devtools/client/pages/modules/analyze-build.vue @@ -53,7 +53,7 @@ function formatDuration(build: AnalyzeBuildMeta) { registerCommands(() => [ { - id: 'analyze-build: start', + id: 'action:analyze-build: start', title: 'Start a new analyze build', icon: 'i-carbon-edge-node', action: start, diff --git a/packages/devtools/client/pages/modules/modules.vue b/packages/devtools/client/pages/modules/modules.vue index 6688eeae99..7b47114cd1 100644 --- a/packages/devtools/client/pages/modules/modules.vue +++ b/packages/devtools/client/pages/modules/modules.vue @@ -15,7 +15,7 @@ const iconBase = 'https://api.nuxtjs.org/api/ipx/s_80,f_webp/gh/nuxt/modules/mai registerCommands(() => [ { - id: 'modules:install', + id: 'action:modules:install', title: 'Install a new module', icon: 'i-carbon-intent-request-create ', action: () => { From 1d855ab303d6a54c2a948d5969a35c913d1507cd Mon Sep 17 00:00:00 2001 From: arashsheyda Date: Thu, 25 May 2023 12:24:43 +0300 Subject: [PATCH 2/4] fix: group list --- .../client/components/CommandPalette.vue | 30 ++++++++++++++----- 1 file changed, 22 insertions(+), 8 deletions(-) diff --git a/packages/devtools/client/components/CommandPalette.vue b/packages/devtools/client/components/CommandPalette.vue index 6dfb964c3c..bae14628c6 100644 --- a/packages/devtools/client/components/CommandPalette.vue +++ b/packages/devtools/client/components/CommandPalette.vue @@ -6,9 +6,17 @@ const search = ref('') const items = useCommands() -const groups = computed(() => ['all', ...new Set(items.value.map(i => i.id.split(':')[0]))]) -const currentGroup = ref(groups.value[0]) -const groupItems = computed(() => items.value.filter(i => i.id.startsWith(currentGroup.value) || currentGroup.value === 'all')) +const groups = ['all', 'fixed', 'tab', 'action', 'doc', 'other'] +const currentGroup = ref(groups[0]) +const groupItems = computed(() => { + if (currentGroup.value === 'all') + return items.value + + if (currentGroup.value === 'other') + return items.value.filter(i => !groups.includes(i.id.split(':')[0])) + + return items.value.filter(i => i.id.startsWith(currentGroup.value)) +}) const fuse = computed(() => new Fuse(groupItems.value, { keys: [ @@ -19,10 +27,9 @@ const fuse = computed(() => new Fuse(groupItems.value, { })) const filtered = computed(() => { - const result = search.value - ? fuse.value.search(search.value).map(i => i.item) - : (groupItems.value || []) - return result + if (search.value) + return fuse.value.search(search.value).map(i => i.item) + return groupItems.value || [] }) const selectedIndex = ref(0) @@ -70,6 +77,13 @@ useEventListener('keydown', (e) => { show.value = false } }) + +function idToGroup(str: string) { + const [firstGroup, ...rest] = str.split(':') + const groupName = groups.includes(firstGroup) ? firstGroup : 'other' + const items = [groupName, ...rest.slice(0, -1)].reverse().map(item => item.replace(/-/g, ' ')) + return items.join(' . ') +}