Grapesjs: [Question] How can I stop the drop from block:drag:start event? Also is there any event to listen Invalid target position?

Created on 29 Nov 2018  路  2Comments  路  Source: artf/grapesjs

First of all, I really appreciate your replies on my questions. Thank you for all.

Question 1 : I've a situation where if a particular block is already in the canvas, I can't let that block to be added again. What I'm doing is, I'm listing to block:drag:start and compares if the component is already dropped (in gjs-components), I got success till now, but how to prevent the drag event right there?

Question 2 : When a component is bound to specific tag/component, where can listen when the invalid drop happens?

outdated

Most helpful comment

Question 1 : I've a situation where if a particular block is already in the canvas, I can't let that block to be added again. What I'm doing is, I'm listing to block:drag:start and compares if the component is already dropped (in gjs-components), I got success till now, but how to prevent the drag event right there?

I'd suggest to move this logic to components. So create a new component type (eg. my-cmp) and setup few listeners:

editor.on('component:add', component => {
    if (component.is('my-cmp')) {
        editor.BlockManager.get('my-cmp-block') // eg. hide the block
        // ...
    }
});
editor.on('component:remove', component => {
    if (component.is('my-cmp')) {
        editor.BlockManager.get('my-cmp-block') // eg. show the block
        // ...
    }
});

Question 2 : When a component is bound to specific tag/component, where can listen when the invalid drop happens?

Currently you can do it only via sorter:drag:end event

editor.on('sorter:drag:end', (targetCollection, modelToDrop, errors) => {
    if (errors.length) {
        console.log('errors', errors);
    }
})

All 2 comments

Question 1 : I've a situation where if a particular block is already in the canvas, I can't let that block to be added again. What I'm doing is, I'm listing to block:drag:start and compares if the component is already dropped (in gjs-components), I got success till now, but how to prevent the drag event right there?

I'd suggest to move this logic to components. So create a new component type (eg. my-cmp) and setup few listeners:

editor.on('component:add', component => {
    if (component.is('my-cmp')) {
        editor.BlockManager.get('my-cmp-block') // eg. hide the block
        // ...
    }
});
editor.on('component:remove', component => {
    if (component.is('my-cmp')) {
        editor.BlockManager.get('my-cmp-block') // eg. show the block
        // ...
    }
});

Question 2 : When a component is bound to specific tag/component, where can listen when the invalid drop happens?

Currently you can do it only via sorter:drag:end event

editor.on('sorter:drag:end', (targetCollection, modelToDrop, errors) => {
    if (errors.length) {
        console.log('errors', errors);
    }
})

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

desilvaNSP picture desilvaNSP  路  3Comments

crazyxhz picture crazyxhz  路  3Comments

FlashPapa picture FlashPapa  路  3Comments

kawika-connell picture kawika-connell  路  3Comments

kosirm picture kosirm  路  3Comments