kdu-router 3.4.0-beta.0 → 3.6.0

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.
Files changed (49) hide show
  1. package/LICENSE +21 -21
  2. package/README.md +11 -9
  3. package/dist/composables.js +253 -0
  4. package/dist/composables.mjs +244 -0
  5. package/dist/kdu-router.common.js +2682 -2572
  6. package/dist/kdu-router.esm.browser.js +2677 -2563
  7. package/dist/kdu-router.esm.browser.min.js +5 -5
  8. package/dist/kdu-router.esm.js +2685 -2572
  9. package/dist/kdu-router.js +2682 -2573
  10. package/dist/kdu-router.min.js +5 -5
  11. package/ketur/attributes.json +38 -38
  12. package/ketur/tags.json +20 -20
  13. package/package.json +115 -111
  14. package/src/components/link.js +224 -197
  15. package/src/components/view.js +155 -149
  16. package/src/composables/globals.js +34 -0
  17. package/src/composables/guards.js +68 -0
  18. package/src/composables/index.js +3 -0
  19. package/src/composables/useLink.js +113 -0
  20. package/src/composables/utils.js +11 -0
  21. package/src/create-matcher.js +226 -200
  22. package/src/create-route-map.js +220 -205
  23. package/src/entries/cjs.js +3 -0
  24. package/src/entries/esm.js +12 -0
  25. package/src/history/abstract.js +72 -68
  26. package/src/history/base.js +379 -400
  27. package/src/history/hash.js +152 -163
  28. package/src/history/html5.js +99 -94
  29. package/src/index.js +3 -277
  30. package/src/install.js +52 -52
  31. package/src/router.js +293 -0
  32. package/src/util/async.js +18 -18
  33. package/src/util/dom.js +3 -3
  34. package/src/util/errors.js +86 -85
  35. package/src/util/location.js +69 -69
  36. package/src/util/misc.js +6 -6
  37. package/src/util/params.js +37 -37
  38. package/src/util/path.js +74 -74
  39. package/src/util/push-state.js +46 -46
  40. package/src/util/query.js +113 -96
  41. package/src/util/resolve-components.js +109 -109
  42. package/src/util/route.js +151 -132
  43. package/src/util/scroll.js +175 -165
  44. package/src/util/state-key.js +22 -22
  45. package/src/util/warn.js +14 -14
  46. package/types/composables.d.ts +53 -0
  47. package/types/index.d.ts +25 -17
  48. package/types/kdu.d.ts +22 -22
  49. package/types/router.d.ts +564 -170
