-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
224 lines (201 loc) · 4.94 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
'use strict'
function _typeof(obj) {
if (typeof Symbol === 'function' && typeof Symbol.iterator === 'symbol') {
_typeof = function _typeof(obj) {
return typeof obj
}
} else {
_typeof = function _typeof(obj) {
return obj &&
typeof Symbol === 'function' &&
obj.constructor === Symbol &&
obj !== Symbol.prototype
? 'symbol'
: typeof obj
}
}
return _typeof(obj)
}
Object.defineProperty(exports, '__esModule', {
value: true
})
exports['default'] = exports.Link = void 0
var _react = _interopRequireWildcard(require('react'))
var _stora = _interopRequireDefault(require('@rawewhat/stora'))
function _interopRequireDefault(obj) {
return obj && obj.__esModule ? obj : { default: obj }
}
function _getRequireWildcardCache() {
if (typeof WeakMap !== 'function') return null
var cache = new WeakMap()
_getRequireWildcardCache = function _getRequireWildcardCache() {
return cache
}
return cache
}
function _interopRequireWildcard(obj) {
if (obj && obj.__esModule) {
return obj
}
if (
obj === null ||
(_typeof(obj) !== 'object' && typeof obj !== 'function')
) {
return { default: obj }
}
var cache = _getRequireWildcardCache()
if (cache && cache.has(obj)) {
return cache.get(obj)
}
var newObj = {}
var hasPropertyDescriptor =
Object.defineProperty && Object.getOwnPropertyDescriptor
for (var key in obj) {
if (Object.prototype.hasOwnProperty.call(obj, key)) {
var desc = hasPropertyDescriptor
? Object.getOwnPropertyDescriptor(obj, key)
: null
if (desc && (desc.get || desc.set)) {
Object.defineProperty(newObj, key, desc)
} else {
newObj[key] = obj[key]
}
}
}
newObj['default'] = obj
if (cache) {
cache.set(obj, newObj)
}
return newObj
}
function _slicedToArray(arr, i) {
return (
_arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _nonIterableRest()
)
}
function _nonIterableRest() {
throw new TypeError('Invalid attempt to destructure non-iterable instance')
}
function _iterableToArrayLimit(arr, i) {
if (
!(
Symbol.iterator in Object(arr) ||
Object.prototype.toString.call(arr) === '[object Arguments]'
)
) {
return
}
var _arr = []
var _n = true
var _d = false
var _e = undefined
try {
for (
var _i = arr[Symbol.iterator](), _s;
!(_n = (_s = _i.next()).done);
_n = true
) {
_arr.push(_s.value)
if (i && _arr.length === i) break
}
} catch (err) {
_d = true
_e = err
} finally {
try {
if (!_n && _i['return'] != null) _i['return']()
} finally {
if (_d) throw _e
}
}
return _arr
}
function _arrayWithHoles(arr) {
if (Array.isArray(arr)) return arr
}
function getConfig(config) {
if (config) return config
try {
return require('../../../routra.config')['default']
} catch (e) {
try {
return require('../../../src/routra.config')['default']
} catch (e) {
return [
{
path: '/',
screen: _react['default'].createElement('span', null, 'Routra Demo')
}
]
}
}
}
var Router = function Router() {
var _useStora = (0, _stora['default'])(),
_useStora2 = _slicedToArray(_useStora, 1),
route = _useStora2[0].routra.route
var config = getConfig()
var Component = _react['default'].createElement('span', null, 'Error')
config.forEach(function(cfg) {
if (cfg.path.length <= 1 && cfg.path.startsWith('/'))
Component = _react['default'].cloneElement(cfg.screen)
else {
var path = cfg.path.startsWith('/') ? cfg.path.replace('/', '') : cfg.path
console.log('path', path, 'route', route)
if (path === route) {
var Comp = cfg.screen
Component = _react['default'].createElement(Comp, null)
}
}
})
var Screen = (0, _react.useMemo)(
function() {
return Component
},
[route]
)
return _react['default'].createElement(
_react.Suspense,
{
fallback: _react['default'].createElement('span', null, 'Loading')
},
Screen
)
}
var Link = function Link(_ref) {
var title = _ref.title,
path = _ref.path
var _useStora3 = (0, _stora['default'])(),
_useStora4 = _slicedToArray(_useStora3, 2),
visit = _useStora4[1].routra.visit
return (0, _react.useMemo)(function() {
return _react['default'].createElement(
'span',
{
style: {
cursor: 'pointer',
color: 'blue',
textDecoration: 'underline'
},
onClick: function onClick() {
if (path.length > 1 && path.startsWith('/')) {
path = path.replace('/', '')
}
visit(path)
window.history.pushState(
{
title: title,
path: path
},
title,
path
)
}
},
title
)
}, [])
}
exports.Link = Link
var _default = Router
exports['default'] = _default