criação de novas patterns de composição, com indice correto, nos projetos

This commit is contained in:
JotaChina committed 2025-12-27 19:34:55 -03:00
1 parent 0383274a50
commit 86f64918a2
2 files changed
+40 -15

No files matched your search

+17 -14
View File
@@ -213,24 +213,27 @@ document.addEventListener("DOMContentLoaded", () => {
}
});
// --- Criar/Remover Pattern (toolbar) ---
const addPatternBtn = document.getElementById("add-pattern-btn");
const removePatternBtn = document.getElementById("remove-pattern-btn");
function getNextPatternIndex() {
const nonBass = (appState.pattern.tracks || []).filter(t => t.type !== "bassline");
const maxFromNonBass = nonBass.reduce(
(m, t) => Math.max(m, (t.patterns?.length || 0) - 1),
-1
);
const maxFromBass = (appState.pattern.tracks || [])
.filter(t => t.type === "bassline" && Number.isFinite(Number(t.patternIndex)))
.reduce((m, t) => Math.max(m, Number(t.patternIndex)), -1);
return Math.max(maxFromNonBass, maxFromBass) + 1;
}
addPatternBtn?.addEventListener("click", () => {
const refTrack = (appState.pattern.tracks || []).find(t => t.type !== "bassline");
const nextIndex = refTrack?.patterns?.length ?? 0;
const defaultName = `Pattern ${nextIndex + 1}`;
const idx = getNextPatternIndex();
const defaultName = `Pattern ${idx + 1}`;
const name = (prompt("Nome do novo pattern:", defaultName) || defaultName).trim();
// cria e já seleciona
sendAction({ type: "ADD_PATTERN", patternIndex: nextIndex, name, select: true });
});
// opcional (se quiser implementar remover depois)
removePatternBtn?.addEventListener("click", () => {
sendAction({ type: "REMOVE_LAST_PATTERN" });
sendAction({ type: "ADD_PATTERN", patternIndex: idx, name, select: true });
});
//Seleção de pattern