@@ -3,27 +3,29 @@ import { useDevToolsClient } from '~/logic/client'
33import { rpc } from ' ~/logic/rpc'
44
55const props = defineProps <{
6- asset : AssetInfo
6+ modelValue : AssetInfo
77}>()
8-
8+ const emit = defineEmits <{ (... args : any ): void }>()
9+ const asset = useVModel (props , ' modelValue' , emit , { passive: true })
10+ const showNotification = useNotification ()
911const origin = window .parent .location .origin
1012
1113const imageMeta = computedAsync (() => {
12- if (props . asset .type !== ' image' )
14+ if (asset . value .type !== ' image' )
1315 return undefined
14- return rpc .getImageMeta (props . asset .filePath )
16+ return rpc .getImageMeta (asset . value .filePath )
1517})
1618
1719const textContent = computedAsync (() => {
18- if (props . asset .type !== ' text' )
20+ if (asset . value .type !== ' text' )
1921 return undefined
20- return rpc .getTextAssetContent (props . asset .filePath )
22+ return rpc .getTextAssetContent (asset . value .filePath )
2123})
2224
2325const copy = useCopy ()
24- const timeago = useTimeAgo (() => props . asset .mtime )
26+ const timeAgo = useTimeAgo (() => asset . value .mtime )
2527const fileSize = computed (() => {
26- const size = props . asset .size
28+ const size = asset . value .size
2729 if (size < 1024 )
2830 return ` ${size } B `
2931 if (size < 1024 * 1024 )
@@ -51,9 +53,65 @@ const supportsPreview = computed(() => {
5153 ' text' ,
5254 ' video' ,
5355 ' font' ,
54- ].includes (props . asset .type )
56+ ].includes (asset . value .type )
5557})
5658
59+ const deleteDialog = ref (false )
60+ async function deleteAsset() {
61+ try {
62+ await rpc .deleteStaticAsset (asset .value .filePath )
63+ asset .value = undefined as any
64+ deleteDialog .value = false
65+ showNotification ({
66+ text: ' Asset deleted' ,
67+ icon: ' carbon-checkmark' ,
68+ type: ' primary' ,
69+ })
70+ }
71+ catch (error ) {
72+ deleteDialog .value = false
73+ showNotification ({
74+ text: ' Something went wrong!' ,
75+ icon: ' carbon-warning' ,
76+ type: ' error' ,
77+ })
78+ }
79+ }
80+
81+ const renameDialog = ref (false )
82+ const newName = ref (' ' )
83+ async function renameAsset() {
84+ const parts = asset .value .filePath .split (' /' )
85+ const oldName = parts .slice (- 1 )[0 ].split (' .' ).slice (0 , - 1 ).join (' .' )
86+ if (! newName .value || newName .value === oldName ) {
87+ return showNotification ({
88+ text: ' Please enter a new name' ,
89+ icon: ' carbon-warning' ,
90+ type: ' error' ,
91+ })
92+ }
93+ try {
94+ const extension = parts .slice (- 1 )[0 ].split (' .' ).slice (- 1 )[0 ]
95+ const fullPath = ` ${parts .slice (0 , - 1 ).join (' /' )}/${newName .value }.${extension } `
96+ await rpc .renameStaticAsset (asset .value .filePath , fullPath )
97+
98+ asset .value = undefined as any
99+ renameDialog .value = false
100+ showNotification ({
101+ text: ' Asset renamed' ,
102+ icon: ' carbon-checkmark' ,
103+ type: ' primary' ,
104+ })
105+ }
106+ catch (error ) {
107+ showNotification ({
108+ text: ' Something went wrong!' ,
109+ icon: ' carbon-warning' ,
110+ type: ' error' ,
111+ })
112+ }
113+ }
114+
57115const client = useDevToolsClient ()
58116 </script >
59117
@@ -161,7 +219,7 @@ const client = useDevToolsClient()
161219 <td w-30 ws-nowrap pr5 text-right op50 >
162220 Last modified
163221 </td >
164- <td >{{ new Date(asset.mtime).toLocaleString() }} <span op70 >({{ timeago }})</span ></td >
222+ <td >{{ new Date(asset.mtime).toLocaleString() }} <span op70 >({{ timeAgo }})</span ></td >
165223 </tr >
166224 </tbody >
167225 </table >
@@ -174,11 +232,50 @@ const client = useDevToolsClient()
174232 <div x-divider />
175233 </div >
176234 <div flex =" ~ gap2 wrap" >
177- <VDButton :to =" `${origin}${asset.publicPath}`" download target =" _blank" icon =" carbon-download" >
235+ <VDButton :to =" `${origin}${asset.publicPath}`" download target =" _blank" icon =" carbon-download" n = " green " >
178236 Download
179237 </VDButton >
238+ <VDButton icon =" carbon-text-annotation-toggle" n =" blue" @click =" renameDialog = !renameDialog" >
239+ Rename
240+ </VDButton >
241+ <VDButton icon =" carbon-delete" n =" red" @click =" deleteDialog = !deleteDialog" >
242+ Delete
243+ </VDButton >
180244 </div >
181245
182246 <div flex-auto />
247+
248+ <VDDialog
249+ v-model =" deleteDialog" @close =" deleteDialog = false"
250+ >
251+ <div flex =" ~ col gap-4" min-h-full w-full of-hidden p8 >
252+ <span >
253+ Are you sure you want to delete this asset?
254+ </span >
255+ <div flex =" ~ gap2 wrap justify-center" >
256+ <VDButton icon =" carbon-close" @click =" deleteDialog = false" >
257+ Cancel
258+ </VDButton >
259+ <VDButton icon =" carbon-delete" n =" red" @click =" deleteAsset" >
260+ Delete
261+ </VDButton >
262+ </div >
263+ </div >
264+ </VDDialog >
265+ <VDDialog
266+ v-model =" renameDialog" @close =" deleteDialog = false"
267+ >
268+ <div flex =" ~ col gap-4" min-h-full w-full of-hidden p8 >
269+ <VDTextInput v-model =" newName" placeholder =" New name" n =" blue" />
270+ <div flex =" ~ gap2 wrap justify-center" >
271+ <VDButton icon =" carbon-close" @click =" renameDialog = false" >
272+ Cancel
273+ </VDButton >
274+ <VDButton icon =" carbon-text-annotation-toggle" n =" blue" @click =" renameAsset" >
275+ Rename
276+ </VDButton >
277+ </div >
278+ </div >
279+ </VDDialog >
183280 </div >
184281</template >
0 commit comments