one
Version:
One is a React framework for web, iOS, and Android apps.
111 lines (108 loc) • 3.97 kB
JavaScript
import { getContextKey, matchDynamicName } from "./router/matchers.native.js";
var layoutRouteNamePrefix = "__one_layout:";
function convertDynamicRouteToReactNavigation(segment) {
if (segment === "index") {
return "";
}
if (segment === "+not-found") {
return "*not-found";
}
var dynamicMatch = 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(function(child) {
return 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) {
var _state_index;
return state.routes[(_state_index = state.index) !== null && _state_index !== void 0 ? _state_index : state.routes.length - 1];
}
function resolveInitialRouteNameFromState(contextKey, state) {
var _focusedRoute;
var contextSegments = contextKey.split("/").filter(Boolean);
var current = state;
var consumedSegments = 0;
while (consumedSegments < contextSegments.length) {
if (!(current === null || current === void 0 ? void 0 : current.routes.length)) return void 0;
var route = focusedRoute(current);
if (!(route === null || route === void 0 ? void 0 : route.state)) return void 0;
if (route.name.startsWith(layoutRouteNamePrefix)) {
var routeContextSegments = 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 {
var 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 === null || current === void 0 ? void 0 : current.routes.length) ? (_focusedRoute = focusedRoute(current)) === null || _focusedRoute === void 0 ? void 0 : _focusedRoute.name : void 0;
}
function getReactNavigationInitialRouteName(nodes, initialRouteName) {
if (!initialRouteName) return void 0;
var initialRoute = nodes.find(function(node) {
return node.route === initialRouteName;
});
return initialRoute ? getReactNavigationRouteName(initialRoute) : initialRouteName;
}
function convertRouteNodeToScreen(node, metaOnly) {
var path = parseRouteSegments(node.route);
if (!node.children.length) {
if (!metaOnly) {
return {
path,
screens: {},
_route: node
};
}
return path;
}
var screens = getReactNavigationScreensConfig(node.children, metaOnly);
var 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(function(node) {
return [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)
};
}
export { getReactNavigationConfig, getReactNavigationRouteName, resolveInitialRouteNameFromState };
//# sourceMappingURL=getReactNavigationConfig.native.js.map