@leafer-ui/miniapp 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.
@@ -1,4 +1,4 @@
1
- import { LeaferCanvasBase, Platform, canvasPatch, DataHelper, canvasSizeAttrs, ResizeEvent, Creator, LeaferImage, defineKey, FileHelper, LeafList, RenderEvent, ChildEvent, WatchEvent, PropertyEvent, LeafHelper, BranchHelper, Bounds, LeafBoundsHelper, Debug, LeafLevelList, LayoutEvent, Run, ImageManager, BoundsHelper, Answer, MatrixHelper, MathHelper, AlignHelper, ImageEvent, AroundHelper, PointHelper, Direction4, TwoPointBoundsHelper, TaskProcessor, Matrix } from '@leafer/core';
1
+ import { LeaferCanvasBase, Platform, canvasPatch, DataHelper, canvasSizeAttrs, ResizeEvent, Creator, LeaferImage, defineKey, FileHelper, LeafList, RenderEvent, ChildEvent, WatchEvent, PropertyEvent, LeafHelper, BranchHelper, Bounds, LeafBoundsHelper, Debug, LeafLevelList, LayoutEvent, Run, ImageManager, BoundsHelper, Plugin, MatrixHelper, MathHelper, AlignHelper, ImageEvent, AroundHelper, PointHelper, Direction4 } from '@leafer/core';
2
2
  export * from '@leafer/core';
3
3
  export { LeaferImage } from '@leafer/core';
4
4
  import { InteractionHelper, InteractionBase, HitCanvasManager } from '@leafer-ui/core';
@@ -417,7 +417,7 @@ class LayoutBlockData {
417
417
  }
418
418
 
419
419
  const { updateAllMatrix, updateAllChange } = LeafHelper;
