<script src="https://unpkg.com/grapesjs"></script>
<link rel="stylesheet" href="https://unpkg.com/grapesjs/dist/css/grapes.min.css">
<script src="https://unpkg.com/grapesjs-blocks-basic"></script>
<div draggable="true">
I'm a div with draggable="true"! Drag me in!!!!
</div>
<div>
I'm a div! Select my text and drag me in!!!!
</div>
<img...
CSS
body, html {
margin: 0;
height: 100%;
}
Babel + JSX
const editor = grapesjs.init({
container: '#gjs',
fromElement: true,
height: '100%',
storageManager: false,
plugins: ['gjs-blocks-basic']
});
document.querySelectorAll('[draggable=true]').forEach(el => {
el.addEventListener('dragstart', ev => {
const { target } = ev;
// You can use only 'text' data type if you want
// to support also IE11
ev.dataTransfer.setData('text',
`<span>${target.innerHTML}</span>`);
})
})
// You can use `canvas:dragdata` to customize
// the data based on dataTransfer
editor.on('canvas:dragdata', (dataTransfer, result) => {
// use dataTransfer.files if you need to read from droppped files
const text = dataTransfer.getData('text');
if (text) {
// result.content can also be any Component Definition,
// not only a string
result.content = `<div>
${text}
<div>Extra content</div>
</div>`;
}
})
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.