Skip to content

Commit d1c347a

Browse files
authored
Refactor attachment image preview section (#7944)
1 parent a94993a commit d1c347a

File tree

9 files changed

+118
-173
lines changed

9 files changed

+118
-173
lines changed

ui/console-src/modules/contents/attachments/components/AttachmentDetailModal.vue

Lines changed: 5 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<script lang="ts" setup>
2+
import AttachmentImagePreview from "@/components/attachment/AttachmentImagePreview.vue";
23
import AttachmentPermalinkList from "@/components/attachment/AttachmentPermalinkList.vue";
3-
import LazyImage from "@/components/image/LazyImage.vue";
44
import { isImage } from "@/utils/image";
55
import { coreApiClient } from "@halo-dev/api-client";
66
import {
@@ -16,7 +16,6 @@ import { utils } from "@halo-dev/ui-shared";
1616
import { useQuery } from "@tanstack/vue-query";
1717
import prettyBytes from "pretty-bytes";
1818
import { computed, ref, toRefs, useTemplateRef } from "vue";
19-
import AttachmentThumbnailList from "./AttachmentThumbnailList.vue";
2019
import DisplayNameEditForm from "./DisplayNameEditForm.vue";
2120
2221
const props = withDefaults(
@@ -99,7 +98,7 @@ const showDisplayNameForm = ref(false);
9998
display_name: attachment?.spec.displayName || '',
10099
})
101100
"
102-
:width="1000"
101+
:width="1200"
103102
:mount-to-body="mountToBody"
104103
:layer-closable="true"
105104
height="calc(100vh - 20px)"
@@ -116,35 +115,10 @@ const showDisplayNameForm = ref(false);
116115
<VDescriptionItem
117116
:label="$t('core.attachment.detail_modal.fields.preview')"
118117
>
119-
<a
118+
<AttachmentImagePreview
120119
v-if="isImage(attachment?.spec.mediaType)"
121-
:href="attachment?.status?.permalink"
122-
target="_blank"
123-
>
124-
<LazyImage
125-
v-tooltip="{
126-
content: attachment?.status?.permalink,
127-
placement: 'bottom',
128-
}"
129-
:alt="attachment?.spec.displayName"
130-
:src="
131-
attachment?.status?.thumbnails?.M ||
132-
attachment?.status?.permalink
133-
"
134-
classes="max-w-full cursor-pointer rounded"
135-
>
136-
<template #loading>
137-
<span class="text-gray-400">
138-
{{ $t("core.common.status.loading") }}...
139-
</span>
140-
</template>
141-
<template #error>
142-
<span class="text-red-400">
143-
{{ $t("core.common.status.loading_error") }}
144-
</span>
145-
</template>
146-
</LazyImage>
147-
</a>
120+
:attachment="attachment"
121+
/>
148122
<div v-else-if="attachment?.spec.mediaType?.startsWith('video/')">
149123
<video
150124
:src="attachment.status?.permalink"
@@ -220,15 +194,6 @@ const showDisplayNameForm = ref(false);
220194
>
221195
<AttachmentPermalinkList :attachment="attachment" />
222196
</VDescriptionItem>
223-
<VDescriptionItem
224-
v-if="
225-
isImage(attachment?.spec.mediaType) &&
226-
!!attachment?.status?.thumbnails
227-
"
228-
:label="$t('core.attachment.detail_modal.fields.thumbnails')"
229-
>
230-
<AttachmentThumbnailList :attachment="attachment" />
231-
</VDescriptionItem>
232197
</VDescription>
233198
</div>
234199
</div>

ui/console-src/modules/contents/attachments/components/AttachmentThumbnailList.vue

Lines changed: 0 additions & 91 deletions
This file was deleted.
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
<script lang="ts" setup>
2+
import LazyImage from "@/components/image/LazyImage.vue";
3+
import {
4+
GetThumbnailByUriSizeEnum,
5+
type Attachment,
6+
} from "@halo-dev/api-client";
7+
import { VTabbar } from "@halo-dev/components";
8+
import { THUMBNAIL_WIDTH_MAP } from "@halo-dev/ui-shared";
9+
import { computed, ref, toRefs } from "vue";
10+
import { useI18n } from "vue-i18n";
11+
12+
const { t } = useI18n();
13+
14+
const props = withDefaults(
15+
defineProps<{
16+
attachment?: Attachment;
17+
}>(),
18+
{
19+
attachment: undefined,
20+
}
21+
);
22+
23+
const { attachment } = toRefs(props);
24+
25+
const sizeOrder: Record<GetThumbnailByUriSizeEnum, number> = {
26+
XL: 4,
27+
L: 3,
28+
M: 2,
29+
S: 1,
30+
};
31+
32+
const items = computed(
33+
(): { label: string; id: string; permalink?: string }[] => {
34+
return [
35+
{
36+
label: t("core.attachment.common.text.original"),
37+
id: "original",
38+
permalink: attachment.value?.status?.permalink,
39+
},
40+
...Object.entries(attachment.value?.status?.thumbnails || {})
41+
.sort(
42+
([sizeA], [sizeB]) =>
43+
(sizeOrder[sizeB as GetThumbnailByUriSizeEnum] || 0) -
44+
(sizeOrder[sizeA as GetThumbnailByUriSizeEnum] || 0)
45+
)
46+
.map(([size, permalink]) => ({
47+
label: `${THUMBNAIL_WIDTH_MAP[size as GetThumbnailByUriSizeEnum]}w`,
48+
id: size,
49+
permalink,
50+
})),
51+
];
52+
}
53+
);
54+
55+
const activeId = ref(items.value?.[0]?.id);
56+
</script>
57+
<template>
58+
<VTabbar
59+
v-model:active-id="activeId"
60+
:items="
61+
items.map((item) => ({
62+
id: item.id,
63+
label: item.label,
64+
}))
65+
"
66+
type="outline"
67+
></VTabbar>
68+
<div class="mt-3">
69+
<a
70+
v-for="item in items"
71+
:key="item.permalink"
72+
class="block"
73+
target="_blank"
74+
:href="item.permalink"
75+
>
76+
<LazyImage
77+
v-if="item.id === activeId"
78+
v-tooltip="{
79+
content: item.permalink,
80+
placement: 'bottom',
81+
}"
82+
:src="item.permalink"
83+
classes="max-w-full rounded"
84+
>
85+
<template #loading>
86+
<span class="text-gray-400">
87+
{{ $t("core.common.status.loading") }}...
88+
</span>
89+
</template>
90+
<template #error>
91+
<span class="text-red-400">
92+
{{ $t("core.common.status.loading_error") }}
93+
</span>
94+
</template>
95+
</LazyImage>
96+
</a>
97+
</div>
98+
</template>