420
- const debug$2 = Debug.get('Layouter');
420
+ const debug$1 = Debug.get('Layouter');
421
421
  class Layouter {
422
422
  constructor(target, userConfig) {
423
423
  this.totalTimes = 0;
@@ -452,7 +452,7 @@ class Layouter {
452
452
  target.emitEvent(new LayoutEvent(LayoutEvent.END, this.layoutedBlocks, this.times));
453
453
  }
454
454
  catch (e) {
455
- debug$2.error(e);
455
+ debug$1.error(e);
456
456
  }
457
457
  this.layoutedBlocks = null;
458
458
  }
@@ -466,9 +466,9 @@ class Layouter {
466
466
  }
467
467
  layoutOnce() {
468
468
  if (this.layouting)
469
- return debug$2.warn('layouting');
469
+ return debug$1.warn('layouting');
470
470
  if (this.times > 3)
471
- return debug$2.warn('layout max times');
471
+ return debug$1.warn('layout max times');
472
472
  this.times++;
473
473
  this.totalTimes++;
474
474
  this.layouting = true;
@@ -572,7 +572,7 @@ class Layouter {
572
572
  }
573
573
  }
574
574
 
575
- const debug$1 = Debug.get('Renderer');
575
+ const debug = Debug.get('Renderer');
576
576
  class Renderer {
577
577
  get needFill() { return !!(!this.canvas.allowBackgroundColor && this.config.fill); }
578
578
  constructor(target, canvas, userConfig) {
@@ -610,7 +610,7 @@ class Renderer {
610
610
  const { target } = this;
611
611
  this.times = 0;
612
612
  this.totalBounds = new Bounds();
613
- debug$1.log(target.innerName, '--->');
613
+ debug.log(target.innerName, '--->');
614
614
  try {
615
615
  if (!target.isApp)
616
616
  target.app.emit(RenderEvent.CHILD_START, target);
@@ -621,9 +621,9 @@ class Renderer {
621
621
  }
622
622
  catch (e) {
623
623
  this.rendering = false;
624
- debug$1.error(e);
624
+ debug.error(e);
625
625
  }
626
- debug$1.log('-------------|');
626
+ debug.log('-------------|');
627
627
  }
628
628
  renderAgain() {
629
629
  if (this.rendering) {
@@ -635,9 +635,9 @@ class Renderer {
635
635
  }
636
636
  renderOnce(callback) {
637
637
  if (this.rendering)
638
- return debug$1.warn('rendering');
638
+ return debug.warn('rendering');
639
639
  if (this.times > 3)
640
- return debug$1.warn('render max times');
640
+ return debug.warn('render max times');
641
641
  this.times++;
642
642
  this.totalTimes++;
643
643
  this.rendering = true;
@@ -674,7 +674,7 @@ class Renderer {
674
674
  partRender() {
675
675
  const { canvas, updateBlocks: list } = this;
676
676
  if (!list)
677
- return debug$1.warn('PartRender: need update attr');
677
+ return debug.warn('PartRender: need update attr');
678
678
  this.mergeBlocks();
679
679
  list.forEach(block => { if (canvas.bounds.hit(block) && !block.isEmpty())
680
680
  this.clipRender(block); });
@@ -777,7 +777,7 @@ class Renderer {
777
777
  empty = (!leaf.__world.width || !leaf.__world.height);
778
778
  if (empty) {
779
779
  if (!leaf.isLeafer)
780
- debug$1.tip(leaf.innerName, ': empty');
780
+ debug.tip(leaf.innerName, ': empty');
781
781
  empty = (!leaf.isBranch || leaf.isBranchLeaf);
782
782
  }
783
783
  return empty;
@@ -943,143 +943,26 @@ class Picker {
943
943
  }
944
944
  }
945
945
 
946
- const { Yes, NoAndSkip, YesAndSkip } = Answer;
947
- const idCondition = {}, classNameCondition = {}, tagCondition = {};
948
946
  class Selector {
949
947
  constructor(target, userConfig) {
950
948
  this.config = {};
951
- this.innerIdMap = {};
952
- this.idMap = {};
953
- this.methods = {
954
- id: (leaf, name) => leaf.id === name ? (this.target && (this.idMap[name] = leaf), 1) : 0,
955
- innerId: (leaf, innerId) => leaf.innerId === innerId ? (this.target && (this.innerIdMap[innerId] = leaf), 1) : 0,
956
- className: (leaf, name) => leaf.className === name ? 1 : 0,
957
- tag: (leaf, name) => leaf.__tag === name ? 1 : 0,
958
- tags: (leaf, nameMap) => nameMap[leaf.__tag] ? 1 : 0
959
- };
960
- this.target = target;
961
949
  if (userConfig)
962
950
  this.config = DataHelper.default(userConfig, this.config);
963
- this.picker = new Picker(target, this);
964
- if (target)
965
- this.__listenEvents();
966
- }
967
- getBy(condition, branch, one, options) {
968
- switch (typeof condition) {
969
- case 'number':
970
- const leaf = this.getByInnerId(condition, branch);
971
- return one ? leaf : (leaf ? [leaf] : []);
972
- case 'string':
973
- switch (condition[0]) {
974
- case '#':
975
- idCondition.id = condition.substring(1), condition = idCondition;
976
- break;
977
- case '.':
978
- classNameCondition.className = condition.substring(1), condition = classNameCondition;
979
- break;
980
- default:
981
- tagCondition.tag = condition, condition = tagCondition;
982
- }
983
- case 'object':
984
- if (condition.id !== undefined) {
985
- const leaf = this.getById(condition.id, branch);
986
- return one ? leaf : (leaf ? [leaf] : []);
987
- }
988
- else if (condition.tag) {
989
- const { tag } = condition, isArray = tag instanceof Array;
990
- return this.getByMethod(isArray ? this.methods.tags : this.methods.tag, branch, one, isArray ? DataHelper.toMap(tag) : tag);
991
- }
992
- else {
993
- return this.getByMethod(this.methods.className, branch, one, condition.className);
994
- }
995
- case 'function':
996
- return this.getByMethod(condition, branch, one, options);
997
- }
951
+ this.picker = new Picker(this.target = target, this);
952
+ this.finder = Creator.finder && Creator.finder();
998
953
  }
999
954
  getByPoint(hitPoint, hitRadius, options) {
1000
- if (Platform.name === 'node' && this.target)
1001
- this.target.emit(LayoutEvent.CHECK_UPDATE);
955
+ if (Platform.backgrounder && this.target)
956
+ this.target.updateLayout();
1002
957
  return this.picker.getByPoint(hitPoint, hitRadius, options);
1003
958
  }
1004
- getByInnerId(innerId, branch) {
1005
- const cache = this.innerIdMap[innerId];
1006
- if (cache)
1007
- return cache;
1008
- this.eachFind(this.toChildren(branch), this.methods.innerId, null, innerId);
1009
- return this.findLeaf;
1010
- }
1011
- getById(id, branch) {
1012
- const cache = this.idMap[id];
1013
- if (cache && LeafHelper.hasParent(cache, branch || this.target))
1014
- return cache;
1015
- this.eachFind(this.toChildren(branch), this.methods.id, null, id);
1016
- return this.findLeaf;
1017
- }
1018
- getByClassName(className, branch) {
1019
- return this.getByMethod(this.methods.className, branch, false, className);
1020
- }
1021
- getByTag(tag, branch) {
1022
- return this.getByMethod(this.methods.tag, branch, false, tag);
1023
- }
1024
- getByMethod(method, branch, one, options) {
1025
- const list = one ? null : [];
1026
- this.eachFind(this.toChildren(branch), method, list, options);
1027
- return list || this.findLeaf;
1028
- }
1029
- eachFind(children, method, list, options) {
1030
- let child, result;
1031
- for (let i = 0, len = children.length; i < len; i++) {
1032
- child = children[i];
1033
- result = method(child, options);
1034
- if (result === Yes || result === YesAndSkip) {
1035
- if (list) {
1036
- list.push(child);
1037
- }
1038
- else {
1039
- this.findLeaf = child;
1040
- return;
1041
- }
1042
- }
1043
- if (child.isBranch && result < NoAndSkip)
1044
- this.eachFind(child.children, method, list, options);
1045
- }
1046
- }
1047
- toChildren(branch) {
1048
- this.findLeaf = null;
1049
- return [branch || this.target];
1050
- }
1051
- __onRemoveChild(event) {
1052
- const { id, innerId } = event.child;
1053
- if (this.idMap[id])
1054
- delete this.idMap[id];
1055
- if (this.innerIdMap[innerId])
1056
- delete this.innerIdMap[innerId];
1057
- }
1058
- __checkIdChange(event) {
1059
- if (event.attrName === 'id') {
1060
- const id = event.oldValue;
1061
- if (this.idMap[id])
1062
- delete this.idMap[id];
1063
- }
1064
- }
1065
- __listenEvents() {
1066
- this.__eventIds = [
1067
- this.target.on_(ChildEvent.REMOVE, this.__onRemoveChild, this),
1068
- this.target.on_(PropertyEvent.CHANGE, this.__checkIdChange, this)
1069
- ];
1070
- }
1071
- __removeListenEvents() {
1072
- this.target.off_(this.__eventIds);
1073
- this.__eventIds.length = 0;
959
+ getBy(condition, branch, one, options) {
960
+ return this.finder ? this.finder.getBy(condition, branch, one, options) : Plugin.need('find');
1074
961
  }
1075
962
  destroy() {
1076
- if (this.__eventIds.length) {
1077
- this.__removeListenEvents();
1078
- this.picker.destroy();
1079
- this.findLeaf = null;
1080
- this.innerIdMap = {};
1081
- this.idMap = {};
1082
- }
963
+ this.picker.destroy();
964
+ if (this.finder)
965
+ this.finder.destroy();
1083
966
  }
1084
967
  }
1085
968
 
@@ -2707,197 +2590,12 @@ const ColorConvertModule = {
2707
2590
  string
2708
2591
  };
2709
2592
 
2710
- const { setPoint, addPoint, toBounds } = TwoPointBoundsHelper;
2711
- function getTrimBounds(canvas) {
2712
- const { width, height } = canvas.view;
2713
- const { data } = canvas.context.getImageData(0, 0, width, height);
2714
- let x, y, pointBounds, index = 0;
2715
- for (let i = 0; i < data.length; i += 4) {
2716
- if (data[i + 3] !== 0) {
2717
- x = index % width;
2718
- y = (index - x) / width;
2719
- pointBounds ? addPoint(pointBounds, x, y) : setPoint(pointBounds = {}, x, y);
2720
- }
2721
- index++;
2722
- }
2723
- const bounds = new Bounds();
2724
- toBounds(pointBounds, bounds);
2725
- return bounds.scale(1 / canvas.pixelRatio).ceil();
2726
- }
2727
-
2728
- const ExportModule = {
2729
- export(leaf, filename, options) {
2730
- this.running = true;
2731
- const fileType = FileHelper.fileType(filename);
2732
- const isDownload = filename.includes('.');
2733
- options = FileHelper.getExportOptions(options);
2734
- return addTask((success) => new Promise((resolve) => {
2735
- const over = (result) => {
2736
- success(result);
2737
- resolve();
2738
- this.running = false;
2739
- };
2740
- const { toURL } = Platform;
2741
- const { download } = Platform.origin;
2742
- if (fileType === 'json') {
2743
- isDownload && download(toURL(JSON.stringify(leaf.toJSON(options.json)), 'text'), filename);
2744
- return over({ data: isDownload ? true : leaf.toJSON(options.json) });
2745
- }
2746
- if (fileType === 'svg') {
2747
- isDownload && download(toURL(leaf.toSVG(), 'svg'), filename);
2748
- return over({ data: isDownload ? true : leaf.toSVG() });
2749
- }
2750
- const { leafer } = leaf;
2751
- if (leafer) {
2752
- checkLazy(leaf);
2753
- leafer.waitViewCompleted(() => __awaiter(this, void 0, void 0, function* () {
2754
- let renderBounds, trimBounds, scaleX = 1, scaleY = 1;
2755
- const { worldTransform, isLeafer, isFrame } = leaf;
2756
- const { slice, trim, onCanvas } = options;
2757
- const smooth = options.smooth === undefined ? leafer.config.smooth : options.smooth;
2758
- const contextSettings = options.contextSettings || leafer.config.contextSettings;
2759
- const screenshot = options.screenshot || leaf.isApp;
2760
- const fill = (isLeafer && screenshot) ? (options.fill === undefined ? leaf.fill : options.fill) : options.fill;
2761
- const needFill = FileHelper.isOpaqueImage(filename) || fill, matrix = new Matrix();
2762
- if (screenshot) {
2763
- renderBounds = screenshot === true ? (isLeafer ? leafer.canvas.bounds : leaf.worldRenderBounds) : screenshot;
2764
- }
2765
- else {
2766
- let relative = options.relative || (isLeafer ? 'inner' : 'local');
2767
- scaleX = worldTransform.scaleX;
2768
- scaleY = worldTransform.scaleY;
2769
- switch (relative) {
2770
- case 'inner':
2771
- matrix.set(worldTransform);
2772
- break;
2773
- case 'local':
2774
- matrix.set(worldTransform).divide(leaf.localTransform);
2775
- scaleX /= leaf.scaleX;
2776
- scaleY /= leaf.scaleY;
2777
- break;
2778
- case 'world':
2779
- scaleX = 1;
2780
- scaleY = 1;
2781
- break;
2782
- case 'page':
2783
- relative = leaf.leafer;
2784
- default:
2785
- matrix.set(worldTransform).divide(leaf.getTransform(relative));
2786
- const l = relative.worldTransform;
2787
- scaleX /= scaleX / l.scaleX;
2788
- scaleY /= scaleY / l.scaleY;
2789
- }
2790
- renderBounds = leaf.getBounds('render', relative);
2791
- }
2792
- const scaleData = { scaleX: 1, scaleY: 1 };
2793
- MathHelper.getScaleData(options.scale, options.size, renderBounds, scaleData);
2794
- let pixelRatio = options.pixelRatio || 1;
2795
- if (leaf.isApp) {
2796
- scaleData.scaleX *= pixelRatio;
2797
- scaleData.scaleY *= pixelRatio;
2798
- pixelRatio = leaf.app.pixelRatio;
2799
- }
2800
- const { x, y, width, height } = new Bounds(renderBounds).scale(scaleData.scaleX, scaleData.scaleY);
2801
- const renderOptions = { matrix: matrix.scale(1 / scaleData.scaleX, 1 / scaleData.scaleY).invert().translate(-x, -y).withScale(1 / scaleX * scaleData.scaleX, 1 / scaleY * scaleData.scaleY) };
2802
- let canvas = Creator.canvas({ width: Math.round(width), height: Math.round(height), pixelRatio, smooth, contextSettings });
2803
- let sliceLeaf;
2804
- if (slice) {
2805
- sliceLeaf = leaf;
2806
- sliceLeaf.__worldOpacity = 0;
2807
- leaf = leafer;
2808
- renderOptions.bounds = canvas.bounds;
2809
- }
2810
- canvas.save();
2811
- if (isFrame && fill !== undefined) {
2812
- const oldFill = leaf.get('fill');
2813
- leaf.fill = '';
2814
- leaf.__render(canvas, renderOptions);
2815
- leaf.fill = oldFill;
2816
- }
2817
- else {
2818
- leaf.__render(canvas, renderOptions);
2819
- }
2820
- canvas.restore();
2821
- if (sliceLeaf)
2822
- sliceLeaf.__updateWorldOpacity();
2823
- if (trim) {
2824
- trimBounds = getTrimBounds(canvas);
2825
- const old = canvas, { width, height } = trimBounds;
2826
- const config = { x: 0, y: 0, width, height, pixelRatio };
2827
- canvas = Creator.canvas(config);
2828
- canvas.copyWorld(old, trimBounds, config);
2829
- }
2830
- if (needFill)
2831
- canvas.fillWorld(canvas.bounds, fill || '#FFFFFF', 'destination-over');
2832
- if (onCanvas)
2833
- onCanvas(canvas);
2834
- const data = filename === 'canvas' ? canvas : yield canvas.export(filename, options);
2835
- over({ data, width: canvas.pixelWidth, height: canvas.pixelHeight, renderBounds, trimBounds });
2836
- }));
2837
- }
2838
- else {
2839
- over({ data: false });
2840
- }
2841
- }));
2842
- }
2843
- };
2844
- let tasker;
2845
- function addTask(task) {
2846
- if (!tasker)
2847
- tasker = new TaskProcessor();
2848
- return new Promise((resolve) => {
2849
- tasker.add(() => __awaiter(this, void 0, void 0, function* () { return yield task(resolve); }), { parallel: false });
2850
- });
2851
- }
2852
- function checkLazy(leaf) {
2853
- if (leaf.__.__needComputePaint)
2854
- leaf.__.__computePaint();
2855
- if (leaf.isBranch)
2856
- leaf.children.forEach(child => checkLazy(child));
2857
- }
2858
-
2859
- const canvas = LeaferCanvasBase.prototype;
2860
- const debug = Debug.get('@leafer-ui/export');
2861
- canvas.export = function (filename, options) {
2862
- const { quality, blob } = FileHelper.getExportOptions(options);
2863
- if (filename.includes('.'))
2864
- return this.saveAs(filename, quality);
2865
- else if (blob)
2866
- return this.toBlob(filename, quality);
2867
- else
2868
- return this.toDataURL(filename, quality);
2869
- };
2870
- canvas.toBlob = function (type, quality) {
2871
- return new Promise((resolve) => {
2872
- Platform.origin.canvasToBolb(this.view, type, quality).then((blob) => {
2873
- resolve(blob);
2874
- }).catch((e) => {
2875
- debug.error(e);
2876
- resolve(null);
2877
- });
2878
- });
2879
- };
2880
- canvas.toDataURL = function (type, quality) {
2881
- return Platform.origin.canvasToDataURL(this.view, type, quality);
2882
- };
2883
- canvas.saveAs = function (filename, quality) {
2884
- return new Promise((resolve) => {
2885
- Platform.origin.canvasSaveAs(this.view, filename, quality).then(() => {
2886
- resolve(true);
2887
- }).catch((e) => {
2888
- debug.error(e);
2889
- resolve(false);
2890
- });
2891
- });
2892
- };
2893
-
2894
2593
  Object.assign(TextConvert, TextConvertModule);
2895
2594
  Object.assign(ColorConvert, ColorConvertModule);
2896
2595
  Object.assign(Paint, PaintModule);
2897
2596
  Object.assign(PaintImage, PaintImageModule);
2898
2597
  Object.assign(PaintGradient, PaintGradientModule);
2899
2598
  Object.assign(Effect, EffectModule);
2900
- Object.assign(Export, ExportModule);
2901
2599
 
2902
2600
  Object.assign(Creator, {
2903
2601
  interaction: (target, canvas, selector, options) => new Interaction(target, canvas, selector, options),
@@ -2937,4 +2635,4 @@ if (platform === 'ios') {
2937
2635
  };
2938
2636
  }
2939
2637
 
2940
- export { Interaction, Layouter, LeaferCanvas, Renderer, Selector, Watcher, useCanvas };
2638
+ export { Interaction, Layouter, LeaferCanvas, Picker, Renderer, Selector, Watcher, useCanvas };