Skip to content

Commit

Permalink
refactor: add workspace command and await actions
Browse files Browse the repository at this point in the history
fix: await action on click
  • Loading branch information
logotip4ik committed Sep 14, 2024
1 parent eb5d55e commit 1a7693c
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 16 deletions.
2 changes: 1 addition & 1 deletion components/Workspace/Search/Search.vue
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ async function openItem() {
const whitespaceIdx = search.indexOf(' ');
const arg = whitespaceIdx === -1 ? undefined : search.substring(whitespaceIdx + 1);
action?.(arg);
await action?.(arg);
}
else {
await navigateTo(
Expand Down
2 changes: 1 addition & 1 deletion components/Workspace/Search/SearchItem.vue
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ async function handleActionClick() {
const action = commandActions[props.item.key];
action?.();
await action?.();
}
</script>

Expand Down
1 change: 1 addition & 0 deletions types/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export const SearchAction = {
SaveNote: 5,
Details: 6,
Shortcuts: 7,
Workspace: 8,
} as const;

export type SearchActionKeys = keyof typeof SearchAction;
Expand Down
31 changes: 17 additions & 14 deletions utils/menu.ts
Original file line number Diff line number Diff line change
@@ -1,33 +1,30 @@
import type { Promisable } from 'type-fest';

import { SearchAction } from '~/types/common';

import type { SearchActionValues } from '~/types/common';

interface Command {
key: SearchActionValues
name: string
handler: (arg?: string) => void
handler: (arg?: string) => Promisable<void>
}

export const commandActions: Record<Command['key'], Command['handler']> = {
[SearchAction.New]: (arg) => {
const { state } = useContentsSidebar();
const folder = useNuxtApp()._asyncData.folder;

if (!folder) {
return;
}

const folderData = folder.data as Ref<FolderWithContents>;

if (!folderData.value) {
return;
}

preCreateItem(folderData.value, arg ? { name: arg } : undefined);
const { data: folder } = useNuxtData('folder');

if (state.value === 'hidden') {
state.value = 'visible';
}

const stop = watch(() => folder.value, (folder) => {
if (folder) {
preCreateItem(folder, arg ? { name: arg } : undefined);
stop();
}
});
},
[SearchAction.Refresh]: () => {
refreshNuxtData(['note', 'folder']);
Expand Down Expand Up @@ -60,6 +57,11 @@ export const commandActions: Record<Command['key'], Command['handler']> = {
[SearchAction.Shortcuts]: () => {
useMitt().emit('shortcuts:show');
},
[SearchAction.Workspace]: async () => {
const user = useUser();

await navigateTo(`/@${user.value?.username}`);
},
};

export const commandActionsMin: Map<Command['key'], Pick<Command, 'key' | 'name'>> = new Map([
Expand All @@ -70,6 +72,7 @@ export const commandActionsMin: Map<Command['key'], Pick<Command, 'key' | 'name'
[SearchAction.SaveNote, { name: 'save-note', key: SearchAction.SaveNote }],
[SearchAction.Details, { name: 'details', key: SearchAction.Details }],
[SearchAction.Shortcuts, { name: 'shortcuts', key: SearchAction.Shortcuts }],
[SearchAction.Workspace, { name: 'workspace', key: SearchAction.Workspace }],
]);

export function generateSearchRelativeItemPath(path: string, username: string) {
Expand Down

0 comments on commit 1a7693c

Please sign in to comment.