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

Add SPN key indicator #21

Merged
merged 1 commit into from
Oct 17, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions piano-block.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ function piano_block_render_callback( $attributes ) {
$instrument = ! empty( $attributes['instrument'] ) ? $attributes['instrument'] : 'acoustic-piano';
$synthesizer_setting = ! empty( $attributes['synthesizerSetting'] ) ? $attributes['synthesizerSetting'] : array();
$key_layout = ! empty( $attributes['keyLayout'] ) ? $attributes['keyLayout'] : 'qwerty-1';
$key_indicator = ! empty( $attributes['keyIndicator'] ) ? $attributes['keyIndicator'] : 'key';

wp_enqueue_style( 'wp-components' );

Expand Down
3 changes: 3 additions & 0 deletions src/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@
},
"keyLayout": {
"type": "string"
},
"keyIndicator": {
"type": "string"
}
},
"supports": {
Expand Down
25 changes: 23 additions & 2 deletions src/components/controls/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import {
INSTRUMENTS,
DEFAULT_ENVELOPE,
OCTAVE_OFFSETS,
KEY_INDICATORS,
MIN_VOLUME,
MAX_VOLUME,
} from '../../constants';
Expand All @@ -42,8 +43,15 @@ type Props = {
};

const Controls = ( { settings, piano, onChange }: Props ) => {
const { volume, useSustainPedal, octaveOffset, instrument, synthesizerSetting, keyLayout } =
settings;
const {
volume,
useSustainPedal,
octaveOffset,
instrument,
synthesizerSetting,
keyLayout,
keyIndicator,
} = settings;
const [ isHelpOpen, setIsHelpOpen ] = useState< boolean >( false );
const [ isSynthesizerSettingOpen, setIsSynthesizerSettingOpen ] = useState< boolean >( false );

Expand Down Expand Up @@ -73,6 +81,10 @@ const Controls = ( { settings, piano, onChange }: Props ) => {
onChange( { keyLayout: newKeyLayout } );
};

const onKeyIndicatorChange = ( newKeyIndicator: string ) => {
onChange( { keyIndicator: newKeyIndicator } );
};

const onSynthesizerSettingChange = (
newSynthesizerSetting: BlockAttributes[ 'synthesizerSetting' ]
) => {
Expand Down Expand Up @@ -166,6 +178,15 @@ const Controls = ( { settings, piano, onChange }: Props ) => {
} ) }
onChange={ onKeyLayoutChange }
/>
<SelectControl
className="piano-block-controls__control"
label={ __( 'Key Indicator', 'piano-block' ) }
value={ keyIndicator }
options={ KEY_INDICATORS.map( ( { label, value } ) => {
return { label, value };
} ) }
onChange={ onKeyIndicatorChange }
/>
<Button
className="piano-block-controls__help"
label={ __( 'Help', 'piano-block' ) }
Expand Down
50 changes: 27 additions & 23 deletions src/components/keyboard/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,11 @@ import type { Key } from '../../constants';
type Props = {
activeKeys: Key[];
keyLayout: string;
keyIndicator: string;
onKeyClick: ( note: string, octave: number ) => void;
};

const Keyboard = ( { activeKeys, keyLayout, onKeyClick }: Props ) => {
const Keyboard = ( { activeKeys, keyLayout, keyIndicator, onKeyClick }: Props ) => {
const keys =
KEYBOARD_LAYOUTS.find( ( { value } ) => value === keyLayout )?.keys ||
KEYBOARD_LAYOUTS[ 0 ].keys;
Expand All @@ -33,28 +34,31 @@ const Keyboard = ( { activeKeys, keyLayout, onKeyClick }: Props ) => {
return (
<div className="piano-block-keyboard">
<div className="piano-block-keyboard__inner">
{ keys.map( ( key, index ) => (
<button
key={ index }
tabIndex={ -1 }
className={ classnames( 'piano-block-keyboard__key', {
'piano-block-keyboard__key--white': ! key.isBlackKey,
'piano-block-keyboard__key--black': key.isBlackKey,
'is-pressed': activeKeys.some(
( { note, octave } ) => key.note === note && key.octave === octave
),
} ) }
aria-label={ sprintf(
/* translators: %s is replaced with the key name. */
__( 'Note: %s', 'piano-block' ),
key.note + key.octave
) }
type="button"
onClick={ () => onClick( key.note, key.octave ) }
>
{ key.name.join( ' ' ) }
</button>
) ) }
{ keys.map( ( key, index ) => {
return (
<button
key={ index }
tabIndex={ -1 }
className={ classnames( 'piano-block-keyboard__key', {
'piano-block-keyboard__key--white': ! key.isBlackKey,
'piano-block-keyboard__key--black': key.isBlackKey,
'is-pressed': activeKeys.some(
( { note, octave } ) => key.note === note && key.octave === octave
),
} ) }
aria-label={ sprintf(
/* translators: %s is replaced with the key name. */
__( 'Note: %s', 'piano-block' ),
key.note + key.octave
) }
type="button"
onClick={ () => onClick( key.note, key.octave ) }
>
{ keyIndicator === 'key' && key.name.join( ' ' ) }
{ keyIndicator === 'spn' && `${ key.note }${ key.octave }` }
</button>
);
} ) }
</div>
</div>
);
Expand Down
2 changes: 1 addition & 1 deletion src/components/keyboard/style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
justify-content: center;
padding: 0;
padding-bottom: 8px;
font-size: 13px;
font-size: 12px;
font-weight: normal;
text-transform: uppercase;

Expand Down
12 changes: 10 additions & 2 deletions src/components/piano/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,15 @@ type Props = {

const Piano = ( { settings, onChange }: Props ) => {
const { assetsUrl } = window.pianoBlockVars;
const { volume, useSustainPedal, octaveOffset, instrument, synthesizerSetting, keyLayout } =
settings;
const {
volume,
useSustainPedal,
octaveOffset,
instrument,
synthesizerSetting,
keyLayout,
keyIndicator,
} = settings;
const [ piano, setPiano ] = useState< Tone.Sampler | Tone.PolySynth >();
const [ isReady, setIsReady ] = useState< boolean >( false );
const [ activeKeys, setActiveKeys ] = useState< Key[] >( [] );
Expand Down Expand Up @@ -190,6 +197,7 @@ const Piano = ( { settings, onChange }: Props ) => {
const keyboardProps = {
activeKeys,
keyLayout,
keyIndicator,
onKeyClick,
};

Expand Down
17 changes: 17 additions & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export interface BlockAttributes {
};
};
keyLayout: string;
keyIndicator: string;
}

export const MIN_VOLUME = -10 as const;
Expand Down Expand Up @@ -48,6 +49,7 @@ export const DEFAULT_SETTINGS = {
envelope: DEFAULT_ENVELOPE,
},
keyLayout: 'qwerty-1',
keyIndicator: 'key',
};

export const INSTRUMENTS = [
Expand Down Expand Up @@ -328,6 +330,21 @@ export const EMVELOPE_CONTROLS = [
},
] as const;

export const KEY_INDICATORS = [
{
label: __( 'Keyboard key', 'piano-block' ),
value: 'key',
},
{
label: __( 'SPN', 'piano-block' ),
value: 'spn',
},
{
label: __( 'None', 'piano-block' ),
value: '',
},
] as const;

export interface Instrument {
label: string;
value: ( typeof INSTRUMENTS )[ number ][ 'value' ];
Expand Down
4 changes: 4 additions & 0 deletions src/view.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ domReady( function () {
const octaveOffset = parseFloat( domNode.getAttribute( 'data-octave-offset' ) || '' );
const instrument = domNode.getAttribute( 'data-instrument' ) || DEFAULT_SETTINGS.instrument;
const keyLayout = domNode.getAttribute( 'data-key-layout' ) || DEFAULT_SETTINGS.keyLayout;
const keyIndicator =
domNode.getAttribute( 'data-key-indicator' ) || DEFAULT_SETTINGS.keyIndicator;

const synthesizerSetting = domNode.getAttribute( 'data-synthesizer-setting' );
const parsedSynthesizerSetting = synthesizerSetting
? JSON.parse( synthesizerSetting )
Expand All @@ -64,6 +67,7 @@ domReady( function () {
octaveOffset,
instrument,
keyLayout,
keyIndicator,
synthesizerSetting: normalizedSynthesizerSetting,
showOnFront: true,
};
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<!-- wp:piano-block/piano {"volume":-0.3,"octaveOffset":-1,"instrument":"synthesizer","useSustainPedal":false,"showOnFront":false,"synthesizerSetting":{"oscillator":{"type":"sawtooth"},"envelope":{"attack":0.4,"decay":1.1,"sustain":0.6,"release":1.6}},"keyLayout":"qwerty-2"} /-->
<!-- wp:piano-block/piano {"volume":-0.3,"octaveOffset":-1,"instrument":"synthesizer","useSustainPedal":false,"showOnFront":false,"synthesizerSetting":{"oscillator":{"type":"sawtooth"},"envelope":{"attack":0.4,"decay":1.1,"sustain":0.6,"release":1.6}},"keyLayout":"qwerty-2","keyIndicator":"spn"} /-->
6 changes: 6 additions & 0 deletions test/e2e/test.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,12 @@ test.describe( 'Block', () => {
await page.keyboard.press( 'Enter' );
await page.keyboard.press( 'ArrowDown' );
await page.keyboard.press( 'Enter' );
// Key Indicator
await page.keyboard.press( 'ArrowRight' );
await page.keyboard.press( 'Enter' );
await page.keyboard.press( 'ArrowDown' );
await page.keyboard.press( 'Enter' );

expect( await editor.getEditedPostContent() ).toMatchSnapshot();
} );

Expand Down
Loading