UNPKG

one

Version:

One is a React framework for web, iOS, and Android apps.

145 lines (144 loc) • 4.62 kB
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 findRouteNode_exports = {}; __export(findRouteNode_exports, { extractParamsFromState: () => extractParamsFromState, extractPathnameFromHref: () => extractPathnameFromHref, extractSearchFromHref: () => extractSearchFromHref, findAllRouteNodesFromState: () => findAllRouteNodesFromState, findRouteNodeFromState: () => findRouteNodeFromState }); module.exports = __toCommonJS(findRouteNode_exports); var import_getReactNavigationConfig = require("../getReactNavigationConfig.cjs"); function findRouteNodeFromState(state, rootNode) { if (!state || !state.routes || !rootNode) { return null; } const currentRoute = state.routes[state.index ?? state.routes.length - 1]; if (!currentRoute) { return null; } const matchingNode = findNodeByRouteName(rootNode, currentRoute.name); if (!matchingNode) { return null; } if (currentRoute.state && currentRoute.state.routes) { const nestedResult = findRouteNodeFromState(currentRoute.state, matchingNode); if (nestedResult) { return nestedResult; } } return matchingNode; } function findNodeByRouteName(node, routeName) { if ((0, import_getReactNavigationConfig.getReactNavigationRouteName)(node) === routeName) { return node; } for (const child of node.children) { const found = findNodeByRouteName(child, routeName); if (found) { return found; } } return null; } function extractParamsFromState(state) { if (!state || !state.routes) { return {}; } const params = {}; const route = state.routes[state.index ?? state.routes.length - 1]; if (route?.params) { Object.assign(params, route.params); } if (route?.state) { Object.assign(params, extractParamsFromState(route.state)); } return params; } function extractSearchFromHref(href) { const searchIndex = href.indexOf("?"); if (searchIndex === -1) { return {}; } const searchString = href.slice(searchIndex + 1); const params = {}; const searchParams = new URLSearchParams(searchString); searchParams.forEach((value, key) => { const existing = params[key]; if (existing === void 0) { params[key] = value; } else if (Array.isArray(existing)) { existing.push(value); } else { params[key] = [existing, value]; } }); return params; } function extractPathnameFromHref(href) { const searchIndex = href.indexOf("?"); const hashIndex = href.indexOf("#"); let endIndex = href.length; if (searchIndex !== -1) endIndex = Math.min(endIndex, searchIndex); if (hashIndex !== -1) endIndex = Math.min(endIndex, hashIndex); return href.slice(0, endIndex); } function findAllRouteNodesFromState(state, rootNode) { if (!state || !state.routes || !rootNode) { return []; } const nodes = []; function collectNodes(currentState, parentNode) { if (!currentState || !currentState.routes || !parentNode) { return; } const currentRoute = currentState.routes[currentState.index ?? currentState.routes.length - 1]; if (!currentRoute) { return; } const matchingNode = findNodeByRouteName(parentNode, currentRoute.name); if (!matchingNode) { if (process.env.NODE_ENV === "development") { console.log("[one] findAllRouteNodesFromState: could not find node for", currentRoute.name, "in", parentNode.route); } return; } nodes.push(matchingNode); if (currentRoute.state && currentRoute.state.routes) { collectNodes(currentRoute.state, matchingNode); } else if (currentRoute.params?.screen) { const childRouteName = currentRoute.params.screen; const childNode = matchingNode.children.find((c) => c.route === childRouteName); if (childNode) { nodes.push(childNode); if (currentRoute.params.params && currentRoute.params.params.screen) { collectNodes({ routes: [{ name: childRouteName, params: currentRoute.params.params }] }, childNode); } } } } collectNodes(state, rootNode); return nodes; }