newcat / baklavajs

Graph / node editor in the browser using VueJS
http://baklava.tech
MIT License
1.52k stars 113 forks source link

Feature Request : Convert a graph to a node #346

Closed chuteany closed 8 months ago

chuteany commented 9 months ago

I have a requirement to convert a graph into a node, save it to the database, and allow others to download this node to add it to the NodePalette for general usage. How can I implement it

newcat commented 8 months ago

You can create a new GraphTemplate using the fromGraph function:

import { useBaklava, GraphTemplate } from "baklavajs";
const baklava = useBaklava();
const template = GraphTemplate.fromGraph(baklava.editor.graph, baklava.editor);
JSON.stringify(template.save()) // => string that you can save in the database

To load a graph template into other editors:

import { useBaklava, GraphTemplate } from "baklavajs";
const baklava = useBaklava();
const state = JSON.parse("<string from the database>");
const template = new GraphTemplate(state, baklava.editor);
baklava.editor.addGraphTemplate(template);