ui/src/components/image/LazyImage.vue

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,5 +48,7 @@ onMounted(async () => {
4848
<template v-else-if="error">
4949
<slot name="error"> error </slot>
5050
</template>
51-
<img v-else :src="src" :alt="alt" :class="classes" />
51+
<Transition v-else appear name="fade">
52+
<img :src="src" :alt="alt" :class="classes" />
53+
</Transition>
5254
</template>

ui/src/locales/_missing_translations_es.yaml

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -217,18 +217,17 @@ core:
217217
original_comment: Original comment
218218
content: Reply content
219219
attachment:
220+
common:
221+
text:
222+
original: Original
220223
filters:
221224
sort:
222225
items:
223226
display_name_asc: Ascending order by display name
224227
display_name_desc: Descending order by display name
225228
detail_modal:
226-
fields:
227-
thumbnails: Thumbnails
228229
display_name_form:
229230
help: Custom attachment name, only for display in the management interface, does not affect the file name and access link itself
230-
preview:
231-
not_support_thumbnail: This image does not support generating thumbnails.
232231
permalink_list:
233232
relative: Relative path
234233
absolute: Absolute path
@@ -332,7 +331,7 @@ core:
332331
plugin:
333332
list:
334333
fields:
335-
builtin: Built-in
334+
system_reserved: System Reserved
336335
operations:
337336
uninstall_in_batch:
338337
title: Uninstall the selected plugins

ui/src/locales/en.yaml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -629,6 +629,7 @@ core:
629629
common:
630630
text:
631631
ungrouped: Ungrouped
632+
original: Original
632633
actions:
633634
storage_policies: Storage Policies
634635
empty:
@@ -686,14 +687,12 @@ core:
686687
owner: Owner
687688
creation_time: Creation time
688689
permalink: Permalink
689-
thumbnails: Thumbnails
690690
display_name_form:
691691
help: Custom attachment name, only for display in the management interface, does not affect the file name and access link itself
692692
preview:
693693
video_not_support: The current browser does not support video playback.
694694
audio_not_support: The current browser does not support audio playback.
695695
not_support: This file does not support preview.
696-
not_support_thumbnail: This image does not support generating thumbnails.
697696
permalink_list:
698697
relative: Relative path
699698
absolute: Absolute path

ui/src/locales/zh-CN.yaml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -598,6 +598,7 @@ core:
598598
common:
599599
text:
600600
ungrouped: 未分组
601+
original: 原始
601602
actions:
602603
storage_policies: 存储策略
603604
empty:
@@ -653,14 +654,12 @@ core:
653654
owner: 上传者
654655
creation_time: 上传时间
655656
permalink: 链接
656-
thumbnails: 缩略图
657657
display_name_form:
658658
help: 自定义附件名称,仅用于管理界面显示,不会影响文件本身名称以及访问链接
659659
preview:
660660
video_not_support: 当前浏览器不支持该视频播放
661661
audio_not_support: 当前浏览器不支持该音频播放
662662
not_support: 此文件不支持预览
663-
not_support_thumbnail: 此图片不支持生成缩略图
664663
group_editing_modal:
665664
titles:
666665
create: 新增附件分组

ui/src/locales/zh-TW.yaml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -583,6 +583,7 @@ core:
583583
common:
584584
text:
585585
ungrouped: 未分組
586+
original: 原始
586587
actions:
587588
storage_policies: 存儲策略
588589
empty:
@@ -638,14 +639,12 @@ core:
638639
owner: 上傳者
639640
creation_time: 上傳時間
640641
permalink: 連結
641-
thumbnails: 縮略圖
642642
display_name_form:
643643
help: 自定義附件名稱,僅用於管理介面顯示,不會影響文件本身名稱以及訪問連結
644644
preview:
645645
video_not_support: 當前瀏覽器不支援該影片播放
646646
audio_not_support: 當前瀏覽器不支援該音頻播放
647647
not_support: 此文件不支援預覽
648-
not_support_thumbnail: 此圖片不支持生成縮略圖
649648
group_editing_modal:
650649
titles:
651650
create: 新增附件分組

0 commit comments

Comments
 (0)