Files
ComfyUI_frontend/src/composables/useTreeExpansion.ts
2025-02-05 15:05:56 -05:00

71 lines
1.5 KiB
TypeScript

import type { TreeNode } from 'primevue/treenode'
import { Ref } from 'vue'
export function useTreeExpansion(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 toggleNodeOnEvent = (e: MouseEvent | KeyboardEvent, node: TreeNode) => {
if (e.ctrlKey) {
toggleNodeRecursive(node)
} else {
toggleNode(node)
}
}
return {
toggleNode,
toggleNodeRecursive,
expandNode,
collapseNode,
toggleNodeOnEvent
}
}