├── .gitignore
├── .prettierrc
├── .vscode
└── settings.json
├── CHANGELOG.md
├── LICENSE
├── README.md
├── index.js
├── jsconfig.json
├── package-lock.json
├── package.json
└── test
└── test.js
/.gitignore:
--------------------------------------------------------------------------------
1 | .DS_Store
2 | node_modules
--------------------------------------------------------------------------------
/.prettierrc:
--------------------------------------------------------------------------------
1 | {
2 | "useTabs": true,
3 | "singleQuote": true,
4 | "trailingComma": "none",
5 | "printWidth": 100
6 | }
7 |
--------------------------------------------------------------------------------
/.vscode/settings.json:
--------------------------------------------------------------------------------
1 | {
2 | "editor.formatOnSave": true,
3 | "files.insertFinalNewline": true
4 | }
--------------------------------------------------------------------------------
/CHANGELOG.md:
--------------------------------------------------------------------------------
1 | # svelte-undo changelog
2 |
3 | ## 1.0.2
4 |
5 | - Allow `push` to take a function
6 |
7 | ## 1.0.1
8 |
9 | - Expose `current` value
10 |
11 | ## 1.0.0
12 |
13 | - First release
14 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | Copyright (c) 2021 [these people](https://github.com/Rich-Harris/svelte-undo/graphs/contributors)
2 |
3 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4 |
5 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6 |
7 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
8 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # svelte-undo
2 |
3 | A small utility for managing an undo stack, that you can subscribe to in your Svelte applications (or indeed anywhere else).
4 |
5 | [Demo here](https://svelte.dev/repl/5af74b1765414b46927414815a6477d1?version=3.38.3).
6 |
7 | ## Usage
8 |
9 | ```js
10 | import { createStack } from 'svelte-undo';
11 |
12 | let value = { answer: 42 };
13 |
14 | const stack = createStack(value);
15 |
16 | // undo/redo have no effect if we're at the
17 | // beginning/end of the stack
18 | console.log((value = stack.undo())); // { answer: 42 }
19 | console.log((value = stack.redo())); // { answer: 42 }
20 |
21 | // stack.push returns the new value
22 | value = stack.push({ answer: 43 });
23 | value = stack.push({ answer: 44 });
24 | value = stack.push({ answer: 45 });
25 |
26 | console.log(value); // { answer: 45 }
27 |
28 | console.log((value = stack.undo())); // { answer: 44 }
29 | console.log((value = stack.undo())); // { answer: 43 }
30 | console.log((value = stack.undo())); // { answer: 42 }
31 | console.log((value = stack.undo())); // { answer: 42 }
32 |
33 | console.log((value = stack.redo())); // { answer: 43 }
34 |
35 | // pushing clears anything 'forward' in the stack
36 | value = stack.push({ answer: 99 });
37 |
38 | // you can also pass a function to `push`
39 | value = stack.push((value) => ({ answer: value.answer + 1 }));
40 |
41 | console.log((value = stack.undo())); // { answer: 99 }
42 | console.log((value = stack.undo())); // { answer: 43 }
43 | console.log((value = stack.redo())); // { answer: 99 }
44 | console.log((value = stack.redo())); // { answer: 100 }
45 | console.log((value = stack.redo())); // { answer: 100 }
46 |
47 | // you can subscribe to the state of the undo stack
48 | const unsubscribe = stack.subscribe(($stack) => {
49 | console.log($stack.first); // false
50 | console.log($stack.last); // true — we're currently at the end of the stack
51 | console.log($stack.current); // { answer: 99 }
52 | });
53 |
54 | unsubscribe();
55 | ```
56 |
57 | In a Svelte component, you can reference `first` and `last` as store properties without manually subscribing:
58 |
59 | ```svelte
60 |
61 |
62 | ```
63 |
64 | You can also access the current value of the stack, if that's preferable to tracking the value manually:
65 |
66 | ```svelte
67 |