Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/biswaviraj/time-travel
It is a simple TypeScript library for managing undo/redo functionality with a time-travel mechanism. It's designed to keep track of changes in state, allowing you to navigate back and forth through history with ease.
https://github.com/biswaviraj/time-travel
redo time-travel undo undo-redo
Last synced: 24 days ago
JSON representation
It is a simple TypeScript library for managing undo/redo functionality with a time-travel mechanism. It's designed to keep track of changes in state, allowing you to navigate back and forth through history with ease.
- Host: GitHub
- URL: https://github.com/biswaviraj/time-travel
- Owner: BiswaViraj
- Created: 2022-02-12T13:44:08.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2024-08-11T11:03:36.000Z (3 months ago)
- Last Synced: 2024-10-10T18:41:32.440Z (26 days ago)
- Topics: redo, time-travel, undo, undo-redo
- Language: TypeScript
- Homepage:
- Size: 180 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Time Travel
It is a simple TypeScript library for managing undo/redo functionality with a time-travel mechanism. It's designed to keep track of changes in state, allowing you to navigate back and forth through history with ease.
[![npm version](https://img.shields.io/npm/v/@biswaviraj/time-travel)](https://www.npmjs.com/package/@biswaviraj/time-travel)
![Minified + Gzipped size](https://badgen.net/bundlephobia/minzip/@biswaviraj/time-travel)
![Minified size](https://badgen.net/bundlephobia/min/@biswaviraj/time-travel)## Installation
```bash
npm install @biswaviraj/time-travel
```## Features
- **Undo/Redo Functionality**: Easily navigate through past states and reapply changes.
- **History Management**: Manage a configurable history limit to control memory usage.
- **Simple API**: Intuitive methods for interacting with the history and state.## Usage
Here's how you can use the time travel package in your TypeScript project:
#### Basic Example
```typescript
import { timeTravel } from "@biswaviraj/time-travel";// Create a time travel instance with an initial value
const history = timeTravel(0, { limit: 5 });// Add new values
history.add(1);
history.add(2);
history.add(3);// Get the current value
console.log(history.get()); // Output: 3// Undo the last change
history.undo();
console.log(history.get()); // Output: 2// Redo the undone change
history.redo();
console.log(history.get()); // Output: 3
```#### Example with Arrays
```typescript
import { timeTravel } from "@biswaviraj/time-travel";// Create a time travel instance with an initial array value
const history = timeTravel([1, 2, 3], { limit: 5 });// Add new array values
history.add([4, 5]);
history.add(6); // can add single values as well
history.add([7]);// Get the current value
console.log(history.get()); // Output: 7// Undo the last change
history.undo();
console.log(history.get()); // Output: 6// Redo the undone change
history.redo();
console.log(history.get()); // Output: 7
```#### Example with Array of Objects
```typescript
import { timeTravel } from "@biswaviraj/time-travel";// Define a type for our objects
type Item = { id: number; name: string };// Create a time travel instance with an initial array of objects
const history = timeTravel(
[
{ id: 1, name: "Item 1" },
{ id: 2, name: "Item 2" },
],
{ limit: 5 }
);// Add new array of objects
history.add({ id: 3, name: "Item 3" });history.add([
{ id: 4, name: "Item 4" },
{ id: 5, name: "Item 5" },
]);// Get the current state
console.log(history.get());
// Output: { id: 5, name: 'Item 5' }// Undo the last change
history.undo();
console.log(history.get());
// Output: { id: 4, name: 'Item 4' }// Redo the undone change
history.redo();
console.log(history.get());
// Output: { id: 5, name: 'Item 5' }
```## API
`timeTravel(initialValue: T | T[], options?: { limit?: number })`
Creates a time travel instance.- initialValue: The initial state, which can be a single value or an array of values.
- options: Optional configuration object.
- limit: The maximum number of past and future states to keep. Defaults to 10.The time travel library provides the following methods for managing history:
- `add(value: T): void`: Add a new value to the history.
- `get(): T`: Get the current value from the history.
- `undo(): void`: Undo the last change and move back in history.
- `redo(): void`: Redo the last undone change and move forward in history.
## TypeScript Support
The library is written in TypeScript and provides type definitions out of the box. This ensures you get proper type checking and autocompletion when using the library in your TypeScript projects.
## Contributing
Feel free to contribute to the project by submitting issues or pull requests. Please follow the standard GitHub workflow for contributions.
For more information or to report issues, please visit the [GitHub repository](https://github.com/biswaviraj/time-travel).