Skip to content

JavaScript Undo/Redo mechanism with simple API ✌️

License

Notifications You must be signed in to change notification settings

uditalias/timeline

Repository files navigation

🕰
Timeline

JavaScript Undo/Redo mechanism with simple API < 1KB

npm version npm version npm bundle size

Langauge

Install

NPM
npm i @udidu/timeline
YARN
yarn add @udidu/timeline
CDN
<script src="https://cdn.jsdelivr.net/npm/@udidu/timeline@latest/umd/timeline.js"></script>

Minified

<script src="https://cdn.jsdelivr.net/npm/@udidu/timeline@latest/umd/timeline.min.js"></script>

Usage

import Timeline from "@udidu/timeline";

const timeline = new Timeline();

timeline.setPresent(1);
timeline.setPresent(2);
timeline.setPresent(3);

timeline.present; // 3

timeline.undo(); // 2

timeline.hasPast; // true

timeline.hasFuture; // true

timeline.setPresent(4);

timeline.hasFuture; // false

timeline.undo(); // 2

timeline.redo(); // 4

timeline.clear();
timeline.hasPast; // false
timeline.hasFuture; // false

Options

All options are optional

const timeline = new Timeline({
  // The size of the past queue
  // default: Infinity
  size: 5,

  // When calling `setPresent(preset)` the value is passed to this function.
  // this is good for cases when we want to clone the object before saving it.
  // default: echo function - returns the same value/reference
  cloneFn: item => toJS(item),

  // Initialize the present
  // default: undefined
  present: 2
});

For your convenience, I created this Fiddle, so you can take Timeline for a ride.


License

The MIT License (MIT)

Copyright (c) 2019 Udi Talias

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.