bing-lyric.vue 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520
  1. <template>
  2. <view class="lyric-main" :style="{
  3. width:cuAreaStyle.width,
  4. height:cuAreaStyle.height,
  5. backgroundImage:cuAreaStyle.background}">
  6. <scroll-view id="lyric-show" class="lyric-show" scroll-with-animation="true" :scroll-y="true" :scroll-into-view="showLyricId" @scroll="lyricScroll"
  7. :style="{
  8. top:scrollView.top,
  9. width:cuAreaStyle.width,
  10. height:scrollView.height}">
  11. <view class="lrc-item" v-for="(v, i) in mLyrics.lrcs" :key="i" @tap="select(v,i)" @longpress="itemLongpress(v,i)"
  12. @touchstart="textTouchstart" @touchcancel="textTouchEnd" @touchend="textTouchEnd">
  13. <text class="check" v-if="selectStatus && selects[i] && v" value="[v,i]">✔</text>
  14. <text class="lrc" :id="'lrc-' + i"
  15. :style="{
  16. opacity:touchIndex == i && touchStatus && !selectStatus ? 0.8 : 1,
  17. color:((curLyricIndex == i || (touchIndex ==i && touchStatus)) && !selectStatus) ? cuLyricStyle.activeColor : cuLyricStyle.color,
  18. fontSize:((curLyricIndex == i || (touchIndex ==i && touchStatus))&& !selectStatus) ? cuLyricStyle.activeFontSize : cuLyricStyle.fontSize,
  19. height:((curLyricIndex == i || (touchIndex ==i && touchStatus))&& !selectStatus) ? cuLyricStyle.activeLineHeight : cuLyricStyle.lineHeight,
  20. backgroundColor:(selectStatus && selects[i] && v) ? cuLyricStyle.selectBGColor:'inherit'
  21. }">{{v}}</text>
  22. </view>
  23. </scroll-view>
  24. <view class="center-view" :style="{top:centerLineTop,display:showCenterView?'flex':'none',fontSize:cuCenterStyle.fontSize,height:cuCenterStyle.height}">
  25. <image v-if="cuCenterStyle.btnImg" class="center-btn" @click="centerBtnClick" :src="cuCenterStyle.btnImg" :style="{width:cuCenterStyle.height,height:cuCenterStyle.height}"></image>
  26. <view v-else class="center-btn" @click="centerBtnClick">{{cuCenterStyle.btnText}}</view>
  27. <view class="center-line" :style="{height:cuCenterStyle.lineHeight,backgroundColor:cuCenterStyle.color}"></view>
  28. <view class="center-time">{{centerTime}}</view>
  29. </view>
  30. <view class="selectControl" v-if="selectStatus" :style="{color:cuSelectControlStyle.color,backgroundColor:cuSelectControlStyle.backgroundColor,fontSize:cuSelectControlStyle.itemFontSize}">
  31. <view class="selectAll" @click="selectAll" :style="{borderRadius:cuSelectControlStyle.itemBorderRadius,backgroundColor:cuSelectControlStyle.itemBackgroundColor}">{{selectAllStatus ? '全不选' : '全选'}}</view>
  32. <view class="copy" @click="copyLyric" :style="{borderRadius:cuSelectControlStyle.itemBorderRadius,backgroundColor:cuSelectControlStyle.itemBackgroundColor}">复制歌词</view>
  33. <view class="cancel" @click="selectCancel" :style="{borderRadius:cuSelectControlStyle.itemBorderRadius,backgroundColor:cuSelectControlStyle.itemBackgroundColor}">取消</view>
  34. </view>
  35. </view>
  36. </template>
  37. <script>
  38. export default{
  39. data() {
  40. return {
  41. font2line4height: 2.5,
  42. selectAllStatus: false,
  43. selects: {}, // 选择状态下选中的歌词id
  44. scrollStatus: false, // 用于判断是否是在拖动歌词,是否应该开启选择歌词模式
  45. touchIndex: 0,
  46. curLyricId: 'lrc-0', // 时间进度控制
  47. showLyricId: 'lrc-0', // 界面显示,时间和touch控制
  48. curLyricIndex: 0,
  49. selectStatus: false,
  50. touchStatus: false,
  51. showCenterView: false,
  52. spaceLineNum: 0,
  53. timeout: {
  54. centerViewHide: null,
  55. goCenter: null,
  56. },
  57. cuSelectControlStyle: {
  58. height: '40px',
  59. backgroundColor: 'grey',
  60. itemFontSize: '16px',
  61. itemBorderRadius: '99px',
  62. itemBackgroundColor: '#00ffff'
  63. },
  64. cuLyricStyle: {
  65. color: "#000000",
  66. activeColor: '#ffffff',
  67. fontSize: '16px',
  68. activeFontSize: '16px',
  69. lineHeight: '40px',
  70. activeLineHeight: '40px',
  71. selectedBGColor: 'inherit'
  72. },
  73. cuAreaStyle: {
  74. width: '100vw',
  75. height: '70vh',
  76. background: 'linear-gradient(#8cc8b4, #ffaa7f, #8cc8b4)',
  77. },
  78. cuCenterStyle: {
  79. btnImg: '',
  80. btnText: 'btn',
  81. color: '#ffffff',
  82. lineHeight: '1px',
  83. height: '16px',
  84. fontSize: '14px',
  85. },
  86. scrollView: {
  87. height: '75%',
  88. top: 0
  89. },
  90. screen: {
  91. width:0,
  92. height:0,
  93. px2rpxscale: 1,
  94. },
  95. }
  96. },
  97. beforeMount() {
  98. const res = uni.getSystemInfoSync()
  99. this.screen.width = res.windowWidth
  100. this.screen.height = res.windowHeight
  101. this.screen.px2rpxscale = 750 / res.windowWidth;
  102. this.cuAreaStyle = Object.assign(this.cuAreaStyle, this.areaStyle)
  103. this.cuLyricStyle = Object.assign(this.cuLyricStyle, this.lyricStyle)
  104. this.cuCenterStyle = Object.assign(this.cuCenterStyle, this.centerStyle)
  105. this.cuSelectControlStyle = Object.assign(this.cuSelectControlStyle, this.selectControlStyle)
  106. if (this.lyricStyle){
  107. if(!('activeFontSize' in this.lyricStyle)){
  108. this.cuLyricStyle['activeFontSize'] = this.cuLyricStyle['fontSize']
  109. }
  110. if(!('lineHeight' in this.lyricStyle)){
  111. let s = this.sizeDeal(this.cuLyricStyle['fontSize'])
  112. this.cuLyricStyle['lineHeight'] = s[0] * this.font2line4height + s[1] // 默认行高为字体大小的倍数
  113. }
  114. if(!('activeLineHeight' in this.lyricStyle)){
  115. let s = this.sizeDeal(this.cuLyricStyle['activeFontSize'])
  116. this.cuLyricStyle['activeLineHeight'] = s[0] * this.font2line4height + s[1]
  117. }
  118. }
  119. if (this.centerStyle){
  120. if (!('height' in this.centerStyle)){
  121. let s = this.sizeDeal(this.cuCenterStyle['fontSize'])
  122. this.cuCenterStyle['height'] = s[0] * 1.2 + s[1]
  123. }
  124. }
  125. },
  126. mounted() {
  127. this.$nextTick(() => {
  128. const query = uni.createSelectorQuery().in(this)
  129. query.select(".lyric-main").boundingClientRect(res =>{
  130. this.cuAreaStyle.height = res.height + 'px'
  131. let size = this.sizeDeal(this.cuLyricStyle.lineHeight)
  132. let asize = this.sizeDeal(this.cuLyricStyle.activeLineHeight)
  133. let sumLine = Math.floor((res.height - asize[0]) / size[0]) // 不包含activeLine
  134. if (sumLine % 2 !== 0){
  135. sumLine -= 1
  136. }
  137. this.spaceLineNum = Math.floor(sumLine / 2)
  138. //this.scrollView.height = sumLine * size[0] + asize[0] + 'px'
  139. this.scrollView.top = (res.height - (sumLine * size[0] + asize[0])) / 2 + 'px'
  140. }).exec()
  141. })
  142. },
  143. props: {
  144. selectControlStyle: {
  145. default: () => {},
  146. type: Object
  147. },
  148. centerStyle:{
  149. default: () => {},
  150. type: Object
  151. },
  152. lyrics: {
  153. default: () => [],
  154. type: Array
  155. },
  156. curTime: {
  157. default: 0,
  158. type: [Number, String]
  159. },
  160. lyricStyle: {
  161. default: () => {},
  162. type: Object
  163. },
  164. areaStyle: {
  165. default: () => {},
  166. type: Object
  167. }
  168. },
  169. watch: {
  170. curTime(t) {
  171. let index = this.getIndex(t, this.mLyrics.times)
  172. this.curLyricIndex = index + this.spaceLineNum
  173. this.curLyricId = 'lrc-' + index
  174. if (!this.touchStatus && !this.selectStatus){
  175. this.showLyricId = this.curLyricId
  176. }
  177. },
  178. },
  179. computed: {
  180. lineHeightNum(){
  181. let size = this.sizeDeal(this.cuLyricStyle.fontSize)
  182. return size[0] * this.font2line4height
  183. },
  184. centerTime(){
  185. let index = this.touchIndex - this.spaceLineNum
  186. let s = this.mLyrics.times[index]
  187. return this.timeFormat(s)
  188. },
  189. centerLineTop(){
  190. let h = this.sizeDeal(this.cuAreaStyle.height)
  191. h = h[0] / 2 - this.sizeDeal(this.cuCenterStyle.height)[0] / 2 + h[1]
  192. return h
  193. },
  194. lineHeight() {
  195. let size = this.sizeDeal(this.cuLyricStyle.fontSize)
  196. return size[0] * 2 + size[1]
  197. },
  198. activeLineHeight() {
  199. let size = this.sizeDeal(this.cuLyricStyle.activeFontSize)
  200. return size[0] * 2 + size[1]
  201. },
  202. mLyrics () {
  203. return this.lrcDeal(this.lyrics)
  204. }
  205. },
  206. methods: {
  207. selectAll(){
  208. this.selectAllStatus = !this.selectAllStatus
  209. this.$nextTick(() => {
  210. if (this.selectAllStatus){
  211. for (let i=0;i<this.mLyrics.lrcs.length;i++){
  212. let lrc = this.mLyrics.lrcs[i]
  213. if(lrc){
  214. this.selects[i] =lrc
  215. }
  216. }
  217. }
  218. else{
  219. this.selects = {}
  220. }
  221. this.$forceUpdate()
  222. })
  223. },
  224. copyLyric(){
  225. let datas = {}
  226. for(let k in this.selects){
  227. datas[this.mLyrics.times[k - this.spaceLineNum]] = this.selects[k]
  228. }
  229. this.$emit('copyLyrics',{lyrics:datas})
  230. this.selectCancel()
  231. },
  232. selectCancel(){
  233. this.selectStatus = false
  234. this.touchStatus = false
  235. this.selectAllStatus = false
  236. this.selects = {}
  237. },
  238. itemLongpress(v,i){
  239. if (!this.scrollStatus && !this.selectStatus){
  240. this.selects = {}
  241. this.selectModel()
  242. if (v){
  243. if(this.selects[i]){
  244. delete this.selects[i]
  245. }
  246. else{
  247. this.selects[i] = v
  248. }
  249. }
  250. this.$forceUpdate()
  251. }
  252. },
  253. select(v,i){
  254. if(this.selects[i]){
  255. delete this.selects[i]
  256. }
  257. else{
  258. this.selects[i] = v
  259. }
  260. this.$forceUpdate()
  261. },
  262. selectModel() {
  263. this.showCenterView = false
  264. this.touchStatus = false
  265. this.selectStatus = true
  266. },
  267. centerBtnClick() {
  268. let lrc = this.mLyrics.lrcs[this.touchIndex]
  269. let ctime = Number(this.mLyrics.times[this.touchIndex - this.spaceLineNum])
  270. this.$emit('centerBtnClick',{centerTime:ctime,centerLyric:lrc})
  271. },
  272. textTouchstart() {
  273. clearTimeout(this.timeout.goCenter)
  274. clearTimeout(this.timeout.centerViewHide)
  275. this.touchStatus = true
  276. },
  277. textTouchEnd() {
  278. this.touchStatus = 2
  279. this.scrollStatus = false
  280. if (!this.selectStatus){
  281. this.timeout.goCenter = setTimeout(()=>{
  282. this.showLyricId = 'lrc-' + (this.touchIndex - this.spaceLineNum)
  283. },500)
  284. this.timeout.centerViewHide = setTimeout(this.clearTouch,5000)
  285. }
  286. },
  287. clearTouch() {
  288. this.touchStatus = false
  289. this.showLyricId = this.curLyricId
  290. this.showCenterView = false
  291. },
  292. lyricScroll(e) {
  293. let top = e.detail.scrollTop
  294. let topIndex = Math.round(top / this.lineHeightNum)
  295. this.touchIndex = topIndex + this.spaceLineNum
  296. if (this.touchStatus === true && !this.selectStatus){
  297. this.scrollStatus = true
  298. }
  299. if (this.touchStatus && !this.selectStatus && !this.showCenterView){
  300. this.showCenterView = true
  301. }
  302. this.$forceUpdate()
  303. },
  304. getIndex(t, items) {
  305. t = Number(t)
  306. let index = 0
  307. for ( var k_ in items){
  308. let k = Number(items[k_])
  309. if (t == k){
  310. return index
  311. }
  312. else if(t < k){
  313. return index - 1
  314. }
  315. else if ( index == items.length - 1){
  316. return items.length - 1
  317. }
  318. index += 1
  319. }
  320. },
  321. timeFormat(t){
  322. t = Number(t)
  323. if(t >= 0){
  324. let h = parseInt(t / 3600)
  325. let m = parseInt(t / 60) - h*60
  326. let s = parseInt(t) - m*60
  327. if (h.toString().length == 1){h = '0' + h}
  328. if (m.toString().length == 1){m = '0' + m}
  329. if (s.toString().length == 1){s = '0' + s}
  330. if (h != '00'){
  331. return h + ":" + m + ":" + s
  332. }
  333. else {
  334. return m + ":" + s
  335. }
  336. }
  337. else {
  338. return ''
  339. }
  340. },
  341. sizeDeal(size) {
  342. // 分离字体大小和单位,rpx 转 px
  343. let s = Number.isNaN(parseFloat(size)) ? 0 : parseFloat(size)
  344. let u = size.toString().replace(/[0-9\.]/g, '')
  345. if (u == 'rpx') {
  346. s /= this.screen.px2rpxscale
  347. u = 'px'
  348. } else if (u == '') {
  349. u = 'px'
  350. }else if (u == 'vw') {
  351. u = 'px'
  352. s = s / 100 * 750 / this.screen.px2rpxscale
  353. }
  354. return [s, u, s + u]
  355. },
  356. lrcDeal(lrcl) {
  357. let lrcj = {lrcs:[],times:[]}
  358. if (lrcl.length < 1){
  359. //没有歌词
  360. lrcj.lrcs.push('')
  361. }
  362. else{
  363. for (let i = 0; i < lrcl.length; i++ ){
  364. let lrc = lrcl[i].toString()
  365. let tl = lrc.split(']')
  366. if (tl.length > 1){
  367. // t: time; l: lyric
  368. let t_ = tl[0].replace('[','')
  369. let t = t_.split(':')
  370. if(t.length > 1){
  371. let treverse = t.reverse()
  372. let ts = 0
  373. if (treverse[0].indexOf('.') != -1){
  374. // 毫秒以小数形式放在了秒上面
  375. for (let j=0; j<treverse.length;j++){
  376. ts += Number(treverse[j]) * 60 ** (j)
  377. }
  378. }
  379. else{
  380. // 毫秒单独放置
  381. for (let j=0; j<treverse.length;j++){
  382. if (j == 0){
  383. ts += Number(treverse[j]) / 1000
  384. }
  385. else{
  386. ts += Number(treverse[j]) * 60 ** (j - 1)
  387. }
  388. }
  389. }
  390. ts = ts.toFixed(2)
  391. let l = tl.splice(1,1000).join(']').trim()
  392. if (l.length > 0){
  393. lrcj.times.push(ts)
  394. lrcj.lrcs.push(l)
  395. }
  396. }
  397. else{
  398. let l = tl.splice(1,1000).join(']').trim()
  399. if (l.length > 0){
  400. lrcj.times.push(Number(t_).toFixed(2))
  401. lrcj.lrcs.push(l)
  402. }
  403. }
  404. }
  405. }
  406. }
  407. for (let i=0;i<this.spaceLineNum;i++){
  408. lrcj.lrcs.push('')
  409. lrcj.lrcs.splice(0,0,'')
  410. }
  411. return lrcj
  412. },
  413. }
  414. }
  415. </script>
  416. <style>
  417. .lyric-main {
  418. position: relative;
  419. z-index: 10;
  420. background-repeat:no-repeat;
  421. background-size:100% 100%;
  422. -moz-background-size:100% 100%;
  423. }
  424. .lyric-show {
  425. position: absolute;
  426. overflow-anchor: none;
  427. }
  428. .lyric-show ::-webkit-scrollbar{
  429. display: none;
  430. width: 0 !important;
  431. height: 0 !important;
  432. -webkit-appearance: none;
  433. background: transparent;
  434. }
  435. .lyric-show text{
  436. display: flex;
  437. flex-direction: row;
  438. align-items: center;
  439. justify-content: space-around;
  440. text-align: center;
  441. white-space: nowrap;
  442. height: 32px;
  443. font-size: 16px;
  444. overflow: auto;
  445. }
  446. .center-view {
  447. position: absolute;
  448. display: flex;
  449. flex-direction: row;
  450. align-items: center;
  451. white-space: nowrap;
  452. width: 100%;
  453. opacity: 0.6;
  454. overflow: visible;
  455. pointer-events: none;
  456. }
  457. .center-btn{
  458. position: absolute;
  459. left: 0;
  460. width: 20%;
  461. text-align: left;
  462. padding: 0 5px;
  463. color: #ffffff;
  464. pointer-events: all;
  465. }
  466. .center-btn:active{
  467. opacity: 0.6;
  468. }
  469. .center-line {
  470. position: absolute;
  471. left: 20%;
  472. width: 60%;
  473. height: 1px;
  474. background-color: #ffffff;
  475. pointer-events: none;
  476. }
  477. .center-time{
  478. position: absolute;
  479. right: 0;
  480. width: 20%;
  481. text-align: right;
  482. padding: 0 5px;
  483. color: #ffffff;
  484. }
  485. .check{
  486. position: absolute;
  487. float: left;
  488. padding: 0 5px;
  489. opacity: 0.5;
  490. }
  491. .selectControl{
  492. position: absolute;
  493. top: 0;
  494. display: flex;
  495. flex-direction: row;
  496. align-items: center;
  497. justify-content: space-around;
  498. width: 100%;
  499. background-color: grey;
  500. }
  501. .selectControl view:active{
  502. opacity: 0.8;
  503. }
  504. .selectControl view{
  505. margin: 5px 0;
  506. padding: 5px 2px;
  507. width: 30%;
  508. overflow: hidden;
  509. white-space: nowrap;
  510. text-align: center;
  511. font-size: 16px;
  512. border-radius: 30px;
  513. background-color: grey;
  514. }
  515. </style>