# All files / composer/utils/draft-js-custom-plugins/autocomplete/utils removeFacebookAutocompleteEntities.js

6.25% Statements 1/16
0% Branches 0/4
0% Functions 0/4
6.67% Lines 1/15

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63

    7x

     import { EditorState, Modifier, SelectionState } from '@bufferapp/draft-js';

    /**
     * Remove all Facebook Autocomplete entities from the editorState.
     * Currently used when copying a draft's contents to another that doesn't support
     * Facebook entities.
     */
    const removeFacebookAutocompleteEntities = function(editorState) {
      let contentState = editorState.getCurrentContent();
      const originalSelectionBefore = contentState.getSelectionBefore();
      const originalSelection = editorState.getSelection();

      const filterFacebookAutocompleteEntities = (characterMetadata) => {
        const entityKey = characterMetadata.getEntity();
        if (entityKey === null) return false;

        const entityType = contentState.getEntity(entityKey).getType();
        return entityType === 'mention' || entityType === 'IMPORTED_MENTION';
      };

      contentState.getBlockMap().forEach((contentBlock) => {
        const blockKey = contentBlock.getKey();

        contentBlock.findEntityRanges(filterFacebookAutocompleteEntities, (start, end) => {
          contentState = Modifier.applyEntity(
            contentState,
            new SelectionState({
              anchorKey: blockKey,
              anchorOffset: start,
              focusKey: blockKey,
              focusOffset: end,
            }),
            null
          );
        });
      });

      /**
       * Set selectionBefore/After back to what they were before they were changed
       * by Modifier.applyEntity for proper undo/redo behavior.
       */
      contentState = contentState.merge({
        selectionBefore: originalSelectionBefore,
        selectionAfter: originalSelection,
      });

      /**
       * Instead of using EditorState.push() which normally should be used for pushing
       * new actions to the editor, we update the editor's state directly in order
       * to keep the undo stack as is. The original SelectionState is also preserved
       * for the process to be completely seamless to users, and lastChangeType is
       * udpated to 'apply-entity' so that future keystrokes/changes are considered
       * boundary states for undo/redo.
       */
      return EditorState.set(editorState, {
        currentContent: contentState,
        lastChangeType: 'apply-hashtag',
        selection: originalSelection,
      });
    };

    export default removeFacebookAutocompleteEntities;