swico 0.9.0-beta → 0.9.0-beta10

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "swico",
3
- "version": "0.9.0-beta",
3
+ "version": "0.9.0-beta10",
4
4
  "description": "Simple, efficient, and practical front-end framework",
5
5
  "main": "./src/index.js",
6
6
  "types": "./src/index.d.ts",
@@ -1,6 +1,5 @@
1
1
  import { ConfigRouterType } from '../../../utils/config';
2
2
  import { SwicoHistoryType } from '../../../typings/global-type';
3
- export { unstable_HistoryRouter as HistoryRouter } from 'react-router-dom';
4
3
  export declare const compareURLPatterns: (urlPatternWithValues: string, urlPatternWithParams: string) => boolean;
5
4
  export declare const interpolatePath: (pathTemplate: string, params: Record<string, any>) => string;
6
5
  export declare const formatAncPath: (ancPath: string) => string;
@@ -9,4 +8,4 @@ export declare const pathNameList: {
9
8
  name: string;
10
9
  }[];
11
10
  export declare let history: SwicoHistoryType;
12
- export declare const getOriHistory: (routerType: ConfigRouterType['type']) => import("history").HashHistory;
11
+ export declare const getRouter: (routerType: ConfigRouterType['type']) => import("@remix-run/router").Router;
@@ -3,13 +3,15 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
3
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.getOriHistory = exports.history = exports.pathNameList = exports.formatAncPath = exports.interpolatePath = exports.compareURLPatterns = exports.HistoryRouter = void 0;
6
+ exports.getRouter = exports.history = exports.pathNameList = exports.formatAncPath = exports.interpolatePath = exports.compareURLPatterns = void 0;
7
+ var index_1 = require("./index");
7
8
  var qs_1 = __importDefault(require("qs"));
8
9
  var history_1 = require("history");
9
10
  var routes_1 = __importDefault(require("./routes"));
10
11
  var config_1 = require("./config");
12
+ var layout_1 = __importDefault(require("../../layout"));
11
13
  var react_router_dom_1 = require("react-router-dom");
12
- Object.defineProperty(exports, "HistoryRouter", { enumerable: true, get: function () { return react_router_dom_1.unstable_HistoryRouter; } });
14
+ var react_1 = __importDefault(require("react"));
13
15
  var lastIndexBase = config_1.routerBase[config_1.routerBase.length - 1];
14
16
  var formatRouterBase = lastIndexBase === '/' ? config_1.routerBase.slice(0, config_1.routerBase.length - 1) : config_1.routerBase;
