Line data Source code
1 : import { clone } from 'es-toolkit/compat'
2 : import { defineStore } from 'pinia'
3 : import { computed } from 'vue'
4 :
5 : import { useSettingStore } from '@/platform/settings/settingStore'
6 : import type { BookmarkCustomization } from '@comfyorg/settings-core/types'
7 : import type { TreeNode } from '@/types/treeExplorerTypes'
8 :
9 : import {
10 : buildNodeDefTree,
11 : createDummyFolderNodeDef,
12 : useNodeDefStore
13 : } from './nodeDefStore'
14 : import type { ComfyNodeDefImpl } from '@/core/graph/nodeDef/ComfyNodeDefImpl'
15 :
16 2355 : const BOOKMARK_SETTING_ID = 'Comfy.NodeLibrary.Bookmarks.V2'
17 :
18 2355 : export const useNodeBookmarkStore = defineStore('nodeBookmark', () => {
19 204 : const settingStore = useSettingStore()
20 204 : const nodeDefStore = useNodeDefStore()
21 204 : const bookmarks = computed<string[]>(() =>
22 : settingStore.get(BOOKMARK_SETTING_ID)
23 : )
24 :
25 204 : const bookmarksSet = computed<Set<string>>(() => new Set(bookmarks.value))
26 :
27 204 : const bookmarkedRoot = computed<TreeNode<ComfyNodeDefImpl>>(() =>
28 : buildBookmarkTree(bookmarks.value)
29 : )
30 :
31 : // For a node in custom bookmark folders, check if its nodePath is in bookmarksSet
32 : // For a node in the nodeDefStore, check if its name is bookmarked at top level
33 204 : const isBookmarked = (node: ComfyNodeDefImpl) =>
34 : bookmarksSet.value.has(node.nodePath) || bookmarksSet.value.has(node.name)
35 :
36 204 : const toggleBookmark = async (node: ComfyNodeDefImpl) => {
37 6 : if (isBookmarked(node)) {
38 3 : await deleteBookmark(node.nodePath)
39 : // Delete the bookmark at the top level if it exists
40 : // This is used for clicking the bookmark button in the node library, i.e.
41 : // the node is inside original/standard node library tree node
42 3 : await deleteBookmark(node.name)
43 : } else {
44 3 : await addBookmark(node.name)
45 : }
46 : }
47 :
48 204 : const buildBookmarkTree = (bookmarks: string[]) => {
49 102 : const bookmarkNodes = bookmarks
50 : .map((bookmark: string) => {
51 27 : if (bookmark.endsWith('/')) return createDummyFolderNodeDef(bookmark)
52 :
53 10 : const parts = bookmark.split('/')
54 27 : const name = parts.pop() ?? ''
55 27 : const category = parts.join('/')
56 27 : const srcNodeDef = nodeDefStore.getNodeDefByName(name)
57 27 : if (!srcNodeDef) return null
58 9 : const nodeDef = clone(srcNodeDef)
59 9 : nodeDef.category = category
60 9 : return nodeDef
61 : })
62 : .filter((nodeDef) => nodeDef !== null)
63 102 : return buildNodeDefTree(bookmarkNodes)
64 : }
65 :
66 204 : const addBookmark = async (nodePath: string) => {
67 6 : await settingStore.set(BOOKMARK_SETTING_ID, [...bookmarks.value, nodePath])
68 : }
69 :
70 204 : const deleteBookmark = async (nodePath: string) => {
71 6 : await settingStore.set(
72 : BOOKMARK_SETTING_ID,
73 : bookmarks.value.filter((b: string) => b !== nodePath)
74 : )
75 : }
76 :
77 204 : const addNewBookmarkFolder = async (
78 : parent: ComfyNodeDefImpl | undefined,
79 : folderName: string
80 : ) => {
81 : const parentPath = parent ? parent.nodePath : ''
82 2 : const newFolderPath = parentPath + folderName + '/'
83 2 : await addBookmark(newFolderPath)
84 2 : return newFolderPath
85 : }
86 :
87 204 : const renameBookmarkFolder = async (
88 : folderNode: ComfyNodeDefImpl,
89 : newName: string
90 : ) => {
91 2 : if (!folderNode.isDummyFolder) {
92 0 : throw new Error('Cannot rename non-folder node')
93 : }
94 :
95 2 : if (newName.includes('/')) {
96 0 : throw new Error('Folder name cannot contain "/"')
97 : }
98 :
99 2 : const newNodePath =
100 : folderNode.category.split('/').slice(0, -1).concat(newName).join('/') +
101 : '/'
102 :
103 2 : if (newNodePath === folderNode.nodePath) {
104 0 : return
105 : }
106 :
107 2 : if (bookmarks.value.some((b: string) => b.startsWith(newNodePath))) {
108 0 : throw new Error(`Folder name "${newNodePath}" already exists`)
109 : }
110 :
111 2 : await settingStore.set(
112 : BOOKMARK_SETTING_ID,
113 : bookmarks.value.map((b: string) =>
114 : b.startsWith(folderNode.nodePath)
115 : ? b.replace(folderNode.nodePath, newNodePath)
116 : : b
117 : )
118 : )
119 2 : await renameBookmarkCustomization(folderNode.nodePath, newNodePath)
120 : }
121 :
122 204 : const deleteBookmarkFolder = async (folderNode: ComfyNodeDefImpl) => {
123 2 : if (!folderNode.isDummyFolder) {
124 0 : throw new Error('Cannot delete non-folder node')
125 : }
126 2 : await settingStore.set(
127 : BOOKMARK_SETTING_ID,
128 : bookmarks.value.filter(
129 : (b: string) =>
130 : b !== folderNode.nodePath && !b.startsWith(folderNode.nodePath)
131 : )
132 : )
133 2 : await deleteBookmarkCustomization(folderNode.nodePath)
134 : }
135 :
136 204 : const bookmarksCustomization = computed<
137 : Record<string, BookmarkCustomization>
138 : >(() => settingStore.get('Comfy.NodeLibrary.BookmarksCustomization'))
139 :
140 204 : const updateBookmarkCustomization = async (
141 : nodePath: string,
142 : customization: BookmarkCustomization
143 : ) => {
144 : const currentCustomization =
145 : nodePath in bookmarksCustomization.value
146 : ? bookmarksCustomization.value[nodePath]
147 : : {}
148 3 : const newCustomization = { ...currentCustomization, ...customization }
149 :
150 : // Remove attributes that are set to default values
151 3 : if (newCustomization.icon === defaultBookmarkIcon) {
152 0 : delete newCustomization.icon
153 : }
154 3 : if (newCustomization.color === defaultBookmarkColor) {
155 1 : delete newCustomization.color
156 : }
157 :
158 : // If the customization is empty, remove it entirely
159 3 : if (Object.keys(newCustomization).length === 0) {
160 0 : await deleteBookmarkCustomization(nodePath)
161 : } else {
162 3 : await settingStore.set('Comfy.NodeLibrary.BookmarksCustomization', {
163 : ...bookmarksCustomization.value,
164 : [nodePath]: newCustomization
165 : })
166 : }
167 : }
168 :
169 204 : const deleteBookmarkCustomization = async (nodePath: string) => {
170 2 : await settingStore.set('Comfy.NodeLibrary.BookmarksCustomization', {
171 : ...bookmarksCustomization.value,
172 : [nodePath]: undefined
173 : } as Record<string, BookmarkCustomization>)
174 : }
175 :
176 204 : const renameBookmarkCustomization = async (
177 : oldNodePath: string,
178 : newNodePath: string
179 : ) => {
180 2 : const updatedCustomization = { ...bookmarksCustomization.value }
181 2 : if (oldNodePath in updatedCustomization) {
182 1 : updatedCustomization[newNodePath] = updatedCustomization[oldNodePath]
183 1 : delete updatedCustomization[oldNodePath]
184 : }
185 2 : await settingStore.set(
186 : 'Comfy.NodeLibrary.BookmarksCustomization',
187 : updatedCustomization
188 : )
189 : }
190 :
191 204 : const defaultBookmarkIcon = 'pi-bookmark-fill'
192 204 : const defaultBookmarkColor = '#a1a1aa'
193 :
194 204 : return {
195 : bookmarks,
196 : bookmarkedRoot,
197 : isBookmarked,
198 : toggleBookmark,
199 : addBookmark,
200 : addNewBookmarkFolder,
201 : renameBookmarkFolder,
202 : deleteBookmarkFolder,
203 :
204 : bookmarksCustomization,
205 : updateBookmarkCustomization,
206 : deleteBookmarkCustomization,
207 : renameBookmarkCustomization,
208 : defaultBookmarkIcon,
209 : defaultBookmarkColor
210 : }
211 : })
|