Skip to content

Commit 81b25ef

Browse files
committed
feat: revert and upload in context
1 parent 5fcfb8c commit 81b25ef

File tree

5 files changed

+39
-7
lines changed

5 files changed

+39
-7
lines changed

src/app/src/components/panel/base/PanelBaseBody.vue

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,11 @@ const isFolderCreationInProgress = computed(() => context.actionInProgress.value
1717
1818
async function onFileDrop(event: DragEvent) {
1919
if (event.dataTransfer?.files) {
20-
for (const file of event.dataTransfer.files) {
21-
await draftMedias.upload(tree.value.currentItem.value.fsPath, file)
20+
if (context.feature.value === StudioFeature.Media) {
21+
await context.itemActionHandler[StudioItemActionId.UploadMedia]({
22+
directory: tree.value.currentItem.value.fsPath,
23+
files: Array.from(event.dataTransfer.files),
24+
})
2225
}
2326
}
2427
}

src/app/src/components/shared/item/ItemActionsDropdown.vue

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<script setup lang="ts">
2-
import { computeActionItems } from '../../../utils/context'
2+
import { computeActionItems, computeActionParams } from '../../../utils/context'
33
import { computed, type PropType } from 'vue'
44
import type { TreeItem } from '../../../types'
55
import { useStudio } from '../../../composables/useStudio'
@@ -17,7 +17,7 @@ const props = defineProps({
1717
const actions = computed<DropdownMenuItem[]>(() => {
1818
return computeActionItems(context.itemActions.value, props.item).map(action => ({
1919
...action,
20-
onSelect: action.handler,
20+
onSelect: () => action.handler?.(computeActionParams(action.id, { item: props.item })),
2121
}))
2222
})
2323
</script>

src/app/src/composables/useContext.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { createSharedComposable } from '@vueuse/core'
22
import { computed, ref } from 'vue'
33
import type { useUi } from './useUi'
4-
import { type CreateFileParams, type StudioHost, type StudioAction, type TreeItem, StudioItemActionId, type ActionHandlerParams } from '../types'
4+
import { type UploadMediaParams, type CreateFileParams, type StudioHost, type StudioAction, type TreeItem, StudioItemActionId, type ActionHandlerParams, StudioFeature } from '../types'
55
import { oneStepActions, STUDIO_ITEM_ACTION_DEFINITIONS, twoStepActions } from '../utils/context'
66
import type { useDraftDocuments } from './useDraftDocuments'
77
import { useModal } from './useModal'
@@ -59,8 +59,20 @@ export const useContext = createSharedComposable((
5959
const draftItem = await draftDocuments.create(document)
6060
tree.selectItemById(draftItem.id)
6161
},
62+
[StudioItemActionId.UploadMedia]: async ({ directory, files }: UploadMediaParams) => {
63+
for (const file of files) {
64+
await draftMedias.upload(directory, file)
65+
}
66+
},
6267
[StudioItemActionId.RevertItem]: async (id: string) => {
63-
modal.openConfirmActionModal(id, StudioItemActionId.RevertItem, () => draftDocuments.revert(id))
68+
modal.openConfirmActionModal(id, StudioItemActionId.RevertItem, async () => {
69+
if (currentFeature.value === StudioFeature.Content) {
70+
await draftDocuments.revert(id)
71+
}
72+
else {
73+
await draftMedias.revert(id)
74+
}
75+
})
6476
},
6577
[StudioItemActionId.RenameItem]: async ({ path, file }: { path: string, file: TreeItem }) => {
6678
alert(`rename file ${path} ${file.name}`)

src/app/src/types/context.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ export enum StudioFeature {
88
export enum StudioItemActionId {
99
CreateFolder = 'create-folder',
1010
CreateDocument = 'create-document',
11+
UploadMedia = 'upload-media',
1112
RevertItem = 'revert-item',
1213
RenameItem = 'rename-item',
1314
DeleteItem = 'delete-item',
@@ -19,7 +20,7 @@ export interface StudioAction {
1920
label: string
2021
icon: string
2122
tooltip: string
22-
handler?: (args: string & CreateFileParams & RenameFileParams) => void
23+
handler?: (args: ActionHandlerParams[StudioItemActionId]) => void
2324
}
2425

2526
export interface CreateFileParams {
@@ -33,9 +34,15 @@ export interface RenameFileParams {
3334
file: TreeItem
3435
}
3536

37+
export interface UploadMediaParams {
38+
directory: string
39+
files: File[]
40+
}
41+
3642
export type ActionHandlerParams = {
3743
[StudioItemActionId.CreateFolder]: string
3844
[StudioItemActionId.CreateDocument]: CreateFileParams
45+
[StudioItemActionId.UploadMedia]: UploadMediaParams
3946
[StudioItemActionId.RevertItem]: string
4047
[StudioItemActionId.RenameItem]: RenameFileParams
4148
[StudioItemActionId.DeleteItem]: string

src/app/src/utils/context.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { type StudioAction, StudioFeature, type TreeItem } from '../types'
22
import { DraftStatus } from '../types/draft'
3+
import type { ActionHandlerParams } from '../types/context'
34
import { StudioItemActionId } from '../types/context'
45

56
export const FEATURE_DISPLAY_MAP = {
@@ -88,3 +89,12 @@ export function computeActionItems(itemActions: StudioAction[], item?: TreeItem
8889

8990
return itemActions.filter(action => !forbiddenActions.includes(action.id))
9091
}
92+
93+
export function computeActionParams(action: StudioItemActionId, { item }: { item: TreeItem }): ActionHandlerParams[typeof action] {
94+
switch (action) {
95+
case StudioItemActionId.RevertItem:
96+
return item.id
97+
default:
98+
return {}
99+
}
100+
}

0 commit comments

Comments
 (0)