nodeTemplates.js 6.41 KB
Newer Older
1
2
import { app } from "../../scripts/app.js";
import { ComfyDialog, $el } from "../../scripts/ui.js";
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24

// Adds the ability to save and add multiple nodes as a template
// To save:
// Select multiple nodes (ctrl + drag to select a region or ctrl+click individual nodes)
// Right click the canvas
// Save Node Template -> give it a name
//
// To add:
// Right click the canvas
// Node templates -> click the one to add
//
// To delete/rename:
// Right click the canvas
// Node templates -> Manage

const id = "Comfy.NodeTemplates";

class ManageTemplates extends ComfyDialog {
	constructor() {
		super();
		this.element.classList.add("comfy-manage-templates");
		this.templates = this.load();
Jairo Correa's avatar
Jairo Correa committed
25
26
27
28
29
30
31
32
33

		this.importInput = $el("input", {
			type: "file",
			accept: ".json",
			multiple: true,
			style: {display: "none"},
			parent: document.body,
			onchange: () => this.importAll(),
		});
34
35
36
37
38
39
40
41
42
43
44
45
	}

	createButtons() {
		const btns = super.createButtons();
		btns[0].textContent = "Cancel";
		btns.unshift(
			$el("button", {
				type: "button",
				textContent: "Save",
				onclick: () => this.save(),
			})
		);
Jairo Correa's avatar
Jairo Correa committed
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
		btns.unshift(
			$el("button", {
				type: "button",
				textContent: "Export",
				onclick: () => this.exportAll(),
			})
		);
		btns.unshift(
			$el("button", {
				type: "button",
				textContent: "Import",
				onclick: () => {
					this.importInput.click();
				},
			})
		);
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
		return btns;
	}

	load() {
		const templates = localStorage.getItem(id);
		if (templates) {
			return JSON.parse(templates);
		} else {
			return [];
		}
	}

	save() {
		// Find all visible inputs and save them as our new list
		const inputs = this.element.querySelectorAll("input");
		const updated = [];

		for (let i = 0; i < inputs.length; i++) {
			const input = inputs[i];
			if (input.parentElement.style.display !== "none") {
				const t = this.templates[i];
				t.name = input.value.trim() || input.getAttribute("data-name");
				updated.push(t);
			}
		}

		this.templates = updated;
		this.store();
		this.close();
	}

	store() {
		localStorage.setItem(id, JSON.stringify(this.templates));
	}

Jairo Correa's avatar
Jairo Correa committed
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
	async importAll() {
		for (const file of this.importInput.files) {
			if (file.type === "application/json" || file.name.endsWith(".json")) {
				const reader = new FileReader();
				reader.onload = async () => {
					var importFile = JSON.parse(reader.result);
					if (importFile && importFile?.templates) {
						for (const template of importFile.templates) {
							if (template?.name && template?.data) {
								this.templates.push(template);
							}
						}
						this.store();
					}
				};
				await reader.readAsText(file);
			}
		}

116
117
		this.importInput.value = null;

Jairo Correa's avatar
Jairo Correa committed
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
		this.close();
	}

	exportAll() {
		if (this.templates.length == 0) {
			alert("No templates to export.");
			return;
		}

		const json = JSON.stringify({templates: this.templates}, null, 2); // convert the data to a JSON string
		const blob = new Blob([json], {type: "application/json"});
		const url = URL.createObjectURL(blob);
		const a = $el("a", {
			href: url,
			download: "node_templates.json",
			style: {display: "none"},
			parent: document.body,
		});
		a.click();
		setTimeout(function () {
			a.remove();
			window.URL.revokeObjectURL(url);
		}, 0);
	}

143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
	show() {
		// Show list of template names + delete button
		super.show(
			$el(
				"div",
				{
					style: {
						display: "grid",
						gridTemplateColumns: "1fr auto",
						gap: "5px",
					},
				},
				this.templates.flatMap((t) => {
					let nameInput;
					return [
						$el(
							"label",
							{
								textContent: "Name: ",
							},
							[
								$el("input", {
									value: t.name,
									dataset: { name: t.name },
									$: (el) => (nameInput = el),
								}),
							]
						),
Jairo Correa's avatar
Jairo Correa committed
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
						$el(
							"div",
							{},
							[
								$el("button", {
									textContent: "Export",
									style: {
										fontSize: "12px",
										fontWeight: "normal",
									},
									onclick: (e) => {
										const json = JSON.stringify({templates: [t]}, null, 2); // convert the data to a JSON string
										const blob = new Blob([json], {type: "application/json"});
										const url = URL.createObjectURL(blob);
										const a = $el("a", {
											href: url,
187
											download: (nameInput.value || t.name) + ".json",
Jairo Correa's avatar
Jairo Correa committed
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
											style: {display: "none"},
											parent: document.body,
										});
										a.click();
										setTimeout(function () {
											a.remove();
											window.URL.revokeObjectURL(url);
										}, 0);
									},
								}),
								$el("button", {
									textContent: "Delete",
									style: {
										fontSize: "12px",
										color: "red",
										fontWeight: "normal",
									},
									onclick: (e) => {
										nameInput.value = "";
										e.target.parentElement.style.display = "none";
										e.target.parentElement.previousElementSibling.style.display = "none";
									},
								}),
							]
						),
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
					];
				})
			)
		);
	}
}

app.registerExtension({
	name: id,
	setup() {
		const manage = new ManageTemplates();

		const clipboardAction = (cb) => {
			// We use the clipboard functions but dont want to overwrite the current user clipboard
			// Restore it after we've run our callback
			const old = localStorage.getItem("litegrapheditor_clipboard");
			cb();
			localStorage.setItem("litegrapheditor_clipboard", old);
		};

		const orig = LGraphCanvas.prototype.getCanvasMenuOptions;
		LGraphCanvas.prototype.getCanvasMenuOptions = function () {
			const options = orig.apply(this, arguments);

			options.push(null);
			options.push({
				content: `Save Selected as Template`,
				disabled: !Object.keys(app.canvas.selected_nodes || {}).length,
				callback: () => {
					const name = prompt("Enter name");
					if (!name || !name.trim()) return;

					clipboardAction(() => {
						app.canvas.copyToClipboard();
						manage.templates.push({
							name,
							data: localStorage.getItem("litegrapheditor_clipboard"),
						});
						manage.store();
					});
				},
			});

			// Map each template to a menu item
			const subItems = manage.templates.map((t) => ({
				content: t.name,
				callback: () => {
					clipboardAction(() => {
						localStorage.setItem("litegrapheditor_clipboard", t.data);
						app.canvas.pasteFromClipboard();
					});
				},
			}));

Jairo Correa's avatar
Jairo Correa committed
267
268
269
270
			subItems.push(null, {
				content: "Manage",
				callback: () => manage.show(),
			});
271

Jairo Correa's avatar
Jairo Correa committed
272
273
274
275
276
277
			options.push({
				content: "Node Templates",
				submenu: {
					options: subItems,
				},
			});
278
279
280
281
282

			return options;
		};
	},
});