# All files / src/mixins TextAreaSelectionMixin.js

89.29% Statements 25/28
62.5% Branches 5/8
81.82% Functions 9/11
89.29% Lines 25/28

    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
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    754x
    4x

    4x

    4x

    1x
    1x

    21x
    21x
    21x

    19x

    2x

    2x

    2x

    1x
    1x
    1x
    1x

    1x

    1x
    1x

    1x

    1x
    1x
    1x

    4x
     var MarkdownEditorActions = require('../actions/MarkdownEditorActions');
    var Reflux = require('reflux');

    var _timerClick;
    var _canClear = true;

    var TextAreaSelectionMixin = {
      mixins: [Reflux.ListenerMixin],

      clearSelection: function() {
        Eif (_canClear) {
          MarkdownEditorActions.clearSelection();
        }
      },

      bindSelectEvent: function() {
        Eif (this.refs.editor !== null) {
          this.textAreaElem = this.refs.editor;
          this.textAreaElem.addEventListener('select', this.onSelectHandler);
        }
      },

      componentDidMount: function() {
        this.bindSelectEvent();
      },

      componentWillUpdate: function() {
        this.unbindSelectEvent();
      },

      componentDidUpdate: function() {
        this.bindSelectEvent();
      },

      unbindSelectEvent: function() {
        this.textAreaElem.removeEventListener('select', this.onSelectHandler);
      },

      componentWillUnmount: function() {
        this.unbindSelectEvent();
      },

      onSelectHandler: function(e) {
        var _eventSource = this._getEventSource(e);
        var _selectionStart = _eventSource.selectionStart;
        var _selectionEnd = _eventSource.selectionEnd;
        var _selectedText = _eventSource.value.slice(_selectionStart, _selectionEnd);

        var selection = {
          selectionStart: _selectionStart,
          selectionEnd: _selectionEnd,
          selectedText: _selectedText
        };

        MarkdownEditorActions.setSelection(selection);
        this._preventClearSelectionAfterSelectIfNeeded(e);
      },

      _getEventSource: function(e) {
        return e.srcElement || e.target;
      },

      _preventClearSelectionAfterSelectIfNeeded: function(e) {
        Eif (e.target !== null) {
          _canClear = false;
          _timerClick = setTimeout(function() {
            _canClear = true;
            _timerClick = null;
          }, 100);
        }
      }
    };

    module.exports = TextAreaSelectionMixin;