question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

A problem about Draft when remove images by Backspace key

See original GitHub issue

Finally I decide to report it after all my try. I do not know whether it’s a bug when you use backspace key to remove a image that created by AtomicBlockUtils. Draftjs just remove the atomic block in contentState but the image’s entity still in the EntityMap:

gif

this is my code to insert a image:

hdl_addImage2EditerState(ediorState) {
    const contentState = ediorState.getCurrentContent();

    const contentStateWithEntity = contentState.createEntity(
      "IMAGE",
      "IMMUTABLE",
      { src: "https://avatars2.githubusercontent.com/u/9550456?v=4&s=460" }
    );
    const entityKey = contentStateWithEntity.getLastCreatedEntityKey();
    const editorStateWithEntity = EditorState.set(ediorState, {
      currentContent: contentStateWithEntity
    });

    const newEditorState = AtomicBlockUtils.insertAtomicBlock(
      editorStateWithEntity,
      entityKey,
      ' '//can't remove this character because the cursor jumps before it....
    );
    return EditorState.forceSelection(
      newEditorState,
      newEditorState.getCurrentContent().getSelectionAfter()
    );
  }

and when I replace the ' ' with the image’s url:

const newEditorState = AtomicBlockUtils.insertAtomicBlock(
      editorStateWithEntity,
      entityKey,
      "https://avatars2.githubusercontent.com/u/9550456?v=4&s=460"
    );

you will see: gif2

the browser is Chrome 62 the OS is win7 the vision of Draft is 0.10.4

and it’s similar happened in draft-js-plugin’s example… gif3

Issue Analytics

  • State:open
  • Created 6 years ago
  • Reactions:1
  • Comments:17 (2 by maintainers)

github_iconTop GitHub Comments

5reactions
TeemoWancommented, Dec 24, 2017

hi,希望能解决你的问题

  1. remove entities of current atomic block
    const contentState = editorState.getCurrentContent();
    const withoutAtomicEntity = Modifier.removeRange(
      contentState,
      new SelectionState({
        anchorKey: atomicBlock.getKey(),
        anchorOffset: 0,
        focusKey: atomicBlock.getKey(),
        focusOffset: atomicBlock.getLength(),
      }),
      'backward',
    );
  1. remove atomic block
      const blockMap = withoutAtomicEntity.getBlockMap().delete(atomicBlock.getKey());
      var withoutAtomic = withoutAtomicEntity.merge({
        blockMap,
        selectionAfter: selection,
      });

3.set EditorState

        return EditorState.push(
          editorState,
          withoutAtomic,
          'remove-range',
        );
4reactions
thibaudcolascommented, Mar 6, 2021

I believe this is the same issue as #1354, #915, #483, (https://github.com/facebook/draft-js/issues/1681#issuecomment-371087387).

The solution is to use RichUtils.handleKeyCommand in the handleKeyCommand handler, so it can properly handle backspace on atomic blocks. Further details: https://github.com/facebook/draft-js/issues/1681#issuecomment-371143376.

Read more comments on GitHub >

github_iconTop Results From Across the Web

A problem about Draft when remove images by Backspace key
When deleting an image with text in the draft.js editor for image upload, occasionally the IMAGE entity remains even though the image was ......
Read more >
Send or delete an email stuck in your outbox - Microsoft Support
Just select it and press Delete. Drag the message to your drafts folder, double-click to open the message, delete the attachment (click it...
Read more >
Create, edit, manage, or delete a post - Blogger Help
You can write, edit, or delete posts and drafts any time. Write a new post Sign in to Blogger. Click New Post.
Read more >
How do I delete one or more Draft posts?
Hover over any draft you'd like to delete and select the check box in the top right corner of the image. You can...
Read more >
How To Delete Drafts On TikTok! (2022) - YouTube
Get The Cheapest iPhones Here: https://amzn.to/3JTnWArGet The Cheapest Androids Here: https://amzn.to/3r2k1stFollow Me On Twitter: ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found