@leafer-ui/draw 1.0.4 → 1.0.6

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
@@ -67,6 +67,13 @@ function zoomLayerType() {
67
67
 
68
68
  const TextConvert = {};
69
69
  const ColorConvert = {};
70
+ const UnitConvert = {
71
+ number(value, percentRefer) {
72
+ if (typeof value === 'object')
73
+ return value.type === 'percent' ? value.value * percentRefer : value.value;
74
+ return value;
75
+ }
76
+ };
70
77
  const PathArrow = {};
71
78
  const Paint = {};
72
79
  const PaintImage = {};
@@ -87,7 +94,7 @@ const Transition = {
87
94
  }
88
95
  };
89
96
 
90
- const { parse } = core.PathConvert;
97
+ const { parse, objectToCanvasData } = core.PathConvert;
91
98
  const emptyPaint = {};
92
99
  const debug$1 = core.Debug.get('UIData');
93
100
  class UIData extends core.LeafData {
@@ -101,10 +108,11 @@ class UIData extends core.LeafData {
101
108
  scaleX = -scaleX;
102
109
  return scaleX > 1 ? strokeWidth / scaleX : strokeWidth;
103
110
  }
104
- else {
111
+ else
105
112
  return strokeWidth;
106
- }
107
113
  }
114
+ get __hasStroke() { return this.stroke && this.strokeWidth; }
115
+ get __clipAfterFill() { return (this.cornerRadius || this.__pathInputed); }
108
116
  get __autoWidth() { return !this._width; }
109
117
  get __autoHeight() { return !this._height; }
110
118
  get __autoSide() { return !this._width || !this._height; }
@@ -121,9 +129,8 @@ class UIData extends core.LeafData {
121
129
  this.__leaf.scaleX *= -1;
122
130
  debug$1.warn('width < 0, instead -scaleX ', this);
123
131
  }
124
- else {
132
+ else
125
133
  this._width = value;
126
- }
127
134
  }
