npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

undo-redo-manager

v1.0.1

Published

A simple manager for undo and redo. High customization, can cover update or diff update both.

Downloads

12

Readme

Undo-redo manager

A simple manager for undo and redo. High customization, can cover update or diff update both.

Installation

npm install --save undo-redo-manager

Reference

constructor

  • @param {Function} rollbackFn
    • will execute when call undo or redo method. pass the stepDetail and isLastRollback for the function(require return a stepDetail to push on undo/redo stack)
  • @param {Number} maxStep
    • max stored undoList and redoList

methods

push

  • pushes a stepDetail on the undo stack, and clears the redo stack
  • @param {*} stepDetail
    • the stepDetail to push on last of the undo stack

undo

  • call once or more rollbackFn function, and push rollbackFn returns to redo stack
  • @param {Number} stepNum
    • number of undo times

redo

  • call once or more rollbackFn function, and push rollbackFn returns to undo stack
  • @param {Number} stepNum
    • number of redo times

clear

  • clear undo stack and redo stack

properties

canUndo

  • return {Boolean} True if have undo stack

canRedo

  • return {Boolean} True if have redo stack

undoStack

  • return undo stack

redoStack

  • return redo stack

Examples

- cover update

let data = 1
let manager = new Manager((detail, isLast) => {
  console.log(isLast)
  let tmp = data
  data = detail
  return tmp
})

data = 2
manager.push(1)
data = 3
manager.push(2)

console.log(manager.undoStack) //[1,2]
manager.undo(1) //true
console.log(data) //2
console.log(manager.redoStack) //[3]

console.log(manager.undoStack) //[1]
manager.undo(2) //false true
console.log(data) //1
console.log(manager.redoStack) //[3,2]

manager.redo(1) // true
console.log(data) //2
console.log(manager.redoStack) //[3]

manager.push(data)
data = 99
console.log(manager.undoStack) //[1,2]
console.log(manager.redoStack) //[]

- diff update

let data = [{id:1, value:1}]
const typeReverse = {
  add:'del',
  del:'add',
  update:'update'
}
let manager = new Manager(detail => {
  let index = data.findIndex(i => i.id === detail.id)
  let oldValue

  switch(detail.type){
    case 'add':
      oldValue = data.splice(index,1).value
      break

    case 'update':
      oldValue = data[index].value
      data[index].value = detail.oldValue
      break

    case 'del':
      data.push({id:detail.id,value:detail.oldValue})
  }

  return {id:detail.id, type: typeReverse[detail.type], oldValue}
})

let obj = {id:2,value:2}
data.push(obj)
manager.push({type:'add',id:2})
obj.value = 'WTF'
manager.push({type:'update', id:2, oldValue:2})
console.log(manager.undoStack) //[{type:'add',id:2}, {type:'update', id:2, oldValue:2}]
manager.undo()
console.log(data) //[{id:1, value:1}, {id:2, value:2}]
console.log(manager.redoStack) //[{type:'update', id:2, oldValue:'WTF'}]
manager.redo()
console.log(data) //[{id:1, value:1}, {id:2, value:'WTF'}]

Tips

storage of non-basic type variables can use JSON.stringify and JSON.parse method