one
Version:
One is a new React Framework that makes Vite serve both native and web.
212 lines • 8.11 kB
JavaScript
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
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 __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
// If the importer is in node compatibility mode or this is not an ESM
// file that has been converted to a CommonJS file using a Babel-
// compatible transform (i.e. "__esModule" has not been set), then set
// "default" to the CommonJS "module.exports" for node compatibility.
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
value: mod,
enumerable: true
}) : target, mod));
var __toCommonJS = mod => __copyProps(__defProp({}, "__esModule", {
value: true
}), mod);
var getPathFromState_exports = {};
__export(getPathFromState_exports, {
default: () => getPathFromState_default,
getPathDataFromState: () => getPathDataFromState,
getPathFromState: () => getPathFromState
});
module.exports = __toCommonJS(getPathFromState_exports);
var sharedModUtils = __toESM(require("./_shared.cjs"), 1);
var import_getPathFromState_mods = require("./getPathFromState-mods.cjs");
var import_validatePathConfig = require("./validatePathConfig.cjs");
const NAV_PARAM_SCREEN = "screen";
const NAV_PARAM_PARAMS = "params";
const NAV_PARAM_KEY = "key";
const getActiveRoute = state => {
const route = typeof state.index === "number" ? state.routes[state.index] : state.routes[state.routes.length - 1];
if (route.state) {
return getActiveRoute(route.state);
}
return route;
};
const cachedNormalizedConfigs = /* @__PURE__ */new WeakMap();
const getNormalizedConfigs = options => {
if (!options?.screens) return {};
const cached = cachedNormalizedConfigs.get(options?.screens);
if (cached) return cached;
const normalizedConfigs = createNormalizedConfigs(options.screens);
cachedNormalizedConfigs.set(options.screens, normalizedConfigs);
return normalizedConfigs;
};
function getPathFromState(state, options) {
return getPathDataFromState(state, options).path;
}
function getPathDataFromState(state, options) {
if (state == null) {
throw Error("Got 'undefined' for the navigation state. You must pass a valid state object.");
}
if (options) {
(0, import_validatePathConfig.validatePathConfig)(options);
}
const configs = getNormalizedConfigs(options);
let path = "/";
let current = state;
const allParams = {};
while (current) {
if (!current.routes?.length) {
break;
}
let index = typeof current.index === "number" ? current.index : 0;
let route = current.routes[index];
let pattern;
let focusedParams;
const focusedRoute = getActiveRoute(state);
let currentOptions = configs;
const nestedRouteNames = [];
let hasNext = true;
while (route.name in currentOptions && hasNext) {
pattern = currentOptions[route.name].pattern;
nestedRouteNames.push(route.name);
if (route.params) {
const stringify = currentOptions[route.name]?.stringify;
const currentParams = Object.fromEntries(Object.entries(route.params).flatMap(([key, value]) => {
if (key === NAV_PARAM_SCREEN || key === NAV_PARAM_PARAMS || key === NAV_PARAM_KEY) {
return [];
}
return [[key, stringify?.[key] ? stringify[key](value) : Array.isArray(value) ? value.map(String) : typeof value === "undefined" ? value : String(value)]];
}));
Object.assign(allParams, currentParams);
if (focusedRoute === route) {
focusedParams = {
...currentParams
};
pattern?.split("/").filter(p => sharedModUtils.isDynamicPart(p)).forEach(p => {
const name = sharedModUtils.getParamName(p);
if (focusedParams) {
delete focusedParams[name];
}
});
}
}
if (!currentOptions[route.name].screens || route.state === void 0) {
const screens = currentOptions[route.name].screens;
const screen = route.params && NAV_PARAM_SCREEN in route.params ? route.params[NAV_PARAM_SCREEN]?.toString() : screens ? Object.keys(screens)[0] : void 0;
if (screen && screens && currentOptions[route.name].screens?.[screen]) {
route = {
...screens[screen],
name: screen,
key: screen,
params: route.params?.[NAV_PARAM_PARAMS]
};
currentOptions = screens;
} else {
hasNext = false;
}
} else {
index = typeof route.state.index === "number" ? route.state.index : route.state.routes.length - 1;
const nextRoute = route.state.routes[index];
const nestedConfig = currentOptions[route.name].screens;
if (nestedConfig && nextRoute.name in nestedConfig) {
route = nextRoute;
currentOptions = nestedConfig;
} else {
hasNext = false;
}
}
}
if (currentOptions[route.name] !== void 0) {
if (pattern === void 0) {
pattern = nestedRouteNames.join("/");
}
path += (0, import_getPathFromState_mods.getPathWithConventionsCollapsed)({
...options,
pattern,
route,
params: allParams,
initialRouteName: configs[route.name]?.initialRouteName
});
} else if (!route.name.startsWith("+")) {
path += encodeURIComponent(route.name);
}
if (!focusedParams) {
focusedParams = {
...focusedRoute.params
};
}
if (route.state) {
path += "/";
} else if (focusedParams) {
for (const param in focusedParams) {
if (focusedParams[param] === "undefined") {
delete focusedParams[param];
}
}
delete focusedParams["#"];
const query = new URLSearchParams(focusedParams).toString();
if (query) {
path += `?${query}`;
}
}
current = route.state;
}
path = path.replace(/\/+/g, "/");
path = path.length > 1 ? path.replace(/\/$/, "") : path;
if (options?.path) {
path = joinPaths(options.path, path);
}
path = (0, import_getPathFromState_mods.appendBaseUrl)(path);
if (allParams["#"]) {
path += `#${allParams["#"]}`;
}
return {
path,
params: allParams
};
}
const joinPaths = (...paths) => [].concat(...paths.map(p => p.split("/"))).filter(Boolean).join("/");
const createConfigItem = (config, parentPattern) => {
if (typeof config === "string") {
const pattern2 = parentPattern ? joinPaths(parentPattern, config) : config;
return {
pattern: pattern2
};
}
if (config.exact && config.path === void 0) {
throw new Error("A 'path' needs to be specified when specifying 'exact: true'. If you don't want this screen in the URL, specify it as empty string, e.g. `path: ''`.");
}
const pattern = config.exact !== true ? joinPaths(parentPattern || "", config.path || "") : config.path || "";
const screens = config.screens ? createNormalizedConfigs(config.screens, pattern) : void 0;
return {
// Normalize pattern to remove any leading, trailing slashes, duplicate slashes etc.
pattern: pattern?.split("/").filter(Boolean).join("/"),
stringify: config.stringify,
screens
};
};
const createNormalizedConfigs = (options, pattern) => Object.fromEntries(Object.entries(options).map(([name, c]) => {
const result = createConfigItem(c, pattern);
return [name, result];
}));
var getPathFromState_default = getPathFromState;