Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion screen.html
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,16 @@ <h3 class="text-sm font-semibold text-gray-300">Editor</h3>

<!-- Syllable list -->
<div class="mb-3">
<div class="text-xs text-gray-500 mb-1"><span id="ls-editor-count">0</span> syllables</div>
<div class="flex items-center justify-between gap-3 mb-1">
<div class="text-xs text-gray-500"><span id="ls-editor-count">0</span> <span id="ls-editor-count-label">syllables</span></div>
<div class="flex items-center gap-1 text-xs text-gray-400">
View:
<button onclick="lsEditorSetListGrouping('word')" id="ls-editor-view-word-btn"
class="px-2 py-1 rounded bg-accent/30 text-accent">Words</button>
<button onclick="lsEditorSetListGrouping('line')" id="ls-editor-view-line-btn"
class="px-2 py-1 rounded bg-dark-700 hover:bg-dark-600">Lines</button>
</div>
</div>
<div id="ls-editor-list" class="bg-dark-900 border border-gray-800 rounded-lg max-h-64 overflow-y-auto divide-y divide-gray-800/60"></div>
</div>

Expand Down
143 changes: 121 additions & 22 deletions screen.js
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,7 @@
let _lsEditorWaveformDuration = 0;
let _lsEditorOffscreen = null; // waveform pre-rendered here; blitted every frame
let _lsEditorDragState = null; // {index, mode, startX, origT, origD, pointerId}
let _lsEditorListGrouping = 'word'; // 'word' shows raw entries; 'line' folds entries up to each brk marker
const _lsEditorEls = {}; // cached DOM refs, (re)resolved in _lsEditorCacheEls

