xmirror-components 1.0.36 → 1.0.37

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,3 +1,12 @@
1
1
  import type { SearchParams, SearchParamsType, UseSearchParamsOptions } from './types.ts';
2
+ declare global {
3
+ interface Window {
4
+ routerHistory: Map<string, {
5
+ path: string;
6
+ replace: boolean;
7
+ state?: Record<string, any>;
8
+ }>;
9
+ }
10
+ }
2
11
  declare const useSearchParams: <T extends SearchParamsType>(initialValue: T, options?: UseSearchParamsOptions) => SearchParams<T>;
3
12
  export default useSearchParams;
@@ -55,6 +55,8 @@ import { ConfigContext } from '../../components/ConfigProvider';
55
55
  var initialSearchParams = {
56
56
  y: undefined
57
57
  };
58
+ // 确保 routerHistory 已初始化
59
+ window.routerHistory = window.routerHistory || new Map();
58
60
  var useSearchParams = function (initialValue, options) {
59
61
  var _a;
60
62
  var defaultConfigOptions = useContext(ConfigContext).useSearchParams;
@@ -85,17 +87,33 @@ var useSearchParams = function (initialValue, options) {
85
87
  * 根据配置更新存储
86
88
  */
87
89
  var persistParams = function (params) { return __awaiter(void 0, void 0, void 0, function () {
88
- var payload;
89
- var _a;
90
- return __generator(this, function (_b) {
90
+ var payload, params_1, search, key, value;
91
+ var _a, _b;
92
+ return __generator(this, function (_c) {
91
93
  payload = __assign(__assign({}, params), { y: window.scrollY });
92
94
  if (storage === 'url') {
93
- setUrlParams(encodeParams(payload));
95
+ params_1 = encodeParams(payload);
96
+ setUrlParams(params_1);
97
+ search = '';
98
+ for (key in params_1) {
99
+ value = params_1[key];
100
+ search += "".concat(key, "=").concat(value, "&");
101
+ }
102
+ search = search.slice(0, -1);
103
+ window.routerHistory.set(location.pathname, {
104
+ path: "".concat(location.pathname, "?").concat(search),
105
+ replace: true,
106
+ });
94
107
  return [2 /*return*/];
95
108
  }
109
+ window.routerHistory.set(location.pathname, {
110
+ path: location.pathname,
111
+ replace: true,
112
+ state: __assign(__assign({}, (location.state || {})), (_a = {}, _a[stateKey] = payload, _a)),
113
+ });
96
114
  navigator(location.pathname, {
97
115
  replace: true,
98
- state: __assign(__assign({}, location.state), (_a = {}, _a[stateKey] = payload, _a))
116
+ state: __assign(__assign({}, location.state), (_b = {}, _b[stateKey] = payload, _b))
99
117
  });
100
118
  return [2 /*return*/];
101
119
  });
@@ -159,6 +177,9 @@ var useSearchParams = function (initialValue, options) {
159
177
  !navEndRef.current))) {
160
178
  setTimeout(function () {
161
179
  var targetLocation = targetLocationRef.current;
180
+ if (!targetLocation) {
181
+ return;
182
+ }
162
183
  var path = "".concat(targetLocation.pathname).concat(targetLocation.search);
163
184
  navigator(path, {
164
185
  replace: targetLocation.historyAction !== 'PUSH',
@@ -60,6 +60,8 @@ var ConfigProvider_1 = require("../../components/ConfigProvider");
60
60
  var initialSearchParams = {
61
61
  y: undefined
62
62
  };
63
+ // 确保 routerHistory 已初始化
64
+ window.routerHistory = window.routerHistory || new Map();
63
65
  var useSearchParams = function (initialValue, options) {
64
66
  var _a;
65
67
  var defaultConfigOptions = (0, react_1.useContext)(ConfigProvider_1.ConfigContext).useSearchParams;
@@ -90,17 +92,33 @@ var useSearchParams = function (initialValue, options) {
90
92
  * 根据配置更新存储
91
93
  */
92
94
  var persistParams = function (params) { return __awaiter(void 0, void 0, void 0, function () {
93
- var payload;
94
- var _a;
95
- return __generator(this, function (_b) {
95
+ var payload, params_1, search, key, value;
96
+ var _a, _b;
97
+ return __generator(this, function (_c) {
96
98
  payload = __assign(__assign({}, params), { y: window.scrollY });
97
99
  if (storage === 'url') {
98
- setUrlParams((0, util_1.encodeParams)(payload));
100
+ params_1 = (0, util_1.encodeParams)(payload);
101
+ setUrlParams(params_1);
102
+ search = '';
103
+ for (key in params_1) {
104
+ value = params_1[key];
105
+ search += "".concat(key, "=").concat(value, "&");
106
+ }
107
+ search = search.slice(0, -1);
108
+ window.routerHistory.set(location.pathname, {
109
+ path: "".concat(location.pathname, "?").concat(search),
110
+ replace: true,
111
+ });
99
112
  return [2 /*return*/];
100
113
  }
114
+ window.routerHistory.set(location.pathname, {
115
+ path: location.pathname,
116
+ replace: true,
117
+ state: __assign(__assign({}, (location.state || {})), (_a = {}, _a[stateKey] = payload, _a)),
118
+ });
101
119
  navigator(location.pathname, {
102
120
  replace: true,
103
- state: __assign(__assign({}, location.state), (_a = {}, _a[stateKey] = payload, _a))
121
+ state: __assign(__assign({}, location.state), (_b = {}, _b[stateKey] = payload, _b))
104
122
  });
105
123
  return [2 /*return*/];
106
124
  });
@@ -164,6 +182,9 @@ var useSearchParams = function (initialValue, options) {
164
182
  !navEndRef.current))) {
165
183
  setTimeout(function () {
166
184
  var targetLocation = targetLocationRef.current;
185
+ if (!targetLocation) {
186
+ return;
187
+ }
167
188
  var path = "".concat(targetLocation.pathname).concat(targetLocation.search);
168
189
  navigator(path, {
169
190
  replace: targetLocation.historyAction !== 'PUSH',
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "xmirror-components",
3
- "version": "1.0.36",
3
+ "version": "1.0.37",
4
4
  "type": "module",
5
5
  "main": "build/lib/index.js",
6
6
  "module": "build/es/index.js",
@@ -1,3 +1,12 @@
1
1
  import type { SearchParams, SearchParamsType, UseSearchParamsOptions } from './types.ts';
2
+ declare global {
3
+ interface Window {
4
+ routerHistory: Map<string, {
5
+ path: string;
6
+ replace: boolean;
7
+ state?: Record<string, any>;
8
+ }>;
9
+ }
10
+ }
2
11
  declare const useSearchParams: <T extends SearchParamsType>(initialValue: T, options?: UseSearchParamsOptions) => SearchParams<T>;
3
12
  export default useSearchParams;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "xmirror-components",
3
- "version": "1.0.36",
3
+ "version": "1.0.37",
4
4
  "type": "module",
5
5
  "main": "build/lib/index.js",
6
6
  "module": "build/es/index.js",