-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdialogs.mjs
185 lines (158 loc) · 6.13 KB
/
dialogs.mjs
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
import { renderVicinityInCanvas } from './render.mjs';
import { machineTime, parseTime } from './subtitles.mjs';
function baseDialog(contentText, populateFn) {
return new Promise((resolve) => {
const dialogEl = document.createElement('dialog');
dialogEl.setAttribute('open', '');
dialogEl.addEventListener('mousedown', (ev) => ev.stopPropagation());
const pEl = document.createElement('div');
pEl.appendChild( document.createTextNode(contentText) );
dialogEl.appendChild(pEl);
const onEnd = (result) => {
window.removeEventListener('keydown', onKeyDown);
dialogEl.close();
dialogEl.parentNode?.removeChild(dialogEl);
resolve(result);
}
const onKeyDown = (ev) => {
const key = ev.key;
if (key === 'Escape') onEnd('');
};
window.addEventListener('keydown', onKeyDown);
document.body.appendChild(dialogEl);
populateFn(dialogEl, onEnd);
const o = dialogEl.getBoundingClientRect();
dialogEl.style.marginLeft = `-${o.width / 2}px`;
dialogEl.style.marginTop = `-${o.height / 2}px`;
});
}
export function alertDialog(contentText, buttonLabels = ['ok']) {
return baseDialog(contentText, (dialogEl, onEnd) => {
let firstButtonEl;
for (const buttonLabel of buttonLabels) {
const buttonEl = document.createElement('button');
if (!firstButtonEl) firstButtonEl = buttonEl;
buttonEl.type = 'button';
buttonEl.appendChild( document.createTextNode(buttonLabel) );
buttonEl.addEventListener('click', () => onEnd(buttonLabel));
dialogEl.appendChild(buttonEl);
}
firstButtonEl?.focus();
});
}
function promptDialog(contentText, initialValue = '', dialogClass) {
return baseDialog(contentText, (dialogEl, onEnd) => {
const inputEl = document.createElement('input');
inputEl.type = 'text';
inputEl.style.width = '80dvw';
inputEl.value = initialValue;
dialogEl.appendChild(inputEl);
inputEl.addEventListener('keyup', () => {
inputEl.value = inputEl.value.substring(0, inputEl.value.length - 1);
}, {once: true});
inputEl.addEventListener('keydown', (ev) => {
if (ev.key !== 'Enter') return;
ev.preventDefault();
ev.stopPropagation();
onEnd(inputEl.value);
});
inputEl.focus();
});
}
export function editDialog(initialValue) {
return promptDialog('edit', initialValue);
}
export function joinDialog() {
return alertDialog('join', ['with previous', 'with next']);
}
export function splitDialog(ratios) {
return alertDialog('split', ratios.map(r => r.toFixed(1)));
}
export function tweakTimesDialog(subtitles, metadata, currentSubIndex, audio) {
return baseDialog('tweak subtitle timings', (dialogEl, onEnd) => {
const W = 800;
const H = 60;
const canvasEl = document.createElement('canvas');
canvasEl.setAttribute('width', W);
canvasEl.setAttribute('height', H);
dialogEl.appendChild(canvasEl);
const ctx = canvasEl.getContext('2d');
ctx.textAlign = 'center';
ctx.textBaseline = 'middle';
ctx.font = `9px 'Arial Narrow',sans-serif`;
const inputEls = [];
const sub3 = [undefined, undefined, undefined];
const references = [
[sub3[0], 'end'],
[sub3[1], 'start'],
[sub3[1], 'end'],
[sub3[2], 'start'],
];
const updateSubTrio = () => {
const s0 = subtitles[currentSubIndex - 1] || { content: 'STUB START', start: -1, end: 0 };
const s1 = subtitles[currentSubIndex ];
const s2 = subtitles[currentSubIndex + 1] || { content: 'STUB END', start: audio.duration, end: audio.duration + 1 };
sub3[0] = s0;
sub3[1] = s1;
sub3[2] = s1;
sub3[3] = s2;
references[0][0] = s0;
references[1][0] = s1;
references[2][0] = s1;
references[3][0] = s2;
}
updateSubTrio();
const deltaT = 4;
const refresh = () => {
renderVicinityInCanvas(ctx, [W, H], subtitles, metadata, audio.currentTime, deltaT);
};
refresh();
references.forEach(([s, attr]) => {
const inputEl = document.createElement('input');
inputEl.type = 'text';
inputEl.value = machineTime(s[attr]);
dialogEl.appendChild(inputEl);
inputEls.push(inputEl);
inputEl.addEventListener('change', () => {
s[attr] = parseTime(inputEl.value);
refresh();
});
});
const updateLabels = () => {
let i = 0;
for (const [s, attr] of references) {
const el = inputEls[i];
el.value = machineTime(s[attr]);
++i;
}
}
const onTimeUpdate = () => {
const t = audio.currentTime;
refresh();
const [_, s1, __] = sub3;
if (t < s1.start || t > s1.end) {
const newIndex = subtitles.findIndex((s) => s && t >= s.start && t <= s.end);
if (newIndex !== -1 && newIndex !== currentSubIndex) {
currentSubIndex = newIndex;
updateSubTrio();
updateLabels();
}
}
}
audio.addEventListener('timeupdate', onTimeUpdate);
for (const label of ['play', 'done']) {
const buttonEl = document.createElement('button');
buttonEl.appendChild(document.createTextNode(label));
dialogEl.appendChild(buttonEl);
buttonEl.addEventListener('click', () => {
if (label === 'play') {
if (audio.paused) audio.play();
else audio.pause();
} else {
audio.removeEventListener('timeupdate', onTimeUpdate);
onEnd();
}
});
}
});
}