# All files / composer/utils/draft-js-custom-plugins/inspect-selection/utils getCharacterBeforeSelectionStart.js

10% Statements 1/10
0% Branches 0/2
0% Functions 0/1
11.11% Lines 1/9

Press n or j to go to the next uncovered block, b, p or k for the previous block.

    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

    1x

     /**
     * Note: Both SelectionState.getStartOffset() and String.prototype.charAt() don't
     * support non-BMP characters: surrogate pairs are treated individually.
     *
     * This isn't an issue as getCharacterBeforeSelectionStart() is currently only
     * used to determine if that character is whitespace or not; but we'll want
     * to keep this limitation in mind if we start using that method for other purposes.
     */

    const getCharacterBeforeSelectionStart = function(store) {
      const editorState = store.getEditorState();
      const selectionState = editorState.getSelection();

      const startOffset = selectionState.getStartOffset();
      if (startOffset === 0) return null;

      const startKey = selectionState.getStartKey();
      const selectedBlock = editorState.getCurrentContent().getBlockForKey(startKey);
      const characterBeforeSelectionStart = selectedBlock.getText().charAt(startOffset - 1);

      return characterBeforeSelectionStart;
    };

    export default getCharacterBeforeSelectionStart;