From 9932be32526f00d47d6b3bb0818d30350eb6cc95 Mon Sep 17 00:00:00 2001 From: Kevin Bulteel Date: Sun, 10 Mar 2024 16:21:29 +0100 Subject: [PATCH] test --- packages/@hec.js/ui/lib/src/routing.js | 42 +++++++++++++++++++------- 1 file changed, 31 insertions(+), 11 deletions(-) diff --git a/packages/@hec.js/ui/lib/src/routing.js b/packages/@hec.js/ui/lib/src/routing.js index 29d5c76..a437ee5 100644 --- a/packages/@hec.js/ui/lib/src/routing.js +++ b/packages/@hec.js/ui/lib/src/routing.js @@ -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, @@ -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 = '') {