Coder Social home page Coder Social logo

Drag & Drop about draft-js HOT 22 CLOSED

facebook avatar facebook commented on April 20, 2024 1
Drag & Drop

from draft-js.

Comments (22)

jan4984 avatar jan4984 commented on April 20, 2024 7

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.

from draft-js.

leejaen avatar leejaen commented on April 20, 2024 3

@jan4984 亲,提供个示例可以否?就找到这么一个靠谱的方案
English version: Darling, could you please give me an example? This is the best solution ever I found. @bkniffler

from draft-js.

jan4984 avatar jan4984 commented on April 20, 2024 3

@kozhuhds @leejaen I'm busy in my project now. I will have some time one month later, I will publish a working demo then.

from draft-js.

jan4984 avatar jan4984 commented on April 20, 2024 2

@kozhuhds @leejaen please check the demo project.

from draft-js.

bkniffler avatar bkniffler commented on April 20, 2024 1

Okay, I'm currently playing around with HTML drag/drop.
Here is what I've come up with, quick and dirty
foto

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.

from draft-js.

mmahalwy avatar mmahalwy commented on April 20, 2024

Is there even a way to include images with the current editor?

from draft-js.

bkniffler avatar bkniffler commented on April 20, 2024

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.

from draft-js.

davisml avatar davisml commented on April 20, 2024

See http://facebook.github.io/draft-js/docs/advanced-topics-block-components.html for an example of creating a media block component

from draft-js.

hellendag avatar hellendag commented on April 20, 2024

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.

from draft-js.

hellendag avatar hellendag commented on April 20, 2024

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. :)

from draft-js.

hellendag avatar hellendag commented on April 20, 2024

Ok, I played around with this a bit. I think a couple of things would be useful additions to the API:

  1. A prop function on Editor to expose drops:
    handleDrop?: (e: SyntheticDragEvent, target: SelectionState) => boolean
  2. A Modifier 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.

from draft-js.

nelix avatar nelix commented on April 20, 2024

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

from draft-js.

bkniffler avatar bkniffler commented on April 20, 2024

@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).

from draft-js.

bkniffler avatar bkniffler commented on April 20, 2024

@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?

from draft-js.

hellendag avatar hellendag commented on April 20, 2024

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.

from draft-js.

bkniffler avatar bkniffler commented on April 20, 2024

@hellendag Thanks for creating the slack, though I think you should enable email signup to make it easier to join in :)

from draft-js.

rgbkrk avatar rgbkrk commented on April 20, 2024

I'd love to join too.

from draft-js.

hellendag avatar hellendag commented on April 20, 2024

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.

from draft-js.

balupton avatar balupton commented on April 20, 2024

For slack you can use https://github.com/rauchg/slackin to automate the process

from draft-js.

kozhuhds avatar kozhuhds commented on April 20, 2024

@jan4984 cool! could you provide code examples or demo?

from draft-js.

leejaen avatar leejaen commented on April 20, 2024

Amazing job! 1000 Thanks!

from draft-js.

praveenchand avatar praveenchand commented on April 20, 2024

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.

from draft-js.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.