Skip to content

Commit

Permalink
Implement rudimentary asset viewer capabilities
Browse files Browse the repository at this point in the history
  • Loading branch information
dokempf committed Jul 22, 2024
1 parent ffb20e0 commit a6ba81b
Show file tree
Hide file tree
Showing 6 changed files with 107 additions and 2 deletions.
3 changes: 1 addition & 2 deletions src/App.svelte
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<script>
import { Button, Heading, Input, Label, MultiSelect, Select } from "flowbite-svelte";
import { assetStore, easydbInstanceStore, easydbInstanceDataStore, easydbInstanceDataPromiseStore, uuidStore } from "./lib/stores";
import { easydbInstanceStore, easydbInstanceDataStore, easydbInstanceDataPromiseStore, uuidStore } from "./lib/stores";
import EasyDbDetailView from "./components/EasyDBDetailView.svelte";
let uuid = "859e2318-32f6-4013-8468-ef8cec0b581b";
Expand All @@ -15,7 +15,6 @@
let selected_data_languages = ['de-DE', 'en-US'];
function dumpStores() {
console.log("assetStore: ", $assetStore);
console.log("easydbInstanceStore: ", $easydbInstanceStore);
console.log("easydbInstanceDataStore: ", $easydbInstanceDataStore);
console.log("easydbInstanceDataPromiseStore: ", $easydbInstanceDataPromiseStore);
Expand Down
2 changes: 2 additions & 0 deletions src/components/EasyDBDetailView.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import { maskObj } from "../lib/easydbHelpers";
import { appLanguageStore, dataLanguagesStore, easydbInstanceStore, easydbInstanceDataPromiseStore, uuidStore } from "../lib/stores";
import AssetViewer from "./logic/AssetViewer.svelte";
import RecursiveEasyDbDetailView from "./logic/RecursiveEasyDBDetailView.svelte";
export let uuid = "";
Expand All @@ -24,6 +25,7 @@
{#await easydb_api_object($uuidStore.at(-1), mask) }
Waiting for API response...
{:then data }
<AssetViewer fields={maskObj(data).fields} data={data} table={maskObj(data).table_name_hint}/>
<RecursiveEasyDbDetailView fields={maskObj(data).fields} data={data} table={maskObj(data).table_name_hint}/>
{/await}
{/await}
23 changes: 23 additions & 0 deletions src/components/logic/AssetDispatch.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<script>
import NotImplemented from "../utils/NotImplemented.svelte";
export let asset;
// Importing all the implememented viewers.
import ImageViewer from "../viewer/ImageViewer.svelte";
import VideoViewer from "../viewer/VideoViewer.svelte";
// Mapping the "class" names from the data to a Svelte component
// implementing the respective viewer.
const componentMapping = {
"audio": VideoViewer,
"image": ImageViewer,
"video": VideoViewer,
};
</script>

{#if asset.class in componentMapping}
<svelte:component this={componentMapping[asset.class]} asset={asset} />
{:else}
<NotImplemented message="Asset type '{asset.class}' not implemented" />
{/if}
63 changes: 63 additions & 0 deletions src/components/logic/AssetViewer.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<script>
import { fieldData, findSchemaColumn, hasReverseSubData, hasSubData, linkedSubData, reverseLinkedSubData } from "../../lib/easydbHelpers";
import { Carousel, Pagination } from "flowbite-svelte";
import AssetDispatch from "./AssetDispatch.svelte";
export let fields;
export let table;
export let data;
function findAssets(fields, table, data) {
let ret = [];
for (const field of fields) {
if (field.kind === "field") {
if (findSchemaColumn(table, field).type === "eas") {
ret = ret.concat(fieldData(data, table, field));
}
}
if (field.kind === "linked-table") {
if (hasSubData(data, table, field)) {
for (const subdata of linkedSubData(data, table, field)) {
ret = ret.concat(findAssets(field.mask.fields, field.other_table_name_hint, subdata));
}
}
}
if (field.kind === "reverse-linked-table") {
if (hasReverseSubData(data, table, field)) {
for (const subdata of reverseLinkedSubData(data, table, field)) {
ret = ret.concat(findAssets(field.mask.fields, field.other_table_name_hint, subdata));
}
}
}
}
return ret;
}
function getCarouselImages(assets) {
return assets.map((asset) => { return {src: asset.versions.preview.url}});
}
const assets = findAssets(fields, table, data);
console.log(assets);
</script>

<!-- If there are no assets, no viewer is shown -->
{#if assets.length > 0}
<!-- If all assets are images, we show them as a carousel, as we have this
component readily available from flowbite svelte -->
{#if assets.every((asset) => asset.class === "image") }
<Carousel images={getCarouselImages(assets)} imgClass="object-contain" class="bg-gray-200" let:Controls>
<Controls />
</Carousel>
{:else}
<!-- In this case we construct our own carousel-like thing with pagination -->
{#if assets.length > 1}
Pagination
<!-- <Pagination /> -->
{/if}
{#each assets as asset}
<AssetDispatch asset={asset} />
{/each}
{/if}
{/if}
11 changes: 11 additions & 0 deletions src/components/viewer/ImageViewer.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<!-- NB: This component is used much less than you would expect, because
datasets that do only have image assets are displayed using the
Carousel component from Flowbite Svelte. Only in the case of mixing
image and non-image assets this is used to display images. -->
<script>
import { Img } from "flowbite-svelte";
export let asset;
</script>

<Img src={asset.versions.preview.url} alt={asset.original_filename} />
7 changes: 7 additions & 0 deletions src/components/viewer/VideoViewer.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<script>
import { Video } from "flowbite-svelte";
export let asset;
</script>

<Video src={asset.versions.original.url} controls />

0 comments on commit a6ba81b

Please sign in to comment.