Better lyrics handling

This commit is contained in:
An Phan 2016-03-06 15:44:38 +08:00
parent 29e5eea323
commit 70a34401f6
4 changed files with 27 additions and 16 deletions

View file

@ -233,6 +233,9 @@ class Song extends Model
*/
public function getLyricsAttribute($value)
{
return str_replace("\n\n", "\n", str_replace("\r", "\n", $value));
// We don't use nl2br() here, because the function actually preserve linebreaks -
// it just _appends_ a "<br />" after each of them. This would case our client
// implementation of br2nl fails with duplicated linebreaks.
return str_replace(["\r\n", "\r", "\n"], '<br />', $value);
}
}

View file

@ -21,8 +21,4 @@
<style lang="sass">
@import "resources/assets/sass/partials/_vars.scss";
@import "resources/assets/sass/partials/_mixins.scss";
.content > div {
white-space: pre-wrap;
}
</style>

View file

@ -66,6 +66,7 @@
import _ from 'lodash';
import $ from 'jquery';
import utils from '../../services/utils';
import artistStore from '../../stores/artist';
import albumStore from '../../stores/album';
import songStore from '../../stores/song';
@ -196,10 +197,10 @@
songStore.getInfo(this.songs[0], () => {
this.loading = false;
this.formData.lyrics = this.songs[0].lyrics;
this.formData.lyrics = utils.br2nl(this.songs[0].lyrics);
});
} else {
this.formData.lyrics = this.songs[0].lyrics;
this.formData.lyrics = utils.br2nl(this.songs[0].lyrics);
}
} else {
this.formData.albumName = this.inSameAlbum ? this.songs[0].album.name : '';

View file

@ -7,7 +7,7 @@ export default {
*/
secondsToHis(d) {
d = parseInt(d);
var s = d%60;
if (s < 10) {
@ -31,9 +31,9 @@ export default {
/**
* Parse the validation error from the server into a flattened array of messages.
*
*
* @param {Object} error The error object in JSON format.
*
*
* @return {Array.<String>}
*/
parseValidationError(error) {
@ -42,7 +42,7 @@ export default {
/**
* Check if AudioContext is supported by the current browser.
*
*
* @return {Boolean}
*/
isAudioContextSupported() {
@ -51,11 +51,11 @@ export default {
return false;
}
var AudioContext = (window.AudioContext ||
window.webkitAudioContext ||
window.mozAudioContext ||
window.oAudioContext ||
window.msAudioContext);
var AudioContext = (window.AudioContext ||
window.webkitAudioContext ||
window.mozAudioContext ||
window.oAudioContext ||
window.msAudioContext);
if (!AudioContext) {
return false;
@ -69,4 +69,15 @@ export default {
return true;
},
/**
* Turn <br> into new line characters.
*
* @param {string} str
*
* @return {string}
*/
br2nl(str) {
return str.replace(/<br\s*[\/]?>/gi, "\n");
}
};