@leafer-ui/worker 1.2.2 → 1.3.0

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
@@ -70,6 +70,7 @@ function useCanvas(_canvasType, _power) {
70
70
  }
71
71
  core.Platform.name = 'web';
72
72
  core.Platform.isWorker = true;
73
+ core.Platform.backgrounder = true;
73
74
  core.Platform.requestRender = function (render) { requestAnimationFrame(render); };
74
75
  core.defineKey(core.Platform, 'devicePixelRatio', { get() { return 1; } });
75
76
  const { userAgent } = navigator;
@@ -276,7 +277,7 @@ class LayoutBlockData {
276
277
  }
277
278
 
278
279
  const { updateAllMatrix, updateAllChange } = core.LeafHelper;
279
- const debug$2 = core.Debug.get('Layouter');
280
+ const debug$1 = core.Debug.get('Layouter');
280
281
  class Layouter {
281
282
  constructor(target, userConfig) {
282
283
  this.totalTimes = 0;
@@ -311,7 +312,7 @@ class Layouter {
311
312
  target.emitEvent(new core.LayoutEvent(core.LayoutEvent.END, this.layoutedBlocks, this.times));
312
313
  }
313
314
  catch (e) {
314
- debug$2.error(e);
315
+ debug$1.error(e);
315
316
  }
316
317
  this.layoutedBlocks = null;
317
318
  }
@@ -325,9 +326,9 @@ class Layouter {
325
326
  }
326
327
  layoutOnce() {
327
328
  if (this.layouting)
328
- return debug$2.warn('layouting');
329
+ return debug$1.warn('layouting');
329
330
  if (this.times > 3)
330
- return debug$2.warn('layout max times');
331
+ return debug$1.warn('layout max times');
331
332
  this.times++;
332
333
  this.totalTimes++;
333
334
  this.layouting = true;
@@ -431,7 +432,7 @@ class Layouter {
431
432
  }
432
433
  }
433
434
 
434
- const debug$1 = core.Debug.get('Renderer');
435
+ const debug = core.Debug.get('Renderer');
435
436
  class Renderer {
436
437
  get needFill() { return !!(!this.canvas.allowBackgroundColor && this.config.fill); }
437
438
  constructor(target, canvas, userConfig) {
@@ -469,7 +470,7 @@ class Renderer {
469
470
  const { target } = this;
470
471
  this.times = 0;
471
472
  this.totalBounds = new core.Bounds();
472
- debug$1.log(target.innerName, '--->');
473
+ debug.log(target.innerName, '--->');
473
474
  try {
474
475
  if (!target.isApp)
475
476
  target.app.emit(core.RenderEvent.CHILD_START, target);
@@ -480,9 +481,9 @@ class Renderer {
480
481
  }
481
482
  catch (e) {
482
483
  this.rendering = false;
483
- debug$1.error(e);
484
+ debug.error(e);
484
485
  }
485
- debug$1.log('-------------|');
486
+ debug.log('-------------|');
486
487
  }
487
488
  renderAgain() {
488
489
  if (this.rendering) {
@@ -494,9 +495,9 @@ class Renderer {
494
495
  }
495
496
  renderOnce(callback) {
496
497
  if (this.rendering)
497
- return debug$1.warn('rendering');
498
+ return debug.warn('rendering');
498
499
  if (this.times > 3)
499
- return debug$1.warn('render max times');
500
+ return debug.warn('render max times');
500
501
  this.times++;
501
502
  this.totalTimes++;
502
503
  this.rendering = true;
@@ -533,7 +534,7 @@ class Renderer {
533
534
  partRender() {
534
535
  const { canvas, updateBlocks: list } = this;
535
536
  if (!list)
536
- return debug$1.warn('PartRender: need update attr');
537
+ return debug.warn('PartRender: need update attr');
537
538
  this.mergeBlocks();
538
539
  list.forEach(block => { if (canvas.bounds.hit(block) && !block.isEmpty())
539
540
  this.clipRender(block); });
@@ -636,7 +637,7 @@ class Renderer {
636
637
  empty = (!leaf.__world.width || !leaf.__world.height);
637
638
  if (empty) {
638
639
  if (!leaf.isLeafer)
639
- debug$1.tip(leaf.innerName, ': empty');
640
+ debug.tip(leaf.innerName, ': empty');
640
641
  empty = (!leaf.isBranch || leaf.isBranchLeaf);
641
642
  }
642
643
  return empty;
@@ -802,143 +803,26 @@ class Picker {
802
803
  }
803
804
  }
804
805
 
805
- const { Yes, NoAndSkip, YesAndSkip } = core.Answer;
806
- const idCondition = {}, classNameCondition = {}, tagCondition = {};
807
806
  class Selector {
808
807
  constructor(target, userConfig) {
809
808
  this.config = {};
810
- this.innerIdMap = {};
811
- this.idMap = {};
812
- this.methods = {
813
- id: (leaf, name) => leaf.id === name ? (this.target && (this.idMap[name] = leaf), 1) : 0,
814
- innerId: (leaf, innerId) => leaf.innerId === innerId ? (this.target && (this.innerIdMap[innerId] = leaf), 1) : 0,
815
- className: (leaf, name) => leaf.className === name ? 1 : 0,
816
- tag: (leaf, name) => leaf.__tag === name ? 1 : 0,
817
- tags: (leaf, nameMap) => nameMap[leaf.__tag] ? 1 : 0
818
- };
819
- this.target = target;
820
809
  if (userConfig)
821
810
  this.config = core.DataHelper.default(userConfig, this.config);
822
- this.picker = new Picker(target, this);
823
- if (target)
824
- this.__listenEvents();
825
- }
826
- getBy(condition, branch, one, options) {
827
- switch (typeof condition) {
828
- case 'number':
829
- const leaf = this.getByInnerId(condition, branch);
830
- return one ? leaf : (leaf ? [leaf] : []);
831
- case 'string':
832
- switch (condition[0]) {
833
- case '#':
834
- idCondition.id = condition.substring(1), condition = idCondition;
835
- break;
836
- case '.':
837
- classNameCondition.className = condition.substring(1), condition = classNameCondition;
838
- break;
839
- default:
840
- tagCondition.tag = condition, condition = tagCondition;
841
- }
842
- case 'object':
843
- if (condition.id !== undefined) {
844
- const leaf = this.getById(condition.id, branch);
845
- return one ? leaf : (leaf ? [leaf] : []);
846
- }
847
- else if (condition.tag) {
848
- const { tag } = condition, isArray = tag instanceof Array;
849
- return this.getByMethod(isArray ? this.methods.tags : this.methods.tag, branch, one, isArray ? core.DataHelper.toMap(tag) : tag);
850
- }
851
- else {
852
- return this.getByMethod(this.methods.className, branch, one, condition.className);
853
- }
854
- case 'function':
855
- return this.getByMethod(condition, branch, one, options);
856
- }
811
+ this.picker = new Picker(this.target = target, this);
812
+ this.finder = core.Creator.finder && core.Creator.finder();
857
813
  }
858
814
  getByPoint(hitPoint, hitRadius, options) {
859
- if (core.Platform.name === 'node' && this.target)
860
- this.target.emit(core.LayoutEvent.CHECK_UPDATE);
815
+ if (core.Platform.backgrounder && this.target)
816
+ this.target.updateLayout();
861
817
  return this.picker.getByPoint(hitPoint, hitRadius, options);
862
818
  }
863
- getByInnerId(innerId, branch) {
864
- const cache = this.innerIdMap[innerId];
865
- if (cache)
866
- return cache;
867
- this.eachFind(this.toChildren(branch), this.methods.innerId, null, innerId);
868
- return this.findLeaf;
869
- }
870
- getById(id, branch) {
871
- const cache = this.idMap[id];
872
- if (cache && core.LeafHelper.hasParent(cache, branch || this.target))
873
- return cache;
874
- this.eachFind(this.toChildren(branch), this.methods.id, null, id);
875
- return this.findLeaf;
876
- }
877
- getByClassName(className, branch) {
878
- return this.getByMethod(this.methods.className, branch, false, className);
879
- }
880
- getByTag(tag, branch) {
881
- return this.getByMethod(this.methods.tag, branch, false, tag);
882
- }
883
- getByMethod(method, branch, one, options) {
884
- const list = one ? null : [];
885
- this.eachFind(this.toChildren(branch), method, list, options);
886
- return list || this.findLeaf;
887
- }
888
- eachFind(children, method, list, options) {
889
- let child, result;
890
- for (let i = 0, len = children.length; i < len; i++) {
891
- child = children[i];
892
- result = method(child, options);
893
- if (result === Yes || result === YesAndSkip) {
894
- if (list) {
895
- list.push(child);
896
- }
897
- else {
898
- this.findLeaf = child;
899
- return;
900
- }
901
- }
902
- if (child.isBranch && result < NoAndSkip)
903
- this.eachFind(child.children, method, list, options);
904
- }
905
- }
906
- toChildren(branch) {
907
- this.findLeaf = null;
908
- return [branch || this.target];
909
- }
910
- __onRemoveChild(event) {
911
- const { id, innerId } = event.child;
912
- if (this.idMap[id])
913
- delete this.idMap[id];
914
- if (this.innerIdMap[innerId])
915
- delete this.innerIdMap[innerId];
916
- }
917
- __checkIdChange(event) {
918
- if (event.attrName === 'id') {
919
- const id = event.oldValue;
920
- if (this.idMap[id])
921
- delete this.idMap[id];
922
- }
923
- }
924
- __listenEvents() {
925
- this.__eventIds = [
926
- this.target.on_(core.ChildEvent.REMOVE, this.__onRemoveChild, this),
927
- this.target.on_(core.PropertyEvent.CHANGE, this.__checkIdChange, this)
928
- ];
929
- }
930
- __removeListenEvents() {
931
- this.target.off_(this.__eventIds);
932
- this.__eventIds.length = 0;
819
+ getBy(condition, branch, one, options) {
820
+ return this.finder ? this.finder.getBy(condition, branch, one, options) : core.Plugin.need('find');
933
821
  }
934
822
  destroy() {
935
- if (this.__eventIds.length) {
936
- this.__removeListenEvents();
937
- this.picker.destroy();
938
- this.findLeaf = null;
939
- this.innerIdMap = {};
940
- this.idMap = {};
941
- }
823
+ this.picker.destroy();
824
+ if (this.finder)
825
+ this.finder.destroy();
942
826
  }
943
827
  }
944
828
 
@@ -2461,197 +2345,12 @@ const ColorConvertModule = {
2461
2345
  string
2462
2346
  };
2463
2347
 
2464
- const { setPoint, addPoint, toBounds } = core.TwoPointBoundsHelper;
2465
- function getTrimBounds(canvas) {
2466
- const { width, height } = canvas.view;
2467
- const { data } = canvas.context.getImageData(0, 0, width, height);
2468
- let x, y, pointBounds, index = 0;
2469
- for (let i = 0; i < data.length; i += 4) {
2470
- if (data[i + 3] !== 0) {
2471
- x = index % width;
2472
- y = (index - x) / width;
2473
- pointBounds ? addPoint(pointBounds, x, y) : setPoint(pointBounds = {}, x, y);
2474
- }
2475
- index++;
2476
- }
2477
- const bounds = new core.Bounds();
2478
- toBounds(pointBounds, bounds);
2479
- return bounds.scale(1 / canvas.pixelRatio).ceil();
2480
- }
2481
-
2482
- const ExportModule = {
2483
- export(leaf, filename, options) {
2484
- this.running = true;
2485
- const fileType = core.FileHelper.fileType(filename);
2486
- const isDownload = filename.includes('.');
2487
- options = core.FileHelper.getExportOptions(options);
2488
- return addTask((success) => new Promise((resolve) => {
2489
- const over = (result) => {
2490
- success(result);
2491
- resolve();
2492
- this.running = false;
2493
- };
2494
- const { toURL } = core.Platform;
2495
- const { download } = core.Platform.origin;
2496
- if (fileType === 'json') {
2497
- isDownload && download(toURL(JSON.stringify(leaf.toJSON(options.json)), 'text'), filename);
2498
- return over({ data: isDownload ? true : leaf.toJSON(options.json) });
2499
- }
2500
- if (fileType === 'svg') {
2501
- isDownload && download(toURL(leaf.toSVG(), 'svg'), filename);
2502
- return over({ data: isDownload ? true : leaf.toSVG() });
2503
- }
2504
- const { leafer } = leaf;
2505
- if (leafer) {
2506
- checkLazy(leaf);
2507
- leafer.waitViewCompleted(() => __awaiter(this, void 0, void 0, function* () {
2508
- let renderBounds, trimBounds, scaleX = 1, scaleY = 1;
2509
- const { worldTransform, isLeafer, isFrame } = leaf;
2510
- const { slice, trim, onCanvas } = options;
2511
- const smooth = options.smooth === undefined ? leafer.config.smooth : options.smooth;
2512
- const contextSettings = options.contextSettings || leafer.config.contextSettings;
2513
- const screenshot = options.screenshot || leaf.isApp;
2514
- const fill = (isLeafer && screenshot) ? (options.fill === undefined ? leaf.fill : options.fill) : options.fill;
2515
- const needFill = core.FileHelper.isOpaqueImage(filename) || fill, matrix = new core.Matrix();
2516
- if (screenshot) {
2517
- renderBounds = screenshot === true ? (isLeafer ? leafer.canvas.bounds : leaf.worldRenderBounds) : screenshot;
2518
- }
2519
- else {
2520
- let relative = options.relative || (isLeafer ? 'inner' : 'local');
2521
- scaleX = worldTransform.scaleX;
2522
- scaleY = worldTransform.scaleY;
2523
- switch (relative) {
2524
- case 'inner':
2525
- matrix.set(worldTransform);
2526
- break;
2527
- case 'local':
2528
- matrix.set(worldTransform).divide(leaf.localTransform);
2529
- scaleX /= leaf.scaleX;
2530
- scaleY /= leaf.scaleY;
2531
- break;
2532
- case 'world':
2533
- scaleX = 1;
2534
- scaleY = 1;
2535
- break;
2536
- case 'page':
2537
- relative = leaf.leafer;
2538
- default:
2539
- matrix.set(worldTransform).divide(leaf.getTransform(relative));
2540
- const l = relative.worldTransform;
2541
- scaleX /= scaleX / l.scaleX;
2542
- scaleY /= scaleY / l.scaleY;
2543
- }
2544
- renderBounds = leaf.getBounds('render', relative);
2545
- }
2546
- const scaleData = { scaleX: 1, scaleY: 1 };
2547
- core.MathHelper.getScaleData(options.scale, options.size, renderBounds, scaleData);
2548
- let pixelRatio = options.pixelRatio || 1;
2549
- if (leaf.isApp) {
2550
- scaleData.scaleX *= pixelRatio;
2551
- scaleData.scaleY *= pixelRatio;
2552
- pixelRatio = leaf.app.pixelRatio;
2553
- }
2554
- const { x, y, width, height } = new core.Bounds(renderBounds).scale(scaleData.scaleX, scaleData.scaleY);
2555
- const renderOptions = { matrix: matrix.scale(1 / scaleData.scaleX, 1 / scaleData.scaleY).invert().translate(-x, -y).withScale(1 / scaleX * scaleData.scaleX, 1 / scaleY * scaleData.scaleY) };
2556
- let canvas = core.Creator.canvas({ width: Math.round(width), height: Math.round(height), pixelRatio, smooth, contextSettings });
2557
- let sliceLeaf;
2558
- if (slice) {
2559
- sliceLeaf = leaf;
2560
- sliceLeaf.__worldOpacity = 0;
2561
- leaf = leafer;
2562
- renderOptions.bounds = canvas.bounds;
2563
- }
2564
- canvas.save();
2565
- if (isFrame && fill !== undefined) {
2566
- const oldFill = leaf.get('fill');
2567
- leaf.fill = '';
2568
- leaf.__render(canvas, renderOptions);
2569
- leaf.fill = oldFill;
2570
- }
2571
- else {
2572
- leaf.__render(canvas, renderOptions);
2573
- }
2574
- canvas.restore();
2575
- if (sliceLeaf)
2576
- sliceLeaf.__updateWorldOpacity();
2577
- if (trim) {
2578
- trimBounds = getTrimBounds(canvas);
2579
- const old = canvas, { width, height } = trimBounds;
2580
- const config = { x: 0, y: 0, width, height, pixelRatio };
2581
- canvas = core.Creator.canvas(config);
2582
- canvas.copyWorld(old, trimBounds, config);
2583
- }
2584
- if (needFill)
2585
- canvas.fillWorld(canvas.bounds, fill || '#FFFFFF', 'destination-over');
2586
- if (onCanvas)
2587
- onCanvas(canvas);
2588
- const data = filename === 'canvas' ? canvas : yield canvas.export(filename, options);
2589
- over({ data, width: canvas.pixelWidth, height: canvas.pixelHeight, renderBounds, trimBounds });
2590
- }));
2591
- }
2592
- else {
2593
- over({ data: false });
2594
- }
2595
- }));
2596
- }
2597
- };
2598
- let tasker;
2599
- function addTask(task) {
2600
- if (!tasker)
2601
- tasker = new core.TaskProcessor();
2602
- return new Promise((resolve) => {
2603
- tasker.add(() => __awaiter(this, void 0, void 0, function* () { return yield task(resolve); }), { parallel: false });
2604
- });
2605
- }
2606
- function checkLazy(leaf) {
2607
- if (leaf.__.__needComputePaint)
2608
- leaf.__.__computePaint();
2609
- if (leaf.isBranch)
2610
- leaf.children.forEach(child => checkLazy(child));
2611
- }
2612
-
2613
- const canvas = core.LeaferCanvasBase.prototype;
2614
- const debug = core.Debug.get('@leafer-ui/export');
2615
- canvas.export = function (filename, options) {
2616
- const { quality, blob } = core.FileHelper.getExportOptions(options);
2617
- if (filename.includes('.'))
2618
- return this.saveAs(filename, quality);
2619
- else if (blob)
2620
- return this.toBlob(filename, quality);
2621
- else
2622
- return this.toDataURL(filename, quality);
2623
- };
2624
- canvas.toBlob = function (type, quality) {
2625
- return new Promise((resolve) => {
2626
- core.Platform.origin.canvasToBolb(this.view, type, quality).then((blob) => {
2627
- resolve(blob);
2628
- }).catch((e) => {
2629
- debug.error(e);
2630
- resolve(null);
2631
- });
2632
- });
2633
- };
2634
- canvas.toDataURL = function (type, quality) {
2635
- return core.Platform.origin.canvasToDataURL(this.view, type, quality);
2636
- };
2637
- canvas.saveAs = function (filename, quality) {
2638
- return new Promise((resolve) => {
2639
- core.Platform.origin.canvasSaveAs(this.view, filename, quality).then(() => {
2640
- resolve(true);
2641
- }).catch((e) => {
2642
- debug.error(e);
2643
- resolve(false);
2644
- });
2645
- });
2646
- };
2647
-
2648
2348
  Object.assign(draw.TextConvert, TextConvertModule);
2649
2349
  Object.assign(draw.ColorConvert, ColorConvertModule);
2650
2350
  Object.assign(draw.Paint, PaintModule);
2651
2351
  Object.assign(draw.PaintImage, PaintImageModule);
2652
2352
  Object.assign(draw.PaintGradient, PaintGradientModule);
2653
2353
  Object.assign(draw.Effect, EffectModule);
2654
- Object.assign(draw.Export, ExportModule);
2655
2354
 
2656
2355
  Object.assign(core.Creator, {
2657
2356
  interaction: (target, canvas, selector, options) => new core$1.InteractionBase(target, canvas, selector, options),
@@ -2666,6 +2365,7 @@ Object.defineProperty(exports, "LeaferImage", {
2666
2365
  });
2667
2366
  exports.Layouter = Layouter;
2668
2367
  exports.LeaferCanvas = LeaferCanvas;
2368
+ exports.Picker = Picker;
2669
2369
  exports.Renderer = Renderer;
2670
2370
  exports.Selector = Selector;
2671
2371
  exports.Watcher = Watcher;