@leafer-ui/worker 2.0.1 → 2.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/dist/worker.cjs CHANGED
@@ -15,6 +15,39 @@ exports.PathNodeHandleType = void 0;
15
15
  PathNodeHandleType[PathNodeHandleType["mirror"] = 4] = "mirror";
16
16
  })(exports.PathNodeHandleType || (exports.PathNodeHandleType = {}));
17
17
 
18
+ function __awaiter(thisArg, _arguments, P, generator) {
19
+ function adopt(value) {
20
+ return value instanceof P ? value : new P(function(resolve) {
21
+ resolve(value);
22
+ });
23
+ }
24
+ return new (P || (P = Promise))(function(resolve, reject) {
25
+ function fulfilled(value) {
26
+ try {
27
+ step(generator.next(value));
28
+ } catch (e) {
29
+ reject(e);
30
+ }
31
+ }
32
+ function rejected(value) {
33
+ try {
34
+ step(generator["throw"](value));
35
+ } catch (e) {
36
+ reject(e);
37
+ }
38
+ }
39
+ function step(result) {
40
+ result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected);
41
+ }
42
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
43
+ });
44
+ }
45
+
46
+ typeof SuppressedError === "function" ? SuppressedError : function(error, suppressed, message) {
47
+ var e = new Error(message);
48
+ return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
49
+ };
50
+
18
51
  class LeaferCanvas extends core.LeaferCanvasBase {
19
52
  get allowBackgroundColor() {
20
53
  return true;
@@ -39,8 +72,6 @@ core.canvasPatch(OffscreenCanvasRenderingContext2D.prototype);
39
72
 
40
73
  core.canvasPatch(Path2D.prototype);
41
74
 
42
- const {mineType: mineType} = core.FileHelper;
43
-
44
75
  Object.assign(core.Creator, {
45
76
  canvas: (options, manager) => new LeaferCanvas(options, manager),
46
77
  image: options => new core.LeaferImage(options)
@@ -51,7 +82,7 @@ function useCanvas(_canvasType, _power) {
51
82
  createCanvas: (width, height) => new OffscreenCanvas(width, height),
52
83
  canvasToDataURL: (canvas, type, quality) => new Promise((resolve, reject) => {
53
84
  canvas.convertToBlob({
54
- type: mineType(type),
85
+ type: core.FileHelper.mimeType(type),
55
86
  quality: quality
56
87
  }).then(blob => {
57
88
  var reader = new FileReader;
@@ -63,14 +94,14 @@ function useCanvas(_canvasType, _power) {
63
94
  });
64
95
  }),
65
96
  canvasToBolb: (canvas, type, quality) => canvas.convertToBlob({
66
- type: mineType(type),
97
+ type: core.FileHelper.mimeType(type),
67
98
  quality: quality
68
99
  }),
69
100
  canvasSaveAs: (_canvas, _filename, _quality) => new Promise(resolve => resolve()),
70
101
  download(_url, _filename) {
71
102
  return undefined;
72
103
  },
73
- loadImage(src) {
104
+ loadImage(src, _crossOrigin, _leaferImage) {
74
105
  return new Promise((resolve, reject) => {
75
106
  let req = new XMLHttpRequest;
76
107
  req.open("GET", core.Platform.image.getRealURL(src), true);
@@ -85,6 +116,13 @@ function useCanvas(_canvasType, _power) {
85
116
  req.onerror = e => reject(e);
86
117
  req.send();
87
118
  });
119
+ },
120
+ loadContent(url_1) {
121
+ return __awaiter(this, arguments, void 0, function*(url, responseType = "text") {
122
+ const response = yield fetch(url);
123
+ if (!response.ok) throw new Error(`${response.status}`);
124
+ return yield response[responseType]();
125
+ });
88
126
  }
89
127
  };
90
128
  core.Platform.canvas = core.Creator.canvas();
@@ -584,7 +622,7 @@ class Renderer {
584
622
  getCellList() {
585
623
  return undefined;
586
624
  }
587
- addBlock(block) {
625
+ addBlock(block, _leafList) {
588
626
  if (!this.updateBlocks) this.updateBlocks = [];
589
627
  this.updateBlocks.push(block);
590
628
  }
@@ -632,7 +670,8 @@ class Renderer {
632
670
  __onLayoutEnd(event) {
633
671
  if (event.data) event.data.map(item => {
634
672
  let empty;
635
- if (item.updatedList) item.updatedList.list.some(leaf => {
673
+ const {updatedList: updatedList} = item;
674
+ if (updatedList) updatedList.list.some(leaf => {
636
675
  empty = !leaf.__world.width || !leaf.__world.height;
637
676
  if (empty) {
638
677
  if (!leaf.isLeafer) debug.tip(leaf.innerName, ": empty");
@@ -640,7 +679,7 @@ class Renderer {
640
679
  }
641
680
  return empty;
642
681
  });
643
- this.addBlock(empty ? this.canvas.bounds : item.updatedBounds);
682
+ this.addBlock(empty ? this.canvas.bounds : item.updatedBounds, updatedList);
644
683
  });
645
684
  }
646
685
  emitRender(type, bounds, options) {
@@ -846,7 +885,7 @@ class Selector {
846
885
  this.config = {};
847
886
  if (userConfig) this.config = core.DataHelper.default(userConfig, this.config);
848
887
  this.picker = new Picker(this.target = target, this);
849
- this.finder = core.Creator.finder && core.Creator.finder();
888
+ this.finder = core.Creator.finder && core.Creator.finder(target, this.config);
850
889
  }
851
890
  getByPoint(hitPoint, hitRadius, options) {
852
891
  const {target: target, picker: picker} = this;
@@ -905,8 +944,8 @@ function fills(fills, ui, canvas, renderOptions) {
905
944
  canvas.save();
906
945
  if (item.transform) canvas.transform(item.transform);
907
946
  if (originPaint.scaleFixed) {
908
- const {scaleX: scaleX, scaleY: scaleY} = ui.getRenderScaleData(true);
909
- if (originPaint.scaleFixed === true || originPaint.scaleFixed === "zoom-in" && scaleX > 1 && scaleY > 1) canvas.scale(1 / scaleX, 1 / scaleY);
947
+ const {scaleX: scaleX, scaleY: scaleY} = ui.getRenderScaleData(true, originPaint.scaleFixed, false);
948
+ if (scaleX !== 1) canvas.scale(scaleX, scaleY);
910
949
  }
911
950
  if (originPaint.blendMode) canvas.blendMode = originPaint.blendMode;
912
951
  fillPathOrText(ui, canvas, renderOptions);
@@ -1180,11 +1219,14 @@ function compute(attrName, ui) {
1180
1219
  function getLeafPaint(attrName, paint, ui) {
1181
1220
  if (!core.isObject(paint) || paint.visible === false || paint.opacity === 0) return undefined;
1182
1221
  let leafPaint;
1183
- const {boxBounds: boxBounds} = ui.__layout;
1184
- switch (paint.type) {
1222
+ const {boxBounds: boxBounds} = ui.__layout, {type: type} = paint;
1223
+ switch (type) {
1185
1224
  case "image":
1225
+ case "film":
1226
+ case "video":
1186
1227
  if (!paint.url) return undefined;
1187
1228
  leafPaint = draw.PaintImage.image(ui, attrName, paint, boxBounds, !recycleMap || !recycleMap[paint.url]);
1229
+ if (type !== "image") draw.PaintImage[type](leafPaint);
1188
1230
  break;
1189
1231
 
1190
1232
  case "linear":
@@ -1200,7 +1242,7 @@ function getLeafPaint(attrName, paint, ui) {
1200
1242
  break;
1201
1243
 
1202
1244
  case "solid":
1203
- const {type: type, color: color, opacity: opacity} = paint;
1245
+ const {color: color, opacity: opacity} = paint;
1204
1246
  leafPaint = {
1205
1247
  type: type,
1206
1248
  style: draw.ColorConvert.string(color, opacity)
@@ -1244,7 +1286,7 @@ const {isSame: isSame} = core.BoundsHelper;
1244
1286
 
1245
1287
  function image(ui, attrName, paint, boxBounds, firstUse) {
1246
1288
  let leafPaint, event;
1247
- const image = core.ImageManager.get(paint);
1289
+ const image = core.ImageManager.get(paint, paint.type);
1248
1290
  if (cache && paint === cache.paint && isSame(boxBounds, cache.boxBounds)) {
1249
1291
  leafPaint = cache.leafPaint;
1250
1292
  } else {
@@ -1305,8 +1347,8 @@ function image(ui, attrName, paint, boxBounds, firstUse) {
1305
1347
  }
1306
1348
 
1307
1349
  function checkSizeAndCreateData(ui, attrName, paint, image, leafPaint, boxBounds) {
1308
- if (attrName === "fill" && !ui.__.__naturalWidth) {
1309
- const data = ui.__;
1350
+ const data = ui.__;
1351
+ if (attrName === "fill" && !data.__naturalWidth) {
1310
1352
  data.__naturalWidth = image.width / data.pixelRatio;
1311
1353
  data.__naturalHeight = image.height / data.pixelRatio;
1312
1354
  if (data.__autoSide) {
@@ -1318,7 +1360,13 @@ function checkSizeAndCreateData(ui, attrName, paint, image, leafPaint, boxBounds
1318
1360
  return false;
1319
1361
  }
1320
1362
  }
1321
- if (!leafPaint.data) draw.PaintImage.createData(leafPaint, image, paint, boxBounds);
1363
+ if (!leafPaint.data) {
1364
+ draw.PaintImage.createData(leafPaint, image, paint, boxBounds);
1365
+ const {transform: transform} = leafPaint.data, {opacity: opacity, blendMode: blendMode} = paint;
1366
+ const clip = transform && !transform.onlyScale || data.path || data.cornerRadius;
1367
+ if (clip || opacity && opacity < 1 || blendMode) leafPaint.complex = clip ? 2 : true;
1368
+ }
1369
+ if (paint.filter) draw.PaintImage.applyFilter(leafPaint, image, paint.filter, ui);
1322
1370
  return true;
1323
1371
  }
1324
1372
 
@@ -1361,7 +1409,7 @@ function getPatternData(paint, box, image) {
1361
1409
  if (paint.padding) box = tempBox.set(box).shrink(paint.padding);
1362
1410
  if (paint.mode === "strench") paint.mode = "stretch";
1363
1411
  const {width: width, height: height} = image;
1364
- const {opacity: opacity, mode: mode, align: align, offset: offset, scale: scale, size: size, rotation: rotation, skew: skew, clipSize: clipSize, repeat: repeat, gap: gap, filters: filters, interlace: interlace} = paint;
1412
+ const {mode: mode, align: align, offset: offset, scale: scale, size: size, rotation: rotation, skew: skew, clipSize: clipSize, repeat: repeat, gap: gap, interlace: interlace} = paint;
1365
1413
  const sameBox = box.width === width && box.height === height;
1366
1414
  const data = {
1367
1415
  mode: mode
@@ -1424,8 +1472,6 @@ function getPatternData(paint, box, image) {
1424
1472
  data.scaleX = scaleX;
1425
1473
  data.scaleY = scaleY;
1426
1474
  }
1427
- if (opacity && opacity < 1) data.opacity = opacity;
1428
- if (filters) data.filters = filters;
1429
1475
  if (repeat) data.repeat = core.isString(repeat) ? repeat === "x" ? "repeat-x" : "repeat-y" : "repeat";
1430
1476
  if (interlace) data.interlace = core.isNumber(interlace) || interlace.type === "percent" ? {
1431
1477
  type: "x",
@@ -1456,7 +1502,7 @@ const {get: get$2, set: set, rotateOfOuter: rotateOfOuter$1, translate: translat
1456
1502
 
1457
1503
  function stretchMode(data, box, scaleX, scaleY) {
1458
1504
  const transform = get$2(), {x: x, y: y} = box;
1459
- if (x || y) translate(transform, x, y); else transform.onlyScale = true;
1505
+ if (x || y) translate(transform, x, y); else if (scaleX > 0 && scaleY > 0) transform.onlyScale = true;
1460
1506
  scaleHelper(transform, scaleX, scaleY);
1461
1507
  data.transform = transform;
1462
1508
  }
@@ -1528,39 +1574,6 @@ function layout(transform, box, x, y, scaleX, scaleY, rotation, skew) {
1528
1574
  translate(transform, box.x + x, box.y + y);
1529
1575
  }
1530
1576
 
1531
- function __awaiter(thisArg, _arguments, P, generator) {
1532
- function adopt(value) {
1533
- return value instanceof P ? value : new P(function(resolve) {
1534
- resolve(value);
1535
- });
1536
- }
1537
- return new (P || (P = Promise))(function(resolve, reject) {
1538
- function fulfilled(value) {
1539
- try {
1540
- step(generator.next(value));
1541
- } catch (e) {
1542
- reject(e);
1543
- }
1544
- }
1545
- function rejected(value) {
1546
- try {
1547
- step(generator["throw"](value));
1548
- } catch (e) {
1549
- reject(e);
1550
- }
1551
- }
1552
- function step(result) {
1553
- result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected);
1554
- }
1555
- step((generator = generator.apply(thisArg, _arguments || [])).next());
1556
- });
1557
- }
1558
-
1559
- typeof SuppressedError === "function" ? SuppressedError : function(error, suppressed, message) {
1560
- var e = new Error(message);
1561
- return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
1562
- };
1563
-
1564
1577
  const {get: get$1, scale: scale, copy: copy$1} = core.MatrixHelper;
1565
1578
 
1566
1579
  const {getFloorScale: getFloorScale} = core.MathHelper, {abs: abs$1} = Math;
@@ -1578,10 +1591,10 @@ function createPatternTask(paint, ui, canvas, renderOptions) {
1578
1591
  }
1579
1592
 
1580
1593
  function createPattern(paint, ui, canvas, renderOptions) {
1581
- let {scaleX: scaleX, scaleY: scaleY} = draw.PaintImage.getImageRenderScaleData(paint, ui, canvas, renderOptions), id = scaleX + "-" + scaleY;
1594
+ let {scaleX: scaleX, scaleY: scaleY} = draw.PaintImage.getImageRenderScaleData(paint, ui, canvas, renderOptions), id = paint.film ? paint.nowIndex : scaleX + "-" + scaleY;
1582
1595
  if (paint.patternId !== id && !ui.destroyed) {
1583
1596
  if (!(core.Platform.image.isLarge(paint.image, scaleX, scaleY) && !paint.data.repeat)) {
1584
- const {image: image, data: data} = paint, {transform: transform, gap: gap} = data, fixScale = draw.PaintImage.getPatternFixScale(paint, scaleX, scaleY);
1597
+ const {image: image, data: data} = paint, {opacity: opacity} = paint.originPaint, {transform: transform, gap: gap} = data, fixScale = draw.PaintImage.getPatternFixScale(paint, scaleX, scaleY);
1585
1598
  let imageMatrix, xGap, yGap, {width: width, height: height} = image;
1586
1599
  if (fixScale) scaleX *= fixScale, scaleY *= fixScale;
1587
1600
  width *= scaleX;
@@ -1597,7 +1610,7 @@ function createPattern(paint, ui, canvas, renderOptions) {
1597
1610
  if (transform) copy$1(imageMatrix, transform);
1598
1611
  scale(imageMatrix, 1 / scaleX, 1 / scaleY);
1599
1612
  }
1600
- const imageCanvas = image.getCanvas(width, height, data.opacity, data.filters, xGap, yGap, ui.leafer && ui.leafer.config.smooth, data.interlace);
1613
+ const imageCanvas = image.getCanvas(width, height, opacity, undefined, xGap, yGap, ui.leafer && ui.leafer.config.smooth, data.interlace);
1601
1614
  const pattern = image.getPattern(imageCanvas, data.repeat || (core.Platform.origin.noRepeat || "no-repeat"), imageMatrix, paint);
1602
1615
  paint.style = pattern;
1603
1616
  paint.patternId = id;
@@ -1618,15 +1631,15 @@ function getPatternFixScale(paint, imageScaleX, imageScaleY) {
1618
1631
  }
1619
1632
 
1620
1633
  function checkImage(paint, drawImage, ui, canvas, renderOptions) {
1621
- const {scaleX: scaleX, scaleY: scaleY} = draw.PaintImage.getImageRenderScaleData(paint, ui, canvas, renderOptions);
1634
+ const {scaleX: scaleX, scaleY: scaleY} = draw.PaintImage.getImageRenderScaleData(paint, ui, canvas, renderOptions), id = paint.film ? paint.nowIndex : scaleX + "-" + scaleY;
1622
1635
  const {image: image, data: data, originPaint: originPaint} = paint, {exporting: exporting, snapshot: snapshot} = renderOptions;
1623
- if (!data || paint.patternId === scaleX + "-" + scaleY && !exporting || snapshot) {
1636
+ if (!data || paint.patternId === id && !exporting || snapshot) {
1624
1637
  return false;
1625
1638
  } else {
1626
1639
  if (drawImage) {
1627
1640
  if (data.repeat) {
1628
1641
  drawImage = false;
1629
- } else if (!(originPaint.changeful || core.Platform.name === "miniapp" && core.ResizeEvent.isResizing(ui) || exporting)) {
1642
+ } else if (!(originPaint.changeful || paint.film || core.Platform.name === "miniapp" && core.ResizeEvent.isResizing(ui) || exporting)) {
1630
1643
  drawImage = core.Platform.image.isLarge(image, scaleX, scaleY) || image.width * scaleX > 8096 || image.height * scaleY > 8096;
1631
1644
  }
1632
1645
  }
@@ -1644,20 +1657,21 @@ function checkImage(paint, drawImage, ui, canvas, renderOptions) {
1644
1657
  }
1645
1658
  }
1646
1659
 
1647
- function drawImage(paint, _imageScaleX, _imageScaleY, ui, canvas, _renderOptions) {
1648
- const {data: data, image: image} = paint, {blendMode: blendMode} = paint.originPaint, {opacity: opacity, transform: transform} = data, view = image.getFull(data.filters), u = ui.__;
1649
- let {width: width, height: height} = image, clipUI;
1650
- if ((clipUI = transform && !transform.onlyScale || u.path || u.cornerRadius) || opacity || blendMode) {
1660
+ function drawImage(paint, imageScaleX, imageScaleY, ui, canvas, _renderOptions) {
1661
+ const {data: data, image: image, complex: complex} = paint;
1662
+ let {width: width, height: height} = image;
1663
+ if (complex) {
1664
+ const {blendMode: blendMode, opacity: opacity} = paint.originPaint, {transform: transform} = data;
1651
1665
  canvas.save();
1652
- clipUI && canvas.clipUI(ui);
1666
+ complex === 2 && canvas.clipUI(ui);
1653
1667
  blendMode && (canvas.blendMode = blendMode);
1654
1668
  opacity && (canvas.opacity *= opacity);
1655
1669
  transform && canvas.transform(transform);
1656
- canvas.drawImage(view, 0, 0, width, height);
1670
+ image.render(canvas, 0, 0, width, height, ui, paint, imageScaleX, imageScaleY);
1657
1671
  canvas.restore();
1658
1672
  } else {
1659
1673
  if (data.scaleX) width *= data.scaleX, height *= data.scaleY;
1660
- canvas.drawImage(view, 0, 0, width, height);
1674
+ image.render(canvas, 0, 0, width, height, ui, paint, imageScaleX, imageScaleY);
1661
1675
  }
1662
1676
  }
1663
1677
 
@@ -1687,6 +1701,7 @@ function recycleImage(attrName, data) {
1687
1701
  if (!recycleMap) recycleMap = {};
1688
1702
  recycleMap[url] = true;
1689
1703
  core.ImageManager.recyclePaint(paint);
1704
+ if (data.__willDestroy && image.parent) draw.PaintImage.recycleFilter(image, data.__leaf);
1690
1705
  if (image.loading) {
1691
1706
  if (!input) {
1692
1707
  input = data.__input && data.__input[attrName] || [];
@@ -2602,6 +2617,13 @@ Object.assign(core.Creator, {
2602
2617
 
2603
2618
  useCanvas();
2604
2619
 
2620
+ Object.defineProperty(exports, "LeaferFilm", {
2621
+ enumerable: true,
2622
+ get: function() {
2623
+ return core.LeaferFilm;
2624
+ }
2625
+ });
2626
+
2605
2627
  Object.defineProperty(exports, "LeaferImage", {
2606
2628
  enumerable: true,
2607
2629
  get: function() {
@@ -2609,6 +2631,13 @@ Object.defineProperty(exports, "LeaferImage", {
2609
2631
  }
2610
2632
  });
2611
2633
 
2634
+ Object.defineProperty(exports, "LeaferVideo", {
2635
+ enumerable: true,
2636
+ get: function() {
2637
+ return core.LeaferVideo;
2638
+ }
2639
+ });
2640
+
2612
2641
  exports.Layouter = Layouter;
2613
2642
 
2614
2643
  exports.LeaferCanvas = LeaferCanvas;