one
Version:
One is a React framework for web, iOS, and Android apps.
126 lines (125 loc) • 4.58 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 getReactNavigationConfig_exports = {};
__export(getReactNavigationConfig_exports, {
getReactNavigationConfig: () => getReactNavigationConfig,
getReactNavigationRouteName: () => getReactNavigationRouteName,
resolveInitialRouteNameFromState: () => resolveInitialRouteNameFromState
});
module.exports = __toCommonJS(getReactNavigationConfig_exports);
var import_matchers = require("./router/matchers.cjs");
const layoutRouteNamePrefix = "__one_layout:";
function convertDynamicRouteToReactNavigation(segment) {
if (segment === "index") {
return "";
}
if (segment === "+not-found") {
return "*not-found";
}
const dynamicMatch = (0, import_matchers.matchDynamicName)(segment);
if (dynamicMatch) {
if (dynamicMatch.deep) {
return "*" + dynamicMatch.name;
}
return `:${dynamicMatch.name}`;
}
return segment;
}
function parseRouteSegments(segments) {
return segments.split("/").map(convertDynamicRouteToReactNavigation).filter(Boolean).join("/");
}
function hasDescendantWithRoute(node, route) {
return node.children.some((child) => child.route === route || hasDescendantWithRoute(child, route));
}
function getReactNavigationRouteName(node) {
if (node.children.length && hasDescendantWithRoute(node, node.route)) {
return `${layoutRouteNamePrefix}${node.contextKey}`;
}
return node.route;
}
function focusedRoute(state) {
return state.routes[state.index ?? state.routes.length - 1];
}
function resolveInitialRouteNameFromState(contextKey, state) {
const contextSegments = contextKey.split("/").filter(Boolean);
let current = state;
let consumedSegments = 0;
while (consumedSegments < contextSegments.length) {
if (!current?.routes.length) return void 0;
const route = focusedRoute(current);
if (!route?.state) return void 0;
if (route.name.startsWith(layoutRouteNamePrefix)) {
const routeContextSegments = (0, import_matchers.getContextKey)(route.name.slice(layoutRouteNamePrefix.length)).split("/").filter(Boolean);
if (routeContextSegments.length <= consumedSegments || routeContextSegments.join("/") !== contextSegments.slice(0, routeContextSegments.length).join("/")) {
return void 0;
}
consumedSegments = routeContextSegments.length;
} else {
const routeSegments = route.name.split("/").filter(Boolean);
if (route.name !== contextSegments.slice(consumedSegments, consumedSegments + routeSegments.length).join("/")) {
return void 0;
}
consumedSegments += routeSegments.length;
}
current = route.state;
}
return current?.routes.length ? focusedRoute(current)?.name : void 0;
}
function getReactNavigationInitialRouteName(nodes, initialRouteName) {
if (!initialRouteName) return void 0;
const initialRoute = nodes.find((node) => node.route === initialRouteName);
return initialRoute ? getReactNavigationRouteName(initialRoute) : initialRouteName;
}
function convertRouteNodeToScreen(node, metaOnly) {
const path = parseRouteSegments(node.route);
if (!node.children.length) {
if (!metaOnly) {
return {
path,
screens: {},
_route: node
};
}
return path;
}
const screens = getReactNavigationScreensConfig(node.children, metaOnly);
const screen = {
path,
screens,
initialRouteName: getReactNavigationInitialRouteName(node.children, node.initialRouteName)
};
if (!metaOnly) {
screen._route = node;
}
return screen;
}
function getReactNavigationScreensConfig(nodes, metaOnly) {
return Object.fromEntries(nodes.map((node) => [getReactNavigationRouteName(node), convertRouteNodeToScreen(node, metaOnly)]));
}
function getReactNavigationConfig(routes, metaOnly) {
if (!routes) {
return { screens: {} };
}
return {
initialRouteName: getReactNavigationInitialRouteName(routes.children, routes.initialRouteName),
screens: getReactNavigationScreensConfig(routes.children, metaOnly)
};
}