Skip to content

Commit

Permalink
fix: routing when there is no parent
Browse files Browse the repository at this point in the history
KevinBLT committed Mar 10, 2024

Verified

This commit was signed with the committer’s verified signature.
derekpierre Derek Pierre
1 parent c690680 commit 994921f
Showing 1 changed file with 31 additions and 27 deletions.
58 changes: 31 additions & 27 deletions packages/@hec.js/ui/lib/src/routing.js
Original file line number Diff line number Diff line change
@@ -39,34 +39,38 @@ export function routeCompare(a,b) {

/** @param { Route } route */
export function addRoute(route) {
let node = route.node,
path = route.path,
parent = node.parentElement?.closest('[data-route]'),
parentRoute = routingNodes.get(parent),
parentPath = parentRoute?.pattern?.pathname,
targetRoutes = parentRoute?.group ?? routes;

route.group = [];

routingNodes.set(node, route);

if (parentPath) {
path = path == '/' ? '' : path;
path = parentPath.replaceAll(/[^\/a-zA-Z0-9]+$/gm, '') + path.replaceAll(/^[^\/a-zA-Z0-9]+/gm, '');
}

path = path.replaceAll(/\/+/g, '/').replace(/\/$/m, '');

route.pattern = new URLPattern({ pathname: !path ? '/' : path });

targetRoutes.push(route);
targetRoutes.sort(routeCompare);

if (!state.updateQueued) {
state.updateQueued = true;

queueMicrotask(updateRouting);
}
queueMicrotask(() => {
let node = route.node,
path = route.path,
parent = node.parentElement?.closest('[data-route]'),
parentRoute = routingNodes.get(parent),
parentPath = parentRoute?.pattern?.pathname,
targetRoutes = parentRoute?.group ?? routes;

route.group = [];

routingNodes.set(node, route);

if (parentPath) {
path = path == '/' ? '' : path;
path = parentPath.replaceAll(/[^\/a-zA-Z0-9]+$/gm, '') + path.replaceAll(/^[^\/a-zA-Z0-9]+/gm, '');
}

path = path.replaceAll(/\/+/g, '/').replace(/\/$/m, '');

route.pattern = new URLPattern({ pathname: !path ? '/' : path });

targetRoutes.push(route);
targetRoutes.sort(routeCompare);

if (!state.updateQueued) {
state.updateQueued = true;

queueMicrotask(updateRouting);
}
});

}

export function navigate(path = '') {

0 comments on commit 994921f

Please sign in to comment.