소스 검색

fix(subtitles): improve multisubtitle change operation

ngyyuusora 3 년 전
부모
커밋
b974065ea4
1개의 변경된 파일33개의 추가작업 그리고 5개의 파일을 삭제
  1. 33 5
      src/js/subtitles.js

+ 33 - 5
src/js/subtitles.js

@@ -9,17 +9,45 @@ class Subtitles {
             this.show();
         });
 
-        for (let i = 0; i < this.player.template.subtitlesItem.length; i++) {
+        const lastItemIndex = this.player.template.subtitlesItem.length - 1;
+        for (let i = 0; i < lastItemIndex; i++) {
             this.player.template.subtitlesItem[i].addEventListener('click', () => {
                 this.hide();
+                if (this.player.options.subtitle.index !== i) {
+                    // clear subtitle show for new subtitle don't have now duration time. If don't, will display last subtitle.
+                    this.player.template.subtitle.innerHTML = `<p></p>`;
+                    // update video track src
+                    this.player.template.subtrack.src = this.player.template.subtitlesItem[i].dataset.subtitle;
+                    // update options current subindex for reload (such as changeQuality)
+                    this.player.options.subtitle.index = i;
+                    if (this.player.template.subtitle.classList.contains('dplayer-subtitle-hide')) {
+                        this.subContainerShow();
+                    }
+                }
+            });
+        }
+        this.player.template.subtitlesItem[lastItemIndex].addEventListener('click', () => {
+            this.hide();
+            if (this.player.options.subtitle.index !== lastItemIndex) {
                 // clear subtitle show for new subtitle don't have now duration time. If don't, will display last subtitle.
                 this.player.template.subtitle.innerHTML = `<p></p>`;
                 // update video track src
-                this.player.template.subtrack.src = this.player.template.subtitlesItem[i].dataset.subtitle;
+                this.player.template.subtrack.src = '';
                 // update options current subindex for reload (such as changeQuality)
-                this.player.options.subtitle.index = i;
-            });
-        }
+                this.player.options.subtitle.index = lastItemIndex;
+                this.subContainerHide();
+            }
+        });
+    }
+
+    subContainerShow() {
+        this.player.template.subtitle.classList.remove('dplayer-subtitle-hide');
+        this.player.events.trigger('subtitle_show');
+    }
+
+    subContainerHide() {
+        this.player.template.subtitle.classList.add('dplayer-subtitle-hide');
+        this.player.events.trigger('subtitle_hide');
     }
 
     hide() {