Template raw JSON view

This commit is contained in:
space-nuko
2023-05-25 16:43:59 -05:00
parent fecd06d3f4
commit e6b446079a
10 changed files with 300 additions and 51 deletions

View File

@@ -338,6 +338,7 @@ export function serializeTemplate(canvas: ComfyGraphCanvas, template: ComfyBoxTe
uiState.update(s => { s.forceSaveUserState = null; return s; });
nodes = relocateNodes(nodes);
nodes = removeTags(nodes);
[nodes, links] = pruneDetachedLinks(nodes, links);
const svg = renderSvg(canvas, graph, TEMPLATE_SVG_PADDING);
@@ -357,22 +358,35 @@ export function serializeTemplate(canvas: ComfyGraphCanvas, template: ComfyBoxTe
return serTemplate;
}
/*
* Extract embedded workflow from desc tags
*/
export function extractTemplateJSONFromSVG(svg: string): string | null {
const descEnd = svg.lastIndexOf("</desc>");
if (descEnd !== -1) {
const descStart = svg.lastIndexOf("<desc>", descEnd);
if (descStart !== -1) {
const json = svg.substring(descStart + 6, descEnd);
return unescapeXml(json);
}
}
return null;
}
/*
* Credit goes to pythongosssss for this format
*/
export function deserializeTemplateFromSVG(file: File): Promise<SerializedComfyBoxTemplate> {
return new Promise((resolve, reject) => {
const reader = new FileReader();
reader.onload = async () => {
const svg = reader.result as string;
let template = null;
// Extract embedded workflow from desc tags
const descEnd = svg.lastIndexOf("</desc>");
if (descEnd !== -1) {
const descStart = svg.lastIndexOf("<desc>", descEnd);
if (descStart !== -1) {
const json = svg.substring(descStart + 6, descEnd);
template = JSON.parse(unescapeXml(json));
}
}
let templateJSON = extractTemplateJSONFromSVG(svg);
if (templateJSON)
template = JSON.parse(templateJSON);
if (!isSerializedComfyBoxTemplate(template)) {
reject("Invalid template format!")