diff --git a/player/components/helper.js b/player/components/helper.js index f61fcac..7574eef 100644 --- a/player/components/helper.js +++ b/player/components/helper.js @@ -2,6 +2,7 @@ module.exports = class Helper { static validateTitle = title => { var ret = title; ret = ret.replace(/\.vtt/gi, ""); + ret = ret.replace(/\.srt/gi, ""); return ret.replace(/\-/gi, " "); }; @@ -24,6 +25,7 @@ module.exports = class Helper { let ret = path.replace(/[\\\/]/g, " > "); ret = ret.replace(/\.mp4/g, ""); ret = ret.replace(/\.vtt/g, ""); + ret = ret.replace(/\.srt/g, ""); return ret.replace(/[^A-Za-z0-9\.\>]/g, " "); }; diff --git a/player/components/tutorial_list.js b/player/components/tutorial_list.js index 9e4fca6..db77454 100644 --- a/player/components/tutorial_list.js +++ b/player/components/tutorial_list.js @@ -3,7 +3,11 @@ module.exports = class TutorialList { this.UI = document.getElementById("tutorialList"); this.selectorButton = document.getElementById("selectListBtn"); this.selectorButton.addEventListener("click", e => { - this.setDir(); + this.setDir(() => { + // load(); + alert("The list file was imported sucessfully!"); + location.reload(); + }); }); } @@ -15,7 +19,7 @@ module.exports = class TutorialList { var cached_list_dir = Helper.getListDir(); if (cached_list_dir) { await Helper.loadScript(cached_list_dir + "/list_mp4.js"); - await Helper.loadScript(cached_list_dir + "/list_vtt.js"); + await Helper.loadScript(cached_list_dir + "/list_subtitle.js"); await this.loadOptionTags(); } @@ -39,7 +43,7 @@ module.exports = class TutorialList { } }; - setDir = function() { + setDir = function(cb) { // const win = require('electron').remote.getCurrentWindow(); // win.setFullScreen(true); // return; @@ -53,7 +57,7 @@ module.exports = class TutorialList { if (list_dir) { Helper.setConf("list_dir", list_dir); } - load(); + cb(); }; clearHtml = function() { @@ -63,7 +67,7 @@ module.exports = class TutorialList { loadOptionTags = () => { this.clearHtml(); - let temp_vtt_file_dir = ""; + let temp_subtitle_dir = ""; let optionNode; optionNode = document.createElement("option"); @@ -71,27 +75,31 @@ module.exports = class TutorialList { optionNode.innerHTML = "Select a video to play"; this.UI.appendChild(optionNode); - for (var k in vtt_files) { - var vtt_file = vtt_files[k]; - var vtt_file_dir = vtt_file.split(/\//gi)[0]; + for (var k in subtitles) { + var subtitle = subtitles[k]; + var subtitle_dir = subtitle.split(/\//gi)[0]; - if (vtt_file.indexOf("/") > -1 && vtt_file_dir != temp_vtt_file_dir) { - optionNode = document.createElement("optgroup"); - optionNode.label = Helper.validateTitle(vtt_file_dir); - this.UI.appendChild(optionNode); - } + var optionText = ""; - optionNode = document.createElement("option"); - optionNode.value = k; - if (vtt_file.indexOf("/") > -1){ - optionNode.innerHTML = Helper.validateTitle( - vtt_file.replace(vtt_file_dir + "/", "   ") + if (subtitle.indexOf("/") > -1) { + if (subtitle_dir != temp_subtitle_dir) { + optionNode = document.createElement("optgroup"); + optionNode.label = Helper.validateTitle(subtitle_dir); + this.UI.appendChild(optionNode); + } + optionText = Helper.validateTitle( + subtitle.replace(subtitle_dir + "/", "   ") ); } else { - optionNode.innerHTML = vtt_file; + optionText = subtitle.substr(0, subtitle.length - 4); } + + optionNode = document.createElement("option"); + optionNode.innerHTML = optionText; + + optionNode.value = k; this.UI.appendChild(optionNode); - temp_vtt_file_dir = vtt_file_dir; + temp_subtitle_dir = subtitle_dir; } this.UI.appendChild(optionNode); diff --git a/utils/create_tutorial_list.js b/utils/create_tutorial_list.js index b7cc927..d753d63 100644 --- a/utils/create_tutorial_list.js +++ b/utils/create_tutorial_list.js @@ -19,44 +19,38 @@ function naturalSort(myArray) { return myArray.sort(collator.compare); } -function validateVTT(mp4_files, vtt_files) { - var new_vtt_files = []; - for( var i = 0; i < mp4_files.length; i++ ) { - var mp4_file = mp4_files[i]; - var vtt_file = mp4_file.substr(0, mp4_file.length - 4) + ".vtt"; - var srt_file = mp4_file.substr(0, mp4_file.length - 4) + ".srt"; - if (vtt_files.indexOf(vtt_file) > -1) { - new_vtt_files.push(vtt_file); - } else if (vtt_files.indexOf(srt_file) > -1) { - new_vtt_files.push(srt_file); - } else { - new_vtt_files.push(vtt_file); - } - } - return new_vtt_files; +function generateSubtitle(mp4_files, prefix) { + var subtitle_files = []; + mp4_files.map(mp4_file => { + subtitle_files.push(mp4_file.substr(0, mp4_file.length - 4) + "." + prefix); + }); + return subtitle_files; } module.exports = (dirPath, cb) => { let mp4_files = glob.sync(path.join(dirPath, "/**/*.mp4")); let vtt_files = glob.sync(path.join(dirPath, "/**/*.vtt")); let srt_files = glob.sync(path.join(dirPath, "/**/*.srt")); + let subtitle_type; - if ( vtt_files.length < srt_files.length ) { - vtt_files = srt_files; - } - - mp4_files = naturalSort(mp4_files); - - vtt_files = validateVTT(mp4_files, vtt_files); + // Subtitle files are written in srt or vtt? + if (vtt_files.length < srt_files.length) { + subtitle_type = "SRT"; + } else { + subtitle_type = "VTT"; + } - const newDirPath = dirPath.replace(/\\/g, "/"); - writeListFile(mp4_files, "mp4_files", "list_mp4.js", newDirPath); + mp4_files = naturalSort(mp4_files); - if (vtt_files.length > 0) { - writeListFile(vtt_files, "vtt_files", "list_vtt.js", newDirPath); + if (subtitle_type === "SRT") { + vtt_files = generateSubtitle(mp4_files, "srt"); } else { - writeListFile([], "vtt_files", "list_vtt.js", newDirPath); + vtt_files = generateSubtitle(mp4_files, "vtt"); } + const newDirPath = dirPath.replace(/\\/g, "/"); + writeListFile(mp4_files, "mp4_files", "list_mp4.js", newDirPath); + writeListFile(vtt_files, "subtitles", "list_subtitle.js", newDirPath); + cb({ result: true }); };