undo-redo-proxy

⏳ Effortless timetravel - undo/redo proxy wrapper for arrays

Usage no npm install needed!

<script type="module">
  import undoRedoProxy from 'https://cdn.skypack.dev/undo-redo-proxy';
</script>

README

undo-redo-proxy

Build Status NPM Version License Dependency Status devDependency Status

⏳ Effortless timetravel - undo/redo proxy wrapper for arrays

Installation

npm install undo-redo-proxy

Usage

import trackHistory from 'undo-redo-proxy'
let array = ['my', 'array']
// wraps your array, returns proxy which behaves like a normal array
array = trackHistory(array)
console.log(array) // ['my', 'array']
array.push('foo')  // ['my', 'array', 'foo']
array[2] = 'bar'   // ['my', 'array', 'bar']
array.undo()       // ['my', 'array', 'foo']
array.undo()       // ['my', 'array']
array.shift()      // ['array']
array.redo()       // ['my', 'array']

Can be used in browser as well

import trackHistory from './node_modules/undo-redo-proxy/index.mjs'
let users = trackHistory([])

Only keeps track of the array and its items and positions.

users.push({name: 'Mike'})
users.push({name: 'Lucy'})
users.shift()
console.log(users) // [{name: 'Lucy'}]
users.undo()
console.log(users) // [{name: 'Mike'}, {name: 'Lucy'}]

Does not track content of array items.

users.push({name: 'Mike'}) // can be undone
users[0].name = 'Lucy' // cannot be undone
console.log(users) // [{name: 'Lucy'}]
users.undo()
console.log(users) // [{name: 'Lucy'}]

Licence

MIT, Mike Kovařík, Mutiny.cz