UNPKG

one

Version:

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

208 lines (207 loc) 8.68 kB
var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: !0 }); }, __copyProps = (to, from, except, desc) => { if (from && typeof from == "object" || typeof from == "function") for (let key of __getOwnPropNames(from)) !__hasOwnProp.call(to, key) && key !== except && __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); return to; }; var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: !0 }), mod); var interceptRoutes_exports = {}; __export(interceptRoutes_exports, { closeIntercept: () => closeIntercept, findInterceptRoute: () => findInterceptRoute, getInterceptedActualPath: () => getInterceptedActualPath, getNavigationType: () => getNavigationType, getPreInterceptUrl: () => getPreInterceptUrl, isHardNavigation: () => isHardNavigation, isInterceptedNavigation: () => isInterceptedNavigation, isReturningFromIntercept: () => isReturningFromIntercept, registerClearSlotStates: () => registerClearSlotStates, registerSetSlotState: () => registerSetSlotState, restoreInterceptFromHistory: () => restoreInterceptFromHistory, setNavigationType: () => setNavigationType, setReturningFromIntercept: () => setReturningFromIntercept, storeInterceptState: () => storeInterceptState, updateURLWithoutNavigation: () => updateURLWithoutNavigation }); module.exports = __toCommonJS(interceptRoutes_exports); var import_matchers = require("./matchers"), import_constants = require("../constants"); 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), 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\/?/, ""); return path = (0, import_matchers.stripGroupSegmentsFromPath)(path), "/" + path; } function isLayoutAncestorOfPath(layoutPath, currentPath) { const normalizedLayout = layoutPath.replace(/\/+$/, "") || "/", normalizedCurrent = currentPath.replace(/\/+$/, "") || "/"; if (normalizedLayout === "/") return !0; const layoutParts = normalizedLayout.split("/").filter(Boolean), currentParts = normalizedCurrent.split("/").filter(Boolean); if (layoutParts.length > currentParts.length) return !1; for (let i = 0; i < layoutParts.length; i++) { const layoutPart = layoutParts[i], currentPart = currentParts[i], dynamicMatch = (0, import_matchers.matchDynamicName)(layoutPart); if (dynamicMatch) { if (dynamicMatch.deep) return !0; continue; } if (layoutPart !== currentPart) return !1; } return !0; } function findLayoutsWithSlotsAlongPath(rootNode, currentPath) { if (!rootNode) return []; const ancestorLayouts = collectAllLayoutsWithSlots(rootNode).filter((layout) => { const layoutPath = getLayoutPath(layout); return isLayoutAncestorOfPath(layoutPath, currentPath); }); return ancestorLayouts.sort((a, b) => { const depthA = getLayoutPath(a).split("/").filter(Boolean).length, depthB = getLayoutPath(b).split("/").filter(Boolean).length; return depthA - depthB; }), ancestorLayouts; } function findInterceptRoute(targetPath, rootNode, currentPath) { if (import_constants.isNative || 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, resolvedTargetPath = resolveInterceptTargetPath( interceptTargetPath, levels, layoutNode ), 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 === 1 / 0) return "/" + interceptTargetPath; if (levels === 0) return (layoutPath ? "/" + layoutPath : "") + "/" + interceptTargetPath; const parentParts = layoutPath.split("/").filter(Boolean).slice(0, -levels); return (parentParts.length > 0 ? "/" + parentParts.join("/") : "") + "/" + interceptTargetPath; } function matchPath(path, pattern) { const normalizedPath = "/" + path.replace(/^\/+/, "").replace(/\/+$/, ""), normalizedPattern = "/" + pattern.replace(/^\/+/, "").replace(/\/+$/, ""), pathParts = normalizedPath.split("/").filter(Boolean), patternParts = normalizedPattern.split("/").filter(Boolean), params = {}; let pathIndex = 0; for (let i = 0; i < patternParts.length; i++) { const patternPart = patternParts[i], dynamicMatch = (0, import_matchers.matchDynamicName)(patternPart); if (dynamicMatch) if (dynamicMatch.deep) { const remaining = pathParts.slice(pathIndex); return remaining.length === 0 ? null : (params[dynamicMatch.name] = remaining.join("/"), 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++; } } return pathIndex !== pathParts.length ? null : params; } let preInterceptUrl = null; function updateURLWithoutNavigation(href) { typeof window < "u" && (preInterceptUrl = window.location.pathname + window.location.search, window.history.pushState( { __intercepted: !0, __actualPath: href, __preInterceptUrl: preInterceptUrl }, "", href )); } let clearSlotStatesCallback = null; function registerClearSlotStates(callback) { clearSlotStatesCallback = callback; } function closeIntercept() { return typeof window > "u" || !window.history.state?.__intercepted ? !1 : (returningFromIntercept = !0, clearSlotStatesCallback?.(), window.history.back(), !0); } function isInterceptedNavigation() { return typeof window > "u" ? !1 : window.history.state?.__intercepted === !0; } function getInterceptedActualPath() { return typeof window > "u" ? null : window.history.state?.__actualPath ?? null; } function getPreInterceptUrl() { return typeof window > "u" ? null : window.history.state?.__preInterceptUrl ?? preInterceptUrl; } let returningFromIntercept = !1; function setReturningFromIntercept(value) { returningFromIntercept = value; } function isReturningFromIntercept() { return returningFromIntercept; } let setSlotStateCallback = null; function registerSetSlotState(callback) { setSlotStateCallback = callback; } let lastInterceptRouteNode = null, lastInterceptSlotName = null, lastInterceptParams = null; function storeInterceptState(slotName, routeNode, params) { lastInterceptSlotName = slotName, lastInterceptRouteNode = routeNode, lastInterceptParams = params; } function restoreInterceptFromHistory() { return typeof window > "u" || !window.history.state?.__intercepted ? !1 : lastInterceptRouteNode && lastInterceptSlotName && setSlotStateCallback ? (setSlotStateCallback(lastInterceptSlotName, { activeRouteKey: lastInterceptRouteNode.contextKey, activeRouteNode: lastInterceptRouteNode, params: lastInterceptParams || {}, isIntercepted: !0 }), !0) : !1; } //# sourceMappingURL=interceptRoutes.js.map