criação de novas patterns de composição, com indice correto, nos projetos
Deploy / Deploy (push) Successful in 2m1s
Details
Deploy / Deploy (push) Successful in 2m1s
Details
This commit is contained in:
parent
0dd79a0fca
commit
465f819833
|
|
@ -213,24 +213,27 @@ document.addEventListener("DOMContentLoaded", () => {
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
// --- Criar/Remover Pattern (toolbar) ---
|
function getNextPatternIndex() {
|
||||||
const addPatternBtn = document.getElementById("add-pattern-btn");
|
const nonBass = (appState.pattern.tracks || []).filter(t => t.type !== "bassline");
|
||||||
const removePatternBtn = document.getElementById("remove-pattern-btn");
|
|
||||||
|
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", () => {
|
addPatternBtn?.addEventListener("click", () => {
|
||||||
const refTrack = (appState.pattern.tracks || []).find(t => t.type !== "bassline");
|
const idx = getNextPatternIndex();
|
||||||
const nextIndex = refTrack?.patterns?.length ?? 0;
|
const defaultName = `Pattern ${idx + 1}`;
|
||||||
|
|
||||||
const defaultName = `Pattern ${nextIndex + 1}`;
|
|
||||||
const name = (prompt("Nome do novo pattern:", defaultName) || defaultName).trim();
|
const name = (prompt("Nome do novo pattern:", defaultName) || defaultName).trim();
|
||||||
|
|
||||||
// cria e já seleciona
|
sendAction({ type: "ADD_PATTERN", patternIndex: idx, name, select: true });
|
||||||
sendAction({ type: "ADD_PATTERN", patternIndex: nextIndex, name, select: true });
|
|
||||||
});
|
|
||||||
|
|
||||||
// opcional (se quiser implementar remover depois)
|
|
||||||
removePatternBtn?.addEventListener("click", () => {
|
|
||||||
sendAction({ type: "REMOVE_LAST_PATTERN" });
|
|
||||||
});
|
});
|
||||||
|
|
||||||
//Seleção de pattern
|
//Seleção de pattern
|
||||||
|
|
|
||||||
|
|
@ -729,7 +729,29 @@ async function handleActionBroadcast(action) {
|
||||||
const nonBass = (appState.pattern.tracks || []).filter(t => t.type !== "bassline");
|
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 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();
|
const finalName = String(action.name || `Pattern ${idx + 1}`).trim();
|
||||||
|
|
||||||
// 1) cria patterns vazios em TODAS as tracks (respeita bars-input)
|
// 1) cria patterns vazios em TODAS as tracks (respeita bars-input)
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue