-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
58 lines (49 loc) · 1.43 KB
/
index.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
var read = require('./lib/readSegments');
var write = require('./lib/writeSegments');
var adjust = require('./lib/adjust');
function cp(obj) { return JSON.parse(JSON.stringify(obj)); }
module.exports = function readString(vtt) {
var data = read(vtt);
// Representation of cues that will be exposed via API.
var visibleCues = calculateVisibleCues();
function calculateVisibleCues() {
return data.cues.map(function(cue, idx) {
return {
id: idx,
start: cue.start,
end: cue.end,
text: cue.rest.join('\n')
};
});
}
function move(cueId, newStartMs) {
if (typeof cueId == 'object')
cueId = cueId.id;
var anchor = cp(data.cues[cueId]);
anchor.start = newStartMs;
adjust.move(data.cues, anchor);
visibleCues = calculateVisibleCues();
}
function moveAndScale(cueId1, newStartMs1, cueId2, newStartMs2) {
if (typeof cueId1 == 'object')
cueId1 = cueId1.id;
if (typeof cueId2 == 'object')
cueId2 = cueId2.id;
var anchor = cp(data.cues[cueId1]);
anchor.start = newStartMs1;
var scaleRef = cp(data.cues[cueId2]);
scaleRef.start = newStartMs2;
adjust.moveAndScale(data.cues, anchor, scaleRef);
visibleCues = calculateVisibleCues();
}
function toString() {
return write(data);
}
var adjuster = {
cues: visibleCues,
move: move,
moveAndScale: moveAndScale,
toString: toString
};
return adjuster;
}