Auto wrap every component drag into canvas with another component
@pouyamiralayi I managed to solve the problem by making component as an object. Like so. Anyway thanks for trying to help.
Read full answer below ↓Question
I am trying to make sure every component drag into the canvas to be wrapped with a column component. To achieve that I use canvas event.
First I disabled window event to prevent default html 5 drag and drop event to be fired.
canvas:drop event
ditor.on('canvas:drop', function (dataTransfer) {
event.preventDefault()
})
Let's say that my column component is so simple, only a single div.
canvas:dragdata event
editor.on('canvas:dragdata', handleDragData)
function handleDragData (dataTransfer, result) {
if (!event.currentTarget.closest('.column')) {
result.content `
<div class="column" data-gjs-type="column">
<div class="column-inner">
${dataTransfer.getData(dataTransfer.types[0])}
</div>
</div>
`
}
Above code return error, saying that dataTransfer not defined
What I understand is we can customize what we drop to the canvas with result.content
something like
//...
result.content = { type: 'heading' }
or
//...
result.content = `<div>I will replace the default dragged component</div>`
What I don't understand is how to wrap the dragged component with element/component.
Answers (2)
@pouyamiralayi I managed to solve the problem by making component as an object. Like so.
result.content = {
tagName: 'div',
removable: true, // Can't remove it
draggable: true, // Can't move it
copyable: true, // Disable copy/past
classes: ['container'],
type: 'grid',
attributes: { title: 'here' },
// style: { color: 'red'},
components: {
tagName: 'div',
classes: ['row'],
hoverable: false,
selectable: false,
draggable: false,
components: {
tagName: 'div',
classes: ['col-auto'],
type: 'column',
style: { width: '100%', flex: 1 },
components: {
// type: JSON.parse(dataTransfer.getData(dataTransfer.types[0]))['type']
type: result.content.type
}
}
}
}
Anyway thanks for trying to help.
Hello there! the event you are looking for is this
editor.on('block:drag:stop', function (element) { element.replaceWith({ tagName:'div', style:{'border': '1px solid red'}, components: [element] }) })
Notice here I am using a red bordered div as my wrapper; in your case it is the column.
cheers.
Related Questions and Answers
Continue research with similar issue discussions.
Issue #2129
[Question] how to append a button inside default modal and catch it's click event
I want a form inside a modal (using the default modal ), on click of the submit button make an ajax call. I have created a new component wh...
Issue #1895
[QUESTION] Drag component over canvas with script, script appended multiple times.
Hi, not sure if this is a bug or not. But I noticed a weird behavior, when I drag a block that has a component that contains a script funct...
Issue #1414
[QUESTION] Component "first render" event
Hi. I'm developing a plugin for charts using chart.js. I need to persist the model of the chart inside the generated component to render th...
Issue #3939
Disabled drag and drop in preview
GrapesJS version[X] I confirm to use the latest version of GrapesJSWhat browser are you using? v95Reproducible demo link https://codesandbo...
Paid Plugins That Match This Issue
Curated by issue keywords and label relevance to help you ship faster.
Loading paid plugin recommendations...
Check the open-source GrapesJS plugins on GitHub or run a quick search in our free catalog.
Browse free plugins →Premium plugins ship with support, regular updates, and production-ready features — save days of integration work.
Browse premium plugins →Browse Plugin Categories
Jump directly to plugin category pages on the marketplace.