I'd love to know what the ideal way to implement drag&drop of blocks, like the latex component in the tex example, would be. I see that the facebook notes editor does not provide such a feature for images. Any info on that?
Is there even a way to include images with the current editor?
No, I'd really hope for a few more examples (inline toolbar, collaborative editing maybe?), but think images shouldn't be too much of a problem using custom components.
See http://facebook.github.io/draft-js/docs/advanced-topics-block-components.html for an example of creating a media block component
Correct, inline images can be rendered with custom block components.
I've experimented with drag&drop for photos with the Notes editor, but haven't shipped anything. I wasn't using regular HTML drag/drop since I was aiming for a design that animated blocks around my mouse movement, using rebound.js. (Absolute positioning for every block set via blockProps, etc.) It worked, but it was a low-pri feature for us and I didn't pursue it further.
I'd love to see an example that uses the normal drag/drop events, and I'd be happy to help support it if changes are needed. I haven't implemented it myself, though.
Okay, I'm currently playing around with HTML drag/drop.
Here is what I've come up with, quick and dirty

Here the script: https://gist.github.com/bkniffler/f50b3d58401556c5b0ae
Just put it into an example/drag-drop folder of draftJS.
A helper method on blocks to get the blocks range would be cool.
contentBlock.getRange();
// or something like that
ContentBlock.getRangeAsSelection();
// It would be an easy function, but helpful
function getRange(){
return new SelectionState({
anchorKey: blockKey,
anchorOffset: 0,
focusKey: blockKey,
focusOffset: block.getLength(),
});
}
Also helper functions for adding/removing blocks would be great.
For some reason, onDragOver, onDrop, onDragEnter are not fired if put on the Editor, thats why I bound the event to the wrapping div.
A helper method on blocks to get the blocks range would be cool.
Sounds useful. I think there are a few places within the repo (and in FB use cases) that would benefit. How about:
class SelectionState ... {
static function fromBlock(block: ContentBlock): SelectionState {
const blockKey = block.getKey();
// your implementation above, plus `isBackward: false`
}
...
}
To remove a block, you can use Modifier.removeRange with the target set to the block you want to remove.
I'm going to give your drag/drop example a try now. :)
Ok, I played around with this a bit. I think a couple of things would be useful additions to the API:
Editor to expose drops:handleDrop?: (e: SyntheticDragEvent, target: SelectionState) => booleanModifier method that encapsulates moving an entire block within the ContentState. It requires a bit of moving, deleting, and block-type setting, and if block component drag/drop is going to be more widespread, a common method would probably be useful.Here's a gist with some of the changes I made to try to get it working: https://gist.github.com/hellendag/6cd425613caabb7f238d
Right now it's copying the block for the drag rather than moving it entirely, so I'm sure I missed something.
I think this has potential to be a really useful example for the repo! Thanks for getting it started.
I have been messing around with https://github.com/gaearon/react-dnd and draft, but so far I am finding it hard to understand what happens with block components under the hood
@nelix I think react-dnd is not the right way to go (in this specific case), as it would require to change the way draft-js works.
@hellendag Thanks for the effort, I'll take a closer look at your changes later today to check out what to incorporate into the example and what to learn from it. Yesterday I've extended my code to work better and to get block resizing to work. I uploaded it a few moments ago on github and heroku, you can check it out here:
Heroku: http://draft-wysiwyg.herokuapp.com/
Github: https://github.com/bkniffler/draft-wysiwyg
The modifier method you talked about would be extremely helpful, along with a few more helpers like
Currently it is not too easy to implement moving a block (add a copy of the block, deleting; though I'm having trouble completely removing it, so using this workaround https://github.com/bkniffler/draft-wysiwyg/blob/master/src/draft.js#L106).
@hellendag I tried to run your code, but it will throw an error after drop
Uncaught Invariant Violation: Unknown node in selection range.
btw Do you have a slack channel or some other discussion platform?
btw Do you have a slack channel or some other discussion platform?
Great suggestion! I've just created a Slack team for this: https://draftjs.slack.com
I tried to run your code, but it will throw an error after drop
Ha not surprised, I was hacking around with it and probably broke a few things. I'll play around with it a bit more.
@hellendag Thanks for creating the slack, though I think you should enable email signup to make it easier to join in :)
I'd love to join too.
Ah sorry, I'm a Slack noob.
It looks like I can only enable certain domains for the Slack team, or invite by email, unless there's another option I'm missing. If so, it seems like the simplest thing for me to do right now is to keep invite-by-email and post details in the Readme to help people join. If you want to send me your email address at [email protected], I'll send you an invite.
For slack you can use https://github.com/rauchg/slackin to automate the process
Actually I did a working d&d with react-dnd in myproject. It's not problem to break draft-js, because we can not do any edit action when dragging a block. Only a small change to DraftEditorContents to open a prop for reorder elements.

@jan4984 cool! could you provide code examples or demo?
@jan4984 亲,提供个示例可以否?就找到这么一个靠谱的方案
English version: Darling, could you please give me an example? This is the best solution ever I found. @bkniffler
@kozhuhds @leejaen I'm busy in my project now. I will have some time one month later, I will publish a working demo then.
@kozhuhds @leejaen please check the demo project.
Amazing job! 1000 Thanks!
Friends, We are implementing drag and drop feature with draftjs akin to sortablejs drag/drop. Any inputs or suggestions regarding this will be appreciated. Thanks.
Most helpful comment
Actually I did a working d&d with react-dnd in myproject. It's not problem to break draft-js, because we can not do any edit action when dragging a block. Only a small change to

DraftEditorContentsto open a prop for reorder elements.