Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor TreeExplorer (Add handleClick hook) #867

Merged
merged 2 commits into from
Sep 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion src/components/common/TreeExplorer.vue
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
class="tree-explorer"
:class="props.class"
v-model:expandedKeys="expandedKeys"
v-model:selectionKeys="selectionKeys"
:value="renderedRoots"
selectionMode="single"
:pt="{
Expand Down Expand Up @@ -42,11 +43,12 @@ import type {
} from '@/types/treeExplorerTypes'
import type { MenuItem, MenuItemCommandEvent } from 'primevue/menuitem'
import { useI18n } from 'vue-i18n'
import { useToast } from 'primevue/usetoast'
import { useErrorHandling } from '@/hooks/errorHooks'

const expandedKeys = defineModel<Record<string, boolean>>('expandedKeys')
provide('expandedKeys', expandedKeys)
const selectionKeys = defineModel<Record<string, boolean>>('selectionKeys')
provide('selectionKeys', selectionKeys)
const props = defineProps<{
roots: TreeExplorerNode[]
class?: string
Expand Down Expand Up @@ -91,6 +93,9 @@ const fillNodeInfo = (node: TreeExplorerNode): RenderedTreeExplorerNode => {
}
}
const onNodeContentClick = (e: MouseEvent, node: RenderedTreeExplorerNode) => {
if (node.handleClick) {
node.handleClick(node, e)
}
emit('nodeClick', node, e)
}
const menu = ref(null)
Expand Down
23 changes: 11 additions & 12 deletions src/components/sidebar/tabs/NodeLibrarySidebarTab.vue
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,17 @@ const renderedRoot = computed<TreeExplorerNode<ComfyNodeDefImpl>>(() => {
}
},
children,
draggable: node.leaf
draggable: node.leaf,
handleClick: (
node: RenderedTreeExplorerNode<ComfyNodeDefImpl>,
e: MouseEvent
) => {
if (node.leaf) {
app.addNodeOnGraph(node.data, { pos: app.getCanvasCenter() })
} else {
toggleNodeOnEvent(e, node)
}
}
}
}
return fillNodeInfo(root.value)
Expand Down Expand Up @@ -164,17 +174,6 @@ const handleSearch = (query: string) => {
})
}

const handleNodeClick = (
node: RenderedTreeExplorerNode<ComfyNodeDefImpl>,
e: MouseEvent
) => {
if (node.leaf) {
app.addNodeOnGraph(node.data, { pos: app.getCanvasCenter() })
} else {
toggleNodeOnEvent(e, node)
}
}

const onAddFilter = (filterAndValue: FilterAndValue) => {
filters.value.push({
filter: filterAndValue,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
:roots="renderedBookmarkedRoot.children"
:expandedKeys="expandedKeys"
:extraMenuItems="extraMenuItems"
@nodeClick="handleNodeClick"
>
<template #folder="{ node }">
<NodeTreeFolder :node="node" />
Expand Down Expand Up @@ -37,11 +36,11 @@ import type {
} from '@/types/treeExplorerTypes'
import type { TreeNode } from 'primevue/treenode'
import { computed, nextTick, ref, watch } from 'vue'
import { useI18n } from 'vue-i18n'
import { useTreeExpansion } from '@/hooks/treeHooks'
import { app } from '@/scripts/app'
import { findNodeByKey } from '@/utils/treeUtil'
import { useErrorHandling } from '@/hooks/errorHooks'
import { useI18n } from 'vue-i18n'

const props = defineProps<{
filteredNodeDefs: ComfyNodeDefImpl[]
Expand All @@ -50,17 +49,6 @@ const props = defineProps<{
const expandedKeys = ref<Record<string, boolean>>({})
const { expandNode, toggleNodeOnEvent } = useTreeExpansion(expandedKeys)

const handleNodeClick = (
node: RenderedTreeExplorerNode<ComfyNodeDefImpl>,
e: MouseEvent
) => {
if (node.leaf) {
app.addNodeOnGraph(node.data, { pos: app.getCanvasCenter() })
} else {
toggleNodeOnEvent(e, node)
}
}

const nodeBookmarkStore = useNodeBookmarkStore()
const bookmarkedRoot = computed<TreeNode>(() => {
const filterTree = (node: TreeNode): TreeNode | null => {
Expand Down Expand Up @@ -147,6 +135,16 @@ const renderedBookmarkedRoot = computed<TreeExplorerNode<ComfyNodeDefImpl>>(
const nodePath = folderNodeDef.category + '/' + nodeDefToAdd.name
nodeBookmarkStore.addBookmark(nodePath)
},
handleClick: (
node: RenderedTreeExplorerNode<ComfyNodeDefImpl>,
e: MouseEvent
) => {
if (node.leaf) {
app.addNodeOnGraph(node.data, { pos: app.getCanvasCenter() })
} else {
toggleNodeOnEvent(e, node)
}
},
...(node.leaf
? {}
: {
Expand Down
2 changes: 2 additions & 0 deletions src/types/treeExplorerTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ export interface TreeExplorerNode<T = any> {
node: TreeExplorerNode<T>,
data: TreeExplorerDragAndDropData
) => void
// Function to handle clicking a node
handleClick?: (node: TreeExplorerNode<T>, event: MouseEvent) => void
// Function to handle errors
handleError?: (error: Error) => void
}
Expand Down
Loading