intentx-react-router 1.0.1 → 1.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/build/index.esm.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{createEventBus as n}from"eventbus-z";import{jsx as t,Fragment as o,jsxs as r}from"react/jsx-runtime";import{Link as e,useLocation as i,useNavigate as c,BrowserRouter as u}from"react-router-dom";import{useEffect as
|
|
1
|
+
import{createEventBus as n}from"eventbus-z";import{jsx as t,Fragment as o,jsxs as r}from"react/jsx-runtime";import{Link as e,useLocation as i,useNavigate as c,BrowserRouter as u}from"react-router-dom";import{useEffect as s}from"react";const a=n();let f=null;let l={},p=null,m=!1;const h=[],d={},T={};let E=null;const y="undefined"!=typeof window&&!0===window.__INTENT_ROUTER_DEBUG__;function A(...n){y&&console.log("[IntentRouter]",...n)}function w(n){if(!n)return"";const t=new URLSearchParams;for(const o in n){const r=n[o];null!=r&&(Array.isArray(r)?r.forEach(n=>t.append(o,String(n))):t.append(o,String(r)))}const o=t.toString();return o?`?${o}`:""}function N(n){l=n,A("intent map loaded",n)}function g(n){p=n,m||(a.$onMultiple("INTENT_NAVIGATE",U),m=!0)}function I(n){E=n}function _(n,t,o,r){A("emit",n,{params:t,query:o});const e={intent:n,params:t,query:o,...r?{options:r}:{}};a.$emit("INTENT_NAVIGATE",e),f&&f.$emit("INTENT_NAVIGATE",e)}function v(n){h.push(n)}function R(n,t){d[n]||(d[n]=[]),d[n].push(t)}function q(n,t){T[n]||(T[n]=[]),T[n].push(t)}function S(n){$(n)}async function U(n){const t=l[n.intent];if(A("receive",n.intent,n),!t)return E&&p?.(E),void console.warn("Unknown intent:",n.intent);const o=k(t,n.params);for(const t of h){const r=await t(n.intent,o);if(!1===r)return;if("string"==typeof r)return void p?.(r,n.options)}const r=d[n.intent]||[];for(const t of r){const r=await t(n.intent,o);if(!1===r)return;if("string"==typeof r)return void p?.(r,n.options)}$(n.intent);const e=G(t,o)+w(n.query);A("navigate",{intent:n.intent,params:o,query:n.query,url:e}),p?.(e,n.options),f&&f.$emit("INTENT_NAVIGATE",n)}function $(n){const t=T[n];if(t)for(const n of t)try{const t=n();t instanceof Promise&&t.catch(()=>{})}catch{}}function k(n,t){const o=function(n){const t=[];return n.replace(/:([A-Za-z0-9_]+)/g,(n,o)=>(t.push(o),"")),t}(n);if(!t)return{};if(Array.isArray(t)){const n={};return o.forEach((o,r)=>{void 0===t[r]&&console.warn(`Missing param "${o}"`),n[o]=t[r]}),n}return"object"==typeof t?t:1===o.length?{[o[0]]:t}:{}}function G(n,t){return n.replace(/:([A-Za-z0-9_]+)/g,(n,o)=>{const r=t[o];return void 0!==r?encodeURIComponent(r):""})}function M(n,t,o){const r=l[n];if(!r)return"";return G(r,k(r,t))+w(o)}function V(n){const[t,o]=n.split("?"),r=function(n){if(!n)return{};const t=new URLSearchParams(n),o={};return t.forEach((n,t)=>{o[t]?Array.isArray(o[t])?o[t].push(n):o[t]=[o[t],n]:o[t]=n}),o}(o);for(const n in l){const o=l[n],e=[],i=new RegExp("^"+o.replace(/:([A-Za-z0-9_]+)/g,(n,t)=>(e.push(t),"([^/]+)"))+"$"),c=t.match(i);if(!c)continue;const u={};return e.forEach((n,t)=>{u[n]=decodeURIComponent(c[t+1])}),{intent:n,params:u,query:r}}return null}function z(n){f=n,f.$onMultiple("INTENT_NAVIGATE",U)}function C({intent:n,params:r,query:i,scrollTop:c,prefetch:u,onClick:s,onMouseEnter:a,onTouchStart:f,...l}){const p=M(n,r,i);return p?t(e,{to:p,onClick:n=>{c&&window.scrollTo({top:0}),s?.(n)},onMouseEnter:t=>{u&&S(n),a?.(t)},onTouchStart:t=>{u&&S(n),f?.(t)},...l}):(console.warn("IntentLink: unknown intent",n),t(o,{children:l.children}))}function L(){const n=i(),t=V(n.pathname),o=new URLSearchParams(n.search),r={};return o.forEach((n,t)=>{r[t]=n}),{intent:t?.intent??null,params:t?.params??{},query:r}}function P({intent:n,component:r,fallback:e=null,loading:i,guard:c}){const u=L();return u?u.intent!==n?null:c&&!c(u)?t(o,{children:e}):t(r,{...u}):i?t(o,{children:i}):null}function b({navigate:n}){const t=c();return s(()=>{g(n??t)},[n,t]),null}function x({children:n}){return r(u,{children:[t(b,{}),n]})}function Z(){const n=c();return s(()=>{g(n)},[n]),_}export{C as IntentLink,P as IntentRoute,x as IntentRouter,b as RouterBinder,v as addIntentGuard,R as addIntentGuardFor,q as addIntentPreload,g as bindNavigate,N as createIntentRouter,M as generatePathFromIntent,_ as navigateIntent,S as preloadIntent,V as resolveIntentFromUrl,I as setFallbackPath,z as setSharedBus,L as useIntentRouter,Z as useNavigateIntent};
|
package/build/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";var n=require("eventbus-z"),t=require("react/jsx-runtime"),e=require("react-router-dom"),r=require("react");const o=n.createEventBus();let i=null;let s={},c=null,u=!1;const a=[],
|
|
1
|
+
"use strict";var n=require("eventbus-z"),t=require("react/jsx-runtime"),e=require("react-router-dom"),r=require("react");const o=n.createEventBus();let i=null;let s={},c=null,u=!1;const a=[],f={},l={};let p=null;const d="undefined"!=typeof window&&!0===window.__INTENT_ROUTER_DEBUG__;function h(...n){d&&console.log("[IntentRouter]",...n)}function m(n){if(!n)return"";const t=new URLSearchParams;for(const e in n){const r=n[e];null!=r&&(Array.isArray(r)?r.forEach(n=>t.append(e,String(n))):t.append(e,String(r)))}const e=t.toString();return e?`?${e}`:""}function I(n){c=n,u||(o.$onMultiple("INTENT_NAVIGATE",E),u=!0)}function x(n,t,e,r){h("emit",n,{params:t,query:e});const s={intent:n,params:t,query:e,...r?{options:r}:{}};o.$emit("INTENT_NAVIGATE",s),i&&i.$emit("INTENT_NAVIGATE",s)}function g(n){T(n)}async function E(n){const t=s[n.intent];if(h("receive",n.intent,n),!t)return p&&c?.(p),void console.warn("Unknown intent:",n.intent);const e=N(t,n.params);for(const t of a){const r=await t(n.intent,e);if(!1===r)return;if("string"==typeof r)return void c?.(r,n.options)}const r=f[n.intent]||[];for(const t of r){const r=await t(n.intent,e);if(!1===r)return;if("string"==typeof r)return void c?.(r,n.options)}T(n.intent);const o=y(t,e)+m(n.query);h("navigate",{intent:n.intent,params:e,query:n.query,url:o}),c?.(o,n.options),i&&i.$emit("INTENT_NAVIGATE",n)}function T(n){const t=l[n];if(t)for(const n of t)try{const t=n();t instanceof Promise&&t.catch(()=>{})}catch{}}function N(n,t){const e=function(n){const t=[];return n.replace(/:([A-Za-z0-9_]+)/g,(n,e)=>(t.push(e),"")),t}(n);if(!t)return{};if(Array.isArray(t)){const n={};return e.forEach((e,r)=>{void 0===t[r]&&console.warn(`Missing param "${e}"`),n[e]=t[r]}),n}return"object"==typeof t?t:1===e.length?{[e[0]]:t}:{}}function y(n,t){return n.replace(/:([A-Za-z0-9_]+)/g,(n,e)=>{const r=t[e];return void 0!==r?encodeURIComponent(r):""})}function A(n,t,e){const r=s[n];if(!r)return"";return y(r,N(r,t))+m(e)}function w(n){const[t,e]=n.split("?"),r=function(n){if(!n)return{};const t=new URLSearchParams(n),e={};return t.forEach((n,t)=>{e[t]?Array.isArray(e[t])?e[t].push(n):e[t]=[e[t],n]:e[t]=n}),e}(e);for(const n in s){const e=s[n],o=[],i=new RegExp("^"+e.replace(/:([A-Za-z0-9_]+)/g,(n,t)=>(o.push(t),"([^/]+)"))+"$"),c=t.match(i);if(!c)continue;const u={};return o.forEach((n,t)=>{u[n]=decodeURIComponent(c[t+1])}),{intent:n,params:u,query:r}}return null}function v(){const n=e.useLocation(),t=w(n.pathname),r=new URLSearchParams(n.search),o={};return r.forEach((n,t)=>{o[t]=n}),{intent:t?.intent??null,params:t?.params??{},query:o}}function R({navigate:n}){const t=e.useNavigate();return r.useEffect(()=>{I(n??t)},[n,t]),null}exports.IntentLink=function({intent:n,params:r,query:o,scrollTop:i,prefetch:s,onClick:c,onMouseEnter:u,onTouchStart:a,...f}){const l=A(n,r,o);return l?t.jsx(e.Link,{to:l,onClick:n=>{i&&window.scrollTo({top:0}),c?.(n)},onMouseEnter:t=>{s&&g(n),u?.(t)},onTouchStart:t=>{s&&g(n),a?.(t)},...f}):(console.warn("IntentLink: unknown intent",n),t.jsx(t.Fragment,{children:f.children}))},exports.IntentRoute=function({intent:n,component:e,fallback:r=null,loading:o,guard:i}){const s=v();return s?s.intent!==n?null:i&&!i(s)?t.jsx(t.Fragment,{children:r}):t.jsx(e,{...s}):o?t.jsx(t.Fragment,{children:o}):null},exports.IntentRouter=function({children:n}){return t.jsxs(e.BrowserRouter,{children:[t.jsx(R,{}),n]})},exports.RouterBinder=R,exports.addIntentGuard=function(n){a.push(n)},exports.addIntentGuardFor=function(n,t){f[n]||(f[n]=[]),f[n].push(t)},exports.addIntentPreload=function(n,t){l[n]||(l[n]=[]),l[n].push(t)},exports.bindNavigate=I,exports.createIntentRouter=function(n){s=n,h("intent map loaded",n)},exports.generatePathFromIntent=A,exports.navigateIntent=x,exports.preloadIntent=g,exports.resolveIntentFromUrl=w,exports.setFallbackPath=function(n){p=n},exports.setSharedBus=function(n){i=n,i.$onMultiple("INTENT_NAVIGATE",E)},exports.useIntentRouter=v,exports.useNavigateIntent=function(){const n=e.useNavigate();return r.useEffect(()=>{I(n)},[n]),x};
|
|
@@ -1,10 +1,10 @@
|
|
|
1
|
+
import { type LinkProps } from "react-router-dom";
|
|
1
2
|
type Props = {
|
|
2
3
|
intent: string;
|
|
3
4
|
params?: any;
|
|
4
5
|
query?: Record<string, any>;
|
|
5
|
-
children: React.ReactNode;
|
|
6
|
-
replace?: boolean;
|
|
7
6
|
scrollTop?: boolean;
|
|
8
|
-
|
|
9
|
-
|
|
7
|
+
prefetch?: boolean;
|
|
8
|
+
} & Omit<LinkProps, "to" | "prefetch">;
|
|
9
|
+
export declare function IntentLink({ intent, params, query, scrollTop, prefetch, onClick, onMouseEnter, onTouchStart, ...rest }: Props): JSX.Element;
|
|
10
10
|
export {};
|
|
@@ -2,8 +2,8 @@ type Props = {
|
|
|
2
2
|
intent: string;
|
|
3
3
|
component: React.ComponentType<any>;
|
|
4
4
|
fallback?: React.ReactNode;
|
|
5
|
-
guard?: (data: any) => boolean;
|
|
6
5
|
loading?: React.ReactNode;
|
|
6
|
+
guard?: (data: any) => boolean;
|
|
7
7
|
};
|
|
8
|
-
export declare function IntentRoute({ intent, component: Component, fallback, guard,
|
|
8
|
+
export declare function IntentRoute({ intent, component: Component, fallback, loading, guard, }: Props): JSX.Element | null;
|
|
9
9
|
export {};
|