@coaction/history 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024 Michael Lin
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,43 @@
1
+ # @coaction/history
2
+
3
+ ![Node CI](https://github.com/unadlib/coaction/workflows/Node%20CI/badge.svg)
4
+ [![npm](https://img.shields.io/npm/v/@coaction/history.svg)](https://www.npmjs.com/package/@coaction/history)
5
+ ![license](https://img.shields.io/npm/l/@coaction/history)
6
+
7
+ A undo/redo middleware for Coaction.
8
+
9
+ ## Installation
10
+
11
+ You can install it via npm, yarn or pnpm.
12
+
13
+ ```sh
14
+ npm install coaction @coaction/history
15
+ ```
16
+
17
+ ## Usage
18
+
19
+ ```ts
20
+ import { create } from 'coaction';
21
+ import { history } from '@coaction/history';
22
+
23
+ const store = create(
24
+ (set) => ({
25
+ count: 0,
26
+ increment() {
27
+ set((draft) => {
28
+ draft.count += 1;
29
+ });
30
+ }
31
+ }),
32
+ {
33
+ middlewares: [history()]
34
+ }
35
+ );
36
+
37
+ store.getState().increment();
38
+ (store as any).history.undo();
39
+ ```
40
+
41
+ ## Documentation
42
+
43
+ You can find the documentation [here](https://github.com/unadlib/coaction).
@@ -0,0 +1,18 @@
1
+ import { Middleware } from 'coaction';
2
+
3
+ type HistoryOptions<T extends object> = {
4
+ limit?: number;
5
+ partialize?: (state: T) => object;
6
+ };
7
+ type HistoryApi<T extends object> = {
8
+ undo: () => boolean;
9
+ redo: () => boolean;
10
+ clear: () => void;
11
+ canUndo: () => boolean;
12
+ canRedo: () => boolean;
13
+ getPast: () => object[];
14
+ getFuture: () => object[];
15
+ };
16
+ declare const history: <T extends object>({ limit, partialize }?: HistoryOptions<T>) => Middleware<T>;
17
+
18
+ export { type HistoryApi, type HistoryOptions, history };
@@ -0,0 +1,18 @@
1
+ import { Middleware } from 'coaction';
2
+
3
+ type HistoryOptions<T extends object> = {
4
+ limit?: number;
5
+ partialize?: (state: T) => object;
6
+ };
7
+ type HistoryApi<T extends object> = {
8
+ undo: () => boolean;
9
+ redo: () => boolean;
10
+ clear: () => void;
11
+ canUndo: () => boolean;
12
+ canRedo: () => boolean;
13
+ getPast: () => object[];
14
+ getFuture: () => object[];
15
+ };
16
+ declare const history: <T extends object>({ limit, partialize }?: HistoryOptions<T>) => Middleware<T>;
17
+
18
+ export { type HistoryApi, type HistoryOptions, history };
package/dist/index.js ADDED
@@ -0,0 +1,122 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // index.ts
21
+ var index_exports = {};
22
+ __export(index_exports, {
23
+ history: () => history
24
+ });
25
+ module.exports = __toCommonJS(index_exports);
26
+
27
+ // src/index.ts
28
+ var toSnapshot = (state) => {
29
+ if (Array.isArray(state)) {
30
+ return state.map((item) => toSnapshot(item));
31
+ }
32
+ if (typeof state === "object" && state !== null) {
33
+ const next = {};
34
+ for (const key in state) {
35
+ const value = state[key];
36
+ if (typeof value === "function") {
37
+ continue;
38
+ }
39
+ next[key] = toSnapshot(value);
40
+ }
41
+ return next;
42
+ }
43
+ return state;
44
+ };
45
+ var isEqual = (a, b) => JSON.stringify(a) === JSON.stringify(b);
46
+ var history = ({
47
+ limit = 100,
48
+ partialize = (state) => state
49
+ } = {}) => (store) => {
50
+ const past = [];
51
+ const future = [];
52
+ let isTimeTraveling = false;
53
+ const getSnapshot = () => toSnapshot(partialize(store.getPureState()));
54
+ const pushPast = (snapshot) => {
55
+ past.push(snapshot);
56
+ if (past.length > limit) {
57
+ past.shift();
58
+ }
59
+ };
60
+ const baseSetState = store.setState;
61
+ store.setState = (next, updater) => {
62
+ const previous = getSnapshot();
63
+ const result = baseSetState(next, updater);
64
+ if (isTimeTraveling) {
65
+ return result;
66
+ }
67
+ const current = getSnapshot();
68
+ if (!isEqual(previous, current)) {
69
+ pushPast(previous);
70
+ future.length = 0;
71
+ }
72
+ return result;
73
+ };
74
+ const api = {
75
+ undo: () => {
76
+ const previous = past.pop();
77
+ if (!previous) {
78
+ return false;
79
+ }
80
+ const current = getSnapshot();
81
+ future.push(current);
82
+ isTimeTraveling = true;
83
+ try {
84
+ baseSetState(previous);
85
+ } finally {
86
+ isTimeTraveling = false;
87
+ }
88
+ return true;
89
+ },
90
+ redo: () => {
91
+ const next = future.pop();
92
+ if (!next) {
93
+ return false;
94
+ }
95
+ const current = getSnapshot();
96
+ past.push(current);
97
+ isTimeTraveling = true;
98
+ try {
99
+ baseSetState(next);
100
+ } finally {
101
+ isTimeTraveling = false;
102
+ }
103
+ return true;
104
+ },
105
+ clear: () => {
106
+ past.length = 0;
107
+ future.length = 0;
108
+ },
109
+ canUndo: () => past.length > 0,
110
+ canRedo: () => future.length > 0,
111
+ getPast: () => [...past],
112
+ getFuture: () => [...future]
113
+ };
114
+ Object.assign(store, {
115
+ history: api
116
+ });
117
+ return store;
118
+ };
119
+ // Annotate the CommonJS export names for ESM import in node:
120
+ 0 && (module.exports = {
121
+ history
122
+ });
package/dist/index.mjs ADDED
@@ -0,0 +1,95 @@
1
+ // src/index.ts
2
+ var toSnapshot = (state) => {
3
+ if (Array.isArray(state)) {
4
+ return state.map((item) => toSnapshot(item));
5
+ }
6
+ if (typeof state === "object" && state !== null) {
7
+ const next = {};
8
+ for (const key in state) {
9
+ const value = state[key];
10
+ if (typeof value === "function") {
11
+ continue;
12
+ }
13
+ next[key] = toSnapshot(value);
14
+ }
15
+ return next;
16
+ }
17
+ return state;
18
+ };
19
+ var isEqual = (a, b) => JSON.stringify(a) === JSON.stringify(b);
20
+ var history = ({
21
+ limit = 100,
22
+ partialize = (state) => state
23
+ } = {}) => (store) => {
24
+ const past = [];
25
+ const future = [];
26
+ let isTimeTraveling = false;
27
+ const getSnapshot = () => toSnapshot(partialize(store.getPureState()));
28
+ const pushPast = (snapshot) => {
29
+ past.push(snapshot);
30
+ if (past.length > limit) {
31
+ past.shift();
32
+ }
33
+ };
34
+ const baseSetState = store.setState;
35
+ store.setState = (next, updater) => {
36
+ const previous = getSnapshot();
37
+ const result = baseSetState(next, updater);
38
+ if (isTimeTraveling) {
39
+ return result;
40
+ }
41
+ const current = getSnapshot();
42
+ if (!isEqual(previous, current)) {
43
+ pushPast(previous);
44
+ future.length = 0;
45
+ }
46
+ return result;
47
+ };
48
+ const api = {
49
+ undo: () => {
50
+ const previous = past.pop();
51
+ if (!previous) {
52
+ return false;
53
+ }
54
+ const current = getSnapshot();
55
+ future.push(current);
56
+ isTimeTraveling = true;
57
+ try {
58
+ baseSetState(previous);
59
+ } finally {
60
+ isTimeTraveling = false;
61
+ }
62
+ return true;
63
+ },
64
+ redo: () => {
65
+ const next = future.pop();
66
+ if (!next) {
67
+ return false;
68
+ }
69
+ const current = getSnapshot();
70
+ past.push(current);
71
+ isTimeTraveling = true;
72
+ try {
73
+ baseSetState(next);
74
+ } finally {
75
+ isTimeTraveling = false;
76
+ }
77
+ return true;
78
+ },
79
+ clear: () => {
80
+ past.length = 0;
81
+ future.length = 0;
82
+ },
83
+ canUndo: () => past.length > 0,
84
+ canRedo: () => future.length > 0,
85
+ getPast: () => [...past],
86
+ getFuture: () => [...future]
87
+ };
88
+ Object.assign(store, {
89
+ history: api
90
+ });
91
+ return store;
92
+ };
93
+ export {
94
+ history
95
+ };
package/package.json ADDED
@@ -0,0 +1,64 @@
1
+ {
2
+ "name": "@coaction/history",
3
+ "version": "1.0.0",
4
+ "description": "A undo/redo middleware for Coaction",
5
+ "keywords": [
6
+ "state",
7
+ "coaction",
8
+ "middleware",
9
+ "history",
10
+ "undo",
11
+ "redo"
12
+ ],
13
+ "authors": [
14
+ "Michael Lin <unadlib@gmail.com> (https://github.com/unadlib)"
15
+ ],
16
+ "homepage": "https://github.com/unadlib/coaction/tree/main/packages/coaction-history#readme",
17
+ "license": "MIT",
18
+ "main": "dist/index.js",
19
+ "module": "dist/index.mjs",
20
+ "exports": {
21
+ ".": {
22
+ "types": "./dist/index.d.ts",
23
+ "import": "./dist/index.mjs",
24
+ "require": "./dist/index.js",
25
+ "default": "./dist/index.mjs"
26
+ },
27
+ "./package.json": "./package.json"
28
+ },
29
+ "types": "dist/index.d.ts",
30
+ "sideEffects": false,
31
+ "files": [
32
+ "dist"
33
+ ],
34
+ "repository": {
35
+ "type": "git",
36
+ "url": "https://github.com/unadlib/coaction.git",
37
+ "directory": "packages/coaction-history"
38
+ },
39
+ "bugs": {
40
+ "url": "https://github.com/unadlib/coaction/issues"
41
+ },
42
+ "peerDependencies": {
43
+ "coaction": "^0.2.0"
44
+ },
45
+ "peerDependenciesMeta": {
46
+ "coaction": {
47
+ "optional": true
48
+ }
49
+ },
50
+ "devDependencies": {
51
+ "coaction": "^0.2.0"
52
+ },
53
+ "publishConfig": {
54
+ "access": "public",
55
+ "provenance": true
56
+ },
57
+ "author": "unadlib",
58
+ "scripts": {
59
+ "clean": "rm -rf dist",
60
+ "test": "vitest run test",
61
+ "build": "tsup index.ts --format cjs,esm --dts --clean --out-dir dist",
62
+ "dev": "tsup index.ts --format cjs,esm --dts --watch --out-dir dist"
63
+ }
64
+ }