@meta2d/core 1.0.0 → 1.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,39 +1,39 @@
1
1
  {
2
- "name": "@meta2d/core",
3
- "version": "1.0.0",
4
- "description": "@meta2d/core: Powerful, Beautiful, Simple, Open - Web-Based 2D At Its Best .",
5
- "main": "index.js",
6
- "types": "index.d.ts",
7
- "scripts": {
8
- "copy": "shx cp package.build.json ../../dist/core/package.json",
9
- "build": "tsc && npm run copy"
10
- },
11
- "keywords": [
12
- "meta2d",
13
- "diagram",
14
- "2D",
15
- "canvas"
16
- ],
17
- "author": "alsmile123@qq.com",
18
- "license": "MIT",
19
- "repository": {
20
- "type": "git",
21
- "url": "git+https://github.com/le5le-com/meta2d.js.git"
22
- },
23
- "bugs": {
24
- "url": "https://github.com/le5le-com/meta2d.js/issues"
25
- },
26
- "homepage": "https://github.com/le5le-com/meta2d.js#readme",
27
- "devDependencies": {
28
- "@types/marked": "^4.0.3",
29
- "@types/offscreencanvas": "latest"
30
- },
31
- "dependencies": {
32
- "mitt": "^2.1.0",
33
- "mqtt": "^4.2.6"
34
- },
35
- "publishConfig": {
36
- "access": "public"
37
- },
38
- "gitHead": "78f2a53ca1839c89b56e2e498d17ba4eb987ad14"
39
- }
2
+ "name": "@meta2d/core",
3
+ "version": "1.0.2",
4
+ "description": "@meta2d/core: Powerful, Beautiful, Simple, Open - Web-Based 2D At Its Best .",
5
+ "main": "index.js",
6
+ "types": "index.d.ts",
7
+ "scripts": {
8
+ "copy": "shx cp package.build.json ../../dist/core/package.json",
9
+ "build": "tsc && npm run copy"
10
+ },
11
+ "keywords": [
12
+ "meta2d",
13
+ "diagram",
14
+ "2D",
15
+ "canvas"
16
+ ],
17
+ "author": "alsmile123@qq.com",
18
+ "license": "MIT",
19
+ "repository": {
20
+ "type": "git",
21
+ "url": "git+https://github.com/le5le-com/meta2d.js.git"
22
+ },
23
+ "bugs": {
24
+ "url": "https://github.com/le5le-com/meta2d.js/issues"
25
+ },
26
+ "homepage": "https://github.com/le5le-com/meta2d.js#readme",
27
+ "devDependencies": {
28
+ "@types/marked": "^4.0.3",
29
+ "@types/offscreencanvas": "latest"
30
+ },
31
+ "dependencies": {
32
+ "mitt": "^2.1.0",
33
+ "mqtt": "^4.2.6"
34
+ },
35
+ "publishConfig": {
36
+ "access": "public"
37
+ },
38
+ "gitHead": "78f2a53ca1839c89b56e2e498d17ba4eb987ad14"
39
+ }
@@ -253,9 +253,19 @@ var Canvas = /** @class */ (function () {
253
253
  // 触摸板平移
254
254
  var isTouchPad = !(!e.deltaX && e.deltaY);
255
255
  var now = performance.now();
256
+ var _scale = 0.1;
256
257
  if (now - _this.touchStart < 50) {
257
258
  return;
258
259
  }
260
+ if (now - _this.touchStart < 100) {
261
+ _scale = 0.5;
262
+ }
263
+ else if (now - _this.touchStart < 200) {
264
+ _scale = 0.3;
265
+ }
266
+ else {
267
+ _scale = 0.1;
268
+ }
259
269
  _this.touchStart = now;
260
270
  var x = e.offsetX, y = e.offsetY;
261
271
  if (isTouchPad) {
@@ -263,10 +273,10 @@ var Canvas = /** @class */ (function () {
263
273
  }
264
274
  else {
265
275
  if (e.deltaY < 0) {
266
- _this.scale(_this.store.data.scale + 0.1, { x: x, y: y });
276
+ _this.scale(_this.store.data.scale + _scale, { x: x, y: y });
267
277
  }
268
278
  else {
269
- _this.scale(_this.store.data.scale - 0.1, { x: x, y: y });
279
+ _this.scale(_this.store.data.scale - _scale, { x: x, y: y });
270
280
  }
271
281
  }
272
282
  _this.externalElements.focus(); // 聚焦
@@ -430,6 +440,9 @@ var Canvas = /** @class */ (function () {
430
440
  _this.store.hover === _this.store.active[0]) {
431
441
  _this.splitLine(_this.store.active[0], _this.store.hoverAnchor);
432
442
  }
443
+ // 保存
444
+ (e.ctrlKey || e.metaKey) &&
445
+ _this.store.emitter.emit('save', { event: e });
433
446
  break;
434
447
  case 'c':
435
448
  case 'C':
@@ -524,6 +537,11 @@ var Canvas = /** @class */ (function () {
524
537
  _this.patchFlags = true;
525
538
  }
526
539
  break;
540
+ case 'E':
541
+ case 'e':
542
+ _this.store.options.disableAnchor = !_this.store.options.disableAnchor;
543
+ _this.store.emitter.emit('disableAnchor', _this.store.options.disableAnchor);
544
+ break;
527
545
  }
528
546
  _this.render(false);
529
547
  };
@@ -736,7 +754,7 @@ var Canvas = /** @class */ (function () {
736
754
  e.preventDefault();
737
755
  };
738
756
  this.onMouseDown = function (e) {
739
- var _a, _b;
757
+ var _a, _b, _c;
740
758
  if (e.buttons === 2 && !_this.drawingLine) {
741
759
  _this.mouseRight = MouseRight.Down;
742
760
  }
@@ -759,6 +777,25 @@ var Canvas = /** @class */ (function () {
759
777
  _this.setAnchor(_this.store.pointAt);
760
778
  return;
761
779
  }
780
+ //shift 快捷添加锚点并连线
781
+ if (!_this.store.options.autoAnchor && !_this.drawingLine) {
782
+ if (e.shiftKey) {
783
+ _this.setAnchor(_this.store.pointAt);
784
+ _this.drawingLineName = _this.store.options.drawingLineName;
785
+ var anchor = _this.store.activeAnchor;
786
+ var pt = {
787
+ id: s8(),
788
+ x: anchor.x,
789
+ y: anchor.y,
790
+ };
791
+ _this.drawingLine = _this.createDrawingLine(pt);
792
+ var _pt = getFromAnchor(_this.drawingLine);
793
+ _this.drawingLine.calculative.activeAnchor = _pt;
794
+ connectLine(_this.store.hover, anchor, _this.drawingLine, pt);
795
+ _this.drawline();
796
+ return;
797
+ }
798
+ }
762
799
  // Translate
763
800
  if (_this.hotkeyType === HotkeyType.Translate ||
764
801
  (_this.mouseRight === MouseRight.Down &&
@@ -777,12 +814,29 @@ var Canvas = /** @class */ (function () {
777
814
  _this.finishDrawline(true);
778
815
  return;
779
816
  }
817
+ //shift快捷添加锚点并完成连线
818
+ if (!_this.store.options.autoAnchor) {
819
+ if (e.shiftKey) {
820
+ _this.setAnchor(_this.store.pointAt);
821
+ var to_2 = getToAnchor(_this.drawingLine);
822
+ var anchor = _this.store.activeAnchor;
823
+ to_2.x = anchor.x;
824
+ to_2.y = anchor.y;
825
+ connectLine(_this.store.hover, anchor, _this.drawingLine, to_2);
826
+ _this.drawline();
827
+ _this.finishDrawline(true);
828
+ return;
829
+ }
830
+ }
780
831
  // 右键,完成绘画
781
832
  if (e.buttons === 2 ||
782
833
  (_this.drawingLineName === 'mind' &&
783
- ((_a = _this.drawingLine) === null || _a === void 0 ? void 0 : _a.calculative.worldAnchors.length) > 1)) {
834
+ ((_a = _this.drawingLine) === null || _a === void 0 ? void 0 : _a.calculative.worldAnchors.length) > 1) ||
835
+ (_this.store.options.drawingLineLength &&
836
+ ((_b = _this.drawingLine) === null || _b === void 0 ? void 0 : _b.calculative.worldAnchors.length) >
837
+ _this.store.options.drawingLineLength)) {
784
838
  _this.finishDrawline(true);
785
- if ((_b = _this.store.active[0]) === null || _b === void 0 ? void 0 : _b.anchors[0].connectTo) {
839
+ if ((_c = _this.store.active[0]) === null || _c === void 0 ? void 0 : _c.anchors[0].connectTo) {
786
840
  _this.drawingLineName = '';
787
841
  }
788
842
  else {
@@ -792,12 +846,12 @@ var Canvas = /** @class */ (function () {
792
846
  }
793
847
  // 自动锚点(单击节点),完成绘画
794
848
  if (_this.store.options.autoAnchor && _this.hoverType === HoverType.Node) {
795
- var to_2 = getToAnchor(_this.drawingLine);
849
+ var to_3 = getToAnchor(_this.drawingLine);
796
850
  var anchor = nearestAnchor(_this.store.hover, e);
797
- to_2.x = anchor.x;
798
- to_2.y = anchor.y;
851
+ to_3.x = anchor.x;
852
+ to_3.y = anchor.y;
799
853
  _this.drawingLine.autoTo = true;
800
- connectLine(_this.store.hover, anchor, _this.drawingLine, to_2);
854
+ connectLine(_this.store.hover, anchor, _this.drawingLine, to_3);
801
855
  _this.drawline();
802
856
  _this.finishDrawline(true);
803
857
  return;
@@ -1130,14 +1184,68 @@ var Canvas = /** @class */ (function () {
1130
1184
  }
1131
1185
  }
1132
1186
  if (_this.drawingLineName === 'line') {
1133
- if (e.ctrlKey) {
1187
+ if (e.ctrlKey && !e.shiftKey) {
1134
1188
  to.x =
1135
1189
  _this.drawingLine.calculative.worldAnchors[_this.drawingLine.calculative.worldAnchors.length - 2].x;
1136
1190
  }
1137
- else if (e.shiftKey) {
1191
+ else if (e.shiftKey && !e.ctrlKey) {
1138
1192
  to.y =
1139
1193
  _this.drawingLine.calculative.worldAnchors[_this.drawingLine.calculative.worldAnchors.length - 2].y;
1140
1194
  }
1195
+ else if (e.shiftKey && e.ctrlKey) {
1196
+ //快捷定位到特殊角度
1197
+ var last = _this.drawingLine.calculative.worldAnchors[_this.drawingLine.calculative.worldAnchors.length - 2];
1198
+ var angle = 0;
1199
+ var angleArr = [0, 30, 45, 60, 90, 120, 150, 135, 180];
1200
+ //获取实际角度
1201
+ if (to.x - last.x !== 0) {
1202
+ angle =
1203
+ (Math.atan((last.y - to.y) / (to.x - last.x)) * 180) / Math.PI;
1204
+ if (to.x < last.x) {
1205
+ if (angle > 0) {
1206
+ angle -= 180;
1207
+ }
1208
+ else {
1209
+ angle += 180;
1210
+ }
1211
+ }
1212
+ }
1213
+ else {
1214
+ if (last.y > to.y) {
1215
+ angle = 90;
1216
+ }
1217
+ else if (last.y < to.y) {
1218
+ angle = -90;
1219
+ }
1220
+ }
1221
+ //取最近角度
1222
+ var _min = 999;
1223
+ var index = -1;
1224
+ for (var i = 0; i < angleArr.length; i++) {
1225
+ if (angle < 0) {
1226
+ if (Math.abs(angle + angleArr[i]) < _min) {
1227
+ index = i;
1228
+ _min = Math.abs(angle + angleArr[i]);
1229
+ }
1230
+ }
1231
+ else {
1232
+ if (Math.abs(angle - angleArr[i]) < _min) {
1233
+ index = i;
1234
+ _min = Math.abs(angle - angleArr[i]);
1235
+ }
1236
+ }
1237
+ }
1238
+ if (angle < 0) {
1239
+ angle = -angleArr[index];
1240
+ }
1241
+ else {
1242
+ angle = angleArr[index];
1243
+ }
1244
+ var length_1 = Math.sqrt((last.x - to.x) * (last.x - to.x) +
1245
+ (last.y - to.y) * (last.y - to.y));
1246
+ to.x = last.x + Math.cos((angle / 180) * Math.PI) * length_1;
1247
+ to.y = last.y - Math.sin((angle / 180) * Math.PI) * length_1;
1248
+ }
1141
1249
  }
1142
1250
  _this.drawline();
1143
1251
  }
@@ -1330,7 +1438,7 @@ var Canvas = /** @class */ (function () {
1330
1438
  _this.render();
1331
1439
  }
1332
1440
  if (_this.movingPens) {
1333
- if (e.ctrlKey && !e.shiftKey) {
1441
+ if (e.altKey && !e.shiftKey) {
1334
1442
  _this.copyMovedPens();
1335
1443
  }
1336
1444
  else {
@@ -1889,6 +1997,24 @@ var Canvas = /** @class */ (function () {
1889
1997
  else if (anchor.color || anchor.background) {
1890
1998
  ctx.restore();
1891
1999
  }
2000
+ //根父节点
2001
+ if (!_this.store.hover.parentId &&
2002
+ _this.store.hover.children &&
2003
+ _this.store.hover.children.length > 0) {
2004
+ if (anchor === _this.store.hoverAnchor) {
2005
+ ctx.save();
2006
+ ctx.beginPath();
2007
+ ctx.lineWidth = 3;
2008
+ var hoverAnchorColor = _this.store.hover.hoverAnchorColor ||
2009
+ _this.store.options.hoverAnchorColor;
2010
+ if (globalThis.pSBC) {
2011
+ ctx.strokeStyle = globalThis.pSBC(0.5, hoverAnchorColor);
2012
+ }
2013
+ ctx.arc(anchor.x, anchor.y, size + 1.5, 0, Math.PI * 2);
2014
+ ctx.stroke();
2015
+ ctx.restore();
2016
+ }
2017
+ }
1892
2018
  });
1893
2019
  }
1894
2020
  }
@@ -2252,7 +2378,8 @@ var Canvas = /** @class */ (function () {
2252
2378
  .replace(/\<div\>/g, '\n')
2253
2379
  .replace(/\<\/div\>/g, '')
2254
2380
  .replace(/\<br\>/g, '')
2255
- .replace(/&nbsp;/g, ' ');
2381
+ .replace(/&nbsp;/g, ' ')
2382
+ .replace(/(<([^>]+)>)/gi, '');
2256
2383
  if (pen.onInput) {
2257
2384
  pen.onInput(pen, _this.inputDiv.dataset.value);
2258
2385
  }
@@ -2963,6 +3090,7 @@ var Canvas = /** @class */ (function () {
2963
3090
  * 所以 patchFlagsLines 需要放到 copyMovedPens 前
2964
3091
  * */
2965
3092
  var _a = _this.movingPens[i], x = _a.x, y = _a.y;
3093
+ _this.updateLines(pen); //复制前更新被复制节点的连接关系
2966
3094
  return __assign(__assign({}, pen), { x: x, y: y });
2967
3095
  }));
2968
3096
  // 偏移量 0
@@ -3326,7 +3454,7 @@ var Canvas = /** @class */ (function () {
3326
3454
  this.doEditAction(action_1, true);
3327
3455
  step--;
3328
3456
  }
3329
- if (action.type == EditType.Add || EditType.Delete) {
3457
+ if (action.type == EditType.Add || action.type == EditType.Delete) {
3330
3458
  this.activeHistory();
3331
3459
  }
3332
3460
  };
@@ -3344,7 +3472,7 @@ var Canvas = /** @class */ (function () {
3344
3472
  this.doEditAction(action_2, false);
3345
3473
  step--;
3346
3474
  }
3347
- if (action.type == EditType.Add || EditType.Delete) {
3475
+ if (action.type == EditType.Add || action.type == EditType.Delete) {
3348
3476
  this.activeHistory();
3349
3477
  }
3350
3478
  };
@@ -4050,12 +4178,8 @@ var Canvas = /** @class */ (function () {
4050
4178
  var x = p2.x - p1.x;
4051
4179
  var y = p2.y - p1.y;
4052
4180
  var rect = deepClone(this.initActiveRect);
4053
- if (e.shiftKey) {
4054
- // if (x / y > rect.width / rect.height) {
4055
- // y = (rect.height / rect.width) * x;
4056
- // } else {
4057
- // x = (rect.width / rect.height) * y;
4058
- // }
4181
+ if (e.shiftKey ||
4182
+ (this.initPens.length == 1 && this.initPens[0].ratio)) {
4059
4183
  x = (rect.width / rect.height) * y;
4060
4184
  }
4061
4185
  // 得到最准确的 rect 即 resize 后的
@@ -4317,7 +4441,8 @@ var Canvas = /** @class */ (function () {
4317
4441
  this.store.activeAnchor.next.y = e.y;
4318
4442
  rotatePoint(this.store.activeAnchor.next, 180, this.store.activeAnchor);
4319
4443
  }
4320
- else if (this.store.activeAnchor.prevNextType === PrevNextType.Bilateral) {
4444
+ else if (this.store.activeAnchor.prevNextType === PrevNextType.Bilateral &&
4445
+ this.prevAnchor) {
4321
4446
  var rotate = calcRotate(e, this.store.activeAnchor);
4322
4447
  var prevRotate = calcRotate(this.prevAnchor, this.store.activeAnchor);
4323
4448
  this.store.activeAnchor.next.x = this.nextAnchor.x;
@@ -4358,7 +4483,8 @@ var Canvas = /** @class */ (function () {
4358
4483
  this.store.activeAnchor.prev.y = e.y;
4359
4484
  rotatePoint(this.store.activeAnchor.prev, 180, this.store.activeAnchor);
4360
4485
  }
4361
- else if (this.store.activeAnchor.prevNextType === PrevNextType.Bilateral) {
4486
+ else if (this.store.activeAnchor.prevNextType === PrevNextType.Bilateral &&
4487
+ this.nextAnchor) {
4362
4488
  var rotate = calcRotate(e, this.store.activeAnchor);
4363
4489
  var nextRotate = calcRotate(this.nextAnchor, this.store.activeAnchor);
4364
4490
  this.store.activeAnchor.prev.x = this.prevAnchor.x;
@@ -5202,7 +5328,7 @@ var Canvas = /** @class */ (function () {
5202
5328
  }
5203
5329
  deletePens = [];
5204
5330
  this._del(pens, deletePens);
5205
- this.initImageCanvas(pens);
5331
+ this.initImageCanvas(deletePens);
5206
5332
  this.inactive();
5207
5333
  this.clearHover();
5208
5334
  this.render();