-
-
Notifications
You must be signed in to change notification settings - Fork 742
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added duplicate functionality to RenderNode. #521
base: main
Are you sure you want to change the base?
Conversation
✅ Deploy Preview for craftjs ready!
To edit notification comments on pull requests, go to your Netlify site settings. |
Question: @prevwong do you have any suggestions on how I can make my pull request better? Thank you very much for your time! |
i have tried different ways to add duplicate functionality works for any section except ROOT, but couldn't make it work. const duplicate = () => {
const {
data: { type, props },
} = query.node(id).get();
actions.add(query.createNode(React.createElement(type, props)), parent);
}; this one can duplicate the selected element but not the child ones. Any idea how to achieve that? |
the closest i got duplicating the entire node with children is const duplicate = () => {
const originalNodeTree = query.node(id).toNodeTree();
const {
data: { parent },
} = query.node(id).get();
console.log({ originalNodeTree });
actions.addNodeTree(originalNodeTree, parent, 1);
}; but the issue i can see the the old ids remains which causes bunch of issues, |
Hey @JohnFromme, there are some cases that need to be handled when cloning elements. You can't do it just by creating a new element with React and adding it to the editor. Check out this comment and also other discussions on the thread. #209 (comment) |
Thanks, the code from your reference issues, works fantastically. |
Thank you very much! I will take a look & make improvements. |
f14e620
to
84adfd1
Compare
Any updates on this? This would be a great feature to have in the core library @prevwong |
|
The latest updates on your projects. Learn more about Vercel for Git ↗︎
|
The clone appears at the end of the Editor. How do I duplicate it right after the node itself? |
You can get the current node's index in the parent node's children and add the duplicate in the next index. const { actions, query } = useEditor()
const { parentNodeId, id } = useNode(node => ({
parentNodeId: node.data.parent
}))
const childNodes = query.node(parentNodeId).get().data.nodes;
currentNodeIndex = childNodes.indexOf(id);
actions.addNodeTree(nodeTree, parent, currentNodeIndex + 1); |
Added a duplicate button beside the delete button in the landing example for more functionality within the editor. This also provides developers with more examples on how they can use craft.js to meet there needs.