15
17
  var compareURLPatterns = function (urlPatternWithValues, urlPatternWithParams) {
@@ -86,13 +88,14 @@ var getPathNameList = function (routes) {
86
88
  return list;
87
89
  };
88
90
  exports.pathNameList = getPathNameList(routes_1.default);
89
- var getLocation = function (originalHistory) {
91
+ var getLocation = function (originalHistory, state) {
90
92
  var _a = originalHistory.location, pathname = _a.pathname, hash = _a.hash, search = _a.search;
91
93
  var routerBaseLength = formatRouterBase.length;
92
94
  var path = pathname.slice(routerBaseLength);
93
95
  var matchPathNameItem = exports.pathNameList.find(function (item) { return item.path === path || (0, exports.compareURLPatterns)(path, item.path); });
94
96
  var params = interpolatePathParams(matchPathNameItem === null || matchPathNameItem === void 0 ? void 0 : matchPathNameItem.path, path);
95
97
  return {
98
+ state: state !== null && state !== void 0 ? state : {},
96
99
  hash: hash,
97
100
  search: search,
98
101
  pathname: pathname,
@@ -102,22 +105,21 @@ var getLocation = function (originalHistory) {
102
105
  path: path
103
106
  };
104
107
  };
105
- var getFormatHistoryOption = function (to, formatRouterBase, pathList, type) {
108
+ var getFormatHistoryOption = function (to, pathList, type) {
106
109
  var _a;
107
- var params = to.params, path = to.path, hash = to.hash, name = to.name, query = to.query, state = to.state;
110
+ var params = to.params, path = to.path, hash = to.hash, name = to.name, query = to.query;
108
111
  var obj = {
109
112
  hash: hash,
110
- state: state,
111
113
  search: query ? qs_1.default.stringify(query) : undefined
112
114
  };
113
115
  if (path) {
114
- obj.pathname = "".concat(formatRouterBase).concat(path);
116
+ obj.pathname = path;
115
117
  }
116
118
  if (name) {
117
119
  var fullPath = (_a = pathList.find(function (item) { return item.name === name; })) === null || _a === void 0 ? void 0 : _a.path;
118
120
  var targetPath = fullPath ? (0, exports.interpolatePath)(fullPath, params) : null;
119
121
  if (targetPath) {
120
- obj.pathname = "".concat(formatRouterBase).concat(targetPath);
122
+ obj.pathname = targetPath;
121
123
  }
122
124
  else {
123
125
  throw "An error occurred while executing 'history.".concat(type, "' operation: The path for the name \"").concat(name, "\" could not be found");
@@ -125,21 +127,35 @@ var getFormatHistoryOption = function (to, formatRouterBase, pathList, type) {
125
127
  }
126
128
  return obj;
127
129
  };
128
- var getOriHistory = function (routerType) {
130
+ var getRouter = function (routerType) {
129
131
  var _a;
130
- var originalHistory = (_a = (routerType === 'hash' ? history_1.createHashHistory : history_1.createBrowserHistory)) === null || _a === void 0 ? void 0 : _a();
132
+ var routeList = [
133
+ {
134
+ element: react_1.default.createElement(layout_1.default, null),
135
+ path: '/',
136
+ children: (0, index_1.getChildrenRouteList)(routes_1.default, '')
137
+ }
138
+ ];
139
+ var router = (routerType === 'hash' ? react_router_dom_1.createHashRouter : react_router_dom_1.createBrowserRouter)(routeList);
140
+ var oriHistory = (_a = (routerType === 'hash' ? history_1.createHashHistory : history_1.createBrowserHistory)) === null || _a === void 0 ? void 0 : _a();
131
141
  exports.history = {
132
- go: originalHistory.go,
133
- forward: originalHistory.forward,
134
- back: originalHistory.back,
142
+ go: function (delta) {
143
+ router.navigate(delta);
144
+ },
145
+ forward: function () {
146
+ router.navigate(1);
147
+ },
148
+ back: function () {
149
+ router.navigate(-1);
150
+ },
135
151
  push: function (to) {
136
152
  switch (typeof to) {
137
153
  case 'string':
138
- originalHistory.push("".concat(formatRouterBase).concat(to));
154
+ router.navigate(to);
139
155
  break;
140
156
  case 'object':
141
- var formatOption = getFormatHistoryOption(to, formatRouterBase, exports.pathNameList, 'push');
142
- originalHistory.push(formatOption);
157
+ var formatOption = getFormatHistoryOption(to, exports.pathNameList, 'push');
158
+ router.navigate(formatOption, { state: to.state });
143
159
  break;
144
160
  default:
145
161
  throw "An error occurred while executing history.push operation: unexpected type of 'to':".concat(typeof to);
@@ -148,20 +164,20 @@ var getOriHistory = function (routerType) {
148
164
  replace: function (to) {
149
165
  switch (typeof to) {
150
166
  case 'string':
151
- originalHistory.replace("".concat(formatRouterBase).concat(to));
167
+ router.navigate(to, { replace: true });
152
168
  break;
153
169
  case 'object':
154
- var formatOption = getFormatHistoryOption(to, formatRouterBase, exports.pathNameList, 'replace');
155
- originalHistory.replace(formatOption);
170
+ var formatOption = getFormatHistoryOption(to, exports.pathNameList, 'replace');
171
+ router.navigate(formatOption, { state: to.state, replace: true });
156
172
  break;
157
173
  default:
158
174
  throw "An error occurred while executing history.replace operation: unexpected type of 'to':".concat(typeof to);
159
175
  }
160
176
  },
161
177
  get location() {
162
- return getLocation(originalHistory);
178
+ return getLocation(oriHistory, router.state);
163
179
  }
164
180
  };
165
- return originalHistory;
181
+ return router;
166
182
  };
167
- exports.getOriHistory = getOriHistory;
183
+ exports.getRouter = getRouter;
@@ -9,3 +9,4 @@ export type RoutesItemType = {
9
9
  redirect?: string;
10
10
  name?: string;
11
11
  };
12
+ export declare const getChildrenRouteList: (childrenRoutes: RoutesItemType[], ancPathKey: string) => any;
@@ -26,32 +26,35 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
26
26
  return (mod && mod.__esModule) ? mod : { "default": mod };
27
27
  };
28
28
  Object.defineProperty(exports, "__esModule", { value: true });
29
+ exports.getChildrenRouteList = void 0;
29
30
  var client_1 = require("react-dom/client");
30
31
  var react_1 = __importStar(require("react"));
31
32
  var react_router_dom_1 = require("react-router-dom");
32
- var routes_1 = __importDefault(require("./routes"));
33
33
  var loading_1 = __importDefault(require("../loading"));
34
34
  var history_1 = require("./history");
35
35
  var config_1 = require("./config");
36
36
  require("../../global");
37
- var layout_1 = __importDefault(require("../../layout"));
38
- var renderChildrenRouteList = function (childrenRoutes, ancPathKey) {
37
+ var getChildrenRouteList = function (childrenRoutes, ancPathKey) {
39
38
  return childrenRoutes === null || childrenRoutes === void 0 ? void 0 : childrenRoutes.map(function (item) {
40
39
  var component = item.component, path = item.path, children = item.children, redirect = item.redirect;
41
40
  var newAncPathKey = "".concat(ancPathKey, "-").concat(path);
42
- return (react_1.default.createElement(react_router_dom_1.Route, { element: redirect ? (react_1.default.createElement(react_router_dom_1.Navigate, { to: redirect, replace: true })) : component ? (react_1.default.createElement(react_1.Suspense, { fallback: (0, react_1.createElement)(loading_1.default) }, (0, react_1.createElement)((0, react_1.lazy)(component)))) : (react_1.default.createElement(react_router_dom_1.Outlet, null)), key: newAncPathKey, path: path }, renderChildrenRouteList(children, newAncPathKey)));
41
+ return {
42
+ element: redirect ? (react_1.default.createElement(react_router_dom_1.Navigate, { to: redirect, replace: true })) : component ? (react_1.default.createElement(react_1.Suspense, { fallback: react_1.default.createElement(loading_1.default, null) }, (0, react_1.createElement)((0, react_1.lazy)(component)))) : (react_1.default.createElement(react_router_dom_1.Outlet, null)),
43
+ key: newAncPathKey,
44
+ path: path.startsWith('/') ? path.slice(1) : path,
45
+ children: (0, exports.getChildrenRouteList)(children, newAncPathKey)
46
+ };
43
47
  });
44
48
  };
49
+ exports.getChildrenRouteList = getChildrenRouteList;
45
50
  var App = function () {
46
- var originalHistory = (0, history_1.getOriHistory)(config_1.routerType);
51
+ var router = (0, history_1.getRouter)(config_1.routerType);
47
52
  if (config_1.routerBase && config_1.routerBase !== '/') {
48
53
  if (config_1.routerType === 'browser' && window.location.pathname === '/') {
49
54
  window.location.replace("".concat(config_1.routerBase));
50
55
  }
51
56
  }
52
- return (react_1.default.createElement(history_1.HistoryRouter, { basename: config_1.routerBase, history: originalHistory },
53
- react_1.default.createElement(react_router_dom_1.Routes, null,
54
- react_1.default.createElement(react_router_dom_1.Route, { element: react_1.default.createElement(layout_1.default, null), path: '' }, renderChildrenRouteList(routes_1.default, '')))));
57
+ return react_1.default.createElement(react_router_dom_1.RouterProvider, { router: router, basename: config_1.routerBase });
55
58
  };
56
59
  var root = (0, client_1.createRoot)(document.getElementById('root'));
57
60
  root.render(react_1.default.createElement(App, null));
@@ -1,4 +1,15 @@
1
1
  "use strict";
2
+ var __assign = (this && this.__assign) || function () {
3
+ __assign = Object.assign || function(t) {
4
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
5
+ s = arguments[i];
6
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
7
+ t[p] = s[p];
8
+ }
9
+ return t;
10
+ };
11
+ return __assign.apply(this, arguments);
12
+ };
2
13
  var __importDefault = (this && this.__importDefault) || function (mod) {
3
14
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
15
  };
@@ -11,7 +22,7 @@ var useLocation = function () {
11
22
  var _a, _b;
12
23
  var location = (0, react_router_dom_1.useLocation)();
13
24
  var params = (0, react_router_dom_1.useParams)();
14
- var search = location.search, hash = location.hash, pathname = location.pathname;
25
+ var search = location.search, hash = location.hash, pathname = location.pathname, state = location.state;
15
26
  var name = (_a = history_1.pathNameList.find(function (item) {
16
27
  return (Object.keys(params).length > 0 && (0, history_1.compareURLPatterns)(pathname, item.path)) ||
17
28
  item.path === pathname;
@@ -19,6 +30,7 @@ var useLocation = function () {
19
30
  var query = search ? qs_1.default.parse(search.startsWith('?') ? search.slice(1) : search) : {};
20
31
  return {
21
32
  name: name,
33
+ state: state !== null && state !== void 0 ? state : {},
22
34
  path: pathname,
23
35
  pathname: (_b = window === null || window === void 0 ? void 0 : window.location) === null || _b === void 0 ? void 0 : _b.pathname,
24
36
  search: '',
@@ -49,7 +61,7 @@ var useNav = function () {
49
61
  navigate(to);
50
62
  break;
51
63
  case 'object':
52
- navigate(getFormatNavPath(to), options);
64
+ navigate(getFormatNavPath(to), __assign(__assign({}, (options || {})), { state: to.state }));
53
65
  break;
54
66
  default:
55
67
  throw "An error occurred while executing useNav() operation: unexpected type of 'to':".concat(typeof to);
@@ -36,9 +36,10 @@ var getRouter = function () {
36
36
  back: router.back,
37
37
  forward: router.forward,
38
38
  get location() {
39
+ var _a;
39
40
  var lastIndexBase = config_1.routerBase[config_1.routerBase.length - 1];
40
41
  var formatRouterBase = lastIndexBase === '/' ? config_1.routerBase.slice(0, config_1.routerBase.length - 1) : config_1.routerBase;
41
- var _a = router.currentRoute.value, path = _a.path, params = _a.params, query = _a.query, name = _a.name, hash = _a.hash, fullPath = _a.fullPath;
42
+ var _b = router.currentRoute.value, path = _b.path, params = _b.params, query = _b.query, name = _b.name, hash = _b.hash, fullPath = _b.fullPath;
42
43
  return {
43
44
  path: path,
44
45
  pathname: formatRouterBase + path,
@@ -46,6 +47,7 @@ var getRouter = function () {
46
47
  query: query,
47
48
  name: name,
48
49
  hash: hash,
50
+ state: (_a = window.history.state) !== null && _a !== void 0 ? _a : {},
49
51
  search: query ? qs_1.default.stringify(query) : ''
50
52
  };
51
53
  }
@@ -19,6 +19,7 @@ var useLocation = function () {
19
19
  name: '',
20
20
  path: '',
21
21
  search: '',
22
+ state: {},
22
23
  hash: '',
23
24
  pathname: '',
24
25
  params: {},
@@ -34,6 +35,7 @@ var useLocation = function () {
34
35
  location.query = query;
35
36
  location.hash = hash;
36
37
  location.params = params;
38
+ location.state = window.history.state || {};
37
39
  });
38
40
  return location;
39
41
  };
@@ -6,6 +6,7 @@ export type SwicoLocationType = {
6
6
  path: string;
7
7
  pathname: string;
8
8
  search: string;
9
+ state?: Record<string, any>;
9
10
  };
10
11
  export type SwicoHistoryOptionType = {
11
12
  query?: Record<string, any>;