Пример #1
0
 it('must identify the full list as a single range', () => {
   var cb = jest.fn();
   findRangesImmutable(list, returnTrue, returnTrue, cb);
   var calls = cb.mock.calls;
   expect(calls.length).toBe(1);
   expect(calls[0]).toEqual([0, 5]);
 });
Пример #2
0
/**
 * Helper function for getting encoded styles for each inline style. Convert
 * to UTF-8 character counts for storage.
 */
function getEncodedInlinesForType(
  block: ContentBlock,
  styleList: List<DraftInlineStyle>,
  styleToEncode: string
): Array<InlineStyleRange> {
  var ranges = [];

  // Obtain an array with ranges for only the specified style.
  var filteredInlines = styleList
    .map(style => style.has(styleToEncode))
    .toList();

  findRangesImmutable(
    filteredInlines,
    areEqual,
    // We only want to keep ranges with nonzero style values.
    isTruthy,
    (start, end) => {
      var text = block.getText();
      ranges.push({
        offset: UnicodeUtils.strlen(text.slice(0, start)),
        length: UnicodeUtils.strlen(text.slice(start, end)),
        style: styleToEncode,
      });
    }
  );

  return ranges;
}
Пример #3
0
/**
 * 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<CharacterMetadata>,
  entityKey: ?string,
  offset: number,
): {
  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, // 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};
      }
    },
  );
  invariant(
    typeof removalRange === 'object',
    'Removal range must exist within character list.',
  );
  return removalRange;
}
Пример #4
0
const assertFindRangesImmutable = (
  list,
  areEqualFn = returnTrue,
  filterFn = returnTrue,
  foundFn = jest.fn(),
) => {
  findRangesImmutable(list, areEqualFn, filterFn, foundFn);
  expect(foundFn.mock.calls).toMatchSnapshot();
};
Пример #5
0
 it('must be a no-op for an empty list', () => {
   var cb = jest.fn();
   findRangesImmutable(
     Immutable.List(),
     returnTrue,
     returnTrue,
     cb,
   );
   expect(cb.mock.calls.length).toBe(0);
 });
Пример #6
0
 findEntityRanges(
   filterFn: (value: CharacterMetadata) => boolean,
   callback: (start: number, end: number) => void,
 ): void {
   findRangesImmutable(
     this.getCharacterList(),
     haveEqualEntity,
     filterFn,
     callback,
   );
 }
Пример #7
0
 it('must properly use `filterFn`', () => {
   var cb = jest.fn();
   findRangesImmutable(
     list,
     returnTrue,
     () => false, // never an accepted filter result
     cb,
   );
   var calls = cb.mock.calls;
   expect(calls.length).toBe(0);
 });
Пример #8
0
 it('must identify each range', () => {
   var cb = jest.fn();
   findRangesImmutable(
     list,
     (a, b) => a === b,
     returnTrue,
     cb,
   );
   var calls = cb.mock.calls;
   expect(calls.length).toBe(4);
   expect(calls[0]).toEqual([0, 2]);
   expect(calls[1]).toEqual([2, 4]);
   expect(calls[2]).toEqual([4, 6]);
   expect(calls[3]).toEqual([6, 8]);
 });
Пример #9
0
  generate: function(
    contentState: ContentState,
    block: ContentBlock,
    decorator: ?DraftDecoratorType,
  ): List<DecoratorRange> {
    var textLength = block.getLength();
    if (!textLength) {
      return List.of(
        new DecoratorRange({
          start: 0,
          end: 0,
          decoratorKey: null,
          leaves: List.of(
            new LeafRange({start: 0, end: 0}),
          ),
        }),
      );
    }

    var leafSets = [];
    var decorations = decorator ?
      decorator.getDecorations(block, contentState) :
      List(Repeat(null, textLength));

    var chars = block.getCharacterList();

    findRangesImmutable(
      decorations,
      areEqual,
      returnTrue,
      (start, end) => {
        leafSets.push(
          new DecoratorRange({
            start,
            end,
            decoratorKey: decorations.get(start),
            leaves: generateLeaves(
              chars.slice(start, end).toList(),
              start,
            ),
          }),
        );
      },
    );

    return List(leafSets);
  },
Пример #10
0
 it('must properly use `areEqualFn`', () => {
   var cb = jest.fn();
   var areEqual = () => false;
   findRangesImmutable(
     list,
     areEqual, // never equal
     returnTrue,
     cb,
   );
   var calls = cb.mock.calls;
   expect(calls.length).toBe(5);
   expect(calls[0]).toEqual([0, 1]);
   expect(calls[1]).toEqual([1, 2]);
   expect(calls[2]).toEqual([2, 3]);
   expect(calls[3]).toEqual([3, 4]);
   expect(calls[4]).toEqual([4, 5]);
 });
Пример #11
0
/**
 * Generate LeafRange records for a given character list.
 */
function generateLeaves(
  characters: List<CharacterMetadata>,
  offset: number
): List<LeafRange> {
  var leaves = [];
  var inlineStyles = characters.map(c => c.getStyle()).toList();
  findRangesImmutable(
    inlineStyles,
    areEqual,
    returnTrue,
    (start, end) => {
      leaves.push(
        new LeafRange({
          start: start + offset,
          end: end + offset,
        })
      );
    }
  );
  return List(leaves);
}
Пример #12
0
function getRemovalRange(
  characters: List<CharacterMetadata>,
  key: ?string,
  offset: number
): Object {
  var removalRange;
  findRangesImmutable(
    characters,
    (a, b) => a.getEntity() === b.getEntity(),
    element => element.getEntity() === key,
    (start, end) => {
      if (start <= offset && end >= offset) {
        removalRange = {start, end};
      }
    }
  );
  invariant(
    typeof removalRange === 'object',
    'Removal range must exist within character list.'
  );
  return removalRange;
}