-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
> 优化 files 产物预览相关功能 * 🧶 支持路径,行号在 url 中映射,`/package/:name/files/:path#Ld-d` 便于分享 * 🚗 monaco loader 配置 npmmirror cdn ![image](https://github.com/cnpm/cnpmweb/assets/5574625/ab18424a-5899-49ef-808f-4f56fa013730)
- Loading branch information
Showing
7 changed files
with
126 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import { useRouter } from 'next/router'; | ||
import { useState, useEffect } from 'react'; | ||
|
||
export function parseHash(hash: string) { | ||
const singleLineMatch = hash?.match(/^L(\d+)$/); | ||
const multiLineMatch = hash?.match(/^L(\d+)-L(\d+)$/); | ||
if (singleLineMatch) { | ||
const line = parseInt(singleLineMatch[1], 10); | ||
return [line, line]; | ||
} else if (multiLineMatch) { | ||
const startLine = parseInt(multiLineMatch[1], 10); | ||
const endLine = parseInt(multiLineMatch[2], 10); | ||
return [startLine, endLine]; | ||
} | ||
return [null, null]; | ||
} | ||
|
||
export default function useHighlightHash() { | ||
const router = useRouter(); | ||
const [highlightRange, setHighlightRange] = useState<any>([null, null]); | ||
|
||
useEffect(() => { | ||
const res = parseHash(router.asPath.split('#')[1]); | ||
setHighlightRange(res); | ||
}, [router.asPath]); | ||
|
||
const setHashFromSelection = (start: number, end: number) => { | ||
const { pathname, search } = window.location; | ||
|
||
if (start === end) { | ||
router.replace(`${pathname}${search}#L${start}`, undefined, { shallow: true }); | ||
} else { | ||
router.replace(`${pathname}${search}#L${start}-L${end}`, undefined, { shallow: true }); | ||
} | ||
}; | ||
|
||
return [highlightRange, setHashFromSelection]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { useState, useEffect, useMemo } from 'react'; | ||
import { useRouter } from 'next/router'; | ||
|
||
export function usePathState(pattern: string): [string, (newValue: string) => void] { | ||
const router = useRouter(); | ||
const regexPattern = useMemo(() => { | ||
return new RegExp(pattern.replace('*', '(.*)')); | ||
}, [pattern]); | ||
const match = router.asPath.match(regexPattern); | ||
|
||
const [state, setState] = useState(match ? match[1] : ''); | ||
|
||
useEffect(() => { | ||
const handleRouteChange = (url: string) => { | ||
const match = url.match(regexPattern); | ||
setState(match ? match[1] : ''); | ||
}; | ||
|
||
router.events.on('routeChangeComplete', handleRouteChange); | ||
return () => { | ||
router.events.off('routeChangeComplete', handleRouteChange); | ||
}; | ||
}, [router, regexPattern]); | ||
|
||
const setPathState = (newValue: string) => { | ||
const newPath = pattern.replace('*', newValue.replace(/^\//, '')); | ||
router.replace(newPath, newPath, { shallow: true }); | ||
}; | ||
|
||
return [state?.split('#')[0], setPathState]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters