feat: recursive toggle expand

This commit is contained in:
EnixCoda 2020-08-30 02:24:24 +08:00
parent 56a97040d8
commit fde2ad90df
No known key found for this signature in database
GPG key ID: 0C1A07377913A1DD
2 changed files with 31 additions and 17 deletions

View file

@ -213,11 +213,14 @@ export const setExpand: BoundMethodCreator<[TreeNode, boolean]> = dispatch => (
dispatch.call(focusNode, node, false)
}
export const toggleNodeExpansion: BoundMethodCreator<[TreeNode, boolean]> = dispatch => (
node,
skipScrollToNode,
) => {
visibleNodesGenerator.toggleExpand(node)
export const toggleNodeExpansion: BoundMethodCreator<[
TreeNode,
{
skipScrollToNode?: boolean
recursive?: boolean
},
]> = dispatch => (node, { skipScrollToNode = false, recursive = false }) => {
visibleNodesGenerator.toggleExpand(node, recursive)
dispatch.call(focusNode, node, skipScrollToNode)
tasksAfterRender.push(DOMHelper.focusFileExplorer)
}
@ -235,25 +238,22 @@ export const onNodeClick: BoundMethodCreator<[
React.MouseEvent<HTMLElement, MouseEvent>,
TreeNode,
]> = dispatch => (event, node) => {
let preventDefault = true
const preventDefault = !(node.type === 'blob' && node.url?.includes('#'))
if (preventDefault) event.preventDefault()
if (node.type === 'tree') {
dispatch.call(toggleNodeExpansion, node, true)
dispatch.call(toggleNodeExpansion, node, { skipScrollToNode: true, recursive: event.shiftKey })
} else if (node.type === 'blob') {
const [, { loadWithPJAX }] = dispatch.get()
dispatch.call(focusNode, node, true)
if (node.url) {
if (node.url.includes('#')) {
preventDefault = false
} else {
loadWithPJAX(node.url)
}
if (node.url && !node.url.includes('#')) {
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 => {

View file

@ -139,17 +139,31 @@ class L3 {
this.l2 = l2
}
toggleExpand = (node: TreeNode) => {
this.setExpand(node, !this.expandedNodes.has(node.path))
toggleExpand = (node: TreeNode, recursive?: boolean) => {
const expand = !this.expandedNodes.has(node.path)
if (recursive) {
const recursiveSetExpand = (node: TreeNode, expand: boolean) => {
this.barelySetExpand(node, expand)
node.contents?.forEach($node => recursiveSetExpand($node, expand))
}
recursiveSetExpand(node, expand)
this.generateVisibleNodes()
} else {
this.setExpand(node, expand)
}
}
setExpand = (node: TreeNode, expand: boolean) => {
barelySetExpand = (node: TreeNode, expand: boolean) => {
if (expand && node.contents) {
// only node with contents is expandable
this.expandedNodes.add(node.path)
} else {
this.expandedNodes.delete(node.path)
}
}
setExpand = (node: TreeNode, expand: boolean) => {
this.barelySetExpand(node, expand)
this.generateVisibleNodes()
}