/** * External dependencies */ import type * as Tone from 'tone'; /** * WordPress dependencies */ import { __ } from '@wordpress/i18n'; import { useState } from '@wordpress/element'; import { Button, SelectControl, RangeControl, ToggleControl, Popover } from '@wordpress/components'; import { cog, help } from '@wordpress/icons'; /** * Internal dependencies */ import { INSTRUMENTS, DEFAULT_ENVELOPE, OCTAVE_OFFSETS, KEY_INDICATORS, MIN_VOLUME, MAX_VOLUME, } from '../../constants'; import { KEYBOARD_LAYOUTS } from '../../keyboard-layout'; import SynthesizerSetting from '../synthesizer-setting'; import HelpModal from '../help-modal'; import { getNormalizedVolume } from '../../utils'; import type { BlockAttributes } from '../../constants'; type Props = { settings: BlockAttributes; piano: Tone.Sampler | Tone.PolySynth | undefined; onChange: ( newAttributes: Partial< BlockAttributes > ) => void; }; const Controls = ( { settings, piano, onChange }: Props ) => { const { volume, useSustainPedal, octaveOffset, instrument, synthesizerSetting, keyLayout, keyIndicator, } = settings; const [ isHelpOpen, setIsHelpOpen ] = useState( false ); const [ isSynthesizerSettingOpen, setIsSynthesizerSettingOpen ] = useState( false ); const onVolumeChange = ( newVolume: number | undefined ) => { const instrumentSetting = INSTRUMENTS.find( ( { value } ) => value === instrument ); if ( ! instrumentSetting ) { return; } if ( piano ) { piano.volume.value = getNormalizedVolume( newVolume, settings ); } onChange( { volume: newVolume } ); }; const onOctaveOffsetChange = ( newOctaveOffset: string ) => { const allowedOctaveOffset = OCTAVE_OFFSETS.find( ( { value } ) => value.toString() === newOctaveOffset ); if ( ! allowedOctaveOffset ) { return; } onChange( { octaveOffset: allowedOctaveOffset.value } ); }; const onInstrumentChange = ( newInstrument: string ) => { const allowedInstrument = INSTRUMENTS.find( ( { value } ) => value === newInstrument ); if ( ! allowedInstrument ) { return; } onChange( { instrument: allowedInstrument.value } ); }; const onUseSustainPedalChange = () => { onChange( { useSustainPedal: ! useSustainPedal } ); }; const onKeyLayoutChange = ( newKeyLayout: string ) => { onChange( { keyLayout: newKeyLayout } ); }; const onKeyIndicatorChange = ( newKeyIndicator: string ) => { const allowedKeyIndicator = KEY_INDICATORS.find( ( { value } ) => value === newKeyIndicator ); if ( ! allowedKeyIndicator ) { return; } onChange( { keyIndicator: allowedKeyIndicator.value } ); }; const onSynthesizerSettingChange = ( newSynthesizerSetting: BlockAttributes[ 'synthesizerSetting' ] ) => { if ( instrument !== 'synthesizer' ) { return; } if ( piano ) { piano.set( { ...newSynthesizerSetting, envelope: newSynthesizerSetting.envelope || DEFAULT_ENVELOPE, } ); } onChange( { synthesizerSetting: newSynthesizerSetting } ); }; return (