function _lsEditorCloneSyllables(arr) {
Expand Down Expand Up @@ -594,6 +595,9 @@
_lsEditorEls.canvas = document.getElementById('ls-editor-canvas');
_lsEditorEls.seedText = document.getElementById('ls-editor-seed-text');
_lsEditorEls.count = document.getElementById('ls-editor-count');
_lsEditorEls.countLabel = document.getElementById('ls-editor-count-label');
_lsEditorEls.viewWordBtn = document.getElementById('ls-editor-view-word-btn');
_lsEditorEls.viewLineBtn = document.getElementById('ls-editor-view-line-btn');
_lsEditorEls.list = document.getElementById('ls-editor-list');
_lsEditorEls.inspector = document.getElementById('ls-editor-inspector');
_lsEditorEls.inspText = document.getElementById('ls-editor-insp-text');
Expand Down Expand Up @@ -833,56 +837,56 @@

// ── rAF draw loop — cached refs only, no DOM queries ────────────────────

function _lsEditorDrawFrame() {
const canvas = _lsEditorEls.canvas;
if (!canvas || !_lsEditorOffscreen) return;
const ctx = canvas.getContext('2d');
ctx.drawImage(_lsEditorOffscreen, 0, 0);

const height = canvas.height;
const pxPerSec = _lsEditorPxPerSec;
const wrap = _lsEditorEls.timelineWrap;
// Cull blocks outside the scrolled viewport (+padding) so a long song
// at a wide zoom doesn't pay for thousands of off-screen fillRect calls
// every frame.
const scrollLeft = wrap ? wrap.scrollLeft : 0;
const viewW = wrap ? wrap.clientWidth : canvas.width;
const visStart = Math.max(0, scrollLeft - 150);
const visEnd = scrollLeft + viewW + 150;

for (let i = 0; i < _lsEditorSyllables.length; i++) {
const syl = _lsEditorSyllables[i];
if (syl.t == null || syl.d == null) continue;
const x = syl.t * pxPerSec;
const w = Math.max(2, syl.d * pxPerSec);
if (x + w < visStart || x > visEnd) continue;

const selected = i === _lsEditorSelectedIndex;
ctx.fillStyle = selected ? 'rgba(232,192,64,0.55)' : 'rgba(64,128,224,0.35)';
ctx.strokeStyle = selected ? '#e8c040' : 'rgba(64,128,224,0.85)';
ctx.lineWidth = selected ? 2 : 1;
ctx.fillRect(x, 8, w, height - 16);
ctx.strokeRect(x, 8, w, height - 16);
if (w > 14) {
ctx.save();
ctx.beginPath();
ctx.rect(x, 0, w, height);
ctx.clip();
ctx.fillStyle = '#e5e7eb';
ctx.font = '11px monospace';
ctx.textBaseline = 'middle';
const label = syl.w + (syl.join ? '-' : syl.brk ? '+' : '');
ctx.fillText(label, x + 3, height / 2);
ctx.restore();
}
}

if (_lsEditorLoopA != null) _lsEditorDrawMarker(ctx, _lsEditorLoopA * pxPerSec, height, '#34d399');
if (_lsEditorLoopB != null) _lsEditorDrawMarker(ctx, _lsEditorLoopB * pxPerSec, height, '#34d399');

const audio = _lsEditorEls.audio;
if (audio) _lsEditorDrawMarker(ctx, (audio.currentTime || 0) * pxPerSec, height, '#ffffff');
}

Check notice on line 889 in screen.js

View check run for this annotation

codefactor.io / CodeFactor

screen.js#L840-L889

Complex Method

function _lsEditorDrawMarker(ctx, x, height, color) {
ctx.strokeStyle = color;
Expand Down Expand Up @@ -1181,42 +1185,137 @@

// ── List rendering ───────────────────────────────────────────────────────

function _lsEditorRenderList() {
const list = _lsEditorEls.list;
if (_lsEditorEls.count) _lsEditorEls.count.textContent = String(_lsEditorSyllables.length);
if (!list) return;
function lsEditorSetListGrouping(grouping) {
if (grouping !== 'word' && grouping !== 'line') return;
_lsEditorListGrouping = grouping;
_lsEditorRenderList();
}

// createElement + textContent, never interpolated markup — lyric text
// comes from pasted/uploaded input, not trusted for HTML context.
list.innerHTML = '';
_lsEditorSyllables.forEach((syl, i) => {
function _lsEditorUpdateGroupingButtons() {
const wordBtn = _lsEditorEls.viewWordBtn;
const lineBtn = _lsEditorEls.viewLineBtn;
if (!wordBtn || !lineBtn) return;
const active = 'px-2 py-1 rounded bg-accent/30 text-accent';
const inactive = 'px-2 py-1 rounded bg-dark-700 hover:bg-dark-600';
wordBtn.className = _lsEditorListGrouping === 'word' ? active : inactive;
lineBtn.className = _lsEditorListGrouping === 'line' ? active : inactive;
}

function _lsEditorLineRows() {
const rows = [];
let current = null;
_lsEditorSyllables.forEach((syl, index) => {
if (!current) current = { startIndex: index, items: [] };
current.items.push({ syl, index });
if (syl.brk) {
rows.push(current);
current = null;
}
});
if (current) rows.push(current);
return rows;
}

function _lsEditorLineText(items) {
let text = '';
items.forEach(({ syl }, i) => {
text += syl.w;
if (!syl.join && i < items.length - 1) text += ' ';
});
return text;
}

function _lsEditorAppendWordRow(list, syl, i) {
const row = document.createElement('div');
row.className = 'flex items-center gap-2 px-3 py-1.5 text-xs cursor-pointer hover:bg-dark-800/60 transition '
+ (i === _lsEditorSelectedIndex ? 'bg-accent/10' : '');
row.addEventListener('click', () => _lsEditorSelectSyllable(i));

const timeEl = document.createElement('span');
timeEl.className = 'text-gray-500 font-mono w-16 shrink-0';
timeEl.textContent = syl.t != null ? `${syl.t.toFixed(2)}s` : '-';

const wordEl = document.createElement('span');
wordEl.className = 'text-gray-200 flex-1 truncate';
wordEl.textContent = syl.w + (syl.join ? '-' : syl.brk ? '+' : '');

row.appendChild(timeEl);
row.appendChild(wordEl);

if (syl.t == null || syl.d == null) {
const untimedBadge = document.createElement('span');
untimedBadge.className = 'text-[10px] text-yellow-500 shrink-0';
untimedBadge.textContent = 'untimed';
row.appendChild(untimedBadge);
}

list.appendChild(row);
}

function _lsEditorRenderWordList(list) {
_lsEditorSyllables.forEach((syl, i) => _lsEditorAppendWordRow(list, syl, i));
}

function _lsEditorRenderLineList(list) {
const rows = _lsEditorLineRows();
rows.forEach((line) => {
const selected = line.items.some(({ index }) => index === _lsEditorSelectedIndex);
const firstTimed = line.items.find(({ syl }) => syl.t != null);
const untimedCount = line.items.filter(({ syl }) => syl.t == null || syl.d == null).length;
const row = document.createElement('div');
row.className = 'flex items-center gap-2 px-3 py-1.5 text-xs cursor-pointer hover:bg-dark-800/60 transition '
+ (i === _lsEditorSelectedIndex ? 'bg-accent/10' : '');
row.addEventListener('click', () => _lsEditorSelectSyllable(i));
row.className = 'flex items-start gap-2 px-3 py-2 text-xs cursor-pointer hover:bg-dark-800/60 transition '
+ (selected ? 'bg-accent/10' : '');
row.addEventListener('click', () => _lsEditorSelectSyllable(firstTimed ? firstTimed.index : line.startIndex));

const timeEl = document.createElement('span');
timeEl.className = 'text-gray-500 font-mono w-16 shrink-0';
timeEl.textContent = syl.t != null ? `${syl.t.toFixed(2)}s` : '—';
timeEl.className = 'text-gray-500 font-mono w-16 shrink-0 pt-0.5';
timeEl.textContent = firstTimed ? `${firstTimed.syl.t.toFixed(2)}s` : '-';

const wordEl = document.createElement('span');
wordEl.className = 'text-gray-200 flex-1 truncate';
wordEl.textContent = syl.w + (syl.join ? '-' : syl.brk ? '+' : '');
const textEl = document.createElement('span');
textEl.className = 'text-gray-200 flex-1 leading-5';
textEl.textContent = _lsEditorLineText(line.items);

row.appendChild(timeEl);
row.appendChild(wordEl);
const countEl = document.createElement('span');
countEl.className = 'text-[10px] text-gray-600 shrink-0 pt-0.5';
countEl.textContent = `${line.items.length}`;

if (syl.t == null || syl.d == null) {
row.appendChild(timeEl);
row.appendChild(textEl);
row.appendChild(countEl);
if (untimedCount > 0) {
const untimedBadge = document.createElement('span');
untimedBadge.className = 'text-[10px] text-yellow-500 shrink-0';
untimedBadge.textContent = 'untimed';
untimedBadge.className = 'text-[10px] text-yellow-500 shrink-0 pt-0.5';
untimedBadge.textContent = `${untimedCount} untimed`;
row.appendChild(untimedBadge);
}

list.appendChild(row);
});
}

function _lsEditorRenderList() {
const list = _lsEditorEls.list;
const lineCount = _lsEditorLineRows().length;
if (_lsEditorEls.count) {
_lsEditorEls.count.textContent = String(_lsEditorListGrouping === 'line' ? lineCount : _lsEditorSyllables.length);
}
if (_lsEditorEls.countLabel) {
if (_lsEditorListGrouping === 'line') {
_lsEditorEls.countLabel.textContent = `line${lineCount !== 1 ? 's' : ''}`;
} else {
const count = _lsEditorSyllables.length;
_lsEditorEls.countLabel.textContent = `syllable${count !== 1 ? 's' : ''}`;
}
}
_lsEditorUpdateGroupingButtons();
if (!list) return;

// createElement + textContent, never interpolated markup — lyric text
// comes from pasted/uploaded input, not trusted for HTML context.
list.innerHTML = '';
if (_lsEditorListGrouping === 'line') _lsEditorRenderLineList(list);
else _lsEditorRenderWordList(list);
}

function _lsEditorRenderAll() {
_lsEditorRenderList();
_lsEditorSyncInspector();
Expand Down
Loading