@hprint/core 0.0.1-alpha.3 → 0.0.1-alpha.4

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.
@@ -1,232 +1,232 @@
1
- import { fabric } from 'fabric';
2
-
3
- fabric.Canvas.prototype.initialize = (function (originalFn) {
4
- return function (...args) {
5
- originalFn.call(this, ...args);
6
- this._historyInit();
7
- return this;
8
- };
9
- })(fabric.Canvas.prototype.initialize);
10
-
11
- /**
12
- * Override the dispose function for the _historyDispose();
13
- */
14
- fabric.Canvas.prototype.dispose = (function (originalFn) {
15
- return function (...args) {
16
- originalFn.call(this, ...args);
17
- this._historyDispose();
18
- return this;
19
- };
20
- })(fabric.Canvas.prototype.dispose);
21
-
22
- /**
23
- * Returns current state of the string of the canvas
24
- */
25
- fabric.Canvas.prototype._historyNext = function () {
26
- return JSON.stringify(this.toDatalessJSON(this.extraProps));
27
- };
28
-
29
- /**
30
- * Returns an object with fabricjs event mappings
31
- */
32
- fabric.Canvas.prototype._historyEvents = function () {
33
- return {
34
- 'object:added': (e) => this._historySaveAction(e),
35
- 'object:removed': (e) => this._historySaveAction(e),
36
- 'object:modified': (e) => this._historySaveAction(e),
37
- 'object:skewing': (e) => this._historySaveAction(e),
38
- };
39
- };
40
-
41
- /**
42
- * Initialization of the plugin
43
- */
44
- fabric.Canvas.prototype._historyInit = function () {
45
- this.historyStack = [];
46
- this.historyIndex = 0;
47
- this.historyMaxLength = 100;
48
- this.extraProps = [
49
- 'id',
50
- 'gradientAngle',
51
- 'selectable',
52
- 'hasControls',
53
- 'linkData',
54
- 'editable',
55
- 'extensionType',
56
- 'extension',
57
- ];
58
- this.historyNextState = this._historyNext();
59
- // 需要两次操作的标记,为true时表示当前操作记录为最新记录,需要撤销两步,因为最顶层的是当前的最新记录,undo一次后后置为false
60
- this.isLatestHistoryState = true;
61
- // 正在读取历史记录的标记,为 true 时不允许 undo/redo
62
- this.isLoadingHistory = false;
63
-
64
- this.on(this._historyEvents());
65
- };
66
-
67
- /**
68
- * Remove the custom event listeners
69
- */
70
- fabric.Canvas.prototype._historyDispose = function () {
71
- this.off(this._historyEvents());
72
- };
73
-
74
- /**
75
- * It pushes the state of the canvas into history stack
76
- */
77
- fabric.Canvas.prototype._historySaveAction = function (e) {
78
- if (this.historyProcessing) return;
79
- if (!e || (e.target && !e.target.excludeFromExport)) {
80
- const json = this._historyNext();
81
- // 当前操作记录非最新记录,更新记录前需要校正历史索引,不然会丢失一个记录(undo时撤销了两次记录)。理论上不会超出历史记录上限,不过还是加了限制
82
- !this.isLatestHistoryState &&
83
- (this.isLatestHistoryState = true) &&
84
- this.historyIndex < this.historyMaxLength &&
85
- this.historyIndex++;
86
- // 每次的最新操作都要清空历史索引之后的记录,防止redo旧记录,不然可能会redo之前某个阶段的操作记录
87
- this.historyStack.length > this.historyIndex &&
88
- this.historyStack.splice(this.historyIndex);
89
- // 最多保存 historyMaxLength 条记录
90
- if (this.historyIndex >= this.historyMaxLength)
91
- this.historyStack.shift();
92
- this.historyIndex < this.historyMaxLength && this.historyIndex++;
93
- this.historyStack.push(json);
94
- this.historyNextState = this._historyNext();
95
- this.fire('history:append', { json });
96
- }
97
- };
98
-
99
- /**
100
- * Undo to latest history.
101
- * Pop the latest state of the history. Re-render.
102
- * Also, pushes into redo history.
103
- */
104
- fabric.Canvas.prototype.undo = function (callback) {
105
- if (this.isLoadingHistory) return;
106
- if (this.historyIndex <= 0) return;
107
- // The undo process will render the new states of the objects
108
- // Therefore, object:added and object:modified events will triggered again
109
- // To ignore those events, we are setting a flag.
110
- this.historyProcessing = true;
111
-
112
- // 当前操作记录为最新记录,需要撤销两步,因为最顶层的是当前的最新记录
113
- this.isLatestHistoryState &&
114
- this.historyIndex-- &&
115
- (this.isLatestHistoryState = false);
116
- const history = this.historyStack[--this.historyIndex];
117
- if (history) {
118
- // Push the current state to the redo history
119
- this.historyNextState = history;
120
- this._loadHistory(history, 'history:undo', callback);
121
- } else {
122
- console.log(1111);
123
- this.historyIndex < 0 && (this.historyIndex = 0);
124
- this.historyProcessing = false;
125
- }
126
- };
127
-
128
- /**
129
- * Redo to latest undo history.
130
- */
131
- fabric.Canvas.prototype.redo = function (callback) {
132
- if (this.isLoadingHistory) return;
133
- if (this.historyIndex >= this.historyStack.length) return;
134
- // The undo process will render the new states of the objects
135
- // Therefore, object:added and object:modified events will triggered again
136
- // To ignore those events, we are setting a flag.
137
- this.historyProcessing = true;
138
- // 当前操作记录不是最新记录(被撤销过),需要恢复两步,抵消最初撤销时撤销两步的操作
139
- !this.isLatestHistoryState &&
140
- ++this.historyIndex &&
141
- (this.isLatestHistoryState = true);
142
- const history = this.historyStack[this.historyIndex];
143
- if (history) {
144
- // Every redo action is actually a new action to the undo history
145
- this.historyNextState = history;
146
- this._loadHistory(history, 'history:redo', callback);
147
- this.historyIndex++;
148
- } else {
149
- this.historyProcessing = false;
150
- }
151
- };
152
-
153
- // loadFromJSON 是异步操作,所以通过 isLoadingHistory = true 表示历史读取中,不可 undo/redo,
154
- // 不然当页面复杂且快速 undo/redo 多次后,可能会在之前的历史上 redo/undo
155
- fabric.Canvas.prototype._loadHistory = function (history, event, callback) {
156
- this.isLoadingHistory = true;
157
- var that = this;
158
-
159
- // 需要把历史记录中的 workspace 的 evented 属性设置为 false,否则会导致历史记录恢复后,鼠标悬浮 workspace 出现可操作的样式
160
- const workspaceHistory = history.objects?.find(
161
- (item) => item.id === 'workspace'
162
- );
163
- workspaceHistory && (workspaceHistory.evented = false);
164
-
165
- this.loadFromJSON(history, function () {
166
- that.renderAll();
167
- that.fire(event);
168
- that.historyProcessing = false;
169
- that.isLoadingHistory = false;
170
-
171
- if (callback && typeof callback === 'function') callback();
172
- });
173
- };
174
-
175
- /**
176
- * Clear undo and redo history stacks
177
- */
178
- fabric.Canvas.prototype.clearHistory = function (type) {
179
- const one = this.historyStack.pop();
180
- if (!type || !one) {
181
- this.historyStack = [];
182
- this.historyIndex = 0;
183
- this.fire('history:clear');
184
- } else {
185
- this.historyStack = [one];
186
- this.historyIndex = 1;
187
- this.fire('history:clear');
188
- }
189
- this.isLatestHistoryState = true;
190
- };
191
-
192
- fabric.Canvas.prototype.clearUndo = function () {
193
- this.historyStack.splice(this.historyIndex);
194
- };
195
-
196
- // 如果在做一些操作之后,需要撤销上一步的操作并刷新历史记录(想在监听modified事件后做些额外的操作并记录操作后的历史),可以调用这个方法
197
- fabric.Canvas.prototype.refreshHistory = function () {
198
- this.historyProcessing = false;
199
- this.historyStack.splice(--this.historyIndex);
200
- this._historySaveAction();
201
- };
202
-
203
- /**
204
- * On the history
205
- */
206
- fabric.Canvas.prototype.onHistory = function () {
207
- this.historyProcessing = false;
208
-
209
- this._historySaveAction();
210
- };
211
-
212
- /**
213
- * Check if there are actions that can be undone
214
- */
215
-
216
- fabric.Canvas.prototype.canUndo = function () {
217
- return this.historyIndex > 0;
218
- };
219
-
220
- /**
221
- * Check if there are actions that can be redone
222
- */
223
- fabric.Canvas.prototype.canRedo = function () {
224
- return this.historyStack.length > this.historyIndex;
225
- };
226
-
227
- /**
228
- * Off the history
229
- */
230
- fabric.Canvas.prototype.offHistory = function () {
231
- this.historyProcessing = true;
232
- };
1
+ import { fabric } from 'fabric';
2
+
3
+ fabric.Canvas.prototype.initialize = (function (originalFn) {
4
+ return function (...args) {
5
+ originalFn.call(this, ...args);
6
+ this._historyInit();
7
+ return this;
8
+ };
9
+ })(fabric.Canvas.prototype.initialize);
10
+
11
+ /**
12
+ * Override the dispose function for the _historyDispose();
13
+ */
14
+ fabric.Canvas.prototype.dispose = (function (originalFn) {
15
+ return function (...args) {
16
+ originalFn.call(this, ...args);
17
+ this._historyDispose();
18
+ return this;
19
+ };
20
+ })(fabric.Canvas.prototype.dispose);
21
+
22
+ /**
23
+ * Returns current state of the string of the canvas
24
+ */
25
+ fabric.Canvas.prototype._historyNext = function () {
26
+ return JSON.stringify(this.toDatalessJSON(this.extraProps));
27
+ };
28
+
29
+ /**
30
+ * Returns an object with fabricjs event mappings
31
+ */
32
+ fabric.Canvas.prototype._historyEvents = function () {
33
+ return {
34
+ 'object:added': (e) => this._historySaveAction(e),
35
+ 'object:removed': (e) => this._historySaveAction(e),
36
+ 'object:modified': (e) => this._historySaveAction(e),
37
+ 'object:skewing': (e) => this._historySaveAction(e),
38
+ };
39
+ };
40
+
41
+ /**
42
+ * Initialization of the plugin
43
+ */
44
+ fabric.Canvas.prototype._historyInit = function () {
45
+ this.historyStack = [];
46
+ this.historyIndex = 0;
47
+ this.historyMaxLength = 100;
48
+ this.extraProps = [
49
+ 'id',
50
+ 'gradientAngle',
51
+ 'selectable',
52
+ 'hasControls',
53
+ 'linkData',
54
+ 'editable',
55
+ 'extensionType',
56
+ 'extension',
57
+ ];
58
+ this.historyNextState = this._historyNext();
59
+ // 需要两次操作的标记,为true时表示当前操作记录为最新记录,需要撤销两步,因为最顶层的是当前的最新记录,undo一次后后置为false
60
+ this.isLatestHistoryState = true;
61
+ // 正在读取历史记录的标记,为 true 时不允许 undo/redo
62
+ this.isLoadingHistory = false;
63
+
64
+ this.on(this._historyEvents());
65
+ };
66
+
67
+ /**
68
+ * Remove the custom event listeners
69
+ */
70
+ fabric.Canvas.prototype._historyDispose = function () {
71
+ this.off(this._historyEvents());
72
+ };
73
+
74
+ /**
75
+ * It pushes the state of the canvas into history stack
76
+ */
77
+ fabric.Canvas.prototype._historySaveAction = function (e) {
78
+ if (this.historyProcessing) return;
79
+ if (!e || (e.target && !e.target.excludeFromExport)) {
80
+ const json = this._historyNext();
81
+ // 当前操作记录非最新记录,更新记录前需要校正历史索引,不然会丢失一个记录(undo时撤销了两次记录)。理论上不会超出历史记录上限,不过还是加了限制
82
+ !this.isLatestHistoryState &&
83
+ (this.isLatestHistoryState = true) &&
84
+ this.historyIndex < this.historyMaxLength &&
85
+ this.historyIndex++;
86
+ // 每次的最新操作都要清空历史索引之后的记录,防止redo旧记录,不然可能会redo之前某个阶段的操作记录
87
+ this.historyStack.length > this.historyIndex &&
88
+ this.historyStack.splice(this.historyIndex);
89
+ // 最多保存 historyMaxLength 条记录
90
+ if (this.historyIndex >= this.historyMaxLength)
91
+ this.historyStack.shift();
92
+ this.historyIndex < this.historyMaxLength && this.historyIndex++;
93
+ this.historyStack.push(json);
94
+ this.historyNextState = this._historyNext();
95
+ this.fire('history:append', { json });
96
+ }
97
+ };
98
+
99
+ /**
100
+ * Undo to latest history.
101
+ * Pop the latest state of the history. Re-render.
102
+ * Also, pushes into redo history.
103
+ */
104
+ fabric.Canvas.prototype.undo = function (callback) {
105
+ if (this.isLoadingHistory) return;
106
+ if (this.historyIndex <= 0) return;
107
+ // The undo process will render the new states of the objects
108
+ // Therefore, object:added and object:modified events will triggered again
109
+ // To ignore those events, we are setting a flag.
110
+ this.historyProcessing = true;
111
+
112
+ // 当前操作记录为最新记录,需要撤销两步,因为最顶层的是当前的最新记录
113
+ this.isLatestHistoryState &&
114
+ this.historyIndex-- &&
115
+ (this.isLatestHistoryState = false);
116
+ const history = this.historyStack[--this.historyIndex];
117
+ if (history) {
118
+ // Push the current state to the redo history
119
+ this.historyNextState = history;
120
+ this._loadHistory(history, 'history:undo', callback);
121
+ } else {
122
+ console.log(1111);
123
+ this.historyIndex < 0 && (this.historyIndex = 0);
124
+ this.historyProcessing = false;
125
+ }
126
+ };
127
+
128
+ /**
129
+ * Redo to latest undo history.
130
+ */
131
+ fabric.Canvas.prototype.redo = function (callback) {
132
+ if (this.isLoadingHistory) return;
133
+ if (this.historyIndex >= this.historyStack.length) return;
134
+ // The undo process will render the new states of the objects
135
+ // Therefore, object:added and object:modified events will triggered again
136
+ // To ignore those events, we are setting a flag.
137
+ this.historyProcessing = true;
138
+ // 当前操作记录不是最新记录(被撤销过),需要恢复两步,抵消最初撤销时撤销两步的操作
139
+ !this.isLatestHistoryState &&
140
+ ++this.historyIndex &&
141
+ (this.isLatestHistoryState = true);
142
+ const history = this.historyStack[this.historyIndex];
143
+ if (history) {
144
+ // Every redo action is actually a new action to the undo history
145
+ this.historyNextState = history;
146
+ this._loadHistory(history, 'history:redo', callback);
147
+ this.historyIndex++;
148
+ } else {
149
+ this.historyProcessing = false;
150
+ }
151
+ };
152
+
153
+ // loadFromJSON 是异步操作,所以通过 isLoadingHistory = true 表示历史读取中,不可 undo/redo,
154
+ // 不然当页面复杂且快速 undo/redo 多次后,可能会在之前的历史上 redo/undo
155
+ fabric.Canvas.prototype._loadHistory = function (history, event, callback) {
156
+ this.isLoadingHistory = true;
157
+ var that = this;
158
+
159
+ // 需要把历史记录中的 workspace 的 evented 属性设置为 false,否则会导致历史记录恢复后,鼠标悬浮 workspace 出现可操作的样式
160
+ const workspaceHistory = history.objects?.find(
161
+ (item) => item.id === 'workspace'
162
+ );
163
+ workspaceHistory && (workspaceHistory.evented = false);
164
+
165
+ this.loadFromJSON(history, function () {
166
+ that.renderAll();
167
+ that.fire(event);
168
+ that.historyProcessing = false;
169
+ that.isLoadingHistory = false;
170
+
171
+ if (callback && typeof callback === 'function') callback();
172
+ });
173
+ };
174
+
175
+ /**
176
+ * Clear undo and redo history stacks
177
+ */
178
+ fabric.Canvas.prototype.clearHistory = function (type) {
179
+ const one = this.historyStack.pop();
180
+ if (!type || !one) {
181
+ this.historyStack = [];
182
+ this.historyIndex = 0;
183
+ this.fire('history:clear');
184
+ } else {
185
+ this.historyStack = [one];
186
+ this.historyIndex = 1;
187
+ this.fire('history:clear');
188
+ }
189
+ this.isLatestHistoryState = true;
190
+ };
191
+
192
+ fabric.Canvas.prototype.clearUndo = function () {
193
+ this.historyStack.splice(this.historyIndex);
194
+ };
195
+
196
+ // 如果在做一些操作之后,需要撤销上一步的操作并刷新历史记录(想在监听modified事件后做些额外的操作并记录操作后的历史),可以调用这个方法
197
+ fabric.Canvas.prototype.refreshHistory = function () {
198
+ this.historyProcessing = false;
199
+ this.historyStack.splice(--this.historyIndex);
200
+ this._historySaveAction();
201
+ };
202
+
203
+ /**
204
+ * On the history
205
+ */
206
+ fabric.Canvas.prototype.onHistory = function () {
207
+ this.historyProcessing = false;
208
+
209
+ this._historySaveAction();
210
+ };
211
+
212
+ /**
213
+ * Check if there are actions that can be undone
214
+ */
215
+
216
+ fabric.Canvas.prototype.canUndo = function () {
217
+ return this.historyIndex > 0;
218
+ };
219
+
220
+ /**
221
+ * Check if there are actions that can be redone
222
+ */
223
+ fabric.Canvas.prototype.canRedo = function () {
224
+ return this.historyStack.length > this.historyIndex;
225
+ };
226
+
227
+ /**
228
+ * Off the history
229
+ */
230
+ fabric.Canvas.prototype.offHistory = function () {
231
+ this.historyProcessing = true;
232
+ };