rendx-history-plugin 0.1.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) 2025-present wei.liang (https://github.com/weiliang0121)
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/dist/main.cjs ADDED
@@ -0,0 +1,126 @@
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 __typeError = (msg) => {
7
+ throw TypeError(msg);
8
+ };
9
+ var __export = (target, all) => {
10
+ for (var name in all)
11
+ __defProp(target, name, { get: all[name], enumerable: true });
12
+ };
13
+ var __copyProps = (to, from, except, desc) => {
14
+ if (from && typeof from === "object" || typeof from === "function") {
15
+ for (let key of __getOwnPropNames(from))
16
+ if (!__hasOwnProp.call(to, key) && key !== except)
17
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
18
+ }
19
+ return to;
20
+ };
21
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
22
+ var __accessCheck = (obj, member, msg) => member.has(obj) || __typeError("Cannot " + msg);
23
+ var __privateGet = (obj, member, getter) => (__accessCheck(obj, member, "read from private field"), getter ? getter.call(obj) : member.get(obj));
24
+ var __privateAdd = (obj, member, value) => member.has(obj) ? __typeError("Cannot add the same private member more than once") : member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
25
+ var __privateSet = (obj, member, value, setter) => (__accessCheck(obj, member, "write to private field"), setter ? setter.call(obj, value) : member.set(obj, value), value);
26
+
27
+ // src/main.ts
28
+ var main_exports = {};
29
+ __export(main_exports, {
30
+ historyPlugin: () => historyPlugin
31
+ });
32
+ module.exports = __toCommonJS(main_exports);
33
+
34
+ // src/history.ts
35
+ var DEFAULTS = {
36
+ maxSteps: 50
37
+ };
38
+ var _app, _undoStack, _redoStack, _options;
39
+ var HistoryPlugin = class {
40
+ constructor(options = {}) {
41
+ this.name = "history";
42
+ __privateAdd(this, _app, null);
43
+ __privateAdd(this, _undoStack, []);
44
+ __privateAdd(this, _redoStack, []);
45
+ __privateAdd(this, _options);
46
+ __privateSet(this, _options, { ...DEFAULTS, ...options });
47
+ }
48
+ install(app) {
49
+ __privateSet(this, _app, app);
50
+ }
51
+ /**
52
+ * 保存当前场景快照到撤销栈。
53
+ * 在每次用户操作完成后调用(如拖拽结束、属性修改等)。
54
+ */
55
+ push() {
56
+ const app = __privateGet(this, _app);
57
+ if (!app) return;
58
+ __privateGet(this, _undoStack).push(app.toJSON());
59
+ __privateSet(this, _redoStack, []);
60
+ if (__privateGet(this, _undoStack).length > __privateGet(this, _options).maxSteps) {
61
+ __privateGet(this, _undoStack).shift();
62
+ }
63
+ }
64
+ /**
65
+ * 撤销:恢复到上一步快照。
66
+ * @returns 是否成功执行撤销
67
+ */
68
+ undo() {
69
+ const app = __privateGet(this, _app);
70
+ if (!app || __privateGet(this, _undoStack).length === 0) return false;
71
+ __privateGet(this, _redoStack).push(app.toJSON());
72
+ const snapshot = __privateGet(this, _undoStack).pop();
73
+ app.restoreFromJSON(snapshot);
74
+ app.render();
75
+ return true;
76
+ }
77
+ /**
78
+ * 重做:恢复到最近一次被撤销的快照。
79
+ * @returns 是否成功执行重做
80
+ */
81
+ redo() {
82
+ const app = __privateGet(this, _app);
83
+ if (!app || __privateGet(this, _redoStack).length === 0) return false;
84
+ __privateGet(this, _undoStack).push(app.toJSON());
85
+ const snapshot = __privateGet(this, _redoStack).pop();
86
+ app.restoreFromJSON(snapshot);
87
+ app.render();
88
+ return true;
89
+ }
90
+ /** 是否可撤销 */
91
+ get canUndo() {
92
+ return __privateGet(this, _undoStack).length > 0;
93
+ }
94
+ /** 是否可重做 */
95
+ get canRedo() {
96
+ return __privateGet(this, _redoStack).length > 0;
97
+ }
98
+ /** 撤销栈长度 */
99
+ get undoCount() {
100
+ return __privateGet(this, _undoStack).length;
101
+ }
102
+ /** 重做栈长度 */
103
+ get redoCount() {
104
+ return __privateGet(this, _redoStack).length;
105
+ }
106
+ /** 清空所有历史记录 */
107
+ reset() {
108
+ __privateSet(this, _undoStack, []);
109
+ __privateSet(this, _redoStack, []);
110
+ }
111
+ dispose() {
112
+ this.reset();
113
+ __privateSet(this, _app, null);
114
+ }
115
+ };
116
+ _app = new WeakMap();
117
+ _undoStack = new WeakMap();
118
+ _redoStack = new WeakMap();
119
+ _options = new WeakMap();
120
+ function historyPlugin(options) {
121
+ return new HistoryPlugin(options);
122
+ }
123
+ // Annotate the CommonJS export names for ESM import in node:
124
+ 0 && (module.exports = {
125
+ historyPlugin
126
+ });
@@ -0,0 +1,75 @@
1
+ import { Plugin, App } from 'rendx-engine';
2
+
3
+ interface HistoryPluginOptions {
4
+ /** 最大历史步数,默认 50 */
5
+ maxSteps?: number;
6
+ }
7
+ declare class HistoryPlugin implements Plugin {
8
+ #private;
9
+ name: string;
10
+ constructor(options?: HistoryPluginOptions);
11
+ install(app: App): void;
12
+ /**
13
+ * 保存当前场景快照到撤销栈。
14
+ * 在每次用户操作完成后调用(如拖拽结束、属性修改等)。
15
+ */
16
+ push(): void;
17
+ /**
18
+ * 撤销:恢复到上一步快照。
19
+ * @returns 是否成功执行撤销
20
+ */
21
+ undo(): boolean;
22
+ /**
23
+ * 重做:恢复到最近一次被撤销的快照。
24
+ * @returns 是否成功执行重做
25
+ */
26
+ redo(): boolean;
27
+ /** 是否可撤销 */
28
+ get canUndo(): boolean;
29
+ /** 是否可重做 */
30
+ get canRedo(): boolean;
31
+ /** 撤销栈长度 */
32
+ get undoCount(): number;
33
+ /** 重做栈长度 */
34
+ get redoCount(): number;
35
+ /** 清空所有历史记录 */
36
+ reset(): void;
37
+ dispose(): void;
38
+ }
39
+ /**
40
+ * 创建历史记录插件(Undo / Redo)
41
+ *
42
+ * 基于场景快照实现,每次调用 `push()` 保存当前状态,
43
+ * `undo()` / `redo()` 在快照间来回切换。
44
+ *
45
+ * @example
46
+ * ```ts
47
+ * import {historyPlugin} from 'rendx-history-plugin';
48
+ *
49
+ * const app = new App({width: 800, height: 600});
50
+ * app.mount(container);
51
+ *
52
+ * const history = historyPlugin({maxSteps: 100});
53
+ * app.use(history);
54
+ *
55
+ * // 用户操作前保存快照
56
+ * history.push();
57
+ * // ... 用户执行操作(添加节点、修改属性等)
58
+ *
59
+ * // 撤销
60
+ * history.undo(); // → 恢复到上一步
61
+ *
62
+ * // 重做
63
+ * history.redo(); // → 恢复到撤销前
64
+ *
65
+ * // 快捷键绑定
66
+ * document.addEventListener('keydown', (e) => {
67
+ * if (e.metaKey && e.key === 'z') {
68
+ * e.shiftKey ? history.redo() : history.undo();
69
+ * }
70
+ * });
71
+ * ```
72
+ */
73
+ declare function historyPlugin(options?: HistoryPluginOptions): HistoryPlugin;
74
+
75
+ export { type HistoryPluginOptions, historyPlugin };
package/dist/main.d.ts ADDED
@@ -0,0 +1,75 @@
1
+ import { Plugin, App } from 'rendx-engine';
2
+
3
+ interface HistoryPluginOptions {
4
+ /** 最大历史步数,默认 50 */
5
+ maxSteps?: number;
6
+ }
7
+ declare class HistoryPlugin implements Plugin {
8
+ #private;
9
+ name: string;
10
+ constructor(options?: HistoryPluginOptions);
11
+ install(app: App): void;
12
+ /**
13
+ * 保存当前场景快照到撤销栈。
14
+ * 在每次用户操作完成后调用(如拖拽结束、属性修改等)。
15
+ */
16
+ push(): void;
17
+ /**
18
+ * 撤销:恢复到上一步快照。
19
+ * @returns 是否成功执行撤销
20
+ */
21
+ undo(): boolean;
22
+ /**
23
+ * 重做:恢复到最近一次被撤销的快照。
24
+ * @returns 是否成功执行重做
25
+ */
26
+ redo(): boolean;
27
+ /** 是否可撤销 */
28
+ get canUndo(): boolean;
29
+ /** 是否可重做 */
30
+ get canRedo(): boolean;
31
+ /** 撤销栈长度 */
32
+ get undoCount(): number;
33
+ /** 重做栈长度 */
34
+ get redoCount(): number;
35
+ /** 清空所有历史记录 */
36
+ reset(): void;
37
+ dispose(): void;
38
+ }
39
+ /**
40
+ * 创建历史记录插件(Undo / Redo)
41
+ *
42
+ * 基于场景快照实现,每次调用 `push()` 保存当前状态,
43
+ * `undo()` / `redo()` 在快照间来回切换。
44
+ *
45
+ * @example
46
+ * ```ts
47
+ * import {historyPlugin} from 'rendx-history-plugin';
48
+ *
49
+ * const app = new App({width: 800, height: 600});
50
+ * app.mount(container);
51
+ *
52
+ * const history = historyPlugin({maxSteps: 100});
53
+ * app.use(history);
54
+ *
55
+ * // 用户操作前保存快照
56
+ * history.push();
57
+ * // ... 用户执行操作(添加节点、修改属性等)
58
+ *
59
+ * // 撤销
60
+ * history.undo(); // → 恢复到上一步
61
+ *
62
+ * // 重做
63
+ * history.redo(); // → 恢复到撤销前
64
+ *
65
+ * // 快捷键绑定
66
+ * document.addEventListener('keydown', (e) => {
67
+ * if (e.metaKey && e.key === 'z') {
68
+ * e.shiftKey ? history.redo() : history.undo();
69
+ * }
70
+ * });
71
+ * ```
72
+ */
73
+ declare function historyPlugin(options?: HistoryPluginOptions): HistoryPlugin;
74
+
75
+ export { type HistoryPluginOptions, historyPlugin };
package/dist/main.js ADDED
@@ -0,0 +1,100 @@
1
+ var __typeError = (msg) => {
2
+ throw TypeError(msg);
3
+ };
4
+ var __accessCheck = (obj, member, msg) => member.has(obj) || __typeError("Cannot " + msg);
5
+ var __privateGet = (obj, member, getter) => (__accessCheck(obj, member, "read from private field"), getter ? getter.call(obj) : member.get(obj));
6
+ var __privateAdd = (obj, member, value) => member.has(obj) ? __typeError("Cannot add the same private member more than once") : member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
7
+ var __privateSet = (obj, member, value, setter) => (__accessCheck(obj, member, "write to private field"), setter ? setter.call(obj, value) : member.set(obj, value), value);
8
+
9
+ // src/history.ts
10
+ var DEFAULTS = {
11
+ maxSteps: 50
12
+ };
13
+ var _app, _undoStack, _redoStack, _options;
14
+ var HistoryPlugin = class {
15
+ constructor(options = {}) {
16
+ this.name = "history";
17
+ __privateAdd(this, _app, null);
18
+ __privateAdd(this, _undoStack, []);
19
+ __privateAdd(this, _redoStack, []);
20
+ __privateAdd(this, _options);
21
+ __privateSet(this, _options, { ...DEFAULTS, ...options });
22
+ }
23
+ install(app) {
24
+ __privateSet(this, _app, app);
25
+ }
26
+ /**
27
+ * 保存当前场景快照到撤销栈。
28
+ * 在每次用户操作完成后调用(如拖拽结束、属性修改等)。
29
+ */
30
+ push() {
31
+ const app = __privateGet(this, _app);
32
+ if (!app) return;
33
+ __privateGet(this, _undoStack).push(app.toJSON());
34
+ __privateSet(this, _redoStack, []);
35
+ if (__privateGet(this, _undoStack).length > __privateGet(this, _options).maxSteps) {
36
+ __privateGet(this, _undoStack).shift();
37
+ }
38
+ }
39
+ /**
40
+ * 撤销:恢复到上一步快照。
41
+ * @returns 是否成功执行撤销
42
+ */
43
+ undo() {
44
+ const app = __privateGet(this, _app);
45
+ if (!app || __privateGet(this, _undoStack).length === 0) return false;
46
+ __privateGet(this, _redoStack).push(app.toJSON());
47
+ const snapshot = __privateGet(this, _undoStack).pop();
48
+ app.restoreFromJSON(snapshot);
49
+ app.render();
50
+ return true;
51
+ }
52
+ /**
53
+ * 重做:恢复到最近一次被撤销的快照。
54
+ * @returns 是否成功执行重做
55
+ */
56
+ redo() {
57
+ const app = __privateGet(this, _app);
58
+ if (!app || __privateGet(this, _redoStack).length === 0) return false;
59
+ __privateGet(this, _undoStack).push(app.toJSON());
60
+ const snapshot = __privateGet(this, _redoStack).pop();
61
+ app.restoreFromJSON(snapshot);
62
+ app.render();
63
+ return true;
64
+ }
65
+ /** 是否可撤销 */
66
+ get canUndo() {
67
+ return __privateGet(this, _undoStack).length > 0;
68
+ }
69
+ /** 是否可重做 */
70
+ get canRedo() {
71
+ return __privateGet(this, _redoStack).length > 0;
72
+ }
73
+ /** 撤销栈长度 */
74
+ get undoCount() {
75
+ return __privateGet(this, _undoStack).length;
76
+ }
77
+ /** 重做栈长度 */
78
+ get redoCount() {
79
+ return __privateGet(this, _redoStack).length;
80
+ }
81
+ /** 清空所有历史记录 */
82
+ reset() {
83
+ __privateSet(this, _undoStack, []);
84
+ __privateSet(this, _redoStack, []);
85
+ }
86
+ dispose() {
87
+ this.reset();
88
+ __privateSet(this, _app, null);
89
+ }
90
+ };
91
+ _app = new WeakMap();
92
+ _undoStack = new WeakMap();
93
+ _redoStack = new WeakMap();
94
+ _options = new WeakMap();
95
+ function historyPlugin(options) {
96
+ return new HistoryPlugin(options);
97
+ }
98
+ export {
99
+ historyPlugin
100
+ };
package/package.json ADDED
@@ -0,0 +1,46 @@
1
+ {
2
+ "name": "rendx-history-plugin",
3
+ "version": "0.1.0",
4
+ "description": "Undo/redo history plugin for Rendx engine",
5
+ "license": "MIT",
6
+ "author": "wei.liang (https://github.com/weiliang0121)",
7
+ "type": "module",
8
+ "keywords": [
9
+ "rendx",
10
+ "2d",
11
+ "canvas",
12
+ "rendering",
13
+ "visualization",
14
+ "scene-graph"
15
+ ],
16
+ "homepage": "https://weiliang0121.github.io/rendx/",
17
+ "repository": {
18
+ "type": "git",
19
+ "url": "https://github.com/weiliang0121/rendx.git",
20
+ "directory": "packages/history-plugin"
21
+ },
22
+ "main": "dist/main.cjs",
23
+ "module": "dist/main.js",
24
+ "types": "dist/main.d.ts",
25
+ "exports": {
26
+ ".": {
27
+ "types": "./dist/main.d.ts",
28
+ "import": "./dist/main.js",
29
+ "require": "./dist/main.cjs"
30
+ }
31
+ },
32
+ "dependencies": {
33
+ "rendx-engine": "^0.2.0"
34
+ },
35
+ "sideEffects": false,
36
+ "files": [
37
+ "dist"
38
+ ],
39
+ "publishConfig": {
40
+ "access": "public"
41
+ },
42
+ "scripts": {
43
+ "build": "tsup",
44
+ "dev": "tsup --watch"
45
+ }
46
+ }