diff --git a/assets/js/creations/main.js b/assets/js/creations/main.js index 2d6080e2..8491cc0e 100755 --- a/assets/js/creations/main.js +++ b/assets/js/creations/main.js @@ -238,22 +238,26 @@ document.addEventListener("DOMContentLoaded", () => { // Adiciona o novo "ouvinte" de evento para o seletor de pattern globalPatternSelector?.addEventListener("change", () => { - // Pega o novo índice (ex: 0, 1, 2...) do seletor - const newPatternIndex = parseInt(globalPatternSelector.value, 10); + const raw = globalPatternSelector.value; - // --- CORREÇÃO DE LÓGICA (não precisamos mais do activeTrackId) --- - // A ação agora é global e afeta TODAS as tracks. - if (isNaN(newPatternIndex)) { - console.warn("Não é possível trocar pattern: índice inválido."); - return; - } + // ✅ limpar seleção + if (raw === "") { + sendAction({ type: "SET_ACTIVE_PATTERN", patternIndex: null }); + return; + } - // Envia a ação para todos (incluindo você) - sendAction({ - type: "SET_ACTIVE_PATTERN", - patternIndex: newPatternIndex, - }); + const newPatternIndex = parseInt(raw, 10); + if (Number.isNaN(newPatternIndex)) { + console.warn("Não é possível trocar pattern: índice inválido."); + return; + } + + sendAction({ + type: "SET_ACTIVE_PATTERN", + patternIndex: newPatternIndex, }); +}); + // ================================================================= // 👇 INÍCIO DA CORREÇÃO (Botão de Sincronia - Agora envia Ação) diff --git a/assets/js/creations/pattern/pattern_ui.js b/assets/js/creations/pattern/pattern_ui.js index 4ee7f495..16b8f1a8 100755 --- a/assets/js/creations/pattern/pattern_ui.js +++ b/assets/js/creations/pattern/pattern_ui.js @@ -348,7 +348,7 @@ export function updateGlobalPatternSelector() { const isFocused = !!appState.pattern.focusedBasslineId; - // track de referência que tenha patterns + // qualquer instrumento serve como referência de nomes/quantidade de patterns const referenceTrack = (appState.pattern.tracks || []).find( (t) => t.type !== "bassline" && Array.isArray(t.patterns) && t.patterns.length > 0 ); @@ -363,16 +363,12 @@ export function updateGlobalPatternSelector() { return; } - const hasSelection = Number.isInteger(appState.pattern.activePatternIndex); - - // ✅ placeholder só quando NÃO está focado e NÃO tem seleção - if (!isFocused && !hasSelection) { - const ph = document.createElement("option"); - ph.value = ""; - ph.textContent = "Selecione uma pattern"; - ph.disabled = true; - ph.selected = true; - sel.appendChild(ph); + // ✅ fora do foco: permite "limpar seleção" + if (!isFocused) { + const noneOpt = document.createElement("option"); + noneOpt.value = ""; + noneOpt.textContent = "Selecione uma pattern"; + sel.appendChild(noneOpt); } referenceTrack.patterns.forEach((p, idx) => { @@ -382,13 +378,14 @@ export function updateGlobalPatternSelector() { sel.appendChild(opt); }); + // Define seleção atual + const idx = appState.pattern.activePatternIndex; if (isFocused) { - // em foco: se não tiver índice ainda, cai pra 0 - sel.value = String(appState.pattern.activePatternIndex ?? 0); + sel.value = String(Number.isInteger(idx) ? idx : 0); } else { - // fora de foco: só seta valor se realmente existir seleção - sel.value = hasSelection ? String(appState.pattern.activePatternIndex) : ""; + sel.value = Number.isInteger(idx) ? String(idx) : ""; } sel.disabled = false; } + diff --git a/assets/js/creations/socket.js b/assets/js/creations/socket.js index 9be0a207..bb37776d 100755 --- a/assets/js/creations/socket.js +++ b/assets/js/creations/socket.js @@ -1356,6 +1356,15 @@ async function handleActionBroadcast(action) { // índice que veio do seletor global const { patternIndex } = action; + // ✅ limpar seleção + if (!Number.isInteger(patternIndex)) { + appState.pattern.activePatternIndex = null; + appState.pattern.tracks.forEach((t) => (t.activePatternIndex = null)); + renderAll(); + saveStateToSession(); + break; + } + // fonte de verdade global (muitos pontos da UI usam isso) appState.pattern.activePatternIndex = patternIndex;