Skip to content

Commit

Permalink
Extract tree expand/collapse logic as hook
Browse files Browse the repository at this point in the history
  • Loading branch information
huchenlei committed Aug 24, 2024
1 parent f4242f8 commit 10ad3d4
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 42 deletions.
46 changes: 4 additions & 42 deletions src/components/sidebar/tabs/NodeLibrarySidebarTab.vue
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,12 @@ import { useSettingStore } from '@/stores/settingStore'
import { app } from '@/scripts/app'
import { sortedTree } from '@/utils/treeUtil'
import _ from 'lodash'
import { useTreeExpansion } from '@/hooks/treeHooks'
const nodeDefStore = useNodeDefStore()
const { expandedKeys, expandNode, onNonLeafClick } = useTreeExpansion()
const alphabeticalSort = ref(false)
const expandedKeys = ref({})
const hoveredComfyNodeName = ref<string | null>(null)
const hoveredComfyNode = computed<ComfyNodeDefImpl | null>(() => {
if (!hoveredComfyNodeName.value) {
Expand Down Expand Up @@ -232,22 +234,6 @@ const handleNodeHover = async (
}
}
const toggleNode = (node: TreeNode) => {
if (node.key in expandedKeys.value) {
delete expandedKeys.value[node.key]
} else {
expandedKeys.value[node.key] = true
}
}
const toggleNodeRecursive = (node: TreeNode) => {
if (node.key in expandedKeys.value) {
collapseNode(node)
} else {
expandNode(node)
}
}
const insertNode = (nodeDef: ComfyNodeDefImpl) => {
app.addNodeOnGraph(nodeDef, { pos: app.getCanvasCenter() })
}
Expand All @@ -268,34 +254,10 @@ const handleSearch = (query: string) => {
expandNode(filteredRoot.value)
}
const expandNode = (node: TreeNode) => {
if (node.children && node.children.length) {
expandedKeys.value[node.key] = true
for (let child of node.children) {
expandNode(child)
}
}
}
const collapseNode = (node: TreeNode) => {
if (node.children && node.children.length) {
delete expandedKeys.value[node.key]
for (let child of node.children) {
collapseNode(child)
}
}
}
const onNodeContentClick = (e: MouseEvent, node: TreeNode) => {
if (!node.key) return
if (node.type === 'folder') {
if (e.ctrlKey) {
toggleNodeRecursive(node)
} else {
toggleNode(node)
}
onNonLeafClick(e, node)
} else {
insertNode(node.data)
}
Expand Down
73 changes: 73 additions & 0 deletions src/hooks/treeHooks.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import { ref } from 'vue'
import type { TreeNode } from 'primevue/treenode'

export function useTreeExpansion() {
const expandedKeys = ref<Record<string, boolean>>({})

const toggleNode = (node: TreeNode) => {
if (node.key && typeof node.key === 'string') {
if (node.key in expandedKeys.value) {
delete expandedKeys.value[node.key]
} else {
expandedKeys.value[node.key] = true
}
}
}

const toggleNodeRecursive = (node: TreeNode) => {
if (node.key && typeof node.key === 'string') {
if (node.key in expandedKeys.value) {
collapseNode(node)
} else {
expandNode(node)
}
}
}

const expandNode = (node: TreeNode) => {
if (
node.key &&
typeof node.key === 'string' &&
node.children &&
node.children.length
) {
expandedKeys.value[node.key] = true

for (const child of node.children) {
expandNode(child)
}
}
}

const collapseNode = (node: TreeNode) => {
if (
node.key &&
typeof node.key === 'string' &&
node.children &&
node.children.length
) {
delete expandedKeys.value[node.key]

for (const child of node.children) {
collapseNode(child)
}
}
}

const onNonLeafClick = (e: MouseEvent, node: TreeNode) => {
if (e.ctrlKey) {
toggleNodeRecursive(node)
} else {
toggleNode(node)
}
}

return {
expandedKeys,
toggleNode,
toggleNodeRecursive,
expandNode,
collapseNode,
onNonLeafClick
}
}

0 comments on commit 10ad3d4

Please sign in to comment.