Skip to content

Commit

Permalink
version 1.2.3. Add TypeScript Var Export
Browse files Browse the repository at this point in the history
  • Loading branch information
iuroc committed Jan 10, 2024
1 parent 9616932 commit 7289d70
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 17 deletions.
9 changes: 4 additions & 5 deletions js/index.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import van from 'vanjs-core';
const { div } = van.tags;
/** 从 `location.hash` 获取当前路由 */
const nowRoute = () => {
export const nowRoute = () => {
const li = location.hash.split('/');
const route = { name: li[1] ?? 'home', args: li.slice(2) };
return route;
};
const activeRoute = van.state(nowRoute());
export const activeRoute = van.state(nowRoute());
window.addEventListener('hashchange', () => {
activeRoute.val = nowRoute();
});
const Route = (first, ...rest) => {
export const Route = (first, ...rest) => {
const { name, onFirst, onLoad, ...otherProp } = first;
let firstLoad = true;
van.derive(() => {
Expand All @@ -25,7 +25,7 @@ const Route = (first, ...rest) => {
});
return div({ hidden: () => first.name != activeRoute.val.name, ...otherProp }, rest);
};
const routeTo = (name = 'home', args = []) => {
export const routeTo = (name = 'home', args = []) => {
if (args.length == 0) {
if (name == 'home') {
location.hash = '';
Expand All @@ -37,4 +37,3 @@ const routeTo = (name = 'home', args = []) => {
else
location.hash = `/${name}/${args.join('/')}`;
};
export { Route, routeTo };
File renamed without changes.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"vanjs-core": "^1.2.7"
},
"name": "vanjs-router",
"version": "1.2.2",
"version": "1.2.3",
"description": "基于 Van.js 的前端路由控制系统",
"main": "js/index.js",
"types": "src/index.ts",
Expand Down
16 changes: 7 additions & 9 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ import van, { ChildDom, PropValueOrDerived, Props, PropsWithKnownKeys } from 'va
const { div } = van.tags

/** 来自 `location.hash` 的路由信息 */
interface Route {
export interface Route {
/** 路由名称 */
readonly name: string
/** 路由参数 */
readonly args: readonly string[]
}

interface RouteSetting {
export interface RouteSetting {
/** 路由名称 */
readonly name: string
/** 初次路由载入 */
Expand All @@ -21,19 +21,19 @@ interface RouteSetting {
}

/** 从 `location.hash` 获取当前路由 */
const nowRoute = () => {
export const nowRoute = () => {
const li = location.hash.split('/')
const route: Route = { name: li[1] ?? 'home', args: li.slice(2) }
return route
}

const activeRoute = van.state(nowRoute())
export const activeRoute = van.state(nowRoute())

window.addEventListener('hashchange', () => {
activeRoute.val = nowRoute()
})

const Route = (first: RouteSetting & Props & PropsWithKnownKeys<HTMLDivElement>, ...rest: readonly ChildDom[]) => {
export const Route = (first: RouteSetting & Props & PropsWithKnownKeys<HTMLDivElement>, ...rest: readonly ChildDom[]) => {
const { name, onFirst, onLoad, ...otherProp } = first
let firstLoad = true
van.derive(() => {
Expand All @@ -48,13 +48,11 @@ const Route = (first: RouteSetting & Props & PropsWithKnownKeys<HTMLDivElement>,
return div({ hidden: () => first.name != activeRoute.val.name, ...otherProp }, rest)
}

const routeTo = (name: Route['name'] = 'home', args: any[] = []) => {
export const routeTo = (name: Route['name'] = 'home', args: any[] = []) => {
if (args.length == 0) {
if (name == 'home') {
location.hash = ''
history.replaceState(null, '', './')
} else location.hash = `/${name}`
} else location.hash = `/${name}/${args.join('/')}`
}

export { Route, routeTo }
}

0 comments on commit 7289d70

Please sign in to comment.