Skip to content

Commit

Permalink
test
Browse files Browse the repository at this point in the history
  • Loading branch information
KevinBLT committed Mar 10, 2024
1 parent dbe0bed commit 9932be3
Showing 1 changed file with 31 additions and 11 deletions.
42 changes: 31 additions & 11 deletions packages/@hec.js/ui/lib/src/routing.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,23 @@ export const route = signal(location.pathname);

const _pushState = window.history.pushState,
_replaceState = window.history.replaceState,
routeQueue = [],
state = { updateQueued: false };

new MutationObserver((mutations) => {

for (const m of mutations) {
for (const n of m.addedNodes) {
console.log(n, n.dataset?.route);
}
}

}).observe(document, {
childList: true,
subtree: true
});


/**
* @typedef {{
* pattern?: URLPattern,
Expand Down Expand Up @@ -43,32 +58,37 @@ export function addRoute(route) {
let node = route.node,
path = route.path,
placeholder = route.placeholder,
parent = (node.parentElement ?? placeholder?.parentElement)?.closest('[data-route]'),
parentNode = (node.parentElement ?? placeholder?.parentElement),
parent = parentNode?.closest('[data-route]'),
parentRoute = routingNodes.get(parent),
parentPath = parentRoute?.pattern?.pathname,
targetRoutes = parentRoute?.group ?? routes;

route.group = [];
if (!parentNode) {
return routeQueue.push(route);
}

route.group = [];

routingNodes.set(node, route);
routingNodes.set(node, route);

if (parentPath) {
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, '');
path = path.replaceAll(/\/+/g, '/').replace(/\/$/m, '');

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

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

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

queueMicrotask(updateRouting);
}
}
}

export function navigate(path = '') {
Expand Down

0 comments on commit 9932be3

Please sign in to comment.