Skip to content

Commit 0094c08

Browse files
authored
Update save-as-script.js
Add "Save as Script" item to new "Workflow" menu
1 parent 14824e3 commit 0094c08

File tree

1 file changed

+85
-30
lines changed

1 file changed

+85
-30
lines changed

js/save-as-script.js

+85-30
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,37 @@ app.registerExtension({
1010
});
1111
},
1212
async setup() {
13+
function savePythonScript() {
14+
var filename = prompt("Save script as:");
15+
if(filename === undefined || filename === null || filename === "") {
16+
return
17+
}
18+
19+
app.graphToPrompt().then(async (p) => {
20+
const json = JSON.stringify({name: filename + ".json", workflow: JSON.stringify(p.output, null, 2)}, null, 2); // convert the data to a JSON string
21+
var response = await api.fetchApi(`/saveasscript`, { method: "POST", body: json });
22+
if(response.status == 200) {
23+
const blob = new Blob([await response.text()], {type: "text/python;charset=utf-8"});
24+
const url = URL.createObjectURL(blob);
25+
if(!filename.endsWith(".py")) {
26+
filename += ".py";
27+
}
28+
29+
const a = $el("a", {
30+
href: url,
31+
download: filename,
32+
style: {display: "none"},
33+
parent: document.body,
34+
});
35+
a.click();
36+
setTimeout(function () {
37+
a.remove();
38+
window.URL.revokeObjectURL(url);
39+
}, 0);
40+
}
41+
});
42+
}
43+
1344
const menu = document.querySelector(".comfy-menu");
1445
const separator = document.createElement("hr");
1546

@@ -19,37 +50,61 @@ app.registerExtension({
1950

2051
const saveButton = document.createElement("button");
2152
saveButton.textContent = "Save as Script";
22-
saveButton.onclick = () => {
23-
var filename = prompt("Save script as:");
24-
if(filename === undefined || filename === null || filename === "") {
25-
return
26-
}
27-
28-
app.graphToPrompt().then(async (p) => {
29-
const json = JSON.stringify({name: filename + ".json", workflow: JSON.stringify(p.output, null, 2)}, null, 2); // convert the data to a JSON string
30-
var response = await api.fetchApi(`/saveasscript`, { method: "POST", body: json });
31-
if(response.status == 200) {
32-
const blob = new Blob([await response.text()], {type: "text/python;charset=utf-8"});
33-
const url = URL.createObjectURL(blob);
34-
if(!filename.endsWith(".py")) {
35-
filename += ".py";
36-
}
37-
38-
const a = $el("a", {
39-
href: url,
40-
download: filename,
41-
style: {display: "none"},
42-
parent: document.body,
43-
});
44-
a.click();
45-
setTimeout(function () {
46-
a.remove();
47-
window.URL.revokeObjectURL(url);
48-
}, 0);
49-
}
50-
});
51-
}
53+
saveButton.onclick = () => savePythonScript();
5254
menu.append(saveButton);
55+
56+
57+
// Also load to new style menu
58+
const dropdownMenu = document.querySelectorAll(".p-menubar-submenu ")[0];
59+
60+
const separatorMenu = document.createElement("li");
61+
separatorMenu.setAttribute("id", "pv_id_8_0_9");
62+
separatorMenu.setAttribute("class", "p-menubar-separator");
63+
separatorMenu.setAttribute("role", "separator");
64+
separatorMenu.setAttribute("data-pc-section", "separator");
65+
66+
dropdownMenu.append(separatorMenu);
67+
68+
// Adjust list items within to increase setsize by 1
69+
const listItems = dropdownMenu.querySelectorAll("li");
70+
let setsize = 0;
71+
72+
listItems.forEach((item) => {
73+
// First check if it's a separator
74+
if(item.getAttribute("data-pc-section") !== "separator") {
75+
item.setAttribute("aria-setsize", parseInt(item.getAttribute("aria-setsize")) + 2);
76+
setsize = parseInt(item.getAttribute("aria-setsize"));
77+
}
78+
});
79+
80+
console.log(setsize);
81+
82+
// Here's the format of list items
83+
const saveButtonText = document.createElement("li");
84+
saveButtonText.setAttribute("id", "pv_id_8_0_10");
85+
saveButtonText.setAttribute("class", "p-menubar-item relative");
86+
saveButtonText.setAttribute("role", "menuitem");
87+
saveButtonText.setAttribute("aria-label", "Save as Script");
88+
saveButtonText.setAttribute("aria-level", "2");
89+
saveButtonText.setAttribute("aria-setsize", setsize.toString());
90+
saveButtonText.setAttribute("aria-posinset", setsize.toString());
91+
saveButtonText.setAttribute("data-pc-section", "item");
92+
saveButtonText.setAttribute("data-p-active", "false");
93+
saveButtonText.setAttribute("data-p-focused", "false");
94+
95+
saveButtonText.innerHTML = `
96+
<div class="p-menubar-item-content" data-pc-section="itemcontent">
97+
<a class="p-menubar-item-link" tabindex="-1" aria-hidden="true" data-pc-section="itemlink" target="_blank">
98+
<span class="p-menubar-item-icon pi pi-download"></span>
99+
<span class="p-menubar-item-label">Save as Script</span>
100+
</a>
101+
</div>
102+
`
103+
104+
saveButtonText.onclick = () => savePythonScript();
105+
dropdownMenu.append(saveButtonText);
106+
107+
53108

54109
console.log("SaveAsScript loaded");
55110
}

0 commit comments

Comments
 (0)