@leafer-ui/worker 2.0.0 → 2.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/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) {
@@ -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,12 @@ 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
+ }
1322
1369
  return true;
1323
1370
  }
1324
1371
 
@@ -1361,7 +1408,7 @@ function getPatternData(paint, box, image) {
1361
1408
  if (paint.padding) box = tempBox.set(box).shrink(paint.padding);
1362
1409
  if (paint.mode === "strench") paint.mode = "stretch";
1363
1410
  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;
1411
+ 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
1412
  const sameBox = box.width === width && box.height === height;
1366
1413
  const data = {
1367
1414
  mode: mode
@@ -1424,8 +1471,6 @@ function getPatternData(paint, box, image) {
1424
1471
  data.scaleX = scaleX;
1425
1472
  data.scaleY = scaleY;
1426
1473
  }
1427
- if (opacity && opacity < 1) data.opacity = opacity;
1428
- if (filters) data.filters = filters;
1429
1474
  if (repeat) data.repeat = core.isString(repeat) ? repeat === "x" ? "repeat-x" : "repeat-y" : "repeat";
1430
1475
  if (interlace) data.interlace = core.isNumber(interlace) || interlace.type === "percent" ? {
1431
1476
  type: "x",
@@ -1456,7 +1501,7 @@ const {get: get$2, set: set, rotateOfOuter: rotateOfOuter$1, translate: translat
1456
1501
 
1457
1502
  function stretchMode(data, box, scaleX, scaleY) {
1458
1503
  const transform = get$2(), {x: x, y: y} = box;
1459
- if (x || y) translate(transform, x, y); else transform.onlyScale = true;
1504
+ if (x || y) translate(transform, x, y); else if (scaleX > 0 && scaleY > 0) transform.onlyScale = true;
1460
1505
  scaleHelper(transform, scaleX, scaleY);
1461
1506
  data.transform = transform;
1462
1507
  }
@@ -1528,39 +1573,6 @@ function layout(transform, box, x, y, scaleX, scaleY, rotation, skew) {
1528
1573
  translate(transform, box.x + x, box.y + y);
1529
1574
  }
1530
1575
 
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
1576
  const {get: get$1, scale: scale, copy: copy$1} = core.MatrixHelper;
1565
1577
 
1566
1578
  const {getFloorScale: getFloorScale} = core.MathHelper, {abs: abs$1} = Math;
@@ -1578,10 +1590,10 @@ function createPatternTask(paint, ui, canvas, renderOptions) {
1578
1590
  }
1579
1591
 
1580
1592
  function createPattern(paint, ui, canvas, renderOptions) {
1581
- let {scaleX: scaleX, scaleY: scaleY} = draw.PaintImage.getImageRenderScaleData(paint, ui, canvas, renderOptions), id = scaleX + "-" + scaleY;
1593
+ let {scaleX: scaleX, scaleY: scaleY} = draw.PaintImage.getImageRenderScaleData(paint, ui, canvas, renderOptions), id = paint.film ? paint.nowIndex : scaleX + "-" + scaleY;
1582
1594
  if (paint.patternId !== id && !ui.destroyed) {
1583
1595
  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);
1596
+ const {image: image, data: data} = paint, {opacity: opacity, filters: filters} = paint.originPaint, {transform: transform, gap: gap} = data, fixScale = draw.PaintImage.getPatternFixScale(paint, scaleX, scaleY);
1585
1597
  let imageMatrix, xGap, yGap, {width: width, height: height} = image;
1586
1598
  if (fixScale) scaleX *= fixScale, scaleY *= fixScale;
1587
1599
  width *= scaleX;
@@ -1597,7 +1609,7 @@ function createPattern(paint, ui, canvas, renderOptions) {
1597
1609
  if (transform) copy$1(imageMatrix, transform);
1598
1610
  scale(imageMatrix, 1 / scaleX, 1 / scaleY);
1599
1611
  }
1600
- const imageCanvas = image.getCanvas(width, height, data.opacity, data.filters, xGap, yGap, ui.leafer && ui.leafer.config.smooth, data.interlace);
1612
+ const imageCanvas = image.getCanvas(width, height, opacity, filters, xGap, yGap, ui.leafer && ui.leafer.config.smooth, data.interlace);
1601
1613
  const pattern = image.getPattern(imageCanvas, data.repeat || (core.Platform.origin.noRepeat || "no-repeat"), imageMatrix, paint);
1602
1614
  paint.style = pattern;
1603
1615
  paint.patternId = id;
@@ -1618,15 +1630,15 @@ function getPatternFixScale(paint, imageScaleX, imageScaleY) {
1618
1630
  }
1619
1631
 
1620
1632
  function checkImage(paint, drawImage, ui, canvas, renderOptions) {
1621
- const {scaleX: scaleX, scaleY: scaleY} = draw.PaintImage.getImageRenderScaleData(paint, ui, canvas, renderOptions);
1633
+ const {scaleX: scaleX, scaleY: scaleY} = draw.PaintImage.getImageRenderScaleData(paint, ui, canvas, renderOptions), id = paint.film ? paint.nowIndex : scaleX + "-" + scaleY;
1622
1634
  const {image: image, data: data, originPaint: originPaint} = paint, {exporting: exporting, snapshot: snapshot} = renderOptions;
1623
- if (!data || paint.patternId === scaleX + "-" + scaleY && !exporting || snapshot) {
1635
+ if (!data || paint.patternId === id && !exporting || snapshot) {
1624
1636
  return false;
1625
1637
  } else {
1626
1638
  if (drawImage) {
1627
1639
  if (data.repeat) {
1628
1640
  drawImage = false;
1629
- } else if (!(originPaint.changeful || core.Platform.name === "miniapp" && core.ResizeEvent.isResizing(ui) || exporting)) {
1641
+ } else if (!(originPaint.changeful || paint.film || core.Platform.name === "miniapp" && core.ResizeEvent.isResizing(ui) || exporting)) {
1630
1642
  drawImage = core.Platform.image.isLarge(image, scaleX, scaleY) || image.width * scaleX > 8096 || image.height * scaleY > 8096;
1631
1643
  }
1632
1644
  }
@@ -1644,20 +1656,21 @@ function checkImage(paint, drawImage, ui, canvas, renderOptions) {
1644
1656
  }
1645
1657
  }
1646
1658
 
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) {
1659
+ function drawImage(paint, imageScaleX, imageScaleY, ui, canvas, _renderOptions) {
1660
+ const {data: data, image: image, complex: complex} = paint;
1661
+ let {width: width, height: height} = image;
1662
+ if (complex) {
1663
+ const {blendMode: blendMode, opacity: opacity} = paint.originPaint, {transform: transform} = data;
1651
1664
  canvas.save();
1652
- clipUI && canvas.clipUI(ui);
1665
+ complex === 2 && canvas.clipUI(ui);
1653
1666
  blendMode && (canvas.blendMode = blendMode);
1654
1667
  opacity && (canvas.opacity *= opacity);
1655
1668
  transform && canvas.transform(transform);
1656
- canvas.drawImage(view, 0, 0, width, height);
1669
+ image.render(canvas, 0, 0, width, height, ui, paint, imageScaleX, imageScaleY);
1657
1670
  canvas.restore();
1658
1671
  } else {
1659
1672
  if (data.scaleX) width *= data.scaleX, height *= data.scaleY;
1660
- canvas.drawImage(view, 0, 0, width, height);
1673
+ image.render(canvas, 0, 0, width, height, ui, paint, imageScaleX, imageScaleY);
1661
1674
  }
1662
1675
  }
1663
1676
 
@@ -2377,7 +2390,7 @@ function layoutText(drawData, style) {
2377
2390
  let {x: x, y: y, width: width, height: height} = bounds, realHeight = __lineHeight * countRows + (paraSpacing ? paraSpacing * (drawData.paraNumber - 1) : 0);
2378
2391
  let starY = __baseLine;
2379
2392
  if (__clipText && realHeight > height) {
2380
- realHeight = Math.max(height, __lineHeight);
2393
+ realHeight = Math.max(style.__autoHeight ? realHeight : height, __lineHeight);
2381
2394
  if (countRows > 1) drawData.overflow = countRows;
2382
2395
  } else if (height || autoSizeAlign) {
2383
2396
  switch (verticalAlign) {
@@ -2434,10 +2447,10 @@ function layoutText(drawData, style) {
2434
2447
  }
2435
2448
 
2436
2449
  function clipText(drawData, style, x, width) {
2437
- if (!width) return;
2438
2450
  const {rows: rows, overflow: overflow} = drawData;
2439
2451
  let {textOverflow: textOverflow} = style;
2440
- rows.splice(overflow);
2452
+ if (overflow) rows.splice(overflow);
2453
+ if (!width) return;
2441
2454
  if (textOverflow && textOverflow !== "show") {
2442
2455
  if (textOverflow === "hide") textOverflow = ""; else if (textOverflow === "ellipsis") textOverflow = "...";
2443
2456
  let char, charRight;
@@ -2602,6 +2615,13 @@ Object.assign(core.Creator, {
2602
2615
 
2603
2616
  useCanvas();
2604
2617
 
2618
+ Object.defineProperty(exports, "LeaferFilm", {
2619
+ enumerable: true,
2620
+ get: function() {
2621
+ return core.LeaferFilm;
2622
+ }
2623
+ });
2624
+
2605
2625
  Object.defineProperty(exports, "LeaferImage", {
2606
2626
  enumerable: true,
2607
2627
  get: function() {
@@ -2609,6 +2629,13 @@ Object.defineProperty(exports, "LeaferImage", {
2609
2629
  }
2610
2630
  });
2611
2631
 
2632
+ Object.defineProperty(exports, "LeaferVideo", {
2633
+ enumerable: true,
2634
+ get: function() {
2635
+ return core.LeaferVideo;
2636
+ }
2637
+ });
2638
+
2612
2639
  exports.Layouter = Layouter;
2613
2640
 
2614
2641
  exports.LeaferCanvas = LeaferCanvas;