Eingebette Loops
Node.js build / build (20.x) (push) Waiting to run
Node.js build / build (22.x) (push) Waiting to run

This commit is contained in:
Lars committed 2026-01-16 20:10:08 +01:00
1 parent 684217bdf4
commit 5a171987b2
5 files changed
+722 -50

No files matched your search

+64
View File
@@ -7,6 +7,7 @@ export interface LoopRuntimeState {
items: unknown[]; // Committed items
draft: Record<string, unknown>; // Current draft being edited
editIndex: number | null; // Index of item being edited, or null for new item
activeItemStepIndex: number; // Current step index in subwizard (0-based)
}
/**
@@ -17,6 +18,7 @@ export function createLoopState(): LoopRuntimeState {
items: [],
draft: {},
editIndex: null,
activeItemStepIndex: 0,
};
}
@@ -94,6 +96,7 @@ export function commitDraft(state: LoopRuntimeState): LoopRuntimeState {
items: newItems,
draft: {},
editIndex: null,
activeItemStepIndex: 0,
};
}
@@ -117,6 +120,7 @@ export function startEdit(
...state,
draft: { ...(item as Record<string, unknown>) },
editIndex: index,
activeItemStepIndex: 0, // Reset to first step when starting edit
};
}
@@ -152,6 +156,7 @@ export function deleteItem(
editIndex: newEditIndex,
// Clear draft if we were editing the deleted item
draft: newEditIndex === null ? {} : state.draft,
activeItemStepIndex: newEditIndex === null ? 0 : state.activeItemStepIndex,
};
}
@@ -229,5 +234,64 @@ export function clearDraft(state: LoopRuntimeState): LoopRuntimeState {
...state,
draft: {},
editIndex: null,
activeItemStepIndex: 0,
};
}
/**
* Set active item step index.
*/
export function setActiveItemStepIndex(
state: LoopRuntimeState,
index: number
): LoopRuntimeState {
if (index < 0) {
return state; // Invalid index
}
return {
...state,
activeItemStepIndex: index,
};
}
/**
* Move to next step in item subwizard.
*/
export function itemNextStep(
state: LoopRuntimeState,
childStepCount: number
): LoopRuntimeState {
if (state.activeItemStepIndex >= childStepCount - 1) {
return state; // Already at last step
}
return {
...state,
activeItemStepIndex: state.activeItemStepIndex + 1,
};
}
/**
* Move to previous step in item subwizard.
*/
export function itemPrevStep(state: LoopRuntimeState): LoopRuntimeState {
if (state.activeItemStepIndex <= 0) {
return state; // Already at first step
}
return {
...state,
activeItemStepIndex: state.activeItemStepIndex - 1,
};
}
/**
* Reset item wizard to first step.
*/
export function resetItemWizard(state: LoopRuntimeState): LoopRuntimeState {
return {
...state,
activeItemStepIndex: 0,
};
}
+36 -2
View File
@@ -130,9 +130,23 @@ function renderCaptureFrontmatter(step: CaptureFrontmatterStep, answers: RenderA
}
/**
* Render loop step.
* Render loop step (recursive, supports arbitrary nesting depth).
*/
function renderLoop(step: LoopStep, answers: RenderAnswers): string | null {
return renderLoopRecursive(step, answers, 0);
}
/**
* Recursive helper to render loop items with nested steps.
* Supports arbitrary nesting depth (no hard limit, but practical limits apply due to stack size).
*/
function renderLoopRecursive(step: LoopStep, answers: RenderAnswers, depth: number): string | null {
// Safety check: prevent infinite recursion (practical limit: 100 levels)
if (depth > 100) {
console.warn(`Loop nesting depth ${depth} exceeds practical limit, stopping recursion`);
return null;
}
const items = answers.loopContexts.get(step.key);
if (!items || items.length === 0) {
return null;
@@ -147,7 +161,7 @@ function renderLoop(step: LoopStep, answers: RenderAnswers): string | null {
const itemParts: string[] = [];
// Render each nested step for this item
// Render each nested step for this item (recursively)
for (const nestedStep of step.items) {
const fieldValue = (item as Record<string, unknown>)[nestedStep.key];
@@ -198,6 +212,26 @@ function renderLoop(step: LoopStep, answers: RenderAnswers): string | null {
}));
}
}
} else if (nestedStep.type === "loop") {
// Recursively handle nested loop: fieldValue should be an array of items
if (Array.isArray(fieldValue) && fieldValue.length > 0) {
const nestedLoopStep = nestedStep as LoopStep;
// Create a temporary answers object for the nested loop
// The nested loop items are stored in fieldValue, so we need to create
// a temporary loopContexts map for the nested loop
const nestedAnswers: RenderAnswers = {
collectedData: answers.collectedData,
loopContexts: new Map(answers.loopContexts),
};
nestedAnswers.loopContexts.set(nestedLoopStep.key, fieldValue);
// Recursively render the nested loop
const nestedOutput = renderLoopRecursive(nestedLoopStep, nestedAnswers, depth + 1);
if (nestedOutput) {
itemParts.push(nestedOutput);
}
}
}
}