const CELL_SIDE = 62;

var selectedCell;

var multiSelect = false;
var rootMode = false;
var divisionMode = false;
var fractionMode = false;
var fractionUp = true;
var fractionDown = false;

var selectedCells = [];

var columnsNumber = 0;
var rowsNumber = 0;


function drawGrid(w, h) {

  var grid = $('#grid');
  var grid_width, grid_height;

  grid.empty();

  if (w === 0) {
    grid_width = grid.width();
  } else {
    grid_width = w;
  }
  if (h === 0) {
    grid_height = grid.height();
  } else {
    grid_height = h;
  }

  var cell = "<td class='cell'></td>";


  rowsNumber = Math.floor(grid_height / CELL_SIDE);
  columnsNumber = Math.floor(grid_width / CELL_SIDE);
  for (let i = 0; i < rowsNumber; i++) {
    for (let j = 0; j < columnsNumber; j++) {
      grid.append(cell);
    }
  }
  addCellClickBehavior(); // flag current cell selected

}

function flagCell(cell) {

  if (multiSelect) {
    if (selectedCells.length < 1 && selectedCell) {
      selectedCells.push(selectedCell);
    }
    if (cell.hasClass("cell-selected")) {
      cell.removeClass("cell-selected");
      selectedCells.splice(selectedCells.indexOf(cell), 1);
    } else {
      cell.addClass("cell-selected");
      selectedCells.push(cell);
    }
  } else {
    cell.addClass("cell-selected");
    if (selectedCell && cell !== selectedCell) {
      selectedCell.removeClass("cell-selected");
    }
    selectedCell = cell;
  }
}

function getPreviousFilledCell() {
  if (selectedCell) {
    return selectedCell.prevAll("td.filled-cell:eq(0)");
  }
}

function getCurrentCell() {
  if (selectedCell) {
    return selectedCell;
  }
}

function getNextCell() {
  return selectedCell.next("td.cell");
}

function isIndexingMode(previousFilledCell) {
  if (previousFilledCell) {
    return previousFilledCell.data("indexingUpRightApplied") ||
      previousFilledCell.data("indexingUpLeftApplied") ||
      previousFilledCell.data("indexingDownApplied") ||
      (previousFilledCell.data("rootIndexingApplied") && rootMode);
  }
  return false;
}
@@ -1610,32 +1610,78 @@
 tedCell;%0A  %7D%0A%7D%0A%0A
+// TODO : delete and replace with getNextCell%0A
 function getNext
@@ -2072,8 +2072,486 @@
 false;%0A%7D
+%0A%0Avar Selection = function () %7B%0A  let selected = %5B%5D;%0A%0A  function reinitParams() %7B%0A    selected = %5B%5D;%0A  %7D%0A%0A  function nextCell(target) %7B%0A    return target.next(%22td.cell%22);%0A  %7D%0A%0A  function prepareSelection() %7B%0A    reinitParams();%0A    if (multiSelect)%0A      selected = selectedCells;%0A    else%0A      selected.push(selectedCell);%0A  %7D%0A%0A  function getSelected() %7B%0A    prepareSelection();%0A    return selected;%0A  %7D%0A%0A  return %7B%0A    getSelected: getSelected,%0A    getNext: nextCell%0A  %7D%0A%7D();

Add a selection util function