Skip to content

Commit 0aa2747

Browse files
authored
Merge pull request #95 from swilcock0/main
Modification to add menu item in new UI
2 parents 14824e3 + 0351a2e commit 0aa2747

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+
// Get submenu items
60+
const listItems = dropdownMenu.querySelectorAll("li");
61+
let newSetsize = listItems.length;
62+
63+
const separatorMenu = document.createElement("li");
64+
separatorMenu.setAttribute("id", "pv_id_8_0_" + (newSetsize - 1).toString());
65+
separatorMenu.setAttribute("class", "p-menubar-separator");
66+
separatorMenu.setAttribute("role", "separator");
67+
separatorMenu.setAttribute("data-pc-section", "separator");
68+
69+
dropdownMenu.append(separatorMenu);
70+
71+
// Adjust list items within to increase setsize
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", newSetsize);
76+
}
77+
});
78+
79+
console.log(newSetsize);
80+
81+
// Here's the format of list items
82+
const saveButtonText = document.createElement("li");
83+
saveButtonText.setAttribute("id", "pv_id_8_0_" + newSetsize.toString());
84+
saveButtonText.setAttribute("class", "p-menubar-item relative");
85+
saveButtonText.setAttribute("role", "menuitem");
86+
saveButtonText.setAttribute("aria-label", "Save as Script");
87+
saveButtonText.setAttribute("aria-level", "2");
88+
saveButtonText.setAttribute("aria-setsize", newSetsize.toString());
89+
saveButtonText.setAttribute("aria-posinset", newSetsize.toString());
90+
saveButtonText.setAttribute("data-pc-section", "item");
91+
saveButtonText.setAttribute("data-p-active", "false");
92+
saveButtonText.setAttribute("data-p-focused", "false");
93+
94+
saveButtonText.innerHTML = `
95+
<div class="p-menubar-item-content" data-pc-section="itemcontent">
96+
<a class="p-menubar-item-link" tabindex="-1" aria-hidden="true" data-pc-section="itemlink" target="_blank">
97+
<span class="p-menubar-item-icon pi pi-book"></span>
98+
<span class="p-menubar-item-label">Save as Script</span>
99+
</a>
100+
</div>
101+
`
102+
103+
saveButtonText.onclick = () => savePythonScript();
104+
105+
dropdownMenu.append(saveButtonText);
106+
107+
53108

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

0 commit comments

Comments
 (0)