Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/macfja/svelte-undoable

Memento design pattern in Svelte
https://github.com/macfja/svelte-undoable

memento stores svelte svelte-component sveltejs

Last synced: about 2 months ago
JSON representation

Memento design pattern in Svelte

Awesome Lists containing this project

README

        

# Svelte Undoable store

Memento design pattern in Svelte

## Installation

```
npm install @macfja/svelte-undoable
```

## Usage

```javascript
import { undoable } from "@macfja/svelte-undoable"

let name = undoable("John")

$name = "Jeanne"
$name = "Doe"

name.undo()
// Now the value of $name is "Jeanne"

name.undo()
// Now $name is "John"

name.redo()
// Now $name is "Jeanne" again
```

## Example

```html

import { undoable, undo, redo, reset, canUndo, canRedo } from "@macfja/svelte-undoable"
import { derived } from "svelte/store"

let name = undoable("John")
let canUndoName = derived([name], () => canUndo(name))
let canRedoName = derived([name], () => canRedo(name))

let counter = undoable(0, 10, value => value%2 === 0)
let canUndoCounter = derived([counter], () => counter.canUndo())
let canRedoCounter = derived([counter], () => counter.canRedo())

Hello {$name}

undo(name)}>Undo
redo(name)}>Redo
reset(name)}>Reset



Only even number as saved in the store history. The maximum number of remembered value is 10.
(If you go to 20, you can only go back to 2)

$counter++}>
Clicked {$counter} {$counter === 1 ? 'time' : 'times'}

undo(counter)}>Undo
redo(counter)}>Redo
```
([REPL](https://svelte.dev/repl/9412d77adca64a668055027e84619090?version=3.25.0))

## Contributing

Contributions are welcome. Please open up an issue or create PR if you would like to help out.

Read more in the [Contributing file](CONTRIBUTING.md)

## License

The MIT License (MIT). Please see [License File](LICENSE.md) for more information.