@meta2d/core 1.0.17 → 1.0.19

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,6 +1,6 @@
1
1
  {
2
2
  "name": "@meta2d/core",
3
- "version": "1.0.17",
3
+ "version": "1.0.19",
4
4
  "description": "@meta2d/core: Powerful, Beautiful, Simple, Open - Web-Based 2D At Its Best .",
5
5
  "main": "index.js",
6
6
  "types": "index.d.ts",
@@ -12,6 +12,7 @@ import { CanvasImage } from './canvasImage';
12
12
  import { MagnifierCanvas } from './magnifierCanvas';
13
13
  import { Meta2d } from '../core';
14
14
  import { Dialog } from '../dialog';
15
+ import { Title } from '../title';
15
16
  export declare const movingSuffix: "-moving";
16
17
  export declare class Canvas {
17
18
  parent: Meta2d;
@@ -76,6 +77,8 @@ export declare class Canvas {
76
77
  pointSize: 8;
77
78
  pasteOffset: number;
78
79
  opening: boolean;
80
+ maxZindex: number;
81
+ canMoveLine: boolean;
79
82
  /**
80
83
  * @deprecated 改用 beforeAddPens
81
84
  */
@@ -97,6 +100,7 @@ export declare class Canvas {
97
100
  inputRight: HTMLDivElement;
98
101
  dropdown: HTMLUListElement;
99
102
  tooltip: Tooltip;
103
+ title: Title;
100
104
  mousePos: Point;
101
105
  scroll: Scroll;
102
106
  movingAnchor: Point;
@@ -324,7 +328,7 @@ export declare class Canvas {
324
328
  getFrameProps(pen: any): {};
325
329
  animate(): void;
326
330
  get clipboardName(): string;
327
- copy(pens?: Pen[]): Promise<void>;
331
+ copy(pens?: Pen[], emit?: boolean): Promise<void>;
328
332
  cut(pens?: Pen[]): void;
329
333
  paste(): Promise<void>;
330
334
  /**
@@ -98,6 +98,7 @@ import { MagnifierCanvas } from './magnifierCanvas';
98
98
  import { lockedError } from '../utils/error';
99
99
  import { Dialog } from '../dialog';
100
100
  import { setter } from '../utils/object';
101
+ import { Title } from '../title';
101
102
  export var movingSuffix = '-moving';
102
103
  var Canvas = /** @class */ (function () {
103
104
  function Canvas(parent, parentElement, store) {
@@ -125,6 +126,8 @@ var Canvas = /** @class */ (function () {
125
126
  this.pointSize = 8;
126
127
  this.pasteOffset = 10;
127
128
  this.opening = false;
129
+ this.maxZindex = 4;
130
+ this.canMoveLine = false; //moveConnectedLine=false
128
131
  this.inputParent = document.createElement('div');
129
132
  // input = document.createElement('textarea');
130
133
  this.inputDiv = document.createElement('div');
@@ -238,11 +241,11 @@ var Canvas = /** @class */ (function () {
238
241
  if (_this.pencil) {
239
242
  return;
240
243
  }
241
- e.preventDefault();
242
- e.stopPropagation();
243
244
  if (_this.store.options.disableScale) {
244
245
  return;
245
246
  }
247
+ e.preventDefault();
248
+ e.stopPropagation();
246
249
  if (_this.store.data.locked === LockState.Disable)
247
250
  return;
248
251
  if (_this.store.data.locked === LockState.DisableScale)
@@ -250,7 +253,7 @@ var Canvas = /** @class */ (function () {
250
253
  if (_this.store.data.locked === LockState.DisableMoveScale)
251
254
  return;
252
255
  // e.ctrlKey: false - 平移; true - 缩放。老windows触摸板不支持
253
- if (!e.ctrlKey) {
256
+ if (!e.ctrlKey && Math.abs(e.wheelDelta) < 100) {
254
257
  if (_this.store.options.scroll && !e.metaKey && _this.scroll) {
255
258
  _this.scroll.wheel(e.deltaY < 0);
256
259
  return;
@@ -262,17 +265,38 @@ var Canvas = /** @class */ (function () {
262
265
  if (_this.store.options.disableTouchPadScale) {
263
266
  return;
264
267
  }
265
- var x = e.offsetX, y = e.offsetY;
266
- if (e.deltaY < 0) {
267
- _this.scale(_this.store.data.scale + 0.015, { x: x, y: y });
268
+ var scaleOff = 0.015;
269
+ var isMac = /mac os /i.test(navigator.userAgent);
270
+ if (isMac) {
271
+ if (!e.ctrlKey) {
272
+ scaleOff *= e.wheelDeltaY / 240;
273
+ }
274
+ else if (e.deltaY > 0) {
275
+ scaleOff *= -1;
276
+ }
268
277
  }
269
278
  else {
270
- _this.scale(_this.store.data.scale - 0.015, { x: x, y: y });
279
+ if (e.deltaY > 0) {
280
+ scaleOff = -0.2;
281
+ }
282
+ else {
283
+ scaleOff = 0.2;
284
+ }
271
285
  }
286
+ var x = e.offsetX, y = e.offsetY;
287
+ _this.scale(_this.store.data.scale + scaleOff, { x: x, y: y });
272
288
  _this.externalElements.focus(); // 聚焦
273
289
  };
274
290
  this.onkeydown = function (e) {
275
291
  var _a, _b, _c;
292
+ if (_this.store.data.locked >= LockState.DisableEdit &&
293
+ e.target.tagName !== 'INPUT' &&
294
+ e.target.tagName !== 'TEXTAREA') {
295
+ _this.store.active.forEach(function (pen) {
296
+ var _a;
297
+ (_a = pen.onKeyDown) === null || _a === void 0 ? void 0 : _a.call(pen, pen, e.key);
298
+ });
299
+ }
276
300
  if (_this.store.data.locked >= LockState.DisableEdit ||
277
301
  e.target.tagName === 'INPUT' ||
278
302
  e.target.tagName === 'TEXTAREA') {
@@ -546,17 +570,25 @@ var Canvas = /** @class */ (function () {
546
570
  e.stopPropagation();
547
571
  }
548
572
  break;
573
+ case 'l':
574
+ case 'L':
575
+ _this.canMoveLine = true;
576
+ break;
549
577
  }
550
578
  _this.render(false);
551
579
  };
552
580
  this.onkeyup = function (e) {
553
- // switch (e.key) {
554
- // case 'Alt':
555
- // if (this.drawingLine) {
556
- // this.store.options.autoAnchor = !this.store.options.autoAnchor;
557
- // }
558
- // break;
559
- // }
581
+ switch (e.key) {
582
+ case 'l':
583
+ case 'L':
584
+ _this.canMoveLine = false;
585
+ break;
586
+ // case 'Alt':
587
+ // if (this.drawingLine) {
588
+ // this.store.options.autoAnchor = !this.store.options.autoAnchor;
589
+ // }
590
+ // break;
591
+ }
560
592
  if (_this.hotkeyType) {
561
593
  _this.render();
562
594
  }
@@ -565,7 +597,7 @@ var Canvas = /** @class */ (function () {
565
597
  }
566
598
  };
567
599
  this.ondrop = function (event) { return __awaiter(_this, void 0, void 0, function () {
568
- var json, obj, files, isGif, pt, e_1;
600
+ var json, obj, files, isGif, pt;
569
601
  return __generator(this, function (_a) {
570
602
  switch (_a.label) {
571
603
  case 0:
@@ -573,34 +605,46 @@ var Canvas = /** @class */ (function () {
573
605
  console.warn('canvas is locked, can not drop');
574
606
  return [2 /*return*/];
575
607
  }
576
- _a.label = 1;
577
- case 1:
578
- _a.trys.push([1, 4, , 5]);
579
- // TODO: 若画布锁定,阻止默认行为不再执行。在 firefox 上拖拽图片会打开新页
608
+ // Fix bug: 在 firefox 上拖拽图片会打开新页
580
609
  event.preventDefault();
581
610
  event.stopPropagation();
582
611
  json = event.dataTransfer.getData('Meta2d') ||
583
612
  event.dataTransfer.getData('Text');
584
613
  obj = null;
585
- if (!!json) return [3 /*break*/, 3];
614
+ try {
615
+ if (json) {
616
+ obj = JSON.parse(json);
617
+ }
618
+ }
619
+ catch (e) { }
620
+ if (!!obj) return [3 /*break*/, 3];
586
621
  files = event.dataTransfer.files;
587
- if (!(files.length && files[0].type.match('image.*'))) return [3 /*break*/, 3];
622
+ if (!(files.length && files[0].type.match('image.*'))) return [3 /*break*/, 2];
588
623
  isGif = files[0].type === 'image/gif';
589
624
  return [4 /*yield*/, this.fileToPen(files[0], isGif)];
590
- case 2:
625
+ case 1:
591
626
  obj = _a.sent();
627
+ return [3 /*break*/, 3];
628
+ case 2:
629
+ if (this.addCaches.length) {
630
+ obj = this.addCaches;
631
+ this.addCaches = [];
632
+ }
633
+ else {
634
+ this.store.emitter.emit('drop', undefined);
635
+ return [2 /*return*/];
636
+ }
592
637
  _a.label = 3;
593
638
  case 3:
594
- !obj && (obj = JSON.parse(json));
595
- obj = Array.isArray(obj) ? obj : [obj];
596
- pt = { x: event.offsetX, y: event.offsetY };
597
- this.calibrateMouse(pt);
598
- this.dropPens(obj, pt);
599
- return [3 /*break*/, 5];
600
- case 4:
601
- e_1 = _a.sent();
602
- return [3 /*break*/, 5];
603
- case 5: return [2 /*return*/];
639
+ if (obj && obj.draggable !== false) {
640
+ obj = Array.isArray(obj) ? obj : [obj];
641
+ pt = { x: event.offsetX, y: event.offsetY };
642
+ this.calibrateMouse(pt);
643
+ this.dropPens(obj, pt);
644
+ this.addCaches = [];
645
+ }
646
+ this.store.emitter.emit('drop', obj || json);
647
+ return [2 /*return*/];
604
648
  }
605
649
  });
606
650
  }); };
@@ -1571,7 +1615,7 @@ var Canvas = /** @class */ (function () {
1571
1615
  this.inPens = function (pt, pens) {
1572
1616
  var hoverType = HoverType.None;
1573
1617
  var _loop_2 = function (i) {
1574
- var e_2, _a, e_3, _b;
1618
+ var e_1, _a, e_2, _b, e_3, _c;
1575
1619
  var pen = pens[i];
1576
1620
  if (pen.visible == false ||
1577
1621
  pen.calculative.inView == false ||
@@ -1584,12 +1628,36 @@ var Canvas = /** @class */ (function () {
1584
1628
  !pointInRect(pt, pen.calculative.worldRect)) {
1585
1629
  return "continue";
1586
1630
  }
1631
+ //anchor title
1632
+ if (_this.store.data.locked) {
1633
+ // locked>0
1634
+ if (pen.calculative.worldAnchors) {
1635
+ try {
1636
+ for (var _d = (e_1 = void 0, __values(pen.calculative.worldAnchors)), _e = _d.next(); !_e.done; _e = _d.next()) {
1637
+ var anchor = _e.value;
1638
+ if (hitPoint(pt, anchor, _this.pointSize)) {
1639
+ _this.title.show(anchor);
1640
+ if (anchor.title) {
1641
+ return "break-outer";
1642
+ }
1643
+ }
1644
+ }
1645
+ }
1646
+ catch (e_1_1) { e_1 = { error: e_1_1 }; }
1647
+ finally {
1648
+ try {
1649
+ if (_e && !_e.done && (_a = _d.return)) _a.call(_d);
1650
+ }
1651
+ finally { if (e_1) throw e_1.error; }
1652
+ }
1653
+ }
1654
+ }
1587
1655
  // 锚点
1588
1656
  if (!_this.store.data.locked && _this.hotkeyType !== HotkeyType.Resize) {
1589
1657
  if (pen.calculative.worldAnchors) {
1590
1658
  try {
1591
- for (var _c = (e_2 = void 0, __values(pen.calculative.worldAnchors)), _d = _c.next(); !_d.done; _d = _c.next()) {
1592
- var anchor = _d.value;
1659
+ for (var _f = (e_2 = void 0, __values(pen.calculative.worldAnchors)), _g = _f.next(); !_g.done; _g = _f.next()) {
1660
+ var anchor = _g.value;
1593
1661
  hoverType = _this.inAnchor(pt, pen, anchor);
1594
1662
  if (hoverType) {
1595
1663
  return "break-outer";
@@ -1599,7 +1667,7 @@ var Canvas = /** @class */ (function () {
1599
1667
  catch (e_2_1) { e_2 = { error: e_2_1 }; }
1600
1668
  finally {
1601
1669
  try {
1602
- if (_d && !_d.done && (_a = _c.return)) _a.call(_c);
1670
+ if (_g && !_g.done && (_b = _f.return)) _b.call(_f);
1603
1671
  }
1604
1672
  finally { if (e_2) throw e_2.error; }
1605
1673
  }
@@ -1662,7 +1730,7 @@ var Canvas = /** @class */ (function () {
1662
1730
  _this.store.pointAt = pt;
1663
1731
  // 锚点贴边吸附
1664
1732
  if (!pt.ctrlKey) {
1665
- var _e = _this.store.hover.calculative.worldRect, x = _e.x, y = _e.y, ex = _e.ex, ey = _e.ey, rotate_1 = _e.rotate, center_1 = _e.center;
1733
+ var _h = _this.store.hover.calculative.worldRect, x = _h.x, y = _h.y, ex = _h.ex, ey = _h.ey, rotate_1 = _h.rotate, center_1 = _h.center;
1666
1734
  if (rotate_1) {
1667
1735
  var pts = [
1668
1736
  { x: x, y: y },
@@ -1690,7 +1758,7 @@ var Canvas = /** @class */ (function () {
1690
1758
  catch (e_3_1) { e_3 = { error: e_3_1 }; }
1691
1759
  finally {
1692
1760
  try {
1693
- if (pts_1_1 && !pts_1_1.done && (_b = pts_1.return)) _b.call(pts_1);
1761
+ if (pts_1_1 && !pts_1_1.done && (_c = pts_1.return)) _c.call(pts_1);
1694
1762
  }
1695
1763
  finally { if (e_3) throw e_3.error; }
1696
1764
  }
@@ -2520,6 +2588,7 @@ var Canvas = /** @class */ (function () {
2520
2588
  setHover(hover, false);
2521
2589
  };
2522
2590
  this.dialog = new Dialog(parentElement);
2591
+ this.title = new Title(parentElement);
2523
2592
  if (this.store.options.scroll) {
2524
2593
  this.scroll = new Scroll(this);
2525
2594
  }
@@ -3162,6 +3231,7 @@ var Canvas = /** @class */ (function () {
3162
3231
  this.store.active.forEach(function (pen) {
3163
3232
  pen.calculative.active = undefined;
3164
3233
  pen.calculative.activeAnchor = undefined;
3234
+ pen.calculative.hover = false;
3165
3235
  setChildrenActive(pen, false);
3166
3236
  });
3167
3237
  !drawing && this.store.emitter.emit('inactive', this.store.active);
@@ -3175,10 +3245,12 @@ var Canvas = /** @class */ (function () {
3175
3245
  var e_14, _a, _b;
3176
3246
  if (emit === void 0) { emit = true; }
3177
3247
  if (this.store.active) {
3248
+ emit && this.store.emitter.emit('inactive', this.store.active);
3178
3249
  try {
3179
3250
  for (var _c = __values(this.store.active), _d = _c.next(); !_d.done; _d = _c.next()) {
3180
3251
  var pen = _d.value;
3181
3252
  pen.calculative.active = undefined;
3253
+ pen.calculative.hover = false;
3182
3254
  setChildrenActive(pen, false);
3183
3255
  }
3184
3256
  }
@@ -3651,6 +3723,7 @@ var Canvas = /** @class */ (function () {
3651
3723
  else {
3652
3724
  this.initImageCanvas(action.pens);
3653
3725
  }
3726
+ this.parent.onSizeUpdate();
3654
3727
  this.render();
3655
3728
  this.store.emitter.emit(undo ? 'undo' : 'redo', action);
3656
3729
  };
@@ -4383,6 +4456,7 @@ var Canvas = /** @class */ (function () {
4383
4456
  return;
4384
4457
  }
4385
4458
  if (!this.store.options.moveConnectedLine &&
4459
+ !this.canMoveLine &&
4386
4460
  this.store.active.length === 1 &&
4387
4461
  (((_a = this.store.active[0].anchors[0]) === null || _a === void 0 ? void 0 : _a.connectTo) ||
4388
4462
  ((_b = this.store.active[0].anchors[this.store.active[0].anchors.length - 1]) === null || _b === void 0 ? void 0 : _b.connectTo))) {
@@ -4466,7 +4540,7 @@ var Canvas = /** @class */ (function () {
4466
4540
  Canvas.prototype.initMovingPens = function () {
4467
4541
  var _this = this;
4468
4542
  var _a, _b;
4469
- if (!this.store.options.moveConnectedLine) {
4543
+ if (!this.store.options.moveConnectedLine && !this.canMoveLine) {
4470
4544
  for (var i = 0; i < this.store.active.length; i++) {
4471
4545
  var pen = this.store.active[i];
4472
4546
  if (((_a = pen.anchors[0]) === null || _a === void 0 ? void 0 : _a.connectTo) ||
@@ -4810,7 +4884,7 @@ var Canvas = /** @class */ (function () {
4810
4884
  return;
4811
4885
  }
4812
4886
  if (pen.type === PenType.Line) {
4813
- if (!_this.store.options.moveConnectedLine) {
4887
+ if (!_this.store.options.moveConnectedLine && !_this.canMoveLine) {
4814
4888
  return;
4815
4889
  }
4816
4890
  translateLine(pen, x, y);
@@ -5244,7 +5318,8 @@ var Canvas = /** @class */ (function () {
5244
5318
  enumerable: false,
5245
5319
  configurable: true
5246
5320
  });
5247
- Canvas.prototype.copy = function (pens) {
5321
+ Canvas.prototype.copy = function (pens, emit) {
5322
+ if (emit === void 0) { emit = true; }
5248
5323
  return __awaiter(this, void 0, void 0, function () {
5249
5324
  var page, _a, origin, scale, copyPens, clipboard, _b;
5250
5325
  var _this = this;
@@ -5294,14 +5369,17 @@ var Canvas = /** @class */ (function () {
5294
5369
  case 5:
5295
5370
  localStorage.setItem(this.clipboardName, JSON.stringify(clipboard));
5296
5371
  _c.label = 6;
5297
- case 6: return [2 /*return*/];
5372
+ case 6:
5373
+ emit && this.store.emitter.emit('copy', clipboard.pens);
5374
+ return [2 /*return*/];
5298
5375
  }
5299
5376
  });
5300
5377
  });
5301
5378
  };
5302
5379
  Canvas.prototype.cut = function (pens) {
5303
- this.copy(pens);
5380
+ this.copy(pens, false);
5304
5381
  this.delete(pens);
5382
+ this.store.emitter.emit('cut', pens);
5305
5383
  };
5306
5384
  Canvas.prototype.paste = function () {
5307
5385
  var _a;
@@ -5388,6 +5466,7 @@ var Canvas = /** @class */ (function () {
5388
5466
  this.pushHistory({ type: EditType.Add, pens: this.store.clipboard.pens });
5389
5467
  this.render();
5390
5468
  this.store.emitter.emit('add', this.store.clipboard.pens);
5469
+ this.store.emitter.emit('paste', this.store.clipboard.pens);
5391
5470
  return [2 /*return*/];
5392
5471
  }
5393
5472
  });
@@ -6234,10 +6313,11 @@ var Canvas = /** @class */ (function () {
6234
6313
  this.render();
6235
6314
  };
6236
6315
  Canvas.prototype.destroy = function () {
6237
- var _a, _b;
6316
+ var _a, _b, _c;
6238
6317
  this.scroll && this.scroll.destroy();
6239
6318
  (_a = this.tooltip) === null || _a === void 0 ? void 0 : _a.destroy();
6240
6319
  (_b = this.dialog) === null || _b === void 0 ? void 0 : _b.destroy();
6320
+ (_c = this.title) === null || _c === void 0 ? void 0 : _c.destroy();
6241
6321
  // ios
6242
6322
  this.externalElements.removeEventListener('gesturestart', this.onGesturestart);
6243
6323
  this.externalElements.ondragover = function (e) { return e.preventDefault(); };