@ossy/router-react 0.0.1-beta.2 → 0.0.1-beta.3
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.
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import React from 'react';
|
|
2
|
-
import { MultiLanguagePage, RouterOptions, SingleLanguagePage } from '@ossy
|
|
2
|
+
import { MultiLanguagePage, RouterOptions, SingleLanguagePage } from '@ossy/router';
|
|
3
3
|
export declare const RouterContext: React.Context<{
|
|
4
4
|
pages: RouterPage[];
|
|
5
5
|
defaultLanguage: string | undefined;
|
package/build/cjs/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";var e=require("react"),n=require("@ossy
|
|
1
|
+
"use strict";var e=require("react"),n=require("@ossy/router"),r=function(){return r=Object.assign||function(e){for(var n,r=1,t=arguments.length;r<t;r++)for(var a in n=arguments[r])Object.prototype.hasOwnProperty.call(n,a)&&(e[a]=n[a]);return e},r.apply(this,arguments)};"function"==typeof SuppressedError&&SuppressedError;var t=e.createContext({pages:[],defaultLanguage:void 0,supportedLanguages:[]}),a=function(e){return void 0===e&&(e=""),function(e){return"/"===e[e.length-1]?e:"".concat(e,"/")}(function(e){return"/"===e[0]?e:"/".concat(e)}(e))};exports.Router=function(o){var i,u,l,d,c,s=o.url,f=o.pages,g=void 0===f?[]:f,p=o.defaultLanguage,v=void 0===p?void 0:p,w=o.supportedLanguages,h=void 0===w?[]:w;if(s){var m=s.match(/([^?#]*)(\?[^#]*)?(#.*)?/);u=s,l=(null==m?void 0:m[1])||"",d=(null==m?void 0:m[3])||"",c=(null==m?void 0:m[2])||""}else{if("undefined"==typeof window)return e.createElement(e.Fragment,null);u=window.location.href,l=window.location.pathname,c=(null==(d=window.location.hash)?void 0:d.includes("?"))?d.split("?")[1]:window.location.search}var y=h.length>1&&!!v,b=l.split("/")[1]||"",L=h.includes(b)?b:v,k=n.Router.of({pages:g,defaultLanguage:v,supportedLanguages:h}),P=k.getParamsFromUrl(u),R=new URLSearchParams(c),S=k.getPageByUrl(u);if(S||(y&&"/"===l&&"undefined"!=typeof window&&(window.location.href="/"+v),S=y?g.find((function(e){return"*"===a(e.path[L])})):g.find((function(e){return"*"===a(e.path)}))),!S)return console.warn("[RouterReact] No active page found for",l),e.createElement(e.Fragment,null);e.useCallback((function(e,n){if("undefined"!=typeof window){window.history.pushState({},"","".concat(l,"?").concat(R.toString()));var r=new URLSearchParams(R);r.set(e,n),history.pushState({},"","?".concat(r.toString()))}}),[R]);var C=e.useCallback((function(e){var n,r,t,a,o,i={};if("string"==typeof e?(n=e,i=P,r=L):(o=e).id||void 0===o.language?(n=(null==e?void 0:e.id)||S.id,i=(null==e?void 0:e.params)||P,r=(null==e?void 0:e.language)||L):(n=S.id,i=P,r=e.language),n.includes("#")){var u=n.split("#");if(n=u[0],(t=u[1]).includes("?")){var l=n.split("?");t=l[0],a=l[1]}}else if(n.includes("?")){var d=n.split("?");n=d[0],a=d[1]}var c=k.getPathname({id:n.replace("@",""),params:i,language:r});return t&&(c=c+"#"+t),a&&(c=c+"?"+a),c}),[k,L]),x=e.useCallback((function(e){if("undefined"!=typeof window&&e)if("@back"!==e){var n=C(e);n&&(window.location.href=n)}else window.history.back()}),[C]),E=e.useCallback((function(){"undefined"!=typeof window&&window.history.back()}),[]),U={href:u,pages:g,language:L,defaultLanguage:v,supportedLanguages:h,params:P,searchParams:r({},Array.from(R.entries()).reduce((function(e,n){var t,a=n[0],o=n[1];return r(r({},e),((t={})[a]=o,t))}),{})),navigate:x,getHref:C,back:E};(null==S?void 0:S.redirect)&&x({id:S.redirect});var F=(null==S?void 0:S.element)||(null===(i=null==S?void 0:S.render)||void 0===i?void 0:i.call(S));return e.createElement(t.Provider,{value:U},F)},exports.RouterContext=t,exports.useRouter=function(){return e.useContext(t)};
|
package/build/esm/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import e,{createContext as n,useContext as r,useCallback as a}from"react";import{Router as t}from"@ossy
|
|
1
|
+
import e,{createContext as n,useContext as r,useCallback as a}from"react";import{Router as t}from"@ossy/router";var o=function(){return o=Object.assign||function(e){for(var n,r=1,a=arguments.length;r<a;r++)for(var t in n=arguments[r])Object.prototype.hasOwnProperty.call(n,t)&&(e[t]=n[t]);return e},o.apply(this,arguments)};"function"==typeof SuppressedError&&SuppressedError;var i=n({pages:[],defaultLanguage:void 0,supportedLanguages:[]}),u=function(){return r(i)},d=function(e){return void 0===e&&(e=""),function(e){return"/"===e[e.length-1]?e:"".concat(e,"/")}(function(e){return"/"===e[0]?e:"/".concat(e)}(e))},l=function(n){var r,u,l,c,s,f=n.url,g=n.pages,p=void 0===g?[]:g,v=n.defaultLanguage,w=void 0===v?void 0:v,h=n.supportedLanguages,m=void 0===h?[]:h;if(f){var y=f.match(/([^?#]*)(\?[^#]*)?(#.*)?/);u=f,l=(null==y?void 0:y[1])||"",c=(null==y?void 0:y[3])||"",s=(null==y?void 0:y[2])||""}else{if("undefined"==typeof window)return e.createElement(e.Fragment,null);u=window.location.href,l=window.location.pathname,s=(null==(c=window.location.hash)?void 0:c.includes("?"))?c.split("?")[1]:window.location.search}var L=m.length>1&&!!w,P=l.split("/")[1]||"",S=m.includes(P)?P:w,b=t.of({pages:p,defaultLanguage:w,supportedLanguages:m}),E=b.getParamsFromUrl(u),R=new URLSearchParams(s),k=b.getPageByUrl(u);if(k||(L&&"/"===l&&"undefined"!=typeof window&&(window.location.href="/"+w),k=L?p.find((function(e){return"*"===d(e.path[S])})):p.find((function(e){return"*"===d(e.path)}))),!k)return console.warn("[RouterReact] No active page found for",l),e.createElement(e.Fragment,null);a((function(e,n){if("undefined"!=typeof window){window.history.pushState({},"","".concat(l,"?").concat(R.toString()));var r=new URLSearchParams(R);r.set(e,n),history.pushState({},"","?".concat(r.toString()))}}),[R]);var U=a((function(e){var n,r,a,t,o,i={};if("string"==typeof e?(n=e,i=E,r=S):(o=e).id||void 0===o.language?(n=(null==e?void 0:e.id)||k.id,i=(null==e?void 0:e.params)||E,r=(null==e?void 0:e.language)||S):(n=k.id,i=E,r=e.language),n.includes("#")){var u=n.split("#");if(n=u[0],(a=u[1]).includes("?")){var d=n.split("?");a=d[0],t=d[1]}}else if(n.includes("?")){var l=n.split("?");n=l[0],t=l[1]}var c=b.getPathname({id:n.replace("@",""),params:i,language:r});return a&&(c=c+"#"+a),t&&(c=c+"?"+t),c}),[b,S]),F=a((function(e){if("undefined"!=typeof window&&e)if("@back"!==e){var n=U(e);n&&(window.location.href=n)}else window.history.back()}),[U]),O=a((function(){"undefined"!=typeof window&&window.history.back()}),[]),j={href:u,pages:p,language:S,defaultLanguage:w,supportedLanguages:m,params:E,searchParams:o({},Array.from(R.entries()).reduce((function(e,n){var r,a=n[0],t=n[1];return o(o({},e),((r={})[a]=t,r))}),{})),navigate:F,getHref:U,back:O};(null==k?void 0:k.redirect)&&F({id:k.redirect});var x=(null==k?void 0:k.element)||(null===(r=null==k?void 0:k.render)||void 0===r?void 0:r.call(k));return e.createElement(i.Provider,{value:j},x)};export{l as Router,i as RouterContext,u as useRouter};
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import React from 'react';
|
|
2
|
-
import { MultiLanguagePage, RouterOptions, SingleLanguagePage } from '@ossy
|
|
2
|
+
import { MultiLanguagePage, RouterOptions, SingleLanguagePage } from '@ossy/router';
|
|
3
3
|
export declare const RouterContext: React.Context<{
|
|
4
4
|
pages: RouterPage[];
|
|
5
5
|
defaultLanguage: string | undefined;
|
package/package.json
CHANGED
package/src/router-react.tsx
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
'use client'
|
|
2
2
|
import React, { createContext, useContext, useCallback } from 'react'
|
|
3
|
-
import { MultiLanguagePage, Router as OssyRouter, RouterOptions, SingleLanguagePage, Page as _Page } from '@ossy
|
|
3
|
+
import { MultiLanguagePage, Router as OssyRouter, RouterOptions, SingleLanguagePage, Page as _Page } from '@ossy/router'
|
|
4
4
|
|
|
5
5
|
export const RouterContext = createContext({
|
|
6
6
|
pages: [] as RouterPage[],
|