项目作者: MacFJA

项目描述 :
Memento design pattern in Svelte
高级语言: TypeScript
项目地址: git://github.com/MacFJA/svelte-undoable.git
创建时间: 2020-09-12T10:19:41Z
项目社区:https://github.com/MacFJA/svelte-undoable

开源协议:MIT License

下载


Svelte Undoable store

Memento design pattern in Svelte

Installation

  1. npm install @macfja/svelte-undoable

Usage

  1. import { undoable } from "@macfja/svelte-undoable"
  2. let name = undoable("John")
  3. $name = "Jeanne"
  4. $name = "Doe"
  5. name.undo()
  6. // Now the value of $name is "Jeanne"
  7. name.undo()
  8. // Now $name is "John"
  9. name.redo()
  10. // Now $name is "Jeanne" again

Example

  1. <script>
  2. import { undoable, undo, redo, reset, canUndo, canRedo } from "@macfja/svelte-undoable"
  3. import { derived } from "svelte/store"
  4. let name = undoable("John")
  5. let canUndoName = derived([name], () => canUndo(name))
  6. let canRedoName = derived([name], () => canRedo(name))
  7. let counter = undoable(0, 10, value => value%2 === 0)
  8. let canUndoCounter = derived([counter], () => counter.canUndo())
  9. let canRedoCounter = derived([counter], () => counter.canRedo())
  10. </script>
  11. <h1>Hello {$name}</h1>
  12. <input bind:value={$name} />
  13. <button disabled={!$canUndoName} on:click={() => undo(name)}>Undo</button>
  14. <button disabled={!$canRedoName} on:click={() => redo(name)}>Redo</button>
  15. <button disabled={!$canUndoName} on:click={() => reset(name)}>Reset</button>
  16. <hr />
  17. Only even number as saved in the store history. The maximum number of remembered value is 10.
  18. (If you go to <code>20</code>, you can only go back to <code>2</code>)
  19. <button on:click={() => $counter++}>
  20. Clicked {$counter} {$counter === 1 ? 'time' : 'times'}
  21. </button>
  22. <button disabled={!$canUndoCounter} on:click={() => undo(counter)}>Undo</button>
  23. <button disabled={!$canRedoCounter} on:click={() => redo(counter)}>Redo</button>

(REPL)

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

License

The MIT License (MIT). Please see License File for more information.