Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove all segments button #1097

Merged
merged 8 commits into from
Sep 21, 2023
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/i18n/locales/en-US.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
"delete-button": "Delete",
"delete-restore-tooltip": "Mark or unmark the segment at the current position as to be deleted. Hotkey: {{hotkeyName}}",
"delete-restore-tooltip-aria": "Delete and Restore. Mark or unmark the segment at the current position as to be deleted. Hotkey: {{hotKeyName}}.",
"merge-all-button": "Merge All",
"merge-all-tooltip": "Combine all segments into a single segment.",
"merge-all-tooltip-aria": "Merge All. Combine all segments into a single segment.",
"restore-button": "Restore",
"mergeLeft-button": "Merge Left",
"mergeLeft-tooltip": "Combine the currently active segment with the segment to its left. Hotkey: {{hotkeyName}}",
Expand Down
8 changes: 7 additions & 1 deletion src/main/CuttingActions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { basicButtonStyle, flexGapReplacementStyle } from '../cssStyles'
import { IconProp } from "@fortawesome/fontawesome-svg-core";
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
import {
faArrowsLeftRightToLine,
faCut,
faStepBackward,
faStepForward,
Expand All @@ -16,7 +17,7 @@ import { css } from '@emotion/react'

import { useDispatch, useSelector } from 'react-redux';
import {
cut, markAsDeletedOrAlive, selectIsCurrentSegmentAlive, mergeLeft, mergeRight
cut, markAsDeletedOrAlive, selectIsCurrentSegmentAlive, mergeLeft, mergeRight, mergeAll
} from '../redux/videoSlice'
import { GlobalHotKeys, KeySequence, KeyMapOptions } from "react-hotkeys";
import { cuttingKeyMap } from "../globalKeys";
Expand Down Expand Up @@ -96,6 +97,11 @@ const CuttingActions: React.FC = () => {
tooltip={t('cuttingActions.mergeRight-tooltip', { hotkeyName: (cuttingKeyMap[handlers.mergeRight.name] as KeyMapOptions).sequence })}
ariaLabelText={t('cuttingActions.mergeRight-tooltip-aria', { hotkeyName: (cuttingKeyMap[handlers.mergeRight.name] as KeyMapOptions).sequence })}
/>
<CuttingActionsButton iconName={faArrowsLeftRightToLine}
actionName={t("cuttingActions.merge-all-button")} actionHandler={dispatchAction} action={mergeAll}
tooltip={t('cuttingActions.merge-all-tooltip')}
ariaLabelText={t('cuttingActions.merge-all-tooltip-aria')}
/>
</div>
<div css={blockStyle}>
{/* <CuttingActionsButton iconName={faQuestion} actionName="Reset changes" action={null}
Expand Down
27 changes: 17 additions & 10 deletions src/redux/videoSlice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,10 @@ const videoSlice = createSlice({
mergeSegments(state, state.activeSegmentIndex, state.activeSegmentIndex + 1)
state.hasChanges = true
},
mergeAll: state => {
mergeSegments(state, 0, state.segments.length - 1)
state.hasChanges = true
},
},
// For Async Requests
extraReducers: builder => {
Expand Down Expand Up @@ -258,22 +262,25 @@ export const parseSegments = (segments: Segment[], duration: number) => {
}

/**
* Helper function for merging two segments
* Helper function for merging segments
*/
const mergeSegments = (state: video, activeSegmentIndex: number, mergeSegmentIndex: number) => {
const mergeSegments = (state: video, startSegmentIndex: number, endSegmentIndex: number) => {
// Check if mergeSegmentIndex is valid
if (mergeSegmentIndex < 0 || mergeSegmentIndex > state.segments.length - 1) {
if (endSegmentIndex < 0 || endSegmentIndex > state.segments.length - 1) {
return
}

// Increase activeSegment length
state.segments[activeSegmentIndex].start = Math.min(
state.segments[activeSegmentIndex].start, state.segments[mergeSegmentIndex].start)
state.segments[activeSegmentIndex].end = Math.max(
state.segments[activeSegmentIndex].end, state.segments[mergeSegmentIndex].end)
state.segments[startSegmentIndex].start = Math.min(
state.segments[startSegmentIndex].start, state.segments[endSegmentIndex].start)
state.segments[startSegmentIndex].end = Math.max(
state.segments[startSegmentIndex].end, state.segments[endSegmentIndex].end)

// Remove the other segment
state.segments.splice(mergeSegmentIndex, 1);
// Remove the end segment and segments between
state.segments.splice(
startSegmentIndex < endSegmentIndex ? startSegmentIndex + 1 : endSegmentIndex,
Math.abs(endSegmentIndex - startSegmentIndex)
);

// Update active segment
updateActiveSegment(state)
Expand Down Expand Up @@ -336,7 +343,7 @@ const setThumbnailHelper = (state: video, id: Track["id"], uri: Track["thumbnail

export const { setTrackEnabled, setIsPlaying, setIsPlayPreview, setCurrentlyAt, setCurrentlyAtInSeconds,
addSegment, setAspectRatio, setHasChanges, setWaveformImages, setThumbnails, setThumbnail, removeThumbnail,
cut, markAsDeletedOrAlive, setSelectedWorkflowIndex, mergeLeft, mergeRight, setPreviewTriggered,
cut, markAsDeletedOrAlive, setSelectedWorkflowIndex, mergeLeft, mergeRight, mergeAll, setPreviewTriggered,
setClickTriggered } = videoSlice.actions

// Export selectors
Expand Down