-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheditor.js
97 lines (76 loc) · 2.59 KB
/
editor.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
let selectedFile = null;
$('#chords-file-editor').change(function (event) {
selectedFile = event.target.files[0];
});
$('#add-empty-line-button').click(function () {
if (!selectedFile) {
return;
}
const reader = new FileReader();
reader.onload = function (e) {
const lineNumber = $('#line-number').val();
if (lineNumber === '') {
return;
}
insertEmptyLineAtIndex(e.target.result, lineNumber);
};
reader.onerror = function () {
console.error("Error reading file");
};
reader.readAsText(selectedFile);
});
function insertEmptyLineAtIndex(fileContent, index) {
const inputFileContentArray = fileContent.split(/\r?\n/);
let afterIndex = false;
let elementRowIndex = 0;
const outputFileContentArray = [];
let lastInterval = '';
inputFileContentArray.forEach(function (line) {
if (!line.startsWith('#')) {
elementRowIndex++
}
if (line === index) {
afterIndex = true;
outputFileContentArray.push(index);
const lastIntervalEnd = lastInterval.split(' --> ')[1];
const updatedInterval = lastIntervalEnd + ' --> ' + lastIntervalEnd;
outputFileContentArray.push(updatedInterval);
outputFileContentArray.push('');
outputFileContentArray.push('');
outputFileContentArray.push('');
}
let updatedLine = line;
switch (elementRowIndex) {
case 1:
if (afterIndex) {
updatedLine = String(Number(line) + 1);
}
break;
case 2:
lastInterval = line;
break;
case 3:
case 4:
break;
default:
elementRowIndex = 0;
}
outputFileContentArray.push(updatedLine);
});
const filename = getFilenameWithoutExtension(selectedFile.name) + ' (new).srt';
exportToSrtFile(filename, outputFileContentArray)
}
function exportToSrtFile(filename, fileContentArray) {
const fileContent = fileContentArray.join('\n');
const blob = new Blob([fileContent], { type: 'text/plain' });
const link = document.createElement('a');
link.href = URL.createObjectURL(blob);
link.download = filename;
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
URL.revokeObjectURL(link.href);
}
function getFilenameWithoutExtension(filename) {
return filename.substring(0, filename.lastIndexOf('.')) || filename;
}