Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: delete button now acts properly when used #894

Merged
merged 1 commit into from
Oct 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
109 changes: 62 additions & 47 deletions src/renderer/views/MacroEditor.tsx
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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;

Expand All @@ -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;

Expand All @@ -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];
Expand All @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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[] = [];
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -503,19 +512,19 @@ function MacroEditor(props: MacroEditorProps) {
});
updateMacros(aux);
changeSelected(newID);
}
};

function clearMacro() {
const clearMacro = () => {
const { macros, selectedMacro } = state;
if (macros.length === 0) {
return;
}
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);

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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 (
Expand Down Expand Up @@ -774,11 +785,15 @@ function MacroEditor(props: MacroEditorProps) {
<SelectItem value={(-1).toString()} key={`macro-${-1}`} disabled={false}>
No Key
</SelectItem>
{macros.map(macro => (
<SelectItem value={macro.id.toString()} key={`macro-${macro.id}`} disabled={macro.id === -1}>
{macro?.name ? `#${macro.id + 1}. ${macro?.name}` : `#${macro.id + 1}. No name`}
</SelectItem>
))}
{macros.map(macro =>
macro.id !== selectedMacro ? (
<SelectItem value={macro.id.toString()} key={`macro-${macro.id}`} disabled={macro.id === -1}>
{macro?.name ? `#${macro.id + 1}. ${macro?.name}` : `#${macro.id + 1}. No name`}
</SelectItem>
) : (
""
),
)}
</SelectContent>
</Select>
</>
Expand Down Expand Up @@ -810,8 +825,8 @@ function MacroEditor(props: MacroEditorProps) {
saveContext={writeMacros}
destroyContext={destroyThisContext}
inContext={modified}
saveButtonRef={props.saveButtonRef}
discardChangesButtonRef={props.discardChangesButtonRef}
saveButtonRef={saveButtonRef}
discardChangesButtonRef={discardChangesButtonRef}
/>

<Callout
Expand Down
29 changes: 15 additions & 14 deletions src/renderer/views/SuperkeysEditor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -661,20 +661,21 @@ function SuperkeysEditor(props: SuperkeysEditorProps) {

const deleteSuperkey = () => {
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);
}
};

Expand Down
Loading