TemplateDisplay.vue 11.7 KB
Newer Older
LiangLiu's avatar
LiangLiu committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
<script setup>
import { ref, computed, onMounted } from 'vue'
import { useI18n } from 'vue-i18n'

const { t } = useI18n()

// Props
const props = defineProps({
  templates: {
    type: Array,
    default: () => []
  },
  showActions: {
    type: Boolean,
    default: true
  },
  layout: {
    type: String,
    default: 'grid', // 'grid' 或 'waterfall'
    validator: (value) => ['grid', 'waterfall'].includes(value)
  },
  columns: {
    type: Number,
    default: 2
  },
  maxTemplates: {
    type: Number,
    default: 10
  }
})

// 从 utils/other 导入需要的函数
import {
  getTemplateFileUrl,
  handleThumbnailError,
  playVideo,
  pauseVideo,
  toggleVideoPlay,
  onVideoLoaded,
  onVideoError,
  onVideoEnded,
  previewTemplateDetail,
  useTemplate,
  applyTemplateImage,
  applyTemplateAudio,
  copyShareLink
} from '../utils/other'

// 屏幕尺寸响应式状态
const screenSize = ref('large')

// 更新屏幕尺寸
const updateScreenSize = () => {
  screenSize.value = window.innerWidth >= 1024 ? 'large' : 'small'
}

// 随机列布局相关函数(用于网格布局)
const generateRandomColumnLayout = (templates) => {
  if (!templates || templates.length === 0) return { columns: [], templates: [] }

  const numColumns = props.columns

  // 生成随机列宽(总和为100%)
  const columnWidths = []
  let remainingWidth = 100

  for (let i = 0; i < numColumns; i++) {
    if (i === numColumns - 1) {
      columnWidths.push(remainingWidth)
    } else {
      const minWidth = 20
      const maxWidth = Math.min(50, remainingWidth - (numColumns - i - 1) * minWidth)
      const width = Math.random() * (maxWidth - minWidth) + minWidth
      columnWidths.push(Math.round(width))
      remainingWidth -= Math.round(width)
    }
  }

  // 生成每列的起始位置
  const columnStartPositions = []
  for (let i = 0; i < numColumns; i++) {
    const startPosition = Math.random() * 20
    columnStartPositions.push(Math.round(startPosition))
  }

  // 计算每列的起始left位置
  const columnLeftPositions = []
  let currentLeft = 0
  for (let i = 0; i < numColumns; i++) {
    columnLeftPositions.push(currentLeft)
    currentLeft += columnWidths[i]
  }

  // 将模版分配到各列
  const columnTemplates = Array.from({ length: numColumns }, () => [])
  templates.forEach((template, index) => {
    const columnIndex = index % numColumns
    columnTemplates[columnIndex].push(template)
  })

  // 生成列配置
  const columns = columnWidths.map((width, index) => ({
    width: `${width}%`,
    left: `${columnLeftPositions[index]}%`,
    top: `${columnStartPositions[index]}%`,
    templates: columnTemplates[index]
  }))

  return { columns, templates }
}

// 计算属性:带随机列布局的模版
const templatesWithRandomColumns = computed(() => {
  if (props.layout === 'waterfall') {
    return { columns: [], templates: props.templates }
  }
  return generateRandomColumnLayout(props.templates)
})

// 组件挂载时初始化
onMounted(() => {
  updateScreenSize()
  window.addEventListener('resize', updateScreenSize)
})
</script>

