criação de novas patterns de composição, com indice correto, nos projetos
Deploy / Deploy (push) Successful in 2m1s Details

This commit is contained in:
JotaChina 2025-12-27 19:34:55 -03:00
parent 0dd79a0fca
commit 465f819833
2 changed files with 40 additions and 15 deletions

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

View File

@ -729,7 +729,29 @@ async function handleActionBroadcast(action) {
const nonBass = (appState.pattern.tracks || []).filter(t => t.type !== "bassline");
const currentCount = nonBass.reduce((m, t) => Math.max(m, t.patterns?.length ?? 0), 0);
const idx = desiredIndex != null ? desiredIndex : currentCount;
// Próximo índice livre (fim da lista)
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);
const nextFree = Math.max(maxFromNonBass, maxFromBass) + 1;
// tenta usar o índice pedido, mas só se ele ainda não existe
let idx = desiredIndex != null ? desiredIndex : nextFree;
const idxAlreadyExists =
idx <= maxFromNonBass ||
(appState.pattern.tracks || []).some(
t => t.type === "bassline" && Number(t.patternIndex) === idx
);
if (idxAlreadyExists) idx = nextFree;
const finalName = String(action.name || `Pattern ${idx + 1}`).trim();
// 1) cria patterns vazios em TODAS as tracks (respeita bars-input)