one
Version:
One is a React framework for web, iOS, and Android apps.
136 lines (134 loc) • 4.98 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.native.js");
var layoutRouteNamePrefix = "__one_layout:";
function convertDynamicRouteToReactNavigation(segment) {
if (segment === "index") {
return "";
}
if (segment === "+not-found") {
return "*not-found";
}
var 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(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 = (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 {
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)
};
}