nitropage
Version:
A free and open source, extensible visual page builder based on SolidStart.
29 lines (25 loc) • 859 B
text/typescript
import { useContext } from "solid-js";
import { EditorUiContext } from "../../../../lib/context/admin";
import { StateContext } from "../../../../lib/context/page";
import { spliceValue } from "../../../../utils/array";
export const makeCancelElementCreation = ({
stateContext = useContext(StateContext),
editorUiContext = useContext(EditorUiContext),
} = {}) => {
const [state, setState] = stateContext!;
const [editorUi, setEditorUi] = editorUiContext!;
return () => {
const id = editorUi.creatingElementId;
if (!id) return;
if (state.elements[id]) {
setState((draft) => {
const element = draft.elements[id];
spliceValue(id, draft.slots[element.parentSlotId!].elements);
delete draft.elements[id];
});
}
setEditorUi((draft) => {
draft.creatingElementId = undefined;
});
};
};