Skip to content

Commit 47c1891

Browse files
authored
Merge pull request #30 from wu-component/feature/ui-component-marvin
Default Changelist
2 parents ae2ce50 + 5974ba8 commit 47c1891

File tree

14 files changed

+138
-190
lines changed

14 files changed

+138
-190
lines changed

packages/wu-core-router/dev/webpack_dev.config.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@ module.exports = {
88
path: path.resolve(__dirname, '../', "dist"),
99
filename: "bundle.[chunkhash:8].js",
1010
publicPath: '/',
11+
library: {
12+
name: 'wuRouter',
13+
type: 'umd'
14+
}
15+
// name: 'wuRouter',
1116
},
1217
target: 'web',
1318
resolve: {
@@ -17,7 +22,8 @@ module.exports = {
1722
/*new webpack.EvalSourceMapDevToolPlugin({}),*/
1823
new webpack.HotModuleReplacementPlugin(),
1924
new HtmlWebpackPlugin({
20-
template: "./public/index.html"
25+
template: "./public/index.html",
26+
scriptLoading: 'blocking'
2127
}),
2228
/*...commonPlugins*/
2329

packages/wu-core-router/package.json

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,7 @@
3131
"dist",
3232
"LICENSE",
3333
"README.md",
34-
"src/app-data",
35-
"src/declarations",
36-
"src/decorators",
37-
"src/index.ts",
38-
"src/packages",
39-
"src/reactivity",
40-
"src/runtime",
41-
"src/utils"
34+
"src"
4235
],
4336
"devDependencies": {
4437
"@babel/preset-typescript": "^7.13.0",
@@ -89,9 +82,11 @@
8982
"webpack-cli": "^4.9.2",
9083
"webpack-dev-server": "^4.9.0"
9184
},
85+
"peerDependencies": {
86+
"@canyuegongzi/web-core-plus": "file:../web-core-plus",
87+
"reflect-metadata": "^0.1.13"
88+
},
9289
"dependencies": {
93-
"path-to-regexp": "^6.2.1",
94-
"@canyuegongzi/web-core-plus": "file:../web-core-plus",
95-
"reflect-metadata": "^0.1.13"
90+
"path-to-regexp": "^6.2.1"
9691
}
9792
}

packages/wu-core-router/public/index.html

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,6 @@
2727
<script src="./core/index.umd.js"></script>
2828
<!--引入对应的组件产物-->
2929
<script src="./ui/index.umd.js"></script>
30-
<script type="text/javascript">
31-
function linkView(path) {
32-
window.router.push(path);
33-
}
34-
35-
</script>
3630
</head>
3731
<body>
3832
<div class="router-link-container">
@@ -113,3 +107,21 @@
113107
</div>
114108
</body>
115109
</html>
110+
<script type="text/javascript">
111+
function linkView(path) {
112+
window.router.push(path, { path: path });
113+
}
114+
115+
const options = {
116+
type: 'hash',
117+
after: (e) => {
118+
return true
119+
},
120+
before: (e) => {
121+
return true;
122+
}
123+
};
124+
window.router = new wuRouter.Router(options);
125+
console.log(window.router)
126+
127+
</script>

