Skip to content

Commit

Permalink
Added a feature for drag&drop
Browse files Browse the repository at this point in the history
  • Loading branch information
railsjack committed Oct 27, 2019
1 parent dcb67c5 commit 30bfba4
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 47 deletions.
2 changes: 2 additions & 0 deletions player/components/helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -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, " ");
};

Expand All @@ -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, " ");
};

Expand Down
48 changes: 28 additions & 20 deletions player/components/tutorial_list.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
});
}

Expand All @@ -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();
}
Expand All @@ -39,7 +43,7 @@ module.exports = class TutorialList {
}
};

setDir = function() {
setDir = function(cb) {
// const win = require('electron').remote.getCurrentWindow();
// win.setFullScreen(true);
// return;
Expand All @@ -53,7 +57,7 @@ module.exports = class TutorialList {
if (list_dir) {
Helper.setConf("list_dir", list_dir);
}
load();
cb();
};

clearHtml = function() {
Expand All @@ -63,35 +67,39 @@ module.exports = class TutorialList {
loadOptionTags = () => {
this.clearHtml();

let temp_vtt_file_dir = "";
let temp_subtitle_dir = "";
let optionNode;

optionNode = document.createElement("option");
optionNode.value = "";
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);
Expand Down
48 changes: 21 additions & 27 deletions utils/create_tutorial_list.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 });
};

0 comments on commit 30bfba4

Please sign in to comment.