<commit_before>/*
 * Editor based on Draft
 */

import React from 'react'
import PropTypes from 'prop-types'
import R from 'ramda'
import PubSub from 'pubsub-js'

import { EditorState, ContentState, Modifier } from 'draft-js'
import Editor from 'draft-js-plugins-editor'
import createMentionPlugin from 'draft-js-mention-plugin'
import createLinkifyPlugin from 'draft-js-linkify-plugin'

import { EVENT, makeDebugger } from 'utils'
import toRawString from './exportContent'
import { Wrapper } from './styles'

const linkifyPlugin = createLinkifyPlugin({
  theme: { link: 'markdown-editor-link' },
})

const mentionThemeClass = {
  mention: 'markdown-editor-mention',
  mentionSuggestions: 'markdown-editor-suggestions',
  mentionSuggestionsEntry: 'markdown-editor-mentionSuggestionsEntry',
  mentionSuggestionsEntryFocused:
    'markdown-editor-mentionSuggestionsEntryFocused',
  mentionSuggestionsEntryAvatar:
    'markdown-editor-mentionSuggestionsEntryAvatar',
  mentionSuggestionsEntryText: 'markdown-editor-mentionSuggestionsEntryText',
}

/* eslint-disable-next-line */
const debug = makeDebugger('C:MarkdownEditor')

const mentionFilter = (value, mentions) =>
  R.filter(m => R.startsWith(value, R.toLower(m.name)), mentions)

class MarkdownEditor extends React.Component {
  constructor(props) {
    super(props)

    this.mentionPlugin = createMentionPlugin({
      theme: mentionThemeClass,
      mentionPrefix: '@',
    })
    this.initPubSub()
  }

  state = {
    editorState: EditorState.createEmpty(),
    suggestions: [],
    mentionList: [],
    pub: null,
  }

  componentDidMount() {
    this.loadDraft()
    this.loadUserSuggestions()
  }

  componentWillReceiveProps(nextProps) {
    /* eslint-disable react/destructuring-assignment */
    if (
      nextProps.mentionList &&
      !R.equals(nextProps.mentionList, this.state.mentionList)
    ) {
      this.loadUserSuggestions(nextProps.mentionList)
    }
    /* eslint-enable react/destructuring-assignment */
  }

  componentWillUnmount() {
    const { pub } = this.state
    PubSub.unsubscribe(pub)
    this.clearContent()
  }

  initPubSub() {
    const pub = PubSub.subscribe(EVENT.DRAFT_INSERT_SNIPPET, (event, data) => {
      this.insertSnippet(data.data)
    })
    this.setState({ pub })
  }

  loadDraft = () => {
    const { body } = this.props

    // see: https://stackoverflow.com/questions/35884112/draftjs-how-to-initiate-an-editor-with-content
    const editorState = EditorState.createWithContent(
      ContentState.createFromText(body)
    )

    this.setState({ editorState }, () => {
      this.editor.componentWillMount()
    })
  }

  insertSnippet = data => {
    const { editorState } = this.state
    /* const contentState = ContentState.createFromText('ni ma') */
    const contentState = editorState.getCurrentContent()
    const selection = editorState.getSelection()
    const nextContentState = Modifier.insertText(
      contentState,
      selection,
      `\n${data}\n`
    )
<commit_msg>fix(md-editor): edage-case crash editor<commit_after>/*
 * Editor based on Draft
 */

import React from 'react'
import PropTypes from 'prop-types'
import R from 'ramda'
import PubSub from 'pubsub-js'

import { EditorState, ContentState, Modifier } from 'draft-js'
import Editor from 'draft-js-plugins-editor'
import createMentionPlugin from 'draft-js-mention-plugin'
import createLinkifyPlugin from 'draft-js-linkify-plugin'

import { EVENT, makeDebugger } from 'utils'
import toRawString from './exportContent'
import { Wrapper } from './styles'

const linkifyPlugin = createLinkifyPlugin({
  theme: { link: 'markdown-editor-link' },
})

const mentionThemeClass = {
  mention: 'markdown-editor-mention',
  mentionSuggestions: 'markdown-editor-suggestions',
  mentionSuggestionsEntry: 'markdown-editor-mentionSuggestionsEntry',
  mentionSuggestionsEntryFocused:
    'markdown-editor-mentionSuggestionsEntryFocused',
  mentionSuggestionsEntryAvatar:
    'markdown-editor-mentionSuggestionsEntryAvatar',
  mentionSuggestionsEntryText: 'markdown-editor-mentionSuggestionsEntryText',
}

/* eslint-disable-next-line */
const debug = makeDebugger('C:MarkdownEditor')

const mentionFilter = (value, mentions) =>
  R.filter(m => R.startsWith(value, R.toLower(m.name)), mentions)

class MarkdownEditor extends React.Component {
  constructor(props) {
    super(props)

    this.mentionPlugin = createMentionPlugin({
      theme: mentionThemeClass,
      mentionPrefix: '@',
    })
    this.initPubSub()
  }

  state = {
    editorState: EditorState.createEmpty(),
    suggestions: [],
    mentionList: [],
    pub: null,
  }

  componentDidMount() {
    this.loadDraft()
    this.loadUserSuggestions()
  }

  componentWillReceiveProps(nextProps) {
    /* eslint-disable react/destructuring-assignment */
    if (
      nextProps.mentionList &&
      !R.equals(nextProps.mentionList, this.state.mentionList)
    ) {
      this.loadUserSuggestions(nextProps.mentionList)
    }
    /* eslint-enable react/destructuring-assignment */
  }

  componentWillUnmount() {
    const { pub } = this.state
    PubSub.unsubscribe(pub)
    this.clearContent()
  }

  initPubSub() {
    const pub = PubSub.subscribe(EVENT.DRAFT_INSERT_SNIPPET, (event, data) => {
      this.insertSnippet(data.data)
    })
    this.setState({ pub })
  }

  loadDraft = () => {
    const { body } = this.props
    if (!body) return false

    // see: https://stackoverflow.com/questions/35884112/draftjs-how-to-initiate-an-editor-with-content
    const editorState = EditorState.createWithContent(
      ContentState.createFromText(body)
    )

    this.setState({ editorState }, () => {
      this.editor.componentWillMount()
    })
  }

  insertSnippet = data => {
    const { editorState } = this.state
    /* const contentState = ContentState.createFromText('ni ma') */
    const contentState = editorState.getCurrentContent()
    const selection = editorState.getSelection()
    const nextContentState = Modifier.insertText(
      contentState,
      selection,
      `\n${data}\n`
    )

{'fraction_non_alphanumeric': 0.12041018855441614, 'fraction_numerical': 0.0026463777704267286, 'mean_word_length': 3.784018987341772, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 15, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '650113', 'n_tokens_mistral': 1910, 'n_tokens_neox': 1863, 'n_words': 337}