one
Version:
One is a new React Framework that makes Vite serve both native and web.
324 lines (322 loc) • 11.4 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.native.js");
var import_constants = require("../constants.native.js");
var navigationMode = "hard";
function setNavigationType(type) {
navigationMode = type;
}
function getNavigationType() {
return navigationMode;
}
function isHardNavigation() {
return navigationMode === "hard";
}
function collectAllLayoutsWithSlots(node) {
var collected = arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : [];
if (node.slots && node.slots.size > 0) {
collected.push(node);
}
if (node.children) {
var _iteratorNormalCompletion = true,
_didIteratorError = false,
_iteratorError = void 0;
try {
for (var _iterator = node.children[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
var child = _step.value;
collectAllLayoutsWithSlots(child, collected);
}
} catch (err) {
_didIteratorError = true;
_iteratorError = err;
} finally {
try {
if (!_iteratorNormalCompletion && _iterator.return != null) {
_iterator.return();
}
} finally {
if (_didIteratorError) {
throw _iteratorError;
}
}
}
}
return collected;
}
function getLayoutPath(node) {
var 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 [];
var allLayoutsWithSlots = collectAllLayoutsWithSlots(rootNode);
var ancestorLayouts = allLayoutsWithSlots.filter(function (layout) {
var layoutPath = getLayoutPath(layout);
return isLayoutAncestorOfPath(layoutPath, currentPath);
});
ancestorLayouts.sort(function (a, b) {
var depthA = getLayoutPath(a).split("/").filter(Boolean).length;
var 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;
}
var layoutsWithSlots = findLayoutsWithSlotsAlongPath(rootNode, currentPath);
if (layoutsWithSlots.length === 0) {
return null;
}
for (var i = layoutsWithSlots.length - 1; i >= 0; i--) {
var layoutNode = layoutsWithSlots[i];
var _iteratorNormalCompletion = true,
_didIteratorError = false,
_iteratorError = void 0;
try {
for (var _iterator = layoutNode.slots[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
var [slotName, slotConfig] = _step.value;
var result = findMatchingInterceptInSlot(targetPath, slotName, slotConfig, layoutNode, currentPath);
if (result) {
return result;
}
}
} catch (err) {
_didIteratorError = true;
_iteratorError = err;
} finally {
try {
if (!_iteratorNormalCompletion && _iterator.return != null) {
_iterator.return();
}
} finally {
if (_didIteratorError) {
throw _iteratorError;
}
}
}
}
return null;
}
function findMatchingInterceptInSlot(targetPath, slotName, slotConfig, layoutNode, currentPath) {
var _iteratorNormalCompletion = true,
_didIteratorError = false,
_iteratorError = void 0;
try {
for (var _iterator = slotConfig.interceptRoutes[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
var interceptRoute = _step.value;
if (!interceptRoute.intercept) {
continue;
}
var {
levels,
targetPath: interceptTargetPath
} = interceptRoute.intercept;
var resolvedTargetPath = resolveInterceptTargetPath(interceptTargetPath, levels, layoutNode);
var params = matchPath(targetPath, resolvedTargetPath);
if (params !== null) {
return {
interceptRoute,
slotName,
layoutContextKey: layoutNode.contextKey,
params
};
}
}
} catch (err) {
_didIteratorError = true;
_iteratorError = err;
} finally {
try {
if (!_iteratorNormalCompletion && _iterator.return != null) {
_iterator.return();
}
} finally {
if (_didIteratorError) {
throw _iteratorError;
}
}
}
return null;
}
function resolveInterceptTargetPath(interceptTargetPath, levels, layoutNode) {
var fullLayoutPath = getLayoutPath(layoutNode);
var layoutPath = fullLayoutPath === "/" ? "" : fullLayoutPath.slice(1);
if (levels === Infinity) {
return "/" + interceptTargetPath;
}
if (levels === 0) {
var basePath = layoutPath ? "/" + layoutPath : "";
return basePath + "/" + interceptTargetPath;
}
var pathParts = layoutPath.split("/").filter(Boolean);
var parentParts = pathParts.slice(0, -levels);
var parentPath = parentParts.length > 0 ? "/" + parentParts.join("/") : "";
return parentPath + "/" + interceptTargetPath;
}
function matchPath(path, pattern) {
var normalizedPath = "/" + path.replace(/^\/+/, "").replace(/\/+$/, "");
var normalizedPattern = "/" + pattern.replace(/^\/+/, "").replace(/\/+$/, "");
var pathParts = normalizedPath.split("/").filter(Boolean);
var patternParts = normalizedPattern.split("/").filter(Boolean);
var params = {};
var pathIndex = 0;
for (var i = 0; i < patternParts.length; i++) {
var patternPart = patternParts[i];
var dynamicMatch = (0, import_matchers.matchDynamicName)(patternPart);
if (dynamicMatch) {
if (dynamicMatch.deep) {
var 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;
}
var 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);
}
}
var clearSlotStatesCallback = null;
function registerClearSlotStates(callback) {
clearSlotStatesCallback = callback;
}
function closeIntercept() {
if (typeof window === "undefined") return false;
var state = window.history.state;
if (!(state === null || state === void 0 ? void 0 : state.__intercepted)) {
return false;
}
returningFromIntercept = true;
clearSlotStatesCallback === null || clearSlotStatesCallback === void 0 ? void 0 : clearSlotStatesCallback();
window.history.back();
return true;
}
function isInterceptedNavigation() {
var _window_history_state;
if (typeof window === "undefined") return false;
return ((_window_history_state = window.history.state) === null || _window_history_state === void 0 ? void 0 : _window_history_state.__intercepted) === true;
}
function getInterceptedActualPath() {
var _ref;
var _window_history_state;
if (typeof window === "undefined") return null;
return (_ref = (_window_history_state = window.history.state) === null || _window_history_state === void 0 ? void 0 : _window_history_state.__actualPath) !== null && _ref !== void 0 ? _ref : null;
}
function getPreInterceptUrl() {
var _ref;
var _window_history_state;
if (typeof window === "undefined") return null;
return (_ref = (_window_history_state = window.history.state) === null || _window_history_state === void 0 ? void 0 : _window_history_state.__preInterceptUrl) !== null && _ref !== void 0 ? _ref : preInterceptUrl;
}
var returningFromIntercept = false;
function setReturningFromIntercept(value) {
returningFromIntercept = value;
}
function isReturningFromIntercept() {
return returningFromIntercept;
}
var setSlotStateCallback = null;
function registerSetSlotState(callback) {
setSlotStateCallback = callback;
}
var lastInterceptRouteNode = null;
var lastInterceptSlotName = null;
var lastInterceptParams = null;
function storeInterceptState(slotName, routeNode, params) {
lastInterceptSlotName = slotName;
lastInterceptRouteNode = routeNode;
lastInterceptParams = params;
}
function restoreInterceptFromHistory() {
if (typeof window === "undefined") return false;
var state = window.history.state;
if (!(state === null || state === void 0 ? void 0 : state.__intercepted)) {
return false;
}
if (lastInterceptRouteNode && lastInterceptSlotName && setSlotStateCallback) {
setSlotStateCallback(lastInterceptSlotName, {
activeRouteKey: lastInterceptRouteNode.contextKey,
activeRouteNode: lastInterceptRouteNode,
params: lastInterceptParams || {},
isIntercepted: true
});
return true;
}
return false;
}
//# sourceMappingURL=interceptRoutes.native.js.map