@easy-editor/plugin-dashboard 0.0.1

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.
@@ -0,0 +1,873 @@
1
+ 'use strict';
2
+
3
+ var core = require('@easy-editor/core');
4
+ var mobx = require('mobx');
5
+
6
+ function _applyDecs2311(e, t, n, r, o, i) {
7
+ var a,
8
+ c,
9
+ u,
10
+ s,
11
+ f,
12
+ l,
13
+ p,
14
+ d = Symbol.metadata || Symbol.for("Symbol.metadata"),
15
+ m = Object.defineProperty,
16
+ h = Object.create,
17
+ y = [h(null), h(null)],
18
+ v = t.length;
19
+ function g(t, n, r) {
20
+ return function (o, i) {
21
+ n && (i = o, o = e);
22
+ for (var a = 0; a < t.length; a++) i = t[a].apply(o, r ? [i] : []);
23
+ return r ? i : o;
24
+ };
25
+ }
26
+ function b(e, t, n, r) {
27
+ if ("function" != typeof e && (r || void 0 !== e)) throw new TypeError(t + " must " + (n || "be") + " a function" + (r ? "" : " or undefined"));
28
+ return e;
29
+ }
30
+ function applyDec(e, t, n, r, o, i, u, s, f, l, p) {
31
+ function d(e) {
32
+ if (!p(e)) throw new TypeError("Attempted to access private element on non-instance");
33
+ }
34
+ var h = [].concat(t[0]),
35
+ v = t[3],
36
+ w = !u,
37
+ D = 1 === o,
38
+ S = 3 === o,
39
+ j = 4 === o,
40
+ E = 2 === o;
41
+ function I(t, n, r) {
42
+ return function (o, i) {
43
+ return n && (i = o, o = e), r && r(o), P[t].call(o, i);
44
+ };
45
+ }
46
+ if (!w) {
47
+ var P = {},
48
+ k = [],
49
+ F = S ? "get" : j || D ? "set" : "value";
50
+ if (f ? (l || D ? P = {
51
+ get: _setFunctionName(function () {
52
+ return v(this);
53
+ }, r, "get"),
54
+ set: function (e) {
55
+ t[4](this, e);
56
+ }
57
+ } : P[F] = v, l || _setFunctionName(P[F], r, E ? "" : F)) : l || (P = Object.getOwnPropertyDescriptor(e, r)), !l && !f) {
58
+ if ((c = y[+s][r]) && 7 != (c ^ o)) throw Error("Decorating two elements with the same name (" + P[F].name + ") is not supported yet");
59
+ y[+s][r] = o < 3 ? 1 : o;
60
+ }
61
+ }
62
+ for (var N = e, O = h.length - 1; O >= 0; O -= n ? 2 : 1) {
63
+ var T = b(h[O], "A decorator", "be", !0),
64
+ z = n ? h[O - 1] : void 0,
65
+ A = {},
66
+ H = {
67
+ kind: ["field", "accessor", "method", "getter", "setter", "class"][o],
68
+ name: r,
69
+ metadata: a,
70
+ addInitializer: function (e, t) {
71
+ if (e.v) throw new TypeError("attempted to call addInitializer after decoration was finished");
72
+ b(t, "An initializer", "be", !0), i.push(t);
73
+ }.bind(null, A)
74
+ };
75
+ if (w) c = T.call(z, N, H), A.v = 1, b(c, "class decorators", "return") && (N = c);else if (H.static = s, H.private = f, c = H.access = {
76
+ has: f ? p.bind() : function (e) {
77
+ return r in e;
78
+ }
79
+ }, j || (c.get = f ? E ? function (e) {
80
+ return d(e), P.value;
81
+ } : I("get", 0, d) : function (e) {
82
+ return e[r];
83
+ }), E || S || (c.set = f ? I("set", 0, d) : function (e, t) {
84
+ e[r] = t;
85
+ }), N = T.call(z, D ? {
86
+ get: P.get,
87
+ set: P.set
88
+ } : P[F], H), A.v = 1, D) {
89
+ if ("object" == typeof N && N) (c = b(N.get, "accessor.get")) && (P.get = c), (c = b(N.set, "accessor.set")) && (P.set = c), (c = b(N.init, "accessor.init")) && k.unshift(c);else if (void 0 !== N) throw new TypeError("accessor decorators must return an object with get, set, or init properties or undefined");
90
+ } else b(N, (l ? "field" : "method") + " decorators", "return") && (l ? k.unshift(N) : P[F] = N);
91
+ }
92
+ return o < 2 && u.push(g(k, s, 1), g(i, s, 0)), l || w || (f ? D ? u.splice(-1, 0, I("get", s), I("set", s)) : u.push(E ? P[F] : b.call.bind(P[F])) : m(e, r, P)), N;
93
+ }
94
+ function w(e) {
95
+ return m(e, d, {
96
+ configurable: !0,
97
+ enumerable: !0,
98
+ value: a
99
+ });
100
+ }
101
+ return a = h(null == a ? null : a), f = [], l = function (e) {
102
+ e && f.push(g(e));
103
+ }, p = function (t, r) {
104
+ for (var i = 0; i < n.length; i++) {
105
+ var a = n[i],
106
+ c = a[1],
107
+ l = 7 & c;
108
+ if ((8 & c) == t && !l == r) {
109
+ var p = a[2],
110
+ d = !!a[3],
111
+ m = 16 & c;
112
+ applyDec(t ? e : e.prototype, a, m, d ? "#" + p : _toPropertyKey(p), l, l < 2 ? [] : t ? s = s || [] : u = u || [], f, !!t, d, r, t && d ? function (t) {
113
+ return _checkInRHS(t) === e;
114
+ } : o);
115
+ }
116
+ }
117
+ }, p(8, 0), p(0, 0), p(8, 1), p(0, 1), l(u), l(s), c = f, v || w(e), {
118
+ e: c,
119
+ get c() {
120
+ var n = [];
121
+ return v && [w(e = applyDec(e, [t], r, e.name, 5, n)), g(n, 1)];
122
+ }
123
+ };
124
+ }
125
+ function _checkInRHS(e) {
126
+ if (Object(e) !== e) throw TypeError("right-hand side of 'in' should be an object, got " + (null !== e ? typeof e : "null"));
127
+ return e;
128
+ }
129
+ function _setFunctionName(e, t, n) {
130
+ "symbol" == typeof t && (t = (t = t.description) ? "[" + t + "]" : "");
131
+ try {
132
+ Object.defineProperty(e, "name", {
133
+ configurable: !0,
134
+ value: n ? n + " " + t : t
135
+ });
136
+ } catch (e) {}
137
+ return e;
138
+ }
139
+ function _toPrimitive(t, r) {
140
+ if ("object" != typeof t || !t) return t;
141
+ var e = t[Symbol.toPrimitive];
142
+ if (void 0 !== e) {
143
+ var i = e.call(t, r || "default");
144
+ if ("object" != typeof i) return i;
145
+ throw new TypeError("@@toPrimitive must return a primitive value.");
146
+ }
147
+ return ("string" === r ? String : Number)(t);
148
+ }
149
+ function _toPropertyKey(t) {
150
+ var i = _toPrimitive(t, "string");
151
+ return "symbol" == typeof i ? i : i + "";
152
+ }
153
+
154
+ let _initProto, _init_enabled, _init_extra_enabled, _init_guideLines, _init_extra_guideLines, _init_nodeLineMap, _init_extra_nodeLineMap, _init_adsorptionLines, _init_extra_adsorptionLines;
155
+ class GuideLine {
156
+ static {
157
+ [_init_enabled, _init_extra_enabled, _init_guideLines, _init_extra_guideLines, _init_nodeLineMap, _init_extra_nodeLineMap, _init_adsorptionLines, _init_extra_adsorptionLines, _initProto] = _applyDecs2311(this, [], [[mobx.observable, 1, "enabled"], [[mobx.observable, mobx.observable.shallow], 17, "guideLines"], [mobx.computed, 3, "guideLinesMap"], [[mobx.observable, mobx.observable.shallow], 17, "nodeLineMap"], [mobx.observable, 1, "adsorptionLines"], [mobx.action, 2, "addGuideLine"], [mobx.action, 2, "removeGuideLine"], [mobx.action, 2, "updateGuideLine"], [mobx.action, 2, "calculateGuideLineInfo"], [mobx.action, 2, "getAdsorptionPosition"], [mobx.action, 2, "resetAdsorptionLines"]]).e;
158
+ }
159
+ #A = (_initProto(this), _init_enabled(this, true));
160
+ get enabled() {
161
+ return this.#A;
162
+ }
163
+ set enabled(v) {
164
+ this.#A = v;
165
+ }
166
+ #B = (_init_extra_enabled(this), _init_guideLines(this, []));
167
+ get guideLines() {
168
+ return this.#B;
169
+ }
170
+ set guideLines(v) {
171
+ this.#B = v;
172
+ }
173
+ get guideLinesMap() {
174
+ const result = Object.groupBy(this.guideLines, item => item.type);
175
+ return {
176
+ verticalLinesMap: new Map(result.vertical?.map(item => [item.position, item])),
177
+ horizontalLinesMap: new Map(result.horizontal?.map(item => [item.position, item]))
178
+ };
179
+ }
180
+ #C = (_init_extra_guideLines(this), _init_nodeLineMap(this, {
181
+ verticalLinesMap: new Map(),
182
+ horizontalLinesMap: new Map()
183
+ }));
184
+ get nodeLineMap() {
185
+ return this.#C;
186
+ }
187
+ set nodeLineMap(v) {
188
+ this.#C = v;
189
+ }
190
+ #D = (_init_extra_nodeLineMap(this), _init_adsorptionLines(this, {
191
+ verticalLines: new Set(),
192
+ horizontalLines: new Set()
193
+ }));
194
+ get adsorptionLines() {
195
+ return this.#D;
196
+ }
197
+ set adsorptionLines(v) {
198
+ this.#D = v;
199
+ }
200
+ get currentDocument() {
201
+ return this.designer.currentDocument;
202
+ }
203
+ constructor(designer) {
204
+ this.designer = designer;
205
+ this.designer.onEvent(core.DESIGNER_EVENT.VIEWPORT_MOUNT, ({
206
+ viewport
207
+ }) => {
208
+ this.addGuideLine({
209
+ id: 'viewport-vertical-left',
210
+ type: 'vertical',
211
+ position: 0
212
+ });
213
+ this.addGuideLine({
214
+ id: 'viewport-vertical-middle',
215
+ type: 'vertical',
216
+ position: viewport.width / 2
217
+ });
218
+ this.addGuideLine({
219
+ id: 'viewport-vertical-right',
220
+ type: 'vertical',
221
+ position: viewport.width
222
+ });
223
+ this.addGuideLine({
224
+ id: 'viewport-horizontal-top',
225
+ type: 'horizontal',
226
+ position: 0
227
+ });
228
+ this.addGuideLine({
229
+ id: 'viewport-horizontal-middle',
230
+ type: 'horizontal',
231
+ position: viewport.height / 2
232
+ });
233
+ this.addGuideLine({
234
+ id: 'viewport-horizontal-bottom',
235
+ type: 'horizontal',
236
+ position: viewport.height
237
+ });
238
+ });
239
+ }
240
+ addGuideLine(guideLine) {
241
+ this.guideLines.push(guideLine);
242
+ }
243
+ removeGuideLine(id) {
244
+ const index = this.guideLines.findIndex(item => item.id === id);
245
+ if (index !== -1) {
246
+ this.guideLines.splice(index, 1);
247
+ }
248
+ }
249
+ updateGuideLine(id, guideLine) {
250
+ const index = this.guideLines.findIndex(item => item.id === id);
251
+ if (index !== -1) {
252
+ this.guideLines[index] = guideLine;
253
+ }
254
+ }
255
+ calculateGuideLineInfo() {
256
+ if (!this.enabled) return;
257
+ const verticalLinesMap = new Map();
258
+ const horizontalLinesMap = new Map();
259
+ const nodes = [...this.currentDocument.nodesMap.values()];
260
+ const selected = [];
261
+ for (const node of this.designer.selection.getNodes()) {
262
+ if (node.isGroup) {
263
+ selected.push(...node.getAllNodesInGroup().map(node => node.id));
264
+ } else {
265
+ selected.push(node.id);
266
+ }
267
+ }
268
+ nodes.filter(node => !node.hidden).forEach(node => {
269
+ if (selected.includes(node.id) || node.isRoot || node.isGroup) return;
270
+ const nodeRect = node.getDashboardRect();
271
+ const verticalNodeLines = [nodeRect.left, nodeRect.left + nodeRect.width / 2, nodeRect.right];
272
+ const horizontalNodeLines = [nodeRect.top, nodeRect.top + nodeRect.height / 2, nodeRect.bottom];
273
+ for (const line of verticalNodeLines) {
274
+ const position = Math.round(line);
275
+ verticalLinesMap.set(position, {
276
+ type: 'vertical',
277
+ position
278
+ });
279
+ }
280
+ for (const line of horizontalNodeLines) {
281
+ const position = Math.round(line);
282
+ horizontalLinesMap.set(position, {
283
+ type: 'horizontal',
284
+ position
285
+ });
286
+ }
287
+ });
288
+ this.nodeLineMap.verticalLinesMap = verticalLinesMap;
289
+ this.nodeLineMap.horizontalLinesMap = horizontalLinesMap;
290
+ }
291
+ adsorptionSize = (_init_extra_adsorptionLines(this), 10);
292
+ getAdsorptionPosition(rect, adsorption) {
293
+ if (typeof adsorption === 'number') {
294
+ adsorption = [adsorption];
295
+ }
296
+ this.resetAdsorptionLines();
297
+ const adsorptionVerticalLines = [];
298
+ const adsorptionHorizontalLines = [];
299
+ const currentVerticalLine = [rect.left, rect.left + rect.width / 2, rect.right];
300
+ const currentHorizontalLine = [rect.top, rect.top + rect.height / 2, rect.bottom];
301
+ currentVerticalLine.forEach((item, index) => {
302
+ let minDistance = Number.POSITIVE_INFINITY;
303
+ this.nodeLineMap.verticalLinesMap.forEach((_, pos) => {
304
+ const distance = Math.abs(item - pos);
305
+ if (distance !== 0 && distance < this.adsorptionSize && distance < minDistance) {
306
+ minDistance = pos;
307
+ }
308
+ });
309
+ this.guideLinesMap.verticalLinesMap.forEach((_, pos) => {
310
+ const distance = Math.abs(item - pos);
311
+ if (distance !== 0 && distance < this.adsorptionSize && distance < minDistance) {
312
+ minDistance = pos;
313
+ }
314
+ });
315
+ if (minDistance !== Number.POSITIVE_INFINITY && adsorptionVerticalLines.findIndex(item => item.position === minDistance) === -1) {
316
+ adsorptionVerticalLines.push({
317
+ adsorption: index,
318
+ position: minDistance
319
+ });
320
+ }
321
+ });
322
+ currentHorizontalLine.forEach((item, index) => {
323
+ let minDistance = -1;
324
+ this.nodeLineMap.horizontalLinesMap.forEach((_, pos) => {
325
+ if (Math.abs(item - pos) < this.adsorptionSize) {
326
+ minDistance = pos;
327
+ }
328
+ });
329
+ this.guideLinesMap.horizontalLinesMap.forEach((_, pos) => {
330
+ const distance = Math.abs(item - pos);
331
+ if (distance < this.adsorptionSize && distance < minDistance) {
332
+ minDistance = pos;
333
+ }
334
+ });
335
+ if (minDistance !== -1 && adsorptionHorizontalLines.findIndex(item => item.position === minDistance) === -1) {
336
+ adsorptionHorizontalLines.push({
337
+ adsorption: index,
338
+ position: minDistance
339
+ });
340
+ }
341
+ });
342
+ const isAdsorption = adsorptionVerticalLines.length > 0 || adsorptionHorizontalLines.length > 0;
343
+ const adsorb = {
344
+ x: undefined,
345
+ y: undefined
346
+ };
347
+ if (isAdsorption) {
348
+ adsorptionVerticalLines.forEach(item => this.adsorptionLines.verticalLines.add(item.position));
349
+ adsorptionHorizontalLines.forEach(item => this.adsorptionLines.horizontalLines.add(item.position));
350
+ if (adsorptionVerticalLines.length > 0) {
351
+ if (adsorption) {
352
+ adsorb.x = adsorptionVerticalLines.find(item => adsorption.includes(item.adsorption));
353
+ } else {
354
+ const adsorptionPosition = Math.min(...adsorptionVerticalLines.map(item => item.position));
355
+ adsorb.x = adsorptionVerticalLines.find(item => item.position === adsorptionPosition);
356
+ }
357
+ }
358
+ if (adsorptionHorizontalLines.length > 0) {
359
+ if (adsorption) {
360
+ adsorb.y = adsorptionHorizontalLines.find(item => adsorption.includes(item.adsorption));
361
+ } else {
362
+ const adsorptionPosition = Math.min(...adsorptionHorizontalLines.map(item => item.position));
363
+ adsorb.y = adsorptionHorizontalLines.find(item => item.position === adsorptionPosition);
364
+ }
365
+ }
366
+ }
367
+ return {
368
+ isAdsorption,
369
+ adsorb
370
+ };
371
+ }
372
+ resetAdsorptionLines() {
373
+ this.adsorptionLines.verticalLines.clear();
374
+ this.adsorptionLines.horizontalLines.clear();
375
+ }
376
+ }
377
+
378
+ const Group = props => {
379
+ return (
380
+ /*#__PURE__*/
381
+ // TODO: className 需要调整,需要通用的方式
382
+ React.createElement("div", {
383
+ ref: props.ref,
384
+ className: "relative w-full h-full"
385
+ }, props?.children)
386
+ );
387
+ };
388
+
389
+ const configure = {
390
+ props: [
391
+ ],
392
+ component: {},
393
+ supports: {},
394
+ advanced: {
395
+ view: Group
396
+ }
397
+ };
398
+
399
+ const meta = {
400
+ componentName: 'Group',
401
+ title: '分组',
402
+ category: '内置',
403
+ configure
404
+ };
405
+
406
+ const updateNodeRect = (node, offset = {
407
+ x: 0,
408
+ y: 0
409
+ }) => {
410
+ if (node.isGroup) {
411
+ const nodeRect = node.getDashboardRect();
412
+ const delta = {
413
+ x: offset.x - nodeRect.x,
414
+ y: offset.y - nodeRect.y
415
+ };
416
+ for (const childNode of node.getAllNodesInGroup()) {
417
+ const childRect = childNode.getDashboardRect();
418
+ childNode.updateDashboardRect({
419
+ x: childRect.x + delta.x,
420
+ y: childRect.y + delta.y
421
+ });
422
+ }
423
+ } else {
424
+ node.updateDashboardRect({
425
+ x: offset.x,
426
+ y: offset.y
427
+ });
428
+ }
429
+ };
430
+ const updateNodeRectByDOM = (node, offset = {
431
+ x: 0,
432
+ y: 0
433
+ }) => {
434
+ const domNode = node.getDashboardContainer();
435
+ if (!domNode) {
436
+ return;
437
+ }
438
+ domNode.style.left = `${offset.x}px`;
439
+ domNode.style.top = `${offset.y}px`;
440
+ };
441
+
442
+ const defaultGroupSchema = {
443
+ componentName: 'Group',
444
+ title: '分组',
445
+ isGroup: true
446
+ };
447
+ const DashboardPlugin = options => {
448
+ const {
449
+ group = {}
450
+ } = options || {};
451
+ const {
452
+ schema: groupSchema = defaultGroupSchema
453
+ } = group || {};
454
+ return {
455
+ name: 'DashboardPlugin',
456
+ deps: [],
457
+ init(ctx) {
458
+ const {
459
+ designer,
460
+ simulator,
461
+ componentMetaManager,
462
+ hotkey,
463
+ logger
464
+ } = ctx;
465
+ hotkey.bind('ctrl+d', e => {
466
+ e.preventDefault();
467
+ logger.log('ctrl+d');
468
+ });
469
+ designer.onEvent(core.DESIGNER_EVENT.INIT, designer => {
470
+ designer.guideline = new GuideLine(designer);
471
+ });
472
+ componentMetaManager.createComponentMeta(meta);
473
+ simulator.addComponent('Group', Group);
474
+ const startOffsetNodeData = {
475
+ x: 0,
476
+ y: 0
477
+ };
478
+ designer.dragon.onDragstart(e => {
479
+ const {
480
+ dragObject
481
+ } = e;
482
+ if (dragObject && dragObject.type === core.DragObjectType.NodeData) {
483
+ startOffsetNodeData.x = e.globalX - e.target.offsetLeft;
484
+ startOffsetNodeData.y = e.globalY - e.target.offsetTop;
485
+ }
486
+ });
487
+ designer.onEvent(core.DESIGNER_EVENT.INSERT_NODE_BEFORE, e => {
488
+ const {
489
+ event
490
+ } = e;
491
+ const {
492
+ dragObject
493
+ } = event;
494
+ if (dragObject && dragObject.type === core.DragObjectType.NodeData) {
495
+ const nodeData = Array.isArray(dragObject.data) ? dragObject.data : [dragObject.data];
496
+ for (const schema of nodeData) {
497
+ if (!schema) continue;
498
+ if (!schema.$dashboard) {
499
+ schema.$dashboard = {};
500
+ }
501
+ if (!schema.$dashboard.rect) {
502
+ schema.$dashboard.rect = {};
503
+ }
504
+ schema.$dashboard.rect.x = event.canvasX - startOffsetNodeData.x;
505
+ schema.$dashboard.rect.y = event.canvasY - startOffsetNodeData.y;
506
+ }
507
+ }
508
+ });
509
+ let startNodes = {};
510
+ let startOffsetNodes = {};
511
+ let lastOffsetNodes = {};
512
+ designer.dragon.onDragstart(e => {
513
+ const {
514
+ dragObject
515
+ } = e;
516
+ if (dragObject && dragObject.type === core.DragObjectType.Node) {
517
+ designer.guideline.calculateGuideLineInfo();
518
+ for (const node of dragObject.nodes) {
519
+ if (!node) continue;
520
+ const rect = node.getDashboardRect();
521
+ if (rect) {
522
+ startNodes[node.id] = rect;
523
+ startOffsetNodes[node.id] = {
524
+ x: e.canvasX - rect.x,
525
+ y: e.canvasY - rect.y
526
+ };
527
+ }
528
+ }
529
+ const boxRect = calculateDashboardRectBox(dragObject.nodes);
530
+ if (boxRect) {
531
+ startNodes.box = boxRect;
532
+ startOffsetNodes.box = {
533
+ x: e.canvasX - boxRect.x,
534
+ y: e.canvasY - boxRect.y
535
+ };
536
+ }
537
+ }
538
+ });
539
+ designer.dragon.onDrag(e => {
540
+ const {
541
+ dragObject
542
+ } = e;
543
+ if (dragObject && dragObject.type === core.DragObjectType.Node) {
544
+ const {
545
+ x: boxStartX = 0,
546
+ y: boxStartY = 0,
547
+ width = 0,
548
+ height = 0
549
+ } = startNodes.box;
550
+ const {
551
+ x: boxX = 0,
552
+ y: boxY = 0
553
+ } = startOffsetNodes.box;
554
+ const {
555
+ isAdsorption,
556
+ adsorb
557
+ } = designer.guideline.getAdsorptionPosition(new DOMRect(e.canvasX - boxX, e.canvasY - boxY, width, height));
558
+ let adsorbX = undefined;
559
+ let adsorbY = undefined;
560
+ if (isAdsorption) {
561
+ if (adsorb.x) {
562
+ if (adsorb.x.adsorption === 0) {
563
+ adsorbX = adsorb.x.position;
564
+ } else if (adsorb.x.adsorption === 1) {
565
+ adsorbX = adsorb.x.position - width / 2;
566
+ } else if (adsorb.x.adsorption === 2) {
567
+ adsorbX = adsorb.x.position - width;
568
+ }
569
+ }
570
+ if (adsorb.y) {
571
+ if (adsorb.y.adsorption === 0) {
572
+ adsorbY = adsorb.y.position;
573
+ } else if (adsorb.y.adsorption === 1) {
574
+ adsorbY = adsorb.y.position - height / 2;
575
+ } else if (adsorb.y.adsorption === 2) {
576
+ adsorbY = adsorb.y.position - height;
577
+ }
578
+ }
579
+ }
580
+ for (const node of dragObject.nodes) {
581
+ if (!node) continue;
582
+ const {
583
+ x: nodeStartX = 0,
584
+ y: nodeStartY = 0
585
+ } = startNodes[node.id];
586
+ const {
587
+ x,
588
+ y
589
+ } = startOffsetNodes[node.id];
590
+ let offsetX = e.canvasX - x;
591
+ let offsetY = e.canvasY - y;
592
+ if (isAdsorption) {
593
+ offsetX = adsorbX ? adsorbX + nodeStartX - boxStartX : offsetX;
594
+ offsetY = adsorbY ? adsorbY + nodeStartY - boxStartY : offsetY;
595
+ }
596
+ updateNodeRectByDOM(node, {
597
+ x: offsetX,
598
+ y: offsetY
599
+ });
600
+ lastOffsetNodes[node.id] = {
601
+ x: offsetX,
602
+ y: offsetY
603
+ };
604
+ }
605
+ }
606
+ });
607
+ designer.dragon.onDragend(e => {
608
+ const {
609
+ dragObject,
610
+ esc
611
+ } = e;
612
+ if (dragObject && dragObject.type === core.DragObjectType.Node) {
613
+ for (const node of dragObject.nodes) {
614
+ if (!node) continue;
615
+ if (esc) {
616
+ simulator.rerender();
617
+ } else {
618
+ const {
619
+ x: lastX = 0,
620
+ y: lastY = 0
621
+ } = lastOffsetNodes[node.id];
622
+ updateNodeRect(node, {
623
+ x: lastX,
624
+ y: lastY
625
+ });
626
+ }
627
+ }
628
+ designer.guideline.resetAdsorptionLines();
629
+ }
630
+ startNodes = {};
631
+ startOffsetNodes = {};
632
+ lastOffsetNodes = {};
633
+ });
634
+ },
635
+ extend({
636
+ extendClass,
637
+ extend
638
+ }) {
639
+ const {
640
+ Node,
641
+ Designer
642
+ } = extendClass;
643
+ const originalInit = Designer.prototype.init;
644
+ extend('Designer', {
645
+ init: {
646
+ value() {
647
+ originalInit.call(this);
648
+ this.guideline = new GuideLine(this);
649
+ }
650
+ }
651
+ });
652
+ extend('Document', {
653
+ group: {
654
+ value(nodeIdList) {
655
+ if (nodeIdList.length === 0) return;
656
+ let nodeList = [];
657
+ if (typeof nodeIdList[0] === 'string') {
658
+ nodeList = nodeIdList.map(id => this.getNode(id));
659
+ }
660
+ const groupNode = this.createNode(groupSchema);
661
+ let maxZIndex = Number.POSITIVE_INFINITY;
662
+ for (const node of nodeList) {
663
+ if (node.index < maxZIndex) {
664
+ maxZIndex = node.index;
665
+ }
666
+ if (node && !node.isRoot) {
667
+ this.migrateNode(node, groupNode);
668
+ }
669
+ }
670
+ this.rootNode?.insert(groupNode, maxZIndex);
671
+ return groupNode;
672
+ }
673
+ },
674
+ ungroup: {
675
+ value(group) {
676
+ let groupNode;
677
+ if (typeof group === 'string') {
678
+ groupNode = this.getNode(group);
679
+ } else {
680
+ groupNode = group;
681
+ }
682
+ if (!groupNode || !groupNode.isGroup || !groupNode.children) return;
683
+ const nodes = groupNode.childrenNodes;
684
+ while (nodes.length > 0) {
685
+ if (groupNode.parent) {
686
+ this.migrateNode(nodes[0], groupNode.parent);
687
+ }
688
+ }
689
+ this.removeNode(groupNode);
690
+ }
691
+ }
692
+ });
693
+ const originalInitProps = Node.prototype.initBuiltinProps;
694
+ extend('Node', {
695
+ getDashboardContainer: {
696
+ value() {
697
+ return document.getElementById(`${this.id}-mask`);
698
+ }
699
+ },
700
+ getDashboardRect: {
701
+ value() {
702
+ if (!this.isGroup) {
703
+ const rect = this.getExtraPropValue('$dashboard.rect');
704
+ return new DOMRect(rect.x ?? 0, rect.y ?? 0, rect.width ?? 0, rect.height ?? 0);
705
+ }
706
+ let [minX, minY, maxX, maxY] = [Number.POSITIVE_INFINITY, Number.POSITIVE_INFINITY, Number.NEGATIVE_INFINITY, Number.NEGATIVE_INFINITY];
707
+ for (const child of this.childrenNodes) {
708
+ const childRect = child.getDashboardRect();
709
+ minX = Math.min(minX, childRect.x);
710
+ minY = Math.min(minY, childRect.y);
711
+ maxX = Math.max(maxX, childRect.x + childRect.width);
712
+ maxY = Math.max(maxY, childRect.y + childRect.height);
713
+ }
714
+ return new DOMRect(minX, minY, maxX - minX, maxY - minY);
715
+ }
716
+ },
717
+ updateDashboardRect: {
718
+ value(rect) {
719
+ if (this.isGroup) return;
720
+ if (typeof rect.x === 'number') {
721
+ this.setExtraPropValue('$dashboard.rect.x', rect.x);
722
+ }
723
+ if (typeof rect.y === 'number') {
724
+ this.setExtraPropValue('$dashboard.rect.y', rect.y);
725
+ }
726
+ if (typeof rect.width === 'number') {
727
+ this.setExtraPropValue('$dashboard.rect.width', rect.width);
728
+ }
729
+ if (typeof rect.height === 'number') {
730
+ this.setExtraPropValue('$dashboard.rect.height', rect.height);
731
+ }
732
+ }
733
+ },
734
+ isGroup: {
735
+ get() {
736
+ return this.getExtraPropValue('isGroup');
737
+ }
738
+ },
739
+ getCurrentGroup: {
740
+ value() {
741
+ let parent = this.parent;
742
+ while (parent && !parent.isGroup) {
743
+ parent = parent.parent;
744
+ }
745
+ return parent;
746
+ }
747
+ },
748
+ getTopGroup: {
749
+ value() {
750
+ let parent = this.parent;
751
+ let topGroup = null;
752
+ while (parent) {
753
+ if (parent.isGroup) {
754
+ topGroup = parent;
755
+ }
756
+ parent = parent.parent;
757
+ }
758
+ return topGroup;
759
+ }
760
+ },
761
+ getAllGroups: {
762
+ value() {
763
+ const groups = [];
764
+ let parent = this.parent;
765
+ while (parent) {
766
+ if (parent.isGroup) {
767
+ groups.push(parent);
768
+ }
769
+ parent = parent.parent;
770
+ }
771
+ return groups;
772
+ }
773
+ },
774
+ getNodesInGroup: {
775
+ value() {
776
+ if (!this.isGroup) return [];
777
+ const nodes = [];
778
+ for (const node of this.childrenNodes) {
779
+ if (!node.isGroup) {
780
+ nodes.push(node);
781
+ }
782
+ }
783
+ return nodes;
784
+ }
785
+ },
786
+ getAllNodesInGroup: {
787
+ value() {
788
+ if (!this.isGroup) return [];
789
+ const nodes = [];
790
+ for (const node of this.childrenNodes) {
791
+ if (node.isGroup) {
792
+ nodes.push(...node.getAllNodesInGroup());
793
+ } else {
794
+ nodes.push(node);
795
+ }
796
+ }
797
+ return nodes;
798
+ }
799
+ },
800
+ initBuiltinProps: {
801
+ value() {
802
+ originalInitProps.call(this);
803
+ this.props.has(core.getConvertedExtraKey('isGroup')) || this.props.add(core.getConvertedExtraKey('isGroup'), false);
804
+ }
805
+ }
806
+ });
807
+ extend('OffsetObserver', {
808
+ computeRect: {
809
+ value() {
810
+ const {
811
+ node,
812
+ instance
813
+ } = this.nodeInstance;
814
+ const host = node.document?.simulator;
815
+ const rect = host.computeComponentInstanceRect(instance);
816
+ const {
817
+ viewport
818
+ } = node.document.simulator;
819
+ const local = viewport.toLocalPoint({
820
+ clientX: rect.x,
821
+ clientY: rect.y
822
+ });
823
+ return new DOMRect(local.clientX, local.clientY, rect.width / viewport.scale, rect.height / viewport.scale);
824
+ }
825
+ },
826
+ height: {
827
+ get() {
828
+ return this.isRoot ? this.viewport.height : this._height;
829
+ }
830
+ },
831
+ width: {
832
+ get() {
833
+ return this.isRoot ? this.viewport.width : this._width;
834
+ }
835
+ },
836
+ top: {
837
+ get() {
838
+ return this.isRoot ? 0 : this._top;
839
+ }
840
+ },
841
+ left: {
842
+ get() {
843
+ return this.isRoot ? 0 : this._left;
844
+ }
845
+ },
846
+ bottom: {
847
+ get() {
848
+ return this.isRoot ? this.viewport.height : this._bottom;
849
+ }
850
+ },
851
+ right: {
852
+ get() {
853
+ return this.isRoot ? this.viewport.width : this._right;
854
+ }
855
+ }
856
+ });
857
+ }
858
+ };
859
+ };
860
+ const calculateDashboardRectBox = nodes => {
861
+ let [minX, minY, maxX, maxY] = [Number.POSITIVE_INFINITY, Number.POSITIVE_INFINITY, Number.NEGATIVE_INFINITY, Number.NEGATIVE_INFINITY];
862
+ for (const node of nodes) {
863
+ const rect = node.getDashboardRect();
864
+ minX = Math.min(minX, rect.x);
865
+ minY = Math.min(minY, rect.y);
866
+ maxX = Math.max(maxX, rect.x + rect.width);
867
+ maxY = Math.max(maxY, rect.y + rect.height);
868
+ }
869
+ return new DOMRect(minX, minY, maxX - minX, maxY - minY);
870
+ };
871
+
872
+ module.exports = DashboardPlugin;
873
+ //# sourceMappingURL=index.js.map