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

@reatom/undo

v3.4.2

Published

Reatom for undo

Downloads

314

Readme

This package have a set of methods to handle a state history for an atom or a set of atoms. Useful for complex forms, WYSIWYG and so on.

Installation

npm i @reatom/undo

Usage

All methods reuse WithUndo interface which includes the following atoms and actions.

  • jump action allows you to navigate entire history by passed index.
  • undo action is a shortcut to jump(ctx, -1).
  • redo action is a shortcut to jump(ctx, +1).
  • clearHistory action clear the whole history.
  • isUndoAtom atom with a boolean state which represent the current position (is it possible to do "undo").
  • isRedoAtom atom with a boolean state which represent the current position (is it possible to do "redo").
  • positionAtom atom with a number state which represent the index of current history position.
  • historyAtom atom with a list of states, it could help you to know the size of the history. You shouldn't change it by yourself!

All methods accepts the optional properties:

  • length is the max amount of state records, 30 by default
  • shouldUpdate function allows you to ignore some updates, by default it skips state updates which is equal to the last history record
  • shouldReplace function allows you define what to do with the new update, replace the last history record (true) or add a new record (false - by default)
  • withPersist - WithPersist instance from one of the adapter of @reatom/persist. It will persist data from historyAtom and persistAtom, the target atom init state may be derived from the history, if it preserve.

withUndo

withUndo adds extra methods for an existing atom to handle the state history and navigate through it.

import { atom } from '@reatom/core'
import { withUndo } from '@reatom/undo'

const inputAtom = atom('').pipe(withUndo())

shouldReplace

Example: https://codesandbox.io/s/reatom-react-undo-7g2cwg?file=/src/App.tsx

This option helps you store only important updates by replacing the last state with the new one. It is useful when you need to store the fresh data but separate it into parts.

For example, we want to save in the history all user input, but the each record of the history should include only words, not a letters.

const inputAtom = atom('').pipe(
  withUndo({ shouldReplace: (ctx, state) => !state.endsWith(' ') }),
)

for (const letter of 'This is a test') {
  inputAtom(ctx, (s) => s + letter)
}
ctx.get(inputAtom) // 'This is a test'
ctx.get(inputAtom.historyAtom).length // 4

inputAtom.undo(ctx)
ctx.get(inputAtom) // 'This is a'

inputAtom.undo(ctx)
inputAtom.undo(ctx)
ctx.get(inputAtom) // 'This'

reatomUndo

reatomUndo creates a computed atom that collects the states of the passed atoms and manages them in a single history line. You can read the state of the resulting atom as a snapshot of all the states of the passed atoms.

The second argument accepts all withUndo options.

import { atom } from '@reatom/core'
import { reatomUndo } from '@reatom/undo'

const formUndoAtom = reatomUndo([emailAtom, passwordAtom], { length: 50 })

reatomDynamicUndo

reatomDynamicUndo accepts a callback to spy a dynamic list of atoms and manage their changes in a single history line. It is useful when you want to use atomization pattern. It is a more powerful version of reatomUndo, but it requires proper subscription to function correctly. Also, the state of this atom is not useful, do not read it.

The second argument accepts all withUndo options.

Example: https://codesandbox.io/s/reatom-react-atomization-undo-65rmfm?file=/src/model.ts