Skip to content

Commit 4be15da

Browse files
authored
Merge pull request #1 from angular-redux2/feature/settings
Feature/settings
2 parents 3e6d483 + 5a86b57 commit 4be15da

24 files changed

+425
-85
lines changed

README.md

Lines changed: 64 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,21 @@ management using Redux.
88
[![downloads per month](https://img.shields.io/npm/dm/@angular-redux2/undo.svg)](https://www.npmjs.com/package/@angular-redux2/undo)
99

1010
## Installation
11+
1112
You can install angular-redux2/sync using npm:
1213

1314
```bash
1415
npm install @angular-redux2/undo
1516
```
1617

1718
## Usage
18-
- Take me to the [API docs](https://angular-redux2.github.io/undo).
19+
20+
> Take me to the [API docs](https://angular-redux2.github.io/undo).
1921
2022
To use `@angular-redux2/undo` in your Angular application, follow these steps:
2123
Define a StateWatchMap object that maps the properties you want to track for undo/redo operations to their corresponding
22-
state paths and configure the undo middleware in your Angular-Redux2/store setup by including it in the list of middleware:
24+
state paths and configure the undo middleware in your Angular-Redux2/store setup by including it in the list of
25+
middleware:
2326

2427
```typescript
2528
const middleware: Array<Middleware> = [
@@ -28,15 +31,26 @@ const middleware: Array<Middleware> = [
2831
path: 'path.to.property1'
2932
},
3033
propertyName2: {
31-
path: 'path.to.property2'
34+
path: 'path.to.property2',
35+
limit: 5
3236
},
3337
}),
3438
];
3539

3640
ngRedux.configureStore(rootReducer, {}, middleware, enhancer);
3741
```
42+
43+
settings:
44+
45+
- `path` (required): The path to the property in the state object using dot notation.
46+
- `filter` (optional): A filter function that determines if the property should be watched for undo/redo. Return true to
47+
include the property, and false to exclude it. If not specified, all properties are included.
48+
- `limit` (optional): The maximum number of past snapshots to keep in the undo history. If the limit is reached, the
49+
oldest snapshots are discarded. If not specified, no limit is applied.
50+
3851
Implement the undo/redo functionality in your Angular component or service.
39-
You can use the `undo`, `redo`, `jump`, and `clear_history` methods provided by `NgUndoStateActions` to perform the corresponding actions:
52+
You can use the `undo`, `redo`, `jump`, and `clear_history` methods provided by `NgUndoStateActions` to perform the
53+
corresponding actions:
4054

4155
```typescript
4256
// Example component
@@ -54,7 +68,8 @@ import { undo, redo, jump, clear_history } from '@angular-redux2/undo';
5468
`
5569
})
5670
export class ExampleComponent {
57-
constructor(private undoStateActions: NgUndoStateActions) {}
71+
constructor(private undoStateActions: NgUndoStateActions) {
72+
}
5873

5974
@Dispatch
6075
onUndo() {
@@ -77,3 +92,47 @@ export class ExampleComponent {
7792
}
7893
}
7994
```
95+
96+
## State Watch Map
97+
98+
The state watch map is an object that defines the paths to the state properties you want to track for undo. It has the
99+
following structure:
100+
101+
```typescript
102+
export interface StateWatchMap {
103+
[key: string]: {
104+
path: string;
105+
filter?: (action: any, currentState: any, snapshot: any) => boolean;
106+
limit?: number;
107+
}
108+
}
109+
```
110+
111+
- `key` (string): The unique identifier for the state property.
112+
- `path` (string): The dot-separated path to the state property.
113+
- `filter` (optional function): A filter function that determines if an action should be captured in the undo history
114+
for the specific state property.
115+
116+
## Custom Filters
117+
118+
You can define custom filter functions to control which actions are captured in the undo history for each state
119+
property.
120+
The filter function takes three parameters:
121+
122+
- `action`: The dispatched action object.
123+
- `currentState`: The current store state object.
124+
- `snapshot`: The snapshot to insert into the history.
125+
126+
The filter function should return true if the action should be captured, or false otherwise.
127+
Here's an example of a custom filter function that only captures actions with specific types:
128+
129+
```typescript
130+
const stateWatchMap: StateWatchMap = {
131+
'todos': {
132+
path: 'todos',
133+
filter: (action: any, currentState: any, snapshot: any): boolean => {
134+
return action.type === 'ADD_TODO' || action.type === 'REMOVE_TODO';
135+
}
136+
}
137+
};
138+
```

docs/assets/search.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/classes/UndoService.html

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ <h4>Hierarchy</h4>
2020
<ul class="tsd-hierarchy">
2121
<li><span class="target">UndoService</span></li></ul></section><aside class="tsd-sources">
2222
<ul>
23-
<li>Defined in <a href="https://github.com/angular-redux2/undo/blob/0956b3f/projects/angular-redux2/undo/src/services/undo.service.ts#L26">services/undo.service.ts:26</a></li></ul></aside>
23+
<li>Defined in <a href="https://github.com/angular-redux2/undo/blob/c6324e8/projects/angular-redux2/undo/src/services/undo.service.ts#L26">services/undo.service.ts:26</a></li></ul></aside>
2424
<section class="tsd-panel-group tsd-index-group">
2525
<section class="tsd-panel tsd-index-panel">
2626
<details class="tsd-index-content tsd-index-accordion" open><summary class="tsd-accordion-summary tsd-index-summary">
@@ -58,7 +58,7 @@ <h5><span class="tsd-kind-parameter">stateWatchMap</span>: <a href="../interface
5858
</div></li></ul></div>
5959
<h4 class="tsd-returns-title">Returns <a href="UndoService.html" class="tsd-signature-type tsd-kind-class">UndoService</a></h4><aside class="tsd-sources">
6060
<ul>
61-
<li>Defined in <a href="https://github.com/angular-redux2/undo/blob/0956b3f/projects/angular-redux2/undo/src/services/undo.service.ts#L46">services/undo.service.ts:46</a></li></ul></aside></li></ul></section></section>
61+
<li>Defined in <a href="https://github.com/angular-redux2/undo/blob/c6324e8/projects/angular-redux2/undo/src/services/undo.service.ts#L46">services/undo.service.ts:46</a></li></ul></aside></li></ul></section></section>
6262
<section class="tsd-panel-group tsd-member-group">
6363
<h2>Properties</h2>
6464
<section class="tsd-panel tsd-member tsd-is-protected"><a id="watchStateMap" class="tsd-anchor"></a>
@@ -72,20 +72,24 @@ <h4>Type declaration</h4>
7272
<li class="tsd-parameter-index-signature">
7373
<h5><span class="tsd-signature-symbol">[</span><span class="tsd-kind-parameter">key</span>: <span class="tsd-signature-type">string</span><span class="tsd-signature-symbol">]: </span><span class="tsd-signature-type ">NgUndoStateActions</span></h5></li></ul></div><aside class="tsd-sources">
7474
<ul>
75-
<li>Defined in <a href="https://github.com/angular-redux2/undo/blob/0956b3f/projects/angular-redux2/undo/src/services/undo.service.ts#L35">services/undo.service.ts:35</a></li></ul></aside></section></section>
75+
<li>Defined in <a href="https://github.com/angular-redux2/undo/blob/c6324e8/projects/angular-redux2/undo/src/services/undo.service.ts#L35">services/undo.service.ts:35</a></li></ul></aside></section></section>
7676
<section class="tsd-panel-group tsd-member-group">
7777
<h2>Methods</h2>
7878
<section class="tsd-panel tsd-member tsd-is-protected"><a id="detectChange" class="tsd-anchor"></a>
7979
<h3 class="tsd-anchor-link"><code class="tsd-tag ts-flagProtected">Protected</code> <span>detect<wbr/>Change</span><a href="#detectChange" aria-label="Permalink" class="tsd-anchor-icon"><svg class="icon icon-tabler icon-tabler-link" viewBox="0 0 24 24" stroke-width="2" stroke="currentColor" fill="none" stroke-linecap="round" stroke-linejoin="round"><use href="#icon-anchor-a"></use><use href="#icon-anchor-b"></use><use href="#icon-anchor-c"></use></svg></a></h3>
8080
<ul class="tsd-signatures tsd-is-protected">
81-
<li class="tsd-signature tsd-anchor-link" id="detectChange.detectChange-1"><span class="tsd-kind-call-signature">detect<wbr/>Change</span><span class="tsd-signature-symbol">(</span><span class="tsd-kind-parameter">state</span>, <span class="tsd-kind-parameter">newState</span><span class="tsd-signature-symbol">)</span><span class="tsd-signature-symbol">: </span><span class="tsd-signature-type">any</span><a href="#detectChange.detectChange-1" aria-label="Permalink" class="tsd-anchor-icon"><svg class="icon icon-tabler icon-tabler-link" viewBox="0 0 24 24" stroke-width="2" stroke="currentColor" fill="none" stroke-linecap="round" stroke-linejoin="round"><use href="#icon-anchor-a"></use><use href="#icon-anchor-b"></use><use href="#icon-anchor-c"></use></svg></a></li>
81+
<li class="tsd-signature tsd-anchor-link" id="detectChange.detectChange-1"><span class="tsd-kind-call-signature">detect<wbr/>Change</span><span class="tsd-signature-symbol">(</span><span class="tsd-kind-parameter">action</span>, <span class="tsd-kind-parameter">state</span>, <span class="tsd-kind-parameter">newState</span><span class="tsd-signature-symbol">)</span><span class="tsd-signature-symbol">: </span><span class="tsd-signature-type">any</span><a href="#detectChange.detectChange-1" aria-label="Permalink" class="tsd-anchor-icon"><svg class="icon icon-tabler icon-tabler-link" viewBox="0 0 24 24" stroke-width="2" stroke="currentColor" fill="none" stroke-linecap="round" stroke-linejoin="round"><use href="#icon-anchor-a"></use><use href="#icon-anchor-b"></use><use href="#icon-anchor-c"></use></svg></a></li>
8282
<li class="tsd-description"><code class="tsd-tag ts-flagProtected">Protected</code>
8383
<div class="tsd-comment tsd-typography"><p>Detects changes in the state and inserts snapshots into the undo history when necessary.</p>
8484
</div>
8585
<div class="tsd-parameters">
8686
<h4 class="tsd-parameters-title">Parameters</h4>
8787
<ul class="tsd-parameter-list">
8888
<li>
89+
<h5><span class="tsd-kind-parameter">action</span>: <span class="tsd-signature-type">any</span></h5>
90+
<div class="tsd-comment tsd-typography"><p>The dispatched action object.</p>
91+
</div></li>
92+
<li>
8993
<h5><span class="tsd-kind-parameter">state</span>: <span class="tsd-signature-type">any</span></h5>
9094
<div class="tsd-comment tsd-typography"><p>The current state object.</p>
9195
</div></li>
@@ -98,7 +102,7 @@ <h4 class="tsd-returns-title">Returns <span class="tsd-signature-type">any</span
98102
</ul>
99103
<aside class="tsd-sources">
100104
<ul>
101-
<li>Defined in <a href="https://github.com/angular-redux2/undo/blob/0956b3f/projects/angular-redux2/undo/src/services/undo.service.ts#L113">services/undo.service.ts:113</a></li></ul></aside></li></ul></section>
105+
<li>Defined in <a href="https://github.com/angular-redux2/undo/blob/c6324e8/projects/angular-redux2/undo/src/services/undo.service.ts#L117">services/undo.service.ts:117</a></li></ul></aside></li></ul></section>
102106
<section class="tsd-panel tsd-member tsd-is-protected"><a id="watcherAction" class="tsd-anchor"></a>
103107
<h3 class="tsd-anchor-link"><code class="tsd-tag ts-flagProtected">Protected</code> <span>watcher<wbr/>Action</span><a href="#watcherAction" aria-label="Permalink" class="tsd-anchor-icon"><svg class="icon icon-tabler icon-tabler-link" viewBox="0 0 24 24" stroke-width="2" stroke="currentColor" fill="none" stroke-linecap="round" stroke-linejoin="round"><use href="#icon-anchor-a"></use><use href="#icon-anchor-b"></use><use href="#icon-anchor-c"></use></svg></a></h3>
104108
<ul class="tsd-signatures tsd-is-protected">
@@ -122,7 +126,7 @@ <h4 class="tsd-returns-title">Returns <span class="tsd-signature-type">any</span
122126
</ul>
123127
<aside class="tsd-sources">
124128
<ul>
125-
<li>Defined in <a href="https://github.com/angular-redux2/undo/blob/0956b3f/projects/angular-redux2/undo/src/services/undo.service.ts#L78">services/undo.service.ts:78</a></li></ul></aside></li></ul></section>
129+
<li>Defined in <a href="https://github.com/angular-redux2/undo/blob/c6324e8/projects/angular-redux2/undo/src/services/undo.service.ts#L81">services/undo.service.ts:81</a></li></ul></aside></li></ul></section>
126130
<section class="tsd-panel tsd-member"><a id="watcherState" class="tsd-anchor"></a>
127131
<h3 class="tsd-anchor-link"><span>watcher<wbr/>State</span><a href="#watcherState" aria-label="Permalink" class="tsd-anchor-icon"><svg class="icon icon-tabler icon-tabler-link" viewBox="0 0 24 24" stroke-width="2" stroke="currentColor" fill="none" stroke-linecap="round" stroke-linejoin="round"><use href="#icon-anchor-a"></use><use href="#icon-anchor-b"></use><use href="#icon-anchor-c"></use></svg></a></h3>
128132
<ul class="tsd-signatures">
@@ -150,7 +154,7 @@ <h4 class="tsd-returns-title">Returns <span class="tsd-signature-type">any</span
150154
</ul>
151155
<aside class="tsd-sources">
152156
<ul>
153-
<li>Defined in <a href="https://github.com/angular-redux2/undo/blob/0956b3f/projects/angular-redux2/undo/src/services/undo.service.ts#L61">services/undo.service.ts:61</a></li></ul></aside></li></ul></section></section></div>
157+
<li>Defined in <a href="https://github.com/angular-redux2/undo/blob/c6324e8/projects/angular-redux2/undo/src/services/undo.service.ts#L64">services/undo.service.ts:64</a></li></ul></aside></li></ul></section></section></div>
154158
<div class="col-sidebar">
155159
<div class="page-menu">
156160
<div class="tsd-navigation settings">
@@ -182,6 +186,7 @@ <h3><svg width="20" height="20" viewBox="0 0 24 24" fill="none"><path d="M4.9389
182186
<li><a href="UndoService.html" class="current"><svg class="tsd-kind-icon" width="24" height="24" viewBox="0 0 24 24"><rect fill="var(--color-icon-background)" stroke="var(--color-ts-class)" stroke-width="1.5" x="1" y="1" width="22" height="22" rx="6" id="icon-128-path"></rect><path d="M11.898 16.1201C11.098 16.1201 10.466 15.8961 10.002 15.4481C9.53803 15.0001 9.30603 14.3841 9.30603 13.6001V9.64012C9.30603 8.85612 9.53803 8.24012 10.002 7.79212C10.466 7.34412 11.098 7.12012 11.898 7.12012C12.682 7.12012 13.306 7.34812 13.77 7.80412C14.234 8.25212 14.466 8.86412 14.466 9.64012H13.386C13.386 9.14412 13.254 8.76412 12.99 8.50012C12.734 8.22812 12.37 8.09212 11.898 8.09212C11.426 8.09212 11.054 8.22412 10.782 8.48812C10.518 8.75212 10.386 9.13212 10.386 9.62812V13.6001C10.386 14.0961 10.518 14.4801 10.782 14.7521C11.054 15.0161 11.426 15.1481 11.898 15.1481C12.37 15.1481 12.734 15.0161 12.99 14.7521C13.254 14.4801 13.386 14.0961 13.386 13.6001H14.466C14.466 14.3761 14.234 14.9921 13.77 15.4481C13.306 15.8961 12.682 16.1201 11.898 16.1201Z" fill="var(--color-text)" id="icon-128-text"></path></svg><span>Undo<wbr/>Service</span></a></li>
183187
<li><a href="../interfaces/NgUndoAction.html"><svg class="tsd-kind-icon" width="24" height="24" viewBox="0 0 24 24"><rect fill="var(--color-icon-background)" stroke="var(--color-ts-interface)" stroke-width="1.5" x="1" y="1" width="22" height="22" rx="6" id="icon-256-path"></rect><path d="M9.51 16V15.016H11.298V8.224H9.51V7.24H14.19V8.224H12.402V15.016H14.19V16H9.51Z" fill="var(--color-text)" id="icon-256-text"></path></svg><span>Ng<wbr/>Undo<wbr/>Action</span></a></li>
184188
<li><a href="../interfaces/NgUndoState.html"><svg class="tsd-kind-icon" width="24" height="24" viewBox="0 0 24 24"><use href="#icon-256-path"></use><use href="#icon-256-text"></use></svg><span>Ng<wbr/>Undo<wbr/>State</span></a></li>
189+
<li><a href="../interfaces/Settings.html"><svg class="tsd-kind-icon" width="24" height="24" viewBox="0 0 24 24"><use href="#icon-256-path"></use><use href="#icon-256-text"></use></svg><span>Settings</span></a></li>
185190
<li><a href="../interfaces/StateWatchMap.html"><svg class="tsd-kind-icon" width="24" height="24" viewBox="0 0 24 24"><use href="#icon-256-path"></use><use href="#icon-256-text"></use></svg><span>State<wbr/>Watch<wbr/>Map</span></a></li>
186191
<li><a href="../interfaces/UndoState.html"><svg class="tsd-kind-icon" width="24" height="24" viewBox="0 0 24 24"><use href="#icon-256-path"></use><use href="#icon-256-text"></use></svg><span>Undo<wbr/>State</span></a></li>
187192
<li><a href="../variables/HISTORY_STATE_KEY.html"><svg class="tsd-kind-icon" width="24" height="24" viewBox="0 0 24 24"><rect fill="var(--color-icon-background)" stroke="var(--color-ts-variable)" stroke-width="1.5" x="1" y="1" width="22" height="22" rx="6" id="icon-32-path"></rect><path d="M11.106 16L8.85 7.24H9.966L11.454 13.192C11.558 13.608 11.646 13.996 11.718 14.356C11.79 14.708 11.842 14.976 11.874 15.16C11.906 14.976 11.954 14.708 12.018 14.356C12.09 13.996 12.178 13.608 12.282 13.192L13.758 7.24H14.85L12.582 16H11.106Z" fill="var(--color-text)" id="icon-32-text"></path></svg><span>HISTORY_<wbr/>STATE_<wbr/>KEY</span></a></li>

0 commit comments

Comments
 (0)