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

Added the ability to change player skin #60

Merged
merged 9 commits into from
Oct 14, 2022
Merged
Show file tree
Hide file tree
Changes from 3 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
75 changes: 63 additions & 12 deletions src/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@ import { concat } from 'lodash';
import { __ } from '@wordpress/i18n';
import { compose } from '@wordpress/compose';
import { createBlock } from '@wordpress/blocks';
import { PanelBody, TextControl, withNotices } from '@wordpress/components';
import { PanelBody, TextControl, withNotices, ToggleControl } from '@wordpress/components';
import {
useBlockProps,
InspectorControls,
store as blockEditorStore,
MediaPlaceholder,
} from '@wordpress/block-editor';
import { useSelect, useDispatch } from '@wordpress/data';
import { Platform, useMemo } from '@wordpress/element';
import { Platform, useMemo, useState } from '@wordpress/element';
import { createBlobURL } from '@wordpress/blob';

/**
Expand Down Expand Up @@ -77,6 +77,15 @@ function Edit( props ) {
[ clientId ]
);

const [ useCustomUrl, setUseCustomUrl ] = useState( false );
Copy link
Member

@Sidsector9 Sidsector9 Oct 11, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for working on this!

Because the hook is called after a return, that makes it conditional and violates the usage rules of hooks.
Can you move useState to before line 58?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Sidsector9 Makes sense. done. Thanks!


const defaultSkins = [
'https://skins.webamp.org/skin/5e4f10275dcb1fb211d4a8b4f1bda236/base-2.91.wsz/',
'https://skins.webamp.org/skin/cd251187a5e6ff54ce938d26f1f2de02/Winamp3_Classified_v5.5.wsz/',
'https://skins.webamp.org/skin/6856045909b0040283b43c53a002de69/Blue_Fade.wsz/',
'https://skins.webamp.org/skin/a5b4d732f406dd30164ef88358044f03/Samsara-amp.wsz/',
];

// eslint-disable-next-line react-hooks/rules-of-hooks
const audio = useMemo(
() =>
Expand Down Expand Up @@ -201,20 +210,62 @@ function Edit( props ) {
return <div { ...blockProps }>{ mediaPlaceholder }</div>;
}

const skinPreviewUrl = ( skin ) => {
if ( skin ) {
const match = skin.match(
/(?:https?:)?(?:\/\/)?skins\.webamp\.org\/skin\/(\w+)\/(?:.*)?/
);

if ( match && match.length === 2 ) {
return `https://cdn.webampskins.org/screenshots/${ match[ 1 ] }.png`;
}
}
}

return (
<>
<InspectorControls>
<PanelBody title={ __( 'Winamp Player Skin', 'winamp-block' ) }>
<TextControl
label={ __( 'Skin URL', 'winamp-block' ) }
help={ __(
'The URL of the player skin to use. Find skins at https://skins.webamp.org/.',
'winamp-block'
) }
value={ currentSkin }
onChange={ ( skin ) =>
setAttributes( { currentSkin: skin } )
}
{ useCustomUrl ? (
<TextControl
label={ __( 'Skin URL', 'winamp-block' ) }
help={ __(
'The URL of the player skin to use. Find skins at https://skins.webamp.org/.',
'winamp-block'
) }
value={ currentSkin }
onChange={ ( skin ) =>
setAttributes( { currentSkin: skin } )
}
/>
)
:
(
<div className="preview-wrapper">
{ defaultSkins.length && defaultSkins.map( ( skin, index ) => {
return (
<label key={ index } className="winamp-radio-wrapper">
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would be good to add htmlFor to associate with the following input element.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Sidsector9 Done as well.

<input
type="radio"
name="current-skin"
value={ skin }
checked={ currentSkin === skin }
onChange={ ( e ) =>
setAttributes( { currentSkin: e.target.value } )
}
/>
<img src={ skinPreviewUrl( skin ) } width="100" alt="winamp-skin" />
</label>
)
} )
}
</div>
)
}
<ToggleControl
label={ __( 'Use Custom Skin?', 'winamp-block' ) }
checked={ useCustomUrl }
onChange={ () => setUseCustomUrl( ! useCustomUrl ) }
/>
</PanelBody>
</InspectorControls>
Expand Down
22 changes: 21 additions & 1 deletion src/editor.scss
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,26 @@
* Replace them with your own styles or remove the file completely.
*/

.wp-block-tenup-winamp-block {
.wp-block-tenup-winamp-block {
border: 1px dotted #f00;
}

.preview-wrapper {
margin-bottom: 15px;

.winamp-radio-wrapper [type="radio"] {
position: absolute;
opacity: 0;
width: 0;
height: 0;

+ img {
cursor: pointer;
margin: 0 2px;
}

&:checked + img {
outline: 2px solid var(--wp-admin-theme-color);
}
}
}