v-craft
v-craft copied to clipboard
How to duplicate node?
Do you have any suggestions on how i could duplicate a node?
This is what i tried
duplicateNode() {
const { parent } = this.selectedNode;
this.editor.selectNode(parent);
parent.children.push({ ...this.selectNode });
},
But it doesn't work. Any idea what i could do?
duplicateNode(parent = null) {
const { parent } = this.selectedNode;
this.clondeNode(this.selectedNode, parent)
},
cloneNode(selectedNode, parent = null) {
const newNode = new Node(selectedNode.componentName, selectedNode.props, parent, [], selectedNode.rules, selectedNode.additional)
const vnodeChildren = selectedNode.children
const children = vnodeChildren
? vnodeChildren.map((childVNode) => cloneNode(childVNode, newNode))
.filter((childNode) => !!childNode)
: []
newNode.children = children
return newNode
}
Try something like that
Thanks @Devin345458 . This looks like it should work, but where do i get the Node
class from?
It's not exported as part of @v-craft/core
i was going to try import { Node } from '@v-craft/core'
I just copied the whole project because I was doing so much tweaking for it to fit our specific use case.
Yeah i might do that too. I find myself wanting to do some of that as well.
hi @Devin345458 quick question. I followed your recommendation above, but i have an issue: The newely created node cannot be dragged for some reason. any ideas why?
hi @RaymondAtivie ,
I implement duplicate()
method in Node. You can use it via update @v-craft/core
to 0.3.0 (npm i @v-craft/[email protected]
), and I hope this feature would resolve your use case.
https://yoychen.github.io/v-craft/docs/api/node.html#duplicate
We've been trying to use the duplicate method, but we don't know how to add the new node generated by the method in the editor. We just receive the duplicated element as a Javascript object with no use