All files / src reducers.js

20.27% Statements 15/74
14.71% Branches 5/34
66.67% Functions 2/3
20.55% Lines 15/73

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 1341x       1x 1x           1x 1x       1x           1x     1x                             1x       1x   1x                                                                                                                                                                 1x 1x     1x 1x  
import {topmostUndoable} from './utils';
//import SHARED from './shared'; //used only in debug statements
 
function loggerDebug(action, ast) { // in lieu of logger.debug
  Eif (!window.reducerActivities) {
    window.reducerActivities = [];
  }
  // Shallow-clone the action, removing the AST.
  // Then replace the AST with the source code.
  // We'll reconstruct it when replaying the log.
  // Replace focusId with nid
  let activity = {...action, ast: false};
  Iif(action.type == "SET_AST") {
    activity.code = ast.toString();
    delete activity.ast;
  }
  Iif(action.type == "SET_FOCUS") {
    if (ast && ast.getNodeById(action.focusId)) {
      activity.nid = ast.getNodeById(action.focusId).nid;
      delete activity.focusId;
    }
  }
  window.reducerActivities.push(activity);
}
 
const initialState = {
  selections: [],
  editable: {},
  ast: null,
  focusId: null,
  collapsedList: [],
  markedMap: new Map(),
  undoableAction: null,
  actionFocus: false,
  errorId: '',
  cur: null,
  quarantine: null,
  announcer: null,
};
 
export const reducer = (
  state = initialState,
  action) => {
  //console.log(action);
  let result = null;
  let tU;
  switch (action.type) {
  case 'SET_FOCUS':
    result = {...state, focusId: action.focusId};
    break;
  case 'SET_AST':
    result = {...state, ast: action.ast, collapsedList: state.collapsedList.filter(action.ast.getNodeById)};
    break;
  case 'SET_SELECTIONS':
    result = {...state, selections: action.selections};
    break;
  case 'SET_EDITABLE':
    result = {...state, editable: {...state.editable, [action.id]: action.bool}};
    break;
  case 'SET_ERROR_ID':
    result = {...state, errorId: action.errorId};
    break;
  case 'COLLAPSE':
    result = {...state, collapsedList: state.collapsedList.concat([action.id])};
    break;
  case 'UNCOLLAPSE':
    result = {...state, collapsedList: state.collapsedList.filter(e => e !== action.id)};
    break;
  case 'COLLAPSE_ALL':
    result = {...state, collapsedList: [...state.ast.nodeIdMap.keys()]};
    break;
  case 'UNCOLLAPSE_ALL':
    result = {...state, collapsedList: []};
    break;
  case 'SET_CURSOR':
    result = {...state, cur: action.cur};
    break;
  case 'DISABLE_QUARANTINE':
    result = {...state, quarantine: null};
    break;
  case 'CHANGE_QUARANTINE':
    result = {...state, quarantine: [state.quarantine[0], state.quarantine[1], action.text]};
    break;
  case 'SET_QUARANTINE':
    result = {...state, quarantine: [action.start, action.end, action.text]};
    break;
  case 'SET_ANNOUNCER':
    result = {...state, announcer: action.announcer};
    break;
  case 'ADD_MARK':
    result = {...state, markedMap: state.markedMap.set(action.id, action.mark)};
    break;
  case 'CLEAR_MARK':
    state.markedMap.delete(action.id);
    result = {...state};
    break;
 
  case 'DO':
    //console.log('XXX reducers:100 state.focusId=', state.focusId, 'action.focusId=', action.focusId);
    if (state.focusId !== action.focusId) {
      //console.log('XXX reducers:102 updating focusId in state');
      result = {...state, focusId: action.focusId};
    } else {
      result = {...state};
    }
    break;
  case 'UNDO':
    tU = topmostUndoable('redo', state);
    tU.undoableAction = state.undoableAction;
    tU.actionFocus = state.actionFocus;
    state.undoableAction = null;
    state.actionFocus = null;
    result = {...state};
    break;
  case 'REDO':
    tU = topmostUndoable('undo', state);
    tU.undoableAction = state.undoableAction;
    tU.actionFocus = state.actionFocus;
    state.undoableAction = null;
    state.actionFocus = null;
    result = {...state};
    break;
 
  case 'RESET_STORE_FOR_TESTING':
    result =  initialState;
    break;
  default:
    console.log('unprocessed action type=', action.type);
    result =  state;
  }
 
  loggerDebug(action, result.ast);
  return result;
};