fix media item sort

This commit is contained in:
Timi
2025-12-11 19:21:41 +08:00
parent f837c4d4d9
commit c0a0242a02
10 changed files with 201 additions and 101 deletions

View File

@ -1,7 +1,8 @@
// components/journal-detail-panel/index.ts
import { Journal } from "../../types/Journal";
import config from "../../config/index";
import { MediaAttachExt, MediaAttachType } from "../../types/Attachment";
import Toolkit from "../../utils/Toolkit";
import { ImageMetadata, MediaAttachExt, MediaAttachType } from "../../types/Attachment";
import { MediaItem, MediaItemType } from "../../types/UI";
import Time from "../../utils/Time";
@ -61,7 +62,8 @@ Component({
if (!thumbItems) {
return;
}
const mediaItems: MediaItem[] = thumbItems.map((thumbItem) => {
const mediaItems: MediaItem[] = thumbItems.map((thumbItem, index) => {
const metadata = thumbItem.metadata as ImageMetadata;
const ext = thumbItem.ext = JSON.parse(thumbItem.ext!.toString()) as MediaAttachExt;
const thumbURL = `${config.url}/attachment/read/${thumbItem.mongoId}`;
const sourceURL = `${config.url}/attachment/read/${ext.sourceMongoId}`;
@ -70,10 +72,21 @@ Component({
thumbURL,
sourceURL,
size: thumbItem.size || 0,
attachmentId: thumbItem.id
attachmentId: thumbItem.id,
width: metadata.width,
height: metadata.height,
originalIndex: index
} as MediaItem;
});
journal.mediaItems = mediaItems;
// 为每个 journal 添加分列后的 mediaItems
(journal as any).columnedMediaItems = Toolkit.splitItemsIntoColumns(mediaItems, 3, (item) => {
// 计算相对高度:如果有宽高信息,使用宽高比;否则假设为正方形
if (item.width && item.height && 0 < item.width) {
return item.height / item.width;
}
return 1; // 默认正方形
});
})
this.setData({
journals,
@ -119,14 +132,15 @@ Component({
},
/** 预览媒体 */
previewMedia(e: WechatMiniprogram.BaseEvent) {
const journals = this.properties.journals as Journal[];
const journals = this.data.journals;
if (!journals || journals.length === 0) {
return;
}
const { itemIndex } = e.currentTarget.dataset;
const items = journals[this.data.currentJournalIndex].mediaItems!;
const journal = journals[this.data.currentJournalIndex];
const items = journal.mediaItems!;
const total = items.length;
const startIndex = Math.max(0, itemIndex - 25);
const endIndex = Math.min(total, startIndex + 50);
const newCurrentIndex = itemIndex - startIndex;
@ -134,7 +148,7 @@ Component({
const sources = items.slice(startIndex, endIndex).map((item) => {
return {
url: item.sourceURL,
type: item.type === 0 ? "image" : "video"
type: item.type === MediaItemType.IMAGE ? "image" : "video"
}
}) as any;
wx.previewMedia({