packages/wu-core-router/rollup.config.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,19 @@ export default [
2828
})
2929
],
3030
output: {
31-
name: 'webCorePlus',
31+
name: 'wuRouter',
3232
file: pkg.browser,
33-
format: 'umd'
33+
format: 'umd',
34+
globals: {
35+
'@canyuegongzi/web-core-plus': 'webCorePlus'
36+
}
3437
},
38+
external: [ /web-core-plus$/ ],
3539
},
3640
{
3741
input: './src/index.ts',
3842
plugins: [
39-
// terser(),
43+
terser(),
4044
nodeResolve(),
4145
commonjs(),
4246
postcss({
@@ -57,5 +61,6 @@ export default [
5761
{ file: pkg.main, format: 'cjs' },
5862
{ file: pkg.module, format: 'es' }
5963
],
64+
external: [ /web-core-plus$/ ],
6065
}
6166
];

packages/wu-core-router/src/common/util.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,27 @@ export function getUrlParams(url) {
2121
}
2222
return args;
2323
}
24+
25+
export function decodeParams(url: string, obj: Record<any, any> = {}) {
26+
let result = '';
27+
let item;
28+
if (url.includes("?")) {
29+
for (item in obj) {
30+
if (obj[item] && String(obj[item])) {
31+
result += `${item}=${obj[item]}&`;
32+
}
33+
}
34+
}else{
35+
result += "?";
36+
for (item in obj) {
37+
if (obj[item] && String(obj[item])) {
38+
result += `${item}=${obj[item]}&`;
39+
}
40+
}
41+
}
42+
let str = url + result;
43+
if (str.endsWith('&')) {
44+
str = str.substring(0, str.length - 1);
45+
}
46+
return str
47+
}

packages/wu-core-router/src/example/index.tsx

Lines changed: 0 additions & 11 deletions
This file was deleted.
Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,2 @@
11
export * from './router';
22
export * from './view';
3-
4-
import './example/index';

packages/wu-core-router/src/lib/index.ts

Lines changed: 0 additions & 121 deletions
This file was deleted.
Lines changed: 48 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,46 @@
1-
import { CommonRouter } from "./common";
1+
import { CommonRouter, RouterOptions } from "./common";
22
import RouterConfig from "../router/RouterConfig";
3-
import { getParams, getUrlParams } from "../common";
3+
import { decodeParams, getParams, getUrlParams } from "../common";
44

55
export class HashRouter extends CommonRouter {
6-
constructor() {
7-
super();
6+
constructor(options: RouterOptions) {
7+
super(options);
88
this.push = this.push.bind(this);
99
this.back = this.back.bind(this);
1010
}
11-
public back(...args): void {}
1211

12+
/**
13+
* 路由回退
14+
* @param args
15+
*/
16+
public back(...args): void {
17+
window.history.back();
18+
}
19+
20+
/**
21+
* 路由跳转
22+
* @param path
23+
* @param data
24+
* @param args
25+
*/
1326
public push(path: string, data?: Record<any, any>, ...args): void {
1427
this.routeByTo = true;
15-
this.data = data;
28+
this.query = data || {};
1629
if (path[0] === '#') {
17-
location.hash = path;
30+
location.hash = decodeParams(path, data);
1831
} else {
19-
location.hash = '#' + path;
32+
location.hash = '#' + decodeParams(path, data);
2033
}
2134
}
2235

2336
/**
2437
* 初始化路由监听
2538
*/
26-
public init(): void {
39+
public init(options: RouterOptions): void {
2740
window.addEventListener('hashchange', this.hashChange.bind(this));
2841
document.addEventListener('DOMContentLoaded', this.hashChange.bind(this));
42+
this.after = options.after? options.after.bind(this): () => true;
43+
this.before = options.before? options.before.bind(this): () => true;
2944
}
3045

3146
/**
@@ -38,9 +53,6 @@ export class HashRouter extends CommonRouter {
3853
}
3954
this.routeByTo = false;
4055
this.historyLength = window.history.length;
41-
const prevent = false;
42-
// TODO 处理 before
43-
if (prevent) return;
4456
let path = window.location.hash.replace('#', '');
4557
if (path === '') path = '/';
4658

@@ -49,17 +61,35 @@ export class HashRouter extends CommonRouter {
4961
if (toArr) {
5062
const pathArr = key.match(RouterConfig.routeMap[key].reg);
5163
this.params = getParams(toArr, pathArr);
52-
this.query = getUrlParams(path);
64+
this.query = this.query? { ...this.query, ...getUrlParams(path) }: getUrlParams(path);
5365
this.data = null;
54-
RouterConfig.routerViewContainer.default.shadowRoot.innerHTML = '';
55-
RouterConfig.routeMap[key].element.forEach(item => {
56-
RouterConfig.routerViewContainer.default.shadowRoot.appendChild(item);
57-
});
58-
66+
if (this.before({ evt, query: this.query, params: this.params, data: this.data })) {
67+
RouterConfig.routerViewContainer.default.shadowRoot.innerHTML = '';
68+
RouterConfig.routeMap[key].element.forEach(item => {
69+
RouterConfig.routerViewContainer.default.shadowRoot.appendChild(item);
70+
});
71+
this.after?.(evt);
72+
}
5973
return false;
6074
}
6175
return true;
6276
});
6377
}
6478

79+
/**
80+
* 路由前置拦截器
81+
* @param args
82+
*/
83+
public after(...args): boolean {
84+
return true;
85+
}
86+
87+
/**
88+
* 路由后置拦截器
89+
* @param args
90+
*/
91+
public before(...args): boolean {
92+
return true;
93+
}
94+
6595
}

0 commit comments

Comments
 (0)