Skip to content

Commit

Permalink
implement option to shift segments start/end/both
Browse files Browse the repository at this point in the history
  • Loading branch information
mifi committed Mar 28, 2022
1 parent 89d8745 commit 09336f9
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 7 deletions.
2 changes: 1 addition & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
"no-promise-executor-return": 0,
"react/function-component-definition": 0
},
"parserOptions": {
"parserOptions": {
"ecmaVersion": 2022
}
}
15 changes: 11 additions & 4 deletions src/App.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
Expand Down
27 changes: 25 additions & 2 deletions src/dialogs.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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() {
Expand Down

0 comments on commit 09336f9

Please sign in to comment.