v-craft icon indicating copy to clipboard operation
v-craft copied to clipboard

How to duplicate node?

Open RaymondAtivie opened this issue 3 years ago • 7 comments

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?

RaymondAtivie avatar Aug 18 '21 19:08 RaymondAtivie

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

Devin345458 avatar Aug 18 '21 19:08 Devin345458

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'

RaymondAtivie avatar Aug 18 '21 20:08 RaymondAtivie

I just copied the whole project because I was doing so much tweaking for it to fit our specific use case.

Devin345458 avatar Aug 18 '21 20:08 Devin345458

Yeah i might do that too. I find myself wanting to do some of that as well.

RaymondAtivie avatar Aug 18 '21 20:08 RaymondAtivie

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?

RaymondAtivie avatar Aug 23 '21 17:08 RaymondAtivie

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

yoychen avatar Sep 26 '21 12:09 yoychen

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

beyondlevi avatar Dec 05 '22 21:12 beyondlevi