Line data Source code
1 : import { ref } from 'vue'
2 :
3 : import type { LGraph } from '@/lib/litegraph/src/litegraph'
4 : import type { ComfyWorkflow } from '@/platform/workflow/management/stores/comfyWorkflow'
5 :
6 : import { createGraphThumbnail } from './graphThumbnailRenderer'
7 :
8 : // Store thumbnails for each workflow
9 2356 : const workflowThumbnails = ref<Map<string, string>>(new Map())
10 :
11 2356 : export const useWorkflowThumbnail = () => {
12 : /**
13 : * Capture a thumbnail of the given graph
14 : */
15 39103 : const createMinimapPreview = (
16 : graph: LGraph | null | undefined
17 : ): Promise<string | null> => {
18 1548 : try {
19 1548 : const thumbnailDataUrl = createGraphThumbnail(graph)
20 1548 : return Promise.resolve(thumbnailDataUrl)
21 : } catch (error) {
22 0 : console.error('Failed to capture canvas thumbnail:', error)
23 0 : return Promise.resolve(null)
24 : }
25 : }
26 :
27 : /**
28 : * Store a thumbnail of the given graph for a workflow
29 : */
30 39103 : const storeThumbnail = async (
31 : workflow: ComfyWorkflow,
32 : graph: LGraph | null | undefined
33 : ) => {
34 1548 : const thumbnail = await createMinimapPreview(graph)
35 1548 : if (thumbnail) {
36 : // Clean up existing thumbnail if it exists
37 1401 : const existingThumbnail = workflowThumbnails.value.get(workflow.key)
38 1401 : if (existingThumbnail) {
39 83 : URL.revokeObjectURL(existingThumbnail)
40 : }
41 1401 : workflowThumbnails.value.set(workflow.key, thumbnail)
42 : }
43 : }
44 :
45 : /**
46 : * Get a thumbnail for a workflow
47 : */
48 39103 : const getThumbnail = (workflowKey: string): string | undefined => {
49 6338 : return workflowThumbnails.value.get(workflowKey)
50 : }
51 :
52 : /**
53 : * Clear a thumbnail for a workflow
54 : */
55 39103 : const clearThumbnail = (workflowKey: string) => {
56 25 : const thumbnail = workflowThumbnails.value.get(workflowKey)
57 25 : if (thumbnail) {
58 14 : URL.revokeObjectURL(thumbnail)
59 : }
60 25 : workflowThumbnails.value.delete(workflowKey)
61 : }
62 :
63 : /**
64 : * Clear all thumbnails
65 : */
66 39103 : const clearAllThumbnails = () => {
67 0 : for (const thumbnail of workflowThumbnails.value.values()) {
68 0 : URL.revokeObjectURL(thumbnail)
69 : }
70 0 : workflowThumbnails.value.clear()
71 : }
72 :
73 : /**
74 : * Move a thumbnail from one workflow key to another (useful for workflow renaming)
75 : */
76 39103 : const moveWorkflowThumbnail = (oldKey: string, newKey: string) => {
77 : // Don't do anything if moving to the same key
78 100 : if (oldKey === newKey) return
79 :
80 99 : const thumbnail = workflowThumbnails.value.get(oldKey)
81 99 : if (thumbnail) {
82 2 : workflowThumbnails.value.set(newKey, thumbnail)
83 2 : workflowThumbnails.value.delete(oldKey)
84 : }
85 : }
86 :
87 39103 : return {
88 : createMinimapPreview,
89 : storeThumbnail,
90 : getThumbnail,
91 : clearThumbnail,
92 : clearAllThumbnails,
93 : moveWorkflowThumbnail,
94 : workflowThumbnails
95 : }
96 : }
|