@leafer-ui/draw 1.0.1 → 1.0.3

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/lib/draw.cjs CHANGED
@@ -31,36 +31,6 @@ typeof SuppressedError === "function" ? SuppressedError : function (error, suppr
31
31
  return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
32
32
  };
33
33
 
34
- const TextConvert = {};
35
- const ColorConvert = {};
36
- const PathArrow = {};
37
- const Paint = {};
38
- const PaintImage = {};
39
- const PaintGradient = {};
40
- const Effect = {};
41
- const Export = {};
42
- const State = {};
43
-
44
- function stateType(defaultValue) {
45
- return core.decorateLeafAttr(defaultValue, (key) => core.attr({
46
- set(value) {
47
- this.__setAttr(key, value);
48
- this.waitLeafer(() => { if (State.setStyle)
49
- State.setStyle(this, key + 'Style', value); });
50
- }
51
- }));
52
- }
53
- function arrowType(defaultValue) {
54
- return core.decorateLeafAttr(defaultValue, (key) => core.attr({
55
- set(value) {
56
- if (this.__setAttr(key, value)) {
57
- const data = this.__;
58
- data.__useArrow = data.startArrow !== 'none' || data.endArrow !== 'none';
59
- core.doStrokeType(this);
60
- }
61
- }
62
- }));
63
- }
64
34
  function effectType(defaultValue) {
65
35
  return core.decorateLeafAttr(defaultValue, (key) => core.attr({
66
36
  set(value) {
@@ -95,10 +65,33 @@ function zoomLayerType() {
95
65
  };
96
66
  }
97
67
 
68
+ const TextConvert = {};
69
+ const ColorConvert = {};
70
+ const PathArrow = {};
71
+ const Paint = {};
72
+ const PaintImage = {};
73
+ const PaintGradient = {};
74
+ const Effect = {};
75
+ const Export = {};
76
+ const State = {
77
+ setStyleName(_leaf, _styleName, _value) { return core.needPlugin('state'); },
78
+ set(_leaf, _stateName) { return core.needPlugin('state'); }
79
+ };
80
+ const Transition = {
81
+ list: {},
82
+ register(attrName, fn) {
83
+ Transition.list[attrName] = fn;
84
+ },
85
+ get(attrName) {
86
+ return Transition.list[attrName];
87
+ }
88
+ };
89
+
98
90
  const { parse } = core.PathConvert;
99
91
  const emptyPaint = {};
100
92
  const debug$1 = core.Debug.get('UIData');
101
93
  class UIData extends core.LeafData {
94
+ get scale() { const { scaleX, scaleY } = this; return scaleX !== scaleY ? { x: scaleX, y: scaleY } : scaleX; }
102
95
  get __strokeWidth() {
103
96
  const { strokeWidth, strokeWidthFixed } = this;
104
97
  if (strokeWidthFixed) {
@@ -117,9 +110,10 @@ class UIData extends core.LeafData {
117
110
  get __autoSide() { return !this._width || !this._height; }
118
111
  get __autoSize() { return !this._width && !this._height; }
119
112
  setVisible(value) {
120
- if (this.__leaf.leafer)
121
- this.__leaf.leafer.watcher.hasVisible = true;
122
113
  this._visible = value;
114
+ const { leafer } = this.__leaf;
115
+ if (leafer)
116
+ leafer.watcher.hasVisible = true;
123
117
  }
124
118
  setWidth(value) {
125
119
  if (value < 0) {
@@ -243,6 +237,11 @@ class BoxData extends GroupData {
243
237
  }
244
238
 
245
239
  class LeaferData extends GroupData {
240
+ __getInputData() {
241
+ const data = super.__getInputData();
242
+ core.canvasSizeAttrs.forEach(key => delete data[key]);
243
+ return data;
244
+ }
246
245
  }
247
246
 
248
247
  class FrameData extends BoxData {
@@ -305,7 +304,7 @@ class ImageData extends RectData {
305
304
  __setImageFill(value) {
306
305
  if (this.__leaf.image)
307
306
  this.__leaf.image = null;
308
- this.fill = value ? { type: 'image', mode: 'strench', url: value } : undefined;
307
+ this.fill = value ? { type: 'image', mode: 'stretch', url: value } : undefined;
309
308
  }
310
309
  __getData() {
311
310
  const data = super.__getData();
@@ -320,6 +319,11 @@ class ImageData extends RectData {
320
319
  }
321
320
 
322
321
  class CanvasData extends RectData {
322
+ __getInputData() {
323
+ const data = super.__getInputData();
324
+ data.url = this.__leaf.canvas.toDataURL('image/png');
325
+ return data;
326
+ }
323
327
  }
324
328
 
325
329
  const UIBounds = {
@@ -490,19 +494,8 @@ var UI_1;
490
494
  exports.UI = UI_1 = class UI extends core.Leaf {
491
495
  get app() { return this.leafer && this.leafer.app; }
492
496
  get isFrame() { return false; }
493
- set scale(value) {
494
- if (typeof value === 'number') {
495
- this.scaleX = this.scaleY = value;
496
- }
497
- else {
498
- this.scaleX = value.x;
499
- this.scaleY = value.y;
500
- }
501
- }
502
- get scale() {
503
- const { scaleX, scaleY } = this;
504
- return scaleX !== scaleY ? { x: scaleX, y: scaleY } : scaleX;
505
- }
497
+ set scale(value) { core.MathHelper.assignScale(this, value); }
498
+ get scale() { return this.__.scale; }
506
499
  get pen() {
507
500
  const { path } = this.__;
508
501
  core.pen.set(this.path = path || []);
@@ -517,8 +510,15 @@ exports.UI = UI_1 = class UI extends core.Leaf {
517
510
  super(data);
518
511
  }
519
512
  reset(_data) { }
520
- set(data) {
521
- Object.assign(this, data);
513
+ set(data, isTemp) {
514
+ if (isTemp) {
515
+ this.lockNormalStyle = true;
516
+ Object.assign(this, data);
517
+ this.lockNormalStyle = false;
518
+ }
519
+ else {
520
+ Object.assign(this, data);
521
+ }
522
522
  }
523
523
  get(name) {
524
524
  return typeof name === 'string' ? this.__.__getInput(name) : this.__.__getInputData(name);
@@ -581,11 +581,18 @@ exports.UI = UI_1 = class UI extends core.Leaf {
581
581
  drawer.rect(x, y, width, height);
582
582
  }
583
583
  }
584
+ animate(_keyframe, _options, _type, _isTemp) {
585
+ return core.needPlugin('animate');
586
+ }
587
+ killAnimate(_type) { }
584
588
  export(filename, options) {
585
589
  return Export.export(this, filename, options);
586
590
  }
587
- clone() {
588
- return UI_1.one(this.toJSON());
591
+ clone(newData) {
592
+ const json = this.toJSON();
593
+ if (newData)
594
+ Object.assign(json, newData);
595
+ return UI_1.one(json);
589
596
  }
590
597
  static one(data, x, y, width, height) {
591
598
  return core.UICreator.get(data.tag || this.prototype.__tag, data, x, y, width, height);
@@ -601,6 +608,8 @@ exports.UI = UI_1 = class UI extends core.Leaf {
601
608
  static setEditInner(_editorName) { }
602
609
  destroy() {
603
610
  this.fill = this.stroke = null;
611
+ if (this.__animate)
612
+ this.killAnimate();
604
613
  super.destroy();
605
614
  }
606
615
  };
@@ -628,12 +637,6 @@ __decorate([
628
637
  __decorate([
629
638
  core.visibleType(true)
630
639
  ], exports.UI.prototype, "visible", void 0);
631
- __decorate([
632
- stateType(false)
633
- ], exports.UI.prototype, "selected", void 0);
634
- __decorate([
635
- stateType(false)
636
- ], exports.UI.prototype, "disabled", void 0);
637
640
  __decorate([
638
641
  core.surfaceType(false)
639
642
  ], exports.UI.prototype, "locked", void 0);
@@ -706,45 +709,9 @@ __decorate([
706
709
  __decorate([
707
710
  core.pathType(true)
708
711
  ], exports.UI.prototype, "closed", void 0);
709
- __decorate([
710
- core.autoLayoutType(false)
711
- ], exports.UI.prototype, "flow", void 0);
712
712
  __decorate([
713
713
  core.boundsType(0)
714
714
  ], exports.UI.prototype, "padding", void 0);
715
- __decorate([
716
- core.boundsType(0)
717
- ], exports.UI.prototype, "gap", void 0);
718
- __decorate([
719
- core.boundsType('top-left')
720
- ], exports.UI.prototype, "flowAlign", void 0);
721
- __decorate([
722
- core.boundsType(false)
723
- ], exports.UI.prototype, "flowWrap", void 0);
724
- __decorate([
725
- core.boundsType('box')
726
- ], exports.UI.prototype, "itemBox", void 0);
727
- __decorate([
728
- core.boundsType(true)
729
- ], exports.UI.prototype, "inFlow", void 0);
730
- __decorate([
731
- core.boundsType()
732
- ], exports.UI.prototype, "autoWidth", void 0);
733
- __decorate([
734
- core.boundsType()
735
- ], exports.UI.prototype, "autoHeight", void 0);
736
- __decorate([
737
- core.boundsType()
738
- ], exports.UI.prototype, "lockRatio", void 0);
739
- __decorate([
740
- core.boundsType()
741
- ], exports.UI.prototype, "autoBox", void 0);
742
- __decorate([
743
- core.boundsType()
744
- ], exports.UI.prototype, "widthRange", void 0);
745
- __decorate([
746
- core.boundsType()
747
- ], exports.UI.prototype, "heightRange", void 0);
748
715
  __decorate([
749
716
  core.dataType(false)
750
717
  ], exports.UI.prototype, "draggable", void 0);
@@ -808,12 +775,6 @@ __decorate([
808
775
  __decorate([
809
776
  core.strokeType(10)
810
777
  ], exports.UI.prototype, "miterLimit", void 0);
811
- __decorate([
812
- arrowType('none')
813
- ], exports.UI.prototype, "startArrow", void 0);
814
- __decorate([
815
- arrowType('none')
816
- ], exports.UI.prototype, "endArrow", void 0);
817
778
  __decorate([
818
779
  core.pathType(0)
819
780
  ], exports.UI.prototype, "cornerRadius", void 0);
@@ -835,24 +796,6 @@ __decorate([
835
796
  __decorate([
836
797
  effectType()
837
798
  ], exports.UI.prototype, "grayscale", void 0);
838
- __decorate([
839
- core.dataType()
840
- ], exports.UI.prototype, "normalStyle", void 0);
841
- __decorate([
842
- core.dataType()
843
- ], exports.UI.prototype, "hoverStyle", void 0);
844
- __decorate([
845
- core.dataType()
846
- ], exports.UI.prototype, "pressStyle", void 0);
847
- __decorate([
848
- core.dataType()
849
- ], exports.UI.prototype, "focusStyle", void 0);
850
- __decorate([
851
- core.dataType()
852
- ], exports.UI.prototype, "selectedStyle", void 0);
853
- __decorate([
854
- core.dataType()
855
- ], exports.UI.prototype, "disabledStyle", void 0);
856
799
  __decorate([
857
800
  core.dataType({})
858
801
  ], exports.UI.prototype, "data", void 0);
@@ -879,7 +822,7 @@ exports.Group = class Group extends exports.UI {
879
822
  if (!this.children)
880
823
  this.children = [];
881
824
  }
882
- set(data) {
825
+ set(data, isTemp) {
883
826
  if (data.children) {
884
827
  const { children } = data;
885
828
  delete data.children;
@@ -889,7 +832,7 @@ exports.Group = class Group extends exports.UI {
889
832
  else {
890
833
  this.clear();
891
834
  }
892
- super.set(data);
835
+ super.set(data, isTemp);
893
836
  let child;
894
837
  children.forEach(childData => {
895
838
  child = childData.__ ? childData : core.UICreator.get(childData.tag, childData);
@@ -898,7 +841,7 @@ exports.Group = class Group extends exports.UI {
898
841
  data.children = children;
899
842
  }
900
843
  else {
901
- super.set(data);
844
+ super.set(data, isTemp);
902
845
  }
903
846
  }
904
847
  toJSON(options) {
@@ -1225,13 +1168,20 @@ exports.Leafer = Leafer_1 = class Leafer extends exports.Group {
1225
1168
  }
1226
1169
  }
1227
1170
  zoom(_zoomType, _padding, _fixedScale) {
1228
- return debug.error('need @leafer-in/view');
1171
+ return core.needPlugin('view');
1229
1172
  }
1230
1173
  getValidMove(moveX, moveY) { return { x: moveX, y: moveY }; }
1231
1174
  getValidScale(changeScale) { return changeScale; }
1232
1175
  getWorldPointByClient(clientPoint, updateClient) {
1233
1176
  return this.interaction && this.interaction.getLocal(clientPoint, updateClient);
1234
1177
  }
1178
+ getPagePointByClient(clientPoint, updateClient) {
1179
+ return this.getPagePoint(this.getWorldPointByClient(clientPoint, updateClient));
1180
+ }
1181
+ updateClientBounds() {
1182
+ this.canvas && this.canvas.updateClientBounds();
1183
+ }
1184
+ receiveEvent(_event) { }
1235
1185
  __checkUpdateLayout() {
1236
1186
  this.__layout.update();
1237
1187
  }
@@ -1315,7 +1265,7 @@ exports.Rect = __decorate([
1315
1265
  const rect = exports.Rect.prototype;
1316
1266
  const group = exports.Group.prototype;
1317
1267
  const childrenRenderBounds = {};
1318
- const { copy, add, includes: includes$1 } = core.BoundsHelper;
1268
+ const { copy, add, includes: includes$1, copyAndSpread: copyAndSpread$1 } = core.BoundsHelper;
1319
1269
  exports.Box = class Box extends exports.Group {
1320
1270
  get __tag() { return 'Box'; }
1321
1271
  get isBranchLeaf() { return true; }
@@ -1329,20 +1279,23 @@ exports.Box = class Box extends exports.Group {
1329
1279
  return this.__updateRectRenderSpread() || -1;
1330
1280
  }
1331
1281
  __updateRectBoxBounds() { }
1332
- __updateBoxBounds() {
1282
+ __updateBoxBounds(secondLayout) {
1333
1283
  const data = this.__;
1334
1284
  if (this.children.length) {
1335
1285
  if (data.__autoSide) {
1336
1286
  if (this.leafer && this.leafer.ready)
1337
1287
  this.leafer.layouter.addExtra(this);
1338
1288
  super.__updateBoxBounds();
1289
+ const { boxBounds } = this.__layout;
1339
1290
  if (!data.__autoSize) {
1340
- const b = this.__layout.boxBounds;
1341
- if (!data.__autoWidth)
1342
- b.height += b.y, b.width = data.width, b.x = b.y = 0;
1343
- if (!data.__autoHeight)
1344
- b.width += b.x, b.height = data.height, b.y = b.x = 0;
1291
+ if (data.__autoWidth)
1292
+ boxBounds.width += boxBounds.x, boxBounds.height = data.height, boxBounds.y = boxBounds.x = 0;
1293
+ else
1294
+ boxBounds.height += boxBounds.y, boxBounds.width = data.width, boxBounds.x = boxBounds.y = 0;
1345
1295
  }
1296
+ if (secondLayout && data.flow && data.padding)
1297
+ copyAndSpread$1(boxBounds, boxBounds, data.padding, false, data.__autoSize ? null : (data.__autoWidth ? 'width' : 'height'));
1298
+ this.__updateNaturalSize();
1346
1299
  }
1347
1300
  else {
1348
1301
  this.__updateRectBoxBounds();
@@ -1362,13 +1315,13 @@ exports.Box = class Box extends exports.Group {
1362
1315
  super.__updateRenderBounds();
1363
1316
  copy(childrenRenderBounds, renderBounds);
1364
1317
  this.__updateRectRenderBounds();
1365
- isOverflow = !includes$1(renderBounds, childrenRenderBounds) || undefined;
1318
+ isOverflow = !includes$1(renderBounds, childrenRenderBounds) || !this.pathInputed || !this.__.cornerRadius;
1366
1319
  }
1367
1320
  else {
1368
1321
  this.__updateRectRenderBounds();
1369
1322
  }
1370
1323
  this.isOverflow !== isOverflow && (this.isOverflow = isOverflow);
1371
- if (isOverflow && !(this.__.__drawAfterFill = this.__.overflow === 'hide'))
1324
+ if (!(this.__.__drawAfterFill = this.__.overflow === 'hide') && isOverflow)
1372
1325
  add(renderBounds, childrenRenderBounds);
1373
1326
  }
1374
1327
  __updateRectRenderBounds() { }
@@ -1702,14 +1655,26 @@ __decorate([
1702
1655
  exports.Image = __decorate([
1703
1656
  core.registerUI()
1704
1657
  ], exports.Image);
1658
+ const MyImage = exports.Image;
1705
1659
 
1706
1660
  exports.Canvas = class Canvas extends exports.Rect {
1707
1661
  get __tag() { return 'Canvas'; }
1662
+ get ready() { return !this.url; }
1708
1663
  constructor(data) {
1709
1664
  super(data);
1710
1665
  this.canvas = core.Creator.canvas(this.__);
1711
1666
  this.context = this.canvas.context;
1712
1667
  this.__.__isCanvas = this.__.__drawAfterFill = true;
1668
+ if (data && data.url)
1669
+ this.drawImage(data.url);
1670
+ }
1671
+ drawImage(url) {
1672
+ new core.LeaferImage({ url }).load((image) => {
1673
+ this.context.drawImage(image.view, 0, 0);
1674
+ this.url = undefined;
1675
+ this.paint();
1676
+ this.emitEvent(new core.ImageEvent(core.ImageEvent.LOADED, { image }));
1677
+ });
1713
1678
  }
1714
1679
  draw(ui, offset, scale, rotation) {
1715
1680
  ui.__layout.update();
@@ -1726,19 +1691,18 @@ exports.Canvas = class Canvas extends exports.Rect {
1726
1691
  this.paint();
1727
1692
  }
1728
1693
  paint() {
1729
- this.forceUpdate('fill');
1694
+ this.forceRender();
1730
1695
  }
1731
1696
  __drawAfterFill(canvas, _options) {
1732
- const origin = this.canvas.view;
1733
- const { width, height } = this;
1734
- if (this.__.cornerRadius || this.pathInputed) {
1697
+ const { width, height, cornerRadius } = this.__, { view } = this.canvas;
1698
+ if (cornerRadius || this.pathInputed) {
1735
1699
  canvas.save();
1736
1700
  canvas.clip();
1737
- canvas.drawImage(this.canvas.view, 0, 0, origin.width, origin.height, 0, 0, width, height);
1701
+ canvas.drawImage(view, 0, 0, view.width, view.height, 0, 0, width, height);
1738
1702
  canvas.restore();
1739
1703
  }
1740
1704
  else {
1741
- canvas.drawImage(this.canvas.view, 0, 0, origin.width, origin.height, 0, 0, width, height);
1705
+ canvas.drawImage(view, 0, 0, view.width, view.height, 0, 0, width, height);
1742
1706
  }
1743
1707
  }
1744
1708
  __updateSize() {
@@ -1768,7 +1732,7 @@ __decorate([
1768
1732
  resizeType(100)
1769
1733
  ], exports.Canvas.prototype, "height", void 0);
1770
1734
  __decorate([
1771
- resizeType(core.Platform.devicePixelRatio)
1735
+ resizeType(1)
1772
1736
  ], exports.Canvas.prototype, "pixelRatio", void 0);
1773
1737
  __decorate([
1774
1738
  resizeType(true)
@@ -1792,13 +1756,13 @@ exports.Text = class Text extends exports.UI {
1792
1756
  super(data);
1793
1757
  }
1794
1758
  __drawHitPath(canvas) {
1795
- const { __lineHeight, __baseLine, __textDrawData: data } = this.__;
1759
+ const { __lineHeight, fontSize, __baseLine, __textDrawData: data } = this.__;
1796
1760
  canvas.beginPath();
1797
1761
  if (this.__.__letterSpacing < 0) {
1798
1762
  this.__drawPathByData(canvas);
1799
1763
  }
1800
1764
  else {
1801
- data.rows.forEach(row => canvas.rect(row.x, row.y - __baseLine, row.width, __lineHeight));
1765
+ data.rows.forEach(row => canvas.rect(row.x, row.y - __baseLine, row.width, __lineHeight < fontSize ? fontSize : __lineHeight));
1802
1766
  }
1803
1767
  }
1804
1768
  __drawPathByData(drawer, _data) {
@@ -1991,7 +1955,8 @@ exports.Pen = class Pen extends exports.Group {
1991
1955
  drawPoints(_points, _curve, _close) { return this; }
1992
1956
  clearPath() { return this; }
1993
1957
  paint() {
1994
- this.pathElement.forceUpdate('path');
1958
+ if (!this.pathElement.__layout.boxChanged)
1959
+ this.pathElement.forceUpdate('path');
1995
1960
  }
1996
1961
  };
1997
1962
  __decorate([
@@ -2001,7 +1966,7 @@ __decorate([
2001
1966
  penPathType()
2002
1967
  ], exports.Pen.prototype, "path", void 0);
2003
1968
  exports.Pen = __decorate([
2004
- core.useModule(core.PathCreator, ['set', 'beginPath', 'path']),
1969
+ core.useModule(core.PathCreator, ['set', 'beginPath', 'path', 'paint']),
2005
1970
  core.registerUI()
2006
1971
  ], exports.Pen);
2007
1972
  function penPathType() {
@@ -2023,6 +1988,7 @@ exports.GroupData = GroupData;
2023
1988
  exports.ImageData = ImageData;
2024
1989
  exports.LeaferData = LeaferData;
2025
1990
  exports.LineData = LineData;
1991
+ exports.MyImage = MyImage;
2026
1992
  exports.Paint = Paint;
2027
1993
  exports.PaintGradient = PaintGradient;
2028
1994
  exports.PaintImage = PaintImage;
@@ -2036,14 +2002,13 @@ exports.StarData = StarData;
2036
2002
  exports.State = State;
2037
2003
  exports.TextConvert = TextConvert;
2038
2004
  exports.TextData = TextData;
2005
+ exports.Transition = Transition;
2039
2006
  exports.UIBounds = UIBounds;
2040
2007
  exports.UIData = UIData;
2041
2008
  exports.UIRender = UIRender;
2042
2009
  exports.UnitConvert = UnitConvert;
2043
- exports.arrowType = arrowType;
2044
2010
  exports.effectType = effectType;
2045
2011
  exports.resizeType = resizeType;
2046
- exports.stateType = stateType;
2047
2012
  exports.zoomLayerType = zoomLayerType;
2048
2013
  Object.keys(core).forEach(function (k) {
2049
2014
  if (k !== 'default' && !Object.prototype.hasOwnProperty.call(exports, k)) Object.defineProperty(exports, k, {