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

snapback

v0.9.0

Published

DOM Undo/Redo

Downloads

142

Readme

snapback

DOM Undo/Redo library, weighing in at 1.4kb (UMD bundle, minified and gzipped).

Demo

There is a demo plunker. You can view it at https://embed.plnkr.co/Dl84lxM1fYyldOkkZv9a/.

Install

It is highly recommended to use NPM and a bundler like https://github.com/rollup/rollup, https://github.com/substack/node-browserify, https://github.com/webpack/webpack or https://github.com/lasso-js/lasso.

$ npm install snapback

If you want to use a UMD bundle instead, you can use unpkg (https://unpkg.com/snapback@latest/dist/snapback.min.js, or https://unpkg.com/snapback@latest/dist/snapback.js).

Use

const Snapback = require('snapback');

const snapback = new Snapback(document.body);

// snapback needs to be enabled to listen for mutations
snapback.enable();

// register mutations as an Undo/Redo object`
snapback.register();

// undo
snapback.undo();

// redo
snapback.redo();

// mutations observers are pretty intense. try to have as few active as possible
snapback.disable();

When snapback is enabled, it will store all mutations to a mutations array (snapback.mutations). As soon as you want to create an Undo/Redo object from the mutation array, you need to call snapback.register() (calling snapback.undo() should automatically register any unregistered mutations before doing an undo.

If you pass a { timeout: Number } to the constructor, snapback will register any undos if no new mutations are added before timeout.

Configuration

The main thing you want to configure is probably what the MutationObserver should be observing. By default, it watches pretty much everything. IE, the default is:

{
  subtree: true,
  attributes: true,
  attributeOldValue: true,
  childList: true,
  characterData: true,
  characterDataOldValue: true
}

NOTE: if you observe attributes or characterData, you MUST add the option for their old value.

So, to create a snapback instance that only watches for node insertions and removals, and automatically registers undos after 1 sec:

const snapback = new Snapback(someElement, {
  observe: { subtree: true, childList: true },
  timeout: 1000
});

Custom Stores

If you need to store and restore some custom data, you can to pass two methods as options, store and restore. The store function needs to save any data to this.data. When an Undo gets created, it will grab the current value in this.data as the data "before" the undo, and will make a new call to this.store() to calculate the data "after" the undo.

When snapback.undo() or snapback.redo() is called, this.restore will be called with the before or after data respectively.

This functionality was implemented to allow storing and restoring text selections before and after undos (using selektr).

NOTE: You will probably need to call snapback.store() manually to ensure the correct data is set. With selektr, this is done on any movement keystrokes or mouse up (selections).

const snapback = new Snapback(this.el, {
  /**
   * This has to save to `this.data`!
   */
  store: function(data) {
    return (this.data = (data || selektr.get()));
  },

  restore: function(data) {
    selektr.restore(data, true);

    // restored selection is now current selection, ie save it.
    this.store(data);
  },
});

The actual code that does this simply looks like this:

this.undos.push({
  data: isFunction(this.store) ? {
    before: this.data,
    after: this.store()
  } : undefined,
  mutations: this.mutations
});

And then, inside undoRedo it is applied with

isFunction(this.restore) && this.restore(isUndo ? undo.data.before : undo.data.after);

Size

                               | Minified  | Compressed

-----------------------------------|-----------|----------- Snapback (UMD build, all deps) | 3.62kb | 1.4kb