UNPKG

one

Version:

One is a new React Framework that makes Vite serve both native and web.

219 lines (218 loc) 7.33 kB
import { matchDynamicName, matchRoutePattern, stripGroupSegmentsFromPath } from "./matchers.mjs"; import { hasWebHistory, isNative } from "../constants.mjs"; let navigationMode = "hard"; function setNavigationType(type) { navigationMode = type; } function getNavigationType() { return navigationMode; } function isHardNavigation() { return navigationMode === "hard"; } function collectAllLayoutsWithSlots(node, collected = []) { if (node.slots && node.slots.size > 0) { collected.push(node); } if (node.children) { for (const child of node.children) { collectAllLayoutsWithSlots(child, collected); } } return collected; } function getLayoutPath(node) { let path = node.contextKey.replace(/^\.\//, "").replace(/\/?_layout.*$/, "").replace(/^app\/?/, ""); path = stripGroupSegmentsFromPath(path); return "/" + path; } function isLayoutAncestorOfPath(layoutPath, currentPath) { if (!layoutPath.replace(/\/+$/, "")) return true; return matchRoutePattern(layoutPath, currentPath) !== null; } function findLayoutsWithSlotsAlongPath(rootNode, currentPath) { if (!rootNode) return []; const allLayoutsWithSlots = collectAllLayoutsWithSlots(rootNode); const ancestorLayouts = allLayoutsWithSlots.filter(layout => { const layoutPath = getLayoutPath(layout); return isLayoutAncestorOfPath(layoutPath, currentPath); }); ancestorLayouts.sort((a, b) => { const depthA = getLayoutPath(a).split("/").filter(Boolean).length; const depthB = getLayoutPath(b).split("/").filter(Boolean).length; return depthA - depthB; }); return ancestorLayouts; } function findInterceptRoute(targetPath, rootNode, currentPath) { if (isNative) { return null; } if (isHardNavigation()) { return null; } const layoutsWithSlots = findLayoutsWithSlotsAlongPath(rootNode, currentPath); if (layoutsWithSlots.length === 0) { return null; } for (let i = layoutsWithSlots.length - 1; i >= 0; i--) { const layoutNode = layoutsWithSlots[i]; for (const [slotName, slotConfig] of layoutNode.slots) { const result = findMatchingInterceptInSlot(targetPath, slotName, slotConfig, layoutNode, currentPath); if (result) { return result; } } } return null; } function findMatchingInterceptInSlot(targetPath, slotName, slotConfig, layoutNode, currentPath) { for (const interceptRoute of slotConfig.interceptRoutes) { if (!interceptRoute.intercept) { continue; } const { levels, targetPath: interceptTargetPath } = interceptRoute.intercept; const resolvedTargetPath = resolveInterceptTargetPath(interceptTargetPath, levels, layoutNode); const params = matchPath(targetPath, resolvedTargetPath); if (params !== null) { return { interceptRoute, slotName, layoutContextKey: layoutNode.contextKey, params }; } } return null; } function resolveInterceptTargetPath(interceptTargetPath, levels, layoutNode) { const fullLayoutPath = getLayoutPath(layoutNode); let layoutPath = fullLayoutPath === "/" ? "" : fullLayoutPath.slice(1); if (levels === Infinity) { return "/" + interceptTargetPath; } if (levels === 0) { const basePath = layoutPath ? "/" + layoutPath : ""; return basePath + "/" + interceptTargetPath; } const pathParts = layoutPath.split("/").filter(Boolean); const parentParts = pathParts.slice(0, -levels); const parentPath = parentParts.length > 0 ? "/" + parentParts.join("/") : ""; return parentPath + "/" + interceptTargetPath; } function matchPath(path, pattern) { const normalizedPath = "/" + path.replace(/^\/+/, "").replace(/\/+$/, ""); const normalizedPattern = "/" + pattern.replace(/^\/+/, "").replace(/\/+$/, ""); const pathParts = normalizedPath.split("/").filter(Boolean); const patternParts = normalizedPattern.split("/").filter(Boolean); const params = {}; let pathIndex = 0; for (let i = 0; i < patternParts.length; i++) { const patternPart = patternParts[i]; const dynamicMatch = matchDynamicName(patternPart); if (dynamicMatch) { if (dynamicMatch.deep) { const remaining = pathParts.slice(pathIndex); if (remaining.length === 0) { return null; } params[dynamicMatch.name] = remaining.join("/"); return params; } else { if (pathIndex >= pathParts.length) { return null; } params[dynamicMatch.name] = pathParts[pathIndex]; pathIndex++; } } else { if (pathIndex >= pathParts.length || pathParts[pathIndex] !== patternPart) { return null; } pathIndex++; } } if (pathIndex !== pathParts.length) { return null; } return params; } let preInterceptUrl = null; function updateURLWithoutNavigation(href) { if (hasWebHistory) { preInterceptUrl = window.location.pathname + window.location.search; window.history.pushState({ __intercepted: true, __actualPath: href, __preInterceptUrl: preInterceptUrl }, "", href); } } let clearSlotStatesCallback = null; function registerClearSlotStates(callback) { clearSlotStatesCallback = callback; } function closeIntercept() { if (!hasWebHistory) return false; const state = window.history.state; if (!state?.__intercepted) { return false; } returningFromIntercept = true; clearSlotStatesCallback?.(); window.history.back(); return true; } function isInterceptedNavigation() { if (!hasWebHistory) return false; return window.history.state?.__intercepted === true; } function getInterceptedActualPath() { if (!hasWebHistory) return null; return window.history.state?.__actualPath ?? null; } function getPreInterceptUrl() { if (!hasWebHistory) return null; return window.history.state?.__preInterceptUrl ?? preInterceptUrl; } let returningFromIntercept = false; function setReturningFromIntercept(value) { returningFromIntercept = value; } function isReturningFromIntercept() { return returningFromIntercept; } let setSlotStateCallback = null; function registerSetSlotState(callback) { setSlotStateCallback = callback; } let lastInterceptRouteNode = null; let lastInterceptSlotName = null; let lastInterceptParams = null; function storeInterceptState(slotName, routeNode, params) { lastInterceptSlotName = slotName; lastInterceptRouteNode = routeNode; lastInterceptParams = params; } function restoreInterceptFromHistory() { if (!hasWebHistory) return false; const state = window.history.state; if (!state?.__intercepted) { return false; } if (lastInterceptRouteNode && lastInterceptSlotName && setSlotStateCallback) { setSlotStateCallback(lastInterceptSlotName, { activeRouteKey: lastInterceptRouteNode.contextKey, activeRouteNode: lastInterceptRouteNode, params: lastInterceptParams || {}, isIntercepted: true }); return true; } return false; } export { closeIntercept, findInterceptRoute, getInterceptedActualPath, getNavigationType, getPreInterceptUrl, isHardNavigation, isInterceptedNavigation, isReturningFromIntercept, registerClearSlotStates, registerSetSlotState, restoreInterceptFromHistory, setNavigationType, setReturningFromIntercept, storeInterceptState, updateURLWithoutNavigation }; //# sourceMappingURL=interceptRoutes.mjs.map