refactor: handle hash links natively

This commit is contained in:
EnixCoda 2020-06-20 19:18:50 +08:00
parent 9050313ad1
commit 328a04f9bb
No known key found for this signature in database
GPG key ID: 0C1A07377913A1DD
3 changed files with 16 additions and 6 deletions

View file

@ -175,7 +175,7 @@ function ListView({
width: number
focusedNode: TreeNode | null
searchKey: string
onNodeClick(node: TreeNode): void
onNodeClick(event: React.MouseEvent<HTMLElement, MouseEvent>, node: TreeNode): void
renderActions?(node: TreeNode): React.ReactNode
visibleNodes: VisibleNodes
} & Pick<Props, 'metaData'> &

View file

@ -19,7 +19,7 @@ function getIconType(node: TreeNode) {
type Props = {
node: TreeNode
onClick(node: TreeNode): void
onClick(event: React.MouseEvent<HTMLElement, MouseEvent>, node: TreeNode): void
depth: number
expanded: boolean
focused: boolean
@ -49,9 +49,8 @@ export function Node({
// The default behavior, open in new tab
return
}
event.preventDefault()
onClick(node)
onClick(event, node)
}}
className={cx(`node-item`, { focused, disabled: node.accessDenied, expanded })}
style={{ ...style, paddingLeft: `${10 + 20 * depth}px` }}

View file

@ -230,18 +230,29 @@ export const focusNode: BoundMethodCreator<[TreeNode | null, boolean]> = dispatc
dispatch.call(updateVisibleNodes)
}
export const onNodeClick: BoundMethodCreator<[TreeNode]> = dispatch => node => {
export const onNodeClick: BoundMethodCreator<[
React.MouseEvent<HTMLElement, MouseEvent>,
TreeNode,
]> = dispatch => (event, node) => {
let preventDefault = true
if (node.type === 'tree') {
dispatch.call(toggleNodeExpansion, node, true)
} else if (node.type === 'blob') {
const [, { loadWithPJAX }] = dispatch.get()
dispatch.call(focusNode, node, true)
if (node.url) loadWithPJAX(node.url)
if (node.url) {
if (node.url.startsWith('#')) {
preventDefault = false
} else {
loadWithPJAX(node.url)
}
}
} else if (node.type === 'commit') {
if (node.url) {
window.open(node.url, '_blank')
}
}
if (preventDefault) event.preventDefault()
}
export const expandTo: BoundMethodCreator<[string[]]> = dispatch => currentPath => {