From 465f81983384eda68aca0f4bde1bfc35fc33de07 Mon Sep 17 00:00:00 2001 From: JotaChina Date: Sat, 27 Dec 2025 19:34:55 -0300 Subject: [PATCH] =?UTF-8?q?cria=C3=A7=C3=A3o=20de=20novas=20patterns=20de?= =?UTF-8?q?=20composi=C3=A7=C3=A3o,=20com=20indice=20correto,=20nos=20proj?= =?UTF-8?q?etos?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- assets/js/creations/main.js | 31 +++++++++++++++++-------------- assets/js/creations/socket.js | 24 +++++++++++++++++++++++- 2 files changed, 40 insertions(+), 15 deletions(-) diff --git a/assets/js/creations/main.js b/assets/js/creations/main.js index 0e5c1d58..264a9ecb 100755 --- a/assets/js/creations/main.js +++ b/assets/js/creations/main.js @@ -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 diff --git a/assets/js/creations/socket.js b/assets/js/creations/socket.js index 27738305..9be0a207 100755 --- a/assets/js/creations/socket.js +++ b/assets/js/creations/socket.js @@ -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)