From 09336f99cd0edfad3da3d9f5804bf7418ed13a5a Mon Sep 17 00:00:00 2001 From: Mikael Finstad Date: Mon, 28 Mar 2022 14:28:17 +0800 Subject: [PATCH] implement option to shift segments start/end/both --- .eslintrc | 2 +- src/App.jsx | 15 +++++++++++---- src/dialogs.jsx | 27 +++++++++++++++++++++++++-- 3 files changed, 37 insertions(+), 7 deletions(-) diff --git a/.eslintrc b/.eslintrc index 65734688..7743610c 100644 --- a/.eslintrc +++ b/.eslintrc @@ -26,7 +26,7 @@ "no-promise-executor-return": 0, "react/function-component-definition": 0 }, - "parserOptions": { + "parserOptions": { "ecmaVersion": 2022 } } diff --git a/src/App.jsx b/src/App.jsx index e7d0ee54..4b2a854c 100644 --- a/src/App.jsx +++ b/src/App.jsx @@ -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]); diff --git a/src/dialogs.jsx b/src/dialogs.jsx index 477fa065..034aed6a 100644 --- a/src/dialogs.jsx +++ b/src/dialogs.jsx @@ -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() {