80 lines
2.3 KiB
TypeScript
80 lines
2.3 KiB
TypeScript
export type GridInsertionRowType =
|
|
| "circuitCompact"
|
|
| "circuitSummary"
|
|
| "deviceRow"
|
|
| "reserveCircuit"
|
|
| "placeholder";
|
|
|
|
export type GridInsertionCellKind = "circuitField" | "deviceField" | "readonly" | "computed";
|
|
|
|
export interface GridInsertionSelection {
|
|
rowType: GridInsertionRowType;
|
|
cellKind: GridInsertionCellKind;
|
|
sectionId: string;
|
|
circuitId?: string;
|
|
deviceId?: string;
|
|
}
|
|
|
|
export type GridInsertionIntent =
|
|
| { kind: "circuit"; sectionId: string; afterCircuitId?: string }
|
|
| { kind: "device"; sectionId: string; circuitId: string; afterDeviceRowId?: string };
|
|
|
|
export function resolveGridInsertionIntent(
|
|
selection: GridInsertionSelection | null,
|
|
fallbackSectionId?: string
|
|
): GridInsertionIntent | null {
|
|
if (!selection) {
|
|
return fallbackSectionId ? { kind: "circuit", sectionId: fallbackSectionId } : null;
|
|
}
|
|
|
|
if (selection.rowType === "placeholder") {
|
|
return { kind: "circuit", sectionId: selection.sectionId };
|
|
}
|
|
|
|
const targetsDevice =
|
|
selection.rowType === "deviceRow" ||
|
|
((selection.rowType === "circuitCompact" || selection.rowType === "reserveCircuit") &&
|
|
selection.cellKind === "deviceField");
|
|
|
|
if (targetsDevice && selection.circuitId) {
|
|
return {
|
|
kind: "device",
|
|
sectionId: selection.sectionId,
|
|
circuitId: selection.circuitId,
|
|
afterDeviceRowId: selection.deviceId,
|
|
};
|
|
}
|
|
|
|
if (selection.circuitId) {
|
|
return {
|
|
kind: "circuit",
|
|
sectionId: selection.sectionId,
|
|
afterCircuitId: selection.circuitId,
|
|
};
|
|
}
|
|
|
|
return { kind: "circuit", sectionId: selection.sectionId };
|
|
}
|
|
|
|
export function getInsertionSortOrder(
|
|
orderedEntries: Array<{ id: string; sortOrder: number }>,
|
|
afterId?: string
|
|
): number {
|
|
if (orderedEntries.length === 0) {
|
|
return 10;
|
|
}
|
|
if (!afterId) {
|
|
return Math.max(...orderedEntries.map((entry) => entry.sortOrder)) + 10;
|
|
}
|
|
|
|
const entries = [...orderedEntries].sort((left, right) => left.sortOrder - right.sortOrder);
|
|
const index = entries.findIndex((entry) => entry.id === afterId);
|
|
if (index < 0) {
|
|
return entries[entries.length - 1].sortOrder + 10;
|
|
}
|
|
|
|
const current = entries[index].sortOrder;
|
|
const next = entries[index + 1]?.sortOrder;
|
|
return next === undefined ? current + 10 : current + (next - current) / 2;
|
|
}
|