<commit_before>import React, { PropTypes } from "react";
import styled from "styled-components";
import Chromeless from "@atlaskit/editor-core/dist/es5/editor/ui/Appearance/Chromeless";
import { moveCursorToTheEnd } from "@atlaskit/editor-core/dist/es5/utils";
import ProviderFactory from "@atlaskit/editor-core/dist/es5/providerFactory";
import createEditor from "@atlaskit/editor-core/dist/es5/editor/create-editor/create-editor";
import pastePlugin from "@atlaskit/editor-core/dist/es5/editor/plugins/paste";
import basePlugin from "@atlaskit/editor-core/dist/es5/editor/plugins/base";
import blockTypePlugin from "@atlaskit/editor-core/dist/es5/editor/plugins/block-type";
import placeholderPlugin from "@atlaskit/editor-core/dist/es5/editor/plugins/placeholder";
import listPlugin from "@atlaskit/editor-core/dist/es5/editor/plugins/lists";
import tablesPlugin from "@atlaskit/editor-core/dist/es5/editor/plugins/tables";
import textFormattingPlugin from "@atlaskit/editor-core/dist/es5/editor/plugins/text-formatting";
import hyperlinkPlugin from "@atlaskit/editor-core/dist/es5/editor/plugins/hyperlink";
import actionsPlugin from "./plugins/actions";

const EditorStylesWrapper = styled.div`
  flex-grow: 1;
  overflow-y: scroll;
  overflow-x: hidden;
  height: 100%;
  position: relative;
  display: flex;
  flex-direction: column;
  padding-bottom: 8px;
  box-sizing: border-box;

  & > div {
    flex: 1;
    padding: 8px 28px 0;

    & > div {
      height: 100%;
    }
  }

  .ProseMirror {
    height: 100%;

    table {
      width: 100%;
      margin-left: 0;
      margin-right: 0;
    }

    & pre {
      font-family: "SFMono-Medium", "SF Mono", "Segoe UI Mono", "Roboto Mono",
        "Ubuntu Mono", Menlo, Courier, monospace;
      background: #f4f5f7;
      padding: 12px;
      border-radius: 3px;
    }

    & .code,
    & code {
      padding: 2px 4px;
      border-radius: 3px;
      background: #f4f5f7;
      font-size: 12px;
      line-height: 1.4;
    }
  }
`;

const contentTransformer = doc =>
  Promise.resolve({
    content: doc.toJSON(),
    time: Date.now(),
    textContent: doc.textContent,
    size: doc.textContent.length
  });

export default class Editor extends React.Component {
  static contextTypes = {
    editorActions: PropTypes.object
  };

  state = { editor: {} };

  constructor(props) {
    super(props);
    this.providerFactory = new ProviderFactory();
  }

  initEditor = place => {
    if (!place) return;
    const editor = createEditor(
      place,
      [
        pastePlugin,
        basePlugin,
        blockTypePlugin,
        placeholderPlugin,
        textFormattingPlugin,
        hyperlinkPlugin,
        listPlugin,
        tablesPlugin,
        actionsPlugin
      ],
      this.props,
      this.providerFactory
    );
    this.registerEditorForActions(editor);
    this.setState({ editor });

    if (!editor.editorView.hasFocus()) {
      editor.editorView.focus();
    }

    moveCursorToTheEnd(editor.editorView);
  };

  registerEditorForActions(editor) {
    if (this.context && this.context.editorActions) {
      this.context.editorActions._privateRegisterEditor(
        editor.editorView,
        editor.contentTransformer
      );
    }
  }

  unregisterEditorFromActions() {
    if (this.context && this.context.editorActions) {
      this.context.editorActions._privateUnregisterEditor();
    }
  }

