lyrics.ts 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  1. import * as level from '../level';
  2. import * as util from '../util';
  3. export class LyricsEditor {
  4. containerElement: HTMLElement;
  5. firstLine: LineEditor;
  6. lastLine: LineEditor;
  7. constructor(readonly playAt: (time: number, duration?: number) => void) {
  8. this.containerElement = util.getElement(document, '.lyrics');
  9. this.firstLine = new LineEditor(this);
  10. this.lastLine = this.firstLine;
  11. this.appendLine();
  12. }
  13. *[Symbol.iterator]() {
  14. let line: LineEditor | undefined = this.firstLine;
  15. while (line !== undefined) {
  16. yield line;
  17. line = line.nextLine;
  18. }
  19. }
  20. addInterval(time: number): void {
  21. const fixedTime = time.toFixed(2);
  22. for (const line of this) {
  23. const input = line.timeInput;
  24. if (input.value === '') {
  25. input.value = fixedTime;
  26. line.adjustTimeInput();
  27. return;
  28. } else {
  29. const value = parseFloat(input.value);
  30. if (time < value) {
  31. line.pushText('time', fixedTime);
  32. return;
  33. }
  34. }
  35. }
  36. const newLine = this.appendLine();
  37. newLine.timeInput.value = fixedTime;
  38. newLine.adjustTimeInput();
  39. }
  40. appendLine(): LineEditor {
  41. const newLine = new LineEditor(this);
  42. this.lastLine.setNextLine(newLine);
  43. this.toggleLine(this.lastLine, false);
  44. this.toggleLine(newLine, true);
  45. this.lastLine = newLine;
  46. return newLine;
  47. }
  48. removeLine(line: LineEditor): boolean {
  49. if (line === this.firstLine) {
  50. if (line.nextLine === undefined) {
  51. // this should not happen
  52. return false;
  53. } else if (line.nextLine === this.lastLine) {
  54. // this is the only editable line, so we just clear it
  55. line.clear();
  56. return false;
  57. } else {
  58. this.firstLine = line.nextLine;
  59. }
  60. } else if (line === this.lastLine) {
  61. if (line.previousLine === this.firstLine) {
  62. line.clear();
  63. return false;
  64. } else {
  65. this.lastLine = line.previousLine!;
  66. }
  67. }
  68. this.toggleLine(this.lastLine, true);
  69. if (line.previousLine !== undefined) {
  70. line.previousLine.setNextLine(line.nextLine);
  71. }
  72. line.elements.forEach((element) => {
  73. element.remove();
  74. });
  75. return true;
  76. }
  77. toggleLine(line: LineEditor, disabled: boolean): void {
  78. line.elements.forEach((element) => {
  79. if (element instanceof HTMLInputElement && element.type === 'text') {
  80. element.disabled = disabled;
  81. }
  82. });
  83. }
  84. clear(): void {
  85. while (this.removeLine(this.lastLine)) {}
  86. // we always keep one extra line so we have to clear the first line manually
  87. this.removeLine(this.firstLine);
  88. }
  89. getDisplayForTime(time: number): string | null {
  90. for (const line of this) {
  91. if (time < parseFloat(line.timeInput.value)) {
  92. return line.previousLine?.kanjiInput.value ?? null;
  93. }
  94. }
  95. return null;
  96. }
  97. loadLines(lines: level.Line[]): void {
  98. this.clear();
  99. if (lines.length > 0) {
  100. const firstLine = lines[0];
  101. let start = 0;
  102. if (
  103. firstLine !== undefined &&
  104. firstLine.kana === '@' &&
  105. firstLine.kanji === '@'
  106. ) {
  107. start = 1;
  108. }
  109. for (let i = start; i < lines.length; ++i) {
  110. if (i > start) {
  111. this.appendLine();
  112. }
  113. this.lastLine.previousLine!.fromLine(lines[i]);
  114. if (i === lines.length - 1) {
  115. this.lastLine.timeInput.value = `${lines[i].end ?? ''}`;
  116. this.lastLine.adjustTimeInput();
  117. }
  118. }
  119. }
  120. }
  121. toLines(): level.Line[] {
  122. const lines: level.Line[] = [];
  123. for (const lineEditor of this) {
  124. const line = lineEditor.toLine();
  125. if (lineEditor === this.firstLine && line.start !== undefined) {
  126. lines.push({
  127. kana: '@',
  128. kanji: '@',
  129. start: 0,
  130. end: line.start,
  131. });
  132. }
  133. if (lineEditor !== this.lastLine) {
  134. lines.push(line);
  135. }
  136. }
  137. return lines;
  138. }
  139. }
  140. type Part = 'time' | 'kana' | 'kanji';
  141. export class LineEditor {
  142. previousLine?: LineEditor;
  143. nextLine?: LineEditor;
  144. elements: Element[];
  145. timeInput: HTMLInputElement;
  146. kanaInput: HTMLInputElement;
  147. kanjiInput: HTMLInputElement;
  148. constructor(readonly container: LyricsEditor, before?: Element) {
  149. const fragment = util.loadTemplate(document, 'line');
  150. this.elements = Array.from(fragment.children);
  151. this.timeInput = util.getElement(fragment, '.time');
  152. this.kanaInput = util.getElement(fragment, '.kana');
  153. this.kanjiInput = util.getElement(fragment, '.kanji');
  154. this.timeInput.addEventListener('input', () => {
  155. this.adjustTimeInput();
  156. });
  157. this.kanaInput.addEventListener('keydown', (event) => {
  158. this.handleKeyDown('kana', event);
  159. });
  160. this.kanaInput.addEventListener('paste', (event) => {
  161. this.handlePaste('kana', event);
  162. });
  163. this.kanjiInput.addEventListener('keydown', (event) => {
  164. this.handleKeyDown('kanji', event);
  165. });
  166. this.kanjiInput.addEventListener('paste', (event) => {
  167. this.handlePaste('kanji', event);
  168. });
  169. util.getElement(fragment, '.play-section').addEventListener('click', () => {
  170. const line = this.toLine();
  171. if (line.start !== undefined) {
  172. const duration =
  173. line.end !== undefined ? line.end - line.start : undefined;
  174. this.container.playAt(line.start, duration);
  175. }
  176. });
  177. util
  178. .getElement(fragment, '.remove-section')
  179. .addEventListener('click', () => {
  180. this.popText('time');
  181. });
  182. if (before) {
  183. container.containerElement.insertBefore(fragment, before);
  184. } else {
  185. container.containerElement.appendChild(fragment);
  186. }
  187. }
  188. getInput(part: Part): HTMLInputElement {
  189. switch (part) {
  190. case 'time':
  191. return this.timeInput;
  192. case 'kana':
  193. return this.kanaInput;
  194. case 'kanji':
  195. return this.kanjiInput;
  196. }
  197. }
  198. // adjust min value such that we validate and step adds/subtracts based on
  199. // our input value
  200. adjustTimeInput(): void {
  201. if (this.timeInput.value !== '') {
  202. const value = parseFloat(this.timeInput.value);
  203. const step = parseFloat(this.timeInput.step);
  204. this.timeInput.min = (value % step).toFixed(3);
  205. }
  206. }
  207. handlePaste(part: Part, event: ClipboardEvent): void {
  208. if (event.clipboardData !== null) {
  209. event.preventDefault();
  210. const paste = event.clipboardData.getData('text');
  211. const lines = paste.split('\n');
  212. if (lines.length === 0) {
  213. return;
  214. }
  215. const input = this.getInput(part);
  216. const currentText = input.value;
  217. for (let i = lines.length - 1; i > 0; --i) {
  218. this.pushText(part, lines[i]);
  219. }
  220. this.pushText(part, currentText + lines[0]);
  221. }
  222. }
  223. handleKeyDown(part: Part, event: KeyboardEvent): void {
  224. if (event.key === 'ArrowUp' && this.previousLine !== undefined) {
  225. event.preventDefault();
  226. const input = this.getInput(part);
  227. const position = input.selectionStart ?? 0;
  228. const prevInput = this.previousLine.getInput(part);
  229. prevInput.focus();
  230. prevInput.setSelectionRange(position, position);
  231. } else if (event.key === 'ArrowDown' && this.nextLine !== undefined) {
  232. event.preventDefault();
  233. const input = this.getInput(part);
  234. const position = input.selectionStart ?? 0;
  235. const nextInput = this.nextLine.getInput(part);
  236. nextInput.focus();
  237. nextInput.setSelectionRange(position, position);
  238. } else if (event.key === 'Enter') {
  239. const input = this.getInput(part);
  240. const text = input.value;
  241. if (input.selectionStart !== null && input.selectionEnd !== null) {
  242. const remaining = text.substring(0, input.selectionStart);
  243. const afterNewline = text.substring(input.selectionEnd);
  244. input.value = remaining;
  245. const nextLine = this.ensureNextLine(part);
  246. nextLine.pushText(part, afterNewline);
  247. const nextInput = nextLine.getInput(part);
  248. nextInput.focus();
  249. nextInput.setSelectionRange(0, 0);
  250. }
  251. } else if (event.key === 'Backspace' && this.previousLine !== undefined) {
  252. const input = this.getInput(part);
  253. if (input.selectionStart === 0 && input.selectionEnd === 0) {
  254. event.preventDefault();
  255. const prevInput = this.previousLine.getInput(part);
  256. const prevText = prevInput.value;
  257. const prevLength = prevText.length;
  258. prevInput.value = prevText + input.value;
  259. this.popText(part);
  260. prevInput.focus();
  261. prevInput.setSelectionRange(prevLength, prevLength);
  262. }
  263. } else if (event.key === 'Delete' && this.nextLine !== undefined) {
  264. const input = this.getInput(part);
  265. const length = input.value.length;
  266. if (input.selectionStart === length && input.selectionEnd === length) {
  267. event.preventDefault();
  268. const nextInput = this.nextLine.getInput(part);
  269. input.value = input.value + nextInput.value;
  270. input.setSelectionRange(length, length);
  271. this.nextLine.popText(part);
  272. }
  273. }
  274. }
  275. pushText(part: Part, text: string): void {
  276. const input = this.getInput(part);
  277. const current = input.value;
  278. input.value = text;
  279. if (part === 'time') {
  280. this.adjustTimeInput();
  281. }
  282. const isLastLine = part === 'time'
  283. ? this.nextLine === undefined
  284. : this.nextLine === this.container.lastLine;
  285. if (current === '' && isLastLine) {
  286. return;
  287. }
  288. const nextLine = this.ensureNextLine(part);
  289. nextLine.pushText(part, current);
  290. }
  291. popText(part: Part): void {
  292. const input = this.getInput(part);
  293. if (this.nextLine === undefined) {
  294. // we are the last line
  295. if (part === 'time' && this.previousLine !== undefined) {
  296. const { kanaInput, kanjiInput } = this.previousLine;
  297. if (kanaInput.value === '' && kanjiInput.value === '') {
  298. this.container.removeLine(this);
  299. } else {
  300. input.value = '';
  301. }
  302. } else {
  303. input.value = '';
  304. }
  305. } else {
  306. if (this.nextLine.nextLine === undefined) {
  307. // the next line is the last one
  308. input.value = '';
  309. if (
  310. this.kanaInput.value === '' &&
  311. this.kanjiInput.value === '' &&
  312. this.nextLine.timeInput.value === ''
  313. ) {
  314. this.container.removeLine(this.nextLine);
  315. } else {
  316. input.value = this.nextLine.getInput(part).value;
  317. this.nextLine.popText(part);
  318. }
  319. } else {
  320. input.value = this.nextLine.getInput(part).value;
  321. this.nextLine.popText(part);
  322. }
  323. }
  324. if (part === 'time') {
  325. this.adjustTimeInput();
  326. }
  327. }
  328. ensureNextLine(part: Part): LineEditor {
  329. if (part !== 'time' && this.nextLine === this.container.lastLine) {
  330. this.container.appendLine();
  331. return this.nextLine;
  332. } else if (this.nextLine === undefined) {
  333. return this.container.appendLine();
  334. } else {
  335. return this.nextLine;
  336. }
  337. }
  338. setNextLine(nextLine?: LineEditor): void {
  339. this.nextLine = nextLine;
  340. if (this.nextLine !== undefined) {
  341. this.nextLine.previousLine = this;
  342. }
  343. }
  344. clear() {
  345. this.timeInput.value = '';
  346. this.kanaInput.value = '';
  347. this.kanjiInput.value = '';
  348. }
  349. fromLine(line: level.Line) {
  350. this.kanaInput.value = line.kana === '@' ? '' : line.kana;
  351. this.kanjiInput.value = line.kanji === '@' ? '' : line.kanji;
  352. if (line.start !== undefined) {
  353. this.timeInput.value = `${line.start}`;
  354. this.adjustTimeInput();
  355. }
  356. }
  357. toLine(): level.Line {
  358. const line: level.Line = {
  359. kana: this.kanaInput.value || '@',
  360. kanji: this.kanjiInput.value || '@',
  361. };
  362. const time = this.timeInput.value;
  363. if (time !== '') {
  364. const nextValue = this.nextLine?.timeInput?.value;
  365. line.start = parseFloat(time);
  366. if (nextValue) {
  367. line.end = parseFloat(nextValue);
  368. }
  369. }
  370. return line;
  371. }
  372. get time(): number {
  373. return parseFloat(this.timeInput.value);
  374. }
  375. }