<commit_before>/* @flow */
import {EditorState} from 'draft-js';
import stateToHTML from './stateToHTML';
import stateFromHTML from './stateFromHTML';
import stateToMarkdown from './stateToMarkdown';
import stateFromMarkdown from './stateFromMarkdown';

type StringMap = {[key: string]: string};

export default class EditorValue {
  _editorState: ?EditorState;
  _cache: StringMap;

  constructor(editorState: ?EditorState) {
    this._editorState = editorState;
    this._cache = {};
  }

  getEditorState(): EditorState {
    return this._editorState || EditorState.createEmpty();
  }

  setEditorState(editorState: EditorState): EditorValue {
    if (this._editorState === editorState) {
      return this;
    }
    return new EditorValue(editorState);
  }

  toString(format: string): string {
    let fromCache = this._cache[format];
    if (fromCache != null) {
      return fromCache;
    }
    return (this._cache[format] = toString(this.getEditorState(), format));
  }

  static createEmpty(): EditorValue {
    return new EditorValue();
  }

  static createFromString(source: string, format: string): EditorValue {
    let editorState = fromString(source, format);
    return new EditorValue(editorState);
  }

  static createFromState(editorState: EditorState): EditorValue {
    return new EditorValue(editorState);
  }
}

function toString(editorState, format) {
  let contentState = editorState.getCurrentContent();
  switch (format) {
    case 'html': {
      return stateToHTML(contentState);
    }
    case 'markdown': {
      return stateToMarkdown(contentState);
    }
    default: {
      throw new Error('Format not supported: ' + format);
    }
  }
}

function fromString(source, format) {
  switch (format) {
    case 'html': {
      return stateFromHTML(source);
    }
    case 'markdown': {
      return stateFromMarkdown(source);
    }
    default: {
      throw new Error('Format not supported: ' + format);
    }
  }
}
<commit_msg>after setting editorState from source don't re-generate source upon next toString<commit_after>/* @flow */
import {EditorState} from 'draft-js';
import stateToHTML from './stateToHTML';
import stateFromHTML from './stateFromHTML';
import stateToMarkdown from './stateToMarkdown';
import stateFromMarkdown from './stateFromMarkdown';

type StringMap = {[key: string]: string};

export default class EditorValue {
  _editorState: ?EditorState;
  _cache: StringMap;

  constructor(editorState: ?EditorState) {
    this._editorState = editorState;
    this._cache = {};
  }

  getEditorState(): EditorState {
    return this._editorState || EditorState.createEmpty();
  }

  setEditorState(editorState: EditorState): EditorValue {
    if (this._editorState === editorState) {
      return this;
    }
    return new EditorValue(editorState);
  }

  toString(format: string): string {
    let fromCache = this._cache[format];
    if (fromCache != null) {
      return fromCache;
    }
    return (this._cache[format] = toString(this.getEditorState(), format));
  }

  static createEmpty(): EditorValue {
    return new EditorValue();
  }

  static createFromString(source: string, format: string): EditorValue {
    let editorState = fromString(source, format);
    let value = new EditorValue(editorState);
    value._cache[format] = source;
    return value;
  }

  static createFromState(editorState: EditorState): EditorValue {
    return new EditorValue(editorState);
  }
}

function toString(editorState, format) {
  let contentState = editorState.getCurrentContent();
  switch (format) {
    case 'html': {
      return stateToHTML(contentState);
    }
    case 'markdown': {
      return stateToMarkdown(contentState);
    }
    default: {
      throw new Error('Format not supported: ' + format);
    }
  }
}

function fromString(source, format) {
  switch (format) {
    case 'html': {
      return stateFromHTML(source);
    }
    case 'markdown': {
      return stateFromMarkdown(source);
    }
    default: {
      throw new Error('Format not supported: ' + format);
    }
  }
}

{'fraction_non_alphanumeric': 0.11684782608695653, 'fraction_numerical': 0.0, 'mean_word_length': 3.654022988505747, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '5966390', 'n_tokens_mistral': 1174, 'n_tokens_neox': 1159, 'n_words': 295}