  render() {
    const { editor } = this.state;
    const { editorView, contentComponents, eventDispatcher } = editor;

    return (<commit_msg>Unregister editor from actions on unmount<commit_after>import React, { PropTypes } from "react";
import styled from "styled-components";
import Chromeless from "@atlaskit/editor-core/dist/es5/editor/ui/Appearance/Chromeless";
import { moveCursorToTheEnd } from "@atlaskit/editor-core/dist/es5/utils";
import ProviderFactory from "@atlaskit/editor-core/dist/es5/providerFactory";
import createEditor from "@atlaskit/editor-core/dist/es5/editor/create-editor/create-editor";
import pastePlugin from "@atlaskit/editor-core/dist/es5/editor/plugins/paste";
import basePlugin from "@atlaskit/editor-core/dist/es5/editor/plugins/base";
import blockTypePlugin from "@atlaskit/editor-core/dist/es5/editor/plugins/block-type";
import placeholderPlugin from "@atlaskit/editor-core/dist/es5/editor/plugins/placeholder";
import listPlugin from "@atlaskit/editor-core/dist/es5/editor/plugins/lists";
import tablesPlugin from "@atlaskit/editor-core/dist/es5/editor/plugins/tables";
import textFormattingPlugin from "@atlaskit/editor-core/dist/es5/editor/plugins/text-formatting";
import hyperlinkPlugin from "@atlaskit/editor-core/dist/es5/editor/plugins/hyperlink";
import actionsPlugin from "./plugins/actions";

const EditorStylesWrapper = styled.div`
  flex-grow: 1;
  overflow-y: scroll;
  overflow-x: hidden;
  height: 100%;
  position: relative;
  display: flex;
  flex-direction: column;
  padding-bottom: 8px;
  box-sizing: border-box;

  & > div {
    flex: 1;
    padding: 8px 28px 0;

    & > div {
      height: 100%;
    }
  }

  .ProseMirror {
    height: 100%;

    table {
      width: 100%;
      margin-left: 0;
      margin-right: 0;
    }

    & pre {
      font-family: "SFMono-Medium", "SF Mono", "Segoe UI Mono", "Roboto Mono",
        "Ubuntu Mono", Menlo, Courier, monospace;
      background: #f4f5f7;
      padding: 12px;
      border-radius: 3px;
    }

    & .code,
    & code {
      padding: 2px 4px;
      border-radius: 3px;
      background: #f4f5f7;
      font-size: 12px;
      line-height: 1.4;
    }
  }
`;

export default class Editor extends React.Component {
  static contextTypes = {
    editorActions: PropTypes.object
  };

  state = { editor: {} };

  constructor(props) {
    super(props);
    this.providerFactory = new ProviderFactory();
  }

  initEditor = place => {
    if (!place) return;
    const editor = createEditor(
      place,
      [
        pastePlugin,
        basePlugin,
        blockTypePlugin,
        placeholderPlugin,
        textFormattingPlugin,
        hyperlinkPlugin,
        listPlugin,
        tablesPlugin,
        actionsPlugin
      ],
      this.props,
      this.providerFactory
    );
    this.registerEditorForActions(editor);
    this.setState({ editor });

    if (!editor.editorView.hasFocus()) {
      editor.editorView.focus();
    }

    moveCursorToTheEnd(editor.editorView);
  };

  componentWillUnmount() {
    this.unregisterEditorFromActions();
  }

  registerEditorForActions(editor) {
    if (this.context && this.context.editorActions) {
      this.context.editorActions._privateRegisterEditor(
        editor.editorView,
        editor.contentTransformer
      );
    }
  }

  unregisterEditorFromActions() {
    if (this.context && this.context.editorActions) {
      this.context.editorActions._privateUnregisterEditor();
    }
  }

  render() {
    const { editor } = this.state;
    const { editorView, contentComponents, eventDispatcher } = editor;

    return (
{'fraction_non_alphanumeric': 0.1221887981664518, 'fraction_numerical': 0.014038103423578283, 'mean_word_length': 3.1167452830188678, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 10, 'https://': 0, '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': '6790851', 'n_tokens_mistral': 2280, 'n_tokens_neox': 2152, 'n_words': 423}