Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

knockout-undoredo

Package Overview
Dependencies
Maintainers
1
Versions
29
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

knockout-undoredo

Generic undo/redo history-management for knockout observables

  • 3.0.8
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

npm version Build Status Dependency Status devDependency Status Known Vulnerabilities Greenkeeper badge

Knockout Undo-Redo

Generic undo/redo history-management for knockout observables.

Install

$ npm install knockout-undoredo

Usage

Heads up: knockout-undoredo is only available via npm, thus it expects to be run in an node.js environment with require() and everything. If you want to use it in the browser you'll have to browserify the final code.

import ko from 'knockout';
import UndoManager from 'knockout-undoredo';

class ViewModel {
    constructor() {
        this.name = ko.observable('Obama');
        this.message = ko.pureComputed(() => `Thanks ${this.name()}`);
    }
}

const vm = new ViewModel();

// Connect your viewmodel with the undomanager
const undomanager = new UndoManager();
undomanager.startListening(vm);

ko.applyBindings(vm);

// ... and later

console.log(vm.message()); // Thanks Obama
vm.name('Trump');
console.log(vm.message()); // Thanks Trump
undomanager.undo();
console.log(vm.message()); // Thanks Obama
undomanager.redo();
console.log(vm.message()); // Thanks Trump

Create changesets as an undo/redo step

knockout-undoredo has the ability to collect multiple changes over a defined time as a changeset. These collections will be merged to just one undo/redo step in the history timeline.

// ...

const undomanager = new UndoManager({throttle: 300});
undomanager.startListening(vm);

console.log(vm.message()); // Thanks Obama
vm.name('Trump');
vm.name('Clinton');
console.log(vm.message()); // Thanks Clinton
undomanager.undo();
console.log(vm.message()); // Thanks Obama

Constructor Options

PropTypeDefaultDescription
throttleinteger300Timeout in which changes will be collected into a changeset
stepsinteger30Stack size for undoable/redoable changes

Taking manual snapshots

Snapshots are a collection of operations as one undo step. You can configure which operations should be bundled through the throttle contructor option. Besides that you can always manually trigger a snapshot through undomanager.takeSnapshot():

// ...

const undomanager = new UndoManager({throttle: 300});
undomanager.startListening(vm);

console.log(vm.message()); // Thanks Obama
vm.name('Trump');

// Take an early snapshot
undomanager.takeSnapshot()

vm.name('Clinton');
console.log(vm.message()); // Thanks Clinton
undomanager.undo();
console.log(vm.message()); // Thanks Trump

Skipping observables

By default all enumerable properties of an object will be subscribed to. If you want to skip a knockout obersvable you can modify the object's enumerable property:

Object.defineProperty(obj, 'key', {
  enumerable: false,
});

If you are one of the lucky guys who can make use of ES2016+ features in your code (i.e. through babel) you can simply import the nonenumerable decorator from the core-decorators module, or anything alike.

import {nonenumerable} from 'core-decorators';

class Example {
    @nonenumerable
    unobserved = ko.observable();
}

That way you can still explicitly reference the variable in your code, but it won't be collected by for-loops and Object.keys, Object.values or Object.entries respectively. Remember that it'll still be visible to Object.getOwnPropertyNames!

TODOs

  • Implement proper garbage collection for old listeners (2016-11-24)
  • Make knockout-undoredo's properties observable themselves. (steps, throttle, past, future, subscriptions, recording) (2016-11-24)
  • implement hasUndo() and hasRedo(), as an observable of course (2016-12-06)

Build & Release

gulp do-release --patch
git push --tags
npm publish

Keywords

FAQs

Package last updated on 18 Mar 2021

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc