1
0
mirror of https://github.com/mifi/lossless-cut.git synced 2024-11-22 02:12:30 +01:00

implement option to shift segments start/end/both

This commit is contained in:
Mikael Finstad 2022-03-28 14:28:17 +08:00
parent 89d8745fdf
commit 09336f99cd
No known key found for this signature in database
GPG Key ID: 25AB36E3E81CBC26
3 changed files with 37 additions and 7 deletions

View File

@ -26,7 +26,7 @@
"no-promise-executor-return": 0,
"react/function-component-definition": 0
},
"parserOptions": {
"parserOptions": {
"ecmaVersion": 2022
}
}

View File

@ -415,10 +415,17 @@ const App = memo(() => {
}, [currentSegIndexSafe, getSegApparentEnd, currentCutSeg, duration, updateSegAtIndex]);
const shiftAllSegmentTimes = useCallback(async () => {
const shiftValue = await askForShiftSegments();
if (shiftValue == null) return;
const clampValue = (val) => Math.min(Math.max(val + shiftValue, 0), duration);
const newSegments = apparentCutSegments.map((segment) => ({ ...segment, start: clampValue(segment.start + shiftValue), end: clampValue(segment.end + shiftValue) })).filter((segment) => segment.end > segment.start);
const shift = await askForShiftSegments();
if (shift == null) return;
const { shiftAmount, shiftValues } = shift;
const clampValue = (val) => Math.min(Math.max(val + shiftAmount, 0), duration);
const newSegments = apparentCutSegments.map((segment) => {
const newSegment = { ...segment };
shiftValues.forEach((key) => {
newSegment[key] = clampValue(segment[key] + shiftAmount);
});
return newSegment;
}).filter((segment) => segment.end > segment.start);
if (newSegments.length < 1) setCutSegments(createInitialCutSegments());
else setCutSegments(newSegments);
}, [apparentCutSegments, createInitialCutSegments, duration, setCutSegments]);

View File

@ -8,7 +8,7 @@ import SyntaxHighlighter from 'react-syntax-highlighter';
import { tomorrow as style } from 'react-syntax-highlighter/dist/esm/styles/hljs';
import JSON5 from 'json5';
import { parseDuration } from './util/duration';
import { parseDuration, formatDuration } from './util/duration';
import { parseYouTube } from './edlFormats';
import CopyClipboardButton from './components/CopyClipboardButton';
@ -239,6 +239,21 @@ async function askForSegmentDuration(fileDuration) {
return parseDuration(value);
}
async function askForShiftSegmentsVariant(time) {
const { value } = await Swal.fire({
input: 'radio',
showCancelButton: true,
inputOptions: {
start: i18n.t('Start'),
end: i18n.t('End'),
both: i18n.t('Both'),
},
inputValue: 'both',
text: i18n.t('Do you want to shift the start or end timestamp by {{time}}?', { time: formatDuration({ seconds: time, shorten: true }) }),
});
return value;
}
export async function askForShiftSegments() {
function parseValue(value) {
let parseableValue = value;
@ -267,7 +282,15 @@ export async function askForShiftSegments() {
});
if (value == null) return undefined;
return parseValue(value);
const parsed = parseValue(value);
const shiftVariant = await askForShiftSegmentsVariant(parsed);
if (shiftVariant == null) return undefined;
return {
shiftAmount: parsed,
shiftValues: shiftVariant === 'both' ? ['start', 'end'] : [shiftVariant],
};
}
export async function askForMetadataKey() {