A powerful and simple JavaScript library provides a history for undo/redo functionality. Just like a time machine! 🕐

Overview

UndoRedo.js

npm

A powerful and simple Javascript library provides a history for undo/redo functionality. Just like a time machine! 🕐


Installation:

Node.js:

Use this command to install the node module:

npm install undoredo.js --save

Then simply require it:

const UndoRedojs = require("undoredo.js")

Browser:

You can get the UndoRedo.js file from JsDeliver CDN:

<script src='https://cdn.jsdelivr.net/gh/iMrDJAi/UndoRedo.js/src/UndoRedo.js'></script>

You can download it and use it locally:

📄index.html
📁js
 ↳ 📄UndoRedo.js

This time it will be:

<script src='./js/UndorRedo.js'></script>

By the same way you can use UndoRedo.min.js instead, which is a minified version of UndoRedo.js. JsDeliver CDN link.

The main function will be declared as window.UndoRedojs:

const UndoRedojs = window.UndoRedojs

You can also bundle it into your project using Webpack. You can use it in anyway you want, it's just another npm packege after all! 😅

Usage:

This package is useful for any step-by-step task, for example:

  • Undo/Redo functionality for a text input.
  • Something like a browser history.
  • A settings page.
  • A files explorer.
  • A calculator (why not xD).
  • And more...

Basic usage:

Lets setup our history:

 const myHistory = new UndoRedojs(5)

This will return a class object with the methods and the properties that we will use later.

As you can see, we added the number 5 as a parameter, this will be used for cooldowns, keep reading for more details.

To push new elements to the history, use the record method:

myHistory.record('1')
myHistory.record('12')
myHistory.record('123')
myHistory.record('1234')
myHistory.record('12345')
myHistory.record('123456')
myHistory.record('1234567', true)

To get the history array, use the stack property:

console.log(myHistory.stack)
// output => Array(4) [ "", "12345", "123456", "1234567" ]

You can get the current history position using the currentIndex property:

console.log(myHistory.currentIndex)
// output => 3

Remember that arrays always start from 0, so the number 3 here is actually the 4th element, wanna check?

console.log(myHistory.stack[myHistory.currentIndex])
// output => "1234567"

There is another way to get the current element, using the current method:

console.log(myHistory.current())
// output => "1234567"

The history array always starts with an empty element, this can be very helpful for text areas.

So we called the record method 7 times, but we only got 3 recorded elements (without the 1st empty one). Why?

We used the number 5 as a parameter for the UndoRedojs function, it's just like saying: 'Record every 5 calls', that makes cooldowns. We call this parameter: cooldown.

So during cooldowns the record method will not gonna push new elements to the history stack, instead of that it will update the current element with the new one.

To disable that, just use the number 1, or keep it empty because the default cooldown is 1:

 const myHistory = UndoRedojs()

But we see that the "1234567" element is recorded during a cooldown. Why?

The difference here is that we added true as a 2nd parameter for the record method, it's just like saying: 'Force to record', that will make it bypass the cooldown. We call this parameter: force.

To undo, just use the undo method:

const undo = myHistory.undo()
console.log(undo)
// output => "123456"
console.log(myHistory.current())
// output => "123456"

So the undo method will make you step back once inside the array and will return the previous element.

What if we add true as a parameter?

const undo = myHistory.undo(true);
console.log(undo);
// output => "123456"
console.log(myHistory.current());
// output => "1234567"

As you can see the current element stays "1234567", so this time it returns the previous element without stepping back. We call this parameter: readOnly.

To redo, just use the redo method:

const redo = myHistory.redo()
console.log(redo)
// output => "1234567"
console.log(myHistory.current())
// output => "1234567"

So the redo method will make you step forward once inside the array and will return the next element.

What if we add true as a parameter?

const redo = myHistory.redo(true);
console.log(redo);
// output => "1234567"
console.log(myHistory.current());
// output => "123456"

As you can see the current element stays "123456", so this time it returns the next element without stepping forward. We call this parameter: readOnly.

What if we undo then record then redo again?

myHistory.undo()
myHistory.record('12345678')
console.log(myHistory.redo())
// output => undefined

Why? Because the record method will remove every next element.

You can update the current element using the current method:

console.log(myHistory.current('1234567'));
// output => "1234567"

Examples:

You can find a good example of a textarea with working undo/redo buttons here.

That's a live demo.

Dependents Projects:

Wanna use UndoRedo.js on your next big project? Let me now and it will be listed here! :)

Special Thanks:

Notes:

  • This is my first package, leave a star if you like it <3.
  • You are free to suggest anything and I will try to add it soon if I found it useful.
  • If you found any mistake (including the README file) feel free to help to fix it.
  • Please report any bugs.
  • Made with in Algeria 🇩🇿 .
You might also like...

Responsive Tabs is a jQuery plugin that provides responsive tab functionality.

Responsive Tabs is a jQuery plugin that provides responsive tab functionality. The tabs transform to an accordion when it reaches a CSS breakpoint. You can use this plugin as a solution for displaying tabs elegantly on desktop, tablet and mobile.

Dec 8, 2022

⚗️Nitro provides a powerful toolchain and a runtime framework from the UnJS ecosystem to build and deploy any JavaScript server, anywhere

⚗️Nitro provides a powerful toolchain and a runtime framework from the UnJS ecosystem to build and deploy any JavaScript server, anywhere

Jan 5, 2023

Easiest 1-click way to install and use Stable Diffusion on your own computer. Provides a browser UI for generating images from text prompts and images. Just enter your text prompt, and see the generated image.

