Skip to content
Open
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
20 changes: 16 additions & 4 deletions src/composables/graph/useSelectionState.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { storeToRefs } from 'pinia'
import { computed } from 'vue'
import { computed, watch } from 'vue'

import { useNodeLibrarySidebarTab } from '@/composables/sidebarTabs/useNodeLibrarySidebarTab'
import type { LGraphNode } from '@/lib/litegraph/src/litegraph'
Expand Down Expand Up @@ -94,6 +94,19 @@ export function useSelectionState() {
computeSelectionStatesFromNodes(selectedNodes.value)
)

// Keep help panel in sync when it is open and the user changes selection.
watch(
() => nodeDef.value,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Optional: Why a getter here if you're checking the value of the ref itself?

Suggested change
() => nodeDef.value,
nodeDef

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, if you only want to fire it off if the value is defined, you could use whenever

(def) => {
if (!nodeHelpStore.isHelpOpen || !def) return

const currentHelpNode = nodeHelpStore.currentHelpNode
if (currentHelpNode?.nodePath === def.nodePath) return

nodeHelpStore.openHelp(def)
}
)
Comment on lines +98 to +108
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since this composable isn't shared, this means we could have multiple instances of this watcher that are independently trying to manage the opened item, right?


// On-demand computation (non-reactive) so callers can fetch fresh flags
const computeSelectionFlags = (): NodeSelectionState =>
computeSelectionStatesFromNodes(selectedNodes.value)
Expand All @@ -105,12 +118,11 @@ export function useSelectionState() {

const isSidebarActive =
sidebarTabStore.activeSidebarTabId === nodeLibraryTabId
const currentHelpNode: any = nodeHelpStore.currentHelpNode
const currentHelpNode = nodeHelpStore.currentHelpNode
const isSameNodeHelpOpen =
isSidebarActive &&
nodeHelpStore.isHelpOpen &&
currentHelpNode &&
currentHelpNode.nodePath === def.nodePath
currentHelpNode?.nodePath === def.nodePath

if (isSameNodeHelpOpen) {
nodeHelpStore.closeHelp()
Expand Down
56 changes: 55 additions & 1 deletion tests-ui/tests/composables/graph/useSelectionState.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { createPinia, setActivePinia } from 'pinia'
import { beforeEach, describe, expect, test, vi } from 'vitest'
import { type Ref, ref } from 'vue'
import { nextTick, ref } from 'vue'
import type { Ref } from 'vue'

import { useSelectionState } from '@/composables/graph/useSelectionState'
import { useNodeLibrarySidebarTab } from '@/composables/sidebarTabs/useNodeLibrarySidebarTab'
Expand Down Expand Up @@ -267,4 +268,57 @@ describe('useSelectionState', () => {
expect(newIsPinned).toBe(false)
})
})

describe('Help Sync', () => {
test('opens help for newly selected node when help is open', async () => {
const nodeDefStore = useNodeDefStore() as any
nodeDefStore.fromLGraphNode.mockImplementation((node: TestNode) => ({
nodePath: node.type
}))

const nodeHelpStore = useNodeHelpStore() as any
nodeHelpStore.isHelpOpen = true
nodeHelpStore.currentHelpNode = { nodePath: 'NodeA' }

const nodeA = createTestNode({ type: 'NodeA' })
mockSelectedItems.value = [nodeA]

useSelectionState()

const nodeB = createTestNode({ type: 'NodeB' })
mockSelectedItems.value = [nodeB]

await nextTick()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you use some of the test helpers to interact with this more like a user would? That way you wouldn't have to manually manage the DOM updates.
https://test-utils.vuejs.org/guide/advanced/async-suspense


expect(nodeHelpStore.openHelp).toHaveBeenCalledWith({
nodePath: 'NodeB'
})
})

test('does not reopen help when selection is unchanged or closed', async () => {
const nodeDefStore = useNodeDefStore() as any
nodeDefStore.fromLGraphNode.mockImplementation((node: TestNode) => ({
nodePath: node.type
}))

const nodeHelpStore = useNodeHelpStore() as any
const nodeA = createTestNode({ type: 'NodeA' })
mockSelectedItems.value = [nodeA]

useSelectionState()

// Help closed -> no call
nodeHelpStore.isHelpOpen = false
mockSelectedItems.value = [nodeA]
await nextTick()
expect(nodeHelpStore.openHelp).not.toHaveBeenCalled()

// Help open but same node -> no call
nodeHelpStore.isHelpOpen = true
nodeHelpStore.currentHelpNode = { nodePath: 'NodeA' }
mockSelectedItems.value = [nodeA]
await nextTick()
expect(nodeHelpStore.openHelp).not.toHaveBeenCalled()
})
})
})
Loading