Draft.js. How to get all entities data from the ContentState

draft-js mentions
draft js contentstate
draft-js get current block
draft-js converttoraw
draft-js-autocomplete
draft-js block types
draft-js tutorial
draft-js-markdown

From official docs I know about 2 methods: get entity by its key and get last created entity. In my case, I also need a method to access all entities from current ContentState. Is there any method that could perform this? If not, is there a one that can provide all entities keys?


const getEntities = (editorState, entityType = null) => {
    const content = editorState.getCurrentContent();
    const entities = [];
    content.getBlocksAsArray().forEach((block) => {
        let selectedEntity = null;
        block.findEntityRanges(
            (character) => {
                if (character.getEntity() !== null) {
                    const entity = content.getEntity(character.getEntity());
                    if (!entityType || (entityType && entity.getType() === entityType)) {
                        selectedEntity = {
                            entityKey: character.getEntity(),
                            blockKey: block.getKey(),
                            entity: content.getEntity(character.getEntity()),
                        };
                        return true;
                    }
                }
                return false;
            },
            (start, end) => {
                entities.push({...selectedEntity, start, end});
            });
    });
    return entities;
};

Draft.js. How to get all entities data from the ContentState, How I get the all entities keys: const contentState = editorState.getCurrentContent​() const entityKeys = Object.keys(convertToRaw(contentState).entityMap)  Entities should be created using contentState.createEntity, which accepts the three properties above as arguments. This method returns a ContentState record updated to include the newly created entity, then you can call contentState.getLastCreatedEntityKey to get the key of the newly created entity record. This key is the value that should be


How I get the all entities keys:

const contentState = editorState.getCurrentContent()
const entityKeys = Object.keys(convertToRaw(contentState).entityMap)

result:

[0, 1]

then you can call the getEntity(key) method to get the responding entity.

this is how convertToRaw(contentState) looks:

Entities, For instance, a 'LINK' entity might contain a data object that contains the href value for that link. All entities are stored in the ContentState record. getLastCreatedEntityKey to get the key of the newly created entity record. ContentState is an Immutable Record that represents the full state of:. The entire contents of an editor: text, block and inline styles, and entity ranges.; Two selection states of an editor: before and after the rendering of these contents.


Bao, You will find it inside key called 'blocks'.

convertToRaw(contentState).blocks.map(el=>el.text)

ContentState, ContentState is an Immutable. The entire contents of an editor: text, block and inline styles, and entity ranges. Returns an object store containing all DraftEntity records that have been created. except it will totally discard the existing data value for the instance and replace it with the specified newData. import {EditorState} from ' draft-js ' function removeBlock (editorState, block) { const contentState = editorState. getCurrentContent () const newBlockMap = contentState. blockMap. delete (atomicBlock. getKey ()) // this is the important one that actually deletes a block const newContentState = contentState. set (' blockMap ', newBlockMap


Unfortunatelly your suggested way using convertToRaw doesnt work because it reindexes all keys to ["0", .., "n"], but the real keys differ when you act with the editor. New ones > n will be added and unused will be omitted.

ContentBlock, A ContentState object contains an OrderedMap of these ContentBlock objects, By making heavy use of immutability and data persistence for these lists and to the content generally have little impact on the memory footprint of the editor. This List contains all styling and entity information for the block. I have been playing with Draft.js and I'm stuck trying to get immutable entities working properly. I would like to insert an immutable entity when a user clicks a button. Here is the function that inserts the entity:


const rawState = convertToRaw(contentState) const { entityMap } = rawState;

This entityMap will have list of all entities. But this is an expensive conversion. Because, it will convert whole thing to raw. A better way is loop through blocks and check for entity.

Data Conversion, The raw state contains a list of content blocks, as well as a map of all relevant entity objects. Functions#. convertFromRaw() #. Copy. Contribute to facebook/draft-js development by creating an account on GitHub. Summary: **Summary** Prettier can also be used to format more than just JS files, it can format Markdown files too. Instead of using Flow parser for all files in `prettier.config.js`, we can just


DraftEntity should provide a method like "getAll" to return all , DraftEntity does not provide a method to return all instances of Entity. of Entity const entitiesWillLiterate = contentState.getEntityMap().getAll(); Which versions of Draft.js, and which browser / OS are affected by this issue? but I've been using the following to get the key of/data in the selected entity: The editor plugin also needs a decorator (for inline entities) or block (for block entities): a React component responsible for displaying entity instances within the editor. Like for inline styles and blocks, set up the to/from DB conversion. The conversion usually is more involved, since entities contain data that needs to be serialised to HTML.


javascript: Draft.js. How to get all entities data from the ContentState, From official docs I know about 2 methods: get entity by its key and get last created entity. In my case, I also need a method to access all entities from current​  Demonstrates inline equations with copy/paste supported in draft.js - draftjs_inline_equations.js


Entities, I have hooked up the console to automatically receive the entity information at the data. To give you an instance of a real world usage of entities — suppose, we If you have read the contents inside the editor, it pretty much explains about all the key of the newly created DraftEntity record. const entityKey = contentstate. data: An optional object containing metadata for the entity. For instance, a 'LINK' entity might contain a data object that contains the href value for that link. All entities are stored in a single object store within the Entity module, and are referenced by key within ContentState and React components used to decorate annotated ranges.