# All files / src MarkdownEditor.js

63.38% Statements 45/71
18.18% Branches 4/22
50% Functions 4/8
63.38% Lines 45/71

    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
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    1844x
    4x
    4x
    4x
    4x
    4x
    4x
    4x
    4x
    4x
    4x
    4x
    4x
    4x
    4x
    4x
    4x
    4x
    4x

    4x
    4x
    4x
    4x
    4x
    4x
    4x

    4x

    21x
    21x

    23x
    23x

    23x

    23x

    23x
    23x

    23x
    23x

    23x

    2x
    1x

    2x

    19x
    19x

    4x

    4x
     var React = require('react');
    var ReactDOM = require('react-dom');
    var Reflux = require('reflux');
    var Markdown = require('markdown').markdown;
    var MarkdownEditorActions = require('./actions/MarkdownEditorActions');
    var PublicMarkdownEditorActions = require('./actions/PublicMarkdownEditorActions');
    var MarkdownSelectionActions = require('./actions/MarkdownSelectionActions');
    var TextAreaSelectionMixin = require('./mixins/TextAreaSelectionMixin');
    var ButtonManagerMixin = require('./mixins/ButtonManagerMixin');
    var MarkdownEditorStore = require('./stores/MarkdownEditorStore');
    var MarkdownSelectionStore = require('./stores/MarkdownSelectionStore');
    var MarkdownEditorTabsInteractionStore = require('./stores/MarkdownEditorTabsInteractionStore');
    var MarkdownTokenFactory = require('./utils/MarkdownTokenFactory');
    var MarkdownUtils = require('./utils/MarkdownUtils');
    var MarkdownEditorMenu = require('./components/MarkdownEditorMenu');
    var MarkdownEditorTabs = require('./components/MarkdownEditorTabs');
    var MarkdownEditorContent = require('./components/MarkdownEditorContent');
    var MarkdownEditorPreview = require('./components/MarkdownEditorPreview');
    var objectAssign = require('object-assign');

    var NullMarkdownToken = MarkdownTokenFactory.NullMarkdownToken;
    var RegularMarkdownToken = MarkdownTokenFactory.RegularMarkdownToken;
    var HeaderMarkdownToken = MarkdownTokenFactory.HeaderMarkdownToken;
    var SubHeaderMarkdownToken = MarkdownTokenFactory.SubHeaderMarkdownToken;
    var UrlMarkdownToken = MarkdownTokenFactory.UrlMarkdownToken;
    var ListMarkdownToken = MarkdownTokenFactory.ListMarkdownToken;
    var ImageMarkdownToken = MarkdownTokenFactory.ImageMarkdownToken;

    var MarkdownEditor = React.createClass({
      mixins: [Reflux.ListenerMixin],

      propTypes: {
        initialContent: React.PropTypes.string.isRequired,
        iconsSet: React.PropTypes.oneOf(['font-awesome', 'materialize-ui']).isRequired,
        onContentChange: React.PropTypes.func,
        editorTabs: React.PropTypes.bool
      },

      getInitialState: function() {
        var uniqueInstanceRef = Math.random().toString(36).substring(7)
        return {content: this.props.initialContent, inEditMode: true, instanceRef: uniqueInstanceRef};
      },

      render: function() {
        var divContent;
        var editorMenu;

        Eif (this.state.inEditMode) {
          divContent = <MarkdownEditorContent styles={{styleMarkdownTextArea: this.props.styles.styleMarkdownTextArea}}
                                              content={this.state.content} onChangeHandler={this.onChangeHandler}/>;
          Eif (this.props.editorTabs !== false){

              editorMenu = <MarkdownEditorMenu styles={{styleMarkdownMenu: this.props.styles.styleMarkdownMenu}}
                                                iconsSet={this.props.iconsSet} instanceRef={this.state.instanceRef}/>;
          }
        } else {
          divContent = <MarkdownEditorPreview styles={{styleMarkdownPreviewArea: this.props.styles.styleMarkdownPreviewArea}}
                                              content={this.state.content} />;
          editorMenu = null;
        }

        var styleMarkdownEditorHeader = MarkdownEditor.defaultProps.styles.styleMarkdownEditorHeader;
        objectAssign(styleMarkdownEditorHeader, this.props.styles.styleMarkdownEditorHeader);

        var styleMarkdownEditorContainer = MarkdownEditor.defaultProps.styles.styleMarkdownEditorContainer;
        objectAssign(styleMarkdownEditorContainer, this.props.styles.styleMarkdownEditorContainer);

        return (
          <div style={styleMarkdownEditorContainer}>
            <div style={styleMarkdownEditorHeader} className='md-editor-header'>
              {editorMenu}
              <MarkdownEditorTabs styles={{ styleMarkdownEditorTabs: this.props.styles.styleMarkdownEditorTabs,
                                            styleTab: this.props.styles.styleTab,
                                            styleActiveTab: this.props.styles.styleActiveTab}} />
            </div>
            {divContent}
          </div>
        );
      },

      onChangeHandler: function(newContent) {
        if (this.props.onContentChange) {
          this.props.onContentChange(newContent);
        }

        this.setState({content: newContent});
      },

      componentDidMount: function() {
        this.listenTo(MarkdownEditorStore, this.handleMarkdowEditorStoreUpdated);
        this.listenTo(MarkdownEditorTabsInteractionStore, this.handleMDEditorTabsInteractionStoreUpdated);
      },

      handleMarkdowEditorStoreUpdated: function(markdownEditorStoreState) {
        var currentSelection = markdownEditorStoreState.currentSelection;

        if (currentSelection != null && markdownEditorStoreState.instanceRef === this.state.instanceRef) {
          this.updateText(this.state.content, currentSelection, markdownEditorStoreState.action);
        }
      },

      handleMDEditorTabsInteractionStoreUpdated: function(mdEditorTabsInteractionStoreState) {
        if (mdEditorTabsInteractionStoreState.activeTab != null) {
          var _inEditMode = mdEditorTabsInteractionStoreState.activeTab === 0;
          this.setState({inEditMode: _inEditMode});
        }
      },

      updateText: function(text, selection, actionType) {
        var token = this.generateMarkdownToken(actionType);
        var beforeSelectionContent = text.slice(0, selection.selectionStart);
        var afterSelectionContent = text.slice(selection.selectionEnd, text.length);
        var updatedText = token.applyTokenTo(selection.selectedText);
        var _updatedContent = beforeSelectionContent + updatedText + afterSelectionContent;
        PublicMarkdownEditorActions.updateText(MarkdownUtils.toMarkdown(_updatedContent));
        this.setState({content: _updatedContent});

        if (this.props.onContentChange) {
          this.props.onContentChange(_updatedContent);
        }
      },

      generateMarkdownToken: function(actionType) {
        switch (actionType) {
          case 'bold':
            return new RegularMarkdownToken('**', true);

          case 'italic':
            return new RegularMarkdownToken('_', true);

          case 'header':
            return new HeaderMarkdownToken();

          case 'subheader':
            return new SubHeaderMarkdownToken();

          case 'link':
            return new UrlMarkdownToken();

          case 'list':
            return new ListMarkdownToken();

          case 'image':
            return new ImageMarkdownToken();

          default:
            return new NullMarkdownToken();
        }
      }
    });

    MarkdownEditor.defaultProps = {
       styles : {
            styleMarkdownEditorHeader : {
              'display': 'flex',
              'flexDirection': 'column',
              'borderBottom': '1px solid #ddd',
              'marginLeft': '0px',
              'marginRight': '0px',
              'minHeight': '50px',
              'justifyContent': 'center',
              'position': 'relative',
            },
            styleMarkdownEditorContainer : {
              'display': 'flex',
              'flexDirection': 'column',
              'marginTop': '2px',
              'paddingTop': '10px',
              'border': '1px solid #ddd',
              'backgroundColor': '#f7f7f7'
            },
            styleMarkdownMenu : {
                'margin': '5px 0',
                'flex': '1',
                'display': 'flex',
                'position': 'absolute',
                'right': '20px',
                'top': '10px'
            }
          }
    }

    module.exports = MarkdownEditor;