diff --git a/src/model/transaction/removeEntitiesAtEdges.js b/src/model/transaction/removeEntitiesAtEdges.js index 8aeab0d4da..b5b630e2d2 100644 --- a/src/model/transaction/removeEntitiesAtEdges.js +++ b/src/model/transaction/removeEntitiesAtEdges.js @@ -65,18 +65,36 @@ function removeEntitiesAtEdges( }); } +/** + * Given a list of characters and an offset that is in the middle of an entity, + * returns the start and end of the entity that is overlapping the offset. + * Note: This method requires that the offset be in an entity range. + */ function getRemovalRange( characters: List, - key: ?string, + entityKey: ?string, offset: number, -): Object { +): { + start: number, + end: number, +} { var removalRange; + + // Iterates through a list looking for ranges of matching items + // based on the 'isEqual' callback. + // Then instead of returning the result, call the 'found' callback + // with each range. + // Then filters those ranges based on the 'filter' callback + // + // Here we use it to find ranges of characters with the same entity key. findRangesImmutable( - characters, - (a, b) => a.getEntity() === b.getEntity(), - element => element.getEntity() === key, - (start, end) => { + characters, // the list to iterate through + (a, b) => a.getEntity() === b.getEntity(), // 'isEqual' callback + element => element.getEntity() === entityKey, // 'filter' callback + (start: number, end: number) => { + // 'found' callback if (start <= offset && end >= offset) { + // this entity overlaps the offset index removalRange = {start, end}; } },