Skip to content

Commit

Permalink
add menu bar to audioviewer
Browse files Browse the repository at this point in the history
  • Loading branch information
Tarek committed Oct 11, 2023
1 parent 017bb9c commit ac81ccc
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 0 deletions.
54 changes: 54 additions & 0 deletions src/components/shared/AudioViewer/MenuBar.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import SettingsIcon from '../../../icons/Settings';
import Dropdown from '../../../components/ui/Dropdown';
import Menu from '../../../components/ui/Menu';
import { FunctionComponent } from 'react';
import tw, { styled } from 'twin.macro';

const Styles = styled.div`
${tw`z-10 px-2 py-1 absolute top-0 right-0 items-start flex flex-row-reverse`}
button {
${tw`text-gray-100
hover:text-gray-400
active:hover:text-gray-200
focus:text-gray-600
`}
}
`;

interface Props {
className?: string;
isLooping: boolean;
shouldAutostart: boolean;
onChangeLoop: (enabled: boolean) => void;
onChangeAutostart: (enabled: boolean) => void;
}

const MenuBar: FunctionComponent<Props> = ({
className,
isLooping,
shouldAutostart,
onChangeLoop,
onChangeAutostart,
}) => {
const content = (
<Menu>
<Menu.Title>Player settings</Menu.Title>
<Menu.Switch value={isLooping} onChange={onChangeLoop}>
Loop
</Menu.Switch>
<Menu.Switch value={shouldAutostart} onChange={onChangeAutostart}>
Autoplay
</Menu.Switch>
</Menu>
);

return (
<Styles className={className}>
<Dropdown content={content}>
<SettingsIcon />
</Dropdown>
</Styles>
);
};

export default MenuBar;
37 changes: 37 additions & 0 deletions src/components/shared/AudioViewer/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import { notifyProblem } from '../../../notify';
import WaveSurfer from 'wavesurfer.js';
import RegionsPlugin from 'wavesurfer.js/dist/plugin/wavesurfer.regions.min.js';
import CursorPlugin from 'wavesurfer.js/dist/plugin/wavesurfer.cursor.min.js';
import useSetting from '../../../lenses/useSetting';
import MenuBar from './MenuBar';

// Maximum zoom level for waveform
const MAX_ZOOM = 2500;
Expand Down Expand Up @@ -169,6 +171,8 @@ const AudioViewer = ({

const [isPlaying, setIsPlaying] = useState(false);
const [isReady, setIsReady] = useState(false);
const [autoplay, setAutoplay] = useSetting('autoplay', false);
const [looping, setLooping] = useSetting('looping', false);

const redrawWaveform = (height: number) => {
waveform.current?.setHeight(height);
Expand Down Expand Up @@ -287,6 +291,14 @@ const AudioViewer = ({
redrawWaveform(height);

setIsReady(true);

if (!waveform.current) return;

if (autoplay) waveform.current.play();
// "media" does not exist @compiletime.
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
waveform.current.backend.media.loop = looping;
});

// In case this widget was paused from the outside
Expand Down Expand Up @@ -315,6 +327,8 @@ const AudioViewer = ({
onRegionClick,
onRegionEnter,
onRegionLeave,
autoplay,
looping,
]);

useEffect(() => {
Expand Down Expand Up @@ -397,6 +411,23 @@ const AudioViewer = ({
}
};

const toggleRepeat = (enabled: boolean) => {
if (waveform.current?.isReady) {
if (!waveform.current) return;

setLooping(enabled);
waveform.current.backend.media.loop = enabled;

Check failure on line 419 in src/components/shared/AudioViewer/index.tsx

View workflow job for this annotation

GitHub Actions / 🧱 Build Spotlight

Property 'media' does not exist on type 'WaveSurferBackend'.

Check failure on line 419 in src/components/shared/AudioViewer/index.tsx

View workflow job for this annotation

GitHub Actions / 🔍 Check

Property 'media' does not exist on type 'WaveSurferBackend'.
}
};

const toggleAutoPlay = (enabled: boolean) => {
if (waveform.current?.isReady) {
if (!waveform.current) return;
setAutoplay(enabled);
//waveform.current.backend.autoplay = true;//!waveform.current.autoplay;
}
};

const fitToScreen = () => {
if (!waveform.current?.isReady) return;

Expand Down Expand Up @@ -558,6 +589,12 @@ const AudioViewer = ({
)}
{/* eslint-disable-next-line jsx-a11y/media-has-caption */}
<audio ref={audioRef} src={url} />
<MenuBar
isLooping={looping}
shouldAutostart={autoplay}
onChangeLoop={toggleRepeat}
onChangeAutostart={toggleAutoPlay}
/>
</Container>
);
};
Expand Down

0 comments on commit ac81ccc

Please sign in to comment.