← Files Flows by plugin.spaceARCHIVED FILE
lib/navigation.mjs
1.47 KB · Nov 30, 2026 · 00:17 UTC
export function startPresentation(document, selectedId = document.startNode, visibleEdges) {
// Keep history anchored at the start so Back and source reconciliation work,
// while opening the selected step on the currently chosen branch.
const allowed = visibleEdges && new Set(visibleEdges);
const pending = [[document.startNode]], visited = new Set();
for (let index = 0; index < pending.length; index++) {
const history = pending[index], current = history.at(-1);
if (current === selectedId) return history;
if (visited.has(current)) continue;
visited.add(current);
for (const edge of document.edges) if (edge.source === current && (!allowed || allowed.has(edge.id)) && !visited.has(edge.target)) pending.push([...history, edge.target]);
}
return [document.startNode];
}
export function followEdge(document, history, edgeId) {
const current = history.at(-1);
const edge = document.edges.find(item => item.id === edgeId && item.source === current);
return edge ? [...history, edge.target] : history;
}
export function reconcileHistory(document, history) {
if (!history.length || history[0] !== document.startNode) return [document.startNode];
const valid = [document.startNode];
for (let index = 1; index < history.length; index += 1) {
if (!document.nodes.some(node => node.id === history[index]) || !document.edges.some(edge => edge.source === valid.at(-1) && edge.target === history[index])) break;
valid.push(history[index]);
}
return valid;
}
SHA-256: f10dde9b874ffe12a99f5c893bd07ff2cd6bb90541e775e3ee3b6c1feb266efc