128
135
  setHeight(value) {
129
136
  if (value < 0) {
@@ -131,9 +138,8 @@ class UIData extends core.LeafData {
131
138
  this.__leaf.scaleY *= -1;
132
139
  debug$1.warn('height < 0, instead -scaleY', this);
133
140
  }
134
- else {
141
+ else
135
142
  this._height = value;
136
- }
137
143
  }
138
144
  setFill(value) {
139
145
  if (this.__naturalWidth)
@@ -174,9 +180,10 @@ class UIData extends core.LeafData {
174
180
  }
175
181
  }
176
182
  setPath(value) {
177
- if (typeof value === 'string') {
183
+ const isString = typeof value === 'string';
184
+ if (isString || (value && typeof value[0] === 'object')) {
178
185
  this.__setInput('path', value);
179
- this._path = parse(value);
186
+ this._path = isString ? parse(value) : objectToCanvasData(value);
180
187
  }
181
188
  else {
182
189
  if (this.__input)
@@ -191,12 +198,8 @@ class UIData extends core.LeafData {
191
198
  value = value.filter((item) => item.visible !== false);
192
199
  this._shadow = value.length ? value : null;
193
200
  }
194
- else if (value) {
195
- this._shadow = value.visible === false ? null : [value];
196
- }
197
- else {
198
- this._shadow = null;
199
- }
201
+ else
202
+ this._shadow = value && value.visible !== false ? [value] : null;
200
203
  }
201
204
  setInnerShadow(value) {
202
205
  this.__setInput('innerShadow', value);
@@ -205,12 +208,8 @@ class UIData extends core.LeafData {
205
208
  value = value.filter((item) => item.visible !== false);
206
209
  this._innerShadow = value.length ? value : null;
207
210
  }
208
- else if (value) {
209
- this._innerShadow = value.visible === false ? null : [value];
210
- }
211
- else {
212
- this._innerShadow = null;
213
- }
211
+ else
212
+ this._innerShadow = value && value.visible !== false ? [value] : null;
214
213
  }
215
214
  __computePaint() {
216
215
  const { fill, stroke } = this.__input;
@@ -221,24 +220,19 @@ class UIData extends core.LeafData {
221
220
  this.__needComputePaint = false;
222
221
  }
223
222
  }
224
- const UnitConvert = {
225
- number(value, percentRefer) {
226
- if (typeof value === 'object')
227
- return value.type === 'percent' ? value.value * percentRefer : value.value;
228
- return value;
229
- }
230
- };
231
223
 
232
224
  class GroupData extends UIData {
233
225
  }
234
226
 
235
227
  class BoxData extends GroupData {
236
228
  get __boxStroke() { return !this.__pathInputed; }
229
+ get __drawAfterFill() { return this.overflow === 'hide' && this.__clipAfterFill && this.__leaf.children.length; }
230
+ get __clipAfterFill() { return this.__leaf.isOverflow || super.__clipAfterFill; }
237
231
  }
238
232
 
239
233
  class LeaferData extends GroupData {
240
- __getInputData() {
241
- const data = super.__getInputData();
234
+ __getInputData(names, options) {
235
+ const data = super.__getInputData(names, options);
242
236
  core.canvasSizeAttrs.forEach(key => delete data[key]);
243
237
  return data;
244
238
  }
@@ -265,6 +259,7 @@ class StarData extends UIData {
265
259
  }
266
260
 
267
261
  class PathData extends UIData {
262
+ get __pathInputed() { return 2; }
268
263
  }
269
264
 
270
265
  class PenData extends GroupData {
@@ -311,16 +306,18 @@ class ImageData extends RectData {
311
306
  delete data.fill;
312
307
  return data;
313
308
  }
314
- __getInputData() {
315
- const data = super.__getInputData();
309
+ __getInputData(names, options) {
310
+ const data = super.__getInputData(names, options);
316
311
  delete data.fill;
317
312
  return data;
318
313
  }
319
314
  }
320
315
 
321
316
  class CanvasData extends RectData {
322
- __getInputData() {
323
- const data = super.__getInputData();
317
+ get __isCanvas() { return true; }
318
+ get __drawAfterFill() { return true; }
319
+ __getInputData(names, options) {
320
+ const data = super.__getInputData(names, options);
324
321
  data.url = this.__leaf.canvas.toDataURL('image/png');
325
322
  return data;
326
323
  }
@@ -347,16 +344,12 @@ const UIBounds = {
347
344
  let width = 0;
348
345
  const { shadow, innerShadow, blur, backgroundBlur } = this.__;
349
346
  if (shadow)
350
- shadow.forEach(item => {
351
- width = Math.max(width, Math.max(Math.abs(item.y), Math.abs(item.x)) + (item.spread > 0 ? item.spread : 0) + item.blur * 1.5);
352
- });
347
+ shadow.forEach(item => width = Math.max(width, Math.max(Math.abs(item.y), Math.abs(item.x)) + (item.spread > 0 ? item.spread : 0) + item.blur * 1.5));
353
348
  if (blur)
354
349
  width = Math.max(width, blur);
355
350
  let shapeWidth = width = Math.ceil(width);
356
351
  if (innerShadow)
357
- innerShadow.forEach(item => {
358
- shapeWidth = Math.max(shapeWidth, Math.max(Math.abs(item.y), Math.abs(item.x)) + (item.spread < 0 ? -item.spread : 0) + item.blur * 1.5);
359
- });
352
+ innerShadow.forEach(item => shapeWidth = Math.max(shapeWidth, Math.max(Math.abs(item.y), Math.abs(item.x)) + (item.spread < 0 ? -item.spread : 0) + item.blur * 1.5));
360
353
  if (backgroundBlur)
361
354
  shapeWidth = Math.max(shapeWidth, backgroundBlur);
362
355
  this.__layout.renderShapeSpread = shapeWidth;
@@ -438,6 +431,16 @@ const UIRender = {
438
431
  if (stroke && !ignoreStroke)
439
432
  this.__.__pixelStroke ? Paint.strokes(stroke, this, canvas) : Paint.stroke('#000000', this, canvas);
440
433
  }
434
+ },
435
+ __drawAfterFill(canvas, options) {
436
+ if (this.__.__clipAfterFill) {
437
+ canvas.save();
438
+ this.windingRule ? canvas.clip(this.windingRule) : canvas.clip();
439
+ this.__drawContent(canvas, options);
440
+ canvas.restore();
441
+ }
442
+ else
443
+ this.__drawContent(canvas, options);
441
444
  }
442
445
  };
443
446
  function drawFast(ui, canvas, options) {
@@ -504,8 +507,8 @@ exports.UI = UI_1 = class UI extends core.Leaf {
504
507
  return core.pen;
505
508
  }
506
509
  get editConfig() { return undefined; }
507
- get editOuter() { return this.__.__isLinePath ? 'LineEditTool' : 'EditTool'; }
508
- get editInner() { return 'PathEditor'; }
510
+ get editOuter() { return ''; }
511
+ get editInner() { return ''; }
509
512
  constructor(data) {
510
513
  super(data);
511
514
  }
@@ -516,9 +519,8 @@ exports.UI = UI_1 = class UI extends core.Leaf {
516
519
  Object.assign(this, data);
517
520
  this.lockNormalStyle = false;
518
521
  }
519
- else {
522
+ else
520
523
  Object.assign(this, data);
521
- }
522
524
  }
523
525
  get(name) {
524
526
  return typeof name === 'string' ? this.__.__getInput(name) : this.__.__getInputData(name);
@@ -564,12 +566,7 @@ exports.UI = UI_1 = class UI extends core.Leaf {
564
566
  this.__drawPathByData(canvas, this.__.path);
565
567
  }
566
568
  __drawPathByData(drawer, data) {
567
- if (data) {
568
- core.PathDrawer.drawPathByData(drawer, data);
569
- }
570
- else {
571
- this.__drawPathByBox(drawer);
572
- }
569
+ data ? core.PathDrawer.drawPathByData(drawer, data) : this.__drawPathByBox(drawer);
573
570
  }
574
571
  __drawPathByBox(drawer) {
575
572
  const { x, y, width, height } = this.__layout.boxBounds;
@@ -577,9 +574,8 @@ exports.UI = UI_1 = class UI extends core.Leaf {
577
574
  const { cornerRadius } = this.__;
578
575
  drawer.roundRect(x, y, width, height, typeof cornerRadius === 'number' ? [cornerRadius] : cornerRadius);
579
576
  }
580
- else {
577
+ else
581
578
  drawer.rect(x, y, width, height);
582
- }
583
579
  }
584
580
  animate(_keyframe, _options, _type, _isTemp) {
585
581
  return core.needPlugin('animate');
@@ -826,23 +822,13 @@ exports.Group = class Group extends exports.UI {
826
822
  if (data.children) {
827
823
  const { children } = data;
828
824
  delete data.children;
829
- if (!this.children) {
830
- this.__setBranch();
831
- }
832
- else {
833
- this.clear();
834
- }
825
+ this.children ? this.clear() : this.__setBranch();
835
826
  super.set(data, isTemp);
836
- let child;
837
- children.forEach(childData => {
838
- child = childData.__ ? childData : core.UICreator.get(childData.tag, childData);
839
- this.add(child);
840
- });
827
+ children.forEach(child => this.add(child));
841
828
  data.children = children;
842
829
  }
843
- else {
830
+ else
844
831
  super.set(data, isTemp);
845
- }
846
832
  }
847
833
  toJSON(options) {
848
834
  const data = super.toJSON(options);
@@ -1262,10 +1248,9 @@ exports.Rect = __decorate([
1262
1248
  core.registerUI()
1263
1249
  ], exports.Rect);
1264
1250
 
1265
- const rect = exports.Rect.prototype;
1266
- const group = exports.Group.prototype;
1251
+ const { copy, add, includes: includes$1 } = core.BoundsHelper;
1252
+ const rect = exports.Rect.prototype, group = exports.Group.prototype;
1267
1253
  const childrenRenderBounds = {};
1268
- const { copy, add, includes: includes$1, copyAndSpread: copyAndSpread$1 } = core.BoundsHelper;
1269
1254
  exports.Box = class Box extends exports.Group {
1270
1255
  get __tag() { return 'Box'; }
1271
1256
  get isBranchLeaf() { return true; }
@@ -1275,37 +1260,31 @@ exports.Box = class Box extends exports.Group {
1275
1260
  }
1276
1261
  __updateStrokeSpread() { return 0; }
1277
1262
  __updateRectRenderSpread() { return 0; }
1278
- __updateRenderSpread() {
1279
- return this.__updateRectRenderSpread() || -1;
1280
- }
1263
+ __updateRenderSpread() { return this.__updateRectRenderSpread() || -1; }
1281
1264
  __updateRectBoxBounds() { }
1282
- __updateBoxBounds(secondLayout) {
1265
+ __updateBoxBounds(_secondLayout) {
1283
1266
  const data = this.__;
1284
1267
  if (this.children.length) {
1285
1268
  if (data.__autoSide) {
1286
- if (this.leafer && this.leafer.ready)
1287
- this.leafer.layouter.addExtra(this);
1288
1269
  super.__updateBoxBounds();
1289
1270
  const { boxBounds } = this.__layout;
1290
1271
  if (!data.__autoSize) {
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;
1272
+ if (data.__autoWidth) {
1273
+ boxBounds.width += boxBounds.x, boxBounds.x = 0;
1274
+ boxBounds.height = data.height, boxBounds.y = 0;
1275
+ }
1276
+ else {
1277
+ boxBounds.height += boxBounds.y, boxBounds.y = 0;
1278
+ boxBounds.width = data.width, boxBounds.x = 0;
1279
+ }
1295
1280
  }
1296
- if (secondLayout && data.flow && data.padding)
1297
- copyAndSpread$1(boxBounds, boxBounds, data.padding, false, data.__autoSize ? null : (data.__autoWidth ? 'width' : 'height'));
1298
1281
  this.__updateNaturalSize();
1299
1282
  }
1300
- else {
1283
+ else
1301
1284
  this.__updateRectBoxBounds();
1302
- }
1303
- if (data.flow)
1304
- this.__updateContentBounds();
1305
1285
  }
1306
- else {
1286
+ else
1307
1287
  this.__updateRectBoxBounds();
1308
- }
1309
1288
  }
1310
1289
  __updateStrokeBounds() { }
1311
1290
  __updateRenderBounds() {
@@ -1315,14 +1294,13 @@ exports.Box = class Box extends exports.Group {
1315
1294
  super.__updateRenderBounds();
1316
1295
  copy(childrenRenderBounds, renderBounds);
1317
1296
  this.__updateRectRenderBounds();
1318
- isOverflow = !includes$1(renderBounds, childrenRenderBounds) || !this.pathInputed || !this.__.cornerRadius;
1297
+ isOverflow = !includes$1(renderBounds, childrenRenderBounds);
1298
+ if (isOverflow && this.__.overflow !== 'hide')
1299
+ add(renderBounds, childrenRenderBounds);
1319
1300
  }
1320
- else {
1301
+ else
1321
1302
  this.__updateRectRenderBounds();
1322
- }
1323
- this.isOverflow !== isOverflow && (this.isOverflow = isOverflow);
1324
- if (!(this.__.__drawAfterFill = this.__.overflow === 'hide') && isOverflow)
1325
- add(renderBounds, childrenRenderBounds);
1303
+ !this.isOverflow !== !isOverflow && (this.isOverflow = isOverflow);
1326
1304
  }
1327
1305
  __updateRectRenderBounds() { }
1328
1306
  __updateRectChange() { }
@@ -1342,20 +1320,9 @@ exports.Box = class Box extends exports.Group {
1342
1320
  this.__renderGroup(canvas, options);
1343
1321
  }
1344
1322
  }
1345
- __drawAfterFill(canvas, options) {
1346
- const { length } = this.children;
1347
- if (this.isOverflow) {
1348
- canvas.save();
1349
- canvas.clip();
1350
- if (length)
1351
- this.__renderGroup(canvas, options);
1352
- canvas.restore();
1353
- }
1354
- else {
1355
- if (length)
1356
- this.__renderGroup(canvas, options);
1357
- }
1358
- if (this.__.stroke && length) {
1323
+ __drawContent(canvas, options) {
1324
+ this.__renderGroup(canvas, options);
1325
+ if (this.__.__hasStroke) {
1359
1326
  canvas.setWorld(this.__nowWorld);
1360
1327
  this.__drawRenderPath(canvas);
1361
1328
  }
@@ -1367,6 +1334,9 @@ __decorate([
1367
1334
  __decorate([
1368
1335
  core.dataType(false)
1369
1336
  ], exports.Box.prototype, "resizeChildren", void 0);
1337
+ __decorate([
1338
+ core.dataType(false)
1339
+ ], exports.Box.prototype, "textBox", void 0);
1370
1340
  __decorate([
1371
1341
  core.affectRenderBoundsType('show')
1372
1342
  ], exports.Box.prototype, "overflow", void 0);
@@ -1516,17 +1486,15 @@ exports.Line = class Line extends exports.UI {
1516
1486
  if (data.__useArrow)
1517
1487
  PathArrow.addArrows(this, false);
1518
1488
  }
1519
- else {
1489
+ else
1520
1490
  super.__updateRenderPath();
1521
- }
1522
1491
  }
1523
1492
  __updateBoxBounds() {
1524
1493
  if (this.points) {
1525
1494
  toBounds(this.__.__pathForRender, this.__layout.boxBounds);
1526
1495
  }
1527
- else {
1496
+ else
1528
1497
  super.__updateBoxBounds();
1529
- }
1530
1498
  }
1531
1499
  };
1532
1500
  __decorate([
@@ -1664,7 +1632,6 @@ exports.Canvas = class Canvas extends exports.Rect {
1664
1632
  super(data);
1665
1633
  this.canvas = core.Creator.canvas(this.__);
1666
1634
  this.context = this.canvas.context;
1667
- this.__.__isCanvas = this.__.__drawAfterFill = true;
1668
1635
  if (data && data.url)
1669
1636
  this.drawImage(data.url);
1670
1637
  }
@@ -1677,8 +1644,7 @@ exports.Canvas = class Canvas extends exports.Rect {
1677
1644
  });
1678
1645
  }
1679
1646
  draw(ui, offset, scale, rotation) {
1680
- ui.__layout.update();
1681
- const matrix = new core.Matrix(ui.__world).invert();
1647
+ const matrix = new core.Matrix(ui.worldTransform).invert();
1682
1648
  const m = new core.Matrix();
1683
1649
  if (offset)
1684
1650
  m.translate(offset.x, offset.y);
@@ -1693,17 +1659,9 @@ exports.Canvas = class Canvas extends exports.Rect {
1693
1659
  paint() {
1694
1660
  this.forceRender();
1695
1661
  }
1696
- __drawAfterFill(canvas, _options) {
1697
- const { width, height, cornerRadius } = this.__, { view } = this.canvas;
1698
- if (cornerRadius || this.pathInputed) {
1699
- canvas.save();
1700
- canvas.clip();
1701
- canvas.drawImage(view, 0, 0, view.width, view.height, 0, 0, width, height);
1702
- canvas.restore();
1703
- }
1704
- else {
1705
- canvas.drawImage(view, 0, 0, view.width, view.height, 0, 0, width, height);
1706
- }
1662
+ __drawContent(canvas, _options) {
1663
+ const { width, height } = this.__, { view } = this.canvas;
1664
+ canvas.drawImage(view, 0, 0, view.width, view.height, 0, 0, width, height);
1707
1665
  }
1708
1666
  __updateSize() {
1709
1667
  const { canvas } = this;
@@ -1747,7 +1705,6 @@ exports.Canvas = __decorate([
1747
1705
  const { copyAndSpread, includes, isSame, spread, setList } = core.BoundsHelper;
1748
1706
  exports.Text = class Text extends exports.UI {
1749
1707
  get __tag() { return 'Text'; }
1750
- get editInner() { return 'TextEditor'; }
1751
1708
  get textDrawData() {
1752
1709
  this.__layout.update();
1753
1710
  return this.__.__textDrawData;
@@ -1896,6 +1853,9 @@ __decorate([
1896
1853
  __decorate([
1897
1854
  core.boundsType('top')
1898
1855
  ], exports.Text.prototype, "verticalAlign", void 0);
1856
+ __decorate([
1857
+ core.boundsType(true)
1858
+ ], exports.Text.prototype, "autoSizeAlign", void 0);
1899
1859
  __decorate([
1900
1860
  core.boundsType('normal')
1901
1861
  ], exports.Text.prototype, "textWrap", void 0);
@@ -1910,7 +1870,6 @@ exports.Path = class Path extends exports.UI {
1910
1870
  get __tag() { return 'Path'; }
1911
1871
  constructor(data) {
1912
1872
  super(data);
1913
- this.__.__pathInputed = 2;
1914
1873
  }
1915
1874
  };
1916
1875
  __decorate([