<commit_before>import * as vl from 'vega-lite';

import { UPDATE_VEGA_SPEC, UPDATE_VEGA_LITE_SPEC, PARSE_SPEC, TOGGLE_AUTO_PARSE, CYCLE_RENDERER, SET_VEGA_EXAMPLE, SET_VEGA_LITE_EXAMPLE,
  SHOW_COMPILED_VEGA_SPEC, SET_GIST_VEGA_SPEC, SET_GIST_VEGA_LITE_SPEC, SET_MODE, SHOW_ERROR_PANE, LOG_ERROR } from '../../actions/editor';
import { MODES, RENDERERS } from '../../constants';
import {validateVegaLite} from './validate';

const JSON3 = require('../../../lib/json3-compactstringify');

class LocalLogger {
  warns = [];
  infos = [];
  debugs = [];

  level() {
    return this;
  }

  warn(...args) {
    this.warns.push(...args);
    return this;
  }

  info(...args) {
    this.infos.push(...args);
    return this;
  }

  debug(...args) {
    this.debugs.push(...args);
    return this;
  }
}

const logger = new LocalLogger();

export default (state = {
  editorString: '{}',
  vegaSpec: {},
  vegaLiteSpec: null,
  selectedExample: null,
  mode: MODES.Vega,
  renderer: RENDERERS.Canvas,
  autoParse: true,
  parse: false,
  compiledVegaSpec: false,
  gist: null,
  error: null,
  errorPane: false,
  warningsLogger: logger
}, action) => {
  let spec, vegaSpec;
  switch (action.type) {
    case SET_MODE:
      return Object.assign({}, state, {
        mode: action.mode,
        vegaSpec: {},
        vegaLiteSpec: {},
        selectedExample: null,
        editorString: '{}',
        compiledVegaSpec: false,
        gist: null,
        parse: false,
        warningsLogger: logger
      });
    case PARSE_SPEC:
      return Object.assign({}, state, {
        parse: action.parse
      });
    case UPDATE_VEGA_SPEC:
      try {
        spec = JSON.parse(action.spec);
      } catch (e) {
        console.warn('Error parsing json string');
        return Object.assign({}, state, {
          error: e.message,
          editorString: JSON3.stringify(spec, null, 2, 60),
          warningsLogger: logger
        });
      }
      return Object.assign({}, state, {
        vegaSpec: spec,
        mode: MODES.Vega,
        editorString: action.spec,
        error: null,
        warningsLogger: logger
      });
    case SET_VEGA_EXAMPLE:
      try {
        spec = JSON.parse(action.spec);
      } catch (e) {
        console.warn('Error parsing json string');
        return Object.assign({}, state, {
          warningsLogger: logger,
          error: e.message,
          editorString: JSON3.stringify(spec, null, 2, 60)
        });
      }
      return Object.assign({}, state, {
        vegaSpec: spec,
        mode: MODES.Vega,
        editorString: action.spec,
        selectedExample: action.example,
        error: null,
        warningsLogger: logger,
      });
    case SET_VEGA_LITE_EXAMPLE:
      try {
        spec = JSON.parse(action.spec);
        vegaSpec = spec;
        if (action.spec !== '{}') {
          vegaSpec = vl.compile(spec).spec;
        }
      } catch (e) {
        console.warn(e);
        return Object.assign({}, state, {
          warningsLogger: logger,
          error: e.message,
          editorString: JSON3.stringify(spec, null, 2, 60)
        });
      }
      return Object.assign({}, state, {
        vegaLiteSpec: spec,
        vegaSpec: vegaSpec,
        mode: MODES.VegaLite,
        editorString: action.spec,
        selectedExample: action.example,
        error: null,
        warningsLogger: logger
      });
    case UPDATE_VEGA_LITE_SPEC: {
      const currLogger = new LocalLogger();
      try {
        spec = JSON.parse(action.spec);
        validateVegaLite(spec, currLogger);
        vegaSpec = vl.compile(spec, currLogger).spec;
      } catch (e) {
        console.warn(e);
        return Object.assign({}, state, {
          error: e.message,
          editorString: JSON3.stringify(spec, null, 2, 60),
          warningsLogger: currLogger
        });
      }
      return Object.assign({}, state, {
        vegaLiteSpec: spec,
        vegaSpec: vegaSpec,
        mode: MODES.VegaLite,
        editorString: action.spec,
        error: null,
        warningsLogger: currLogger
      });
    }
    case SET_GIST_VEGA_SPEC:
      try {
        spec = JSON.parse(action.spec);
      } catch(e) {
        console.warn('Error parsing json string');
        return Object.assign({}, state, {
          warningsLogger: logger,
          error: e.message,
          editorString: JSON3.stringify(spec, null, 2, 60)
        });
      }
      return Object.assign({}, state, {
        vegaSpec: spec,
        mode: MODES.Vega,
        editorString: action.spec,
        gist: action.gist,
        error: null,
        warningsLogger: logger
      });
    case SET_GIST_VEGA_LITE_SPEC: {
      const currLogger = new LocalLogger();
      try {
        spec = JSON.parse(action.spec);
        vegaSpec = vl.compile(spec, currLogger).spec;
      } catch(e) {
        console.warn(e);
        return Object.assign({}, state, {
          warningsLogger: currLogger,
          error: e.message,
          editorString: JSON3.stringify(spec, null, 2, 60)
        });
      }
      return Object.assign({}, state, {
        vegaLiteSpec: spec,
        vegaSpec: vegaSpec,
        mode: MODES.VegaLite,
        editorString: action.spec,
        gist: action.gist,
        error: null,
        warningsLogger: currLogger
      });
    }
    case TOGGLE_AUTO_PARSE:
      return Object.assign({}, state, {
        autoParse: !state.autoParse,
        parse: !state.autoParse<commit_msg>fixed spec disappears bug (#178)<commit_after>import * as vl from 'vega-lite';

import { UPDATE_VEGA_SPEC, UPDATE_VEGA_LITE_SPEC, PARSE_SPEC, TOGGLE_AUTO_PARSE, CYCLE_RENDERER, SET_VEGA_EXAMPLE, SET_VEGA_LITE_EXAMPLE,
  SHOW_COMPILED_VEGA_SPEC, SET_GIST_VEGA_SPEC, SET_GIST_VEGA_LITE_SPEC, SET_MODE, SHOW_ERROR_PANE, LOG_ERROR } from '../../actions/editor';
import { MODES, RENDERERS } from '../../constants';
import {validateVegaLite} from './validate';

const JSON3 = require('../../../lib/json3-compactstringify');

class LocalLogger {
  warns = [];
  infos = [];
  debugs = [];

  level() {
    return this;
  }

  warn(...args) {
    this.warns.push(...args);
    return this;
  }

  info(...args) {
    this.infos.push(...args);
    return this;
  }

  debug(...args) {
    this.debugs.push(...args);
    return this;
  }
}

const logger = new LocalLogger();

export default (state = {
  editorString: '{}',
  vegaSpec: {},
  vegaLiteSpec: null,
  selectedExample: null,
  mode: MODES.Vega,
  renderer: RENDERERS.Canvas,
  autoParse: true,
  parse: false,
  compiledVegaSpec: false,
  gist: null,
  error: null,
  errorPane: false,
  warningsLogger: logger
}, action) => {
  let spec, vegaSpec;
  switch (action.type) {
    case SET_MODE:
      return Object.assign({}, state, {
        mode: action.mode,
        vegaSpec: {},
        vegaLiteSpec: {},
        selectedExample: null,
        editorString: '{}',
        compiledVegaSpec: false,
        gist: null,
        parse: false,
        warningsLogger: logger
      });
    case PARSE_SPEC:
      return Object.assign({}, state, {
        parse: action.parse
      });
    case UPDATE_VEGA_SPEC:
      try {
        spec = JSON.parse(action.spec);
      } catch (e) {
        console.warn('Error parsing json string');
        return Object.assign({}, state, {
          error: e.message,
          editorString: action.spec,
          warningsLogger: logger
        });
      }
      return Object.assign({}, state, {
        vegaSpec: spec,
        mode: MODES.Vega,
        editorString: action.spec,
        error: null,
        warningsLogger: logger
      });
    case SET_VEGA_EXAMPLE:
      try {
        spec = JSON.parse(action.spec);
      } catch (e) {
        console.warn('Error parsing json string');
        return Object.assign({}, state, {
          warningsLogger: logger,
          error: e.message,
          editorString: action.spec
        });
      }
      return Object.assign({}, state, {
        vegaSpec: spec,
        mode: MODES.Vega,
        editorString: action.spec,
        selectedExample: action.example,
        error: null,
        warningsLogger: logger,
      });
    case SET_VEGA_LITE_EXAMPLE:
      try {
        spec = JSON.parse(action.spec);
        vegaSpec = spec;
        if (action.spec !== '{}') {
          vegaSpec = vl.compile(spec).spec;
        }
      } catch (e) {
        console.warn(e);
        return Object.assign({}, state, {
          warningsLogger: logger,
          error: e.message,
          editorString: action.spec
        });
      }
      return Object.assign({}, state, {
        vegaLiteSpec: spec,
        vegaSpec: vegaSpec,
        mode: MODES.VegaLite,
        editorString: action.spec,
        selectedExample: action.example,
        error: null,
        warningsLogger: logger
      });
    case UPDATE_VEGA_LITE_SPEC: {
      const currLogger = new LocalLogger();
      try {
        spec = JSON.parse(action.spec);
        validateVegaLite(spec, currLogger);
        vegaSpec = vl.compile(spec, currLogger).spec;
      } catch (e) {
        console.warn(e);
        return Object.assign({}, state, {
          error: e.message,
          editorString: action.spec,
          warningsLogger: currLogger
        });
      }
      return Object.assign({}, state, {
        vegaLiteSpec: spec,
        vegaSpec: vegaSpec,
        mode: MODES.VegaLite,
        editorString: action.spec,
        error: null,
        warningsLogger: currLogger
      });
    }
    case SET_GIST_VEGA_SPEC:
      try {
        spec = JSON.parse(action.spec);
      } catch(e) {
        console.warn('Error parsing json string');
        return Object.assign({}, state, {
          warningsLogger: logger,
          error: e.message,
          editorString: action.spec
        });
      }
      return Object.assign({}, state, {
        vegaSpec: spec,
        mode: MODES.Vega,
        editorString: action.spec,
        gist: action.gist,
        error: null,
        warningsLogger: logger
      });
    case SET_GIST_VEGA_LITE_SPEC: {
      const currLogger = new LocalLogger();
      try {
        spec = JSON.parse(action.spec);
        vegaSpec = vl.compile(spec, currLogger).spec;
      } catch(e) {
        console.warn(e);
        return Object.assign({}, state, {
          warningsLogger: currLogger,
          error: e.message,
          editorString: action.spec
        });
      }
      return Object.assign({}, state, {
        vegaLiteSpec: spec,
        vegaSpec: vegaSpec,
        mode: MODES.VegaLite,
        editorString: action.spec,
        gist: action.gist,
        error: null,
        warningsLogger: currLogger
      });
    }
    case TOGGLE_AUTO_PARSE:
      return Object.assign({}, state, {
        autoParse: !state.autoParse,
        parse: !state.autoParse
{'fraction_non_alphanumeric': 0.1251277287505806, 'fraction_numerical': 0.002879702740362285, 'mean_word_length': 2.038667795653401, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, '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': '1520887', 'n_tokens_mistral': 3439, 'n_tokens_neox': 3417, 'n_words': 719}