nitropage
Version:
A free and open source, extensible visual page builder based on SolidStart.
62 lines (51 loc) • 1.33 kB
text/typescript
import { Config, Element, ElementSlot, State } from "#../types";
import { createId } from "@paralleldrive/cuid2";
export const createSlot = (
key: string,
parentElementId: string,
id = createId(),
): ElementSlot => {
return {
id,
parentElementId,
key,
elements: [],
};
};
export const addSlotToState = (draft: State, newSlot: ElementSlot) => {
draft.slots[newSlot.id] = structuredClone(newSlot);
draft.elements[newSlot.parentElementId].slots[newSlot.key] = newSlot.id;
};
export const slotHasSpace = function (
state: State,
declaration: Config,
targetElement?: Element,
targetSlotKey?: string,
) {
if (!targetElement) {
return true;
}
if (targetElement.id === "root") {
return true;
}
if (!targetSlotKey) {
return true;
}
const targetSlotId = targetElement.slots[targetSlotKey];
const targetSlot: ElementSlot | undefined = state.slots[targetSlotId];
if (targetSlot?.layoutSlotId) return false;
const blueprint = targetElement.blueprintId
? declaration.blueprints[targetElement.blueprintId]
: undefined;
if (!blueprint) {
return true;
}
const slot = blueprint.slots?.[targetSlotKey];
if (!slot) {
return false;
}
if (slot.max != null && (targetSlot?.elements.length ?? 0) >= slot.max) {
return false;
}
return true;
};