diff --git a/components/edit/relations.vue b/components/edit/relations.vue index 515ff3f..4b8a93b 100644 --- a/components/edit/relations.vue +++ b/components/edit/relations.vue @@ -62,9 +62,29 @@ function toEdit(relation) { type: relation.type, direction: relation.direction, comment: relation.comment || null, + share: relation.share || null, }; } +const shareModeLabels = { + from: 'from', + to: 'to', + both: 'both ways', +}; + +// e.g. 'Copies tags both ways, actors from' +function getShareSummary(relation) { + const share = getEditRelation(relation)?.share; + + if (!share) { + return null; + } + + const summary = Object.entries(share).map(([field, mode]) => `${field} ${shareModeLabels[mode] || mode}`).join(', '); + + return `Copies ${summary} the related scene`.replace(/ both ways the related scene$/, ' both ways'); +} + // adding an existing relation again replaces its comment function addRelation(relation) { showDialog.value = false; @@ -116,6 +136,13 @@ watch(() => props.scene, () => { > ({{ getComment(relation) }}) + + props.scene, () => { text-decoration: inherit; } +.shared { + height: auto; + padding: .25rem .3rem; + fill: var(--primary); +} + .relation-comment { display: inline-block; max-width: 15rem; diff --git a/components/edit/revisions.vue b/components/edit/revisions.vue index aaa1611..96e2e35 100644 --- a/components/edit/revisions.vue +++ b/components/edit/revisions.vue @@ -254,7 +254,16 @@ async function reviewRevision(revision, isApproved) { revisionId: revision.id, }); - revisions.value = revisions.value.map((rev) => (rev.id === updatedRevision.revision.id ? updatedRevision.revision : rev)); + // revisions generated by sharing a relation were reviewed along with this one + const childIds = new Set(updatedRevision.revision.childRevisions?.map((child) => child.id) || []); + const updatedChildren = await Promise.all(revisions.value + .filter((rev) => childIds.has(rev.id)) + .map(async (rev) => (await get(`/revisions/${domain}/${rev.id}`, { revisionId: rev.id })).revision)); + + const updatedById = Object.fromEntries([updatedRevision.revision, ...updatedChildren].map((rev) => [rev.id, rev])); + + childIds.forEach((childId) => reviewedRevisions.value.add(childId)); + revisions.value = revisions.value.map((rev) => updatedById[rev.id] || rev); } catch (error) { reviewedRevisions.value.delete(revision.id); @@ -351,6 +360,11 @@ async function banEditor(revision) { class="nolink" >{{ rev.review.username }} {{ format(rev.review.reviewedAt, 'yyyy-MM-dd hh:mm') }} + Reviewed with #{{ rev.parentRevisionId }} +
import { format, startOfDay, startOfMonth, startOfYear } from 'date-fns'; -import { computed, onMounted, ref, useId } from 'vue'; +import { computed, onMounted, ref, useId, watch } from 'vue'; import Dialog from '#/components/dialog/dialog.vue'; +import Ellipsis from '#/components/loading/ellipsis.vue'; -import { get } from '#/src/api.js'; +import { get, post } from '#/src/api.js'; import getPath from '#/src/get-path.js'; import { getRelationLabel, sortRelationTypes } from '#/utils/relation-labels.js'; @@ -37,6 +38,34 @@ const searchCompleted = ref(false); // only show 'no results' once a search for let searchTimeout = null; let searchCount = 0; +// copy actors or tags between the scenes once when the revision is applied, relative to this scene +const share = ref({}); +const shareOptions = ref(null); // what each scene would get from the other if everything were copied both ways +const previewing = ref(false); + +const shareFieldLabels = { + actors: 'Copy actors', + tags: 'Copy tags', + duration: 'Duration', +}; + +const sceneTitle = computed(() => props.scene.title || `scene #${props.scene.id}`); +const relatedTitle = computed(() => (selectedScene.value ? selectedScene.value.title || `scene #${selectedScene.value.id}` : 'the related scene')); + +// the same modes for every field, with titles naming the field they apply to +function getShareModes(field) { + const fieldName = (shareFieldLabels[field] || field).toLowerCase(); + + return [ + { mode: 'from', label: 'From related', title: `Copy ${fieldName} from "${relatedTitle.value}" to "${sceneTitle.value}"` }, + { mode: 'both', label: 'Both ways', title: `Copy ${fieldName} between both scenes` }, + { mode: 'to', label: 'To related', title: `Copy ${fieldName} from "${sceneTitle.value}" to "${relatedTitle.value}"` }, + { mode: 'none', label: 'None', title: `Do not share any ${fieldName}` }, + ]; +} + +let previewCount = 0; + // each type can be read from either side, e.g. this scene is a 'Remaster of' or 'Remastered as' the selected scene // symmetric types like variants read the same both ways const options = computed(() => sortRelationTypes(props.types, ({ type }) => type).flatMap(({ type, symmetric }) => (symmetric ? ['both'] : ['target', 'source']).map((direction) => ({ @@ -86,6 +115,100 @@ const inversion = computed(() => { }; }); +const selectedType = computed(() => props.types.find((type) => type.type === selectedOption.value?.type)); +// every type has the same shareable fields, so they can be shown before a type is selected +const shareFields = computed(() => selectedType.value?.shareFields || props.types[0]?.shareFields || []); +const isSharing = computed(() => Object.values(share.value).some((mode) => mode && mode !== 'none')); + +// defaults are stored by which side of the relation receives, translate them to this scene's point of view +function getDefaultShare(type, direction) { + const ownSide = direction === 'target' ? 'source' : 'target'; // direction is the related scene's side + + return Object.fromEntries((type?.shareFields || []).map((field) => { + const receiver = type.shareDefaults?.[field]; + + if (!receiver) { + return [field, 'none']; + } + + if (receiver === 'both' || direction === 'both') { + return [field, 'both']; + } + + return [field, receiver === ownSide ? 'from' : 'to']; + })); +} + +watch(selectedOption, (option) => { + share.value = getDefaultShare(selectedType.value, option?.direction); +}); + +// what can be copied only depends on the two scenes, so it's fetched once per selected scene and filtered by the share modes locally +async function fetchShareOptions() { + const previewId = ++previewCount; + + shareOptions.value = null; + + if (!selectedScene.value) { + previewing.value = false; + return; + } + + previewing.value = true; + + try { + const result = await post(`/scenes/${props.scene.id}/relations/share-preview`, { + sceneId: selectedScene.value.id, + }); + + if (previewId === previewCount) { + shareOptions.value = result; + } + } + catch (_error) { + // the preview is only informative + } + finally { + if (previewId === previewCount) { + previewing.value = false; + } + } +} + +watch(selectedScene, fetchShareOptions); + +// what a scene gets with the selected share modes, out of everything it could get +const emptyShareOption = { actors: [], tags: [], duration: null }; + +// shown before a scene is selected, with nothing to copy yet +function getPreviewScene(key, title, option = emptyShareOption, receives) { + const fields = new Set(Object.entries(share.value).filter(([, mode]) => mode === 'both' || mode === receives).map(([field]) => field)); + + const actors = fields.has('actors') ? option.actors : []; + const tags = fields.has('tags') ? option.tags : []; + const duration = fields.has('duration') ? option.duration : null; + + return { + key, + title, + actors, + tags, + duration, + }; +} + +const preview = computed(() => [ + getPreviewScene('scene', sceneTitle.value, shareOptions.value?.scene, 'from'), + getPreviewScene('related', selectedScene.value ? relatedTitle.value : 'Related scene', shareOptions.value?.relatedScene, 'to'), +]); + +function formatDuration(seconds) { + const hours = Math.floor(seconds / 3600); + const minutes = Math.floor((seconds % 3600) / 60); + + return [hours, minutes, seconds % 60].map((unit, index) => (index === 0 ? unit : String(unit).padStart(2, '0'))).join(':').replace(/^0:/, ''); +} + const canSubmit = computed(() => !!selectedScene.value && !!selectedOption.value); const hasChanges = computed(() => !!selectedScene.value || !!selectedOption.value || comment.value.trim().length > 0); @@ -169,6 +292,9 @@ function addRelation() { type: selectedOption.value.type, direction: selectedOption.value.direction, comment: comment.value.trim() || null, + share: isSharing.value + ? Object.fromEntries(Object.entries(share.value).filter(([, mode]) => mode !== 'none')) + : null, scene: selectedScene.value, }); } @@ -194,7 +320,7 @@ onMounted(() => focusInput()); > - {{ scene.title || `Scene #${scene.id}` }} +

{{ scene.title || `Scene #${scene.id}` }}

{{ formatSceneMeta(scene) }}
@@ -226,7 +352,7 @@ onMounted(() => focusInput()); > - {{ selectedScene.title || `Scene #${selectedScene.id}` }} +

{{ selectedScene.title || `Scene #${selectedScene.id}` }}

{{ formatSceneMeta(selectedScene) }}
@@ -278,7 +404,7 @@ onMounted(() => focusInput()); > - {{ result.title || `Scene #${result.id}` }} +

{{ result.title || `Scene #${result.id}` }}

{{ formatSceneMeta(result) }}
@@ -291,12 +417,85 @@ onMounted(() => focusInput()); - + + + + + +