one
Version:
One is a new React Framework that makes Vite serve both native and web.
257 lines • 8.77 kB
JavaScript
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: true
});
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from)) if (!__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: true
}), 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.cjs");
var import_constants = require("../constants.cjs");
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 = (0, import_matchers.stripGroupSegmentsFromPath)(path);
return "/" + path;
}
function isLayoutAncestorOfPath(layoutPath, currentPath) {
if (!layoutPath.replace(/\/+$/, "")) return true;
return (0, import_matchers.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 (import_constants.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 = (0, import_matchers.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 (typeof window !== "undefined") {
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 (typeof window === "undefined") return false;
const state = window.history.state;
if (!state?.__intercepted) {
return false;
}
returningFromIntercept = true;
clearSlotStatesCallback?.();
window.history.back();
return true;
}
function isInterceptedNavigation() {
if (typeof window === "undefined") return false;
return window.history.state?.__intercepted === true;
}
function getInterceptedActualPath() {
if (typeof window === "undefined") return null;
return window.history.state?.__actualPath ?? null;
}
function getPreInterceptUrl() {
if (typeof window === "undefined") 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 (typeof window === "undefined") 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;
}