@leafer-ui/miniapp 1.0.0-rc.4 → 1.0.0-rc.6

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,7 +1,7 @@
1
1
  import { LeafList, DataHelper, RenderEvent, ChildEvent, WatchEvent, PropertyEvent, LeafHelper, BranchHelper, Bounds, LeafBoundsHelper, BoundsHelper, Debug, LeafLevelList, LayoutEvent, Run, ImageManager, Platform, AnimateEvent, ResizeEvent, Creator, LeaferCanvasBase, canvasPatch, canvasSizeAttrs, InteractionHelper, InteractionBase, LeaferImage, FileHelper, MatrixHelper, ImageEvent, PointHelper, MathHelper, TaskProcessor } from '@leafer/core';
2
2
  export * from '@leafer/core';
3
3
  export { LeaferImage } from '@leafer/core';
4
- import { ColorConvert as ColorConvert$1, Paint, Effect, TextConvert as TextConvert$1, Export as Export$1, Platform as Platform$1 } from '@leafer-ui/core';
4
+ import { ColorConvert as ColorConvert$1, ImageManager as ImageManager$1, Paint, Effect, TextConvert as TextConvert$1, Export as Export$1, Platform as Platform$1 } from '@leafer-ui/core';
5
5
  export * from '@leafer-ui/core';
6
6
 
