UNPKG

one

Version:

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

251 lines (249 loc) 10.9 kB
"use strict"; 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, isSoftNavigation: () => isSoftNavigation, 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"), import_constants = require("../constants.native.js"), navigationMode = "hard"; function setNavigationType(type) { navigationMode = type; } function getNavigationType() { return navigationMode; } function isHardNavigation() { return navigationMode === "hard"; } function isSoftNavigation() { return navigationMode === "soft"; } function collectAllLayoutsWithSlots(node) { var collected = arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : []; if (node.slots && node.slots.size > 0 && collected.push(node), node.children) { var _iteratorNormalCompletion = !0, _didIteratorError = !1, _iteratorError = void 0; try { for (var _iterator = node.children[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = !0) { var child = _step.value; collectAllLayoutsWithSlots(child, collected); } } catch (err) { _didIteratorError = !0, _iteratorError = err; } finally { try { !_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\/?/, ""); return path = (0, import_matchers.stripGroupSegmentsFromPath)(path), "/" + path; } function isLayoutAncestorOfPath(layoutPath, currentPath) { var normalizedLayout = layoutPath.replace(/\/+$/, "") || "/", normalizedCurrent = currentPath.replace(/\/+$/, "") || "/"; return normalizedLayout === "/" ? !0 : normalizedCurrent === normalizedLayout || normalizedCurrent.startsWith(normalizedLayout + "/"); } function findLayoutsWithSlotsAlongPath(rootNode, currentPath) { if (!rootNode) return []; var allLayoutsWithSlots = collectAllLayoutsWithSlots(rootNode), ancestorLayouts = allLayoutsWithSlots.filter(function (layout) { var layoutPath = getLayoutPath(layout); return isLayoutAncestorOfPath(layoutPath, currentPath); }); return ancestorLayouts.sort(function (a, b) { var 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; var layoutsWithSlots = findLayoutsWithSlotsAlongPath(rootNode, currentPath); if (layoutsWithSlots.length === 0) return null; for (var i = layoutsWithSlots.length - 1; i >= 0; i--) { var layoutNode = layoutsWithSlots[i], _iteratorNormalCompletion = !0, _didIteratorError = !1, _iteratorError = void 0; try { for (var _iterator = layoutNode.slots[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = !0) { var [slotName, slotConfig] = _step.value, result = findMatchingInterceptInSlot(targetPath, slotName, slotConfig, layoutNode, currentPath); if (result) return result; } } catch (err) { _didIteratorError = !0, _iteratorError = err; } finally { try { !_iteratorNormalCompletion && _iterator.return != null && _iterator.return(); } finally { if (_didIteratorError) throw _iteratorError; } } } return null; } function findMatchingInterceptInSlot(targetPath, slotName, slotConfig, layoutNode, currentPath) { var _iteratorNormalCompletion = !0, _didIteratorError = !1, _iteratorError = void 0; try { for (var _iterator = slotConfig.interceptRoutes[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = !0) { var interceptRoute = _step.value; if (interceptRoute.intercept) { var { levels, targetPath: interceptTargetPath } = interceptRoute.intercept, resolvedTargetPath = resolveInterceptTargetPath(interceptTargetPath, levels, layoutNode, currentPath), params = matchPath(targetPath, resolvedTargetPath); if (params !== null) return { interceptRoute, slotName, layoutContextKey: layoutNode.contextKey, params }; } } } catch (err) { _didIteratorError = !0, _iteratorError = err; } finally { try { !_iteratorNormalCompletion && _iterator.return != null && _iterator.return(); } finally { if (_didIteratorError) throw _iteratorError; } } return null; } function resolveInterceptTargetPath(interceptTargetPath, levels, layoutNode, currentPath) { var layoutPath = layoutNode.contextKey.replace(/^\.\//, "").replace(/\/?_layout.*$/, "").replace(/^app\/?/, ""); if (layoutPath = (0, import_matchers.stripGroupSegmentsFromPath)(layoutPath), (!layoutPath || layoutPath === "/") && (layoutPath = ""), levels === 1 / 0) return "/" + interceptTargetPath; if (levels === 0) { var basePath = layoutPath ? "/" + layoutPath : ""; return basePath + "/" + interceptTargetPath; } var pathParts = layoutPath.split("/").filter(Boolean), parentParts = pathParts.slice(0, -levels), parentPath = parentParts.length > 0 ? "/" + parentParts.join("/") : ""; return parentPath + "/" + interceptTargetPath; } function matchPath(path, pattern) { for (var normalizedPath = "/" + path.replace(/^\/+/, "").replace(/\/+$/, ""), normalizedPattern = "/" + pattern.replace(/^\/+/, "").replace(/\/+$/, ""), pathParts = normalizedPath.split("/").filter(Boolean), patternParts = normalizedPattern.split("/").filter(Boolean), params = {}, pathIndex = 0, i = 0; i < patternParts.length; i++) { var patternPart = patternParts[i], dynamicMatch = (0, import_matchers.matchDynamicName)(patternPart); if (dynamicMatch) { if (dynamicMatch.deep) { var 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; } var 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)); } var clearSlotStatesCallback = null; function registerClearSlotStates(callback) { clearSlotStatesCallback = callback; } function closeIntercept() { if (typeof window > "u") return !1; var state = window.history.state; return state?.__intercepted ? (returningFromIntercept = !0, clearSlotStatesCallback?.(), window.history.back(), !0) : !1; } function isInterceptedNavigation() { var _window_history_state; return typeof window > "u" ? !1 : ((_window_history_state = window.history.state) === null || _window_history_state === void 0 ? void 0 : _window_history_state.__intercepted) === !0; } function getInterceptedActualPath() { var _ref, _window_history_state; return typeof window > "u" ? null : (_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, _window_history_state; return typeof window > "u" ? null : (_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 = !1; function setReturningFromIntercept(value) { returningFromIntercept = value; } function isReturningFromIntercept() { return returningFromIntercept; } var setSlotStateCallback = null; function registerSetSlotState(callback) { setSlotStateCallback = callback; } var lastInterceptRouteNode = null, lastInterceptSlotName = null, lastInterceptParams = null; function storeInterceptState(slotName, routeNode, params) { lastInterceptSlotName = slotName, lastInterceptRouteNode = routeNode, lastInterceptParams = params; } function restoreInterceptFromHistory() { if (typeof window > "u") return !1; var state = window.history.state; return state?.__intercepted && lastInterceptRouteNode && lastInterceptSlotName && setSlotStateCallback ? (setSlotStateCallback(lastInterceptSlotName, { activeRouteKey: lastInterceptRouteNode.contextKey, activeRouteNode: lastInterceptRouteNode, params: lastInterceptParams || {}, isIntercepted: !0 }), !0) : !1; } //# sourceMappingURL=interceptRoutes.native.js.map