@meta2d/core 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.
Files changed (202) hide show
  1. package/index.d.ts +9 -0
  2. package/index.js +10 -0
  3. package/index.js.map +1 -0
  4. package/package.json +39 -0
  5. package/src/canvas/canvas.d.ts +399 -0
  6. package/src/canvas/canvas.js +5920 -0
  7. package/src/canvas/canvas.js.map +1 -0
  8. package/src/canvas/canvasImage.d.ts +25 -0
  9. package/src/canvas/canvasImage.js +343 -0
  10. package/src/canvas/canvasImage.js.map +1 -0
  11. package/src/canvas/index.d.ts +2 -0
  12. package/src/canvas/index.js +3 -0
  13. package/src/canvas/index.js.map +1 -0
  14. package/src/canvas/magnifierCanvas.d.ts +20 -0
  15. package/src/canvas/magnifierCanvas.js +99 -0
  16. package/src/canvas/magnifierCanvas.js.map +1 -0
  17. package/src/canvas/offscreen.d.ts +2 -0
  18. package/src/canvas/offscreen.js +14 -0
  19. package/src/canvas/offscreen.js.map +1 -0
  20. package/src/core.d.ts +348 -0
  21. package/src/core.js +2465 -0
  22. package/src/core.js.map +1 -0
  23. package/src/data.d.ts +34 -0
  24. package/src/data.js +83 -0
  25. package/src/data.js.map +1 -0
  26. package/src/diagrams/arrow.d.ts +4 -0
  27. package/src/diagrams/arrow.js +47 -0
  28. package/src/diagrams/arrow.js.map +1 -0
  29. package/src/diagrams/circle.d.ts +2 -0
  30. package/src/diagrams/circle.js +9 -0
  31. package/src/diagrams/circle.js.map +1 -0
  32. package/src/diagrams/cloud.d.ts +2 -0
  33. package/src/diagrams/cloud.js +12 -0
  34. package/src/diagrams/cloud.js.map +1 -0
  35. package/src/diagrams/cube.d.ts +2 -0
  36. package/src/diagrams/cube.js +70 -0
  37. package/src/diagrams/cube.js.map +1 -0
  38. package/src/diagrams/diamond.d.ts +2 -0
  39. package/src/diagrams/diamond.js +13 -0
  40. package/src/diagrams/diamond.js.map +1 -0
  41. package/src/diagrams/file.d.ts +2 -0
  42. package/src/diagrams/file.js +18 -0
  43. package/src/diagrams/file.js.map +1 -0
  44. package/src/diagrams/gif.d.ts +5 -0
  45. package/src/diagrams/gif.js +80 -0
  46. package/src/diagrams/gif.js.map +1 -0
  47. package/src/diagrams/hexagon.d.ts +2 -0
  48. package/src/diagrams/hexagon.js +55 -0
  49. package/src/diagrams/hexagon.js.map +1 -0
  50. package/src/diagrams/iframe.d.ts +5 -0
  51. package/src/diagrams/iframe.js +52 -0
  52. package/src/diagrams/iframe.js.map +1 -0
  53. package/src/diagrams/index.d.ts +67 -0
  54. package/src/diagrams/index.js +72 -0
  55. package/src/diagrams/index.js.map +1 -0
  56. package/src/diagrams/line/curve.d.ts +16 -0
  57. package/src/diagrams/line/curve.js +233 -0
  58. package/src/diagrams/line/curve.js.map +1 -0
  59. package/src/diagrams/line/index.d.ts +4 -0
  60. package/src/diagrams/line/index.js +5 -0
  61. package/src/diagrams/line/index.js.map +1 -0
  62. package/src/diagrams/line/line.d.ts +42 -0
  63. package/src/diagrams/line/line.js +419 -0
  64. package/src/diagrams/line/line.js.map +1 -0
  65. package/src/diagrams/line/polyline.d.ts +10 -0
  66. package/src/diagrams/line/polyline.js +622 -0
  67. package/src/diagrams/line/polyline.js.map +1 -0
  68. package/src/diagrams/line/smooth.d.ts +3 -0
  69. package/src/diagrams/line/smooth.js +174 -0
  70. package/src/diagrams/line/smooth.js.map +1 -0
  71. package/src/diagrams/message.d.ts +2 -0
  72. package/src/diagrams/message.js +15 -0
  73. package/src/diagrams/message.js.map +1 -0
  74. package/src/diagrams/mindLine.d.ts +3 -0
  75. package/src/diagrams/mindLine.js +31 -0
  76. package/src/diagrams/mindLine.js.map +1 -0
  77. package/src/diagrams/mindNode.d.ts +3 -0
  78. package/src/diagrams/mindNode.js +189 -0
  79. package/src/diagrams/mindNode.js.map +1 -0
  80. package/src/diagrams/pentagon.d.ts +3 -0
  81. package/src/diagrams/pentagon.js +46 -0
  82. package/src/diagrams/pentagon.js.map +1 -0
  83. package/src/diagrams/pentagram.d.ts +3 -0
  84. package/src/diagrams/pentagram.js +77 -0
  85. package/src/diagrams/pentagram.js.map +1 -0
  86. package/src/diagrams/people.d.ts +2 -0
  87. package/src/diagrams/people.js +19 -0
  88. package/src/diagrams/people.js.map +1 -0
  89. package/src/diagrams/rectangle.d.ts +3 -0
  90. package/src/diagrams/rectangle.js +26 -0
  91. package/src/diagrams/rectangle.js.map +1 -0
  92. package/src/diagrams/svg/parse.d.ts +15 -0
  93. package/src/diagrams/svg/parse.js +326 -0
  94. package/src/diagrams/svg/parse.js.map +1 -0
  95. package/src/diagrams/svgPath.d.ts +2 -0
  96. package/src/diagrams/svgPath.js +30 -0
  97. package/src/diagrams/svgPath.js.map +1 -0
  98. package/src/diagrams/triangle.d.ts +3 -0
  99. package/src/diagrams/triangle.js +41 -0
  100. package/src/diagrams/triangle.js.map +1 -0
  101. package/src/diagrams/video.d.ts +5 -0
  102. package/src/diagrams/video.js +143 -0
  103. package/src/diagrams/video.js.map +1 -0
  104. package/src/event/event.d.ts +45 -0
  105. package/src/event/event.js +17 -0
  106. package/src/event/event.js.map +1 -0
  107. package/src/event/index.d.ts +1 -0
  108. package/src/event/index.js +2 -0
  109. package/src/event/index.js.map +1 -0
  110. package/src/map/index.d.ts +1 -0
  111. package/src/map/index.js +2 -0
  112. package/src/map/index.js.map +1 -0
  113. package/src/map/map.d.ts +20 -0
  114. package/src/map/map.js +143 -0
  115. package/src/map/map.js.map +1 -0
  116. package/src/options.d.ts +70 -0
  117. package/src/options.js +61 -0
  118. package/src/options.js.map +1 -0
  119. package/src/pen/arrow.d.ts +4 -0
  120. package/src/pen/arrow.js +186 -0
  121. package/src/pen/arrow.js.map +1 -0
  122. package/src/pen/index.d.ts +5 -0
  123. package/src/pen/index.js +6 -0
  124. package/src/pen/index.js.map +1 -0
  125. package/src/pen/math.d.ts +28 -0
  126. package/src/pen/math.js +304 -0
  127. package/src/pen/math.js.map +1 -0
  128. package/src/pen/model.d.ts +354 -0
  129. package/src/pen/model.js +172 -0
  130. package/src/pen/model.js.map +1 -0
  131. package/src/pen/render.d.ts +132 -0
  132. package/src/pen/render.js +1908 -0
  133. package/src/pen/render.js.map +1 -0
  134. package/src/pen/text.d.ts +7 -0
  135. package/src/pen/text.js +311 -0
  136. package/src/pen/text.js.map +1 -0
  137. package/src/pen/utils.d.ts +2 -0
  138. package/src/pen/utils.js +41 -0
  139. package/src/pen/utils.js.map +1 -0
  140. package/src/point/index.d.ts +1 -0
  141. package/src/point/index.js +2 -0
  142. package/src/point/index.js.map +1 -0
  143. package/src/point/point.d.ts +53 -0
  144. package/src/point/point.js +119 -0
  145. package/src/point/point.js.map +1 -0
  146. package/src/rect/index.d.ts +1 -0
  147. package/src/rect/index.js +2 -0
  148. package/src/rect/index.js.map +1 -0
  149. package/src/rect/rect.d.ts +49 -0
  150. package/src/rect/rect.js +446 -0
  151. package/src/rect/rect.js.map +1 -0
  152. package/src/rect/triangle.d.ts +2 -0
  153. package/src/rect/triangle.js +10 -0
  154. package/src/rect/triangle.js.map +1 -0
  155. package/src/scroll/index.d.ts +1 -0
  156. package/src/scroll/index.js +2 -0
  157. package/src/scroll/index.js.map +1 -0
  158. package/src/scroll/scroll.d.ts +32 -0
  159. package/src/scroll/scroll.js +193 -0
  160. package/src/scroll/scroll.js.map +1 -0
  161. package/src/store/global.d.ts +25 -0
  162. package/src/store/global.js +18 -0
  163. package/src/store/global.js.map +1 -0
  164. package/src/store/index.d.ts +2 -0
  165. package/src/store/index.js +3 -0
  166. package/src/store/index.js.map +1 -0
  167. package/src/store/store.d.ts +104 -0
  168. package/src/store/store.js +73 -0
  169. package/src/store/store.js.map +1 -0
  170. package/src/tooltip/index.d.ts +1 -0
  171. package/src/tooltip/index.js +2 -0
  172. package/src/tooltip/index.js.map +1 -0
  173. package/src/tooltip/tooltip.d.ts +40 -0
  174. package/src/tooltip/tooltip.js +162 -0
  175. package/src/tooltip/tooltip.js.map +1 -0
  176. package/src/utils/browser.d.ts +1 -0
  177. package/src/utils/browser.js +4 -0
  178. package/src/utils/browser.js.map +1 -0
  179. package/src/utils/clone.d.ts +8 -0
  180. package/src/utils/clone.js +73 -0
  181. package/src/utils/clone.js.map +1 -0
  182. package/src/utils/color.d.ts +3 -0
  183. package/src/utils/color.js +126 -0
  184. package/src/utils/color.js.map +1 -0
  185. package/src/utils/error.d.ts +2 -0
  186. package/src/utils/error.js +6 -0
  187. package/src/utils/error.js.map +1 -0
  188. package/src/utils/file.d.ts +2 -0
  189. package/src/utils/file.js +84 -0
  190. package/src/utils/file.js.map +1 -0
  191. package/src/utils/index.d.ts +7 -0
  192. package/src/utils/index.js +8 -0
  193. package/src/utils/index.js.map +1 -0
  194. package/src/utils/math.d.ts +18 -0
  195. package/src/utils/math.js +141 -0
  196. package/src/utils/math.js.map +1 -0
  197. package/src/utils/padding.d.ts +7 -0
  198. package/src/utils/padding.js +47 -0
  199. package/src/utils/padding.js.map +1 -0
  200. package/src/utils/uuid.d.ts +4 -0
  201. package/src/utils/uuid.js +13 -0
  202. package/src/utils/uuid.js.map +1 -0