package/LICENSE CHANGED
@@ -1,21 +1,21 @@
1
- MIT License
2
-
3
- Copyright (c) 2021-present NKDuy
4
-
5
- Permission is hereby granted, free of charge, to any person obtaining a copy
6
- of this software and associated documentation files (the "Software"), to deal
7
- in the Software without restriction, including without limitation the rights
8
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
- copies of the Software, and to permit persons to whom the Software is
10
- furnished to do so, subject to the following conditions:
11
-
12
- The above copyright notice and this permission notice shall be included in all
13
- copies or substantial portions of the Software.
14
-
15
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
- SOFTWARE.
1
+ MIT License
2
+
3
+ Copyright (c) 2021-present NKDuy
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -1,9 +1,11 @@
1
- # kdu-router
2
-
3
- > This is kdu-router 3.0 which works only with Kdu 2.0.
4
-
5
- ## License
6
-
7
- [MIT](http://opensource.org/licenses/MIT)
8
-
9
- Copyright (c) 2021-present NKDuy
1
+ # kdu-router
2
+
3
+ ---
4
+
5
+ Get started with the [documentation](http://kdujs-router.web.app).
6
+
7
+ ## License
8
+
9
+ [MIT](http://opensource.org/licenses/MIT)
10
+
11
+ Copyright (c) 2021-present NKDuy
@@ -0,0 +1,253 @@
1
+ /*!
2
+ * kdu-router v3.6.0
3
+ * (c) 2026 NKDuy
4
+ * @license MIT
5
+ */
6
+ 'use strict';
7
+
8
+ Object.defineProperty(exports, '__esModule', { value: true });
9
+
10
+ var kdu = require('kdu');
11
+
12
+ // dev only warn if no current instance
13
+
14
+ function throwNoCurrentInstance (method) {
15
+ if (!kdu.getCurrentInstance()) {
16
+ throw new Error(
17
+ ("[kdu-router]: Missing current instance. " + method + "() must be called inside <script setup> or setup().")
18
+ )
19
+ }
20
+ }
21
+
22
+ function useRouter () {
23
+ if (process.env.NODE_ENV !== 'production') {
24
+ throwNoCurrentInstance('useRouter');
25
+ }
26
+
27
+ return kdu.getCurrentInstance().proxy.$root.$router
28
+ }
29
+
30
+ function useRoute () {
31
+ if (process.env.NODE_ENV !== 'production') {
32
+ throwNoCurrentInstance('useRoute');
33
+ }
34
+
35
+ var root = kdu.getCurrentInstance().proxy.$root;
36
+ if (!root._$route) {
37
+ var route = kdu.effectScope(true).run(function () { return kdu.shallowReactive(Object.assign({}, root.$router.currentRoute)); }
38
+ );
39
+ root._$route = route;
40
+
41
+ root.$router.afterEach(function (to) {
42
+ Object.assign(route, to);
43
+ });
44
+ }
45
+
46
+ return root._$route
47
+ }
48
+
49
+ function onBeforeRouteUpdate (guard) {
50
+ if (process.env.NODE_ENV !== 'production') {
51
+ throwNoCurrentInstance('onBeforeRouteUpdate');
52
+ }
53
+
54
+ return useFilteredGuard(guard, isUpdateNavigation)
55
+ }
56
+ function isUpdateNavigation (to, from, depth) {
57
+ var toMatched = to.matched;
58
+ var fromMatched = from.matched;
59
+ return (
60
+ toMatched.length >= depth &&
61
+ toMatched
62
+ .slice(0, depth + 1)
63
+ .every(function (record, i) { return record === fromMatched[i]; })
64
+ )
65
+ }
66
+
67
+ function isLeaveNavigation (to, from, depth) {
68
+ var toMatched = to.matched;
69
+ var fromMatched = from.matched;
70
+ return toMatched.length < depth || toMatched[depth] !== fromMatched[depth]
71
+ }
72
+
73
+ function onBeforeRouteLeave (guard) {
74
+ if (process.env.NODE_ENV !== 'production') {
75
+ throwNoCurrentInstance('onBeforeRouteLeave');
76
+ }
77
+
78
+ return useFilteredGuard(guard, isLeaveNavigation)
79
+ }
80
+
81
+ var noop = function () {};
82
+ function useFilteredGuard (guard, fn) {
83
+ var instance = kdu.getCurrentInstance();
84
+ var router = useRouter();
85
+
86
+ var target = instance.proxy;
87
+ // find the nearest RouterView to know the depth
88
+ while (
89
+ target &&
90
+ target.$knode &&
91
+ target.$knode.data &&
92
+ target.$knode.data.routerViewDepth == null
93
+ ) {
94
+ target = target.$parent;
95
+ }
96
+
97
+ var depth =
98
+ target && target.$knode && target.$knode.data
99
+ ? target.$knode.data.routerViewDepth
100
+ : null;
101
+
102
+ if (depth != null) {
103
+ var removeGuard = router.beforeEach(function (to, from, next) {
104
+ return fn(to, from, depth) ? guard(to, from, next) : next()
105
+ });
106
+
107
+ kdu.onUnmounted(removeGuard);
108
+ return removeGuard
109
+ }
110
+
111
+ return noop
112
+ }
113
+
114
+ /* */
115
+
116
+ function guardEvent (e) {
117
+ // don't redirect with control keys
118
+ if (e.metaKey || e.altKey || e.ctrlKey || e.shiftKey) { return }
119
+ // don't redirect when preventDefault called
120
+ if (e.defaultPrevented) { return }
121
+ // don't redirect on right click
122
+ if (e.button !== undefined && e.button !== 0) { return }
123
+ // don't redirect if `target="_blank"`
124
+ if (e.currentTarget && e.currentTarget.getAttribute) {
125
+ var target = e.currentTarget.getAttribute('target');
126
+ if (/\b_blank\b/i.test(target)) { return }
127
+ }
128
+ // this may be a Weex event which doesn't have this method
129
+ if (e.preventDefault) {
130
+ e.preventDefault();
131
+ }
132
+ return true
133
+ }
134
+
135
+ function includesParams (outer, inner) {
136
+ var loop = function ( key ) {
137
+ var innerValue = inner[key];
138
+ var outerValue = outer[key];
139
+ if (typeof innerValue === 'string') {
140
+ if (innerValue !== outerValue) { return { v: false } }
141
+ } else {
142
+ if (
143
+ !Array.isArray(outerValue) ||
144
+ outerValue.length !== innerValue.length ||
145
+ innerValue.some(function (value, i) { return value !== outerValue[i]; })
146
+ ) {
147
+ return { v: false }
148
+ }
149
+ }
150
+ };
151
+
152
+ for (var key in inner) {
153
+ var returned = loop( key );
154
+
155
+ if ( returned ) return returned.v;
156
+ }
157
+
158
+ return true
159
+ }
160
+
161
+ // helpers from kdu router 4
162
+
163
+ function isSameRouteLocationParamsValue (a, b) {
164
+ return Array.isArray(a)
165
+ ? isEquivalentArray(a, b)
166
+ : Array.isArray(b)
167
+ ? isEquivalentArray(b, a)
168
+ : a === b
169
+ }
170
+
171
+ function isEquivalentArray (a, b) {
172
+ return Array.isArray(b)
173
+ ? a.length === b.length && a.every(function (value, i) { return value === b[i]; })
174
+ : a.length === 1 && a[0] === b
175
+ }
176
+
177
+ function isSameRouteLocationParams (a, b) {
178
+ if (Object.keys(a).length !== Object.keys(b).length) { return false }
179
+
180
+ for (var key in a) {
181
+ if (!isSameRouteLocationParamsValue(a[key], b[key])) { return false }
182
+ }
183
+
184
+ return true
185
+ }
186
+
187
+ function useLink (props) {
188
+ if (process.env.NODE_ENV !== 'production') {
189
+ throwNoCurrentInstance('useLink');
190
+ }
191
+
192
+ var router = useRouter();
193
+ var currentRoute = useRoute();
194
+
195
+ var resolvedRoute = kdu.computed(function () { return router.resolve(kdu.unref(props.to), currentRoute); });
196
+
197
+ var activeRecordIndex = kdu.computed(function () {
198
+ var route = resolvedRoute.value.route;
199
+ var matched = route.matched;
200
+ var length = matched.length;
201
+ var routeMatched = matched[length - 1];
202
+ var currentMatched = currentRoute.matched;
203
+ if (!routeMatched || !currentMatched.length) { return -1 }
204
+ var index = currentMatched.indexOf(routeMatched);
205
+ if (index > -1) { return index }
206
+ // possible parent record
207
+ var parentRecord = currentMatched[currentMatched.length - 2];
208
+
209
+ return (
210
+ // we are dealing with nested routes
211
+ length > 1 &&
212
+ // if the parent and matched route have the same path, this link is
213
+ // referring to the empty child. Or we currently are on a different
214
+ // child of the same parent
215
+ parentRecord && parentRecord === routeMatched.parent
216
+ )
217
+ });
218
+
219
+ var isActive = kdu.computed(
220
+ function () { return activeRecordIndex.value > -1 &&
221
+ includesParams(currentRoute.params, resolvedRoute.value.route.params); }
222
+ );
223
+ var isExactActive = kdu.computed(
224
+ function () { return activeRecordIndex.value > -1 &&
225
+ activeRecordIndex.value === currentRoute.matched.length - 1 &&
226
+ isSameRouteLocationParams(currentRoute.params, resolvedRoute.value.route.params); }
227
+ );
228
+
229
+ var navigate = function (e) {
230
+ var href = resolvedRoute.value.route;
231
+ if (guardEvent(e)) {
232
+ return props.replace
233
+ ? router.replace(href)
234
+ : router.push(href)
235
+ }
236
+ return Promise.resolve()
237
+ };
238
+
239
+ return {
240
+ href: kdu.computed(function () { return resolvedRoute.value.href; }),
241
+ route: kdu.computed(function () { return resolvedRoute.value.route; }),
242
+ isExactActive: isExactActive,
243
+ isActive: isActive,
244
+ navigate: navigate
245
+ }
246
+ }
247
+
248
+ exports.isSameRouteLocationParams = isSameRouteLocationParams;
249
+ exports.onBeforeRouteLeave = onBeforeRouteLeave;
250
+ exports.onBeforeRouteUpdate = onBeforeRouteUpdate;
251
+ exports.useLink = useLink;
252
+ exports.useRoute = useRoute;
253
+ exports.useRouter = useRouter;
@@ -0,0 +1,244 @@
1
+ /*!
2
+ * kdu-router v3.6.0
3
+ * (c) 2026 NKDuy
4
+ * @license MIT
5
+ */
6
+ import { getCurrentInstance, effectScope, shallowReactive, onUnmounted, computed, unref } from 'kdu';
7
+
8
+ // dev only warn if no current instance
9
+
10
+ function throwNoCurrentInstance (method) {
11
+ if (!getCurrentInstance()) {
12
+ throw new Error(
13
+ ("[kdu-router]: Missing current instance. " + method + "() must be called inside <script setup> or setup().")
14
+ )
15
+ }
16
+ }
17
+
18
+ function useRouter () {
19
+ if (process.env.NODE_ENV !== 'production') {
20
+ throwNoCurrentInstance('useRouter');
21
+ }
22
+
23
+ return getCurrentInstance().proxy.$root.$router
24
+ }
25
+
26
+ function useRoute () {
27
+ if (process.env.NODE_ENV !== 'production') {
28
+ throwNoCurrentInstance('useRoute');
29
+ }
30
+
31
+ var root = getCurrentInstance().proxy.$root;
32
+ if (!root._$route) {
33
+ var route = effectScope(true).run(function () { return shallowReactive(Object.assign({}, root.$router.currentRoute)); }
34
+ );
35
+ root._$route = route;
36
+
37
+ root.$router.afterEach(function (to) {
38
+ Object.assign(route, to);
39
+ });
40
+ }
41
+
42
+ return root._$route
43
+ }
44
+
45
+ function onBeforeRouteUpdate (guard) {
46
+ if (process.env.NODE_ENV !== 'production') {
47
+ throwNoCurrentInstance('onBeforeRouteUpdate');
48
+ }
49
+
50
+ return useFilteredGuard(guard, isUpdateNavigation)
51
+ }
52
+ function isUpdateNavigation (to, from, depth) {
53
+ var toMatched = to.matched;
54
+ var fromMatched = from.matched;
55
+ return (
56
+ toMatched.length >= depth &&
57
+ toMatched
58
+ .slice(0, depth + 1)
59
+ .every(function (record, i) { return record === fromMatched[i]; })
60
+ )
61
+ }
62
+
63
+ function isLeaveNavigation (to, from, depth) {
64
+ var toMatched = to.matched;
65
+ var fromMatched = from.matched;
66
+ return toMatched.length < depth || toMatched[depth] !== fromMatched[depth]
67
+ }
68
+
69
+ function onBeforeRouteLeave (guard) {
70
+ if (process.env.NODE_ENV !== 'production') {
71
+ throwNoCurrentInstance('onBeforeRouteLeave');
72
+ }
73
+
74
+ return useFilteredGuard(guard, isLeaveNavigation)
75
+ }
76
+
77
+ var noop = function () {};
78
+ function useFilteredGuard (guard, fn) {
79
+ var instance = getCurrentInstance();
80
+ var router = useRouter();
81
+
82
+ var target = instance.proxy;
83
+ // find the nearest RouterView to know the depth
84
+ while (
85
+ target &&
86
+ target.$knode &&
87
+ target.$knode.data &&
88
+ target.$knode.data.routerViewDepth == null
89
+ ) {
90
+ target = target.$parent;
91
+ }
92
+
93
+ var depth =
94
+ target && target.$knode && target.$knode.data
95
+ ? target.$knode.data.routerViewDepth
96
+ : null;
97
+
98
+ if (depth != null) {
99
+ var removeGuard = router.beforeEach(function (to, from, next) {
100
+ return fn(to, from, depth) ? guard(to, from, next) : next()
101
+ });
102
+
103
+ onUnmounted(removeGuard);
104
+ return removeGuard
105
+ }
106
+
107
+ return noop
108
+ }
109
+
110
+ /* */
111
+
112
+ function guardEvent (e) {
113
+ // don't redirect with control keys
114
+ if (e.metaKey || e.altKey || e.ctrlKey || e.shiftKey) { return }
115
+ // don't redirect when preventDefault called
116
+ if (e.defaultPrevented) { return }
117
+ // don't redirect on right click
118
+ if (e.button !== undefined && e.button !== 0) { return }
119
+ // don't redirect if `target="_blank"`
120
+ if (e.currentTarget && e.currentTarget.getAttribute) {
121
+ var target = e.currentTarget.getAttribute('target');
122
+ if (/\b_blank\b/i.test(target)) { return }
123
+ }
124
+ // this may be a Weex event which doesn't have this method
125
+ if (e.preventDefault) {
126
+ e.preventDefault();
127
+ }
128
+ return true
129
+ }
130
+
131
+ function includesParams (outer, inner) {
132
+ var loop = function ( key ) {
133
+ var innerValue = inner[key];
134
+ var outerValue = outer[key];
135
+ if (typeof innerValue === 'string') {
136
+ if (innerValue !== outerValue) { return { v: false } }
137
+ } else {
138
+ if (
139
+ !Array.isArray(outerValue) ||
140
+ outerValue.length !== innerValue.length ||
141
+ innerValue.some(function (value, i) { return value !== outerValue[i]; })
142
+ ) {
143
+ return { v: false }
144
+ }
145
+ }
146
+ };
147
+
148
+ for (var key in inner) {
149
+ var returned = loop( key );
150
+
151
+ if ( returned ) return returned.v;
152
+ }
153
+
154
+ return true
155
+ }
156
+
157
+ // helpers from kdu router 4
158
+
159
+ function isSameRouteLocationParamsValue (a, b) {
160
+ return Array.isArray(a)
161
+ ? isEquivalentArray(a, b)
162
+ : Array.isArray(b)
163
+ ? isEquivalentArray(b, a)
164
+ : a === b
165
+ }
166
+
167
+ function isEquivalentArray (a, b) {
168
+ return Array.isArray(b)
169
+ ? a.length === b.length && a.every(function (value, i) { return value === b[i]; })
170
+ : a.length === 1 && a[0] === b
171
+ }
172
+
173
+ function isSameRouteLocationParams (a, b) {
174
+ if (Object.keys(a).length !== Object.keys(b).length) { return false }
175
+
176
+ for (var key in a) {
177
+ if (!isSameRouteLocationParamsValue(a[key], b[key])) { return false }
178
+ }
179
+
180
+ return true
181
+ }
182
+
183
+ function useLink (props) {
184
+ if (process.env.NODE_ENV !== 'production') {
185
+ throwNoCurrentInstance('useLink');
186
+ }
187
+
188
+ var router = useRouter();
189
+ var currentRoute = useRoute();
190
+
191
+ var resolvedRoute = computed(function () { return router.resolve(unref(props.to), currentRoute); });
192
+
193
+ var activeRecordIndex = computed(function () {
194
+ var route = resolvedRoute.value.route;
195
+ var matched = route.matched;
196
+ var length = matched.length;
197
+ var routeMatched = matched[length - 1];
198
+ var currentMatched = currentRoute.matched;
199
+ if (!routeMatched || !currentMatched.length) { return -1 }
200
+ var index = currentMatched.indexOf(routeMatched);
201
+ if (index > -1) { return index }
202
+ // possible parent record
203
+ var parentRecord = currentMatched[currentMatched.length - 2];
204
+
205
+ return (
206
+ // we are dealing with nested routes
207
+ length > 1 &&
208
+ // if the parent and matched route have the same path, this link is
209
+ // referring to the empty child. Or we currently are on a different
210
+ // child of the same parent
211
+ parentRecord && parentRecord === routeMatched.parent
212
+ )
213
+ });
214
+
215
+ var isActive = computed(
216
+ function () { return activeRecordIndex.value > -1 &&
217
+ includesParams(currentRoute.params, resolvedRoute.value.route.params); }
218
+ );
219
+ var isExactActive = computed(
220
+ function () { return activeRecordIndex.value > -1 &&
221
+ activeRecordIndex.value === currentRoute.matched.length - 1 &&
222
+ isSameRouteLocationParams(currentRoute.params, resolvedRoute.value.route.params); }
223
+ );
224
+
225
+ var navigate = function (e) {
226
+ var href = resolvedRoute.value.route;
227
+ if (guardEvent(e)) {
228
+ return props.replace
229
+ ? router.replace(href)
230
+ : router.push(href)
231
+ }
232
+ return Promise.resolve()
233
+ };
234
+
235
+ return {
236
+ href: computed(function () { return resolvedRoute.value.href; }),
237
+ route: computed(function () { return resolvedRoute.value.route; }),
238
+ isExactActive: isExactActive,
239
+ isActive: isActive,
240
+ navigate: navigate
241
+ }
242
+ }
243
+
244
+ export { isSameRouteLocationParams, onBeforeRouteLeave, onBeforeRouteUpdate, useLink, useRoute, useRouter };