<template>
  <div class="template-display">
    <!-- 瀑布流布局 -->
    <div v-if="layout === 'waterfall'" class="waterfall-layout">
      <div class="columns-2 md:columns-2 lg:columns-3 xl:columns-3 gap-4">
        <div v-for="item in templates" :key="item.task_id"
             class="break-inside-avoid mb-4 group relative bg-dark-light/50 rounded-xl overflow-hidden border border-gray-700/30 hover:border-laser-purple/50 transition-all duration-300 hover:shadow-laser/30 hover:bg-dark-light/70">
          <!-- 视频缩略图区域 -->
          <div class="cursor-pointer bg-gray-800 relative flex flex-col"
               @click="showActions ? previewTemplateDetail(item) : null"
               :title="showActions ? t('viewTemplateDetail') : ''">
            <!-- 视频预览 -->
            <video v-if="item?.outputs?.output_video"
                   :src="getTemplateFileUrl(item.outputs.output_video,'videos')"
                   :poster="getTemplateFileUrl(item.inputs.input_image,'images')"
                   class="w-full h-auto object-contain group-hover:scale-105 transition-transform duration-300"
                   preload="auto" playsinline webkit-playsinline
                   @mouseenter="playVideo($event)" @mouseleave="pauseVideo($event)"
                   @loadeddata="onVideoLoaded($event)"
                   @ended="onVideoEnded($event)"
                   @error="onVideoError($event)"></video>
            <!-- 图片缩略图 -->
            <img v-else
                 :src="getTemplateFileUrl(item.inputs.input_image,'images')"
                 :alt="item.params?.prompt || '模板图片'"
                 class="w-full h-auto object-contain group-hover:scale-105 transition-transform duration-300"
                 @error="handleThumbnailError" />
            <!-- 移动端播放按钮 -->
            <button v-if="item?.outputs?.output_video"
                    @click.stop="toggleVideoPlay($event)"
                    class="md:hidden absolute bottom-3 left-1/2 transform -translate-x-1/2 w-10 h-10 rounded-full bg-black/50 backdrop-blur-sm flex items-center justify-center text-white hover:bg-black/70 transition-colors z-20">
              <i class="fas fa-play text-sm"></i>
            </button>
            <!-- 悬浮操作按钮(仅当 showActions 为 true 时显示) -->
            <div v-if="showActions"
                 class="hidden md:flex absolute bottom-3 left-1/2 transform -translate-x-1/2 items-center justify-center opacity-0 group-hover:opacity-100 transition-opacity duration-200 pointer-events-none z-10 w-full">
              <div class="flex space-x-2 pointer-events-auto">
                <button @click.stop="applyTemplateImage(item)"
                        class="w-10 h-10 rounded-full bg-laser-purple backdrop-blur-sm flex items-center justify-center text-white hover:bg-laser-purple transition-colors"
                        :title="t('applyImage')">
                  <i class="fas fa-image text-sm"></i>
                </button>
                <button @click.stop="applyTemplateAudio(item)"
                        class="w-10 h-10 rounded-full bg-laser-purple backdrop-blur-sm flex items-center justify-center text-white hover:bg-laser-purple transition-colors"
                        :title="t('applyAudio')">
                  <i class="fas fa-music text-sm"></i>
                </button>
                <button @click.stop="useTemplate(item)"
                        class="w-10 h-10 rounded-full bg-laser-purple backdrop-blur-sm flex items-center justify-center text-white hover:bg-laser-purple transition-colors"
                        :title="t('useTemplate')">
                  <i class="fas fa-clone text-sm"></i>
                </button>
                <button @click.stop="copyShareLink(item.task_id, 'template')"
                        class="w-10 h-10 rounded-full bg-blue-500 backdrop-blur-sm flex items-center justify-center text-white hover:bg-blue-600 transition-colors"
                        :title="t('shareTemplate')">
                  <i class="fas fa-share-alt text-sm"></i>
                </button>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>

    <!-- 网格布局 -->
    <div v-else class="grid-layout">
      <div class="relative min-h-[400px] lg:min-h-[600px]">
        <!-- 随机列 -->
        <div v-for="(column, columnIndex) in templatesWithRandomColumns.columns" :key="columnIndex"
             class="absolute transition-all duration-500 animate-fade-in"
             :style="{
               width: column.width,
               left: column.left,
               top: column.top,
               animationDelay: `${columnIndex * 0.2}s`
             }">
          <!-- 列内的模版卡片 -->
          <div v-for="item in column.templates" :key="item.task_id"
               class="mb-3 group relative bg-dark-light rounded-xl overflow-hidden border border-gray-700/50 hover:border-laser-purple/40 transition-all duration-300 hover:shadow-laser/20">
            <!-- 视频缩略图区域 -->
            <div class="cursor-pointer bg-gray-800 relative flex flex-col"
                 @click="showActions ? previewTemplateDetail(item) : null"
                 :title="showActions ? t('viewTemplateDetail') : ''">
              <!-- 视频预览 -->
              <video v-if="item?.outputs?.output_video"
                     :src="getTemplateFileUrl(item.outputs.output_video,'videos')"
                     :poster="getTemplateFileUrl(item.inputs.input_image,'images')"
                     class="w-full h-auto object-contain group-hover:scale-105 transition-transform duration-300"
                     preload="auto" playsinline webkit-playsinline
                     @mouseenter="playVideo($event)" @mouseleave="pauseVideo($event)"
                     @loadeddata="onVideoLoaded($event)"
                     @ended="onVideoEnded($event)"
                     @error="onVideoError($event)"></video>
              <!-- 图片缩略图 -->
              <img v-else
                   :src="getTemplateFileUrl(item.inputs.input_image,'images')"
                   :alt="item.params?.prompt || '模板图片'"
                   class="w-full h-auto object-contain group-hover:scale-105 transition-transform duration-300"
                   @error="handleThumbnailError" />
              <!-- 移动端播放按钮 -->
              <button v-if="item?.outputs?.output_video"
                      @click.stop="toggleVideoPlay($event)"
                      class="md:hidden absolute bottom-3 left-1/2 transform -translate-x-1/2 w-10 h-10 rounded-full bg-black/50 backdrop-blur-sm flex items-center justify-center text-white hover:bg-black/70 transition-colors z-20">
                <i class="fas fa-play text-sm"></i>
              </button>
              <!-- 悬浮操作按钮(仅当 showActions 为 true 时显示) -->
              <div v-if="showActions"
                   class="hidden md:flex absolute bottom-3 left-1/2 transform -translate-x-1/2 items-center justify-center opacity-0 group-hover:opacity-100 transition-opacity duration-200 pointer-events-none z-10 w-full">
                <div class="flex space-x-3 pointer-events-auto">
                  <button @click.stop="applyTemplateImage(item)"
                          class="w-10 h-10 rounded-full bg-laser-purple backdrop-blur-sm flex items-center justify-center text-white hover:bg-laser-purple transition-colors"
                          :title="t('applyImage')">
                    <i class="fas fa-image text-sm"></i>
                  </button>
                  <button @click.stop="applyTemplateAudio(item)"
                          class="w-10 h-10 rounded-full bg-laser-purple backdrop-blur-sm flex items-center justify-center text-white hover:bg-laser-purple transition-colors"
                          :title="t('applyAudio')">
                    <i class="fas fa-music text-sm"></i>
                  </button>
                  <button @click.stop="useTemplate(item)"
                          class="w-10 h-10 rounded-full bg-laser-purple backdrop-blur-sm flex items-center justify-center text-white hover:bg-laser-purple transition-colors"
                          :title="t('useTemplate')">
                    <i class="fas fa-clone text-sm"></i>
                  </button>
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<style scoped>
.template-display {
  width: 100%;
}

.waterfall-layout {
  width: 100%;
}

.grid-layout {
  width: 100%;
}

/* 动画效果 */
@keyframes fade-in {
  from {
    opacity: 0;
    transform: translateY(20px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

.animate-fade-in {
  animation: fade-in 0.6s ease-out forwards;
}
</style>