@esmx/router 3.0.0-rc.12
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/dist/history/abstract.d.ts +29 -0
- package/dist/history/abstract.mjs +107 -0
- package/dist/history/base.d.ts +79 -0
- package/dist/history/base.mjs +275 -0
- package/dist/history/html.d.ts +22 -0
- package/dist/history/html.mjs +181 -0
- package/dist/history/index.d.ts +7 -0
- package/dist/history/index.mjs +16 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.mjs +3 -0
- package/dist/matcher/create-matcher.d.ts +5 -0
- package/dist/matcher/create-matcher.mjs +218 -0
- package/dist/matcher/create-matcher.spec.d.ts +1 -0
- package/dist/matcher/create-matcher.spec.mjs +0 -0
- package/dist/matcher/index.d.ts +1 -0
- package/dist/matcher/index.mjs +1 -0
- package/dist/router.d.ts +111 -0
- package/dist/router.mjs +399 -0
- package/dist/task-pipe/index.d.ts +1 -0
- package/dist/task-pipe/index.mjs +1 -0
- package/dist/task-pipe/task.d.ts +30 -0
- package/dist/task-pipe/task.mjs +66 -0
- package/dist/utils/bom.d.ts +5 -0
- package/dist/utils/bom.mjs +10 -0
- package/dist/utils/encoding.d.ts +48 -0
- package/dist/utils/encoding.mjs +44 -0
- package/dist/utils/guards.d.ts +9 -0
- package/dist/utils/guards.mjs +12 -0
- package/dist/utils/index.d.ts +7 -0
- package/dist/utils/index.mjs +27 -0
- package/dist/utils/path.d.ts +60 -0
- package/dist/utils/path.mjs +264 -0
- package/dist/utils/path.spec.d.ts +1 -0
- package/dist/utils/path.spec.mjs +30 -0
- package/dist/utils/scroll.d.ts +25 -0
- package/dist/utils/scroll.mjs +59 -0
- package/dist/utils/utils.d.ts +16 -0
- package/dist/utils/utils.mjs +11 -0
- package/dist/utils/warn.d.ts +2 -0
- package/dist/utils/warn.mjs +12 -0
- package/package.json +66 -0
- package/src/history/abstract.ts +149 -0
- package/src/history/base.ts +408 -0
- package/src/history/html.ts +231 -0
- package/src/history/index.ts +20 -0
- package/src/index.ts +3 -0
- package/src/matcher/create-matcher.spec.ts +3 -0
- package/src/matcher/create-matcher.ts +293 -0
- package/src/matcher/index.ts +1 -0
- package/src/router.ts +521 -0
- package/src/task-pipe/index.ts +1 -0
- package/src/task-pipe/task.ts +97 -0
- package/src/utils/bom.ts +14 -0
- package/src/utils/encoding.ts +153 -0
- package/src/utils/guards.ts +25 -0
- package/src/utils/index.ts +27 -0
- package/src/utils/path.spec.ts +44 -0
- package/src/utils/path.ts +397 -0
- package/src/utils/scroll.ts +120 -0
- package/src/utils/utils.ts +30 -0
- package/src/utils/warn.ts +13 -0
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
RouterRawLocation,
|
|
3
|
+
ScrollPosition,
|
|
4
|
+
ScrollPositionCoordinates,
|
|
5
|
+
_ScrollPositionNormalized
|
|
6
|
+
} from '../types';
|
|
7
|
+
import { warn } from './warn';
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* 获取当前滚动位置
|
|
11
|
+
*/
|
|
12
|
+
export const computeScrollPosition = (): _ScrollPositionNormalized => ({
|
|
13
|
+
left: window.scrollX,
|
|
14
|
+
top: window.scrollY
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* 获取元素位置
|
|
19
|
+
*/
|
|
20
|
+
function getElementPosition(
|
|
21
|
+
el: Element,
|
|
22
|
+
offset: ScrollPositionCoordinates
|
|
23
|
+
): _ScrollPositionNormalized {
|
|
24
|
+
const docRect = document.documentElement.getBoundingClientRect();
|
|
25
|
+
const elRect = el.getBoundingClientRect();
|
|
26
|
+
|
|
27
|
+
return {
|
|
28
|
+
behavior: offset.behavior,
|
|
29
|
+
left: elRect.left - docRect.left - (offset.left || 0),
|
|
30
|
+
top: elRect.top - docRect.top - (offset.top || 0)
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* 滚动到指定位置
|
|
36
|
+
*/
|
|
37
|
+
export function scrollToPosition(position: ScrollPosition): void {
|
|
38
|
+
let scrollToOptions: ScrollPositionCoordinates;
|
|
39
|
+
|
|
40
|
+
if ('el' in position) {
|
|
41
|
+
const positionEl = position.el;
|
|
42
|
+
const isIdSelector =
|
|
43
|
+
typeof positionEl === 'string' && positionEl.startsWith('#');
|
|
44
|
+
|
|
45
|
+
const el =
|
|
46
|
+
typeof positionEl === 'string'
|
|
47
|
+
? isIdSelector
|
|
48
|
+
? document.getElementById(positionEl.slice(1))
|
|
49
|
+
: document.querySelector(positionEl)
|
|
50
|
+
: positionEl;
|
|
51
|
+
|
|
52
|
+
if (!el) {
|
|
53
|
+
return;
|
|
54
|
+
}
|
|
55
|
+
scrollToOptions = getElementPosition(el, position);
|
|
56
|
+
} else {
|
|
57
|
+
scrollToOptions = position;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
if ('scrollBehavior' in document.documentElement.style) {
|
|
61
|
+
window.scrollTo(scrollToOptions);
|
|
62
|
+
} else {
|
|
63
|
+
window.scrollTo(
|
|
64
|
+
scrollToOptions.left != null
|
|
65
|
+
? scrollToOptions.left
|
|
66
|
+
: window.scrollX,
|
|
67
|
+
scrollToOptions.top != null ? scrollToOptions.top : window.scrollY
|
|
68
|
+
);
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* 存储的滚动位置
|
|
74
|
+
*/
|
|
75
|
+
export const scrollPositions = new Map<string, _ScrollPositionNormalized>();
|
|
76
|
+
|
|
77
|
+
const POSITION_KEY = '__scroll_position_key';
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* 保存滚动位置
|
|
81
|
+
*/
|
|
82
|
+
export function saveScrollPosition(
|
|
83
|
+
key: string,
|
|
84
|
+
scrollPosition: _ScrollPositionNormalized
|
|
85
|
+
) {
|
|
86
|
+
scrollPositions.set(key, scrollPosition);
|
|
87
|
+
// preserve existing history state as it could be overriden by the user
|
|
88
|
+
const stateCopy = Object.assign({}, window.history.state);
|
|
89
|
+
stateCopy[POSITION_KEY] = scrollPosition;
|
|
90
|
+
|
|
91
|
+
try {
|
|
92
|
+
const protocolAndPath =
|
|
93
|
+
window.location.protocol + '//' + window.location.host;
|
|
94
|
+
const absolutePath = window.location.href.replace(protocolAndPath, '');
|
|
95
|
+
window.history.replaceState(stateCopy, '', absolutePath);
|
|
96
|
+
} catch (error) {
|
|
97
|
+
warn(`Failed to save scroll position.`, error);
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* 获取存储的滚动位置
|
|
103
|
+
*/
|
|
104
|
+
export function getSavedScrollPosition(
|
|
105
|
+
key: string
|
|
106
|
+
): _ScrollPositionNormalized | null {
|
|
107
|
+
const scroll = scrollPositions.get(key) || history.state[POSITION_KEY];
|
|
108
|
+
|
|
109
|
+
// 保存的滚动位置信息不应当被多次使用, 下一次应当使用新保存的位置信息
|
|
110
|
+
scrollPositions.delete(key);
|
|
111
|
+
return scroll || null;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/**
|
|
115
|
+
* 获取跳转配置的保持滚动位置参数
|
|
116
|
+
*/
|
|
117
|
+
export function getKeepScrollPosition(location: RouterRawLocation): boolean {
|
|
118
|
+
if (typeof location === 'string') return false;
|
|
119
|
+
return location.keepScrollPosition || false;
|
|
120
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 是否在浏览器环境
|
|
3
|
+
*/
|
|
4
|
+
export const inBrowser = typeof window !== 'undefined';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Symbol 是否可用
|
|
8
|
+
*/
|
|
9
|
+
export const isSymbolAble =
|
|
10
|
+
typeof Symbol === 'function' && typeof Symbol.toStringTag === 'symbol';
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* 判断是否是 es module 对象
|
|
14
|
+
*/
|
|
15
|
+
export function isESModule(obj: any): boolean {
|
|
16
|
+
return (
|
|
17
|
+
Boolean(obj.__esModule) ||
|
|
18
|
+
(isSymbolAble && obj[Symbol.toStringTag] === 'Module')
|
|
19
|
+
);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* 判断是否是合法的值
|
|
24
|
+
*/
|
|
25
|
+
export function isValidValue(value: any): boolean {
|
|
26
|
+
if (value === null) return false;
|
|
27
|
+
if (value === undefined) return false;
|
|
28
|
+
if (typeof value === 'number' && isNaN(value)) return false;
|
|
29
|
+
return true;
|
|
30
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
export function assert(condition: boolean, message: string) {
|
|
2
|
+
if (!condition) {
|
|
3
|
+
throw new Error(`[@esmx/router] ${message}`);
|
|
4
|
+
}
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
export function warn(...args: any[]) {
|
|
8
|
+
console.log(
|
|
9
|
+
'%c ROUTER WARNING:',
|
|
10
|
+
'color: rgb(214, 77, 77); font-weight: bold;',
|
|
11
|
+
...args
|
|
12
|
+
);
|
|
13
|
+
}
|