@sn8/vuex-undo-redo

A Vue.js plugin to undo/redo mutations
2,847
Install
npm install @sn8/vuex-undo-redo
Latest Version:1.0.3
Vue:2
License:MIT
Last Updated:May 21, 2019
Links: GitHub  ·  npm
Maintainer: sn8

vuex-undo-redo

A Vue.js plugin that allows you to undo or redo a mutation. Based on anthonygore/vuex-undo-redo, but using another idea for undo or redo a mutations.

Installation

yarn add vuex-undo-redo

Browser

<script type="text/javascript" src="node_modules/vuex-undo-redo/dist/vuex-undo-redo.min.js"></script>

Usage

Since it's a plugin, use it like:

import VuexUndoRedo from 'vuex-undo-redo';

Vue.use(VuexUndoRedo, {
  rules: [
    {
      from: 'INSERT',
      to: `DELETE`,
    },
    {
      from: 'DELETE',
      to: 'INSERT',
    },
    {
      from: 'UPDATE',
      to: 'UPDATE',
      mapPayload: { oldValues: 'newValues', newValues: 'oldValues' },
    },
  ],
});

You must, of course, have the Vuex plugin installed as well, and it must be intalled before this plugin.

new Vuex.Store({
  state: {
    elements: [],
  },
  mutations: {
    /* "element" should be non-reactive */
    INSERT(state, { index, element }) {
      state.elements.splice(index, 0, element);   
    },
    /* "element" should be non-reactive */
    DELETE(state, { index, element }) {
      state.elements.splice(index, 1);
    },
    /* "newValues" and "oldValues" should be non-reactive */
    UPDATE(state, { index, newValues, oldValues }) {
      const element = state.elements[index];
      for (const [key, value] of Object.entries(newValues)) {
        element[key] = value;
      }
    },
  },
});

API

Computed properties

canUndo a boolean which tells you if the state is undo-able

canRedo a boolean which tells you if the state is redo-able

Methods

undo undoes the last mutation

redo redoes the last mutation

resetUndoRedo resets the all history

Version History

Version Released Vue License
1.0.3 ^2.5.17 MIT
1.0.2 ^2.5.17 MIT
1.0.1 ^2.5.17 MIT
1.0.0 ^2.5.17 MIT