Skip to content

Commit

Permalink
1.1.0 Release
Browse files Browse the repository at this point in the history
  • Loading branch information
iMrDJAi committed Jul 17, 2020
1 parent 9d5132e commit a081bd0
Show file tree
Hide file tree
Showing 4 changed files with 146 additions and 145 deletions.
69 changes: 37 additions & 32 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# UndoRedo.js
[![npm](https://img.shields.io/npm/v/undoredo.js?color=red)](https://www.npmjs.com/package/undoredo.js)

A powerful and simple javascript library provides a history for undo/redo functionality. Just like a time machine! 🕐
A powerful and simple Javascript library provides a history for undo/redo functionality. Just like a time machine! 🕐
***
## Install:
**Node.js**:
Expand All @@ -12,7 +12,7 @@ npm install undoredo.js --save
```
Then simply require it:
```js
const UndoRedojs = require("undoredo.js");
const UndoRedojs = require("undoredo.js")
```

**Browser**:
Expand All @@ -38,58 +38,61 @@ By the same way you can use `UndoRedo.min.js` instead, which is a minified versi

The main function will be declared as `window.UndoRedojs`:
```js
const UndoRedojs = window.UndoRedojs;
const UndoRedojs = window.UndoRedojs
```

You can also bundle it info your project using [Webpack](https://webpack.js.org/guides/getting-started/). 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 tasks, for example:
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.
- And more...

**Basic usage**:

>Lets setup our history:
```js
const myHistory = UndoRedojs(5);
const myHistory = new UndoRedojs(5)
```
>This function will return an object with the methods and the properties that we will use later.
>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:
```js
myHistory.record('1');
myHistory.record('12');
myHistory.record('123');
myHistory.record('1234');
myHistory.record('12345');
myHistory.record('123456');
myHistory.record('1234567', true);
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:
```js
console.log(myHistory.stack);
console.log(myHistory.stack)
// output => Array(4) [ "", "12345", "123456", "1234567" ]
```
>You can get the current history position using the `currentNumber` property:
```js
console.log(myHistory.currentNumber);
console.log(myHistory.currentNumber)
// output => 3
```
>Remember that arrays always start from **0**, so the number **3** here is actually the **4th** element, wanna check?
```js
console.log(myHistory.stack[myHistory.currentNumber]);
console.log(myHistory.stack[myHistory.currentNumber])
// output => "1234567"
```
>There is another way to get the current element, using the `current` method:
```js
console.log(myHistory.current());
console.log(myHistory.current())
// output => "1234567"
```
>The history array always starts with an empty element, this is helpful.
>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?
>
Expand All @@ -99,7 +102,7 @@ console.log(myHistory.current());
>
>To disable that, just use the number **1**, or keep it empty because the default `cooldownNumber` is **1**:
```js
const myHistory = UndoRedojs();
const myHistory = UndoRedojs()
```
>
>But we see that the `"1234567"` element is recorded during a cooldown. Why?
Expand All @@ -108,10 +111,10 @@ console.log(myHistory.current());
>
>To undo, just use the `undo` method:
```js
const undo = myHistory.undo();
console.log(undo);
const undo = myHistory.undo()
console.log(undo)
// output => "123456"
console.log(myHistory.current());
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.
Expand All @@ -128,13 +131,13 @@ console.log(myHistory.current());
>
>To redo, just use the `redo` method:
```js
const redo = myHistory.redo();
console.log(redo);
const redo = myHistory.redo()
console.log(redo)
// output => "1234567"
console.log(myHistory.current());
console.log(myHistory.current())
// output => "1234567"
```
>So the `undo` method will make you step forward once inside the array and will return the next element.
>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?
```js
Expand All @@ -148,9 +151,9 @@ console.log(myHistory.current());
>
>What if we undo then record then redo again?
```js
myHistory.undo();
myHistory.record('12345678');
console.log(myHistory.redo());
myHistory.undo()
myHistory.record('12345678')
console.log(myHistory.redo())
// output => undefined
```
>Why? Because the `record` method will remove every next element.
Expand All @@ -165,10 +168,12 @@ You can find a good example of a textarea with working undo/redo buttons [here](

That's a [live demo](https://imrdjai.github.io/UndoRedo.js).

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

## Notes:
- This package has made by [${Mr.DJA}](https://invite.gg/MrDJA).
- This is my first package, leave a star if you like it.
- You are free to suggest anything and I will add it soon if I found it useful.
- 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 🇩🇿**.
84 changes: 42 additions & 42 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,54 +2,54 @@
<html>

<head>
<meta charset="UTF-8">
<title>UndoRedo.js - Textarea Example </title>
<meta charset="UTF-8">
<title>UndoRedo.js - Textarea Example</title>
</head>

<body>
<textarea id="input" style="height: 300px; width: 400px;"></textarea>
<br />
<button id="undo" style="height: 50px; width: 200px;">undo</button>
<button id="redo" style="height: 50px; width: 200px;">redo</button>
<script src="./src/UndoRedo.js"></script>
<textarea id="input" style="height: 300px; width: 400px;"></textarea>
<br />
<button id="undo" style="height: 50px; width: 200px;">undo</button>
<button id="redo" style="height: 50px; width: 200px;">redo</button>
<script src="./src/UndoRedo.js"></script>
</body>

<script>
// Require the library function
const txtHistory = window.UndoRedojs(5);
// Get the textarea
const textarea = document.querySelector("#input");
// Add event listener for inputs on the textarea
textarea.addEventListener('input', () => {
// Check if the new textarea value is different
if (txtHistory.current() !== textarea.value) {
// Check for pastes, auto corrects..
if ((textarea.value.length - txtHistory.current().length) > 1 || (textarea.value.length - txtHistory.current().length) < -1 || (textarea.value.length - txtHistory.current().length) === 0) {
// Record the textarea value and force to bypass cooldown
txtHistory.record(textarea.value, true);
// Check for single key press, single chacacter paste..
} else {
// Record the textarea value
txtHistory.record(textarea.value);
}
}
});
// Some browsers will auto-fill the textarea again after reloading, this will deal with that
setTimeout(() => {
if (textarea.value) txtHistory.record(textarea.value, true);
}, 100);
// The undo button
document.querySelector("#undo").addEventListener('click', () => {
if (txtHistory.undo(true) !== undefined) {
textarea.value = txtHistory.undo();
}
});
// The redo button
document.querySelector("#redo").addEventListener('click', () => {
if (txtHistory.redo(true) !== undefined) {
textarea.value = txtHistory.redo();
}
});
// Require the library function
const txtHistory = new window.UndoRedojs(5)
// Get the textarea
const textarea = document.querySelector("#input")
// Add event listener for inputs on the textarea
textarea.addEventListener('input', () => {
// Check if the new textarea value is different
if (txtHistory.current() !== textarea.value) {
// Check for pastes, auto corrects..
if ((textarea.value.length - txtHistory.current().length) > 1 || (textarea.value.length - txtHistory.current().length) < -1 || (textarea.value.length - txtHistory.current().length) === 0) {
// Record the textarea value and force to bypass cooldown
txtHistory.record(textarea.value, true)
// Check for single key press, single chacacter paste..
} else {
// Record the textarea value
txtHistory.record(textarea.value)
}
}
});
// Some browsers will auto-fill the textarea again after reloading, this will deal with that
setTimeout(() => {
if (textarea.value) txtHistory.record(textarea.value, true)
}, 100);
// The undo button
document.querySelector("#undo").addEventListener('click', () => {
if (txtHistory.undo(true) !== undefined) {
textarea.value = txtHistory.undo()
}
});
// The redo button
document.querySelector("#redo").addEventListener('click', () => {
if (txtHistory.redo(true) !== undefined) {
textarea.value = txtHistory.redo()
}
});
</script>

</html>
13 changes: 4 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
{
"name": "undoredo.js",
"version": "1.0.2",
"description": "A powerful and simple javascript library provides a history for undo/redo functionality. Just like a time machine! 🕐",
"version": "1.1.0",
"description": "A powerful and simple Javascript library provides a history for undo/redo functionality. Just like a time machine! 🕐",
"main": "./src/undoredo.js",
"scripts": {
"test": "echo \"Warning: no test specified\""
},
"repository": {
"type": "git",
"url": "git+https://github.com/iMrDJAi/UndoRedo.js.git"
Expand All @@ -20,13 +17,11 @@
"redo"
],
"author": {
"name": "${Mr.DJA}",
"email": "[email protected]",
"discord": "https://invite.gg/MrDJA"
"name": "${Mr.DJA}"
},
"license": "MIT",
"bugs": {
"url": "https://github.com/iMrDJAi/UndoRedo.js/issues"
},
"homepage": "https://github.com/iMrDJAi/UndoRedo.js#readme"
}
}
Loading

0 comments on commit a081bd0

Please sign in to comment.