-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
doc: 增加部分注释 #1
base: master
Are you sure you want to change the base?
doc: 增加部分注释 #1
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,7 +6,13 @@ import { ReactNode, createContext, useContext, useState } from "react"; | |
* @since 0.1.0 | ||
* @version 1.0.0 | ||
*/ | ||
function combine(paths: readonly any[]): string { | ||
|
||
/** | ||
* 拼接Url并消除多余的斜杠 | ||
* | ||
* @param paths | ||
*/ | ||
function combineUrl(paths: readonly any[]): string { | ||
const p = paths | ||
.filter(x => x) | ||
.map(x => x!.toString().replace(/^\/+/, "")) | ||
|
@@ -30,13 +36,17 @@ interface IRouter { | |
} | ||
|
||
/** | ||
* 路由方法 | ||
HarryG55 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
* | ||
* @author Aloento | ||
* @since 0.1.0 | ||
* @version 1.0.0 | ||
*/ | ||
const Router = createContext({} as IRouter); | ||
|
||
/** | ||
* 上下文更新路由信息 | ||
HarryG55 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
* | ||
* @author Aloento | ||
* @since 0.1.0 | ||
* @version 1.0.0 | ||
|
@@ -46,26 +56,35 @@ export function useRouter() { | |
} | ||
|
||
/** | ||
* 根据路径解析路由并刷新组件 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 没有直接刷新组件的操作,都是间接影响 |
||
* | ||
* @author Aloento | ||
* @since 0.1.0 | ||
* @version 1.0.0 | ||
*/ | ||
export function BrowserRouter({ children }: { children: ReactNode }): ReactNode { | ||
const [router, setRouter] = useState<IRouter>(() => ({ | ||
// 解析当前路径 | ||
Paths: location.pathname.split("/").filter(x => x), | ||
// 获取查询入参 | ||
Search: new URLSearchParams(location.search), | ||
Put: put, | ||
Nav: (...p) => nav(combine(p)), | ||
Rep: (...p) => rep(combine(p)), | ||
Nav: (...p) => nav(combineUrl(p)), | ||
Rep: (...p) => replace(combineUrl(p)), | ||
Reload: (...p) => reload(p), | ||
})); | ||
|
||
function put(search: URLSearchParams) { | ||
// 替换新路由条目 | ||
history.replaceState(null, "", `${location.pathname}${search.size ? "?" : ""}${search.toString()}`); | ||
router.Search = new URLSearchParams(search); | ||
// 触发重新渲染 | ||
setRouter({ ...router }); | ||
} | ||
|
||
* 刷新页面 | ||
HarryG55 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
* @param path | ||
HarryG55 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
*/ | ||
function update(path: string) { | ||
router.Paths = path.split("/").filter(x => x); | ||
router.Search = new URLSearchParams(location.search); | ||
|
@@ -77,37 +96,49 @@ export function BrowserRouter({ children }: { children: ReactNode }): ReactNode | |
update(path); | ||
} | ||
|
||
function rep(path: string) { | ||
function replace(path: string) { | ||
history.replaceState(null, "", path); | ||
update(path); | ||
} | ||
|
||
/** | ||
* 重载页面:访问Reload路径后,访问目标路径 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 访问Reload的目的是完全卸载页面呈现的组件 |
||
* @param paths 重载路径 | ||
*/ | ||
function reload(paths: readonly string[]) { | ||
history.replaceState(null, "", "/Reload"); | ||
update("/Reload"); | ||
|
||
setTimeout(() => { | ||
const path = paths.length ? combine(paths) : location.pathname; | ||
const path = paths.length ? combineUrl(paths) : location.pathname; | ||
history.pushState(null, "", path); | ||
update(path); | ||
}, 100); | ||
} | ||
|
||
/** | ||
* 组件挂载初始化逻辑 | ||
*/ | ||
useMount(() => { | ||
// ?/路径数据清除 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ?/是怎么来的,为什么要清除,清除后又做了什么? |
||
if (location.pathname === "/") | ||
if (location.search.startsWith("?/")) | ||
rep(location.search.substring(2)); | ||
replace(location.search.substring(2)); | ||
|
||
|
||
addEventListener("click", e => { | ||
// 外部网站跳转校验 | ||
const target = (e.target as HTMLElement)?.closest("a"); | ||
|
||
if (target) { | ||
// 禁止外部网站跳转 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 啊? |
||
if (target.origin !== location.origin) { | ||
target.target = "_blank"; | ||
return; | ||
} | ||
|
||
|
||
// 阻止浏览器默认操作 | ||
e.preventDefault(); | ||
// 刷新组件并压栈 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ?导航到新页面? |
||
nav(target.pathname); | ||
} | ||
}); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
你破坏了原有的注释