leafer-ui 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/web.esm.js CHANGED
@@ -1,11 +1,11 @@
1
- import { Debug, LeaferCanvasBase, Platform, DataHelper, canvasSizeAttrs, ResizeEvent, canvasPatch, Creator, LeaferImage, defineKey, FileHelper, LeafList, RenderEvent, ChildEvent, WatchEvent, PropertyEvent, LeafHelper, BranchHelper, Bounds, LeafBoundsHelper, LeafLevelList, LayoutEvent, Run, ImageManager, BoundsHelper, Answer, MathHelper, MatrixHelper, AlignHelper, ImageEvent, AroundHelper, PointHelper, Direction4, TwoPointBoundsHelper, TaskProcessor, Matrix } from '@leafer/core';
1
+ import { Debug, LeaferCanvasBase, Platform, DataHelper, canvasSizeAttrs, ResizeEvent, canvasPatch, Creator, LeaferImage, defineKey, FileHelper, LeafList, RenderEvent, ChildEvent, WatchEvent, PropertyEvent, LeafHelper, BranchHelper, Bounds, LeafBoundsHelper, LeafLevelList, LayoutEvent, Run, ImageManager, BoundsHelper, Plugin, MathHelper, MatrixHelper, 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, Cursor, HitCanvasManager } from '@leafer-ui/core';
5
5
  export * from '@leafer-ui/core';
6
6
  import { PaintImage, ColorConvert, PaintGradient, Export, Group, TextConvert, Paint, Effect } from '@leafer-ui/draw';
7
7
 
8
- const debug$3 = Debug.get('LeaferCanvas');
8
+ const debug$2 = Debug.get('LeaferCanvas');
9
9
  class LeaferCanvas extends LeaferCanvasBase {
10
10
  set zIndex(zIndex) {
11
11
  const { style } = this.view;
@@ -77,7 +77,7 @@ class LeaferCanvas extends LeaferCanvasBase {
77
77
  }
78
78
  }
79
79
  else {
80
- debug$3.error(`no id: ${inputView}`);
80
+ debug$2.error(`no id: ${inputView}`);
81
81
  this.__createView();
82
82
  }
83
83
  }
@@ -115,7 +115,7 @@ class LeaferCanvas extends LeaferCanvasBase {
115
115
  }
116
116
  else {
117
117
  this.checkAutoBounds(this.view);
118
- debug$3.warn('no parent');
118
+ debug$2.warn('no parent');
119
119
  }
120
120
  }
