All files / src/components MarkdownEditorContent.js

82.35% Statements 42/51
50% Branches 7/14
80% Functions 12/15
82.35% Lines 42/51

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 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 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 1384x 4x 4x 4x 4x 4x 4x 4x 4x       4x                 7x           1x 1x         9x       7x 7x 7x 7x         2x 2x 2x         2x 2x 2x 2x         2x                     1x 1x 1x 1x   1x           1x 1x       1x       1x 1x 1x               9x   9x     11x                                     4x                     4x  
var React = require('react');
var ReactDOM = require('react-dom');
var Reflux = require('reflux');
var PropTypes = require('prop-types');
var createClass = require('create-react-class');
var MarkdownUtils = require('../utils/MarkdownUtils');
var MarkdownEditorActions = require('../actions/MarkdownEditorActions');
var PublicMarkdownEditorActions = require('../actions/PublicMarkdownEditorActions');
var objectAssign = require('object-assign');
 
var _timerClick;
 
var MarkdownEditorContent = createClass({
  propTypes: {
    content: PropTypes.string.isRequired,
    onChangeHandler: PropTypes.func.isRequired
  },
 
  mixins: [Reflux.ListenerMixin],
 
  getInitialState: function() {
    return {
      canClear: true,
    }
  },
 
  clearSelection: function() {
    Eif (this.state.canClear) {
      MarkdownEditorActions.clearSelection();
    }
  },
 
  bindSelectEvent: function(element) {
    element.addEventListener('select', this.onSelectHandler);
  },
 
  componentDidMount: function() {
    var element = this.textAreaElement;
    Eif (element) {
      this.bindSelectEvent(element);
      element.value = this.props.content;
    }
  },
 
  componentWillUpdate: function() {
    var element = this.textAreaElement;
    Eif (element) {
      this.unbindSelectEvent(element);
    }
  },
 
  componentDidUpdate: function() {
    var element = this.textAreaElement;
    Eif (element) {
      this.bindSelectEvent(element);
      element.value = this.props.content;
    }
  },
 
  unbindSelectEvent: function(element) {
    element.removeEventListener('select', this.onSelectHandler);
  },
 
  componentWillUnmount: function() {
    var element = this.textAreaElement;
    if (element) {
      this.unbindSelectEvent(element);
    }
  },
 
  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) {
      this.setState({canClear: false});
      _timerClick = setTimeout(function() {
        this.setState({canClear: true});
        _timerClick = null;
      }.bind(this), 100);
    }
  },
 
  render: function() {
     var styleMarkdownTextArea = objectAssign(MarkdownEditorContent.defaultProps.styles.styleMarkdownTextArea, this.props.styles.styleMarkdownTextArea)
 
    return (
      <textarea
        id='rmd-textarea'
        ref={(textArea) => { this.textAreaElement = textArea; }}
        className='md-editor-textarea'
        style={styleMarkdownTextArea}
        onChange={this.onChange}
        onClick={this.clearSelection}
        onKeyUp={this.clearSelection}>
      </textarea>
    );
  },
 
  onChange: function() {
    var content = document.getElementById('rmd-textarea').value;
    var markdownContent = MarkdownUtils.toMarkdown(content);
    PublicMarkdownEditorActions.updateText(markdownContent);
 
    this.props.onChangeHandler(content.replace(/[\n\r]/g, '\n'));
  },
});
 
MarkdownEditorContent.defaultProps = {
  styles : {
    styleMarkdownTextArea : {
      height: '90%',
      width: '100%',
      padding: '30px 10px',
      border: 'none'
    }
  }
}
 
module.exports = MarkdownEditorContent;