Browse Source

feat: support line break for subtitle (#396)

Fix subtitle bug
DIYgod 6 years ago
parent
commit
405817404d
1 changed files with 6 additions and 8 deletions
  1. 6 8
      src/js/subtitle.js

+ 6 - 8
src/js/subtitle.js

@@ -18,14 +18,12 @@ class Subtitle {
 
             track.oncuechange = () => {
                 const cue = track.activeCues[0];
+                this.container.innerHTML = '';
                 if (cue) {
-                    this.container.innerHTML = '';
-                    const p = document.createElement('p');
-                    p.appendChild(cue.getCueAsHTML());
-                    this.container.appendChild(p);
-                }
-                else {
-                    this.container.innerHTML = '';
+                    const template = document.createElement('div');
+                    template.appendChild(cue.getCueAsHTML());
+                    const trackHtml = template.innerHTML.split(/\r?\n/).map(item => `<p>${item}</p>`).join('');
+                    this.container.innerHTML = trackHtml;
                 }
                 this.events.trigger('subtitle_change');
             };
@@ -52,4 +50,4 @@ class Subtitle {
     }
 }
 
-export default Subtitle;
+export default Subtitle;