@tinkoff/router 0.2.6 → 0.2.8
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/README.md +2 -276
- package/lib/components/react/context.browser.js +7 -0
- package/lib/components/react/context.es.js +7 -0
- package/lib/components/react/context.js +13 -0
- package/lib/components/react/provider.browser.js +12 -0
- package/lib/components/react/provider.es.js +12 -0
- package/lib/components/react/provider.js +16 -0
- package/lib/components/react/useNavigate.browser.js +17 -0
- package/lib/components/react/useNavigate.es.js +17 -0
- package/lib/components/react/useNavigate.js +21 -0
- package/lib/components/react/useRoute.browser.js +8 -0
- package/lib/components/react/useRoute.es.js +8 -0
- package/lib/components/react/useRoute.js +12 -0
- package/lib/components/react/useRouter.browser.js +8 -0
- package/lib/components/react/useRouter.es.js +8 -0
- package/lib/components/react/useRouter.js +12 -0
- package/lib/components/react/useUrl.browser.js +8 -0
- package/lib/components/react/useUrl.es.js +8 -0
- package/lib/components/react/useUrl.js +12 -0
- package/lib/history/base.browser.js +11 -0
- package/lib/history/base.es.js +11 -0
- package/lib/history/base.js +15 -0
- package/lib/history/client.browser.js +121 -0
- package/lib/history/client.es.js +121 -0
- package/lib/history/client.js +125 -0
- package/lib/history/server.es.js +15 -0
- package/lib/history/server.js +19 -0
- package/lib/history/wrapper.browser.js +72 -0
- package/lib/history/wrapper.es.js +72 -0
- package/lib/history/wrapper.js +80 -0
- package/lib/index.browser.js +12 -1169
- package/lib/index.es.js +12 -1097
- package/lib/index.js +36 -1124
- package/lib/logger.browser.js +15 -0
- package/lib/logger.es.js +15 -0
- package/lib/logger.js +23 -0
- package/lib/router/abstract.browser.js +395 -0
- package/lib/router/abstract.es.js +395 -0
- package/lib/router/abstract.js +404 -0
- package/lib/router/browser.browser.js +166 -0
- package/lib/router/client.browser.js +116 -0
- package/lib/router/client.es.js +116 -0
- package/lib/router/client.js +120 -0
- package/lib/router/clientNoSpa.browser.js +17 -0
- package/lib/router/clientNoSpa.es.js +17 -0
- package/lib/router/clientNoSpa.js +21 -0
- package/lib/router/server.es.js +85 -0
- package/lib/router/server.js +89 -0
- package/lib/tree/constants.browser.js +6 -0
- package/lib/tree/constants.es.js +6 -0
- package/lib/tree/constants.js +13 -0
- package/lib/tree/tree.browser.js +148 -0
- package/lib/tree/tree.es.js +148 -0
- package/lib/tree/tree.js +158 -0
- package/lib/tree/utils.browser.js +77 -0
- package/lib/tree/utils.es.js +77 -0
- package/lib/tree/utils.js +90 -0
- package/lib/utils.browser.js +35 -0
- package/lib/utils.es.js +35 -0
- package/lib/utils.js +48 -0
- package/package.json +5 -6
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
import { History } from './base.browser.js';
|
|
2
|
+
import { wrapHistory } from './wrapper.browser.js';
|
|
3
|
+
|
|
4
|
+
const isHistoryState = (state) => {
|
|
5
|
+
return state && typeof state.key === 'string';
|
|
6
|
+
};
|
|
7
|
+
const generateKey = (navigation) => {
|
|
8
|
+
const { to } = navigation;
|
|
9
|
+
if (to) {
|
|
10
|
+
return `${to.name}_${to.path}`;
|
|
11
|
+
}
|
|
12
|
+
};
|
|
13
|
+
const generateState = (navigation, currentState) => {
|
|
14
|
+
const key = generateKey(navigation);
|
|
15
|
+
let { type } = navigation;
|
|
16
|
+
if (navigation.replace && currentState) {
|
|
17
|
+
type = currentState.type === type ? type : 'navigate';
|
|
18
|
+
}
|
|
19
|
+
return {
|
|
20
|
+
key,
|
|
21
|
+
type,
|
|
22
|
+
navigateState: navigation.navigateState,
|
|
23
|
+
};
|
|
24
|
+
};
|
|
25
|
+
class ClientHistory extends History {
|
|
26
|
+
constructor() {
|
|
27
|
+
super();
|
|
28
|
+
this.currentIndex = 0;
|
|
29
|
+
this.historyWrapper = wrapHistory({
|
|
30
|
+
onNavigate: ({ url, replace, navigateState }) => {
|
|
31
|
+
this.listener({
|
|
32
|
+
url,
|
|
33
|
+
replace,
|
|
34
|
+
navigateState,
|
|
35
|
+
});
|
|
36
|
+
},
|
|
37
|
+
});
|
|
38
|
+
}
|
|
39
|
+
init(navigation) {
|
|
40
|
+
var _a;
|
|
41
|
+
this.currentState = isHistoryState((_a = window.history) === null || _a === void 0 ? void 0 : _a.state)
|
|
42
|
+
? window.history.state
|
|
43
|
+
: generateState(navigation);
|
|
44
|
+
this.historyWrapper.init(this.currentState);
|
|
45
|
+
this.historyWrapper.subscribe(async ({ path, state }) => {
|
|
46
|
+
var _a, _b;
|
|
47
|
+
try {
|
|
48
|
+
let navigationType;
|
|
49
|
+
let navigateState;
|
|
50
|
+
if (isHistoryState(state)) {
|
|
51
|
+
const { key: prevKey, type: prevType } = this.currentState;
|
|
52
|
+
const { key, type } = state;
|
|
53
|
+
this.currentState = state;
|
|
54
|
+
navigateState = state.navigateState;
|
|
55
|
+
if (key === prevKey &&
|
|
56
|
+
(type === 'updateCurrentRoute' || prevType === 'updateCurrentRoute')) {
|
|
57
|
+
navigationType = 'updateCurrentRoute';
|
|
58
|
+
}
|
|
59
|
+
else {
|
|
60
|
+
navigationType = 'navigate';
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
else {
|
|
64
|
+
// if it is not HistoryState than it is probably not a state from @tinkoff/router so reset it
|
|
65
|
+
this.currentIndex = 0;
|
|
66
|
+
}
|
|
67
|
+
await this.listener({
|
|
68
|
+
type: navigationType,
|
|
69
|
+
history: true,
|
|
70
|
+
url: path,
|
|
71
|
+
navigateState,
|
|
72
|
+
});
|
|
73
|
+
(_a = this.goPromiseResolve) === null || _a === void 0 ? void 0 : _a.call(this);
|
|
74
|
+
}
|
|
75
|
+
catch (err) {
|
|
76
|
+
(_b = this.goPromiseReject) === null || _b === void 0 ? void 0 : _b.call(this, err);
|
|
77
|
+
}
|
|
78
|
+
});
|
|
79
|
+
}
|
|
80
|
+
save(navigation) {
|
|
81
|
+
const { replace, url } = navigation;
|
|
82
|
+
if (!replace) {
|
|
83
|
+
this.currentIndex++;
|
|
84
|
+
}
|
|
85
|
+
this.currentState = generateState(navigation, this.currentState);
|
|
86
|
+
this.historyWrapper.navigate({
|
|
87
|
+
path: url.path,
|
|
88
|
+
replace,
|
|
89
|
+
state: this.currentState,
|
|
90
|
+
});
|
|
91
|
+
}
|
|
92
|
+
go(to, options) {
|
|
93
|
+
var _a;
|
|
94
|
+
const index = this.currentIndex + to;
|
|
95
|
+
if (index < 0) {
|
|
96
|
+
if (options === null || options === void 0 ? void 0 : options.historyFallback) {
|
|
97
|
+
return this.listener({
|
|
98
|
+
url: options.historyFallback,
|
|
99
|
+
type: 'navigate',
|
|
100
|
+
history: false,
|
|
101
|
+
});
|
|
102
|
+
}
|
|
103
|
+
const historyFallbackRoute = (_a = this.tree) === null || _a === void 0 ? void 0 : _a.getHistoryFallback(window.location.pathname);
|
|
104
|
+
if (historyFallbackRoute) {
|
|
105
|
+
return this.listener({
|
|
106
|
+
url: historyFallbackRoute.actualPath,
|
|
107
|
+
type: 'navigate',
|
|
108
|
+
history: false,
|
|
109
|
+
});
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
const promise = new Promise((resolve, reject) => {
|
|
113
|
+
this.goPromiseResolve = resolve;
|
|
114
|
+
this.goPromiseReject = reject;
|
|
115
|
+
});
|
|
116
|
+
this.historyWrapper.history(to);
|
|
117
|
+
return promise;
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
export { ClientHistory };
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
import { History } from './base.es.js';
|
|
2
|
+
import { wrapHistory } from './wrapper.es.js';
|
|
3
|
+
|
|
4
|
+
const isHistoryState = (state) => {
|
|
5
|
+
return state && typeof state.key === 'string';
|
|
6
|
+
};
|
|
7
|
+
const generateKey = (navigation) => {
|
|
8
|
+
const { to } = navigation;
|
|
9
|
+
if (to) {
|
|
10
|
+
return `${to.name}_${to.path}`;
|
|
11
|
+
}
|
|
12
|
+
};
|
|
13
|
+
const generateState = (navigation, currentState) => {
|
|
14
|
+
const key = generateKey(navigation);
|
|
15
|
+
let { type } = navigation;
|
|
16
|
+
if (navigation.replace && currentState) {
|
|
17
|
+
type = currentState.type === type ? type : 'navigate';
|
|
18
|
+
}
|
|
19
|
+
return {
|
|
20
|
+
key,
|
|
21
|
+
type,
|
|
22
|
+
navigateState: navigation.navigateState,
|
|
23
|
+
};
|
|
24
|
+
};
|
|
25
|
+
class ClientHistory extends History {
|
|
26
|
+
constructor() {
|
|
27
|
+
super();
|
|
28
|
+
this.currentIndex = 0;
|
|
29
|
+
this.historyWrapper = wrapHistory({
|
|
30
|
+
onNavigate: ({ url, replace, navigateState }) => {
|
|
31
|
+
this.listener({
|
|
32
|
+
url,
|
|
33
|
+
replace,
|
|
34
|
+
navigateState,
|
|
35
|
+
});
|
|
36
|
+
},
|
|
37
|
+
});
|
|
38
|
+
}
|
|
39
|
+
init(navigation) {
|
|
40
|
+
var _a;
|
|
41
|
+
this.currentState = isHistoryState((_a = window.history) === null || _a === void 0 ? void 0 : _a.state)
|
|
42
|
+
? window.history.state
|
|
43
|
+
: generateState(navigation);
|
|
44
|
+
this.historyWrapper.init(this.currentState);
|
|
45
|
+
this.historyWrapper.subscribe(async ({ path, state }) => {
|
|
46
|
+
var _a, _b;
|
|
47
|
+
try {
|
|
48
|
+
let navigationType;
|
|
49
|
+
let navigateState;
|
|
50
|
+
if (isHistoryState(state)) {
|
|
51
|
+
const { key: prevKey, type: prevType } = this.currentState;
|
|
52
|
+
const { key, type } = state;
|
|
53
|
+
this.currentState = state;
|
|
54
|
+
navigateState = state.navigateState;
|
|
55
|
+
if (key === prevKey &&
|
|
56
|
+
(type === 'updateCurrentRoute' || prevType === 'updateCurrentRoute')) {
|
|
57
|
+
navigationType = 'updateCurrentRoute';
|
|
58
|
+
}
|
|
59
|
+
else {
|
|
60
|
+
navigationType = 'navigate';
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
else {
|
|
64
|
+
// if it is not HistoryState than it is probably not a state from @tinkoff/router so reset it
|
|
65
|
+
this.currentIndex = 0;
|
|
66
|
+
}
|
|
67
|
+
await this.listener({
|
|
68
|
+
type: navigationType,
|
|
69
|
+
history: true,
|
|
70
|
+
url: path,
|
|
71
|
+
navigateState,
|
|
72
|
+
});
|
|
73
|
+
(_a = this.goPromiseResolve) === null || _a === void 0 ? void 0 : _a.call(this);
|
|
74
|
+
}
|
|
75
|
+
catch (err) {
|
|
76
|
+
(_b = this.goPromiseReject) === null || _b === void 0 ? void 0 : _b.call(this, err);
|
|
77
|
+
}
|
|
78
|
+
});
|
|
79
|
+
}
|
|
80
|
+
save(navigation) {
|
|
81
|
+
const { replace, url } = navigation;
|
|
82
|
+
if (!replace) {
|
|
83
|
+
this.currentIndex++;
|
|
84
|
+
}
|
|
85
|
+
this.currentState = generateState(navigation, this.currentState);
|
|
86
|
+
this.historyWrapper.navigate({
|
|
87
|
+
path: url.path,
|
|
88
|
+
replace,
|
|
89
|
+
state: this.currentState,
|
|
90
|
+
});
|
|
91
|
+
}
|
|
92
|
+
go(to, options) {
|
|
93
|
+
var _a;
|
|
94
|
+
const index = this.currentIndex + to;
|
|
95
|
+
if (index < 0) {
|
|
96
|
+
if (options === null || options === void 0 ? void 0 : options.historyFallback) {
|
|
97
|
+
return this.listener({
|
|
98
|
+
url: options.historyFallback,
|
|
99
|
+
type: 'navigate',
|
|
100
|
+
history: false,
|
|
101
|
+
});
|
|
102
|
+
}
|
|
103
|
+
const historyFallbackRoute = (_a = this.tree) === null || _a === void 0 ? void 0 : _a.getHistoryFallback(window.location.pathname);
|
|
104
|
+
if (historyFallbackRoute) {
|
|
105
|
+
return this.listener({
|
|
106
|
+
url: historyFallbackRoute.actualPath,
|
|
107
|
+
type: 'navigate',
|
|
108
|
+
history: false,
|
|
109
|
+
});
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
const promise = new Promise((resolve, reject) => {
|
|
113
|
+
this.goPromiseResolve = resolve;
|
|
114
|
+
this.goPromiseReject = reject;
|
|
115
|
+
});
|
|
116
|
+
this.historyWrapper.history(to);
|
|
117
|
+
return promise;
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
export { ClientHistory };
|
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
var base = require('./base.js');
|
|
6
|
+
var wrapper = require('./wrapper.js');
|
|
7
|
+
|
|
8
|
+
const isHistoryState = (state) => {
|
|
9
|
+
return state && typeof state.key === 'string';
|
|
10
|
+
};
|
|
11
|
+
const generateKey = (navigation) => {
|
|
12
|
+
const { to } = navigation;
|
|
13
|
+
if (to) {
|
|
14
|
+
return `${to.name}_${to.path}`;
|
|
15
|
+
}
|
|
16
|
+
};
|
|
17
|
+
const generateState = (navigation, currentState) => {
|
|
18
|
+
const key = generateKey(navigation);
|
|
19
|
+
let { type } = navigation;
|
|
20
|
+
if (navigation.replace && currentState) {
|
|
21
|
+
type = currentState.type === type ? type : 'navigate';
|
|
22
|
+
}
|
|
23
|
+
return {
|
|
24
|
+
key,
|
|
25
|
+
type,
|
|
26
|
+
navigateState: navigation.navigateState,
|
|
27
|
+
};
|
|
28
|
+
};
|
|
29
|
+
class ClientHistory extends base.History {
|
|
30
|
+
constructor() {
|
|
31
|
+
super();
|
|
32
|
+
this.currentIndex = 0;
|
|
33
|
+
this.historyWrapper = wrapper.wrapHistory({
|
|
34
|
+
onNavigate: ({ url, replace, navigateState }) => {
|
|
35
|
+
this.listener({
|
|
36
|
+
url,
|
|
37
|
+
replace,
|
|
38
|
+
navigateState,
|
|
39
|
+
});
|
|
40
|
+
},
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
init(navigation) {
|
|
44
|
+
var _a;
|
|
45
|
+
this.currentState = isHistoryState((_a = window.history) === null || _a === void 0 ? void 0 : _a.state)
|
|
46
|
+
? window.history.state
|
|
47
|
+
: generateState(navigation);
|
|
48
|
+
this.historyWrapper.init(this.currentState);
|
|
49
|
+
this.historyWrapper.subscribe(async ({ path, state }) => {
|
|
50
|
+
var _a, _b;
|
|
51
|
+
try {
|
|
52
|
+
let navigationType;
|
|
53
|
+
let navigateState;
|
|
54
|
+
if (isHistoryState(state)) {
|
|
55
|
+
const { key: prevKey, type: prevType } = this.currentState;
|
|
56
|
+
const { key, type } = state;
|
|
57
|
+
this.currentState = state;
|
|
58
|
+
navigateState = state.navigateState;
|
|
59
|
+
if (key === prevKey &&
|
|
60
|
+
(type === 'updateCurrentRoute' || prevType === 'updateCurrentRoute')) {
|
|
61
|
+
navigationType = 'updateCurrentRoute';
|
|
62
|
+
}
|
|
63
|
+
else {
|
|
64
|
+
navigationType = 'navigate';
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
else {
|
|
68
|
+
// if it is not HistoryState than it is probably not a state from @tinkoff/router so reset it
|
|
69
|
+
this.currentIndex = 0;
|
|
70
|
+
}
|
|
71
|
+
await this.listener({
|
|
72
|
+
type: navigationType,
|
|
73
|
+
history: true,
|
|
74
|
+
url: path,
|
|
75
|
+
navigateState,
|
|
76
|
+
});
|
|
77
|
+
(_a = this.goPromiseResolve) === null || _a === void 0 ? void 0 : _a.call(this);
|
|
78
|
+
}
|
|
79
|
+
catch (err) {
|
|
80
|
+
(_b = this.goPromiseReject) === null || _b === void 0 ? void 0 : _b.call(this, err);
|
|
81
|
+
}
|
|
82
|
+
});
|
|
83
|
+
}
|
|
84
|
+
save(navigation) {
|
|
85
|
+
const { replace, url } = navigation;
|
|
86
|
+
if (!replace) {
|
|
87
|
+
this.currentIndex++;
|
|
88
|
+
}
|
|
89
|
+
this.currentState = generateState(navigation, this.currentState);
|
|
90
|
+
this.historyWrapper.navigate({
|
|
91
|
+
path: url.path,
|
|
92
|
+
replace,
|
|
93
|
+
state: this.currentState,
|
|
94
|
+
});
|
|
95
|
+
}
|
|
96
|
+
go(to, options) {
|
|
97
|
+
var _a;
|
|
98
|
+
const index = this.currentIndex + to;
|
|
99
|
+
if (index < 0) {
|
|
100
|
+
if (options === null || options === void 0 ? void 0 : options.historyFallback) {
|
|
101
|
+
return this.listener({
|
|
102
|
+
url: options.historyFallback,
|
|
103
|
+
type: 'navigate',
|
|
104
|
+
history: false,
|
|
105
|
+
});
|
|
106
|
+
}
|
|
107
|
+
const historyFallbackRoute = (_a = this.tree) === null || _a === void 0 ? void 0 : _a.getHistoryFallback(window.location.pathname);
|
|
108
|
+
if (historyFallbackRoute) {
|
|
109
|
+
return this.listener({
|
|
110
|
+
url: historyFallbackRoute.actualPath,
|
|
111
|
+
type: 'navigate',
|
|
112
|
+
history: false,
|
|
113
|
+
});
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
const promise = new Promise((resolve, reject) => {
|
|
117
|
+
this.goPromiseResolve = resolve;
|
|
118
|
+
this.goPromiseReject = reject;
|
|
119
|
+
});
|
|
120
|
+
this.historyWrapper.history(to);
|
|
121
|
+
return promise;
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
exports.ClientHistory = ClientHistory;
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { logger } from '../logger.es.js';
|
|
2
|
+
import { History } from './base.es.js';
|
|
3
|
+
|
|
4
|
+
class ServerHistory extends History {
|
|
5
|
+
save() { }
|
|
6
|
+
go() {
|
|
7
|
+
logger.warn({
|
|
8
|
+
event: 'history.server',
|
|
9
|
+
message: 'Trying to change history on server',
|
|
10
|
+
});
|
|
11
|
+
return Promise.resolve();
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export { ServerHistory };
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
var logger = require('../logger.js');
|
|
6
|
+
var base = require('./base.js');
|
|
7
|
+
|
|
8
|
+
class ServerHistory extends base.History {
|
|
9
|
+
save() { }
|
|
10
|
+
go() {
|
|
11
|
+
logger.logger.warn({
|
|
12
|
+
event: 'history.server',
|
|
13
|
+
message: 'Trying to change history on server',
|
|
14
|
+
});
|
|
15
|
+
return Promise.resolve();
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
exports.ServerHistory = ServerHistory;
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import noop from '@tinkoff/utils/function/noop';
|
|
2
|
+
|
|
3
|
+
const supportsHtml5History = typeof window !== 'undefined' && window.history && window.history.pushState;
|
|
4
|
+
const wrapHistory = ({ onNavigate }) => {
|
|
5
|
+
if (!supportsHtml5History) {
|
|
6
|
+
const navigate = (data, title, url) => {
|
|
7
|
+
window.location.href = url.toString();
|
|
8
|
+
};
|
|
9
|
+
window.history.pushState = navigate;
|
|
10
|
+
window.history.replaceState = navigate;
|
|
11
|
+
return {
|
|
12
|
+
navigate: ({ path }) => navigate({}, '', path),
|
|
13
|
+
history: () => {
|
|
14
|
+
throw new Error('Method not implemented');
|
|
15
|
+
},
|
|
16
|
+
init: noop,
|
|
17
|
+
subscribe: noop,
|
|
18
|
+
};
|
|
19
|
+
}
|
|
20
|
+
let browserHistory = window.history;
|
|
21
|
+
if ('__originalHistory' in window.history) {
|
|
22
|
+
browserHistory = window.history.__originalHistory;
|
|
23
|
+
}
|
|
24
|
+
else {
|
|
25
|
+
window.history.__originalHistory = {
|
|
26
|
+
pushState: browserHistory.pushState.bind(window.history),
|
|
27
|
+
replaceState: browserHistory.replaceState.bind(window.history),
|
|
28
|
+
go: browserHistory.go.bind(window.history),
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
const pushState = browserHistory.pushState.bind(window.history);
|
|
32
|
+
const replaceState = browserHistory.replaceState.bind(window.history);
|
|
33
|
+
const go = browserHistory.go.bind(window.history);
|
|
34
|
+
const navigate = ({ path, replace, state }) => {
|
|
35
|
+
if (replace) {
|
|
36
|
+
replaceState(state, '', path);
|
|
37
|
+
}
|
|
38
|
+
else {
|
|
39
|
+
pushState(state, '', path);
|
|
40
|
+
}
|
|
41
|
+
};
|
|
42
|
+
const history = (delta) => {
|
|
43
|
+
go(delta);
|
|
44
|
+
};
|
|
45
|
+
const browserNavigate = (replace = false) => {
|
|
46
|
+
return (navigateState, title, url) => {
|
|
47
|
+
onNavigate({ url: url.toString(), replace, navigateState });
|
|
48
|
+
};
|
|
49
|
+
};
|
|
50
|
+
window.history.pushState = browserNavigate(false);
|
|
51
|
+
window.history.replaceState = browserNavigate(true);
|
|
52
|
+
window.history.go = history;
|
|
53
|
+
window.history.back = () => history(-1);
|
|
54
|
+
window.history.forward = () => history(1);
|
|
55
|
+
return {
|
|
56
|
+
navigate,
|
|
57
|
+
history,
|
|
58
|
+
init: (state) => {
|
|
59
|
+
replaceState(state, '');
|
|
60
|
+
},
|
|
61
|
+
subscribe: (handler) => {
|
|
62
|
+
window.addEventListener('popstate', ({ state }) => {
|
|
63
|
+
handler({
|
|
64
|
+
path: window.location.pathname + window.location.search + window.location.hash,
|
|
65
|
+
state,
|
|
66
|
+
});
|
|
67
|
+
});
|
|
68
|
+
},
|
|
69
|
+
};
|
|
70
|
+
};
|
|
71
|
+
|
|
72
|
+
export { wrapHistory };
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import noop from '@tinkoff/utils/function/noop';
|
|
2
|
+
|
|
3
|
+
const supportsHtml5History = typeof window !== 'undefined' && window.history && window.history.pushState;
|
|
4
|
+
const wrapHistory = ({ onNavigate }) => {
|
|
5
|
+
if (!supportsHtml5History) {
|
|
6
|
+
const navigate = (data, title, url) => {
|
|
7
|
+
window.location.href = url.toString();
|
|
8
|
+
};
|
|
9
|
+
window.history.pushState = navigate;
|
|
10
|
+
window.history.replaceState = navigate;
|
|
11
|
+
return {
|
|
12
|
+
navigate: ({ path }) => navigate({}, '', path),
|
|
13
|
+
history: () => {
|
|
14
|
+
throw new Error('Method not implemented');
|
|
15
|
+
},
|
|
16
|
+
init: noop,
|
|
17
|
+
subscribe: noop,
|
|
18
|
+
};
|
|
19
|
+
}
|
|
20
|
+
let browserHistory = window.history;
|
|
21
|
+
if ('__originalHistory' in window.history) {
|
|
22
|
+
browserHistory = window.history.__originalHistory;
|
|
23
|
+
}
|
|
24
|
+
else {
|
|
25
|
+
window.history.__originalHistory = {
|
|
26
|
+
pushState: browserHistory.pushState.bind(window.history),
|
|
27
|
+
replaceState: browserHistory.replaceState.bind(window.history),
|
|
28
|
+
go: browserHistory.go.bind(window.history),
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
const pushState = browserHistory.pushState.bind(window.history);
|
|
32
|
+
const replaceState = browserHistory.replaceState.bind(window.history);
|
|
33
|
+
const go = browserHistory.go.bind(window.history);
|
|
34
|
+
const navigate = ({ path, replace, state }) => {
|
|
35
|
+
if (replace) {
|
|
36
|
+
replaceState(state, '', path);
|
|
37
|
+
}
|
|
38
|
+
else {
|
|
39
|
+
pushState(state, '', path);
|
|
40
|
+
}
|
|
41
|
+
};
|
|
42
|
+
const history = (delta) => {
|
|
43
|
+
go(delta);
|
|
44
|
+
};
|
|
45
|
+
const browserNavigate = (replace = false) => {
|
|
46
|
+
return (navigateState, title, url) => {
|
|
47
|
+
onNavigate({ url: url.toString(), replace, navigateState });
|
|
48
|
+
};
|
|
49
|
+
};
|
|
50
|
+
window.history.pushState = browserNavigate(false);
|
|
51
|
+
window.history.replaceState = browserNavigate(true);
|
|
52
|
+
window.history.go = history;
|
|
53
|
+
window.history.back = () => history(-1);
|
|
54
|
+
window.history.forward = () => history(1);
|
|
55
|
+
return {
|
|
56
|
+
navigate,
|
|
57
|
+
history,
|
|
58
|
+
init: (state) => {
|
|
59
|
+
replaceState(state, '');
|
|
60
|
+
},
|
|
61
|
+
subscribe: (handler) => {
|
|
62
|
+
window.addEventListener('popstate', ({ state }) => {
|
|
63
|
+
handler({
|
|
64
|
+
path: window.location.pathname + window.location.search + window.location.hash,
|
|
65
|
+
state,
|
|
66
|
+
});
|
|
67
|
+
});
|
|
68
|
+
},
|
|
69
|
+
};
|
|
70
|
+
};
|
|
71
|
+
|
|
72
|
+
export { wrapHistory };
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
var noop = require('@tinkoff/utils/function/noop');
|
|
6
|
+
|
|
7
|
+
function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
|
|
8
|
+
|
|
9
|
+
var noop__default = /*#__PURE__*/_interopDefaultLegacy(noop);
|
|
10
|
+
|
|
11
|
+
const supportsHtml5History = typeof window !== 'undefined' && window.history && window.history.pushState;
|
|
12
|
+
const wrapHistory = ({ onNavigate }) => {
|
|
13
|
+
if (!supportsHtml5History) {
|
|
14
|
+
const navigate = (data, title, url) => {
|
|
15
|
+
window.location.href = url.toString();
|
|
16
|
+
};
|
|
17
|
+
window.history.pushState = navigate;
|
|
18
|
+
window.history.replaceState = navigate;
|
|
19
|
+
return {
|
|
20
|
+
navigate: ({ path }) => navigate({}, '', path),
|
|
21
|
+
history: () => {
|
|
22
|
+
throw new Error('Method not implemented');
|
|
23
|
+
},
|
|
24
|
+
init: noop__default["default"],
|
|
25
|
+
subscribe: noop__default["default"],
|
|
26
|
+
};
|
|
27
|
+
}
|
|
28
|
+
let browserHistory = window.history;
|
|
29
|
+
if ('__originalHistory' in window.history) {
|
|
30
|
+
browserHistory = window.history.__originalHistory;
|
|
31
|
+
}
|
|
32
|
+
else {
|
|
33
|
+
window.history.__originalHistory = {
|
|
34
|
+
pushState: browserHistory.pushState.bind(window.history),
|
|
35
|
+
replaceState: browserHistory.replaceState.bind(window.history),
|
|
36
|
+
go: browserHistory.go.bind(window.history),
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
const pushState = browserHistory.pushState.bind(window.history);
|
|
40
|
+
const replaceState = browserHistory.replaceState.bind(window.history);
|
|
41
|
+
const go = browserHistory.go.bind(window.history);
|
|
42
|
+
const navigate = ({ path, replace, state }) => {
|
|
43
|
+
if (replace) {
|
|
44
|
+
replaceState(state, '', path);
|
|
45
|
+
}
|
|
46
|
+
else {
|
|
47
|
+
pushState(state, '', path);
|
|
48
|
+
}
|
|
49
|
+
};
|
|
50
|
+
const history = (delta) => {
|
|
51
|
+
go(delta);
|
|
52
|
+
};
|
|
53
|
+
const browserNavigate = (replace = false) => {
|
|
54
|
+
return (navigateState, title, url) => {
|
|
55
|
+
onNavigate({ url: url.toString(), replace, navigateState });
|
|
56
|
+
};
|
|
57
|
+
};
|
|
58
|
+
window.history.pushState = browserNavigate(false);
|
|
59
|
+
window.history.replaceState = browserNavigate(true);
|
|
60
|
+
window.history.go = history;
|
|
61
|
+
window.history.back = () => history(-1);
|
|
62
|
+
window.history.forward = () => history(1);
|
|
63
|
+
return {
|
|
64
|
+
navigate,
|
|
65
|
+
history,
|
|
66
|
+
init: (state) => {
|
|
67
|
+
replaceState(state, '');
|
|
68
|
+
},
|
|
69
|
+
subscribe: (handler) => {
|
|
70
|
+
window.addEventListener('popstate', ({ state }) => {
|
|
71
|
+
handler({
|
|
72
|
+
path: window.location.pathname + window.location.search + window.location.hash,
|
|
73
|
+
state,
|
|
74
|
+
});
|
|
75
|
+
});
|
|
76
|
+
},
|
|
77
|
+
};
|
|
78
|
+
};
|
|
79
|
+
|
|
80
|
+
exports.wrapHistory = wrapHistory;
|