7
7
  class Watcher {
@@ -105,7 +105,7 @@ function updateMatrix(updateList, levelList) {
105
105
  let layout;
106
106
  updateList.list.forEach(leaf => {
107
107
  layout = leaf.__layout;
108
- if (levelList.without(leaf) && !layout.useZoomProxy) {
108
+ if (levelList.without(leaf) && !layout.proxyZoom) {
109
109
  if (layout.matrixChanged) {
110
110
  updateAllWorldMatrix$1(leaf);
111
111
  levelList.push(leaf);
@@ -424,8 +424,7 @@ class Renderer {
424
424
  const { canvas, updateBlocks: list } = this;
425
425
  if (!list)
426
426
  return debug.warn('PartRender: need update attr');
427
- if (list.some(block => block.includes(this.target.__world)))
428
- this.mergeBlocks();
427
+ this.mergeBlocks();
429
428
  list.forEach(block => { if (canvas.bounds.hit(block) && !block.isEmpty())
430
429
  this.clipRender(block); });
431
430
  }
@@ -444,7 +443,7 @@ class Renderer {
444
443
  canvas.clearWorld(bounds, true);
445
444
  canvas.clipWorld(bounds, true);
446
445
  }
447
- this.__render(bounds, realBounds);
446
+ this.__render(bounds, includes, realBounds);
448
447
  canvas.restore();
449
448
  Run.end(t);
450
449
  }
@@ -453,12 +452,12 @@ class Renderer {
453
452
  const { canvas } = this;
454
453
  canvas.save();
455
454
  canvas.clear();
456
- this.__render(canvas.bounds);
455
+ this.__render(canvas.bounds, true);
457
456
  canvas.restore();
458
457
  Run.end(t);
459
458
  }
460
- __render(bounds, realBounds) {
461
- const options = (bounds === null || bounds === void 0 ? void 0 : bounds.includes(this.target.__world)) ? {} : { bounds };
459
+ __render(bounds, includes, realBounds) {
460
+ const options = bounds.includes(this.target.__world) ? { includes } : { bounds, includes };
462
461
  if (this.needFill)
463
462
  this.canvas.fillWorld(bounds, this.config.fill);
464
463
  if (Debug.showRepaint)
@@ -559,7 +558,7 @@ class Renderer {
559
558
  }
560
559
 
561
560
  const { hitRadiusPoint } = BoundsHelper;
562
- class FindPath {
561
+ class Pather {
563
562
  constructor(target, selector) {
564
563
  this.target = target;
565
564
  this.selector = selector;
@@ -679,117 +678,108 @@ class FindPath {
679
678
  class Selector {
680
679
  constructor(target, userConfig) {
681
680
  this.config = {};
682
- this.innerIdList = {};
683
- this.idList = {};
684
- this.classNameList = {};
685
- this.tagNameList = {};
681
+ this.innerIdMap = {};
682
+ this.idMap = {};
683
+ this.methods = {
684
+ id: (leaf, name) => leaf.id === name ? this.idMap[name] = leaf : 0,
685
+ innerId: (leaf, innerId) => leaf.innerId === innerId ? this.innerIdMap[innerId] = leaf : 0,
686
+ className: (leaf, name) => leaf.className === name ? 1 : 0,
687
+ tag: (leaf, name) => leaf.__tag === name ? 1 : 0
688
+ };
686
689
  this.target = target;
687
690
  if (userConfig)
688
691
  this.config = DataHelper.default(userConfig, this.config);
689
- this.findPath = new FindPath(target, this);
692
+ this.pather = new Pather(target, this);
690
693
  this.__listenEvents();
691
694
  }
692
695
  getByPoint(hitPoint, hitRadius, options) {
693
696
  if (Platform.name === 'node')
694
697
  this.target.emit(LayoutEvent.CHECK_UPDATE);
695
- return this.findPath.getByPoint(hitPoint, hitRadius, options);
696
- }
697
- find(name, branch) {
698
- if (typeof name === 'number') {
699
- return this.getByInnerId(name, branch);
700
- }
701
- else if (name.startsWith('#')) {
702
- return this.getById(name.substring(1), branch);
703
- }
704
- else if (name.startsWith('.')) {
705
- return this.getByClassName(name.substring(1), branch);
706
- }
707
- else {
708
- return this.getByTagName(name, branch);
698
+ return this.pather.getByPoint(hitPoint, hitRadius, options);
699
+ }
700
+ getBy(condition, branch, one, options) {
701
+ switch (typeof condition) {
702
+ case 'number':
703
+ const leaf = this.getByInnerId(condition, branch);
704
+ return one ? leaf : (leaf ? [leaf] : []);
705
+ case 'string':
706
+ switch (condition[0]) {
707
+ case '#':
708
+ const leaf = this.getById(condition.substring(1), branch);
709
+ return one ? leaf : (leaf ? [leaf] : []);
710
+ case '.':
711
+ return this.getByMethod(this.methods.className, branch, one, condition.substring(1));
712
+ default:
713
+ return this.getByMethod(this.methods.tag, branch, one, condition);
714
+ }
715
+ case 'function':
716
+ return this.getByMethod(condition, branch, one, options);
709
717
  }
710
718
  }
711
- getByInnerId(name, branch) {
712
- let cache = this.innerIdList[name];
719
+ getByInnerId(innerId, branch) {
720
+ const cache = this.innerIdMap[innerId];
713
721
  if (cache)
714
722
  return cache;
715
- if (!branch)
716
- branch = this.target;
717
- let find;
718
- this.loopFind(branch, (leaf) => {
719
- if (leaf.innerId === name) {
720
- find = leaf;
721
- this.innerIdList[name] = find;
722
- return true;
723
- }
724
- else {
725
- return false;
726
- }
727
- });
728
- return find;
723
+ this.eachFind(this.toChildren(branch), this.methods.innerId, null, innerId);
724
+ return this.findLeaf;
729
725
  }
730
- getById(name, branch) {
731
- let cache = this.idList[name];
732
- if (cache)
726
+ getById(id, branch) {
727
+ const cache = this.idMap[id];
728
+ if (cache && LeafHelper.hasParent(cache, branch || this.target))
733
729
  return cache;
734
- if (!branch)
735
- branch = this.target;
736
- let find;
737
- this.loopFind(branch, (leaf) => {
738
- if (leaf.id === name) {
739
- find = leaf;
740
- this.idList[name] = find;
741
- return true;
742
- }
743
- else {
744
- return false;
745
- }
746
- });
747
- return find;
748
- }
749
- getByClassName(name, branch) {
750
- if (!branch)
751
- branch = this.target;
752
- let find = [];
753
- this.loopFind(branch, (leaf) => {
754
- if (leaf.className === name)
755
- find.push(leaf);
756
- return false;
757
- });
758
- return find;
759
- }
760
- getByTagName(name, branch) {
761
- if (!branch)
762
- branch = this.target;
763
- let find = [];
764
- this.loopFind(branch, (leaf) => {
765
- if (leaf.__tag === name)
766
- find.push(leaf);
767
- return false;
768
- });
769
- return find;
730
+ this.eachFind(this.toChildren(branch), this.methods.id, null, id);
731
+ return this.findLeaf;
770
732
  }
771
- loopFind(branch, find) {
772
- if (find(branch))
773
- return;
774
- const { children } = branch;
733
+ getByClassName(className, branch) {
734
+ return this.getByMethod(this.methods.className, branch, false, className);
735
+ }
736
+ getByTag(tag, branch) {
737
+ return this.getByMethod(this.methods.tag, branch, false, tag);
738
+ }
739
+ getByMethod(method, branch, one, options) {
740
+ const list = one ? null : [];
741
+ this.eachFind(this.toChildren(branch), method, list, options);
742
+ return list || this.findLeaf;
743
+ }
744
+ eachFind(children, method, list, options) {
745
+ let child;
775
746
  for (let i = 0, len = children.length; i < len; i++) {
776
- branch = children[i];
777
- if (find(branch))
778
- return;
779
- if (branch.isBranch)
780
- this.loopFind(branch, find);
747
+ child = children[i];
748
+ if (method(child, options)) {
749
+ if (list) {
750
+ list.push(child);
751
+ }
752
+ else {
753
+ this.findLeaf = child;
754
+ return;
755
+ }
756
+ }
757
+ if (child.isBranch)
758
+ this.eachFind(child.children, method, list, options);
781
759
  }
782
760
  }
761
+ toChildren(branch) {
762
+ this.findLeaf = null;
763
+ return [branch || this.target];
764
+ }
783
765
  __onRemoveChild(event) {
784
- const target = event.target;
785
- if (this.idList[target.id])
786
- this.idList[target.id] = null;
787
- if (this.innerIdList[target.id])
788
- this.innerIdList[target.innerId] = null;
766
+ const { id, innerId } = event.child;
767
+ if (this.idMap[id])
768
+ delete this.idMap[id];
769
+ if (this.innerIdMap[innerId])
770
+ delete this.innerIdMap[innerId];
771
+ }
772
+ __checkIdChange(event) {
773
+ if (event.attrName === 'id') {
774
+ const id = event.oldValue;
775
+ if (this.idMap[id])
776
+ delete this.idMap[id];
777
+ }
789
778
  }
790
779
  __listenEvents() {
791
780
  this.__eventIds = [
792
- this.target.on_(ChildEvent.REMOVE, this.__onRemoveChild, this)
781
+ this.target.on_(ChildEvent.REMOVE, this.__onRemoveChild, this),
782
+ this.target.on_(PropertyEvent.CHANGE, this.__checkIdChange, this)
793
783
  ];
794
784
  }
795
785
  __removeListenEvents() {
@@ -799,11 +789,10 @@ class Selector {
799
789
  destroy() {
800
790
  if (this.__eventIds.length) {
801
791
  this.__removeListenEvents();
802
- this.findPath.destroy();
803
- this.innerIdList = {};
804
- this.idList = {};
805
- this.classNameList = {};
806
- this.tagNameList = {};
792
+ this.pather.destroy();
793
+ this.findLeaf = null;
794
+ this.innerIdMap = {};
795
+ this.idMap = {};
807
796
  }
808
797
  }
809
798
  }
@@ -1118,6 +1107,11 @@ function useCanvas(_canvasType, app) {
1118
1107
  app.offWindowResize(fun);
1119
1108
  }
1120
1109
  };
1110
+ Platform.event = {
1111
+ stopDefault(_origin) { },
1112
+ stopNow(_origin) { },
1113
+ stop(_origin) { }
1114
+ };
1121
1115
  Platform.canvas = Creator.canvas();
1122
1116
  Platform.conicGradientSupport = !!Platform.canvas.context.createConicGradient;
1123
1117
  }
@@ -1403,10 +1397,14 @@ function checkImage(ui, canvas, paint, allowPaint) {
1403
1397
  createPattern(ui, paint, canvas.pixelRatio);
1404
1398
  }
1405
1399
  else {
1406
- ImageManager.patternTasker.add(() => __awaiter(this, void 0, void 0, function* () {
1407
- if (canvas.bounds.hit(ui.__world) && createPattern(ui, paint, canvas.pixelRatio))
1400
+ if (!paint.patternTask) {
1401
+ paint.patternTask = ImageManager.patternTasker.add(() => __awaiter(this, void 0, void 0, function* () {
1402
+ paint.patternTask = null;
1403
+ if (canvas.bounds.hit(ui.__world))
1404
+ createPattern(ui, paint, canvas.pixelRatio);
1408
1405
  ui.forceUpdate('surface');
1409
- }), 300);
1406
+ }), 300);
1407
+ }
1410
1408
  }
1411
1409
  return false;
1412
1410
  }
@@ -1770,17 +1768,30 @@ function conicGradient(paint, box) {
1770
1768
  let recycleMap;
1771
1769
  function compute(attrName, ui) {
1772
1770
  const value = [];
1771
+ const data = ui.__;
1773
1772
  let item;
1774
- let paints = ui.__.__input[attrName];
1773
+ let paints = data.__input[attrName];
1775
1774
  if (!(paints instanceof Array))
1776
1775
  paints = [paints];
1777
- recycleMap = recycleImage(attrName, ui.__);
1776
+ recycleMap = recycleImage(attrName, data);
1778
1777
  for (let i = 0, len = paints.length; i < len; i++) {
1779
1778
  item = getLeafPaint(attrName, paints[i], ui);
1780
1779
  if (item)
1781
1780
  value.push(item);
1782
1781
  }
1783
- ui.__['_' + attrName] = value.length ? value : undefined;
1782
+ data['_' + attrName] = value.length ? value : undefined;
1783
+ let isPixel;
1784
+ if (paints.length === 1) {
1785
+ const paint = paints[0];
1786
+ if (paint.type === 'image')
1787
+ isPixel = ImageManager$1.isPixel(paint);
1788
+ }
1789
+ if (attrName === 'fill') {
1790
+ data.__pixelFill = isPixel;
1791
+ }
1792
+ else {
1793
+ data.__pixelStroke = isPixel;
1794
+ }
1784
1795
  }
1785
1796
  function getLeafPaint(attrName, paint, ui) {
1786
1797
  if (typeof paint !== 'object' || paint.visible === false || paint.opacity === 0)
@@ -2055,6 +2066,8 @@ function createRows(drawData, content, style) {
2055
2066
  const { width, height } = bounds;
2056
2067
  const charMode = width || height || __letterSpacing || (textCase !== 'none');
2057
2068
  if (charMode) {
2069
+ const wrap = style.textWrap !== 'none';
2070
+ const breakAll = style.textWrap === 'break';
2058
2071
  paraStart = true;
2059
2072
  lastCharType = null;
2060
2073
  startCharSize = charWidth = charSize = wordWidth = rowWidth = 0;
@@ -2081,16 +2094,23 @@ function createRows(drawData, content, style) {
2081
2094
  langBreak = (charType === Single && (lastCharType === Single || lastCharType === Letter)) || (lastCharType === Single && charType !== After);
2082
2095
  afterBreak = ((charType === Before || charType === Single) && (lastCharType === Symbol || lastCharType === After));
2083
2096
  realWidth = paraStart && paraIndent ? width - paraIndent : width;
2084
- if (width && rowWidth + wordWidth + charWidth > realWidth) {
2085
- if (!afterBreak)
2086
- afterBreak = charType === Letter && lastCharType == After;
2087
- if (langBreak || afterBreak || charType === Break || charType === Before || charType === Single || (wordWidth + charWidth > realWidth)) {
2097
+ if (wrap && (width && rowWidth + wordWidth + charWidth > realWidth)) {
2098
+ if (breakAll) {
2088
2099
  if (wordWidth)
2089
2100
  addWord();
2090
2101
  addRow();
2091
2102
  }
2092
2103
  else {
2093
- addRow();
2104
+ if (!afterBreak)
2105
+ afterBreak = charType === Letter && lastCharType == After;
2106
+ if (langBreak || afterBreak || charType === Break || charType === Before || charType === Single || (wordWidth + charWidth > realWidth)) {
2107
+ if (wordWidth)
2108
+ addWord();
2109
+ addRow();
2110
+ }
2111
+ else {
2112
+ addRow();
2113
+ }
2094
2114
  }
2095
2115
  }
2096
2116
  if (char === ' ' && paraStart !== true && (rowWidth + wordWidth) === 0) ;
@@ -2160,7 +2180,7 @@ function addRow() {
2160
2180
 
2161
2181
  const CharMode = 0;
2162
2182
  const WordMode = 1;
2163
- const RowMode = 2;
2183
+ const TextMode = 2;
2164
2184
  function layoutChar(drawData, style, width, _height) {
2165
2185
  const { rows } = drawData;
2166
2186
  const { textAlign, paraIndent, letterSpacing } = style;
@@ -2169,15 +2189,12 @@ function layoutChar(drawData, style, width, _height) {
2169
2189
  if (row.words) {
2170
2190
  indentWidth = paraIndent && row.paraStart ? paraIndent : 0;
2171
2191
  addWordWidth = (width && textAlign === 'justify' && row.words.length > 1) ? (width - row.width - indentWidth) / (row.words.length - 1) : 0;
2172
- mode = (letterSpacing || row.isOverflow) ? CharMode : (addWordWidth > 0.01 ? WordMode : RowMode);
2173
- if (mode === RowMode) {
2174
- row.text = '';
2192
+ mode = (letterSpacing || row.isOverflow) ? CharMode : (addWordWidth > 0.01 ? WordMode : TextMode);
2193
+ if (row.isOverflow && !letterSpacing)
2194
+ row.textMode = true;
2195
+ if (mode === TextMode) {
2175
2196
  row.x += indentWidth;
2176
- row.words.forEach(word => {
2177
- word.data.forEach(char => {
2178
- row.text += char.char;
2179
- });
2180
- });
2197
+ toTextChar$1(row);
2181
2198
  }
2182
2199
  else {
2183
2200
  row.x += indentWidth;
@@ -2203,6 +2220,14 @@ function layoutChar(drawData, style, width, _height) {
2203
2220
  }
2204
2221
  });
2205
2222
  }
2223
+ function toTextChar$1(row) {
2224
+ row.text = '';
2225
+ row.words.forEach(word => {
2226
+ word.data.forEach(char => {
2227
+ row.text += char.char;
2228
+ });
2229
+ });
2230
+ }
2206
2231
  function toWordChar(data, charX, wordChar) {
2207
2232
  data.forEach(char => {
2208
2233
  wordChar.char += char.char;
@@ -2223,10 +2248,10 @@ function toChar(data, charX, rowData) {
2223
2248
 
2224
2249
  function layoutText(drawData, style) {
2225
2250
  const { rows, bounds } = drawData;
2226
- const { __lineHeight, __baseLine, __letterSpacing, textAlign, verticalAlign, paraSpacing, textOverflow } = style;
2251
+ const { __lineHeight, __baseLine, __letterSpacing, __clipText, textAlign, verticalAlign, paraSpacing } = style;
2227
2252
  let { x, y, width, height } = bounds, realHeight = __lineHeight * rows.length + (paraSpacing ? paraSpacing * (drawData.paraNumber - 1) : 0);
2228
2253
  let starY = __baseLine;
2229
- if (textOverflow !== 'show' && realHeight > height) {
2254
+ if (__clipText && realHeight > height) {
2230
2255
  realHeight = Math.max(height, __lineHeight);
2231
2256
  drawData.overflow = rows.length;
2232
2257
  }
@@ -2275,39 +2300,58 @@ function layoutText(drawData, style) {
2275
2300
  bounds.x = rowX;
2276
2301
  if (rowWidth > bounds.width)
2277
2302
  bounds.width = rowWidth;
2303
+ if (__clipText && width && width < rowWidth) {
2304
+ row.isOverflow = true;
2305
+ if (!drawData.overflow)
2306
+ drawData.overflow = rows.length;
2307
+ }
2278
2308
  }
2279
2309
  bounds.y = y;
2280
2310
  bounds.height = realHeight;
2281
2311
  }
2282
2312
 
2283
- function clipText(drawData, textOverflow) {
2313
+ function clipText(drawData, style) {
2284
2314
  const { rows, overflow } = drawData;
2315
+ let { textOverflow } = style;
2285
2316
  rows.splice(overflow);
2286
2317
  if (textOverflow !== 'hide') {
2287
2318
  if (textOverflow === 'ellipsis')
2288
2319
  textOverflow = '...';
2320
+ let char, charRight;
2289
2321
  const ellipsisWidth = Platform.canvas.measureText(textOverflow).width;
2290
- const row = rows[overflow - 1];
2291
- let char, end = row.data.length - 1, charRight;
2292
- const { x, width } = drawData.bounds;
2293
- const right = x + width - ellipsisWidth;
2294
- for (let i = end; i > -1; i--) {
2295
- char = row.data[i];
2296
- charRight = char.x + char.width;
2297
- if (i === end && charRight < right) {
2298
- break;
2299
- }
2300
- else if (charRight < right && char.char !== ' ') {
2301
- row.data.splice(i + 1);
2302
- row.width -= char.width;
2303
- break;
2322
+ const right = style.x + style.width - ellipsisWidth;
2323
+ const list = style.textWrap === 'none' ? rows : [rows[overflow - 1]];
2324
+ list.forEach(row => {
2325
+ if (row.isOverflow && row.data) {
2326
+ let end = row.data.length - 1;
2327
+ for (let i = end; i > -1; i--) {
2328
+ char = row.data[i];
2329
+ charRight = char.x + char.width;
2330
+ if (i === end && charRight < right) {
2331
+ break;
2332
+ }
2333
+ else if (charRight < right && char.char !== ' ') {
2334
+ row.data.splice(i + 1);
2335
+ row.width -= char.width;
2336
+ break;
2337
+ }
2338
+ row.width -= char.width;
2339
+ }
2340
+ row.width += ellipsisWidth;
2341
+ row.data.push({ char: textOverflow, x: charRight });
2342
+ if (row.textMode)
2343
+ toTextChar(row);
2304
2344
  }
2305
- row.width -= char.width;
2306
- }
2307
- row.width += ellipsisWidth;
2308
- row.data.push({ char: textOverflow, x: charRight });
2345
+ });
2309
2346
  }
2310
2347
  }
2348
+ function toTextChar(row) {
2349
+ row.text = '';
2350
+ row.data.forEach(char => {
2351
+ row.text += char.char;
2352
+ });
2353
+ row.data = null;
2354
+ }
2311
2355
 
2312
2356
  function decorationText(drawData, style) {
2313
2357
  const { fontSize } = style;
@@ -2328,7 +2372,7 @@ const TextConvert = {
2328
2372
  let x = 0, y = 0;
2329
2373
  let width = style.__getInput('width') || 0;
2330
2374
  let height = style.__getInput('height') || 0;
2331
- const { textDecoration, textOverflow, __font, padding } = style;
2375
+ const { textDecoration, __font, padding } = style;
2332
2376
  if (padding) {
2333
2377
  const [top, right, bottom, left] = MathHelper.fourNumber(padding);
2334
2378
  if (width) {
@@ -2350,7 +2394,7 @@ const TextConvert = {
2350
2394
  layoutText(drawData, style);
2351
2395
  layoutChar(drawData, style, width);
2352
2396
  if (drawData.overflow)
2353
- clipText(drawData, textOverflow);
2397
+ clipText(drawData, style);
2354
2398
  if (textDecoration !== 'none')
2355
2399
  decorationText(drawData, style);
2356
2400
  return drawData;