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.

Image delete keep entity into state

See original GitHub issue

When deleting an image, the entity remains binded to the block above into the entityRanges and the entity is not removed from the entityMap. https://codesandbox.io/s/4ql8746pl7 Step to reproduce:

  • delete the image
  • log the state in console with the proper button

state before:

{
  "entityMap": {
    "0": {
      "type": "image",
      "mutability": "IMMUTABLE",
      "data": {
        "src": "https://www.draft-js-plugins.com/images/canada-landscape-small.jpg"
      }
    }
  },
  "blocks": [
    {
      "key": "9gm3s",
      "text": "You can have images in your text field. This is a very rudimentary example, but you can enhance the image plugin with resizing, focus or alignment plugins.",
      "type": "unstyled",
      "depth": 0,
      "inlineStyleRanges": [],
      "entityRanges": [],
      "data": {}
    },
    {
      "key": "ov7r",
      "text": " ",
      "type": "atomic",
      "depth": 0,
      "inlineStyleRanges": [],
      "entityRanges": [
        {
          "offset": 0,
          "length": 1,
          "key": 0
        }
      ],
      "data": {}
    },
    {
      "key": "e23a8",
      "text": "See advanced examples further down …",
      "type": "unstyled",
      "depth": 0,
      "inlineStyleRanges": [],
      "entityRanges": [],
      "data": {}
    }
  ]
}

state after:

{
  "entityMap": {
    "0": {
      "type": "image",
      "mutability": "IMMUTABLE",
      "data": {
        "src": "https://www.draft-js-plugins.com/images/canada-landscape-small.jpg"
      }
    }
  },
  "blocks": [
    {
      "key": "9gm3s",
      "text": "You can have images in your text field. This is a very rudimentary example, but you can enhance the image plugin with resizing, focus or alignment plugins. See advanced examples further down …",
      "type": "unstyled",
      "depth": 0,
      "inlineStyleRanges": [],
      "entityRanges": [
        {
          "offset": 155,
          "length": 1,
          "key": 0
        }
      ],
      "data": {}
    }
  ]
}

i noticed that in the draft-js media example this didn’t happen, but for now I’ve not been able to solve the problem

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:3
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

9reactions
khsilycommented, Nov 16, 2017

It’s not a real bug-fix but i fixed it like this as a tricky hack.

 onChange(editorState) {
        let newEditorState = editorState;

        const content = editorState.getCurrentContent();
        const key = editorState.getSelection().getFocusKey();
        const type = content.getBlockForKey(key).getType();

        if (type === 'atomic') {
            const keyAfter = content.getKeyAfter(key);
            const newSelection = SelectionState.createEmpty(keyAfter);
            newEditorState = EditorState.forceSelection(editorState, newSelection);
        }

        this.setState({ editorState: newEditorState });
};

handleKeyCommand(command, editorState) {
        const newContent = RichUtils.handleKeyCommand(editorState, command);
        if (newContent) {
            this.onChange(newContent);
            return 'handled';
        } else {
            return 'not-handled';
        }
}

I just tried to move cursor after the atomic block when it is focused so that I could delete images without the bug.

2reactions
kalimantoscommented, Nov 6, 2017

I found a solution but it doesn’t solve the problem at all. Adding

handleKeyCommand: (command, _, { getEditorState, setEditorState }) => {
  const newState = RichUtils.handleKeyCommand(getEditorState(), command);
  if (newState) {
    setEditorState(newState);
    return 'handled';
  }
  return 'not-handled';
},

to the returned object of image plugin it solves the problem. But i think this happens with all the atomic blocks, so putting this code in image-plugin could not be the better solution. The same problem also happen when using the focus plugin and deleting the image when is focused and this “quick fix” doesn’t solve that.

Anyone have some idea where is the best place to fix them? And how fix the problem also for the focus plugin without having some conflicts between the 2 fix?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Deleting an entity with revisions and file/image field does not ...
Images /files uploaded to a file/image fields on entities with multiple revisions are never deleted, even when the entity is deleted.
Read more >
Entity Framework: Deleting an Item from a Collection - TechNet
In this article, let's see how we can remove/delete an item from a collection in terms of Entity Framework. It's always better to...
Read more >
Remove an entity from draft-js Editor Current State on button ...
I'm trying to remove images on my rich text editor with a click of a button embedded in the image div using a...
Read more >
More Efficient Deletes With Entity Framework Core
Using EF Core's Change Tracker to generate DELETE SQL Statements and ... You change the EntityEntry<Person> to have a State of Deleted ....
Read more >
entity:delete - Drush
Delete content entities. To delete configuration entities, see config:delete command. Examples¶. drush entity:delete node --bundle=article .
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