Skip to content
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

How to duplicate node? #19

Open
RaymondAtivie opened this issue Aug 18, 2021 · 7 comments
Open

How to duplicate node? #19

RaymondAtivie opened this issue Aug 18, 2021 · 7 comments

Comments

@RaymondAtivie
Copy link

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?

@Devin345458
Copy link

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

@RaymondAtivie
Copy link
Author

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'

@Devin345458
Copy link

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

@RaymondAtivie
Copy link
Author

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

@RaymondAtivie
Copy link
Author

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?

@yoychen
Copy link
Owner

yoychen commented Sep 26, 2021

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

@beyondlevi
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants