-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathByGroup.vue
More file actions
88 lines (76 loc) · 2.44 KB
/
Copy pathByGroup.vue
File metadata and controls
88 lines (76 loc) · 2.44 KB
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
<template>
<div class="grid grid-cols-1 lg:grid-cols-2 cards-gap">
<div class="contents">
<IconCard v-for="[label, files] in labelsAndFiles" :key="label">
<template #title> Group label: {{ label }} </template>
<FileSelection v-model="files.value" type="image" multiple no-upload />
</IconCard>
</div>
</div>
</template>
<script lang="ts" setup>
import { Map, Set } from "immutable";
import type { Ref, WatchHandle } from "vue";
import { computed, ref, watch } from "vue";
import { Dataset } from "@epfml/discojs";
import { loadImage } from "@epfml/discojs-web";
import IconCard from "@/components/containers/IconCard.vue";
import FileSelection from "../FileSelection.vue";
import type { NamedLabeledImageDataset } from "../types.js";
const props = defineProps<{
labels: Set<string>;
}>();
const dataset = defineModel<NamedLabeledImageDataset | undefined>();
watch(dataset, (dataset: NamedLabeledImageDataset | undefined) => {
if (dataset === undefined)
labelsAndFiles.value.forEach(([_, files]) => {
files.value = undefined;
});
});
const labelsAndFiles = computed<Array<[string, Ref<Set<File> | undefined>]>>(
(oldArray) => {
const old = Map(oldArray);
return props.labels
.valueSeq()
.sort()
.map(
(label) =>
[label, old.get(label) ?? ref()] as [
string,
Ref<Set<File> | undefined>,
],
)
.toArray();
},
);
let watcher: WatchHandle;
function refreshWatcher() {
watcher?.(); // stop old watcher
watcher = watch(
labelsAndFiles.value.map(([_, files]) => files),
() => {
const expanded = labelsAndFiles.value.flatMap(
([label, files]) =>
files.value?.map((f) => [label, f] as const)?.toArray() ?? [],
);
// TODO: rm once dataset supports shuffling
// shuffle the filenames o.w. they are ordered by labels
for (let i = 0; i < expanded.length; i++) {
const j = Math.floor(Math.random() * i);
const swap = expanded[i];
if (swap === undefined || expanded[j] === undefined)
throw new Error("out of bound shuffling");
expanded[i] = expanded[j];
expanded[j] = swap;
}
dataset.value = new Dataset(expanded).map(async ([label, file]) => ({
filename: file.name,
image: await loadImage(file),
label,
}));
},
);
}
refreshWatcher();
watch(labelsAndFiles, () => refreshWatcher());
</script>