question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How do I save a node as a template that I can drag into the editor

See original GitHub issue

Describe the solution you’d like save a node as a template that I can drag into the editor

Something like #209 needs to regenerate the ID of each node. But how do I save it in the sidebar and drag it?

The second parameter can now only be ReactElement, and it should be possible to customize the NodeTree type

connectors.create(
    ref,
    <Element />
  )

should like this

connectors.create(
    ref,
    // executed at the start of each drag to generate a new id for each node
   () => {
        const nodeTree = ...;
        const newNodeId = getRandomId();
        ......
        return nodeTree;
    }
  )

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:16 (5 by maintainers)

github_iconTop GitHub Comments

7reactions
linxianxicommented, Jan 24, 2022

@hugominas This example can save the template anywhere you want, but you can’t drag it from the sidebar to the editor yet, maybe when my PR passes.

// import { getRandomId } from "@craftjs/utils";

const fromEntries = (pairs) => {
  if (Object.fromEntries) {
    return Object.fromEntries(pairs);
  }
  return pairs.reduce(
    (accum, [id, value]) => ({
      ...accum,
      [id]: value,
    }),
    {}
  );
};

const getCloneTree = useCallback(
  (tree: NodeTree) => {
    const newNodes = {};
    const changeNodeId = (node: Node, newParentId?: string) => {
      const newNodeId = getRandomId();
      const childNodes = node.data.nodes.map((childId) =>
        changeNodeId(tree.nodes[childId], newNodeId)
      );
      const linkedNodes = Object.keys(node.data.linkedNodes).reduce(
        (acc, id) => {
          const newLinkedNodeId = changeNodeId(
            tree.nodes[node.data.linkedNodes[id]],
            newNodeId
          );
          return {
            ...acc,
            [id]: newLinkedNodeId,
          };
        },
        {}
      );

      let tmpNode = {
        ...node,
        id: newNodeId,
        data: {
          ...node.data,
          parent: newParentId || node.data.parent,
          nodes: childNodes,
          linkedNodes,
        },
      };
      let freshNode = query.parseFreshNode(tmpNode).toNode();
      newNodes[newNodeId] = freshNode;
      return newNodeId;
    };

    const rootNodeId = changeNodeId(tree.nodes[tree.rootNodeId]);
    return {
      rootNodeId,
      nodes: newNodes,
    };
  },
  [query]
);

// to save as a template
const handleSaveTemplate = useCallback(() => {
  const tree = query.node(id).toNodeTree();
  const nodePairs = Object.keys(tree.nodes).map((id) => [
    id,
    query.node(id).toSerializedNode(),
  ]);
  const serializedNodesJSON = JSON.stringify(fromEntries(nodePairs));
  const saveData = {
    rootNodeId: tree.rootNodeId,
    nodes: serializedNodesJSON,
  };
  // save to your database
  localStorage.setItem("template", JSON.stringify(saveData));
}, [id, query]);

// add templates where you want
const handleAdd = useCallback(() => {
 // get the template from your database
  const data = JSON.parse(localStorage.getItem("template"));
  const newNodes = JSON.parse(data.nodes);
  const nodePairs = Object.keys(newNodes).map((id) => {
    let nodeId = id;

    return [
      nodeId,
      query
        .parseSerializedNode(newNodes[id])
        .toNode((node) => (node.id = nodeId)),
    ];
  });
  const tree = { rootNodeId: data.rootNodeId, nodes: fromEntries(nodePairs) };
  const newTree = getCloneTree(tree);

  // add templates where you want
  actions.addNodeTree(newTree, ROOT_NODE, 0);
  actions.selectNode(newTree.rootNodeId);
}, [actions, getCloneTree, query]);
0reactions
prevwongcommented, Jan 18, 2022

#317 Released in 0.2.0-beta.2 🎉

Read more comments on GitHub >

github_iconTop Results From Across the Web

Node Template??? - Blackmagic Forum • View topic
1. Select all the nodes on the Fusion flow. ... 5. Save the text file with a .setting extension. Or, a different method:...
Read more >
Using the Node-Red Template Node
In this tutorial we will discuss the HTML template node which is a core node, and is located in the function section.
Read more >
How to save a diagram as node Template? - GoJS
I want create a diagram useing some base shapes of GoJS,and then save the diagram as a node template use it to another...
Read more >
How To Save & Reuse YOUR Effects In Davinci Resolve ...
Do you want to learn how to save your davinci resolve Fusion creations so you can use them over and over again to...
Read more >
Workflow Template Editor - PTC Support
◦ Click and drag nodes to rearrange them. ◦ To view the properties of an element, click any link or arrow. You can...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found