constructor (editor) {
    this.editor = editor
    this.history = []
    this.index = -1

    this.clear()

    // helper function to find a Node from a path
    function findNode (path) {
      return editor.node.findNodeByInternalPath(path)
    }

    // map with all supported actions
    this.actions = {
      editField: {
        undo: function (params) {
          const parentNode = findNode(params.parentPath)
          const node = parentNode.childs[params.index]
          node.updateField(params.oldValue)
        },
        redo: function (params) {
          const parentNode = findNode(params.parentPath)
          const node = parentNode.childs[params.index]
          node.updateField(params.newValue)
        }
      },
      editValue: {
        undo: function (params) {
          findNode(params.path).updateValue(params.oldValue)
        },
        redo: function (params) {
          findNode(params.path).updateValue(params.newValue)
        }
      },
      changeType: {
        undo: function (params) {
          findNode(params.path).changeType(params.oldType)
        },
        redo: function (params) {
          findNode(params.path).changeType(params.newType)
        }
      },

      appendNodes: {
        undo: function (params) {
          const parentNode = findNode(params.parentPath)
          params.paths.map(findNode).forEach(node => {
            parentNode.removeChild(node)
          })
        },
        redo: function (params) {
          const parentNode = findNode(params.parentPath)
          params.nodes.forEach(node => {
            parentNode.appendChild(node)
          })
        }
      },
      insertBeforeNodes: {
        undo: function (params) {
          const parentNode = findNode(params.parentPath)
          params.paths.map(findNode).forEach(node => {
            parentNode.removeChild(node)
          })
        },
        redo: function (params) {
          const parentNode = findNode(params.parentPath)
          const beforeNode = findNode(params.beforePath)
          params.nodes.forEach(node => {
            parentNode.insertBefore(node, beforeNode)
          })
        }
      },
      insertAfterNodes: {
        undo: function (params) {
          const parentNode = findNode(params.parentPath)
          params.paths.map(findNode).forEach(node => {
            parentNode.removeChild(node)
          })
        },
        redo: function (params) {
          const parentNode = findNode(params.parentPath)
          let afterNode = findNode(params.afterPath)
          params.nodes.forEach(node => {
            parentNode.insertAfter(node, afterNode)
            afterNode = node
          })
        }
      },
      removeNodes: {
        undo: function (params) {
          const parentNode = findNode(params.parentPath)
          const beforeNode = parentNode.childs[params.index] || parentNode.append
          params.nodes.forEach(node => {
            parentNode.insertBefore(node, beforeNode)
          })
        },
        redo: function (params) {
          const parentNode = findNode(params.parentPath)
          params.paths.map(findNode).forEach(node => {
            parentNode.removeChild(node)
          })
        }
      },
      duplicateNodes: {
        undo: function (params) {
          const parentNode = findNode(params.parentPath)
          params.clonePaths.map(findNode).forEach(node => {
            parentNode.removeChild(node)
          })
        },
        redo: function (params) {
          const parentNode = findNode(params.parentPath)
          let afterNode = findNode(params.afterPath)
          const nodes = params.paths.map(findNode)
          nodes.forEach(node => {
            const clone = node.clone()
            if (parentNode.type === 'object') {
              const existingFieldNames = parentNode.getFieldNames()
              clone.field = findUniqueName(node.field, existingFieldNames)
            }
            parentNode.insertAfter(clone, afterNode)
            afterNode = clone
          })
        }
      },
      moveNodes: {
        undo: function (params) {
          const oldParentNode = findNode(params.oldParentPath)
          const newParentNode = findNode(params.newParentPath)
          const oldBeforeNode = oldParentNode.childs[params.oldIndex] || oldParentNode.append

          // first copy the nodes, then move them
          const nodes = newParentNode.childs.slice(params.newIndex, params.newIndex + params.count)

          nodes.forEach((node, index) => {
            node.field = params.fieldNames[index]
            oldParentNode.moveBefore(node, oldBeforeNode)
          })

          // This is a hack to work around an issue that we don't know tha original
          // path of the new parent after dragging, as the node is already moved at that time.
          if (params.newParentPathRedo === null) {
            params.newParentPathRedo = newParentNode.getInternalPath()
          }
        },
        redo: function (params) {
          const oldParentNode = findNode(params.oldParentPathRedo)
          const newParentNode = findNode(params.newParentPathRedo)
          const newBeforeNode = newParentNode.childs[params.newIndexRedo] || newParentNode.append

          // first copy the nodes, then move them
          const nodes = oldParentNode.childs.slice(params.oldIndexRedo, params.oldIndexRedo + params.count)

          nodes.forEach((node, index) => {
            node.field = params.fieldNames[index]
            newParentNode.moveBefore(node, newBeforeNode)
          })
        }
      },

      sort: {
        undo: function (params) {
          const node = findNode(params.path)
          node.hideChilds()
          node.childs = params.oldChilds
          node.updateDom({ updateIndexes: true })
          node.showChilds()
        },
        redo: function (params) {
          const node = findNode(params.path)
          node.hideChilds()
          node.childs = params.newChilds
          node.updateDom({ updateIndexes: true })
          node.showChilds()
        }
      },

      transform: {
        undo: function (params) {
          findNode(params.path).setInternalValue(params.oldValue)

          // TODO: would be nice to restore the state of the node and childs
        },
        redo: function (params) {
          findNode(params.path).setInternalValue(params.newValue)

          // TODO: would be nice to restore the state of the node and childs
        }
      }

      // TODO: restore the original caret position and selection with each undo
      // TODO: implement history for actions "expand", "collapse", "scroll", "setDocument"
    }
  }
@constructor History
Store action history, enables undo and redo
@param {JSONEditor} editor