Skip to content

Commit

Permalink
fix getEntityMap fn (#4512)
Browse files Browse the repository at this point in the history
It was causing an issue with embedding articles. It should add embedded articles as associations, but because getEntityMap was reading entities from ALL content states that existed during page lifecycle - associations were being added to articles that didn't have any article embeds
  • Loading branch information
tomaskikutis authored Apr 25, 2024
1 parent a0aa4fe commit 86e8c00
Showing 1 changed file with 14 additions and 12 deletions.
26 changes: 14 additions & 12 deletions scripts/core/editor3/helpers/get-entity-map.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import {ContentState} from 'draft-js';
import {Map} from 'immutable';
import {noop} from 'lodash';

// .getAllEntities isn't available in current draft-js version.
// .getEntityMap doesn't work
// .entityMap appears to be private and only works sometimes. Other times it returns what appears to be a entity class.

Expand All @@ -9,20 +11,20 @@ type DraftEntityInstance = ReturnType<ContentState['getEntity']>;
export function getEntityMap(contentState: ContentState): Map<string, DraftEntityInstance> {
let entityMap = Map<string, DraftEntityInstance>();

let endReached = false;
let i = 1;
contentState.getBlockMap().forEach((block) => {
block.findEntityRanges(
(char) => {
const entityKey = char.getEntity();

while (!endReached) {
try {
const entity = contentState.getEntity(i.toString());
if (entityKey != null) {
entityMap = entityMap.set(entityKey, contentState.getEntity(entityKey));
}

entityMap = entityMap.set(i.toString(), entity);

i++;
} catch (e) {
endReached = true;
}
}
return false;
},
noop,
);
});

return entityMap;
}

0 comments on commit 86e8c00

Please sign in to comment.