@leafer-editor/worker 1.0.9 → 1.0.10

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.js CHANGED
@@ -1414,14 +1414,13 @@ var LeaferUI = (function (exports) {
1414
1414
  list: {},
1415
1415
  register(UI) {
1416
1416
  const { __tag: tag } = UI.prototype;
1417
- if (list$3[tag]) {
1417
+ if (list$3[tag])
1418
1418
  debug$f.repeat(tag);
1419
- }
1420
- else {
1421
- list$3[tag] = UI;
1422
- }
1419
+ list$3[tag] = UI;
1423
1420
  },
1424
1421
  get(tag, data, x, y, width, height) {
1422
+ if (!list$3[tag])
1423
+ debug$f.error('not register ' + tag);
1425
1424
  const ui = new list$3[tag](data);
1426
1425
  if (x !== undefined) {
1427
1426
  ui.x = x;
@@ -1445,7 +1444,7 @@ var LeaferUI = (function (exports) {
1445
1444
  Object.keys(Event).forEach(key => {
1446
1445
  name = Event[key];
1447
1446
  if (typeof name === 'string')
1448
- nameList[name] ? debug$e.repeat(name) : nameList[name] = Event;
1447
+ nameList[name] && debug$e.repeat(name), nameList[name] = Event;
1449
1448
  });
1450
1449
  },
1451
1450
  changeName(oldName, newName) {
@@ -1647,7 +1646,7 @@ var LeaferUI = (function (exports) {
1647
1646
  const t = this;
1648
1647
  if (t.blendMode === 'pass-through') {
1649
1648
  const leaf = this.__leaf;
1650
- if ((t.opacity < 1 && leaf.isBranch) || leaf.__hasEraser || t.eraser) {
1649
+ if ((t.opacity < 1 && (leaf.isBranch || t.__hasMultiPaint)) || leaf.__hasEraser || t.eraser) {
1651
1650
  t.__single = true;
1652
1651
  }
1653
1652
  else if (t.__single) {
@@ -2045,8 +2044,9 @@ var LeaferUI = (function (exports) {
2045
2044
  takeCanvas = this.getSameCanvas();
2046
2045
  takeCanvas.copyWorld(this);
2047
2046
  }
2048
- DataHelper.copyAttrs(this.size, size, canvasSizeAttrs);
2049
- this.size.pixelRatio || (this.size.pixelRatio = 1);
2047
+ const s = this.size;
2048
+ DataHelper.copyAttrs(s, size, canvasSizeAttrs);
2049
+ canvasSizeAttrs.forEach(key => s[key] || (s[key] = 1));
2050
2050
  this.bounds = new Bounds(0, 0, this.width, this.height);
2051
2051
  if (this.context && !this.unreal) {
2052
2052
  this.updateViewSize();
@@ -2160,6 +2160,17 @@ var LeaferUI = (function (exports) {
2160
2160
  if (!onlyResetTransform)
2161
2161
  this.useWorldTransform();
2162
2162
  }
2163
+ useGrayscaleAlpha(bounds) {
2164
+ this.setTempBounds(bounds, true, true);
2165
+ let alpha, pixel;
2166
+ const { context } = this, imageData = context.getImageData(tempBounds$1.x, tempBounds$1.y, tempBounds$1.width, tempBounds$1.height), { data } = imageData;
2167
+ for (let i = 0, len = data.length; i < len; i += 4) {
2168
+ pixel = data[i] * 0.299 + data[i + 1] * 0.587 + data[i + 2] * 0.114;
2169
+ if (alpha = data[i + 3])
2170
+ data[i + 3] = alpha === 255 ? pixel : alpha * (pixel / 255);
2171
+ }
2172
+ context.putImageData(imageData, tempBounds$1.x, tempBounds$1.y);
2173
+ }
2163
2174
  useMask(maskCanvas, fromBounds, toBounds) {
2164
2175
  this.copyWorld(maskCanvas, fromBounds, toBounds, 'destination-in');
2165
2176
  }
@@ -2170,7 +2181,7 @@ var LeaferUI = (function (exports) {
2170
2181
  if (blendMode)
2171
2182
  this.blendMode = blendMode;
2172
2183
  this.fillStyle = color;
2173
- tempBounds$1.set(bounds).scale(this.pixelRatio);
2184
+ this.setTempBounds(bounds);
2174
2185
  this.fillRect(tempBounds$1.x, tempBounds$1.y, tempBounds$1.width, tempBounds$1.height);
2175
2186
  if (blendMode)
2176
2187
  this.blendMode = 'source-over';
@@ -2179,22 +2190,18 @@ var LeaferUI = (function (exports) {
2179
2190
  if (blendMode)
2180
2191
  this.blendMode = blendMode;
2181
2192
  this.strokeStyle = color;
2182
- tempBounds$1.set(bounds).scale(this.pixelRatio);
2193
+ this.setTempBounds(bounds);
2183
2194
  this.strokeRect(tempBounds$1.x, tempBounds$1.y, tempBounds$1.width, tempBounds$1.height);
2184
2195
  if (blendMode)
2185
2196
  this.blendMode = 'source-over';
2186
2197
  }
2187
2198
  clearWorld(bounds, ceilPixel) {
2188
- tempBounds$1.set(bounds).scale(this.pixelRatio);
2189
- if (ceilPixel)
2190
- tempBounds$1.ceil();
2199
+ this.setTempBounds(bounds, ceilPixel);
2191
2200
  this.clearRect(tempBounds$1.x, tempBounds$1.y, tempBounds$1.width, tempBounds$1.height);
2192
2201
  }
2193
2202
  clipWorld(bounds, ceilPixel) {
2194
2203
  this.beginPath();
2195
- tempBounds$1.set(bounds).scale(this.pixelRatio);
2196
- if (ceilPixel)
2197
- tempBounds$1.ceil();
2204
+ this.setTempBounds(bounds, ceilPixel);
2198
2205
  this.rect(tempBounds$1.x, tempBounds$1.y, tempBounds$1.width, tempBounds$1.height);
2199
2206
  this.clip();
2200
2207
  }
@@ -2202,6 +2209,14 @@ var LeaferUI = (function (exports) {
2202
2209
  const { pixelRatio } = this;
2203
2210
  this.clearRect(0, 0, this.width * pixelRatio + 2, this.height * pixelRatio + 2);
2204
2211
  }
2212
+ setTempBounds(bounds, ceil, intersect) {
2213
+ tempBounds$1.set(bounds);
2214
+ if (intersect)
2215
+ tempBounds$1.intersect(this.bounds);
2216
+ tempBounds$1.scale(this.pixelRatio);
2217
+ if (ceil)
2218
+ tempBounds$1.ceil();
2219
+ }
2205
2220
  isSameSize(size) {
2206
2221
  return this.width === size.width && this.height === size.height && this.pixelRatio === size.pixelRatio;
2207
2222
  }
@@ -5215,13 +5230,14 @@ var LeaferUI = (function (exports) {
5215
5230
  this.__.__checkSingle();
5216
5231
  },
5217
5232
  __render(canvas, options) {
5233
+ this.__nowWorld = this.__getNowWorld(options);
5218
5234
  if (this.__worldOpacity) {
5219
5235
  if (this.__.__single) {
5220
5236
  if (this.__.eraser === 'path')
5221
5237
  return this.__renderEraser(canvas, options);
5222
5238
  const tempCanvas = canvas.getSameCanvas(false, true);
5223
5239
  this.__renderBranch(tempCanvas, options);
5224
- const nowWorld = this.__getNowWorld(options);
5240
+ const nowWorld = this.__nowWorld;
5225
5241
  canvas.opacity = this.__.opacity;
5226
5242
  canvas.copyWorldByReset(tempCanvas, nowWorld, nowWorld, this.__.__blendMode, true);
5227
5243
  tempCanvas.recycle(nowWorld);
@@ -5962,7 +5978,7 @@ var LeaferUI = (function (exports) {
5962
5978
  }
5963
5979
  }
5964
5980
 
5965
- const version = "1.0.9";
5981
+ const version = "1.0.10";
5966
5982
 
5967
5983
  class LeaferCanvas extends LeaferCanvasBase {
5968
5984
  get allowBackgroundColor() { return true; }
@@ -6431,7 +6447,8 @@ var LeaferUI = (function (exports) {
6431
6447
  this.totalBounds = new Bounds();
6432
6448
  debug$6.log(target.innerName, '--->');
6433
6449
  try {
6434
- target.app.emit(RenderEvent.CHILD_START, target);
6450
+ if (!target.isApp)
6451
+ target.app.emit(RenderEvent.CHILD_START, target);
6435
6452
  this.emitRender(RenderEvent.START);
6436
6453
  this.renderOnce(callback);
6437
6454
  this.emitRender(RenderEvent.END, this.totalBounds);
@@ -6985,6 +7002,12 @@ var LeaferUI = (function (exports) {
6985
7002
  return strokeWidth;
6986
7003
  }
6987
7004
  get __hasStroke() { return this.stroke && this.strokeWidth; }
7005
+ get __hasMultiPaint() {
7006
+ const t = this;
7007
+ if ((t.__isFills && t.fill.length > 1) || (t.__isStrokes && t.stroke.length > 1) || t.__useEffect)
7008
+ return true;
7009
+ return t.fill && this.__hasStroke;
7010
+ }
6988
7011
  get __clipAfterFill() { return (this.cornerRadius || this.__pathInputed); }
6989
7012
  get __autoWidth() { return !this._width; }
6990
7013
  get __autoHeight() { return !this._height; }
@@ -7354,9 +7377,8 @@ var LeaferUI = (function (exports) {
7354
7377
  canvas.strokeRect(half, half, width, height);
7355
7378
  canvas.restore();
7356
7379
  }
7357
- else {
7380
+ else
7358
7381
  canvas.strokeRect(half, half, width, height);
7359
- }
7360
7382
  break;
7361
7383
  case 'outside':
7362
7384
  canvas.strokeRect(-half, -half, width + __strokeWidth, height + __strokeWidth);
@@ -7581,6 +7603,15 @@ var LeaferUI = (function (exports) {
7581
7603
  __decorate([
7582
7604
  boundsType(0)
7583
7605
  ], exports.UI.prototype, "padding", void 0);
7606
+ __decorate([
7607
+ boundsType(false)
7608
+ ], exports.UI.prototype, "lockRatio", void 0);
7609
+ __decorate([
7610
+ boundsType()
7611
+ ], exports.UI.prototype, "widthRange", void 0);
7612
+ __decorate([
7613
+ boundsType()
7614
+ ], exports.UI.prototype, "heightRange", void 0);
7584
7615
  __decorate([
7585
7616
  dataType(false)
7586
7617
  ], exports.UI.prototype, "draggable", void 0);
@@ -7872,8 +7903,6 @@ var LeaferUI = (function (exports) {
7872
7903
  __onResize(event) {
7873
7904
  this.emitEvent(event);
7874
7905
  DataHelper.copyAttrs(this.__, event, canvasSizeAttrs);
7875
- if (!event.width || !event.height)
7876
- debug$4.warn('w = 0 or h = 0');
7877
7906
  setTimeout(() => { if (this.canvasManager)
7878
7907
  this.canvasManager.clearRecycled(); }, 0);
7879
7908
  }
@@ -8198,9 +8227,6 @@ var LeaferUI = (function (exports) {
8198
8227
  __decorate([
8199
8228
  dataType(false)
8200
8229
  ], exports.Box.prototype, "resizeChildren", void 0);
8201
- __decorate([
8202
- dataType(false)
8203
- ], exports.Box.prototype, "textBox", void 0);
8204
8230
  __decorate([
8205
8231
  affectRenderBoundsType('show')
8206
8232
  ], exports.Box.prototype, "overflow", void 0);
@@ -8955,19 +8981,13 @@ var LeaferUI = (function (exports) {
8955
8981
  Object.assign(this, params);
8956
8982
  }
8957
8983
  getBoxPoint(relative) {
8958
- if (!relative)
8959
- relative = this.current;
8960
- return relative.getBoxPoint(this);
8984
+ return (relative || this.current).getBoxPoint(this);
8961
8985
  }
8962
8986
  getInnerPoint(relative) {
8963
- if (!relative)
8964
- relative = this.current;
8965
- return relative.getInnerPoint(this);
8987
+ return (relative || this.current).getInnerPoint(this);
8966
8988
  }
8967
8989
  getLocalPoint(relative) {
8968
- if (!relative)
8969
- relative = this.current;
8970
- return relative.getLocalPoint(this);
8990
+ return (relative || this.current).getLocalPoint(this);
8971
8991
  }
8972
8992
  getPagePoint() {
8973
8993
  return this.current.getPagePoint(this);
@@ -9201,7 +9221,8 @@ var LeaferUI = (function (exports) {
9201
9221
  const LeaferTypeCreator = {
9202
9222
  list: {},
9203
9223
  register(name, fn) {
9204
- list$1[name] ? debug$3.repeat(name) : list$1[name] = fn;
9224
+ list$1[name] && debug$3.repeat(name);
9225
+ list$1[name] = fn;
9205
9226
  },
9206
9227
  run(name, leafer) {
9207
9228
  const fn = list$1[name];
@@ -11217,16 +11238,16 @@ var LeaferUI = (function (exports) {
11217
11238
 
11218
11239
  const { excludeRenderBounds } = LeafBoundsHelper;
11219
11240
  exports.Group.prototype.__renderMask = function (canvas, options) {
11220
- let child, maskCanvas, contentCanvas, maskOpacity, currentMask;
11241
+ let child, maskCanvas, contentCanvas, maskOpacity, currentMask, mask;
11221
11242
  const { children } = this;
11222
11243
  for (let i = 0, len = children.length; i < len; i++) {
11223
- child = children[i];
11224
- if (child.__.mask) {
11244
+ child = children[i], mask = child.__.mask;
11245
+ if (mask) {
11225
11246
  if (currentMask) {
11226
11247
  maskEnd(this, currentMask, canvas, contentCanvas, maskCanvas, maskOpacity);
11227
11248
  maskCanvas = contentCanvas = null;
11228
11249
  }
11229
- if (child.__.mask === 'path') {
11250
+ if (mask === 'path' || mask === 'clipping-path') {
11230
11251
  if (child.opacity < 1) {
11231
11252
  currentMask = 'opacity-path';
11232
11253
  maskOpacity = child.opacity;
@@ -11240,14 +11261,14 @@ var LeaferUI = (function (exports) {
11240
11261
  child.__clip(contentCanvas || canvas, options);
11241
11262
  }
11242
11263
  else {
11243
- currentMask = 'alpha';
11264
+ currentMask = mask === 'grayscale' ? 'grayscale' : 'alpha';
11244
11265
  if (!maskCanvas)
11245
11266
  maskCanvas = getCanvas(canvas);
11246
11267
  if (!contentCanvas)
11247
11268
  contentCanvas = getCanvas(canvas);
11248
11269
  child.__render(maskCanvas, options);
11249
11270
  }
11250
- if (child.__.mask !== 'clipping')
11271
+ if (!(mask === 'clipping' || mask === 'clipping-path'))
11251
11272
  continue;
11252
11273
  }
11253
11274
  if (excludeRenderBounds(child, options))
@@ -11258,6 +11279,8 @@ var LeaferUI = (function (exports) {
11258
11279
  };
11259
11280
  function maskEnd(leaf, maskMode, canvas, contentCanvas, maskCanvas, maskOpacity) {
11260
11281
  switch (maskMode) {
11282
+ case 'grayscale':
11283
+ maskCanvas.useGrayscaleAlpha(leaf.__nowWorld);
11261
11284
  case 'alpha':
11262
11285
  usePixelMask(leaf, canvas, contentCanvas, maskCanvas);
11263
11286
  break;
@@ -11962,15 +11985,12 @@ var LeaferUI = (function (exports) {
11962
11985
  const debug$1 = Debug.get('@leafer-ui/export');
11963
11986
  canvas.export = function (filename, options) {
11964
11987
  const { quality, blob } = FileHelper.getExportOptions(options);
11965
- if (filename.includes('.')) {
11988
+ if (filename.includes('.'))
11966
11989
  return this.saveAs(filename, quality);
11967
- }
11968
- else if (blob) {
11990
+ else if (blob)
11969
11991
  return this.toBlob(filename, quality);
11970
- }
11971
- else {
11992
+ else
11972
11993
  return this.toDataURL(filename, quality);
11973
- }
11974
11994
  };
11975
11995
  canvas.toBlob = function (type, quality) {
11976
11996
  return new Promise((resolve) => {
@@ -13012,7 +13032,7 @@ var LeaferUI = (function (exports) {
13012
13032
  }
13013
13033
  getPointStyle(userStyle) {
13014
13034
  const { stroke, strokeWidth, pointFill, pointSize, pointRadius } = this.editor.mergeConfig;
13015
- const defaultStyle = { fill: pointFill, stroke, strokeWidth, around: 'center', strokeAlign: 'center', width: pointSize, height: pointSize, cornerRadius: pointRadius };
13035
+ const defaultStyle = { fill: pointFill, stroke, strokeWidth, around: 'center', strokeAlign: 'center', width: pointSize, height: pointSize, cornerRadius: pointRadius, offsetX: 0, offsetY: 0 };
13016
13036
  return userStyle ? Object.assign(defaultStyle, userStyle) : defaultStyle;
13017
13037
  }
13018
13038
  getPointsStyle() {
@@ -13094,9 +13114,10 @@ var LeaferUI = (function (exports) {
13094
13114
  const { editor } = this;
13095
13115
  if (editor.single) {
13096
13116
  const { element } = editor;
13097
- if (element.isBranch) {
13117
+ if (element.isBranch && !element.editInner) {
13098
13118
  if (element.textBox) {
13099
- const find = element.children.find(item => item.editable && item instanceof exports.Text);
13119
+ const { children } = element;
13120
+ const find = children.find(item => item.editable && item instanceof exports.Text) || children.find(item => item instanceof exports.Text);
13100
13121
  if (find)
13101
13122
  return editor.openInnerEditor(find);
13102
13123
  }
@@ -13356,7 +13377,8 @@ ${filterStyle}
13356
13377
  list: {},
13357
13378
  register(EditTool) {
13358
13379
  const { tag } = EditTool.prototype;
13359
- list[tag] ? debug.repeat(tag) : (list[tag] = EditTool);
13380
+ list[tag] && debug.repeat(tag);
13381
+ list[tag] = EditTool;
13360
13382
  },
13361
13383
  get(tag, editor) {
13362
13384
  return new list[tag](editor);
@@ -14050,12 +14072,16 @@ ${filterStyle}
14050
14072
  ], exports.LineEditTool);
14051
14073
 
14052
14074
  Creator.editor = function (options) { return new Editor(options); };
14075
+ dataType(false)(exports.Box.prototype, 'textBox');
14053
14076
  defineKey(exports.UI.prototype, 'editOuter', {
14054
14077
  get() { return this.__.__isLinePath ? 'LineEditTool' : 'EditTool'; }
14055
14078
  });
14056
14079
  defineKey(exports.UI.prototype, 'editInner', {
14057
14080
  get() { return 'PathEditor'; }
14058
14081
  });
14082
+ defineKey(exports.Group.prototype, 'editInner', {
14083
+ get() { return ''; }
14084
+ });
14059
14085
  defineKey(exports.Text.prototype, 'editInner', {
14060
14086
  get() { return 'TextEditor'; }
14061
14087
  });
@@ -14683,8 +14709,10 @@ ${filterStyle}
14683
14709
  this.textScale = textScale;
14684
14710
  const { a, b, c, d, e, f } = new Matrix(text.worldTransform).scale(1 / textScale);
14685
14711
  let { x, y } = this.inBody ? text.app.clientBounds : text.app.tree.clientBounds;
14712
+ if (!this.inBody)
14713
+ x -= window.scrollX, y -= window.scrollY;
14686
14714
  let { width, height } = text;
14687
- x -= window.scrollX, y -= window.scrollY, width *= textScale, height *= textScale;
14715
+ width *= textScale, height *= textScale;
14688
14716
  const data = text.__;
14689
14717
  if (data.__autoWidth && data.autoSizeAlign) {
14690
14718
  width += 20;