|
| 1 | +<script setup lang="ts"> |
| 2 | +import { useTimeAgo } from '@vueuse/core' |
| 3 | +import type { AssetInfo } from '~/../src/types' |
| 4 | +
|
| 5 | +const props = defineProps<{ |
| 6 | + asset: AssetInfo |
| 7 | +}>() |
| 8 | +
|
| 9 | +const imageMeta = computedAsync(() => { |
| 10 | + if (props.asset.type !== 'image') |
| 11 | + return undefined |
| 12 | + return rpc.getImageMeta(props.asset.filePath) |
| 13 | +}) |
| 14 | +
|
| 15 | +const textContent = computedAsync(() => { |
| 16 | + if (props.asset.type !== 'text') |
| 17 | + return undefined |
| 18 | + return rpc.getTextAssetContent(props.asset.filePath) |
| 19 | +}) |
| 20 | +
|
| 21 | +const codeSnippets = computed(() => { |
| 22 | + const items: [string, string, string][] = [] |
| 23 | + if (props.asset.type === 'image') { |
| 24 | + const attrs = imageMeta.value?.width |
| 25 | + ? ` width="${imageMeta.value.width}" height="${imageMeta.value.height}" ` |
| 26 | + : ' ' |
| 27 | + items.push( |
| 28 | + ['html', `<nuxt-image${attrs}src="${props.asset.publicPath}" />`, 'with Nuxt Image'], |
| 29 | + ['html', `<img${attrs}src="${props.asset.publicPath}" />`, 'Image'], |
| 30 | + ) |
| 31 | + return items |
| 32 | + } |
| 33 | +
|
| 34 | + items.push(['html', `<a download href="${props.asset.publicPath}">\n Download ${props.asset.path}\n</a>`, 'Download link']) |
| 35 | + return items |
| 36 | +}) |
| 37 | +
|
| 38 | +const copy = useCopy() |
| 39 | +const timeago = useTimeAgo(() => props.asset.mtime) |
| 40 | +const fileSize = computed(() => { |
| 41 | + const size = props.asset.size |
| 42 | + if (size < 1024) |
| 43 | + return `${size} B` |
| 44 | + if (size < 1024 * 1024) |
| 45 | + return `${(size / 1024).toFixed(2)} KB` |
| 46 | + return `${(size / 1024 / 1024).toFixed(2)} MB` |
| 47 | +}) |
| 48 | +
|
| 49 | +const aspectRatio = computed(() => { |
| 50 | + if (!imageMeta.value?.width || !imageMeta.value?.height) |
| 51 | + return '' |
| 52 | + const gcd = (a: number, b: number): number => { |
| 53 | + if (!b) |
| 54 | + return a |
| 55 | + return gcd(b, a % b) |
| 56 | + } |
| 57 | + const ratio = gcd(imageMeta.value.width, imageMeta.value.height) |
| 58 | + if (ratio > 3) |
| 59 | + return `${imageMeta.value.width / ratio} : ${imageMeta.value.height / ratio}` |
| 60 | + return '' |
| 61 | +}) |
| 62 | +
|
| 63 | +const supportsPreview = computed(() => { |
| 64 | + return [ |
| 65 | + 'image', |
| 66 | + 'text', |
| 67 | + 'video', |
| 68 | + ].includes(props.asset.type) |
| 69 | +}) |
| 70 | +</script> |
| 71 | + |
| 72 | +<template> |
| 73 | + <div flex="~ col gap-4" w-full of-hidden p2 h-full of-auto> |
| 74 | + <template v-if="supportsPreview"> |
| 75 | + <div op50 mb--2 flex="~ gap2" items-center> |
| 76 | + <div x-divider /> |
| 77 | + <div flex-none> |
| 78 | + Preview |
| 79 | + </div> |
| 80 | + <div x-divider /> |
| 81 | + </div> |
| 82 | + |
| 83 | + <div flex="~" items-center justify-center> |
| 84 | + <AssetPreview |
| 85 | + rounded max-h-80 w-auto min-h-20 min-w-20 border="~ base" |
| 86 | + :asset="asset" |
| 87 | + :text-content="textContent" |
| 88 | + /> |
| 89 | + </div> |
| 90 | + </template> |
| 91 | + |
| 92 | + <div op50 mb--2 flex="~ gap2" items-center> |
| 93 | + <div x-divider /> |
| 94 | + <div flex-none> |
| 95 | + Details |
| 96 | + </div> |
| 97 | + <div x-divider /> |
| 98 | + </div> |
| 99 | + |
| 100 | + <table w-full> |
| 101 | + <tbody> |
| 102 | + <tr> |
| 103 | + <td op50 text-right pr5 w-max ws-nowrap> |
| 104 | + Filepath |
| 105 | + </td> |
| 106 | + <td ws-w> |
| 107 | + <FilepathItem :filepath="asset.filePath" /> |
| 108 | + </td> |
| 109 | + </tr> |
| 110 | + <tr> |
| 111 | + <td text-right op50 pr5 w-max ws-nowrap> |
| 112 | + Public |
| 113 | + </td> |
| 114 | + <td>{{ asset.publicPath }}</td> |
| 115 | + </tr> |
| 116 | + <tr> |
| 117 | + <td text-right op50 pr5 w-max ws-nowrap> |
| 118 | + Type |
| 119 | + </td> |
| 120 | + <td capitalize> |
| 121 | + {{ asset.type }} |
| 122 | + </td> |
| 123 | + </tr> |
| 124 | + <template v-if="imageMeta?.width"> |
| 125 | + <tr> |
| 126 | + <td text-right op50 pr5 w-max ws-nowrap> |
| 127 | + Image Size |
| 128 | + </td> |
| 129 | + <td>{{ imageMeta.width }} x {{ imageMeta.height }}</td> |
| 130 | + </tr> |
| 131 | + <tr v-if="aspectRatio"> |
| 132 | + <td text-right op50 pr5 w-max ws-nowrap> |
| 133 | + Aspect Ratio |
| 134 | + </td> |
| 135 | + <td>{{ aspectRatio }}</td> |
| 136 | + </tr> |
| 137 | + </template> |
| 138 | + <tr> |
| 139 | + <td text-right op50 pr5 w-max ws-nowrap> |
| 140 | + File size |
| 141 | + </td> |
| 142 | + <td>{{ fileSize }}</td> |
| 143 | + </tr> |
| 144 | + <tr> |
| 145 | + <td text-right op50 pr5 w-max ws-nowrap> |
| 146 | + Last modified |
| 147 | + </td> |
| 148 | + <td>{{ new Date(asset.mtime).toLocaleString() }} <span op70>({{ timeago }})</span></td> |
| 149 | + </tr> |
| 150 | + </tbody> |
| 151 | + </table> |
| 152 | + |
| 153 | + <div op50 mb--2 flex="~ gap2" items-center> |
| 154 | + <div x-divider /> |
| 155 | + <div flex-none> |
| 156 | + Actions |
| 157 | + </div> |
| 158 | + <div x-divider /> |
| 159 | + </div> |
| 160 | + <div flex="~ gap2 wrap"> |
| 161 | + <NButton icon="i-carbon-code" @click="openInEditor(asset.filePath)"> |
| 162 | + Open in Editor |
| 163 | + </NButton> |
| 164 | + <NButton icon="carbon-launch" :to="asset.publicPath" target="_blank"> |
| 165 | + Open in browser |
| 166 | + </NButton> |
| 167 | + <NButton icon="carbon-copy" @click="copy(asset.publicPath)"> |
| 168 | + Copy public path |
| 169 | + </NButton> |
| 170 | + <!-- <NButton v-if="asset.type === 'image'" disabled icon="carbon-image-service"> |
| 171 | + Optimize image (WIP) |
| 172 | + </NButton> --> |
| 173 | + </div> |
| 174 | + |
| 175 | + <div flex-auto /> |
| 176 | + |
| 177 | + <div v-if="codeSnippets?.length" n-code-block border="~ base rounded"> |
| 178 | + <template v-for="cs, idx of codeSnippets" :key="idx"> |
| 179 | + <div v-if="idx" x-divider /> |
| 180 | + <div v-if="cs" of-hidden p2> |
| 181 | + <div items-center flex justify-between pb2> |
| 182 | + <div op50 ml1> |
| 183 | + {{ cs[2] }} |
| 184 | + </div> |
| 185 | + <NButton icon="carbon-copy" n="sm primary" @click="copy(cs[1])"> |
| 186 | + Copy |
| 187 | + </NButton> |
| 188 | + </div> |
| 189 | + <NCodeBlock :code="cs[1]" :lang="cs[0]" of-auto w-full :lines="false" px1 /> |
| 190 | + </div> |
| 191 | + </template> |
| 192 | + </div> |
| 193 | + </div> |
| 194 | +</template> |
0 commit comments