@leafer-ui/node 1.4.1 → 1.5.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/node.cjs CHANGED
@@ -2124,13 +2124,14 @@ function toChar(data, charX, rowData, isOverflow) {
2124
2124
  }
2125
2125
 
2126
2126
  function layoutText(drawData, style) {
2127
- const { rows, bounds } = drawData;
2127
+ const { rows, bounds } = drawData, countRows = rows.length;
2128
2128
  const { __lineHeight, __baseLine, __letterSpacing, __clipText, textAlign, verticalAlign, paraSpacing, autoSizeAlign } = style;
2129
- let { x, y, width, height } = bounds, realHeight = __lineHeight * rows.length + (paraSpacing ? paraSpacing * (drawData.paraNumber - 1) : 0);
2129
+ let { x, y, width, height } = bounds, realHeight = __lineHeight * countRows + (paraSpacing ? paraSpacing * (drawData.paraNumber - 1) : 0);
2130
2130
  let starY = __baseLine;
2131
2131
  if (__clipText && realHeight > height) {
2132
2132
  realHeight = Math.max(height, __lineHeight);
2133
- drawData.overflow = rows.length;
2133
+ if (countRows > 1)
2134
+ drawData.overflow = countRows;
2134
2135
  }
2135
2136
  else if (height || autoSizeAlign) {
2136
2137
  switch (verticalAlign) {
@@ -2142,7 +2143,7 @@ function layoutText(drawData, style) {
2142
2143
  }
2143
2144
  starY += y;
2144
2145
  let row, rowX, rowWidth, layoutWidth = (width || autoSizeAlign) ? width : drawData.maxWidth;
2145
- for (let i = 0, len = rows.length; i < len; i++) {
2146
+ for (let i = 0, len = countRows; i < len; i++) {
2146
2147
  row = rows[i];
2147
2148
  row.x = x;
2148
2149
  if (row.width < width || (row.width > width && !__clipText)) {
@@ -2211,7 +2212,7 @@ function clipText(drawData, style, x, width) {
2211
2212
  if (i === end && charRight < right) {
2212
2213
  break;
2213
2214
  }
2214
- else if (charRight < right && char.char !== ' ') {
2215
+ else if ((charRight < right && char.char !== ' ') || !i) {
2215
2216
  row.data.splice(i + 1);
2216
2217
  row.width -= char.width;
2217
2218
  break;
@@ -2355,124 +2356,134 @@ function getTrimBounds(canvas) {
2355
2356
  }
2356
2357
 
2357
2358
  const ExportModule = {
2358
- export(leaf, filename, options) {
2359
+ syncExport(leaf, filename, options) {
2359
2360
  this.running = true;
2361
+ let result;
2360
2362
  const fileType = draw.FileHelper.fileType(filename);
2361
2363
  const isDownload = filename.includes('.');
2362
2364
  options = draw.FileHelper.getExportOptions(options);
2363
- return addTask((success) => new Promise((resolve) => {
2364
- const over = (result) => {
2365
- success(result);
2366
- resolve();
2367
- this.running = false;
2368
- };
2369
- const { toURL } = draw.Platform;
2370
- const { download } = draw.Platform.origin;
2371
- if (fileType === 'json') {
2372
- isDownload && download(toURL(JSON.stringify(leaf.toJSON(options.json)), 'text'), filename);
2373
- return over({ data: isDownload ? true : leaf.toJSON(options.json) });
2365
+ const { toURL } = draw.Platform;
2366
+ const { download } = draw.Platform.origin;
2367
+ if (fileType === 'json') {
2368
+ isDownload && download(toURL(JSON.stringify(leaf.toJSON(options.json)), 'text'), filename);
2369
+ result = { data: isDownload ? true : leaf.toJSON(options.json) };
2370
+ }
2371
+ else if (fileType === 'svg') {
2372
+ isDownload && download(toURL(leaf.toSVG(), 'svg'), filename);
2373
+ result = { data: isDownload ? true : leaf.toSVG() };
2374
+ }
2375
+ else {
2376
+ let renderBounds, trimBounds, scaleX = 1, scaleY = 1;
2377
+ const { worldTransform, isLeafer, leafer, isFrame } = leaf;
2378
+ const { slice, trim, padding, onCanvas } = options;
2379
+ const smooth = options.smooth === undefined ? (leafer ? leafer.config.smooth : true) : options.smooth;
2380
+ const contextSettings = options.contextSettings || (leafer ? leafer.config.contextSettings : undefined);
2381
+ const screenshot = options.screenshot || leaf.isApp;
2382
+ const fill = (isLeafer && screenshot) ? (options.fill === undefined ? leaf.fill : options.fill) : options.fill;
2383
+ const needFill = draw.FileHelper.isOpaqueImage(filename) || fill, matrix = new draw.Matrix();
2384
+ if (screenshot) {
2385
+ renderBounds = screenshot === true ? (isLeafer ? leafer.canvas.bounds : leaf.worldRenderBounds) : screenshot;
2374
2386
  }
2375
- if (fileType === 'svg') {
2376
- isDownload && download(toURL(leaf.toSVG(), 'svg'), filename);
2377
- return over({ data: isDownload ? true : leaf.toSVG() });
2387
+ else {
2388
+ let relative = options.relative || (isLeafer ? 'inner' : 'local');
2389
+ scaleX = worldTransform.scaleX;
2390
+ scaleY = worldTransform.scaleY;
2391
+ switch (relative) {
2392
+ case 'inner':
2393
+ matrix.set(worldTransform);
2394
+ break;
2395
+ case 'local':
2396
+ matrix.set(worldTransform).divide(leaf.localTransform);
2397
+ scaleX /= leaf.scaleX;
2398
+ scaleY /= leaf.scaleY;
2399
+ break;
2400
+ case 'world':
2401
+ scaleX = 1;
2402
+ scaleY = 1;
2403
+ break;
2404
+ case 'page':
2405
+ relative = leafer || leaf;
2406
+ default:
2407
+ matrix.set(worldTransform).divide(leaf.getTransform(relative));
2408
+ const l = relative.worldTransform;
2409
+ scaleX /= scaleX / l.scaleX;
2410
+ scaleY /= scaleY / l.scaleY;
2411
+ }
2412
+ renderBounds = leaf.getBounds('render', relative);
2378
2413
  }
2379
- const { leafer } = leaf;
2380
- if (leafer) {
2381
- checkLazy(leaf);
2382
- leafer.waitViewCompleted(() => __awaiter(this, void 0, void 0, function* () {
2383
- let renderBounds, trimBounds, scaleX = 1, scaleY = 1;
2384
- const { worldTransform, isLeafer, isFrame } = leaf;
2385
- const { slice, trim, padding, onCanvas } = options;
2386
- const smooth = options.smooth === undefined ? leafer.config.smooth : options.smooth;
2387
- const contextSettings = options.contextSettings || leafer.config.contextSettings;
2388
- const screenshot = options.screenshot || leaf.isApp;
2389
- const fill = (isLeafer && screenshot) ? (options.fill === undefined ? leaf.fill : options.fill) : options.fill;
2390
- const needFill = draw.FileHelper.isOpaqueImage(filename) || fill, matrix = new draw.Matrix();
2391
- if (screenshot) {
2392
- renderBounds = screenshot === true ? (isLeafer ? leafer.canvas.bounds : leaf.worldRenderBounds) : screenshot;
2393
- }
2394
- else {
2395
- let relative = options.relative || (isLeafer ? 'inner' : 'local');
2396
- scaleX = worldTransform.scaleX;
2397
- scaleY = worldTransform.scaleY;
2398
- switch (relative) {
2399
- case 'inner':
2400
- matrix.set(worldTransform);
2401
- break;
2402
- case 'local':
2403
- matrix.set(worldTransform).divide(leaf.localTransform);
2404
- scaleX /= leaf.scaleX;
2405
- scaleY /= leaf.scaleY;
2406
- break;
2407
- case 'world':
2408
- scaleX = 1;
2409
- scaleY = 1;
2410
- break;
2411
- case 'page':
2412
- relative = leaf.leafer;
2413
- default:
2414
- matrix.set(worldTransform).divide(leaf.getTransform(relative));
2415
- const l = relative.worldTransform;
2416
- scaleX /= scaleX / l.scaleX;
2417
- scaleY /= scaleY / l.scaleY;
2418
- }
2419
- renderBounds = leaf.getBounds('render', relative);
2420
- }
2421
- const scaleData = { scaleX: 1, scaleY: 1 };
2422
- draw.MathHelper.getScaleData(options.scale, options.size, renderBounds, scaleData);
2423
- let pixelRatio = options.pixelRatio || 1;
2424
- if (leaf.isApp) {
2425
- scaleData.scaleX *= pixelRatio;
2426
- scaleData.scaleY *= pixelRatio;
2427
- pixelRatio = leaf.app.pixelRatio;
2428
- }
2429
- const { x, y, width, height } = new draw.Bounds(renderBounds).scale(scaleData.scaleX, scaleData.scaleY);
2430
- const renderOptions = { matrix: matrix.scale(1 / scaleData.scaleX, 1 / scaleData.scaleY).invert().translate(-x, -y).withScale(1 / scaleX * scaleData.scaleX, 1 / scaleY * scaleData.scaleY) };
2431
- let canvas = draw.Creator.canvas({ width: Math.round(width), height: Math.round(height), pixelRatio, smooth, contextSettings });
2432
- let sliceLeaf;
2433
- if (slice) {
2434
- sliceLeaf = leaf;
2435
- sliceLeaf.__worldOpacity = 0;
2436
- leaf = leafer;
2437
- renderOptions.bounds = canvas.bounds;
2438
- }
2439
- canvas.save();
2440
- if (isFrame && fill !== undefined) {
2441
- const oldFill = leaf.get('fill');
2442
- leaf.fill = '';
2443
- leaf.__render(canvas, renderOptions);
2444
- leaf.fill = oldFill;
2445
- }
2446
- else {
2447
- leaf.__render(canvas, renderOptions);
2448
- }
2449
- canvas.restore();
2450
- if (sliceLeaf)
2451
- sliceLeaf.__updateWorldOpacity();
2452
- if (trim) {
2453
- trimBounds = getTrimBounds(canvas);
2454
- const old = canvas, { width, height } = trimBounds;
2455
- const config = { x: 0, y: 0, width, height, pixelRatio };
2456
- canvas = draw.Creator.canvas(config);
2457
- canvas.copyWorld(old, trimBounds, config);
2458
- }
2459
- if (padding) {
2460
- const [top, right, bottom, left] = draw.MathHelper.fourNumber(padding);
2461
- const old = canvas, { width, height } = old;
2462
- canvas = draw.Creator.canvas({ width: width + left + right, height: height + top + bottom, pixelRatio });
2463
- canvas.copyWorld(old, old.bounds, { x: left, y: top, width, height });
2464
- }
2465
- if (needFill)
2466
- canvas.fillWorld(canvas.bounds, fill || '#FFFFFF', 'destination-over');
2467
- if (onCanvas)
2468
- onCanvas(canvas);
2469
- const data = filename === 'canvas' ? canvas : yield canvas.export(filename, options);
2470
- over({ data, width: canvas.pixelWidth, height: canvas.pixelHeight, renderBounds, trimBounds });
2471
- }));
2414
+ const scaleData = { scaleX: 1, scaleY: 1 };
2415
+ draw.MathHelper.getScaleData(options.scale, options.size, renderBounds, scaleData);
2416
+ let pixelRatio = options.pixelRatio || 1;
2417
+ if (leaf.isApp) {
2418
+ scaleData.scaleX *= pixelRatio;
2419
+ scaleData.scaleY *= pixelRatio;
2420
+ pixelRatio = leaf.app.pixelRatio;
2421
+ }
2422
+ const { x, y, width, height } = new draw.Bounds(renderBounds).scale(scaleData.scaleX, scaleData.scaleY);
2423
+ const renderOptions = { matrix: matrix.scale(1 / scaleData.scaleX, 1 / scaleData.scaleY).invert().translate(-x, -y).withScale(1 / scaleX * scaleData.scaleX, 1 / scaleY * scaleData.scaleY) };
2424
+ let canvas = draw.Creator.canvas({ width: Math.floor(width), height: Math.floor(height), pixelRatio, smooth, contextSettings });
2425
+ let sliceLeaf;
2426
+ if (slice) {
2427
+ sliceLeaf = leaf;
2428
+ sliceLeaf.__worldOpacity = 0;
2429
+ leaf = leafer || leaf;
2430
+ renderOptions.bounds = canvas.bounds;
2431
+ }
2432
+ canvas.save();
2433
+ if (isFrame && fill !== undefined) {
2434
+ const oldFill = leaf.get('fill');
2435
+ leaf.fill = '';
2436
+ leaf.__render(canvas, renderOptions);
2437
+ leaf.fill = oldFill;
2472
2438
  }
2473
2439
  else {
2474
- over({ data: false });
2440
+ leaf.__render(canvas, renderOptions);
2441
+ }
2442
+ canvas.restore();
2443
+ if (sliceLeaf)
2444
+ sliceLeaf.__updateWorldOpacity();
2445
+ if (trim) {
2446
+ trimBounds = getTrimBounds(canvas);
2447
+ const old = canvas, { width, height } = trimBounds;
2448
+ const config = { x: 0, y: 0, width, height, pixelRatio };
2449
+ canvas = draw.Creator.canvas(config);
2450
+ canvas.copyWorld(old, trimBounds, config);
2451
+ }
2452
+ if (padding) {
2453
+ const [top, right, bottom, left] = draw.MathHelper.fourNumber(padding);
2454
+ const old = canvas, { width, height } = old;
2455
+ canvas = draw.Creator.canvas({ width: width + left + right, height: height + top + bottom, pixelRatio });
2456
+ canvas.copyWorld(old, old.bounds, { x: left, y: top, width, height });
2475
2457
  }
2458
+ if (needFill)
2459
+ canvas.fillWorld(canvas.bounds, fill || '#FFFFFF', 'destination-over');
2460
+ if (onCanvas)
2461
+ onCanvas(canvas);
2462
+ const data = filename === 'canvas' ? canvas : canvas.export(filename, options);
2463
+ result = { data, width: canvas.pixelWidth, height: canvas.pixelHeight, renderBounds, trimBounds };
2464
+ }
2465
+ this.running = false;
2466
+ return result;
2467
+ },
2468
+ export(leaf, filename, options) {
2469
+ this.running = true;
2470
+ return addTask((success) => new Promise((resolve) => {
2471
+ const getResult = () => __awaiter(this, void 0, void 0, function* () {
2472
+ if (!draw.Resource.isComplete)
2473
+ return draw.Platform.requestRender(getResult);
2474
+ const result = ExportModule.syncExport(leaf, filename, options);
2475
+ if (result.data instanceof Promise)
2476
+ result.data = yield result.data;
2477
+ success(result);
2478
+ resolve();
2479
+ });
2480
+ leaf.updateLayout();
2481
+ checkLazy(leaf);
2482
+ const { leafer } = leaf;
2483
+ if (leafer)
2484
+ leafer.waitViewCompleted(getResult);
2485
+ else
2486
+ getResult();
2476
2487
  }));
2477
2488
  }
2478
2489
  };
@@ -2531,6 +2542,9 @@ Object.assign(draw.Export, ExportModule);
2531
2542
  draw.UI.prototype.export = function (filename, options) {
2532
2543
  return draw.Export.export(this, filename, options);
2533
2544
  };
2545
+ draw.UI.prototype.syncExport = function (filename, options) {
2546
+ return draw.Export.syncExport(this, filename, options);
2547
+ };
2534
2548
 
2535
2549
  Object.assign(core.Creator, {
2536
2550
  interaction: (target, canvas, selector, options) => { return new core$1.InteractionBase(target, canvas, selector, options); },