package/src/core.js ADDED
@@ -0,0 +1,2465 @@
1
+ var __assign = (this && this.__assign) || function () {
2
+ __assign = Object.assign || function(t) {
3
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
4
+ s = arguments[i];
5
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
6
+ t[p] = s[p];
7
+ }
8
+ return t;
9
+ };
10
+ return __assign.apply(this, arguments);
11
+ };
12
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
13
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
14
+ return new (P || (P = Promise))(function (resolve, reject) {
15
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
16
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
17
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
18
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
19
+ });
20
+ };
21
+ var __generator = (this && this.__generator) || function (thisArg, body) {
22
+ var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
23
+ return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
24
+ function verb(n) { return function (v) { return step([n, v]); }; }
25
+ function step(op) {
26
+ if (f) throw new TypeError("Generator is already executing.");
27
+ while (_) try {
28
+ if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
29
+ if (y = 0, t) op = [op[0] & 2, t.value];
30
+ switch (op[0]) {
31
+ case 0: case 1: t = op; break;
32
+ case 4: _.label++; return { value: op[1], done: false };
33
+ case 5: _.label++; y = op[1]; op = [0]; continue;
34
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
35
+ default:
36
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
37
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
38
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
39
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
40
+ if (t[2]) _.ops.pop();
41
+ _.trys.pop(); continue;
42
+ }
43
+ op = body.call(thisArg, _);
44
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
45
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
46
+ }
47
+ };
48
+ var __values = (this && this.__values) || function(o) {
49
+ var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
50
+ if (m) return m.call(o);
51
+ if (o && typeof o.length === "number") return {
52
+ next: function () {
53
+ if (o && i >= o.length) o = void 0;
54
+ return { value: o && o[i++], done: !o };
55
+ }
56
+ };
57
+ throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
58
+ };
59
+ var __read = (this && this.__read) || function (o, n) {
60
+ var m = typeof Symbol === "function" && o[Symbol.iterator];
61
+ if (!m) return o;
62
+ var i = m.call(o), r, ar = [], e;
63
+ try {
64
+ while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
65
+ }
66
+ catch (error) { e = { error: error }; }
67
+ finally {
68
+ try {
69
+ if (r && !r.done && (m = i["return"])) m.call(i);
70
+ }
71
+ finally { if (e) throw e.error; }
72
+ }
73
+ return ar;
74
+ };
75
+ var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
76
+ if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
77
+ if (ar || !(i in from)) {
78
+ if (!ar) ar = Array.prototype.slice.call(from, 0, i);
79
+ ar[i] = from[i];
80
+ }
81
+ }
82
+ return to.concat(ar || Array.prototype.slice.call(from));
83
+ };
84
+ import { commonAnchors, commonPens, cube } from './diagrams';
85
+ import { Canvas } from './canvas';
86
+ import { calcInView, calcTextDrawRect, calcTextLines, calcTextRect, facePen, formatAttrs, getAllChildren, getFromAnchor, getParent, getToAnchor, getWords, LockState, PenType, renderPenRaw, setElemPosition, connectLine, nearestAnchor, setChildValue, } from './pen';
87
+ import { rotatePoint } from './point';
88
+ import { clearStore, EditType, globalStore, register, registerAnchors, registerCanvasDraw, useStore, } from './store';
89
+ import { formatPadding, s8, valueInArray, valueInRange, } from './utils';
90
+ import { calcCenter, calcRelativeRect, getRect } from './rect';
91
+ import { deepClone } from './utils/clone';
92
+ import { EventAction } from './event';
93
+ import { ViewMap } from './map';
94
+ import * as mqtt from 'mqtt/dist/mqtt.min.js';
95
+ import pkg from '../package.json';
96
+ import { lockedError } from './utils/error';
97
+ var Meta2d = /** @class */ (function () {
98
+ function Meta2d(parent, opts) {
99
+ var _this = this;
100
+ if (opts === void 0) { opts = {}; }
101
+ this.events = {};
102
+ this.facePen = facePen;
103
+ this.getWords = getWords;
104
+ this.calcTextLines = calcTextLines;
105
+ this.calcTextRect = calcTextRect;
106
+ this.calcTextDrawRect = calcTextDrawRect;
107
+ this.register = register;
108
+ this.registerCanvasDraw = registerCanvasDraw;
109
+ this.registerAnchors = registerAnchors;
110
+ this.onEvent = function (eventName, e) {
111
+ switch (eventName) {
112
+ case 'add':
113
+ {
114
+ e.forEach(function (pen) {
115
+ var _a;
116
+ (_a = pen.onAdd) === null || _a === void 0 ? void 0 : _a.call(pen, pen);
117
+ });
118
+ }
119
+ _this.onSizeUpdate();
120
+ break;
121
+ case 'enter':
122
+ e && e.onMouseEnter && e.onMouseEnter(e, _this.canvas.mousePos);
123
+ _this.store.data.locked && _this.doEvent(e, eventName);
124
+ break;
125
+ case 'leave':
126
+ e && e.onMouseLeave && e.onMouseLeave(e, _this.canvas.mousePos);
127
+ _this.store.data.locked && _this.doEvent(e, eventName);
128
+ break;
129
+ case 'active':
130
+ case 'inactive':
131
+ {
132
+ _this.store.data.locked &&
133
+ e.forEach(function (pen) {
134
+ _this.doEvent(pen, eventName);
135
+ });
136
+ }
137
+ break;
138
+ case 'click':
139
+ e.pen && e.pen.onClick && e.pen.onClick(e.pen, _this.canvas.mousePos);
140
+ _this.store.data.locked && e.pen && _this.doEvent(e.pen, eventName);
141
+ break;
142
+ case 'mousedown':
143
+ e.pen &&
144
+ e.pen.onMouseDown &&
145
+ e.pen.onMouseDown(e.pen, _this.canvas.mousePos);
146
+ _this.store.data.locked && e.pen && _this.doEvent(e.pen, eventName);
147
+ break;
148
+ case 'mouseup':
149
+ e.pen &&
150
+ e.pen.onMouseUp &&
151
+ e.pen.onMouseUp(e.pen, _this.canvas.mousePos);
152
+ _this.store.data.locked && e.pen && _this.doEvent(e.pen, eventName);
153
+ break;
154
+ case 'dblclick':
155
+ _this.store.data.locked && e.pen && _this.doEvent(e.pen, eventName);
156
+ break;
157
+ case 'valueUpdate':
158
+ _this.store.data.locked && _this.doEvent(e, eventName);
159
+ _this.canvas.tooltip.updateText(e);
160
+ break;
161
+ case 'update':
162
+ case 'delete':
163
+ case 'translatePens':
164
+ case 'rotatePens':
165
+ case 'resizePens':
166
+ _this.onSizeUpdate();
167
+ break;
168
+ }
169
+ };
170
+ this.doEvent = function (pen, eventName) {
171
+ var _a;
172
+ if (!pen) {
173
+ return;
174
+ }
175
+ (_a = pen.events) === null || _a === void 0 ? void 0 : _a.forEach(function (event) {
176
+ var _a;
177
+ if (_this.events[event.action] && event.name === eventName) {
178
+ var can = !((_a = event.where) === null || _a === void 0 ? void 0 : _a.type);
179
+ if (event.where) {
180
+ var _b = event.where, fn = _b.fn, fnJs = _b.fnJs, comparison = _b.comparison, key = _b.key, value = _b.value;
181
+ if (fn) {
182
+ can = fn(pen);
183
+ }
184
+ else if (fnJs) {
185
+ try {
186
+ event.where.fn = new Function('pen', fnJs);
187
+ }
188
+ catch (err) {
189
+ console.error('Error: make function:', err);
190
+ }
191
+ if (event.where.fn) {
192
+ can = event.where.fn(pen);
193
+ }
194
+ }
195
+ else {
196
+ switch (comparison) {
197
+ case '>':
198
+ can = pen[key] > +value;
199
+ break;
200
+ case '>=':
201
+ can = pen[key] >= +value;
202
+ break;
203
+ case '<':
204
+ can = pen[key] < +value;
205
+ break;
206
+ case '<=':
207
+ can = pen[key] <= +value;
208
+ break;
209
+ case '=':
210
+ case '==':
211
+ can = pen[key] == value;
212
+ break;
213
+ case '!=':
214
+ can = pen[key] != value;
215
+ break;
216
+ case '[)':
217
+ can = valueInRange(+pen[key], value);
218
+ break;
219
+ case '![)':
220
+ can = !valueInRange(+pen[key], value);
221
+ break;
222
+ case '[]':
223
+ can = valueInArray(+pen[key], value);
224
+ break;
225
+ case '![]':
226
+ can = !valueInArray(+pen[key], value);
227
+ break;
228
+ }
229
+ }
230
+ }
231
+ can && _this.events[event.action](pen, event);
232
+ }
233
+ });
234
+ // 事件冒泡,子执行完,父执行
235
+ _this.doEvent(_this.store.pens[pen.parentId], eventName);
236
+ };
237
+ this.renderPenRaw = renderPenRaw;
238
+ this.setElemPosition = setElemPosition;
239
+ this.store = useStore(s8());
240
+ this.setOptions(opts);
241
+ this.setDatabyOptions(opts);
242
+ this.init(parent);
243
+ this.register(commonPens());
244
+ this.registerCanvasDraw({ cube: cube });
245
+ this.registerAnchors(commonAnchors());
246
+ globalThis.meta2d = this;
247
+ this.initEventFns();
248
+ this.store.emitter.on('*', this.onEvent);
249
+ }
250
+ Object.defineProperty(Meta2d.prototype, "beforeAddPen", {
251
+ /**
252
+ * @deprecated 改用 beforeAddPens
253
+ */
254
+ get: function () {
255
+ return this.canvas.beforeAddPen;
256
+ },
257
+ /**
258
+ * @deprecated 改用 beforeAddPens
259
+ */
260
+ set: function (fn) {
261
+ this.canvas.beforeAddPen = fn;
262
+ },
263
+ enumerable: false,
264
+ configurable: true
265
+ });
266
+ Object.defineProperty(Meta2d.prototype, "beforeAddPens", {
267
+ get: function () {
268
+ return this.canvas.beforeAddPens;
269
+ },
270
+ set: function (fn) {
271
+ this.canvas.beforeAddPens = fn;
272
+ },
273
+ enumerable: false,
274
+ configurable: true
275
+ });
276
+ Object.defineProperty(Meta2d.prototype, "beforeAddAnchor", {
277
+ get: function () {
278
+ return this.canvas.beforeAddAnchor;
279
+ },
280
+ set: function (fn) {
281
+ this.canvas.beforeAddAnchor = fn;
282
+ },
283
+ enumerable: false,
284
+ configurable: true
285
+ });
286
+ Object.defineProperty(Meta2d.prototype, "beforeRemovePens", {
287
+ get: function () {
288
+ return this.canvas.beforeRemovePens;
289
+ },
290
+ set: function (fn) {
291
+ this.canvas.beforeRemovePens = fn;
292
+ },
293
+ enumerable: false,
294
+ configurable: true
295
+ });
296
+ Object.defineProperty(Meta2d.prototype, "beforeRemoveAnchor", {
297
+ get: function () {
298
+ return this.canvas.beforeRemoveAnchor;
299
+ },
300
+ set: function (fn) {
301
+ this.canvas.beforeRemoveAnchor = fn;
302
+ },
303
+ enumerable: false,
304
+ configurable: true
305
+ });
306
+ Meta2d.prototype.setOptions = function (opts) {
307
+ if (opts === void 0) { opts = {}; }
308
+ this.store.options = Object.assign(this.store.options, opts);
309
+ };
310
+ Meta2d.prototype.getOptions = function () {
311
+ return this.store.options;
312
+ };
313
+ Meta2d.prototype.setDatabyOptions = function (options) {
314
+ if (options === void 0) { options = {}; }
315
+ var color = options.color, activeColor = options.activeColor, activeBackground = options.activeBackground, grid = options.grid, gridColor = options.gridColor, gridSize = options.gridSize, fromArrow = options.fromArrow, toArrow = options.toArrow, rule = options.rule, ruleColor = options.ruleColor;
316
+ this.setRule({ rule: rule, ruleColor: ruleColor });
317
+ this.setGrid({
318
+ grid: grid,
319
+ gridColor: gridColor,
320
+ gridSize: gridSize,
321
+ });
322
+ this.store.data = Object.assign(this.store.data, {
323
+ color: color,
324
+ activeColor: activeColor,
325
+ activeBackground: activeBackground,
326
+ fromArrow: fromArrow,
327
+ toArrow: toArrow,
328
+ });
329
+ };
330
+ Meta2d.prototype.init = function (parent) {
331
+ if (typeof parent === 'string') {
332
+ this.canvas = new Canvas(this, document.getElementById(parent), this.store);
333
+ }
334
+ else {
335
+ this.canvas = new Canvas(this, parent, this.store);
336
+ }
337
+ this.resize();
338
+ this.canvas.listen();
339
+ };
340
+ Meta2d.prototype.initEventFns = function () {
341
+ var _this = this;
342
+ this.events[EventAction.Link] = function (pen, e) {
343
+ var _a;
344
+ if (window && e.value && typeof e.value === 'string') {
345
+ window.open(e.value, (_a = e.params) !== null && _a !== void 0 ? _a : '_blank');
346
+ return;
347
+ }
348
+ console.warn('[meta2d] Link param is not a string');
349
+ };
350
+ this.events[EventAction.SetProps] = function (pen, e) {
351
+ // TODO: 若频繁地触发,重复 render 可能带来性能问题,待考虑
352
+ var value = e.value;
353
+ if (value && typeof value === 'object') {
354
+ var pens = e.params ? _this.find(e.params) : [pen];
355
+ pens.forEach(function (pen) {
356
+ if (value.hasOwnProperty('visible')) {
357
+ _this.setVisible(pen, value.visible);
358
+ }
359
+ _this.setValue(__assign({ id: pen.id }, value), { render: false, doEvent: false });
360
+ });
361
+ _this.render();
362
+ return;
363
+ }
364
+ console.warn('[meta2d] SetProps value is not an object');
365
+ };
366
+ this.events[EventAction.StartAnimate] = function (pen, e) {
367
+ if (!e.value || typeof e.value === 'string') {
368
+ _this.startAnimate(e.value || [pen]);
369
+ return;
370
+ }
371
+ console.warn('[meta2d] StartAnimate value is not a string');
372
+ };
373
+ this.events[EventAction.PauseAnimate] = function (pen, e) {
374
+ if (!e.value || typeof e.value === 'string') {
375
+ _this.pauseAnimate(e.value || [pen]);
376
+ return;
377
+ }
378
+ console.warn('[meta2d] PauseAnimate value is not a string');
379
+ };
380
+ this.events[EventAction.StopAnimate] = function (pen, e) {
381
+ if (!e.value || typeof e.value === 'string') {
382
+ _this.stopAnimate(e.value || [pen]);
383
+ return;
384
+ }
385
+ console.warn('[meta2d] StopAnimate event value is not a string');
386
+ };
387
+ this.events[EventAction.StartVideo] = function (pen, e) {
388
+ if (!e.value || typeof e.value === 'string') {
389
+ _this.startVideo(e.value || [pen]);
390
+ return;
391
+ }
392
+ console.warn('[meta2d] StartVideo value is not a string');
393
+ };
394
+ this.events[EventAction.PauseVideo] = function (pen, e) {
395
+ if (!e.value || typeof e.value === 'string') {
396
+ _this.pauseVideo(e.value || [pen]);
397
+ return;
398
+ }
399
+ console.warn('[meta2d] PauseVideo value is not a string');
400
+ };
401
+ this.events[EventAction.StopVideo] = function (pen, e) {
402
+ if (!e.value || typeof e.value === 'string') {
403
+ _this.stopVideo(e.value || [pen]);
404
+ return;
405
+ }
406
+ console.warn('[meta2d] StopVideo event value is not a string');
407
+ };
408
+ this.events[EventAction.Function] = function (pen, e) {
409
+ var _a;
410
+ if (e.value && !e.fn) {
411
+ try {
412
+ if (typeof e.value !== 'string') {
413
+ throw new Error('[meta2d] Function value must be string');
414
+ }
415
+ var fnJs = e.value;
416
+ e.fn = new Function('pen', 'params', fnJs);
417
+ }
418
+ catch (err) {
419
+ console.error('[meta2d]: Error on make a function:', err);
420
+ }
421
+ }
422
+ (_a = e.fn) === null || _a === void 0 ? void 0 : _a.call(e, pen, e.params);
423
+ };
424
+ this.events[EventAction.GlobalFn] = function (pen, e) {
425
+ if (typeof e.value !== 'string') {
426
+ console.warn('[meta2d] GlobalFn value must be a string');
427
+ return;
428
+ }
429
+ if (globalThis[e.value]) {
430
+ globalThis[e.value](pen, e.params);
431
+ }
432
+ };
433
+ this.events[EventAction.Emit] = function (pen, e) {
434
+ if (typeof e.value !== 'string') {
435
+ console.warn('[meta2d] Emit value must be a string');
436
+ return;
437
+ }
438
+ _this.store.emitter.emit(e.value, {
439
+ pen: pen,
440
+ params: e.params,
441
+ });
442
+ };
443
+ this.events[EventAction.SendPropData] = function (pen, e) {
444
+ var value = deepClone(e.value);
445
+ if (value && typeof value === 'object') {
446
+ var _pen = e.params ? _this.findOne(e.params) : pen;
447
+ for (var key in value) {
448
+ if (!value[key]) {
449
+ value[key] = _pen[key];
450
+ }
451
+ }
452
+ value.id = _pen.id;
453
+ _this.doSendDataEvent(value);
454
+ return;
455
+ }
456
+ console.warn('[meta2d] SendPropData value is not an object');
457
+ };
458
+ this.events[EventAction.SendVarData] = function (pen, e) {
459
+ var value = deepClone(e.value);
460
+ if (value && typeof value === 'object') {
461
+ var _pen = e.params ? _this.findOne(e.params) : pen;
462
+ var array = [];
463
+ var _loop_1 = function (key) {
464
+ var obj = {
465
+ dataId: key,
466
+ value: value[key],
467
+ };
468
+ if (!obj.value) {
469
+ var oneForm = _pen.form.find(function (_item) {
470
+ return _item.dataIds &&
471
+ _item.dataIds.dataId === obj.dataId;
472
+ });
473
+ if (oneForm) {
474
+ obj.value = _pen[oneForm.key];
475
+ }
476
+ }
477
+ array.push(obj);
478
+ };
479
+ for (var key in value) {
480
+ _loop_1(key);
481
+ }
482
+ _this.doSendDataEvent(array);
483
+ return;
484
+ }
485
+ console.warn('[meta2d] SendVarData value is not an object');
486
+ };
487
+ };
488
+ Meta2d.prototype.doSendDataEvent = function (value) {
489
+ var data = JSON.stringify(value);
490
+ if (this.mqttClient && this.mqttClient.connected) {
491
+ this.mqttClient.publish(this.store.data.mqttTopics, data);
492
+ }
493
+ if (this.websocket && this.websocket.readyState === 1) {
494
+ this.websocket.send(data);
495
+ }
496
+ if (this.store.data.http) {
497
+ this.sendDatabyHttp(data);
498
+ }
499
+ this.store.emitter.emit('sendData', data);
500
+ };
501
+ Meta2d.prototype.resize = function (width, height) {
502
+ this.canvas.resize(width, height);
503
+ this.render();
504
+ this.store.emitter.emit('resize', { width: width, height: height });
505
+ if (this.canvas.scroll && this.canvas.scroll.isShow) {
506
+ this.canvas.scroll.init();
507
+ }
508
+ };
509
+ /**
510
+ *
511
+ * @param emit 是否发送消息
512
+ */
513
+ Meta2d.prototype.addPen = function (pen, history, emit) {
514
+ if (emit === void 0) { emit = true; }
515
+ return __awaiter(this, void 0, void 0, function () {
516
+ return __generator(this, function (_a) {
517
+ switch (_a.label) {
518
+ case 0: return [4 /*yield*/, this.canvas.addPen(pen, history, emit)];
519
+ case 1: return [2 /*return*/, _a.sent()];
520
+ }
521
+ });
522
+ });
523
+ };
524
+ Meta2d.prototype.addPens = function (pens, history) {
525
+ return __awaiter(this, void 0, void 0, function () {
526
+ return __generator(this, function (_a) {
527
+ switch (_a.label) {
528
+ case 0: return [4 /*yield*/, this.canvas.addPens(pens, history)];
529
+ case 1: return [2 /*return*/, _a.sent()];
530
+ }
531
+ });
532
+ });
533
+ };
534
+ Meta2d.prototype.render = function (patchFlags) {
535
+ this.canvas.render(patchFlags);
536
+ };
537
+ Meta2d.prototype.setBackgroundImage = function (url) {
538
+ var _this = this;
539
+ this.store.data.bkImage = url;
540
+ this.canvas.canvasImageBottom.canvas.style.backgroundImage = url
541
+ ? "url(" + url + ")"
542
+ : '';
543
+ if (url) {
544
+ var img_1 = new Image();
545
+ img_1.src = url;
546
+ img_1.onload = function () {
547
+ // 用作 toPng 的绘制
548
+ _this.store.bkImg = img_1;
549
+ };
550
+ }
551
+ else {
552
+ this.store.bkImg = null;
553
+ }
554
+ };
555
+ Meta2d.prototype.setBackgroundColor = function (color) {
556
+ if (color === void 0) { color = this.store.data.background; }
557
+ this.store.data.background = color;
558
+ this.store.patchFlagsBackground = true;
559
+ };
560
+ Meta2d.prototype.setGrid = function (_a) {
561
+ var _b = _a === void 0 ? {} : _a, _c = _b.grid, grid = _c === void 0 ? this.store.data.grid : _c, _d = _b.gridColor, gridColor = _d === void 0 ? this.store.data.gridColor : _d, _e = _b.gridSize, gridSize = _e === void 0 ? this.store.data.gridSize : _e, _f = _b.gridRotate, gridRotate = _f === void 0 ? this.store.data.gridRotate : _f;
562
+ this.store.data.grid = grid;
563
+ this.store.data.gridColor = gridColor;
564
+ this.store.data.gridSize = gridSize;
565
+ this.store.data.gridRotate = gridRotate;
566
+ this.store.patchFlagsBackground = true;
567
+ };
568
+ Meta2d.prototype.setRule = function (_a) {
569
+ var _b = _a === void 0 ? {} : _a, _c = _b.rule, rule = _c === void 0 ? this.store.data.rule : _c, _d = _b.ruleColor, ruleColor = _d === void 0 ? this.store.data.ruleColor : _d;
570
+ this.store.data.rule = rule;
571
+ this.store.data.ruleColor = ruleColor;
572
+ this.store.patchFlagsTop = true;
573
+ };
574
+ Meta2d.prototype.open = function (data) {
575
+ var e_1, _a, e_2, _b;
576
+ this.clear(false);
577
+ if (data) {
578
+ this.setBackgroundImage(data.bkImage);
579
+ Object.assign(this.store.data, data);
580
+ this.store.data.pens = [];
581
+ try {
582
+ // 第一遍赋初值
583
+ for (var _c = __values(data.pens), _d = _c.next(); !_d.done; _d = _c.next()) {
584
+ var pen = _d.value;
585
+ if (!pen.id) {
586
+ pen.id = s8();
587
+ }
588
+ !pen.calculative && (pen.calculative = { canvas: this.canvas });
589
+ this.store.pens[pen.id] = pen;
590
+ }
591
+ }
592
+ catch (e_1_1) { e_1 = { error: e_1_1 }; }
593
+ finally {
594
+ try {
595
+ if (_d && !_d.done && (_a = _c.return)) _a.call(_c);
596
+ }
597
+ finally { if (e_1) throw e_1.error; }
598
+ }
599
+ try {
600
+ for (var _e = __values(data.pens), _f = _e.next(); !_f.done; _f = _e.next()) {
601
+ var pen = _f.value;
602
+ this.canvas.makePen(pen);
603
+ }
604
+ }
605
+ catch (e_2_1) { e_2 = { error: e_2_1 }; }
606
+ finally {
607
+ try {
608
+ if (_f && !_f.done && (_b = _e.return)) _b.call(_e);
609
+ }
610
+ finally { if (e_2) throw e_2.error; }
611
+ }
612
+ }
613
+ this.initBindDatas();
614
+ this.render();
615
+ this.listenSocket();
616
+ this.connectSocket();
617
+ this.startAnimate();
618
+ this.startVideo();
619
+ this.doInitJS();
620
+ this.store.emitter.emit('opened');
621
+ if (this.canvas.scroll && this.canvas.scroll.isShow) {
622
+ this.canvas.scroll.init();
623
+ }
624
+ };
625
+ Meta2d.prototype.initBindDatas = function () {
626
+ var _this = this;
627
+ this.store.bindDatas = {};
628
+ this.store.data.pens.forEach(function (pen) {
629
+ var _a;
630
+ (_a = pen.form) === null || _a === void 0 ? void 0 : _a.forEach(function (formItem) {
631
+ var dataIds;
632
+ if (formItem.dataIds) {
633
+ if (Array.isArray(formItem.dataIds)) {
634
+ dataIds = formItem.dataIds;
635
+ }
636
+ else {
637
+ dataIds = [formItem.dataIds];
638
+ }
639
+ }
640
+ dataIds === null || dataIds === void 0 ? void 0 : dataIds.forEach(function (item) {
641
+ if (!_this.store.bindDatas[item.dataId]) {
642
+ _this.store.bindDatas[item.dataId] = [];
643
+ }
644
+ _this.store.bindDatas[item.dataId].push({
645
+ id: pen.id,
646
+ formItem: formItem,
647
+ });
648
+ });
649
+ });
650
+ });
651
+ };
652
+ Meta2d.prototype.connectSocket = function () {
653
+ this.connectWebsocket();
654
+ this.connectMqtt();
655
+ this.connectHttp();
656
+ };
657
+ /**
658
+ * open 后执行初始化 Js ,每个图纸可配置一个初始化 js
659
+ */
660
+ Meta2d.prototype.doInitJS = function () {
661
+ var initJs = this.store.data.initJs;
662
+ if (initJs && initJs.trim()) {
663
+ try {
664
+ var fn = new Function(initJs);
665
+ fn();
666
+ }
667
+ catch (e) {
668
+ console.warn('initJs error', e);
669
+ }
670
+ }
671
+ };
672
+ Meta2d.prototype.drawLine = function (lineName) {
673
+ lineName && lockedError(this.store);
674
+ this.canvas.drawingLineName = lineName;
675
+ };
676
+ Meta2d.prototype.drawingPencil = function () {
677
+ this.canvas.drawingPencil();
678
+ };
679
+ Meta2d.prototype.stopPencil = function () {
680
+ this.canvas.stopPencil();
681
+ };
682
+ Meta2d.prototype.lock = function (lock) {
683
+ this.store.data.locked = lock;
684
+ this.finishDrawLine(true);
685
+ this.canvas.drawingLineName = '';
686
+ this.stopPencil();
687
+ if (lock === 0) {
688
+ //恢复可选状态
689
+ this.store.data.pens.forEach(function (pen) {
690
+ if (pen.name === 'echarts') {
691
+ pen.onMove && pen.onMove(pen);
692
+ }
693
+ });
694
+ }
695
+ };
696
+ // end - 当前鼠标位置,是否作为终点
697
+ Meta2d.prototype.finishDrawLine = function (end) {
698
+ return __awaiter(this, void 0, void 0, function () {
699
+ return __generator(this, function (_a) {
700
+ switch (_a.label) {
701
+ case 0: return [4 /*yield*/, this.canvas.finishDrawline(end)];
702
+ case 1:
703
+ _a.sent();
704
+ return [2 /*return*/];
705
+ }
706
+ });
707
+ });
708
+ };
709
+ Meta2d.prototype.finishPencil = function () {
710
+ return __awaiter(this, void 0, void 0, function () {
711
+ return __generator(this, function (_a) {
712
+ switch (_a.label) {
713
+ case 0: return [4 /*yield*/, this.canvas.finishPencil()];
714
+ case 1:
715
+ _a.sent();
716
+ return [2 /*return*/];
717
+ }
718
+ });
719
+ });
720
+ };
721
+ Meta2d.prototype.updateLineType = function (pen, lineName) {
722
+ if (!pen || pen.name != 'line' || !lineName || !this.canvas[lineName]) {
723
+ return;
724
+ }
725
+ pen.lineName = lineName;
726
+ var from = getFromAnchor(pen);
727
+ var to = getToAnchor(pen);
728
+ from.prev = undefined;
729
+ from.next = undefined;
730
+ to.prev = undefined;
731
+ to.next = undefined;
732
+ pen.calculative.worldAnchors = [from, to];
733
+ pen.calculative.activeAnchor = from;
734
+ this.canvas[lineName](this.store, pen, to);
735
+ if (pen.lineName === 'curve') {
736
+ from.prev = {
737
+ penId: from.penId,
738
+ x: from.x - 50,
739
+ y: from.y,
740
+ };
741
+ from.next = {
742
+ penId: from.penId,
743
+ x: from.x + 50,
744
+ y: from.y,
745
+ };
746
+ to.prev = {
747
+ penId: to.penId,
748
+ x: to.x - 50,
749
+ y: to.y,
750
+ };
751
+ to.next = {
752
+ penId: to.penId,
753
+ x: to.x + 50,
754
+ y: to.y,
755
+ };
756
+ }
757
+ pen.calculative.activeAnchor = undefined;
758
+ this.canvas.initLineRect(pen);
759
+ this.render();
760
+ };
761
+ Meta2d.prototype.addDrawLineFn = function (fnName, fn) {
762
+ this.canvas[fnName] = fn;
763
+ this.canvas.drawLineFns.push(fnName);
764
+ };
765
+ Meta2d.prototype.removeDrawLineFn = function (fnName) {
766
+ var index = this.canvas.drawLineFns.indexOf(fnName);
767
+ if (index > -1) {
768
+ this.canvas.drawLineFns.splice(index, 1);
769
+ }
770
+ };
771
+ Meta2d.prototype.showMagnifier = function () {
772
+ this.canvas.showMagnifier();
773
+ };
774
+ Meta2d.prototype.hideMagnifier = function () {
775
+ this.canvas.hideMagnifier();
776
+ };
777
+ Meta2d.prototype.toggleMagnifier = function () {
778
+ this.canvas.toggleMagnifier();
779
+ };
780
+ /**
781
+ * 擦除画布,释放 store 上的 pens
782
+ * @param render 是否重绘
783
+ */
784
+ Meta2d.prototype.clear = function (render) {
785
+ var e_3, _a;
786
+ var _b;
787
+ if (render === void 0) { render = true; }
788
+ try {
789
+ for (var _c = __values(this.store.data.pens), _d = _c.next(); !_d.done; _d = _c.next()) {
790
+ var pen = _d.value;
791
+ (_b = pen.onDestroy) === null || _b === void 0 ? void 0 : _b.call(pen, pen);
792
+ }
793
+ }
794
+ catch (e_3_1) { e_3 = { error: e_3_1 }; }
795
+ finally {
796
+ try {
797
+ if (_d && !_d.done && (_a = _c.return)) _a.call(_c);
798
+ }
799
+ finally { if (e_3) throw e_3.error; }
800
+ }
801
+ clearStore(this.store);
802
+ this.hideInput();
803
+ this.canvas.tooltip.hide();
804
+ this.canvas.clearCanvas();
805
+ sessionStorage.removeItem('page');
806
+ this.store.clipboard = undefined;
807
+ // 非必要,为的是 open 时重绘 背景与网格
808
+ this.store.patchFlagsBackground = true;
809
+ this.store.patchFlagsTop = true;
810
+ this.setBackgroundImage(undefined);
811
+ render && this.render();
812
+ };
813
+ Meta2d.prototype.emit = function (eventType, data) {
814
+ this.store.emitter.emit(eventType, data);
815
+ };
816
+ Meta2d.prototype.on = function (eventType, handler) {
817
+ this.store.emitter.on(eventType, handler);
818
+ return this;
819
+ };
820
+ Meta2d.prototype.off = function (eventType, handler) {
821
+ this.store.emitter.off(eventType, handler);
822
+ return this;
823
+ };
824
+ // customeDock = (store, rect, pens, offset) => {xDock, yDock}
825
+ // customDock return:
826
+ // {
827
+ // xDock: {x, y, step, prev, penId},
828
+ // yDock: {x, y, step, prev, penId},
829
+ // }
830
+ // xDock,yDock - 水平或垂直方向的参考线
831
+ // prev - 参考线的起点
832
+ // x,y - 参考线的终点
833
+ // step - 自动吸附需要的偏移量
834
+ // penId - 参考线的笔
835
+ Meta2d.prototype.registerMoveDock = function (dock) {
836
+ this.canvas.customMoveDock = dock;
837
+ };
838
+ /**
839
+ * 参数同方法 registerMoveDock ,最后一个参数由 offset 偏移修改成了当前 resize 的点
840
+ */
841
+ Meta2d.prototype.registerResizeDock = function (dock) {
842
+ this.canvas.customResizeDock = dock;
843
+ };
844
+ Meta2d.prototype.find = function (idOrTag) {
845
+ return this.canvas.find(idOrTag);
846
+ };
847
+ Meta2d.prototype.findOne = function (idOrTag) {
848
+ return this.canvas.findOne(idOrTag);
849
+ };
850
+ Meta2d.prototype.getPenRect = function (pen) {
851
+ return this.canvas.getPenRect(pen);
852
+ };
853
+ Meta2d.prototype.setPenRect = function (pen, rect, render) {
854
+ if (render === void 0) { render = true; }
855
+ this.canvas.setPenRect(pen, rect, render);
856
+ };
857
+ Meta2d.prototype.startAnimate = function (idOrTagOrPens) {
858
+ var _this = this;
859
+ var pens;
860
+ if (!idOrTagOrPens) {
861
+ pens = this.store.data.pens.filter(function (pen) {
862
+ return (pen.type || pen.frames) && pen.autoPlay;
863
+ });
864
+ }
865
+ else if (typeof idOrTagOrPens === 'string') {
866
+ pens = this.find(idOrTagOrPens);
867
+ }
868
+ else {
869
+ pens = idOrTagOrPens;
870
+ }
871
+ pens.forEach(function (pen) {
872
+ if (pen.calculative.pause) {
873
+ var d = Date.now() - pen.calculative.pause;
874
+ pen.calculative.pause = undefined;
875
+ pen.calculative.frameStart += d;
876
+ pen.calculative.frameEnd += d;
877
+ }
878
+ else {
879
+ _this.store.animates.add(pen);
880
+ if (!pen.type) {
881
+ _this.store.animateMap.set(pen, pen.calculative.canvas.getFrameProps(pen));
882
+ }
883
+ }
884
+ });
885
+ this.canvas.canvasImage.init();
886
+ this.canvas.canvasImageBottom.init();
887
+ this.canvas.animate();
888
+ };
889
+ Meta2d.prototype.pauseAnimate = function (idOrTagOrPens) {
890
+ var pens = [];
891
+ if (!idOrTagOrPens) {
892
+ this.store.animates.forEach(function (pen) {
893
+ pens.push(pen);
894
+ });
895
+ }
896
+ else if (typeof idOrTagOrPens === 'string') {
897
+ pens = this.find(idOrTagOrPens);
898
+ }
899
+ else {
900
+ pens = idOrTagOrPens;
901
+ }
902
+ pens.forEach(function (pen) {
903
+ if (!pen.calculative.pause) {
904
+ pen.calculative.pause = Date.now();
905
+ }
906
+ });
907
+ };
908
+ Meta2d.prototype.stopAnimate = function (idOrTagOrPens) {
909
+ var _this = this;
910
+ var pens = [];
911
+ if (!idOrTagOrPens) {
912
+ this.store.animates.forEach(function (pen) {
913
+ pens.push(pen);
914
+ });
915
+ }
916
+ else if (typeof idOrTagOrPens === 'string') {
917
+ pens = this.find(idOrTagOrPens);
918
+ }
919
+ else {
920
+ pens = idOrTagOrPens;
921
+ }
922
+ pens.forEach(function (pen) {
923
+ pen.calculative.pause = undefined;
924
+ pen.calculative.start = undefined;
925
+ pen.calculative.duration = undefined;
926
+ pen.calculative.animatePos = 0;
927
+ _this.store.animates.delete(pen);
928
+ _this.canvas.restoreNodeAnimate(pen);
929
+ });
930
+ this.initImageCanvas(pens);
931
+ setTimeout(function () {
932
+ _this.canvas.calcActiveRect();
933
+ _this.render();
934
+ }, 20);
935
+ };
936
+ Meta2d.prototype.startVideo = function (idOrTagOrPens) {
937
+ var pens;
938
+ if (!idOrTagOrPens) {
939
+ pens = this.store.data.pens.filter(function (pen) {
940
+ return (pen.video || pen.audio) && pen.autoPlay;
941
+ });
942
+ }
943
+ else if (typeof idOrTagOrPens === 'string') {
944
+ pens = this.find(idOrTagOrPens);
945
+ }
946
+ else {
947
+ pens = idOrTagOrPens;
948
+ }
949
+ pens.forEach(function (pen) {
950
+ var _a, _b;
951
+ (_a = pen.calculative.media) === null || _a === void 0 ? void 0 : _a.play();
952
+ (_b = pen.onStartVideo) === null || _b === void 0 ? void 0 : _b.call(pen, pen);
953
+ });
954
+ };
955
+ Meta2d.prototype.pauseVideo = function (idOrTagOrPens) {
956
+ var pens = [];
957
+ if (!idOrTagOrPens) {
958
+ //TODO 寻找所有 而不是正在播放的
959
+ pens = this.store.data.pens.filter(function (pen) {
960
+ return (pen.video || pen.audio) && pen.autoPlay;
961
+ });
962
+ }
963
+ else if (typeof idOrTagOrPens === 'string') {
964
+ pens = this.find(idOrTagOrPens);
965
+ }
966
+ else {
967
+ pens = idOrTagOrPens;
968
+ }
969
+ pens.forEach(function (pen) {
970
+ var _a, _b;
971
+ (_a = pen.calculative.media) === null || _a === void 0 ? void 0 : _a.pause();
972
+ (_b = pen.onPauseVideo) === null || _b === void 0 ? void 0 : _b.call(pen, pen);
973
+ });
974
+ };
975
+ Meta2d.prototype.stopVideo = function (idOrTagOrPens) {
976
+ var pens = [];
977
+ if (!idOrTagOrPens) {
978
+ pens = this.store.data.pens.filter(function (pen) {
979
+ return (pen.video || pen.audio) && pen.autoPlay;
980
+ });
981
+ }
982
+ else if (typeof idOrTagOrPens === 'string') {
983
+ pens = this.find(idOrTagOrPens);
984
+ }
985
+ else {
986
+ pens = idOrTagOrPens;
987
+ }
988
+ pens.forEach(function (pen) {
989
+ var _a;
990
+ if (pen.calculative.media) {
991
+ pen.calculative.media.currentTime = 0;
992
+ pen.calculative.media.pause();
993
+ }
994
+ (_a = pen.onStopVideo) === null || _a === void 0 ? void 0 : _a.call(pen, pen);
995
+ });
996
+ };
997
+ Meta2d.prototype.calcAnimateDuration = function (pen) {
998
+ return pen.frames.reduce(function (prev, frame) { return prev + frame.duration; }, 0);
999
+ };
1000
+ /**
1001
+ * 组合
1002
+ * @param pens 组合的画笔们
1003
+ * @param showChild 组合后展示第几个孩子
1004
+ */
1005
+ Meta2d.prototype.combine = function (pens, showChild) {
1006
+ if (pens === void 0) { pens = this.store.active; }
1007
+ if (!pens || !pens.length) {
1008
+ return;
1009
+ }
1010
+ var initPens = deepClone(pens);
1011
+ if (pens.length === 1 && pens[0].type) {
1012
+ pens[0].type = PenType.Node;
1013
+ this.canvas.active(pens);
1014
+ this.pushHistory({
1015
+ type: EditType.Update,
1016
+ initPens: initPens,
1017
+ pens: deepClone(pens, true),
1018
+ });
1019
+ this.render();
1020
+ return;
1021
+ }
1022
+ var rect = getRect(pens);
1023
+ var parent = __assign(__assign({ id: s8(), name: 'combine' }, rect), { children: [], showChild: showChild });
1024
+ // const p = pens.find((pen) => {
1025
+ // // TODO: js 计算误差,可能导致包含着其它的 pens 的最大 pen 无法计算出来
1026
+ // return pen.width === rect.width && pen.height === rect.height;
1027
+ // });
1028
+ // // 其中一个认为是父节点
1029
+ // const oneIsParent = p && showChild == undefined;
1030
+ // if (oneIsParent) {
1031
+ // if (!p.children) {
1032
+ // p.children = [];
1033
+ // }
1034
+ // parent = p;
1035
+ // } else {
1036
+ // 若组合为状态,那么 parent 一定是 combine
1037
+ this.canvas.makePen(parent);
1038
+ // }
1039
+ pens.forEach(function (pen) {
1040
+ var _a;
1041
+ if (pen === parent || pen.parentId === parent.id) {
1042
+ return;
1043
+ }
1044
+ // pen 来自于 store.active ,不存在有 parentId 的情况
1045
+ parent.children.push(pen.id);
1046
+ pen.parentId = parent.id;
1047
+ var childRect = calcRelativeRect(pen.calculative.worldRect, rect);
1048
+ Object.assign(pen, childRect);
1049
+ pen.locked = (_a = pen.lockedOnCombine) !== null && _a !== void 0 ? _a : LockState.DisableMove;
1050
+ });
1051
+ this.canvas.active([parent]);
1052
+ var step = 1;
1053
+ // if (!oneIsParent) {
1054
+ // step = 2;
1055
+ // this.pushHistory({
1056
+ // type: EditType.Add,
1057
+ // pens: [parent],
1058
+ // step,
1059
+ // });
1060
+ // this.store.emitter.emit('add', [parent]);
1061
+ // }
1062
+ this.pushHistory({
1063
+ type: EditType.Update,
1064
+ initPens: initPens,
1065
+ pens: pens,
1066
+ step: step,
1067
+ });
1068
+ if (showChild != undefined) {
1069
+ pens.forEach(function (pen) {
1070
+ calcInView(pen, true);
1071
+ });
1072
+ this.initImageCanvas([parent]);
1073
+ }
1074
+ this.render();
1075
+ };
1076
+ Meta2d.prototype.uncombine = function (pen) {
1077
+ var _this = this;
1078
+ if (!pen && this.store.active) {
1079
+ pen = this.store.active[0];
1080
+ }
1081
+ if (!pen || !pen.children) {
1082
+ return;
1083
+ }
1084
+ var children = pen.children.map(function (childId) { return _this.store.pens[childId]; });
1085
+ var initPens = deepClone(children);
1086
+ children.forEach(function (child) {
1087
+ child.parentId = undefined;
1088
+ child.x = child.calculative.worldRect.x;
1089
+ child.y = child.calculative.worldRect.y;
1090
+ child.width = child.calculative.worldRect.width;
1091
+ child.height = child.calculative.worldRect.height;
1092
+ child.locked = LockState.None;
1093
+ child.calculative.active = undefined;
1094
+ child.calculative.hover = false;
1095
+ _this.setVisible(child, true); // 子节点的 visible 属性已经改变,需要恢复
1096
+ });
1097
+ var step = this.isCombine(pen) ? 3 : 2;
1098
+ this.pushHistory({
1099
+ type: EditType.Update,
1100
+ initPens: initPens,
1101
+ pens: children,
1102
+ step: step,
1103
+ });
1104
+ initPens = [deepClone(pen)];
1105
+ pen.children = undefined;
1106
+ // 保存修改 children 的历史记录
1107
+ this.pushHistory({
1108
+ type: EditType.Update,
1109
+ initPens: initPens,
1110
+ pens: [pen],
1111
+ step: step,
1112
+ });
1113
+ if (this.isCombine(pen)) {
1114
+ this.delete([pen]);
1115
+ // delete 会记录 history , 更改 step 即可
1116
+ this.store.histories[this.store.histories.length - 1].step = step;
1117
+ }
1118
+ this.inactive();
1119
+ };
1120
+ Meta2d.prototype.isCombine = function (pen) {
1121
+ if (pen.name === 'combine') {
1122
+ return true;
1123
+ }
1124
+ if (pen.children && pen.children.length > 0) {
1125
+ return true;
1126
+ }
1127
+ return false;
1128
+ };
1129
+ Meta2d.prototype.active = function (pens, emit) {
1130
+ if (emit === void 0) { emit = true; }
1131
+ this.canvas.active(pens, emit);
1132
+ };
1133
+ Meta2d.prototype.inactive = function () {
1134
+ this.canvas.inactive();
1135
+ };
1136
+ /**
1137
+ * 删除画笔
1138
+ * @param pens 需要删除的画笔们
1139
+ * @param canDelLocked 是否删除已经锁住的画笔
1140
+ */
1141
+ Meta2d.prototype.delete = function (pens, canDelLocked, history) {
1142
+ if (canDelLocked === void 0) { canDelLocked = false; }
1143
+ if (history === void 0) { history = true; }
1144
+ this.canvas.delete(pens, canDelLocked, history);
1145
+ };
1146
+ Meta2d.prototype.scale = function (scale, center) {
1147
+ if (center === void 0) { center = { x: 0, y: 0 }; }
1148
+ this.canvas.scale(scale, center);
1149
+ };
1150
+ Meta2d.prototype.translate = function (x, y) {
1151
+ this.canvas.translate(x, y);
1152
+ };
1153
+ Meta2d.prototype.translatePens = function (pens, x, y) {
1154
+ this.canvas.translatePens(pens, x, y);
1155
+ };
1156
+ Meta2d.prototype.getParent = function (pen, root) {
1157
+ return getParent(pen, root);
1158
+ };
1159
+ Meta2d.prototype.data = function () {
1160
+ var data = deepClone(this.store.data);
1161
+ var _a = this.store.data, pens = _a.pens, paths = _a.paths;
1162
+ data.version = pkg.version;
1163
+ // TODO: 未在 delete 时清除,避免撤销等操作。
1164
+ // 清除一些未使用到的 paths
1165
+ data.paths = {};
1166
+ var _loop_2 = function (pathId) {
1167
+ if (Object.prototype.hasOwnProperty.call(paths, pathId)) {
1168
+ if (pens.find(function (pen) { return pen.pathId === pathId; })) {
1169
+ data.paths[pathId] = paths[pathId];
1170
+ }
1171
+ }
1172
+ };
1173
+ for (var pathId in paths) {
1174
+ _loop_2(pathId);
1175
+ }
1176
+ return data;
1177
+ };
1178
+ Meta2d.prototype.copy = function (pens) {
1179
+ this.canvas.copy(pens);
1180
+ };
1181
+ Meta2d.prototype.cut = function (pens) {
1182
+ this.canvas.cut(pens);
1183
+ };
1184
+ Meta2d.prototype.paste = function () {
1185
+ this.canvas.paste();
1186
+ };
1187
+ Meta2d.prototype.undo = function () {
1188
+ this.canvas.undo();
1189
+ };
1190
+ Meta2d.prototype.redo = function () {
1191
+ this.canvas.redo();
1192
+ };
1193
+ Meta2d.prototype.listenSocket = function () {
1194
+ try {
1195
+ var socketFn = void 0;
1196
+ var socketCbJs = this.store.data.socketCbJs;
1197
+ if (socketCbJs) {
1198
+ socketFn = new Function('e', 'topic', socketCbJs);
1199
+ }
1200
+ if (!socketFn) {
1201
+ return false;
1202
+ }
1203
+ this.socketFn = socketFn;
1204
+ }
1205
+ catch (e) {
1206
+ console.error('Create the function for socket:', e);
1207
+ return false;
1208
+ }
1209
+ return true;
1210
+ };
1211
+ Meta2d.prototype.connectWebsocket = function (websocket) {
1212
+ var _this = this;
1213
+ this.closeWebsocket();
1214
+ if (websocket) {
1215
+ this.store.data.websocket = websocket;
1216
+ }
1217
+ if (this.store.data.websocket) {
1218
+ this.websocket = new WebSocket(this.store.data.websocket);
1219
+ this.websocket.onmessage = function (e) {
1220
+ _this.socketCallback(e.data);
1221
+ };
1222
+ this.websocket.onclose = function () {
1223
+ console.info('Canvas websocket closed and reconneting...');
1224
+ _this.connectWebsocket();
1225
+ };
1226
+ }
1227
+ };
1228
+ Meta2d.prototype.closeWebsocket = function () {
1229
+ if (this.websocket) {
1230
+ this.websocket.onclose = undefined;
1231
+ this.websocket.close();
1232
+ this.websocket = undefined;
1233
+ }
1234
+ };
1235
+ Meta2d.prototype.connectMqtt = function (params) {
1236
+ var _this = this;
1237
+ this.closeMqtt();
1238
+ if (params) {
1239
+ this.store.data.mqtt = params.mqtt;
1240
+ this.store.data.mqttTopics = params.mqttTopics;
1241
+ this.store.data.mqttOptions = params.mqttOptions;
1242
+ }
1243
+ if (this.store.data.mqtt) {
1244
+ if (this.store.data.mqttOptions.clientId &&
1245
+ !this.store.data.mqttOptions.customClientId) {
1246
+ this.store.data.mqttOptions.clientId = s8();
1247
+ }
1248
+ this.mqttClient = mqtt.connect(this.store.data.mqtt, this.store.data.mqttOptions);
1249
+ this.mqttClient.on('message', function (topic, message) {
1250
+ _this.socketCallback(message.toString(), topic);
1251
+ });
1252
+ if (this.store.data.mqttTopics) {
1253
+ this.mqttClient.subscribe(this.store.data.mqttTopics.split(','));
1254
+ }
1255
+ }
1256
+ };
1257
+ Meta2d.prototype.closeMqtt = function () {
1258
+ var _a;
1259
+ (_a = this.mqttClient) === null || _a === void 0 ? void 0 : _a.end();
1260
+ };
1261
+ Meta2d.prototype.connectHttp = function () {
1262
+ var _this = this;
1263
+ this.closeHttp();
1264
+ var _a = this.store.data, http = _a.http, httpTimeInterval = _a.httpTimeInterval, httpHeaders = _a.httpHeaders;
1265
+ if (http) {
1266
+ this.httpTimer = setInterval(function () { return __awaiter(_this, void 0, void 0, function () {
1267
+ var res, data;
1268
+ return __generator(this, function (_a) {
1269
+ switch (_a.label) {
1270
+ case 0: return [4 /*yield*/, fetch(http, {
1271
+ headers: httpHeaders,
1272
+ })];
1273
+ case 1:
1274
+ res = _a.sent();
1275
+ if (!res.ok) return [3 /*break*/, 3];
1276
+ return [4 /*yield*/, res.text()];
1277
+ case 2:
1278
+ data = _a.sent();
1279
+ this.socketCallback(data);
1280
+ _a.label = 3;
1281
+ case 3: return [2 /*return*/];
1282
+ }
1283
+ });
1284
+ }); }, httpTimeInterval || 1000);
1285
+ }
1286
+ };
1287
+ Meta2d.prototype.sendDatabyHttp = function (data) {
1288
+ return __awaiter(this, void 0, void 0, function () {
1289
+ var _a, http, httpHeaders, res;
1290
+ return __generator(this, function (_b) {
1291
+ switch (_b.label) {
1292
+ case 0:
1293
+ _a = this.store.data, http = _a.http, httpHeaders = _a.httpHeaders;
1294
+ if (!http) return [3 /*break*/, 2];
1295
+ return [4 /*yield*/, fetch(http, {
1296
+ method: 'post',
1297
+ body: data,
1298
+ headers: httpHeaders,
1299
+ })];
1300
+ case 1:
1301
+ res = _b.sent();
1302
+ if (res.ok) {
1303
+ console.info('http消息发送成功');
1304
+ }
1305
+ _b.label = 2;
1306
+ case 2: return [2 /*return*/];
1307
+ }
1308
+ });
1309
+ });
1310
+ };
1311
+ Meta2d.prototype.closeHttp = function () {
1312
+ clearInterval(this.httpTimer);
1313
+ this.httpTimer = undefined;
1314
+ };
1315
+ Meta2d.prototype.socketCallback = function (message, topic) {
1316
+ var _this = this;
1317
+ if (topic === void 0) { topic = ''; }
1318
+ this.store.emitter.emit('socket', { message: message, topic: topic });
1319
+ if (this.socketFn && !this.socketFn(message, topic)) {
1320
+ return;
1321
+ }
1322
+ var data;
1323
+ if (message.constructor === Object || message.constructor === Array) {
1324
+ data = message;
1325
+ }
1326
+ else if (typeof message === 'string') {
1327
+ try {
1328
+ data = JSON.parse(message);
1329
+ }
1330
+ catch (error) {
1331
+ console.warn('Invalid socket data:', data, error);
1332
+ }
1333
+ }
1334
+ else {
1335
+ return;
1336
+ }
1337
+ if (!data) {
1338
+ return;
1339
+ }
1340
+ if (!Array.isArray(data)) {
1341
+ data = [data];
1342
+ }
1343
+ if (data[0].dataId) {
1344
+ this.setDatas(data);
1345
+ }
1346
+ else {
1347
+ data.forEach(function (_data) {
1348
+ _this.setValue(_data);
1349
+ });
1350
+ }
1351
+ };
1352
+ // 绑定变量方式更新组件数据
1353
+ Meta2d.prototype.setDatas = function (datas, _a) {
1354
+ var _this = this;
1355
+ var _b = _a === void 0 ? {} : _a, _c = _b.render, render = _c === void 0 ? true : _c, _d = _b.doEvent, doEvent = _d === void 0 ? true : _d, history = _b.history;
1356
+ // 把{dataId: string; value: any}转成setValue格式数据
1357
+ var penValues = new Map();
1358
+ datas.forEach(function (v) {
1359
+ var _a;
1360
+ (_a = _this.store.bindDatas[v.dataId]) === null || _a === void 0 ? void 0 : _a.forEach(function (p) {
1361
+ var _a;
1362
+ var pen = _this.store.pens[p.id];
1363
+ if (!pen) {
1364
+ return;
1365
+ }
1366
+ var penValue = penValues.get(pen);
1367
+ if (typeof pen.onBinds === 'function') {
1368
+ // 已经计算了
1369
+ if (penValue) {
1370
+ return;
1371
+ }
1372
+ penValues.set(pen, pen.onBinds(pen, datas, p.formItem));
1373
+ return;
1374
+ }
1375
+ if (penValue) {
1376
+ penValue[p.formItem.key] = v.value;
1377
+ }
1378
+ else {
1379
+ penValue = (_a = {
1380
+ id: p.id
1381
+ },
1382
+ _a[p.formItem.key] = v.value,
1383
+ _a);
1384
+ penValues.set(pen, penValue);
1385
+ }
1386
+ });
1387
+ });
1388
+ var initPens;
1389
+ var pens;
1390
+ if (history) {
1391
+ initPens = [];
1392
+ }
1393
+ penValues.forEach(function (value, pen) {
1394
+ _this.setValue(value, { render: false, doEvent: doEvent, history: false });
1395
+ if (history) {
1396
+ initPens.push(deepClone(pen, true));
1397
+ pens.push(pen);
1398
+ }
1399
+ });
1400
+ render && this.render();
1401
+ if (history) {
1402
+ this.pushHistory({
1403
+ type: EditType.Update,
1404
+ initPens: initPens,
1405
+ pens: pens,
1406
+ });
1407
+ }
1408
+ };
1409
+ Meta2d.prototype.setValue = function (data, _a) {
1410
+ var _this = this;
1411
+ var _b = _a === void 0 ? {} : _a, _c = _b.render, render = _c === void 0 ? true : _c, _d = _b.doEvent, doEvent = _d === void 0 ? true : _d, history = _b.history;
1412
+ var pens = [];
1413
+ if (data.id) {
1414
+ if (data.id === this.store.data.id) {
1415
+ this.setDatabyOptions(data);
1416
+ if (data.bkImage) {
1417
+ this.setBackgroundImage(data.bkImage);
1418
+ }
1419
+ if (data.background) {
1420
+ this.setBackgroundColor(data.background);
1421
+ }
1422
+ this.render();
1423
+ return;
1424
+ }
1425
+ var pen = this.store.pens[data.id];
1426
+ pen && (pens = [pen]);
1427
+ }
1428
+ else if (data.dataId) {
1429
+ pens = [];
1430
+ this.setDatas([data], {
1431
+ render: render,
1432
+ doEvent: doEvent,
1433
+ history: history,
1434
+ });
1435
+ return;
1436
+ }
1437
+ else if (data.tag) {
1438
+ pens = this.find(data.tag);
1439
+ }
1440
+ else {
1441
+ return;
1442
+ }
1443
+ history = history && !this.store.data.locked;
1444
+ var initPens;
1445
+ if (history) {
1446
+ initPens = deepClone(pens);
1447
+ }
1448
+ pens.forEach(function (pen) {
1449
+ var _a;
1450
+ var afterData = pen.onBeforeValue
1451
+ ? pen.onBeforeValue(pen, data)
1452
+ : data;
1453
+ if (data.frames) {
1454
+ _this.stopAnimate([pen]);
1455
+ if (!data.showDuration) {
1456
+ data.showDuration = data.frames.reduce(function (total, item) {
1457
+ return total + item.duration;
1458
+ }, 0);
1459
+ }
1460
+ }
1461
+ setChildValue(pen, afterData);
1462
+ _this.canvas.updateValue(pen, afterData);
1463
+ (_a = pen.onValue) === null || _a === void 0 ? void 0 : _a.call(pen, pen);
1464
+ });
1465
+ if (!this.store.data.locked &&
1466
+ this.store.active.length &&
1467
+ !this.canvas.movingPens) {
1468
+ // 移动过程中,不重算 activeRect
1469
+ this.canvas.calcActiveRect();
1470
+ }
1471
+ if (history) {
1472
+ this.pushHistory({
1473
+ type: EditType.Update,
1474
+ initPens: initPens,
1475
+ pens: pens,
1476
+ });
1477
+ }
1478
+ doEvent &&
1479
+ pens.forEach(function (pen) {
1480
+ _this.store.emitter.emit('valueUpdate', pen);
1481
+ });
1482
+ render && this.render();
1483
+ };
1484
+ /**
1485
+ * @deprecated 改用 setValue
1486
+ */
1487
+ Meta2d.prototype._setValue = function (data, history) {
1488
+ if (history === void 0) { history = false; }
1489
+ this.setValue(data, { history: history, render: false, doEvent: false });
1490
+ };
1491
+ Meta2d.prototype.pushHistory = function (action) {
1492
+ this.canvas.pushHistory(action);
1493
+ };
1494
+ Meta2d.prototype.showInput = function (pen, rect) {
1495
+ this.canvas.showInput(pen, rect);
1496
+ };
1497
+ Meta2d.prototype.hideInput = function () {
1498
+ this.canvas.hideInput();
1499
+ };
1500
+ Meta2d.prototype.clearDropdownList = function () {
1501
+ this.canvas.clearDropdownList();
1502
+ };
1503
+ Meta2d.prototype.pushChildren = function (parent, children) {
1504
+ var _this = this;
1505
+ var initUpdatePens = [deepClone(parent, true)];
1506
+ var addPens = [];
1507
+ if (!parent.children) {
1508
+ parent.children = [];
1509
+ }
1510
+ var updatePens = [];
1511
+ children.forEach(function (pen) {
1512
+ var _a;
1513
+ var oldPen = deepClone(pen, true);
1514
+ if (!pen.id || !_this.store.pens[pen.id]) {
1515
+ // 不存在于 store 中
1516
+ _this.canvas.makePen(pen);
1517
+ oldPen = null; // 添加操作
1518
+ }
1519
+ if (pen.parentId) {
1520
+ var oldParent = _this.store.pens[pen.parentId];
1521
+ var i = oldParent.children.findIndex(function (id) { return id === pen.id; });
1522
+ initUpdatePens.push(deepClone(oldParent, true));
1523
+ oldParent.children.splice(i, 1);
1524
+ updatePens.push(deepClone(oldParent, true));
1525
+ }
1526
+ parent.children.push(pen.id);
1527
+ pen.parentId = parent.id;
1528
+ var childRect = calcRelativeRect(pen.calculative.worldRect, parent.calculative.worldRect);
1529
+ Object.assign(pen, childRect);
1530
+ pen.locked = (_a = pen.lockedOnCombine) !== null && _a !== void 0 ? _a : LockState.DisableMove;
1531
+ if (!oldPen) {
1532
+ addPens.push(deepClone(pen, true));
1533
+ }
1534
+ else {
1535
+ initUpdatePens.push(oldPen);
1536
+ updatePens.push(deepClone(pen, true));
1537
+ }
1538
+ });
1539
+ updatePens.push(deepClone(parent, true));
1540
+ var step = 1;
1541
+ if (addPens.length) {
1542
+ step = 2;
1543
+ this.pushHistory({
1544
+ type: EditType.Add,
1545
+ pens: addPens,
1546
+ step: step,
1547
+ });
1548
+ }
1549
+ this.pushHistory({
1550
+ type: EditType.Update,
1551
+ initPens: initUpdatePens,
1552
+ pens: updatePens,
1553
+ step: step,
1554
+ });
1555
+ };
1556
+ Meta2d.prototype.toPng = function (padding, callback, containBkImg) {
1557
+ if (containBkImg === void 0) { containBkImg = false; }
1558
+ return this.canvas.toPng(padding, callback, containBkImg);
1559
+ };
1560
+ /**
1561
+ * 下载 png
1562
+ * @param name 传入参数自带文件后缀名 例如:'test.png'
1563
+ * @param padding 上右下左的内边距
1564
+ */
1565
+ Meta2d.prototype.downloadPng = function (name, padding) {
1566
+ var a = document.createElement('a');
1567
+ a.setAttribute('download', name || 'le5le.meta2d.png');
1568
+ a.setAttribute('href', this.toPng(padding, undefined, true));
1569
+ var evt = document.createEvent('MouseEvents');
1570
+ evt.initEvent('click', true, true);
1571
+ a.dispatchEvent(evt);
1572
+ };
1573
+ Meta2d.prototype.getRect = function (pens) {
1574
+ if (pens === void 0) { pens = this.store.data.pens; }
1575
+ return getRect(pens);
1576
+ };
1577
+ /**
1578
+ * 放大到屏幕尺寸,并居中
1579
+ * @param fit true,填满但完整展示;false,填满,但长边可能截取(即显示不完整)
1580
+ */
1581
+ Meta2d.prototype.fitView = function (fit, viewPadding) {
1582
+ if (fit === void 0) { fit = true; }
1583
+ if (viewPadding === void 0) { viewPadding = 10; }
1584
+ // 默认垂直填充,两边留白
1585
+ if (!this.hasView())
1586
+ return;
1587
+ // 1. 重置画布尺寸为容器尺寸
1588
+ var canvas = this.canvas.canvas;
1589
+ var width = canvas.offsetWidth, height = canvas.offsetHeight;
1590
+ this.resize(width, height);
1591
+ // 2. 获取设置的留白值
1592
+ var padding = formatPadding(viewPadding);
1593
+ // 3. 获取图形尺寸
1594
+ var rect = this.getRect();
1595
+ // 4. 计算缩放比例
1596
+ var w = (width - padding[1] - padding[3]) / rect.width;
1597
+ var h = (height - padding[0] - padding[2]) / rect.height;
1598
+ var ratio = w;
1599
+ if (fit) {
1600
+ // 完整显示取小的
1601
+ ratio = w > h ? h : w;
1602
+ }
1603
+ else {
1604
+ ratio = w > h ? w : h;
1605
+ }
1606
+ // 该方法直接更改画布的 scale 属性,所以比率应该乘以当前 scale
1607
+ this.scale(ratio * this.store.data.scale);
1608
+ // 5. 居中
1609
+ this.centerView();
1610
+ };
1611
+ Meta2d.prototype.centerView = function () {
1612
+ if (!this.hasView())
1613
+ return;
1614
+ var rect = this.getRect();
1615
+ var viewCenter = this.getViewCenter();
1616
+ var pensRect = this.getPenRect(rect);
1617
+ calcCenter(pensRect);
1618
+ var center = pensRect.center;
1619
+ var _a = this.store.data, scale = _a.scale, origin = _a.origin, dataX = _a.x, dataY = _a.y;
1620
+ // center 的值,在缩放和拖拽画布过程中不发生变化,是相对值
1621
+ // viewCenter 是一个绝对值,需要根据 origin 的值,来计算出相对的值
1622
+ // store.data.x 是画布偏移值,在 translate 方法中与 scale 相关,这里也需要计算
1623
+ this.translate((viewCenter.x - origin.x) / scale - center.x - dataX / scale, (viewCenter.y - origin.y) / scale - center.y - dataY / scale);
1624
+ var canvas = this.canvas.canvas;
1625
+ var x = (canvas.scrollWidth - canvas.offsetWidth) / 2;
1626
+ var y = (canvas.scrollHeight - canvas.offsetHeight) / 2;
1627
+ canvas.scrollTo(x, y);
1628
+ };
1629
+ /**
1630
+ * 画布是否有 画笔
1631
+ * RuleLine 不算
1632
+ */
1633
+ Meta2d.prototype.hasView = function () {
1634
+ return !!this.store.data.pens.filter(function (pen) { return !pen.isRuleLine; }).length;
1635
+ };
1636
+ Meta2d.prototype.getViewCenter = function () {
1637
+ var _a = this.canvas, width = _a.width, height = _a.height;
1638
+ return {
1639
+ x: width / 2,
1640
+ y: height / 2,
1641
+ };
1642
+ };
1643
+ /**
1644
+ * 大小相同
1645
+ * @param pens 画笔们
1646
+ */
1647
+ Meta2d.prototype.beSameByFirst = function (pens, attribute) {
1648
+ if (pens === void 0) { pens = this.store.data.pens; }
1649
+ var initPens = deepClone(pens); // 原 pens ,深拷贝一下
1650
+ // 1. 得到第一个画笔的 宽高 字体大小
1651
+ var firstPen = pens[0];
1652
+ var _a = this.getPenRect(firstPen), width = _a.width, height = _a.height;
1653
+ for (var i = 1; i < pens.length; i++) {
1654
+ var pen = pens[i];
1655
+ if (attribute === 'width') {
1656
+ this.setValue({ id: pen.id, width: width }, { render: false, doEvent: false });
1657
+ }
1658
+ else if (attribute === 'height') {
1659
+ this.setValue({ id: pen.id, height: height }, { render: false, doEvent: false });
1660
+ }
1661
+ else {
1662
+ this.setValue({ id: pen.id, width: width, height: height }, { render: false, doEvent: false });
1663
+ }
1664
+ }
1665
+ this.render();
1666
+ this.pushHistory({
1667
+ type: EditType.Update,
1668
+ initPens: initPens,
1669
+ pens: pens,
1670
+ });
1671
+ };
1672
+ /**
1673
+ * 格式刷(样式相同,大小无需一致。)
1674
+ * @param pens 画笔们
1675
+ */
1676
+ Meta2d.prototype.formatPainterByFirst = function (pens) {
1677
+ if (pens === void 0) { pens = this.store.data.pens; }
1678
+ var initPens = deepClone(pens); // 原 pens ,深拷贝一下
1679
+ var firstPen = pens[0];
1680
+ // 格式刷修改的属性,除开宽高
1681
+ var attrs = {};
1682
+ formatAttrs.forEach(function (attr) {
1683
+ attrs[attr] = firstPen[attr];
1684
+ });
1685
+ for (var i = 1; i < pens.length; i++) {
1686
+ var pen = pens[i];
1687
+ this.setValue(__assign({ id: pen.id }, attrs), { render: false, doEvent: false });
1688
+ }
1689
+ this.render();
1690
+ this.pushHistory({
1691
+ type: EditType.Update,
1692
+ initPens: initPens,
1693
+ pens: pens,
1694
+ });
1695
+ };
1696
+ Meta2d.prototype.alignNodes = function (align, pens, rect) {
1697
+ var e_4, _a;
1698
+ if (pens === void 0) { pens = this.store.data.pens; }
1699
+ !rect && (rect = this.getPenRect(this.getRect(pens)));
1700
+ var initPens = deepClone(pens); // 原 pens ,深拷贝一下
1701
+ try {
1702
+ for (var pens_1 = __values(pens), pens_1_1 = pens_1.next(); !pens_1_1.done; pens_1_1 = pens_1.next()) {
1703
+ var item = pens_1_1.value;
1704
+ this.alignPen(align, item, rect);
1705
+ }
1706
+ }
1707
+ catch (e_4_1) { e_4 = { error: e_4_1 }; }
1708
+ finally {
1709
+ try {
1710
+ if (pens_1_1 && !pens_1_1.done && (_a = pens_1.return)) _a.call(pens_1);
1711
+ }
1712
+ finally { if (e_4) throw e_4.error; }
1713
+ }
1714
+ this.render();
1715
+ this.pushHistory({
1716
+ type: EditType.Update,
1717
+ initPens: initPens,
1718
+ pens: pens,
1719
+ });
1720
+ };
1721
+ /**
1722
+ * 对齐画笔,基于第一个画笔
1723
+ * @param align 左对齐,右对齐,上对齐,下对齐,居中对齐
1724
+ * @param pens
1725
+ */
1726
+ Meta2d.prototype.alignNodesByFirst = function (align, pens) {
1727
+ if (pens === void 0) { pens = this.store.data.pens; }
1728
+ var initPens = deepClone(pens); // 原 pens ,深拷贝一下
1729
+ var firstPen = pens[0];
1730
+ var rect = this.getPenRect(firstPen);
1731
+ for (var i = 1; i < pens.length; i++) {
1732
+ var pen = pens[i];
1733
+ this.alignPen(align, pen, rect);
1734
+ }
1735
+ this.render();
1736
+ this.pushHistory({
1737
+ type: EditType.Update,
1738
+ initPens: initPens,
1739
+ pens: pens,
1740
+ });
1741
+ };
1742
+ /**
1743
+ * 将画笔参照 rect 进行 align 对齐
1744
+ * @param align 左对齐,右对齐,上对齐,下对齐,居中对齐
1745
+ * @param pen 当前需要对齐的画笔
1746
+ * @param rect 参照矩形
1747
+ * @returns
1748
+ */
1749
+ Meta2d.prototype.alignPen = function (align, pen, rect) {
1750
+ var penRect = this.getPenRect(pen);
1751
+ switch (align) {
1752
+ case 'left':
1753
+ penRect.x = rect.x;
1754
+ break;
1755
+ case 'right':
1756
+ penRect.x = rect.x + rect.width - penRect.width;
1757
+ break;
1758
+ case 'top':
1759
+ penRect.y = rect.y;
1760
+ break;
1761
+ case 'bottom':
1762
+ penRect.y = rect.y + rect.height - penRect.height;
1763
+ break;
1764
+ case 'center':
1765
+ penRect.x = rect.x + rect.width / 2 - penRect.width / 2;
1766
+ break;
1767
+ case 'middle':
1768
+ penRect.y = rect.y + rect.height / 2 - penRect.height / 2;
1769
+ break;
1770
+ }
1771
+ this.setValue(__assign({ id: pen.id }, penRect), { render: false, doEvent: false });
1772
+ };
1773
+ /**
1774
+ * 水平或垂直方向的均分
1775
+ * @param direction 方向,width 说明水平方向间距相同
1776
+ * @param pens 节点们,默认全部的
1777
+ * @param distance 总的宽 or 高
1778
+ */
1779
+ Meta2d.prototype.spaceBetweenByDirection = function (direction, pens, distance) {
1780
+ var e_5, _a;
1781
+ var _this = this;
1782
+ if (pens === void 0) { pens = this.store.data.pens; }
1783
+ !distance && (distance = this.getPenRect(this.getRect(pens))[direction]);
1784
+ // 过滤出非父节点
1785
+ pens = pens.filter(function (item) { return !item.parentId; });
1786
+ if (pens.length <= 2) {
1787
+ return;
1788
+ }
1789
+ var initPens = deepClone(pens); // 原 pens ,深拷贝一下
1790
+ // 计算间距
1791
+ var allDistance = pens.reduce(function (distance, currentPen) {
1792
+ var currentPenRect = _this.getPenRect(currentPen);
1793
+ return distance + currentPenRect[direction];
1794
+ }, 0);
1795
+ var space = (distance - allDistance) / (pens.length - 1);
1796
+ // 按照大小顺序排列画笔
1797
+ pens = pens.sort(function (a, b) {
1798
+ if (direction === 'width') {
1799
+ return a.x - b.x;
1800
+ }
1801
+ return a.y - b.y;
1802
+ });
1803
+ var pen0Rect = this.getPenRect(pens[0]);
1804
+ var left = direction === 'width' ? pen0Rect.x : pen0Rect.y;
1805
+ try {
1806
+ for (var pens_2 = __values(pens), pens_2_1 = pens_2.next(); !pens_2_1.done; pens_2_1 = pens_2.next()) {
1807
+ var pen = pens_2_1.value;
1808
+ var penRect = this.getPenRect(pen);
1809
+ direction === 'width' ? (penRect.x = left) : (penRect.y = left);
1810
+ left += penRect[direction] + space;
1811
+ this.setValue(__assign({ id: pen.id }, penRect), { render: false, doEvent: false });
1812
+ }
1813
+ }
1814
+ catch (e_5_1) { e_5 = { error: e_5_1 }; }
1815
+ finally {
1816
+ try {
1817
+ if (pens_2_1 && !pens_2_1.done && (_a = pens_2.return)) _a.call(pens_2);
1818
+ }
1819
+ finally { if (e_5) throw e_5.error; }
1820
+ }
1821
+ this.render();
1822
+ this.pushHistory({
1823
+ type: EditType.Update,
1824
+ initPens: initPens,
1825
+ pens: pens,
1826
+ });
1827
+ };
1828
+ Meta2d.prototype.spaceBetween = function (pens, width) {
1829
+ this.spaceBetweenByDirection('width', pens, width);
1830
+ };
1831
+ Meta2d.prototype.spaceBetweenColumn = function (pens, height) {
1832
+ this.spaceBetweenByDirection('height', pens, height);
1833
+ };
1834
+ Meta2d.prototype.layout = function (pens, width, space) {
1835
+ var _this = this;
1836
+ if (pens === void 0) { pens = this.store.data.pens; }
1837
+ if (space === void 0) { space = 30; }
1838
+ var rect = this.getPenRect(getRect(pens));
1839
+ !width && (width = rect.width);
1840
+ // 1. 拿到全部节点中最大的高
1841
+ pens = pens.filter(function (item) { return !item.type && !item.parentId; });
1842
+ var initPens = deepClone(pens); // 原 pens ,深拷贝一下
1843
+ var maxHeight = 0;
1844
+ pens.forEach(function (pen) {
1845
+ var penRect = _this.getPenRect(pen);
1846
+ penRect.height > maxHeight && (maxHeight = penRect.height);
1847
+ });
1848
+ // 2. 遍历节点调整位置
1849
+ var currentX = rect.x;
1850
+ var currentY = rect.y;
1851
+ pens.forEach(function (pen, index) {
1852
+ var penRect = _this.getPenRect(pen);
1853
+ penRect.x = currentX;
1854
+ penRect.y = currentY + maxHeight / 2 - penRect.height / 2;
1855
+ _this.setValue(__assign({ id: pen.id }, penRect), { render: false, doEvent: false });
1856
+ if (index === pens.length - 1) {
1857
+ return;
1858
+ }
1859
+ var currentWidth = currentX + penRect.width - rect.x;
1860
+ var nextPenRect = _this.getPenRect(pens[index + 1]);
1861
+ if (Math.round(width - currentWidth) >=
1862
+ Math.round(nextPenRect.width + space))
1863
+ // 当前行
1864
+ currentX += penRect.width + space;
1865
+ else {
1866
+ // 换行
1867
+ currentX = rect.x;
1868
+ currentY += maxHeight + space;
1869
+ }
1870
+ });
1871
+ this.render();
1872
+ this.pushHistory({
1873
+ type: EditType.Update,
1874
+ initPens: initPens,
1875
+ pens: pens,
1876
+ });
1877
+ };
1878
+ Meta2d.prototype.gotoView = function (pen) {
1879
+ var center = this.getViewCenter();
1880
+ var x = center.x -
1881
+ pen.calculative.worldRect.x -
1882
+ pen.calculative.worldRect.width / 2;
1883
+ var y = center.y -
1884
+ pen.calculative.worldRect.y -
1885
+ pen.calculative.worldRect.height / 2;
1886
+ if (this.canvas.scroll && this.canvas.scroll.isShow) {
1887
+ this.canvas.scroll.translate(x - this.store.data.x, y - this.store.data.y);
1888
+ }
1889
+ this.store.data.x = x;
1890
+ this.store.data.y = y;
1891
+ this.canvas.canvasImage.init();
1892
+ this.canvas.canvasImageBottom.init();
1893
+ this.render();
1894
+ };
1895
+ Meta2d.prototype.showMap = function () {
1896
+ if (!this.map) {
1897
+ this.map = new ViewMap(this.canvas);
1898
+ }
1899
+ this.map.show();
1900
+ };
1901
+ Meta2d.prototype.hideMap = function () {
1902
+ this.map.hide();
1903
+ };
1904
+ Meta2d.prototype.onSizeUpdate = function () {
1905
+ var _this = this;
1906
+ if (this.mapTimer) {
1907
+ clearTimeout(this.mapTimer);
1908
+ this.mapTimer = undefined;
1909
+ }
1910
+ this.mapTimer = setTimeout(function () {
1911
+ if (_this.map && _this.map.isShow) {
1912
+ _this.map.show();
1913
+ }
1914
+ if (_this.canvas.scroll && _this.canvas.scroll.isShow) {
1915
+ _this.canvas.scroll.resize();
1916
+ }
1917
+ }, 500);
1918
+ };
1919
+ Meta2d.prototype.toggleAnchorMode = function () {
1920
+ this.canvas.toggleAnchorMode();
1921
+ };
1922
+ Meta2d.prototype.addAnchorHand = function () {
1923
+ this.canvas.addAnchorHand();
1924
+ };
1925
+ Meta2d.prototype.removeAnchorHand = function () {
1926
+ this.canvas.removeAnchorHand();
1927
+ };
1928
+ Meta2d.prototype.toggleAnchorHand = function () {
1929
+ this.canvas.toggleAnchorHand();
1930
+ };
1931
+ /**
1932
+ * 将该画笔置顶,即放到数组最后,最后绘制即在顶部
1933
+ * @param pen pen 置顶的画笔
1934
+ * @param pens 画笔们,注意 pen 必须在该数组内才有效
1935
+ */
1936
+ Meta2d.prototype.top = function (pen, pens) {
1937
+ var _this = this;
1938
+ if (pens === void 0) { pens = this.store.data.pens; }
1939
+ // 获取它包含它的子节点
1940
+ var allIds = __spreadArray(__spreadArray([], __read(getAllChildren(pen, this.store)), false), [pen], false).map(function (p) { return p.id; });
1941
+ var allPens = pens.filter(function (p) { return allIds.includes(p.id); });
1942
+ allPens.forEach(function (pen) {
1943
+ var index = pens.findIndex(function (p) { return p.id === pen.id; });
1944
+ if (index > -1) {
1945
+ pens.push(pens[index]);
1946
+ pens.splice(index, 1);
1947
+ _this.initImageCanvas([pen]);
1948
+ }
1949
+ });
1950
+ };
1951
+ /**
1952
+ * 若本次改变的画笔存在图片,并且在上层 or 下层,需要擦除上层 or 下层
1953
+ * 子节点中包含图片,也需要重绘
1954
+ * @param pens 本次改变的 pens
1955
+ */
1956
+ Meta2d.prototype.initImageCanvas = function (pens) {
1957
+ this.canvas.initImageCanvas(pens);
1958
+ };
1959
+ /**
1960
+ * 该画笔置底,即放到数组最前,最后绘制即在底部
1961
+ * @param pens 画笔们,注意 pen 必须在该数组内才有效
1962
+ */
1963
+ Meta2d.prototype.bottom = function (pen, pens) {
1964
+ if (pens === void 0) { pens = this.store.data.pens; }
1965
+ var allIds = __spreadArray(__spreadArray([], __read(getAllChildren(pen, this.store)), false), [pen], false).map(function (p) { return p.id; });
1966
+ var allPens = pens.filter(function (p) { return allIds.includes(p.id); });
1967
+ var _loop_3 = function (i) {
1968
+ var pen_1 = allPens[i];
1969
+ var index = pens.findIndex(function (p) { return p.id === pen_1.id; });
1970
+ if (index > -1) {
1971
+ pens.unshift(pens[index]);
1972
+ pens.splice(index + 1, 1);
1973
+ this_1.initImageCanvas([pen_1]);
1974
+ }
1975
+ };
1976
+ var this_1 = this;
1977
+ // 从后往前,保证 allPens 顺序不变
1978
+ for (var i = allPens.length - 1; i >= 0; i--) {
1979
+ _loop_3(i);
1980
+ }
1981
+ };
1982
+ /**
1983
+ * 该画笔上移,即把该画笔在数组中的位置向后移动一个
1984
+ * @param pens 画笔们,注意 pen 必须在该数组内才有效
1985
+ */
1986
+ Meta2d.prototype.up = function (pen, pens) {
1987
+ if (pens === void 0) { pens = this.store.data.pens; }
1988
+ var index = pens.findIndex(function (p) { return p.id === pen.id; });
1989
+ if (index > -1 && index !== pens.length - 1) {
1990
+ pens.splice(index + 2, 0, pens[index]);
1991
+ pens.splice(index, 1);
1992
+ this.initImageCanvas([pen]);
1993
+ }
1994
+ };
1995
+ /**
1996
+ * 该画笔下移,即把该画笔在该数组中的位置前移一个
1997
+ * @param pens 画笔们,注意 pen 必须在该数组内才有效
1998
+ */
1999
+ Meta2d.prototype.down = function (pen, pens) {
2000
+ if (pens === void 0) { pens = this.store.data.pens; }
2001
+ var index = pens.findIndex(function (p) { return p.id === pen.id; });
2002
+ if (index > -1 && index !== 0) {
2003
+ pens.splice(index - 1, 0, pens[index]);
2004
+ pens.splice(index + 1, 1);
2005
+ this.initImageCanvas([pen]);
2006
+ }
2007
+ };
2008
+ Meta2d.prototype.setLayer = function (pen, toIndex, pens) {
2009
+ if (pens === void 0) { pens = this.store.data.pens; }
2010
+ var index = pens.findIndex(function (p) { return p.id === pen.id; });
2011
+ if (index > -1) {
2012
+ if (index > toIndex) {
2013
+ // 原位置在后,新位置在前
2014
+ pens.splice(toIndex, 0, pens[index]);
2015
+ pens.splice(index + 1, 1);
2016
+ }
2017
+ else if (index < toIndex) {
2018
+ // 新位置在后
2019
+ pens.splice(toIndex, 0, pens[index]);
2020
+ pens.splice(index, 1);
2021
+ }
2022
+ }
2023
+ };
2024
+ Meta2d.prototype.changePenId = function (oldId, newId) {
2025
+ this.canvas.changePenId(oldId, newId);
2026
+ };
2027
+ /**
2028
+ * 得到与当前节点连接的线
2029
+ * @param node 节点,非连线
2030
+ * @param type 类型,全部的连接线/入线/出线
2031
+ */
2032
+ Meta2d.prototype.getLines = function (node, type) {
2033
+ var _this = this;
2034
+ var _a;
2035
+ if (type === void 0) { type = 'all'; }
2036
+ if (node.type === PenType.Line) {
2037
+ return [];
2038
+ }
2039
+ var lines = [];
2040
+ (_a = node.connectedLines) === null || _a === void 0 ? void 0 : _a.forEach(function (_a) {
2041
+ var lineId = _a.lineId;
2042
+ var line = _this.store.pens[lineId];
2043
+ if (!line) {
2044
+ console.warn(node, 'node contain a error connectedLine');
2045
+ return;
2046
+ }
2047
+ switch (type) {
2048
+ case 'all':
2049
+ lines.push(line);
2050
+ break;
2051
+ case 'in':
2052
+ // 进入该节点的线,即 线锚点的最后一个 connectTo 对应该节点
2053
+ getToAnchor(line).connectTo === node.id && lines.push(line);
2054
+ break;
2055
+ case 'out':
2056
+ // 从该节点出去的线,即 线锚点的第一个 connectTo 对应该节点
2057
+ getFromAnchor(line).connectTo === node.id && lines.push(line);
2058
+ break;
2059
+ }
2060
+ });
2061
+ return lines;
2062
+ };
2063
+ /**
2064
+ * 得到当前节点的下一个节点,即出口节点数组
2065
+ * 得到当前连线的出口节点
2066
+ * @param pen 节点或连线
2067
+ */
2068
+ Meta2d.prototype.nextNode = function (pen) {
2069
+ var _this = this;
2070
+ if (pen.type === PenType.Line) {
2071
+ var nextNode = this.store.pens[getToAnchor(pen).connectTo];
2072
+ return nextNode ? [nextNode] : [];
2073
+ }
2074
+ else {
2075
+ // 1. 得到所有的出线
2076
+ var lines = this.getLines(pen, 'out');
2077
+ var nextNodes_1 = [];
2078
+ // 2. 遍历出线的 nextNode
2079
+ lines.forEach(function (line) {
2080
+ var e_6, _a;
2081
+ var lineNextNode = _this.nextNode(line);
2082
+ var _loop_4 = function (node) {
2083
+ var have = nextNodes_1.find(function (next) { return next.id === node.id; });
2084
+ // 3. 不重复的才加进去
2085
+ !have && nextNodes_1.push(node);
2086
+ };
2087
+ try {
2088
+ for (var lineNextNode_1 = __values(lineNextNode), lineNextNode_1_1 = lineNextNode_1.next(); !lineNextNode_1_1.done; lineNextNode_1_1 = lineNextNode_1.next()) {
2089
+ var node = lineNextNode_1_1.value;
2090
+ _loop_4(node);
2091
+ }
2092
+ }
2093
+ catch (e_6_1) { e_6 = { error: e_6_1 }; }
2094
+ finally {
2095
+ try {
2096
+ if (lineNextNode_1_1 && !lineNextNode_1_1.done && (_a = lineNextNode_1.return)) _a.call(lineNextNode_1);
2097
+ }
2098
+ finally { if (e_6) throw e_6.error; }
2099
+ }
2100
+ });
2101
+ return nextNodes_1;
2102
+ }
2103
+ };
2104
+ /**
2105
+ * 得到当前节点的上一个节点,即入口节点数组
2106
+ * 得到当前连线的入口节点
2107
+ * @param pen 节点或连线
2108
+ */
2109
+ Meta2d.prototype.previousNode = function (pen) {
2110
+ var _this = this;
2111
+ if (pen.type === PenType.Line) {
2112
+ var preNode = this.store.pens[getFromAnchor(pen).connectTo];
2113
+ return preNode ? [preNode] : [];
2114
+ }
2115
+ else {
2116
+ // 1. 得到所有的入线
2117
+ var lines = this.getLines(pen, 'in');
2118
+ var preNodes_1 = [];
2119
+ // 2. 遍历入线的 preNode
2120
+ lines.forEach(function (line) {
2121
+ var e_7, _a;
2122
+ var linePreNode = _this.previousNode(line);
2123
+ var _loop_5 = function (node) {
2124
+ var have = preNodes_1.find(function (pre) { return pre.id === node.id; });
2125
+ // 3. 不重复的才加进去
2126
+ !have && preNodes_1.push(node);
2127
+ };
2128
+ try {
2129
+ for (var linePreNode_1 = __values(linePreNode), linePreNode_1_1 = linePreNode_1.next(); !linePreNode_1_1.done; linePreNode_1_1 = linePreNode_1.next()) {
2130
+ var node = linePreNode_1_1.value;
2131
+ _loop_5(node);
2132
+ }
2133
+ }
2134
+ catch (e_7_1) { e_7 = { error: e_7_1 }; }
2135
+ finally {
2136
+ try {
2137
+ if (linePreNode_1_1 && !linePreNode_1_1.done && (_a = linePreNode_1.return)) _a.call(linePreNode_1);
2138
+ }
2139
+ finally { if (e_7) throw e_7.error; }
2140
+ }
2141
+ });
2142
+ return preNodes_1;
2143
+ }
2144
+ };
2145
+ /**
2146
+ * 获取节点所有的下一个连接关系
2147
+ * @param pen
2148
+ *
2149
+ */
2150
+ Meta2d.prototype.getNext = function (pen) {
2151
+ var _this = this;
2152
+ var _a;
2153
+ if (pen.type === PenType.Line) {
2154
+ console.warn('非连线节点');
2155
+ return;
2156
+ }
2157
+ var next = [];
2158
+ (_a = pen.connectedLines) === null || _a === void 0 ? void 0 : _a.forEach(function (_a) {
2159
+ var _b, _c;
2160
+ var lineId = _a.lineId, anchor = _a.anchor;
2161
+ var fromAnchor = (_b = pen.anchors) === null || _b === void 0 ? void 0 : _b.filter(function (_anchor) { return _anchor.id === anchor; })[0];
2162
+ var line = _this.findOne(lineId);
2163
+ if (line.anchors[0].connectTo == pen.id) {
2164
+ //from
2165
+ var connectTo = line.anchors[line.anchors.length - 1].connectTo;
2166
+ if (connectTo) {
2167
+ var _next = _this.findOne(connectTo);
2168
+ var connectedLine_1 = (_c = _next.connectedLines) === null || _c === void 0 ? void 0 : _c.filter(function (item) { return item.lineId === line.id; })[0];
2169
+ var penAnchor = _next.anchors.filter(function (_anchor) { return _anchor.id === connectedLine_1.anchor; })[0];
2170
+ next.push({
2171
+ from: pen,
2172
+ fromAnchor: fromAnchor,
2173
+ line: line,
2174
+ to: _next,
2175
+ toAnchor: penAnchor,
2176
+ });
2177
+ }
2178
+ }
2179
+ });
2180
+ return next;
2181
+ };
2182
+ /**
2183
+ * 为画布添加锚点
2184
+ * @param pen 画笔
2185
+ * @param anchor 待添加锚点
2186
+ * @param index 连线类型 添加锚点到哪个位置
2187
+ */
2188
+ Meta2d.prototype.addAnchor = function (pen, anchor, index) {
2189
+ if (!pen) {
2190
+ return;
2191
+ }
2192
+ if (!pen.anchors) {
2193
+ pen.anchors = [];
2194
+ }
2195
+ if (!pen.calculative.worldAnchors) {
2196
+ pen.calculative.worldAnchors = [];
2197
+ }
2198
+ if (pen.type === PenType.Line) {
2199
+ if (index < 0) {
2200
+ index = pen.anchors.length + 1 + index;
2201
+ }
2202
+ if (index > pen.anchors.length) {
2203
+ index = pen.anchors.length;
2204
+ }
2205
+ if (index < 0) {
2206
+ index = 0;
2207
+ }
2208
+ if ((index == 0 && pen.anchors[0].connectTo) ||
2209
+ (index == pen.anchors.length && pen.anchors[index - 1].connectTo)) {
2210
+ console.warn('端点存在连接关系');
2211
+ return;
2212
+ }
2213
+ }
2214
+ var _anchor = null;
2215
+ var _worldAnchor = null;
2216
+ if (anchor.x <= 1 && anchor.x >= 0 && anchor.y <= 1 && anchor.y >= 0) {
2217
+ //relative
2218
+ _worldAnchor = {
2219
+ id: anchor.id || s8(),
2220
+ penId: pen.id,
2221
+ x: pen.calculative.worldRect.x +
2222
+ pen.calculative.worldRect.width * anchor.x,
2223
+ y: pen.calculative.worldRect.y +
2224
+ pen.calculative.worldRect.height * anchor.y,
2225
+ };
2226
+ if (pen.calculative.worldRect) {
2227
+ if (pen.rotate % 360) {
2228
+ rotatePoint(_worldAnchor, pen.rotate, pen.calculative.worldRect.center);
2229
+ }
2230
+ }
2231
+ _anchor = {
2232
+ id: _worldAnchor.id,
2233
+ penId: pen.id,
2234
+ x: anchor.x,
2235
+ y: anchor.y,
2236
+ };
2237
+ }
2238
+ else {
2239
+ //absolute
2240
+ _worldAnchor = {
2241
+ id: anchor.id || s8(),
2242
+ penId: pen.id,
2243
+ x: anchor.x,
2244
+ y: anchor.y,
2245
+ };
2246
+ if (pen.calculative.worldRect) {
2247
+ if (pen.rotate % 360) {
2248
+ rotatePoint(anchor, -pen.rotate, pen.calculative.worldRect.center);
2249
+ }
2250
+ _anchor = {
2251
+ id: _worldAnchor.id,
2252
+ penId: pen.id,
2253
+ x: (anchor.x - pen.calculative.worldRect.x) /
2254
+ pen.calculative.worldRect.width,
2255
+ y: (anchor.y - pen.calculative.worldRect.y) /
2256
+ pen.calculative.worldRect.height,
2257
+ };
2258
+ }
2259
+ }
2260
+ if (pen.type === PenType.Line) {
2261
+ //Line
2262
+ pen.calculative.worldAnchors.splice(index, 0, _worldAnchor);
2263
+ pen.anchors.splice(index, 0, _anchor);
2264
+ this.canvas.updateLines(pen);
2265
+ this.canvas.initLineRect(pen);
2266
+ this.render();
2267
+ }
2268
+ else {
2269
+ //Node
2270
+ pen.calculative.worldAnchors.push(_worldAnchor);
2271
+ pen.anchors.push(_anchor);
2272
+ }
2273
+ };
2274
+ /**
2275
+ *
2276
+ * @param from 连接节点
2277
+ * @param fromAnchor 连接节点锚点
2278
+ * @param to 被连接节点
2279
+ * @param toAnchor 被连接节点锚点
2280
+ */
2281
+ Meta2d.prototype.connectLine = function (from, to, fromAnchor, toAnchor, render) {
2282
+ if (render === void 0) { render = true; }
2283
+ if (!fromAnchor) {
2284
+ var _worldRect = to.calculative.worldRect;
2285
+ fromAnchor = nearestAnchor(from, {
2286
+ x: _worldRect.x + _worldRect.width / 2,
2287
+ y: _worldRect.y + _worldRect.height / 2,
2288
+ });
2289
+ }
2290
+ if (!toAnchor) {
2291
+ var _worldRect = from.calculative.worldRect;
2292
+ toAnchor = nearestAnchor(to, {
2293
+ x: _worldRect.x + _worldRect.width / 2,
2294
+ y: _worldRect.y + _worldRect.height / 2,
2295
+ });
2296
+ }
2297
+ var absWidth = Math.abs(fromAnchor.x - toAnchor.x);
2298
+ var absHeight = Math.abs(fromAnchor.y - toAnchor.y);
2299
+ var line = {
2300
+ height: absHeight,
2301
+ lineName: 'line',
2302
+ lineWidth: 1,
2303
+ name: 'line',
2304
+ type: 1,
2305
+ width: absWidth,
2306
+ x: Math.min(fromAnchor.x, toAnchor.x),
2307
+ y: Math.min(fromAnchor.y, toAnchor.y),
2308
+ anchors: [
2309
+ {
2310
+ x: fromAnchor.x > toAnchor.x ? 1 : 0,
2311
+ y: fromAnchor.y > toAnchor.y ? 1 : 0,
2312
+ id: s8(),
2313
+ },
2314
+ {
2315
+ x: fromAnchor.x > toAnchor.x ? 0 : 1,
2316
+ y: fromAnchor.x > toAnchor.x ? 0 : 1,
2317
+ id: s8(),
2318
+ },
2319
+ ],
2320
+ };
2321
+ this.addPens([line]);
2322
+ connectLine(from, fromAnchor, line, line.calculative.worldAnchors[0]);
2323
+ connectLine(to, toAnchor, line, line.calculative.worldAnchors[1]);
2324
+ line.calculative.active = false;
2325
+ this.canvas.updateLines(line);
2326
+ this.canvas.updateLines(from);
2327
+ this.canvas.updateLines(to);
2328
+ this.canvas.initLineRect(line);
2329
+ if (render) {
2330
+ this.render();
2331
+ }
2332
+ return line;
2333
+ };
2334
+ /**
2335
+ * 生成一个拷贝组合后的 画笔数组(组合图形),不影响原画布画笔,常用作 二次复用的组件
2336
+ * @param pens 画笔数组
2337
+ * @param showChild 是否作为状态复用(参考 combine showChild)
2338
+ * @param anchor 是否产生默认的锚点
2339
+ * @returns 组合图形
2340
+ */
2341
+ Meta2d.prototype.toComponent = function (pens, showChild, anchor) {
2342
+ if (pens === void 0) { pens = this.store.data.pens; }
2343
+ if (pens.length === 1) {
2344
+ var pen = deepClone(pens[0]);
2345
+ pen.type = PenType.Node;
2346
+ pen.id = undefined;
2347
+ return [pen];
2348
+ }
2349
+ var components = deepClone(pens, true);
2350
+ var rect = getRect(components);
2351
+ var parent = __assign(__assign({ id: s8(), name: 'combine' }, rect), { children: [], showChild: showChild });
2352
+ if (anchor) {
2353
+ parent.anchors = [
2354
+ {
2355
+ id: '0',
2356
+ penId: parent.id,
2357
+ x: 0.5,
2358
+ y: 0,
2359
+ },
2360
+ {
2361
+ id: '1',
2362
+ penId: parent.id,
2363
+ x: 1,
2364
+ y: 0.5,
2365
+ },
2366
+ {
2367
+ id: '2',
2368
+ penId: parent.id,
2369
+ x: 0.5,
2370
+ y: 1,
2371
+ },
2372
+ {
2373
+ id: '3',
2374
+ penId: parent.id,
2375
+ x: 0,
2376
+ y: 0.5,
2377
+ },
2378
+ ];
2379
+ }
2380
+ var p = components.find(function (pen) {
2381
+ return pen.width === rect.width && pen.height === rect.height;
2382
+ });
2383
+ var oneIsParent = p && showChild === undefined;
2384
+ if (oneIsParent) {
2385
+ if (!p.children) {
2386
+ p.children = [];
2387
+ }
2388
+ parent = p;
2389
+ }
2390
+ else {
2391
+ // 不影响画布数据,生成一个组合图形便于二次复用
2392
+ // this.canvas.makePen(parent);
2393
+ }
2394
+ components.forEach(function (pen) {
2395
+ var _a;
2396
+ if (pen === parent || pen.parentId === parent.id) {
2397
+ return;
2398
+ }
2399
+ if (pen.parentId) {
2400
+ // 已经是其它节点的子节点,x,y,w,h 已经是百分比了
2401
+ return;
2402
+ }
2403
+ parent.children.push(pen.id);
2404
+ pen.parentId = parent.id;
2405
+ var childRect = calcRelativeRect(pen.calculative.worldRect, rect);
2406
+ Object.assign(pen, childRect);
2407
+ pen.locked = (_a = pen.lockedOnCombine) !== null && _a !== void 0 ? _a : LockState.DisableMove;
2408
+ // pen.type = PenType.Node;
2409
+ });
2410
+ return oneIsParent
2411
+ ? deepClone(components)
2412
+ : deepClone(__spreadArray([parent], __read(components), false));
2413
+ };
2414
+ Meta2d.prototype.setVisible = function (pen, visible, render) {
2415
+ var e_8, _a;
2416
+ if (render === void 0) { render = true; }
2417
+ this.onSizeUpdate();
2418
+ this.setValue({ id: pen.id, visible: visible }, { render: false, doEvent: false });
2419
+ if (pen.children) {
2420
+ try {
2421
+ for (var _b = __values(pen.children), _c = _b.next(); !_c.done; _c = _b.next()) {
2422
+ var childId = _c.value;
2423
+ var child = this.store.pens[childId];
2424
+ child && this.setVisible(child, visible, false);
2425
+ }
2426
+ }
2427
+ catch (e_8_1) { e_8 = { error: e_8_1 }; }
2428
+ finally {
2429
+ try {
2430
+ if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
2431
+ }
2432
+ finally { if (e_8) throw e_8.error; }
2433
+ }
2434
+ }
2435
+ render && this.render();
2436
+ };
2437
+ Meta2d.prototype.clearHover = function () {
2438
+ this.canvas.clearHover();
2439
+ };
2440
+ Meta2d.prototype.closeSocket = function () {
2441
+ this.closeWebsocket();
2442
+ this.closeMqtt();
2443
+ this.closeHttp();
2444
+ };
2445
+ Meta2d.prototype.destroy = function (onlyData) {
2446
+ this.clear(false);
2447
+ this.closeSocket();
2448
+ this.store.emitter.all.clear(); // 内存释放
2449
+ this.canvas.destroy();
2450
+ this.canvas = undefined;
2451
+ globalStore[this.store.id] = undefined;
2452
+ if (!onlyData) {
2453
+ for (var k in globalStore) {
2454
+ delete globalStore[k];
2455
+ }
2456
+ globalStore.path2dDraws = {};
2457
+ globalStore.canvasDraws = {};
2458
+ globalStore.anchors = {};
2459
+ globalStore.htmlElements = {};
2460
+ }
2461
+ };
2462
+ return Meta2d;
2463
+ }());
2464
+ export { Meta2d };
2465
+ //# sourceMappingURL=core.js.map