355 lines
7.1 KiB
Vue
355 lines
7.1 KiB
Vue
<script setup>
|
|
import { format } from 'date-fns';
|
|
import { computed, onMounted, ref, useId } from 'vue';
|
|
|
|
import Dialog from '#/components/dialog/dialog.vue';
|
|
|
|
import { get } from '#/src/api.js';
|
|
import getPath from '#/src/get-path.js';
|
|
import { getRelationLabel } from '#/utils/relation-labels.js';
|
|
|
|
const props = defineProps({
|
|
scene: {
|
|
type: Object,
|
|
required: true,
|
|
},
|
|
types: {
|
|
type: Array,
|
|
required: true,
|
|
},
|
|
});
|
|
|
|
const emit = defineEmits(['close', 'relation']);
|
|
|
|
const searchDelay = 300;
|
|
const searchDropdownId = useId();
|
|
|
|
const query = ref('');
|
|
const queryInput = ref(null);
|
|
const results = ref([]);
|
|
const searching = ref(false);
|
|
const selectedScene = ref(null);
|
|
const selectedOption = ref(null);
|
|
const searchCompleted = ref(false); // only show 'no results' once a search for the current query has finished
|
|
|
|
let searchTimeout = null;
|
|
let searchCount = 0;
|
|
|
|
// each type can be read from either side, e.g. this scene is a 'Remaster of' or 'Remastered as' the selected scene
|
|
const options = computed(() => props.types.flatMap((type) => ['target', 'source'].map((direction) => ({
|
|
key: `${type}-${direction}`,
|
|
type,
|
|
direction,
|
|
label: getRelationLabel(type, direction),
|
|
}))));
|
|
|
|
const hasQuery = computed(() => query.value.trim().length > 0);
|
|
const showResults = computed(() => results.value.length > 0 || (hasQuery.value && !searching.value && searchCompleted.value));
|
|
|
|
const canSubmit = computed(() => !!selectedScene.value && !!selectedOption.value);
|
|
const hasChanges = computed(() => !!selectedScene.value || !!selectedOption.value);
|
|
|
|
// accept a pasted scene ID or traxxx scene URL, the search index does not match IDs
|
|
function getSceneId(value) {
|
|
const id = value.match(/^\s*(\d+)\s*$/)?.[1] || value.match(/\/scene\/(\d+)/)?.[1];
|
|
|
|
return id ? Number(id) : null;
|
|
}
|
|
|
|
async function search() {
|
|
const searchId = ++searchCount;
|
|
const sceneId = getSceneId(query.value);
|
|
|
|
searching.value = true;
|
|
searchCompleted.value = false;
|
|
|
|
try {
|
|
const scenes = sceneId
|
|
? [await get(`/scenes/${sceneId}`)]
|
|
: (await get('/scenes', { q: query.value, limit: 10 })).scenes;
|
|
|
|
if (searchId === searchCount) {
|
|
results.value = scenes.filter((result) => result && result.id !== props.scene.id);
|
|
}
|
|
}
|
|
catch (_error) {
|
|
if (searchId === searchCount) {
|
|
results.value = [];
|
|
}
|
|
}
|
|
finally {
|
|
if (searchId === searchCount) {
|
|
searching.value = false;
|
|
searchCompleted.value = true;
|
|
}
|
|
}
|
|
}
|
|
|
|
function queueSearch() {
|
|
clearTimeout(searchTimeout);
|
|
searchCompleted.value = false;
|
|
|
|
if (!hasQuery.value) {
|
|
searchCount += 1; // discard pending responses
|
|
results.value = [];
|
|
searching.value = false;
|
|
|
|
return;
|
|
}
|
|
|
|
searchTimeout = setTimeout(search, searchDelay);
|
|
}
|
|
|
|
function focusInput() {
|
|
setTimeout(() => {
|
|
queryInput.value?.focus();
|
|
}, 100);
|
|
}
|
|
|
|
function selectScene(result) {
|
|
selectedScene.value = result;
|
|
results.value = [];
|
|
query.value = '';
|
|
searchCompleted.value = false;
|
|
}
|
|
|
|
function deselectScene() {
|
|
selectedScene.value = null;
|
|
focusInput();
|
|
}
|
|
|
|
function formatSceneMeta(result) {
|
|
return [result.channel.name, result.date && format(result.date, 'yyyy-MM-dd')].filter(Boolean).join(', ');
|
|
}
|
|
|
|
// the relation is only added to the edit, it's stored when the revision is submitted
|
|
function addRelation() {
|
|
emit('relation', {
|
|
sceneId: selectedScene.value.id,
|
|
type: selectedOption.value.type,
|
|
direction: selectedOption.value.direction,
|
|
scene: selectedScene.value,
|
|
});
|
|
}
|
|
|
|
onMounted(() => focusInput());
|
|
</script>
|
|
|
|
<template>
|
|
<Dialog
|
|
title="Add relation"
|
|
:confirm-close="hasChanges"
|
|
@close="emit('close')"
|
|
>
|
|
<form
|
|
class="dialog-body"
|
|
@submit.prevent="addRelation"
|
|
>
|
|
<div class="scene current">
|
|
<img
|
|
v-if="scene.poster"
|
|
:src="getPath(scene.poster, 'thumbnail')"
|
|
class="poster"
|
|
>
|
|
|
|
<span class="info">
|
|
<span class="title ellipsis">{{ scene.title || `Scene #${scene.id}` }}</span>
|
|
<span class="meta ellipsis">{{ formatSceneMeta(scene) }}</span>
|
|
</span>
|
|
</div>
|
|
|
|
<select
|
|
v-model="selectedOption"
|
|
class="input"
|
|
>
|
|
<option
|
|
:value="null"
|
|
disabled
|
|
>Select relation</option>
|
|
|
|
<option
|
|
v-for="option in options"
|
|
:key="`option-${option.key}`"
|
|
:value="option"
|
|
>{{ option.label }}</option>
|
|
</select>
|
|
|
|
<div
|
|
v-if="selectedScene"
|
|
class="scene selected"
|
|
>
|
|
<img
|
|
v-if="selectedScene.poster"
|
|
:src="getPath(selectedScene.poster, 'thumbnail')"
|
|
class="poster"
|
|
>
|
|
|
|
<span class="info">
|
|
<span class="title ellipsis">{{ selectedScene.title || `Scene #${selectedScene.id}` }}</span>
|
|
<span class="meta ellipsis">{{ formatSceneMeta(selectedScene) }}</span>
|
|
</span>
|
|
|
|
<Icon
|
|
icon="cross2"
|
|
title="Choose another scene"
|
|
class="deselect"
|
|
@click="deselectScene"
|
|
/>
|
|
</div>
|
|
|
|
<VDropdown
|
|
v-else
|
|
:aria-id="searchDropdownId"
|
|
:triggers="[]"
|
|
:shown="showResults"
|
|
:auto-hide="false"
|
|
placement="bottom-start"
|
|
class="search"
|
|
>
|
|
<input
|
|
ref="queryInput"
|
|
v-model="query"
|
|
type="search"
|
|
enterkeyhint="search"
|
|
placeholder="Search scene, or paste an ID or URL"
|
|
class="input"
|
|
@input="queueSearch"
|
|
@keydown.enter.prevent
|
|
>
|
|
|
|
<template #popper>
|
|
<ul
|
|
v-if="results.length > 0"
|
|
class="results nolist"
|
|
>
|
|
<li
|
|
v-for="result in results"
|
|
:key="`result-${result.id}`"
|
|
v-close-popper
|
|
class="scene result"
|
|
@click="selectScene(result)"
|
|
>
|
|
<img
|
|
v-if="result.poster"
|
|
:src="getPath(result.poster, 'thumbnail')"
|
|
loading="lazy"
|
|
class="poster"
|
|
>
|
|
|
|
<span class="info">
|
|
<span class="title ellipsis">{{ result.title || `Scene #${result.id}` }}</span>
|
|
<span class="meta ellipsis">{{ formatSceneMeta(result) }}</span>
|
|
</span>
|
|
</li>
|
|
</ul>
|
|
|
|
<p
|
|
v-else
|
|
class="status"
|
|
>No scenes found</p>
|
|
</template>
|
|
</VDropdown>
|
|
|
|
<div class="dialog-actions">
|
|
<button
|
|
:disabled="!canSubmit"
|
|
class="button button-primary"
|
|
>Add relation</button>
|
|
</div>
|
|
</form>
|
|
</Dialog>
|
|
</template>
|
|
|
|
<style scoped>
|
|
.dialog-body {
|
|
width: 30rem;
|
|
max-width: 100%;
|
|
box-sizing: border-box;
|
|
gap: .75rem;
|
|
padding: 1rem;
|
|
|
|
.input {
|
|
width: 100%;
|
|
box-sizing: border-box;
|
|
}
|
|
}
|
|
|
|
.dialog-actions {
|
|
display: flex;
|
|
justify-content: center;
|
|
margin-top: .5rem;
|
|
}
|
|
|
|
.search {
|
|
width: 100%;
|
|
}
|
|
|
|
.results {
|
|
width: 28rem;
|
|
max-width: calc(100vw - 2rem);
|
|
max-height: 18rem;
|
|
overflow-y: auto;
|
|
padding: .25rem 0;
|
|
}
|
|
|
|
.scene {
|
|
display: flex;
|
|
align-items: center;
|
|
gap: .5rem;
|
|
padding: .25rem .5rem;
|
|
overflow: hidden;
|
|
}
|
|
|
|
.result:hover {
|
|
background: var(--glass-weak-50);
|
|
cursor: pointer;
|
|
|
|
.title {
|
|
color: var(--primary);
|
|
}
|
|
}
|
|
|
|
.current,
|
|
.selected {
|
|
border-radius: .25rem;
|
|
background: var(--glass-weak-50);
|
|
}
|
|
|
|
.deselect {
|
|
flex-shrink: 0;
|
|
padding: .5rem;
|
|
fill: var(--glass-strong-10);
|
|
|
|
&:hover {
|
|
fill: var(--primary);
|
|
cursor: pointer;
|
|
}
|
|
}
|
|
|
|
.poster {
|
|
width: 5rem;
|
|
aspect-ratio: 16 / 9;
|
|
flex-shrink: 0;
|
|
object-fit: cover;
|
|
border-radius: .25rem;
|
|
}
|
|
|
|
.info {
|
|
display: flex;
|
|
flex-direction: column;
|
|
flex-grow: 1;
|
|
overflow: hidden;
|
|
}
|
|
|
|
.meta,
|
|
.status {
|
|
color: var(--glass-strong-10);
|
|
font-size: .9rem;
|
|
}
|
|
|
|
.status {
|
|
margin: 0;
|
|
padding: .5rem;
|
|
}
|
|
</style>
|