diff --git a/src/renderer/views/MacroEditor.tsx b/src/renderer/views/MacroEditor.tsx index 6fad7f91a..5d6f9648d 100644 --- a/src/renderer/views/MacroEditor.tsx +++ b/src/renderer/views/MacroEditor.tsx @@ -1,5 +1,3 @@ -/* eslint-disable react/jsx-no-bind */ -/* eslint-disable no-console */ /* eslint-disable no-bitwise */ /* Bazecor -- Kaleidoscope Command Center * Copyright (C) 2018, 2019 Keyboardio, Inc. @@ -217,7 +215,7 @@ function MacroEditor(props: MacroEditorProps) { return mapped; }; - function macrosMap(macros: MacrosType[]) { + const macrosMap = (macros: MacrosType[]) => { const { macrosEraser } = state; log.info( "Macros map function", @@ -257,9 +255,9 @@ function MacroEditor(props: MacroEditorProps) { .join(" "); log.info("MACROS GOING TO BE SAVED", result); return result; - } + }; - function addToActions(actions: MacroActionsType[]) { + const addToActions = (actions: MacroActionsType[]) => { const { startContext } = props; const { macros, selectedMacro } = state; @@ -269,10 +267,10 @@ function MacroEditor(props: MacroEditorProps) { state.modified = true; setState({ ...state }); startContext(); - } + }; // Define updateActions function to update the actions of the selected macro - function updateActions(actions: MacroActionsType[]) { + const updateActions = (actions: MacroActionsType[]) => { const { startContext } = props; const { macros, selectedMacro, modified } = state; @@ -287,9 +285,9 @@ function MacroEditor(props: MacroEditorProps) { state.macros = macrosList; setState({ ...state }); } - } + }; - function saveName(data: string) { + const saveName = (data: string) => { const { startContext } = props; const { macros, selectedMacro } = state; const localMacros = [...macros]; @@ -298,28 +296,28 @@ function MacroEditor(props: MacroEditorProps) { state.modified = true; setState({ ...state }); startContext(); - } + }; - function updateScroll(scrollPos: number) { + const updateScroll = (scrollPos: number) => { state.scrollPos = scrollPos; setState({ ...state }); - } + }; - function changeSelected(id: number) { + const changeSelected = (id: number) => { state.selectedMacro = id < 0 ? 0 : id; setState({ ...state }); - } + }; - function updateMacros(recievedMacros: MacrosType[]) { + const updateMacros = (recievedMacros: MacrosType[]) => { const { startContext } = props; state.macros = recievedMacros; state.modified = true; state.usedMemory = recievedMacros.map(m => m.actions).flat().length; setState({ ...state }); startContext(); - } + }; - function duplicateMacro() { + const duplicateMacro = () => { const { macros, maxMacros, selectedMacro } = state; if (macros.length >= maxMacros) { return; @@ -331,9 +329,9 @@ function MacroEditor(props: MacroEditorProps) { macros.push(aux); updateMacros(macros); changeSelected(aux.id); - } + }; - async function writeMacros() { + const writeMacros = async () => { const { macros, neurons, neuronIdx, keymap, superkeys } = state; const { setLoading, cancelContext } = props; const { currentDevice } = deviceState; @@ -371,15 +369,16 @@ function MacroEditor(props: MacroEditorProps) { setLoading(false); setIsSaving(false); } - } + }; - function toggleDeleteModal() { + const toggleDeleteModal = () => { state.showDeleteModal = false; setState({ ...state }); - } + }; - function ActUponDelete() { - const { selectedList, listToDelete, listToDeleteS, listToDeleteM, keymap, superkeys } = state; + const ActUponDelete = () => { + const { startContext } = props; + const { selectedList, selectedMacro, listToDelete, listToDeleteS, listToDeleteM, keymap, superkeys } = state; let { macros } = state; log.info("Checking list to delete macros", listToDeleteM, macros); for (let i = 0; i < listToDelete.length; i += 1) { @@ -409,25 +408,33 @@ function MacroEditor(props: MacroEditorProps) { macros[listToDeleteM[i].macroIdx].actions[listToDeleteM[i].pos].keyCode = listToDeleteM[i].newKey + 53852; } } + macros.splice(selectedMacro, 1); macros = macros.map(macro => { const newMacro = { ...macro }; newMacro.actions = macro.actions.filter(x => x !== undefined); return newMacro; }); + macros = macros.map((macro, idx) => { + const item = { ...macro }; + item.id = idx; + return item; + }); log.info("result!", macros); state.keymap = keymap; state.superkeys = superkeys; state.macros = macros; + state.modified = true; setState({ ...state }); + startContext(); toggleDeleteModal(); - } + }; - function UpdateList(data: string) { + const UpdateList = (data: string) => { state.selectedList = parseInt(data, 10); setState({ ...state }); - } + }; - function updateKeyboard(keyboardIdx: number) { + const updateKeyboard = (keyboardIdx: number) => { const { macros, superkeys, keymap } = state; let customKeymapList: ListToDeleteType[] = []; let customSuperList: ListToDeleteSType[] = []; @@ -465,10 +472,11 @@ function MacroEditor(props: MacroEditorProps) { state.listToDeleteS = customSuperList; state.listToDeleteM = customMacrosList; state.showDeleteModal = customKeymapList.length > 0 || customSuperList.length > 0 || customMacrosList.length > 0; + log.info("data to check when updating keyboard", customKeymapList, customSuperList, customMacrosList); setState({ ...state }); - } + }; - function deleteMacro() { + const deleteMacro = () => { const { macros, selectedMacro } = state; if (macros.length === 0) { return; @@ -481,14 +489,15 @@ function MacroEditor(props: MacroEditorProps) { item.id = idx; return item; }); - if (selected >= macros.length - 1) { + if (selected === macros.length - 1) { + updateMacros(localMacros); changeSelected(macros.length - 2); + } else { + updateKeyboard(selected); } - updateKeyboard(selected); - updateMacros(localMacros); - } + }; - function addMacro(name: string) { + const addMacro = (name: string) => { const { macros, maxMacros } = state; if (macros.length >= maxMacros) { return; @@ -503,9 +512,9 @@ function MacroEditor(props: MacroEditorProps) { }); updateMacros(aux); changeSelected(newID); - } + }; - function clearMacro() { + const clearMacro = () => { const { macros, selectedMacro } = state; if (macros.length === 0) { return; @@ -513,9 +522,9 @@ function MacroEditor(props: MacroEditorProps) { const localMacros = JSON.parse(JSON.stringify(macros)) as MacrosType[]; localMacros[selectedMacro].actions = []; updateMacros(localMacros); - } + }; - function macroTranslator(raw: string) { + const macroTranslator = (raw: string) => { const { storedMacros } = state; const macrosArray = raw.split(" 0 0")[0].split(" ").map(Number); @@ -588,7 +597,7 @@ function MacroEditor(props: MacroEditorProps) { macro: macro.actions.map(k => keymapDB.parse(k.keyCode as number).label).join(" "), }; }); - } + }; const loadMacros = async () => { const { onDisconnect, cancelContext, setLoading } = props; @@ -728,6 +737,8 @@ function MacroEditor(props: MacroEditorProps) { scrollPos, } = state; + const { saveButtonRef, discardChangesButtonRef } = props; + let ListOfDeletes = listToDelete.map(({ layer, pos, key, newKey }) => { if (newKey === -1) { return ( @@ -774,11 +785,15 @@ function MacroEditor(props: MacroEditorProps) { No Key - {macros.map(macro => ( - - {macro?.name ? `#${macro.id + 1}. ${macro?.name}` : `#${macro.id + 1}. No name`} - - ))} + {macros.map(macro => + macro.id !== selectedMacro ? ( + + {macro?.name ? `#${macro.id + 1}. ${macro?.name}` : `#${macro.id + 1}. No name`} + + ) : ( + "" + ), + )} @@ -810,8 +825,8 @@ function MacroEditor(props: MacroEditorProps) { saveContext={writeMacros} destroyContext={destroyThisContext} inContext={modified} - saveButtonRef={props.saveButtonRef} - discardChangesButtonRef={props.discardChangesButtonRef} + saveButtonRef={saveButtonRef} + discardChangesButtonRef={discardChangesButtonRef} /> { const { superkeys, selectedSuper } = state; - if (superkeys.length > 0) { - let aux = JSON.parse(JSON.stringify(superkeys)); - const selected = selectedSuper; - aux.splice(selected, 1); - aux = aux.map((item: SuperkeysType, index: number) => { - const newItem = item; - newItem.id = index; - return newItem; - }); - if (selected >= superkeys.length - 1) { - checkKBSuperkeys(aux, aux.length - 1, aux.length + 53980); - } else { - checkKBSuperkeys(aux, selected, selected + 53980); - } + if (!Array.isArray(superkeys) || superkeys.length <= 0 || selectedSuper < 0) { + return; + } + let aux = JSON.parse(JSON.stringify(superkeys)); + const selected = selectedSuper; + aux.splice(selected, 1); + aux = aux.map((item: SuperkeysType, index: number) => { + const newItem = item; + newItem.id = index; + return newItem; + }); + if (selected >= superkeys.length - 1) { + checkKBSuperkeys(aux, aux.length - 1, aux.length + 53980); + } else { + checkKBSuperkeys(aux, selected, selected + 53980); } };