JSFiddle - React, Tailwind, and code Playground

by lchau

HTML

<div>
    <button id="reset">Reset</button>
  </div>
  <ul id="breadcrumb"></ul>
  <div id="container"></div>

CSS

ul#breadcrumb {
    height: 50px;
  }
  div#container div {
    color: steelblue;
    background-color: lightblue;
    border: 2px dashed steelblue;
    display: inline-block;
    font-family: sans-serif;
    font-weight: bold;
    line-height: 50px;
    height: 50px;
    width: 50px;
    margin: 10px;
    text-align: center;
    cursor: pointer;
  }
  ul#breadcrumb,
  ul#breadcrumb li {
    list-style-type: none;
    padding: 0;
  }
  ul#breadcrumb {
    margin: 10px;
  }
  ul#breadcrumb span {
    border-bottom: 1px solid blue;
  }
  ul#breadcrumb li {
    color: #555;
    display: inline-block;
    margin-right: 10px;
    cursor: pointer;
    /*background-color: lightyellow;
    border: 1px solid black;*/
    margin: 3px 5px;
    font-size: 24px;
  }
  ul#breadcrumb li:last-child {
    color: black;
    font-weight: bold;
  }
  ul#breadcrumb li+li:before {
    content: " > ";
    color: red;
    font-weight: normal;
  }
  body {
    -webkit-touch-callout: none;
    -webkit-user-select: none;
    -khtml-user-select: none;
    -moz-user-select: none;
    -ms-user-select: none;
    user-select: none;
  }

JavaScript

var Command = function(execute, undo) {
  if (!(this instanceof Command)) {
    return new Command(execute, undo);
  }
  if (!_.isFunction(execute)) {
    throw {
      name: "Invalid function",
      message: "Execute must be a function"
    };
  }

  // TODO: make optional
  if (!_.isFunction(undo)) {
    throw {
      name: "Invalid function",
      message: "Undo must be a function"
    };
  }
  this.execute = execute;
  this.undo = undo;
};

var Navigator = function() {
  var commands = [];
  var redo = [];
  var result = [];

  var clear = function clear(collection) {
    if (_.isArray(collection)) {
      while (!_.isEmpty(collection)) {
        collection.pop();
      }
    }
  };

  var getCount = function getCount(count, collection) {
    count = +count || 1;
    if (_.isEmpty(collection) || count < 1 || isNaN(count) || !isFinite(count)) {
      return 0;
    }
    return Math.min(count, collection.length);
  };

  return {
    execute: function(command) {
      if (!(command instanceof Command)) {
        return;
      }
      result = command.execute(result);
      commands.push(command);
      clear(redo);
      navigationJump = false;
    },

    createBreadcrumb: function() {
      var container = document.getElementById("breadcrumb");
      while (container.lastChild) {
        container.removeChild(container.lastChild);
      }

      var self = this;
      _.each(result, function(e, index) {
        var li = document.createElement("li");
        var span = document.createElement("span");
        span.textContent = e.someObject;
        li.appendChild(span);
        span.onclick = function() {
          var x = container.childElementCount - index - 1;
          if (x < 1) {
            return;
          }
          self.undo(x);
          navigationJump = true;
          window.history.go(-x);
        };
        container.appendChild(li);
      });
    },

    getResult: function() {
      // extract as renderer
      var map = _.map(result,...