# All files / src/components MarkdownEditorContent.js

100% Statements 18/18
100% Branches 0/0
100% Functions 4/4
100% Lines 18/18

    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
    614x
    4x
    4x
    4x
    4x
    4x

    4x

    21x
    21x

    21x

    2x
    2x
    2x

    2x

    19x

    2x

    4x

    4x
     var React = require('react');
    var ReactDOM = require('react-dom');
    var MarkdownUtils = require('../utils/MarkdownUtils');
    var PublicMarkdownEditorActions = require('../actions/PublicMarkdownEditorActions');
    var TextAreaSelectionMixin = require('../mixins/TextAreaSelectionMixin');
    var objectAssign = require('object-assign');

    var MarkdownEditorContent = React.createClass({
      propTypes: {
        content: React.PropTypes.string.isRequired,
        onChangeHandler: React.PropTypes.func.isRequired
      },

      mixins: [TextAreaSelectionMixin],

      render: function() {
        var styleMarkdownTextArea = MarkdownEditorContent.defaultProps.styles.styleMarkdownTextArea
        objectAssign(styleMarkdownTextArea, this.props.styles.styleMarkdownTextArea)

        return (
          <textarea
            ref='editor'
            className='md-editor-textarea'
            style={styleMarkdownTextArea}
            onChange={this.onChange}
            onClick={this.clearSelection}
            onKeyUp={this.clearSelection}>
          </textarea>
        );
      },

      onChange: function() {
        var content = this.refs.editor.value;
        var markdownContent = MarkdownUtils.toMarkdown(content);
        PublicMarkdownEditorActions.updateText(markdownContent);

        this.props.onChangeHandler(content.replace(/[\n\r]/g, '\n'));
      },

      componentDidMount: function() {
        this.refs.editor.value = this.props.content;
      },

      componentDidUpdate: function() {
        this.refs.editor.value = this.props.content;
      }
    });

    MarkdownEditorContent.defaultProps = {
      styles : {
        styleMarkdownTextArea : {
          height: '90%',
          width: '100%',
          padding: '30px 10px',
          border: 'none'
        }
      }
    }

    module.exports = MarkdownEditorContent;