-
https://retejs.org/docs/guides/import-export/ and https://codesandbox.io/s/rete-js-v2-import-export-999y8z?file=/src/index.ts didn't mention how to do this. And my trial also failed (learn from https://retejs.org/examples/performance ), connection is in the export class ReteConnection<A extends ReteNode, B extends ReteNode> extends ClassicPreset.Connection<
A,
B
> {
selected?: boolean;
}
export async function importGraphJSON(
data: IExportedGraphJSON,
editor: NodeEditor<EditorSchemes>,
area: AreaPlugin<EditorSchemes, AreaExtra>,
availableConfigs: Record<string, INodeDefinitionData>,
) {
const addNodeTasks: Array<Promise<unknown>> = [];
for (const nodeData of data.nodes) {
/**
* Node config that can be used to build a runtime node
*/
const nodeConfig = availableConfigs[nodeData.type];
if (nodeConfig === undefined) {
// TODO: emit error to UI or output to console
console.error(`Node ${nodeData.type} is not found in availableConfigs`);
continue;
}
// node definition data -> rete ui node instance
const node = buildNodeFromNodeData(nodeConfig);
applyNodeDataToReteNode(node, nodeData);
const task = editor.addNode(node).then(async () => {
await applyNodeInfoToArea(node, nodeData, area);
});
addNodeTasks.push(task);
}
await Promise.all(addNodeTasks);
const addConnectionTasks: Array<Promise<unknown>> = [];
for (const connectionData of data.connections) {
const connection = new ReteConnection(
editor.getNode(connectionData.from.id),
connectionData.from.portName,
editor.getNode(connectionData.to.id),
connectionData.to.portName,
);
connection.id = connectionData.id ?? connection.id;
// DEBUG: console connection
console.log(`connection`, connection);
addConnectionTasks.push(editor.addConnection(
connection,
));
}
await Promise.all(addConnectionTasks);
// DEBUG: console editor.getConnections()
console.log(`editor.getConnections()`, editor.getConnections());
}
async function applyNodeInfoToArea(node: ReteNode, nodeData: INodeExportedJSON, area: AreaPlugin<EditorSchemes, AreaExtra>) {
if (nodeData.x !== undefined && nodeData.y !== undefined) {
await area.translate(node.id, { x: nodeData.x, y: nodeData.y });
}
} |
Beta Was this translation helpful? Give feedback.
Answered by
linonetwo
May 2, 2023
Replies: 1 comment 2 replies
-
The connection data is exported by data.connections.push(
{
id: connection.id,
from: {
id: connection.source,
portName: connection.sourceOutput,
},
to: {
id: connection.target,
portName: connection.targetInput,
},
} satisfies IConnectionExportedJSON,
); Maybe node id here is wrong. |
Beta Was this translation helpful? Give feedback.
2 replies
Answer selected by
linonetwo
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
editor.getNode(connectionData.from.id)
is undefined, aftereditor.addNode(node)
. I will check why.The connection data is exported by
Maybe node id here is wrong.