Skip to content

Commit 9b15aff

Browse files
[6.x] Asset field listing preview improvements (#13708)
Co-authored-by: Jason Varga <jason@pixelfear.com>
1 parent 985fa88 commit 9b15aff

2 files changed

Lines changed: 20 additions & 3 deletions

File tree

resources/js/components/fieldtypes/assets/AssetsIndexFieldtype.vue

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
11
<template>
2-
<div class="flex text-2xs">
3-
<a v-for="asset in value" :key="asset.id" :href="asset.url" target="_blank">
2+
<div class="flex text-2xs gap-2">
3+
<a v-for="asset in value.assets.splice(0, 5)" :key="asset.id" :href="asset.url" target="_blank">
44
<asset-thumbnail :asset="asset" class="-my-1 h-8 max-w-3xs" />
55
</a>
6+
<span
7+
v-if="value.total > 6"
8+
class="-my-1 flex h-8 min-w-8 items-center justify-center px-1.5 font-mono text-gray-600 dark:text-gray-400"
9+
>
10+
+&thinsp;{{ value.total - 5 }}
11+
</span>
612
</div>
713
</template>
814

src/Fieldtypes/Assets/Assets.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -438,7 +438,16 @@ public function fieldRules()
438438

439439
public function preProcessIndex($data)
440440
{
441-
return $this->getItemsForPreProcessIndex($data)->map(function ($asset) {
441+
$total = $data === null
442+
? 0
443+
: ($this->config('max_files') === 1 ? 1 : count($data));
444+
445+
// Since we only want to display a handful of thumbnails, we'll slice it up here so we don't perform more
446+
// augmentation overhead than necessary. e.g. 5 thumbs then +remainder. If the remainder is 1, we may
447+
// as well show all 6 since the +1 would almost take up the same amount of space.
448+
$data = collect($data)->take(6)->all();
449+
450+
$assets = $this->getItemsForPreProcessIndex($data)->map(function ($asset) {
442451
$arr = [
443452
'id' => $asset->id(),
444453
'is_image' => $isImage = $asset->isImage(),
@@ -456,6 +465,8 @@ public function preProcessIndex($data)
456465

457466
return $arr;
458467
});
468+
469+
return compact('total', 'assets');
459470
}
460471

461472
protected function getItemsForPreProcessIndex($values): Collection

0 commit comments

Comments
 (0)