Line data Source code
1 : import { app, ComfyApp } from '@/scripts/app'
2 : import { ComfyDialog } from '@/scripts/ui'
3 : import { $el } from '@/scripts/ui/utils'
4 : import type { ComfyExtension } from '@/types/comfy'
5 :
6 2355 : class ClipspaceDialog extends ComfyDialog {
7 : static items: Array<
8 : HTMLButtonElement & {
9 : contextPredicate?: () => boolean
10 : }
11 : > = []
12 : static instance: ClipspaceDialog | null = null
13 :
14 : static registerButton(
15 : name: string,
16 : contextPredicate: () => boolean,
17 : callback: () => void
18 : ) {
19 0 : const item = $el('button', {
20 : type: 'button',
21 : textContent: name,
22 : contextPredicate: contextPredicate,
23 : onclick: callback
24 : })
25 :
26 0 : ClipspaceDialog.items.push(item)
27 : }
28 :
29 : static invalidatePreview() {
30 0 : if (
31 : ComfyApp.clipspace &&
32 : ComfyApp.clipspace.imgs &&
33 : ComfyApp.clipspace.imgs.length > 0
34 : ) {
35 0 : const img_preview = document.getElementById('clipspace_preview')
36 0 : if (img_preview instanceof HTMLImageElement) {
37 0 : img_preview.src =
38 : ComfyApp.clipspace.imgs[ComfyApp.clipspace['selectedIndex']].src
39 0 : img_preview.style.maxHeight = '100%'
40 0 : img_preview.style.maxWidth = '100%'
41 : }
42 : }
43 : }
44 :
45 : static invalidate() {
46 0 : if (ClipspaceDialog.instance) {
47 0 : const self = ClipspaceDialog.instance
48 : // allow reconstruct controls when copying from non-image to image content.
49 0 : const imgSettings = self.createImgSettings()
50 0 : const children = $el('div.comfy-modal-content', [
51 : ...(imgSettings ? [imgSettings] : []),
52 : ...self.createButtons()
53 : ])
54 :
55 0 : self.element.replaceChildren(children)
56 :
57 0 : if (self.element.children[0].children.length <= 1) {
58 0 : self.element.children[0].appendChild(
59 : $el('p', {}, [
60 : 'Unable to find the features to edit content of a format stored in the current Clipspace.'
61 : ])
62 : )
63 : }
64 :
65 0 : ClipspaceDialog.invalidatePreview()
66 : }
67 : }
68 :
69 : constructor() {
70 0 : super()
71 : }
72 :
73 : override createButtons() {
74 0 : const buttons = []
75 :
76 0 : for (const item of ClipspaceDialog.items) {
77 0 : if (!item.contextPredicate || item.contextPredicate()) buttons.push(item)
78 : }
79 :
80 0 : buttons.push(
81 : $el('button', {
82 : type: 'button',
83 : textContent: 'Close',
84 : onclick: () => {
85 0 : this.close()
86 : }
87 : })
88 : )
89 :
90 0 : return buttons
91 : }
92 :
93 : createImgSettings(): HTMLTableElement | null {
94 0 : if (ComfyApp.clipspace?.imgs) {
95 0 : const combo_items = []
96 0 : const imgs = ComfyApp.clipspace.imgs
97 :
98 0 : for (let i = 0; i < imgs.length; i++) {
99 0 : combo_items.push($el('option', { value: i }, [`${i}`]))
100 : }
101 :
102 0 : const combo1 = $el(
103 : 'select',
104 : {
105 : id: 'clipspace_img_selector',
106 : onchange: (event: Event) => {
107 0 : if (event.target && ComfyApp.clipspace) {
108 0 : ComfyApp.clipspace['selectedIndex'] = (
109 : event.target as HTMLSelectElement
110 : ).selectedIndex
111 0 : ClipspaceDialog.invalidatePreview()
112 : }
113 : }
114 : },
115 : combo_items
116 : )
117 :
118 0 : const row1 = $el('tr', {}, [
119 : $el('td', {}, [$el('font', { color: 'white' }, ['Select Image'])]),
120 : $el('td', {}, [combo1])
121 : ])
122 :
123 0 : const combo2 = $el(
124 : 'select',
125 : {
126 : id: 'clipspace_img_paste_mode',
127 : onchange: (event: Event) => {
128 0 : if (event.target && ComfyApp.clipspace) {
129 0 : ComfyApp.clipspace['img_paste_mode'] = (
130 : event.target as HTMLSelectElement
131 : ).value
132 : }
133 : }
134 : },
135 : [
136 : $el('option', { value: 'selected' }, 'selected'),
137 : $el('option', { value: 'all' }, 'all')
138 : ]
139 : )
140 0 : combo2.value = ComfyApp.clipspace['img_paste_mode']
141 :
142 0 : const row2 = $el('tr', {}, [
143 : $el('td', {}, [$el('font', { color: 'white' }, ['Paste Mode'])]),
144 : $el('td', {}, [combo2])
145 : ])
146 :
147 0 : const td = $el(
148 : 'td',
149 : { align: 'center', width: '100px', height: '100px', colSpan: '2' },
150 : [$el('img', { id: 'clipspace_preview', ondragstart: () => false }, [])]
151 : )
152 :
153 0 : const row3 = $el('tr', {}, [td])
154 :
155 0 : return $el('table', {}, [row1, row2, row3])
156 : } else {
157 0 : return null
158 : }
159 : }
160 :
161 : createImgPreview(): HTMLImageElement | null {
162 0 : if (ComfyApp.clipspace?.imgs) {
163 0 : return $el('img', { id: 'clipspace_preview', ondragstart: () => false })
164 0 : } else return null
165 : }
166 :
167 : override show() {
168 0 : ClipspaceDialog.invalidate()
169 :
170 0 : this.element.style.display = 'block'
171 : }
172 : }
173 :
174 2355 : export const clipspaceExtension: ComfyExtension = {
175 : name: 'Comfy.Clipspace',
176 : init() {
177 2355 : app.openClipspace = function () {
178 0 : if (!ClipspaceDialog.instance) {
179 0 : ClipspaceDialog.instance = new ClipspaceDialog()
180 0 : ComfyApp.clipspace_invalidate_handler = ClipspaceDialog.invalidate
181 : }
182 :
183 0 : if (ComfyApp.clipspace) {
184 0 : ClipspaceDialog.instance.show()
185 0 : } else app.ui.dialog.show('Clipspace is Empty!')
186 : }
187 : }
188 : }
|