forked from magic-akari/lrc-utils
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
217 lines (217 loc) · 6.52 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
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
import { compressTags } from "./compress-tags.js";
import { parser, stringify } from "./lrc-parser.js";
const $ = (arg) => document.querySelector(arg);
const back = $(".back");
const input = $("#input");
const output = $("#output");
const overwrite = $("#overwrite");
const actions = $(".actions");
const timeTransform = $("#time-transform");
const timeTransformDialog = $(".time-transform.dialog");
const splitTranslation = $("#split-translation");
const splitTranslationDialog = $(".split-translation.dialog");
const regexInput = $("#regex");
const lyricText = localStorage.getItem("lrc-maker-lyric") || "";
const { fixed = 3, spaceStart = 1, spaceEnd = 0, themeColor = "#f58ea8", } = JSON.parse(localStorage.getItem("lrc-maker-preferences") || "{}");
const trimOptions = {
trimStart: spaceStart >= 0,
trimEnd: spaceEnd >= 0,
};
const formatOptions = {
fixed,
spaceStart,
spaceEnd,
};
const state = {
lrc: parser(lyricText, trimOptions),
};
back.addEventListener("transitionend", () => {
history.back();
});
input.addEventListener("blur", (ev) => {
const el = ev.target;
state.lrc = parser(el.value, trimOptions);
});
actions.addEventListener("click", (ev) => {
const el = ev.target;
const action = el.dataset["action"];
if (action === undefined) {
return;
}
const lrc = state.lrc;
switch (action) {
case "compressTags": {
output.value = compressTags(lrc, formatOptions);
return;
}
case "removeTags": {
const info = new Map();
const lyric = lrc.lyric.map((line) => {
return {
text: line.text,
};
});
output.value = stringify({ info, lyric }, formatOptions);
return;
}
case "removeEmpty": {
const info = lrc.info;
const lyric = lrc.lyric.filter((line) => line.text.trim().length > 0);
output.value = stringify({ info, lyric }, formatOptions);
return;
}
case "changeOffset": {
timeTransformDialog.open = true;
return;
}
case "splitTranslation": {
splitTranslationDialog.open = true;
return;
}
case "lrcOverwrite": {
overwriteState.value = !overwriteState.value;
overwrite.focus();
return;
}
}
});
timeTransform.addEventListener("input", (ev) => {
const el = ev.target;
if (el.validity.badInput || el.validity.valueMissing) {
return;
}
const scale = {
a: 1,
c: 0,
};
if (el.name === "offset") {
scale.c = Number.parseInt(el.value) / -1000;
}
if (el.name === "transformA" || el.name === "transformC") {
const form = ev.currentTarget;
const transformA = form.elements.namedItem("transformA");
const transformC = form.elements.namedItem("transformC");
scale.a = Number.parseFloat(transformA.value);
scale.c = Number.parseFloat(transformC.value);
}
if (Number.isNaN(scale.a)) {
scale.a = 1;
}
if (Number.isNaN(scale.c)) {
scale.c = 0;
}
const lrc = state.lrc;
const info = lrc.info;
const lyric = lrc.lyric.map((line) => {
if (line.time !== undefined) {
return {
...line,
time: scale.a * line.time + scale.c,
};
}
return line;
});
output.value = stringify({ info, lyric }, formatOptions);
});
timeTransform.addEventListener("submit", (ev) => {
ev.preventDefault();
timeTransformDialog.open = false;
return false;
});
splitTranslation.addEventListener("change", (ev) => {
const el = ev.target;
if (el.id !== "regex") {
regexInput.value = el.value;
}
const lrc = state.lrc;
try {
const regex = new RegExp(el.value);
const info = lrc.info;
const [lyric1, lyric2] = lrc.lyric.reduce((p, line) => {
const result = line.text.match(regex);
if (result !== null && result.length >= 3) {
p[0].push({ ...line, text: result[1] });
p[1].push({ ...line, text: result[2] });
}
else {
p[0].push(line);
p[1].push(line);
}
return p;
}, [[], []]);
output.value = [lyric1, lyric2]
.map((lyric) => {
return stringify({ info, lyric }, formatOptions);
})
.join("\r\n".repeat(3));
}
catch (error) { }
});
splitTranslation.addEventListener("submit", (ev) => {
ev.preventDefault();
splitTranslationDialog.open = false;
return false;
});
const lrcOverWriteText = {
get value() {
return sessionStorage.getItem("lrc-maker-overwrite") || "";
},
set value(v) {
sessionStorage.setItem("lrc-maker-overwrite", v);
},
};
overwrite.value = lrcOverWriteText.value;
let isOverwrite = false;
const timePadding = $(".overwrite-time-padding");
timePadding.innerText = stringify({
info: new Map(),
lyric: [
{
time: 0,
text: "",
},
],
}, formatOptions);
overwrite.style.paddingLeft = `${timePadding.getBoundingClientRect().width + 10}px`;
const overwriteState = {
state: {},
get value() {
return isOverwrite;
},
set value(v) {
isOverwrite = v;
if (v) {
document.body.classList.add("overwrite-mode");
this.state = parser(input.value, trimOptions);
input.value = stringify(this.state, formatOptions);
}
else {
document.body.classList.remove("overwrite-mode");
}
},
render(text) {
const lyric = text
.split("\n")
.slice(this.state.info.size)
.map((text, i) => {
var _a;
return { time: (_a = this.state.lyric[i]) === null || _a === void 0 ? void 0 : _a.time, text };
});
const newState = { info: this.state.info, lyric };
return stringify(newState, formatOptions);
},
};
overwrite.addEventListener("input", () => {
lrcOverWriteText.value = overwrite.value;
output.value = overwriteState.render(overwrite.value);
});
overwrite.addEventListener("scroll", () => {
output.scrollTop = input.scrollTop = overwrite.scrollTop;
output.scrollLeft = input.scrollLeft = overwrite.scrollLeft;
});
const main = () => {
input.value = lyricText;
document.documentElement.style.setProperty("--theme-color", themeColor);
};
main();
//# sourceMappingURL=index.js.map