cmvr-visual/frontend/src/components/studio/CaptionSegment.vue

204 lines
4.9 KiB
Vue
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<script setup lang="ts">
import { computed, ref, watch } from 'vue'
import type { CaptionEdit, CaptionRow } from '../../types/editors'
import { editCaption, mergeCaption, splitCaption } from '../../domain/creation/captionEdits'
const props = defineProps<{
row: CaptionRow
next?: CaptionRow
playhead: number | null
disabled: boolean
duration: number
}>()
const emit = defineEmits<{ change: [range: CaptionRow, edits: CaptionEdit[]] }>()
const text = ref(''),
start = ref<number | ''>(''),
end = ref<number | ''>(''),
cut = ref(0),
textLock = ref(false),
timeLock = ref(false),
warning = ref('')
const source = computed(() => Array.from(props.row.source_text))
watch(
() => props.row,
(r) => {
text.value = r.text
start.value = r.local_start_frame ?? ''
end.value = r.local_end_frame ?? ''
cut.value = r.source_start + 1
textLock.value = r.text_override
timeLock.value = r.timing_override
warning.value = ''
},
{ immediate: true },
)
function save() {
if (!text.value.trim() || /[\r\n]/.test(text.value)) {
warning.value = '请填写单行显示文案。'
return
}
if (
timeLock.value &&
(typeof start.value !== 'number' ||
typeof end.value !== 'number' ||
!Number.isInteger(start.value) ||
!Number.isInteger(end.value) ||
start.value < 0 ||
end.value <= start.value ||
end.value > props.duration)
) {
warning.value = '请填写镜头范围内的起止帧,结束帧须晚于开始帧。'
return
}
const e = editCaption(props.row)
delete e.text
delete e.start_frame
delete e.end_frame
e.text_override = textLock.value
e.timing_override = timeLock.value
if (e.text_override) e.text = text.value
if (e.timing_override) {
e.start_frame = Number(start.value)
e.end_frame = Number(end.value)
}
warning.value = ''
emit('change', props.row, [e])
}
function split() {
try {
emit('change', props.row, splitCaption(props.row, cut.value, props.playhead ?? NaN))
} catch (e) {
warning.value = (e as Error).message
}
}
function merge() {
if (!props.next) return
try {
const e = mergeCaption(props.row, props.next)
emit('change', { ...props.row, source_end: e.source_end }, [e])
} catch (e) {
warning.value = (e as Error).message
}
}
</script>
<template>
<fieldset
:disabled="disabled"
class="segment"
>
<legend>
{{
{ automatic: '自动', sentence_fallback: '整句回退', manual: '人工', pending: '待校时' }[
row.status
]
}}
· {{ row.start_frame ?? '—' }}–{{ row.end_frame ?? '—' }} 全片帧
</legend>
<label
>显示文案<input
v-model="text"
maxlength="2000"
@input="textLock = true"
/></label>
<div class="options">
<label
><input
v-model="textLock"
type="checkbox"
/>锁定文字</label
>
<label
>镜内开始帧<input
v-model.number="start"
type="number"
min="0"
:max="duration - 1"
@input="timeLock = true"
/></label>
<label
>结束帧<input
v-model.number="end"
type="number"
min="1"
:max="duration"
@input="timeLock = true"
/></label>
<label
><input
v-model="timeLock"
type="checkbox"
/>锁定时间</label
>
<button
type="button"
@click="save"
>
应用片段修改
</button>
</div>
<div class="options">
<label v-if="row.source_end - row.source_start > 1"
>原文断点<select v-model.number="cut">
<option
v-for="n in row.source_end - row.source_start - 1"
:key="n"
:value="row.source_start + n"
>
第 {{ n }} 字「{{ source[row.source_start + n - 1] }}」后
</option>
</select></label
>
<button
type="button"
:disabled="row.source_end - row.source_start < 2 || playhead === null"
@click="split"
>
按播放头拆分
</button>
<button
type="button"
:disabled="!next || next.source_start !== row.source_end"
@click="merge"
>
合并下一段
</button>
<button
type="button"
@click="
emit('change', row, [
{ ...editCaption(row), deleted: true, text_override: false, timing_override: false },
])
"
>
删除片段
</button>
</div>
<p
v-if="warning"
role="alert"
>
{{ warning }}
</p>
</fieldset>
</template>
<style scoped>
.segment {
min-width: 0;
display: grid;
gap: 0.5rem;
border: 1px solid var(--el-border-color);
padding: 0.6rem;
}
.segment > label {
display: grid;
}
.options {
display: flex;
gap: 0.6rem;
align-items: center;
flex-wrap: wrap;
}
.options input[type='number'] {
width: 5rem;
}
</style>