Skip to content

Commit

Permalink
feat: added a boolean to modify the current previous song handler
Browse files Browse the repository at this point in the history
  • Loading branch information
aniketbiswas21 committed May 7, 2021
1 parent 7f4e19e commit 1b9fe0d
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 0 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,7 @@ ReactDOM.render(
| mobileMediaQuery | `string` | `(max-width: 768px) and (orientation : portrait)` | custom mobile media query string, eg use the mobile version UI on iPad. <https://developer.mozilla.org/en-US/docs/Web/CSS/Media_Queries/Using_media_queries> |
| volumeFade | `{ fadeIn: number(ms), fadeOut: number(ms) }` | `-` | audio fade in and out. [Detail](#bulb-audio-volume-fade-in-and-fade-out) |
| sortableOptions | `object` | `{swap: true, animation: 100, swapClass: 'audio-lists-panel-sortable-highlight-bg'}` | [SortableJs Options](https://github.com/SortableJS/Sortable#options) |
| restartCurrentOnPrev | `boolean` | `false` | Restarts the current track when trying to play previous song, if the current time of the song is more than 1 second |

## :bulb: Custom operation ui

Expand All @@ -222,6 +223,7 @@ Support feature:
- `play prev audio`
- `play audio by custom play index`
- `update play index`
- `restart current song on play prev audio`
- [SortableJS methods](https://github.com/SortableJS/Sortable#methods)

```jsx
Expand Down
2 changes: 2 additions & 0 deletions __tests__/tests/__snapshots__/locale.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ exports[`Locale test should render default locale with en_US 1`] = `
remember={false}
remove={true}
responsive={true}
restartCurrentOnPrev={false}
seeked={true}
showDestroy={true}
showDownload={true}
Expand Down Expand Up @@ -771,6 +772,7 @@ exports[`Locale test should render locale with zh_CN 1`] = `
remember={false}
remove={true}
responsive={true}
restartCurrentOnPrev={false}
seeked={true}
showDestroy={true}
showDownload={true}
Expand Down
15 changes: 15 additions & 0 deletions example/example.js
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,12 @@ const options = {
fadeIn: 1000,
fadeOut: 1000,
},
/**
* Restarts the current track when trying to play previous song, if the current time of the song is more than 1 second
Otherwise, plays the previous song in the list
[type `Boolean` default `false`]
*/
restartCurrentOnPrev: false,

// https://github.com/SortableJS/Sortable#options
sortableOptions: {},
Expand Down Expand Up @@ -989,6 +995,15 @@ class Demo extends React.PureComponent {
/>
quietUpdate
</label>
<label htmlFor="restartCurrentOnPrev">
<input
type="checkbox"
id="restartCurrentOnPrev"
checked={params.restartCurrentOnPrev}
onChange={() => this.onChangeKey('restartCurrentOnPrev')}
/>
restartCurrentOnPrev
</label>
<div className="toggle">
theme :{params.theme}
<Switch
Expand Down
1 change: 1 addition & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,7 @@ export interface ReactJkMusicPlayerProps {
fadeIn?: number
fadeOut?: number
}
restartCurrentOnPrev?: boolean
sortableOptions?: object
}

Expand Down
8 changes: 8 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,7 @@ export default class ReactJkMusicPlayer extends PureComponent {
fadeIn: 0,
fadeOut: 0,
},
restartCurrentOnPrev: false,
// https://github.com/SortableJS/Sortable#options
sortableOptions: {},
}
Expand Down Expand Up @@ -1475,6 +1476,7 @@ export default class ReactJkMusicPlayer extends PureComponent {
this.audio.pause()
return
}

this.audioListsPlay(
isNext
? audioLists[currentPlayIndex + 1][PLAYER_KEY]
Expand Down Expand Up @@ -1551,6 +1553,12 @@ export default class ReactJkMusicPlayer extends PureComponent {
}

onPlayPrevAudio = () => {
const { restartCurrentOnPrev } = this.props
if (restartCurrentOnPrev && this.audio.currentTime > 1) {
this.audio.currentTime = 0
return
}

this.audioPrevAndNextBasePlayHandle(false)
}

Expand Down

0 comments on commit 1b9fe0d

Please sign in to comment.