Easiest 1-click way to install and use Stable Diffusion on your own computer. Provides a browser UI for generating images from text prompts and images. Just enter your text prompt, and see the generated image.

Stable Diffusion UI Easiest way to install and use Stable Diffusion on your own computer. No dependencies or technical knowledge required. 1-click ins

Dec 30, 2022

A powerful simple JavaScript function-like toolset.

PrueEval PureEval 因 VoxelGeometry 项目而生,意在打造一个精巧而强大的 JavaScript 函数式工具包。 PureEval 具有以下特点: 具有独特的非变量绑定式 iterate 策略。 支持对任意参数已知函数函数的柯里化。 体积小、代码精简,嵌入成本极低。 Us

Dec 28, 2022

A work-in-progress HTML sanitizer that strives for: performance like window.Sanitizer, readiness like DOMPurify, and ability to run in a WebWorker like neither of those.

Amuchina A work-in-progress HTML sanitizer that strives for: performance like window.Sanitizer, readiness like DOMPurify, and ability to run in a WebW

Sep 17, 2022

A simple to-do app for managing daily tasks built with Webpack, JavaScript, HTML, and CSS with the functionality to manipulate multiple completed tasks and store them to local storage.

A simple to-do app for managing daily tasks built with Webpack, JavaScript, HTML, and CSS with the functionality to manipulate multiple completed tasks and store them to local storage.

TODO LIST APP In this project, I have built a simple HTML list of To Do tasks. The list is styled according to the listed specifications . This simple

Jun 7, 2022

A simple To-do app project made using JavaScript ES6 and Webpack - Microverse. You can add, remove, check tasks, and remove all the tasks that were done at the same time. Feel free to see the live version, if you like it please give it a star!

To Do List a to do list javascript app buit using webpack and es6. Built With HTML CSS JavaScript Wepack Live Demo (if available) Live Demo Link Getti

Dec 17, 2022

This is a simple javascript file that gives you control over the browser cursor, alowing for fully animated cursors using CSS's cursor functionality.

This is a simple javascript file that gives you control over the browser cursor, alowing for fully animated cursors using CSS's cursor functionality.

animatedWebCursors.js This is a simple javascript file that gives you control over the browser cursor, alowing for fully animated cursors using CSS's

Dec 25, 2022

Get a diff view of your Obsidian Sync, File Recovery and Git version history

Get a diff view of your Obsidian Sync, File Recovery and Git version history

Version History Diff (for Sync and File Recovery Core plugins and Git) Note This plugin uses private APIs, so it may break at any time. Use at your ow

Dec 26, 2022
Comments
  • Sometimes it deletes more than 1 action

    Sometimes it deletes more than 1 action

    I liked the package, it is pretty cool, but when testing it sometimes it reverts more than 2 actions. ik u can fix it fast. Keep up the good work dude :)

    opened by salahmak 2
Releases(v1.2.0)
Owner
${Mr.DJA}
Software Engineer 💻
${Mr.DJA}
History.js gracefully supports the HTML5 History/State APIs (pushState, replaceState, onPopState) in all browsers. Including continued support for data, titles, replaceState

History.js gracefully supports the HTML5 History/State APIs (pushState, replaceState, onPopState) in all browsers. Including continued support for data, titles, replaceState. Supports jQuery, MooTools and Prototype.

Browser State 10.8k Dec 26, 2022
Grupprojekt för kurserna 'Javascript med Ramverk' och 'Agil Utveckling'

JavaScript-med-Ramverk-Laboration-3 Grupprojektet för kurserna Javascript med Ramverk och Agil Utveckling. Utvecklingsguide För information om hur utv

Svante Jonsson IT-Högskolan 3 May 18, 2022
Hemsida för personer i Sverige som kan och vill erbjuda boende till människor på flykt

Getting Started with Create React App This project was bootstrapped with Create React App. Available Scripts In the project directory, you can run: np

null 4 May 3, 2022
Kurs-repo för kursen Webbserver och Databaser

Webbserver och databaser This repository is meant for CME students to access exercises and codealongs that happen throughout the course. I hope you wi

null 14 Jan 3, 2023
FortuneSheet is an online spreedsheet component library that provides out-of-the-box features just like Excel

FortuneSheet FortuneSheet is an online spreedsheet component library that provides out-of-the-box features just like Excel English | 简体中文 Purpose The

Suzhou Ruilisi Technology Co., Ltd 1.6k Jan 3, 2023
Solana blockchain candy machine app boilerplate on top of Metaplex Candy Machine. NextJS, Tailwind, Anchor, SolanaLabs.React, dev/mainnet automation scripts.

NFT Candy Factory NOTE: This repo will prob only work on unix-based environments. The NFT Candy Factory project is designed to let users fork, customi

Kevin Faveri 261 Dec 30, 2022
Complete Open Source Front End Candy Machine V2 Minter dAPP Built For The Frog Nation NFT Solana Project. Built With React, Candy Machine V2, Typescript

Complete Open Source Front End Candy Machine V2 Minter dAPP Built For The Frog Nation NFT Solana Project. Built With React, Candy Machine V2, Typescript

null 17 Sep 24, 2022
A JavaScript plugin that provides snapping functionality to a set of panels within your interface.

PanelSnap A JavaScript library that provides snapping functionality to a set of panels within your interface. Introduction PanelSnap is a framework ag

Guido Bouman 616 Dec 16, 2022
A card for Home Assistant Lovelace for exploring the history of your entities interactively and in real time.

History explorer card This is a custom history card for Home Assistant. This card offers a highly interactive and configurable way to view the history

null 165 Dec 31, 2022