Line data Source code
1 : import type { MaybeRefOrGetter } from 'vue'
2 : import { computed, ref, toValue, watch } from 'vue'
3 : import { useI18n } from 'vue-i18n'
4 :
5 : import { nodeHelpService } from '@/services/nodeHelpService'
6 : import type { ComfyNodeDefImpl } from '@/core/graph/nodeDef/ComfyNodeDefImpl'
7 : import { renderMarkdownToHtml } from '@/utils/markdownRendererUtil'
8 : import { getNodeHelpBaseUrl } from '@/workbench/utils/nodeHelpUtil'
9 :
10 : /**
11 : * Composable for fetching and rendering node help content.
12 : * Creates independent state for each usage, allowing multiple panels
13 : * to show help content without interfering with each other.
14 : *
15 : * @param nodeRef - Reactive reference to the node to show help for
16 : * @returns Reactive help content state and rendered HTML
17 : */
18 17 : export function useNodeHelpContent(
19 : nodeRef: MaybeRefOrGetter<ComfyNodeDefImpl | null>
20 : ) {
21 17 : const { locale } = useI18n()
22 :
23 17 : const helpContent = ref<string>('')
24 17 : const isLoading = ref<boolean>(false)
25 17 : const error = ref<string | null>(null)
26 :
27 17 : let currentRequest: Promise<string> | null = null
28 :
29 17 : const baseUrl = computed(() => {
30 14 : const node = toValue(nodeRef)
31 14 : if (!node) return ''
32 14 : return getNodeHelpBaseUrl(node)
33 : })
34 :
35 17 : const renderedHelpHtml = computed(() => {
36 14 : return renderMarkdownToHtml(helpContent.value, baseUrl.value)
37 : })
38 :
39 : // Watch for node changes and fetch help content
40 17 : watch(
41 : () => toValue(nodeRef),
42 : async (node) => {
43 18 : helpContent.value = ''
44 18 : error.value = null
45 :
46 18 : if (node) {
47 18 : isLoading.value = true
48 18 : const request = (currentRequest = nodeHelpService.fetchNodeHelp(
49 : node,
50 : locale.value || 'en'
51 : ))
52 :
53 18 : try {
54 18 : const content = await request
55 14 : if (currentRequest !== request) return
56 14 : helpContent.value = content
57 : } catch (e: unknown) {
58 3 : if (currentRequest !== request) return
59 3 : error.value = e instanceof Error ? e.message : String(e)
60 3 : helpContent.value = node.description || ''
61 : } finally {
62 17 : if (currentRequest === request) {
63 17 : currentRequest = null
64 17 : isLoading.value = false
65 : }
66 : }
67 : }
68 : },
69 : { immediate: true }
70 : )
71 :
72 17 : return {
73 : helpContent,
74 : isLoading,
75 : error,
76 : baseUrl,
77 : renderedHelpHtml
78 : }
79 : }
|