121
121
  catch (_a) {
@@ -451,7 +451,7 @@ class LayoutBlockData {
451
451
  }
452
452
 
453
453
  const { updateAllMatrix, updateAllChange } = LeafHelper;
454
- const debug$2 = Debug.get('Layouter');
454
+ const debug$1 = Debug.get('Layouter');
455
455
  class Layouter {
456
456
  constructor(target, userConfig) {
457
457
  this.totalTimes = 0;
@@ -486,7 +486,7 @@ class Layouter {
486
486
  target.emitEvent(new LayoutEvent(LayoutEvent.END, this.layoutedBlocks, this.times));
487
487
  }
488
488
  catch (e) {
489
- debug$2.error(e);
489
+ debug$1.error(e);
490
490
  }
491
491
  this.layoutedBlocks = null;
492
492
  }
@@ -500,9 +500,9 @@ class Layouter {
500
500
  }
501
501
  layoutOnce() {
502
502
  if (this.layouting)
503
- return debug$2.warn('layouting');
503
+ return debug$1.warn('layouting');
504
504
  if (this.times > 3)
505
- return debug$2.warn('layout max times');
505
+ return debug$1.warn('layout max times');
506
506
  this.times++;
507
507
  this.totalTimes++;
508
508
  this.layouting = true;
@@ -606,7 +606,7 @@ class Layouter {
606
606
  }
607
607
  }
608
608
 
609
- const debug$1 = Debug.get('Renderer');
609
+ const debug = Debug.get('Renderer');
610
610
  class Renderer {
611
611
  get needFill() { return !!(!this.canvas.allowBackgroundColor && this.config.fill); }
612
612
  constructor(target, canvas, userConfig) {
@@ -644,7 +644,7 @@ class Renderer {
644
644
  const { target } = this;
645
645
  this.times = 0;
646
646
  this.totalBounds = new Bounds();
647
- debug$1.log(target.innerName, '--->');
647
+ debug.log(target.innerName, '--->');
648
648
  try {
649
649
  if (!target.isApp)
650
650
  target.app.emit(RenderEvent.CHILD_START, target);
@@ -655,9 +655,9 @@ class Renderer {
655
655
  }
656
656
  catch (e) {
657
657
  this.rendering = false;
658
- debug$1.error(e);
658
+ debug.error(e);
659
659
  }
660
- debug$1.log('-------------|');
660
+ debug.log('-------------|');
661
661
  }
662
662
  renderAgain() {
663
663
  if (this.rendering) {
@@ -669,9 +669,9 @@ class Renderer {
669
669
  }
670
670
  renderOnce(callback) {
671
671
  if (this.rendering)
672
- return debug$1.warn('rendering');
672
+ return debug.warn('rendering');
673
673
  if (this.times > 3)
674
- return debug$1.warn('render max times');
674
+ return debug.warn('render max times');
675
675
  this.times++;
676
676
  this.totalTimes++;
677
677
  this.rendering = true;
@@ -708,7 +708,7 @@ class Renderer {
708
708
  partRender() {
709
709
  const { canvas, updateBlocks: list } = this;
710
710
  if (!list)
711
- return debug$1.warn('PartRender: need update attr');
711
+ return debug.warn('PartRender: need update attr');
712
712
  this.mergeBlocks();
713
713
  list.forEach(block => { if (canvas.bounds.hit(block) && !block.isEmpty())
714
714
  this.clipRender(block); });
@@ -811,7 +811,7 @@ class Renderer {
811
811
  empty = (!leaf.__world.width || !leaf.__world.height);
812
812
  if (empty) {
813
813
  if (!leaf.isLeafer)
814
- debug$1.tip(leaf.innerName, ': empty');
814
+ debug.tip(leaf.innerName, ': empty');
815
815
  empty = (!leaf.isBranch || leaf.isBranchLeaf);
816
816
  }
817
817
  return empty;
@@ -977,143 +977,26 @@ class Picker {
977
977
  }
978
978
  }
979
979
 
980
- const { Yes, NoAndSkip, YesAndSkip } = Answer;
981
- const idCondition = {}, classNameCondition = {}, tagCondition = {};
982
980
  class Selector {
983
981
  constructor(target, userConfig) {
984
982
  this.config = {};
985
- this.innerIdMap = {};
986
- this.idMap = {};
987
- this.methods = {
988
- id: (leaf, name) => leaf.id === name ? (this.target && (this.idMap[name] = leaf), 1) : 0,
989
- innerId: (leaf, innerId) => leaf.innerId === innerId ? (this.target && (this.innerIdMap[innerId] = leaf), 1) : 0,
990
- className: (leaf, name) => leaf.className === name ? 1 : 0,
991
- tag: (leaf, name) => leaf.__tag === name ? 1 : 0,
992
- tags: (leaf, nameMap) => nameMap[leaf.__tag] ? 1 : 0
993
- };
994
- this.target = target;
995
983
  if (userConfig)
996
984
  this.config = DataHelper.default(userConfig, this.config);
997
- this.picker = new Picker(target, this);
998
- if (target)
999
- this.__listenEvents();
1000
- }
1001
- getBy(condition, branch, one, options) {
1002
- switch (typeof condition) {
1003
- case 'number':
1004
- const leaf = this.getByInnerId(condition, branch);
1005
- return one ? leaf : (leaf ? [leaf] : []);
1006
- case 'string':
1007
- switch (condition[0]) {
1008
- case '#':
1009
- idCondition.id = condition.substring(1), condition = idCondition;
1010
- break;
1011
- case '.':
1012
- classNameCondition.className = condition.substring(1), condition = classNameCondition;
1013
- break;
1014
- default:
1015
- tagCondition.tag = condition, condition = tagCondition;
1016
- }
1017
- case 'object':
1018
- if (condition.id !== undefined) {
1019
- const leaf = this.getById(condition.id, branch);
1020
- return one ? leaf : (leaf ? [leaf] : []);
1021
- }
1022
- else if (condition.tag) {
1023
- const { tag } = condition, isArray = tag instanceof Array;
1024
- return this.getByMethod(isArray ? this.methods.tags : this.methods.tag, branch, one, isArray ? DataHelper.toMap(tag) : tag);
1025
- }
1026
- else {
1027
- return this.getByMethod(this.methods.className, branch, one, condition.className);
1028
- }
1029
- case 'function':
1030
- return this.getByMethod(condition, branch, one, options);
1031
- }
985
+ this.picker = new Picker(this.target = target, this);
986
+ this.finder = Creator.finder && Creator.finder();
1032
987
  }
1033
988
  getByPoint(hitPoint, hitRadius, options) {
1034
- if (Platform.name === 'node' && this.target)
1035
- this.target.emit(LayoutEvent.CHECK_UPDATE);
989
+ if (Platform.backgrounder && this.target)
990
+ this.target.updateLayout();
1036
991
  return this.picker.getByPoint(hitPoint, hitRadius, options);
1037
992
  }
1038
- getByInnerId(innerId, branch) {
1039
- const cache = this.innerIdMap[innerId];
1040
- if (cache)
1041
- return cache;
1042
- this.eachFind(this.toChildren(branch), this.methods.innerId, null, innerId);
1043
- return this.findLeaf;
1044
- }
1045
- getById(id, branch) {
1046
- const cache = this.idMap[id];
1047
- if (cache && LeafHelper.hasParent(cache, branch || this.target))
1048
- return cache;
1049
- this.eachFind(this.toChildren(branch), this.methods.id, null, id);
1050
- return this.findLeaf;
1051
- }
1052
- getByClassName(className, branch) {
1053
- return this.getByMethod(this.methods.className, branch, false, className);
1054
- }
1055
- getByTag(tag, branch) {
1056
- return this.getByMethod(this.methods.tag, branch, false, tag);
1057
- }
1058
- getByMethod(method, branch, one, options) {
1059
- const list = one ? null : [];
1060
- this.eachFind(this.toChildren(branch), method, list, options);
1061
- return list || this.findLeaf;
1062
- }
1063
- eachFind(children, method, list, options) {
1064
- let child, result;
1065
- for (let i = 0, len = children.length; i < len; i++) {
1066
- child = children[i];
1067
- result = method(child, options);
1068
- if (result === Yes || result === YesAndSkip) {
1069
- if (list) {
1070
- list.push(child);
1071
- }
1072
- else {
1073
- this.findLeaf = child;
1074
- return;
1075
- }
1076
- }
1077
- if (child.isBranch && result < NoAndSkip)
1078
- this.eachFind(child.children, method, list, options);
1079
- }
1080
- }
1081
- toChildren(branch) {
1082
- this.findLeaf = null;
1083
- return [branch || this.target];
1084
- }
1085
- __onRemoveChild(event) {
1086
- const { id, innerId } = event.child;
1087
- if (this.idMap[id])
1088
- delete this.idMap[id];
1089
- if (this.innerIdMap[innerId])
1090
- delete this.innerIdMap[innerId];
1091
- }
1092
- __checkIdChange(event) {
1093
- if (event.attrName === 'id') {
1094
- const id = event.oldValue;
1095
- if (this.idMap[id])
1096
- delete this.idMap[id];
1097
- }
1098
- }
1099
- __listenEvents() {
1100
- this.__eventIds = [
1101
- this.target.on_(ChildEvent.REMOVE, this.__onRemoveChild, this),
1102
- this.target.on_(PropertyEvent.CHANGE, this.__checkIdChange, this)
1103
- ];
1104
- }
1105
- __removeListenEvents() {
1106
- this.target.off_(this.__eventIds);
1107
- this.__eventIds.length = 0;
993
+ getBy(condition, branch, one, options) {
994
+ return this.finder ? this.finder.getBy(condition, branch, one, options) : Plugin.need('find');
1108
995
  }
1109
996
  destroy() {
1110
- if (this.__eventIds.length) {
1111
- this.__removeListenEvents();
1112
- this.picker.destroy();
1113
- this.findLeaf = null;
1114
- this.innerIdMap = {};
1115
- this.idMap = {};
1116
- }
997
+ this.picker.destroy();
998
+ if (this.finder)
999
+ this.finder.destroy();
1117
1000
  }
1118
1001
  }
1119
1002
 
@@ -2940,197 +2823,12 @@ const ColorConvertModule = {
2940
2823
  string
2941
2824
  };
2942
2825
 
2943
- const { setPoint, addPoint, toBounds } = TwoPointBoundsHelper;
2944
- function getTrimBounds(canvas) {
2945
- const { width, height } = canvas.view;
2946
- const { data } = canvas.context.getImageData(0, 0, width, height);
2947
- let x, y, pointBounds, index = 0;
2948
- for (let i = 0; i < data.length; i += 4) {
2949
- if (data[i + 3] !== 0) {
2950
- x = index % width;
2951
- y = (index - x) / width;
2952
- pointBounds ? addPoint(pointBounds, x, y) : setPoint(pointBounds = {}, x, y);
2953
- }
2954
- index++;
2955
- }
2956
- const bounds = new Bounds();
2957
- toBounds(pointBounds, bounds);
2958
- return bounds.scale(1 / canvas.pixelRatio).ceil();
2959
- }
2960
-
2961
- const ExportModule = {
2962
- export(leaf, filename, options) {
2963
- this.running = true;
2964
- const fileType = FileHelper.fileType(filename);
2965
- const isDownload = filename.includes('.');
2966
- options = FileHelper.getExportOptions(options);
2967
- return addTask((success) => new Promise((resolve) => {
2968
- const over = (result) => {
2969
- success(result);
2970
- resolve();
2971
- this.running = false;
2972
- };
2973
- const { toURL } = Platform;
2974
- const { download } = Platform.origin;
2975
- if (fileType === 'json') {
2976
- isDownload && download(toURL(JSON.stringify(leaf.toJSON(options.json)), 'text'), filename);
2977
- return over({ data: isDownload ? true : leaf.toJSON(options.json) });
2978
- }
2979
- if (fileType === 'svg') {
2980
- isDownload && download(toURL(leaf.toSVG(), 'svg'), filename);
2981
- return over({ data: isDownload ? true : leaf.toSVG() });
2982
- }
2983
- const { leafer } = leaf;
2984
- if (leafer) {
2985
- checkLazy(leaf);
2986
- leafer.waitViewCompleted(() => __awaiter(this, void 0, void 0, function* () {
2987
- let renderBounds, trimBounds, scaleX = 1, scaleY = 1;
2988
- const { worldTransform, isLeafer, isFrame } = leaf;
2989
- const { slice, trim, onCanvas } = options;
2990
- const smooth = options.smooth === undefined ? leafer.config.smooth : options.smooth;
2991
- const contextSettings = options.contextSettings || leafer.config.contextSettings;
2992
- const screenshot = options.screenshot || leaf.isApp;
2993
- const fill = (isLeafer && screenshot) ? (options.fill === undefined ? leaf.fill : options.fill) : options.fill;
2994
- const needFill = FileHelper.isOpaqueImage(filename) || fill, matrix = new Matrix();
2995
- if (screenshot) {
2996
- renderBounds = screenshot === true ? (isLeafer ? leafer.canvas.bounds : leaf.worldRenderBounds) : screenshot;
2997
- }
2998
- else {
2999
- let relative = options.relative || (isLeafer ? 'inner' : 'local');
3000
- scaleX = worldTransform.scaleX;
3001
- scaleY = worldTransform.scaleY;
3002
- switch (relative) {
3003
- case 'inner':
3004
- matrix.set(worldTransform);
3005
- break;
3006
- case 'local':
3007
- matrix.set(worldTransform).divide(leaf.localTransform);
3008
- scaleX /= leaf.scaleX;
3009
- scaleY /= leaf.scaleY;
3010
- break;
3011
- case 'world':
3012
- scaleX = 1;
3013
- scaleY = 1;
3014
- break;
3015
- case 'page':
3016
- relative = leaf.leafer;
3017
- default:
3018
- matrix.set(worldTransform).divide(leaf.getTransform(relative));
3019
- const l = relative.worldTransform;
3020
- scaleX /= scaleX / l.scaleX;
3021
- scaleY /= scaleY / l.scaleY;
3022
- }
3023
- renderBounds = leaf.getBounds('render', relative);
3024
- }
3025
- const scaleData = { scaleX: 1, scaleY: 1 };
3026
- MathHelper.getScaleData(options.scale, options.size, renderBounds, scaleData);
3027
- let pixelRatio = options.pixelRatio || 1;
3028
- if (leaf.isApp) {
3029
- scaleData.scaleX *= pixelRatio;
3030
- scaleData.scaleY *= pixelRatio;
3031
- pixelRatio = leaf.app.pixelRatio;
3032
- }
3033
- const { x, y, width, height } = new Bounds(renderBounds).scale(scaleData.scaleX, scaleData.scaleY);
3034
- const renderOptions = { matrix: matrix.scale(1 / scaleData.scaleX, 1 / scaleData.scaleY).invert().translate(-x, -y).withScale(1 / scaleX * scaleData.scaleX, 1 / scaleY * scaleData.scaleY) };
3035
- let canvas = Creator.canvas({ width: Math.round(width), height: Math.round(height), pixelRatio, smooth, contextSettings });
3036
- let sliceLeaf;
3037
- if (slice) {
3038
- sliceLeaf = leaf;
3039
- sliceLeaf.__worldOpacity = 0;
3040
- leaf = leafer;
3041
- renderOptions.bounds = canvas.bounds;
3042
- }
3043
- canvas.save();
3044
- if (isFrame && fill !== undefined) {
3045
- const oldFill = leaf.get('fill');
3046
- leaf.fill = '';
3047
- leaf.__render(canvas, renderOptions);
3048
- leaf.fill = oldFill;
3049
- }
3050
- else {
3051
- leaf.__render(canvas, renderOptions);
3052
- }
3053
- canvas.restore();
3054
- if (sliceLeaf)
3055
- sliceLeaf.__updateWorldOpacity();
3056
- if (trim) {
3057
- trimBounds = getTrimBounds(canvas);
3058
- const old = canvas, { width, height } = trimBounds;
3059
- const config = { x: 0, y: 0, width, height, pixelRatio };
3060
- canvas = Creator.canvas(config);
3061
- canvas.copyWorld(old, trimBounds, config);
3062
- }
3063
- if (needFill)
3064
- canvas.fillWorld(canvas.bounds, fill || '#FFFFFF', 'destination-over');
3065
- if (onCanvas)
3066
- onCanvas(canvas);
3067
- const data = filename === 'canvas' ? canvas : yield canvas.export(filename, options);
3068
- over({ data, width: canvas.pixelWidth, height: canvas.pixelHeight, renderBounds, trimBounds });
3069
- }));
3070
- }
3071
- else {
3072
- over({ data: false });
3073
- }
3074
- }));
3075
- }
3076
- };
3077
- let tasker;
3078
- function addTask(task) {
3079
- if (!tasker)
3080
- tasker = new TaskProcessor();
3081
- return new Promise((resolve) => {
3082
- tasker.add(() => __awaiter(this, void 0, void 0, function* () { return yield task(resolve); }), { parallel: false });
3083
- });
3084
- }
3085
- function checkLazy(leaf) {
3086
- if (leaf.__.__needComputePaint)
3087
- leaf.__.__computePaint();
3088
- if (leaf.isBranch)
3089
- leaf.children.forEach(child => checkLazy(child));
3090
- }
3091
-
3092
- const canvas = LeaferCanvasBase.prototype;
3093
- const debug = Debug.get('@leafer-ui/export');
3094
- canvas.export = function (filename, options) {
3095
- const { quality, blob } = FileHelper.getExportOptions(options);
3096
- if (filename.includes('.'))
3097
- return this.saveAs(filename, quality);
3098
- else if (blob)
3099
- return this.toBlob(filename, quality);
3100
- else
3101
- return this.toDataURL(filename, quality);
3102
- };
3103
- canvas.toBlob = function (type, quality) {
3104
- return new Promise((resolve) => {
3105
- Platform.origin.canvasToBolb(this.view, type, quality).then((blob) => {
3106
- resolve(blob);
3107
- }).catch((e) => {
3108
- debug.error(e);
3109
- resolve(null);
3110
- });
3111
- });
3112
- };
3113
- canvas.toDataURL = function (type, quality) {
3114
- return Platform.origin.canvasToDataURL(this.view, type, quality);
3115
- };
3116
- canvas.saveAs = function (filename, quality) {
3117
- return new Promise((resolve) => {
3118
- Platform.origin.canvasSaveAs(this.view, filename, quality).then(() => {
3119
- resolve(true);
3120
- }).catch((e) => {
3121
- debug.error(e);
3122
- resolve(false);
3123
- });
3124
- });
3125
- };
3126
-
3127
2826
  Object.assign(TextConvert, TextConvertModule);
3128
2827
  Object.assign(ColorConvert, ColorConvertModule);
3129
2828
  Object.assign(Paint, PaintModule);
3130
2829
  Object.assign(PaintImage, PaintImageModule);
3131
2830
  Object.assign(PaintGradient, PaintGradientModule);
3132
2831
  Object.assign(Effect, EffectModule);
3133
- Object.assign(Export, ExportModule);
3134
2832
 
3135
2833
  Object.assign(Creator, {
3136
2834
  interaction: (target, canvas, selector, options) => new Interaction(target, canvas, selector, options),
@@ -3139,4 +2837,4 @@ Object.assign(Creator, {
3139
2837
  });
3140
2838
  useCanvas();
3141
2839
 
3142
- export { Interaction, Layouter, LeaferCanvas, Renderer, Selector, Watcher, useCanvas };
2840
+ export { Interaction, Layouter, LeaferCanvas, Picker, Renderer, Selector, Watcher, useCanvas };