@logicflow/extension 2.2.0-alpha.3 → 2.2.0-alpha.5

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.
@@ -41,6 +41,9 @@ var __read = (this && this.__read) || function (o, n) {
41
41
  return ar;
42
42
  };
43
43
  import { PolylineEdge, PolylineEdgeModel, h } from '@logicflow/core';
44
+ // 方向组合到圆弧象限的映射。
45
+ // key 由进入方向(dir1)和离开方向(dir2)拼接,例如 'tr' 表示从上(t)到右(r)的拐角。
46
+ // 通过该映射确定在拐点处应该绘制的圆弧象限,用于计算中间控制点。
44
47
  var directionMap = {
45
48
  tr: 'tl',
46
49
  lb: 'tl',
@@ -51,13 +54,17 @@ var directionMap = {
51
54
  bl: 'br',
52
55
  rt: 'br',
53
56
  };
57
+ // 过滤折线中的共线中间点,减少不必要的顶点
54
58
  function pointFilter(points) {
59
+ // 原地修改传入的数组
55
60
  var all = points;
61
+ // 从第二个点开始,检查三点是否共线
56
62
  var i = 1;
57
63
  while (i < all.length - 1) {
58
64
  var _a = __read(all[i - 1], 2), x = _a[0], y = _a[1];
59
65
  var _b = __read(all[i], 2), x1 = _b[0], y1 = _b[1];
60
66
  var _c = __read(all[i + 1], 2), x2 = _c[0], y2 = _c[1];
67
+ // 如果三点在同一条水平或垂直直线上,删除中间点
61
68
  if ((x === x1 && x1 === x2) || (y === y1 && y1 === y2)) {
62
69
  all.splice(i, 1);
63
70
  }
@@ -65,6 +72,7 @@ function pointFilter(points) {
65
72
  i++;
66
73
  }
67
74
  }
75
+ // 返回精简后的点集
68
76
  return all;
69
77
  }
70
78
  function getMidPoints(cur, key, orientation, radius) {
@@ -121,18 +129,22 @@ function getMidPoints(cur, key, orientation, radius) {
121
129
  }
122
130
  function getPartialPath(prev, cur, next, radius) {
123
131
  var _a;
132
+ // 定义误差容错变量
133
+ var tolerance = 1;
124
134
  var dir1 = '';
125
135
  var dir2 = '';
126
- if (prev[0] === cur[0]) {
136
+ if (Math.abs(prev[0] - cur[0]) <= tolerance) {
137
+ // 垂直方向
127
138
  dir1 = prev[1] > cur[1] ? 't' : 'b';
128
139
  }
129
- else if (prev[1] === cur[1]) {
140
+ else if (Math.abs(prev[1] - cur[1]) <= tolerance) {
141
+ // 水平方向
130
142
  dir1 = prev[0] > cur[0] ? 'l' : 'r';
131
143
  }
132
- if (cur[0] === next[0]) {
144
+ if (Math.abs(cur[0] - next[0]) <= tolerance) {
133
145
  dir2 = cur[1] > next[1] ? 't' : 'b';
134
146
  }
135
- else if (cur[1] === next[1]) {
147
+ else if (Math.abs(cur[1] - next[1]) <= tolerance) {
136
148
  dir2 = cur[0] > next[0] ? 'l' : 'r';
137
149
  }
138
150
  var r = Math.min(Math.hypot(cur[0] - prev[0], cur[1] - prev[1]) / 2, Math.hypot(next[0] - cur[0], next[1] - cur[1]) / 2, radius) || (1 / 5) * radius;
@@ -110,10 +110,14 @@ export declare class Snapshot {
110
110
  */
111
111
  private getClassRules;
112
112
  /**
113
- * 根据浏览器类型获取Canvas尺寸限制
114
- * @returns 包含最大主维度和次维度的对象
113
+ * 根据浏览器对 canvas 的大小限制,计算等比缩放比例
114
+ * - 参考 MDN 最大的画布尺寸:
115
+ * https://developer.mozilla.org/zh-CN/docs/Web/HTML/Reference/Elements/canvas#最大的画布尺寸
116
+ *
117
+ * @param width
118
+ * @param height
115
119
  */
116
- private getCanvasDimensionsByBrowser;
120
+ private getCanvasScaleRatio;
117
121
  /**
118
122
  * 将 svg 转化为 canvas
119
123
  * @param svg - svg 元素
@@ -345,24 +345,28 @@ var Snapshot = /** @class */ (function () {
345
345
  return rules;
346
346
  };
347
347
  /**
348
- * 根据浏览器类型获取Canvas尺寸限制
349
- * @returns 包含最大主维度和次维度的对象
348
+ * 根据浏览器对 canvas 的大小限制,计算等比缩放比例
349
+ * - 参考 MDN 最大的画布尺寸:
350
+ * https://developer.mozilla.org/zh-CN/docs/Web/HTML/Reference/Elements/canvas#最大的画布尺寸
351
+ *
352
+ * @param width
353
+ * @param height
350
354
  */
351
- Snapshot.prototype.getCanvasDimensionsByBrowser = function () {
352
- var userAgent = navigator.userAgent;
353
- // 默认值
354
- var maxCanvasDimension = 65535;
355
- var otherMaxCanvasDimension = 4096;
356
- if (userAgent.indexOf('Chrome') !== -1 ||
357
- userAgent.indexOf('Edge') !== -1) {
358
- maxCanvasDimension = 65535;
359
- otherMaxCanvasDimension = 4096;
355
+ Snapshot.prototype.getCanvasScaleRatio = function (width, height) {
356
+ /* 单边最大像素 */
357
+ var maxCanvasSide = 32767;
358
+ /* 最大像素面积 */
359
+ var maxCanvasArea = 268435456;
360
+ var area = width * height;
361
+ if (width <= maxCanvasSide &&
362
+ height <= maxCanvasSide &&
363
+ area <= maxCanvasArea) {
364
+ return 1;
360
365
  }
361
- else if (userAgent.indexOf('Firefox') !== -1) {
362
- maxCanvasDimension = 32767;
363
- otherMaxCanvasDimension = 3814;
364
- }
365
- return { maxCanvasDimension: maxCanvasDimension, otherMaxCanvasDimension: otherMaxCanvasDimension };
366
+ var widthScale = maxCanvasSide / width;
367
+ var heightScale = maxCanvasSide / height;
368
+ var areaScale = maxCanvasArea / area;
369
+ return Math.min(widthScale, heightScale, areaScale);
366
370
  };
367
371
  /**
368
372
  * 将 svg 转化为 canvas
@@ -372,14 +376,14 @@ var Snapshot = /** @class */ (function () {
372
376
  */
373
377
  Snapshot.prototype.getCanvasData = function (svg, toImageOptions) {
374
378
  return __awaiter(this, void 0, void 0, function () {
375
- var width, height, backgroundColor, _a, padding, copy, dpr, base, bbox, layoutCanvas, layout, offsetX, offsetY, graphModel, transformModel, SCALE_X, SCALE_Y, TRANSLATE_X, TRANSLATE_Y, safetyFactor, actualWidth, actualHeight, bboxWidth, bboxHeight, canvas, safetyMargin, _b, maxCanvasDimension, otherMaxCanvasDimension, MAX_CANVAS_DIMENSION, OTHER_MAX_CANVAS_DIMENSION, targetWidth, targetHeight, scaleWidth, scaleHeight, ctx, img, style, foreignObject;
376
- return __generator(this, function (_c) {
377
- switch (_c.label) {
379
+ var width, height, backgroundColor, _a, padding, copy, dpr, base, bbox, layoutCanvas, layout, offsetX, offsetY, graphModel, transformModel, SCALE_X, SCALE_Y, TRANSLATE_X, TRANSLATE_Y, safetyFactor, actualWidth, actualHeight, factorWidth, factorHeight, bboxWidth, bboxHeight, canvas, safetyMargin, targetWidth, targetHeight, scaleRatio, ctx, img, style, foreignObject;
380
+ return __generator(this, function (_b) {
381
+ switch (_b.label) {
378
382
  case 0:
379
383
  width = toImageOptions.width, height = toImageOptions.height, backgroundColor = toImageOptions.backgroundColor, _a = toImageOptions.padding, padding = _a === void 0 ? 40 : _a;
380
384
  return [4 /*yield*/, this.cloneSvg(svg, false)];
381
385
  case 1:
382
- copy = _c.sent();
386
+ copy = _b.sent();
383
387
  dpr = window.devicePixelRatio || 1;
384
388
  if (dpr < 1) {
385
389
  // https://github.com/didi/LogicFlow/issues/1222
@@ -404,62 +408,42 @@ var Snapshot = /** @class */ (function () {
404
408
  graphModel = this.lf.graphModel;
405
409
  transformModel = graphModel.transformModel;
406
410
  SCALE_X = transformModel.SCALE_X, SCALE_Y = transformModel.SCALE_Y, TRANSLATE_X = transformModel.TRANSLATE_X, TRANSLATE_Y = transformModel.TRANSLATE_Y;
407
- safetyFactor = toImageOptions.safetyFactor || 1.1 // 安全系数,增加10%的空间
411
+ safetyFactor = toImageOptions.safetyFactor || 1 // 安全系数,增加10%的空间
408
412
  ;
409
- actualWidth = (bbox.width / SCALE_X) * safetyFactor;
410
- actualHeight = (bbox.height / SCALE_Y) * safetyFactor;
411
- bboxWidth = Math.ceil(actualWidth);
412
- bboxHeight = Math.ceil(actualHeight);
413
+ actualWidth = bbox.width / SCALE_X;
414
+ actualHeight = bbox.height / SCALE_Y;
415
+ factorWidth = actualWidth * (safetyFactor - 1);
416
+ factorHeight = actualHeight * (safetyFactor - 1);
417
+ bboxWidth = Math.ceil(actualWidth + factorWidth);
418
+ bboxHeight = Math.ceil(actualHeight + factorHeight);
413
419
  canvas = document.createElement('canvas');
414
420
  canvas.style.width = "".concat(bboxWidth, "px");
415
421
  canvas.style.height = "".concat(bboxHeight, "px");
416
- safetyMargin = toImageOptions.safetyMargin || 40 // 额外的安全边距
422
+ safetyMargin = toImageOptions.safetyMargin || 0 // 额外的安全边距
417
423
  ;
418
- _b = this.getCanvasDimensionsByBrowser(), maxCanvasDimension = _b.maxCanvasDimension, otherMaxCanvasDimension = _b.otherMaxCanvasDimension;
419
- MAX_CANVAS_DIMENSION = maxCanvasDimension;
420
- OTHER_MAX_CANVAS_DIMENSION = otherMaxCanvasDimension;
421
- targetWidth = bboxWidth * dpr + padding * 2 + safetyMargin;
422
- targetHeight = bboxHeight * dpr + padding * 2 + safetyMargin;
423
- scaleWidth = 1 //宽 缩放
424
- ;
425
- scaleHeight = 1 //高 缩放
426
- ;
427
- // 对宽和高分别进行缩放,如chrome,矩形单边最大宽度不超过65535,如宽超过65535,那么高不能超过4096,否则像素会超,也会显示不出。
428
- if (targetWidth > MAX_CANVAS_DIMENSION &&
429
- targetHeight > OTHER_MAX_CANVAS_DIMENSION) {
430
- scaleWidth = MAX_CANVAS_DIMENSION / targetWidth;
431
- scaleHeight = OTHER_MAX_CANVAS_DIMENSION / targetHeight;
432
- }
433
- else if (targetWidth > OTHER_MAX_CANVAS_DIMENSION &&
434
- targetHeight > MAX_CANVAS_DIMENSION) {
435
- scaleWidth = OTHER_MAX_CANVAS_DIMENSION / targetWidth;
436
- scaleHeight = MAX_CANVAS_DIMENSION / targetHeight;
437
- }
438
- else if (targetWidth > MAX_CANVAS_DIMENSION &&
439
- targetHeight < OTHER_MAX_CANVAS_DIMENSION) {
440
- scaleHeight = scaleWidth = MAX_CANVAS_DIMENSION / targetWidth;
441
- }
442
- else if (targetWidth < OTHER_MAX_CANVAS_DIMENSION &&
443
- targetHeight > MAX_CANVAS_DIMENSION) {
444
- scaleHeight = scaleWidth = MAX_CANVAS_DIMENSION / targetHeight;
445
- }
446
- if (scaleWidth < 1 || scaleHeight < 1) {
447
- targetWidth = Math.floor(targetWidth * scaleWidth);
448
- targetHeight = Math.floor(targetHeight * scaleHeight);
424
+ targetWidth = bboxWidth * dpr;
425
+ targetHeight = bboxHeight * dpr;
426
+ scaleRatio = this.getCanvasScaleRatio(targetWidth, targetHeight);
427
+ if (scaleRatio < 1) {
428
+ targetWidth = Math.floor(targetWidth * scaleRatio);
429
+ targetHeight = Math.floor(targetHeight * scaleRatio);
449
430
  }
450
431
  // 将导出区域移动到左上角,canvas 绘制的时候是从左上角开始绘制的
451
432
  // 在transform矩阵中加入padding值,确保左侧元素不会被截断
452
433
  // 对这个矩阵进行缩放,否则会导致截断
453
434
  ;
454
435
  copy.lastChild.style.transform =
455
- "matrix(".concat(scaleWidth, ", 0, 0, ").concat(scaleHeight, ", ").concat((-offsetX + TRANSLATE_X) * (1 / SCALE_X) * scaleWidth + padding / dpr, ", ").concat((-offsetY + TRANSLATE_Y) * (1 / SCALE_Y) * scaleHeight + padding / dpr, ")");
456
- canvas.width = targetWidth;
457
- canvas.height = targetHeight;
436
+ "matrix(".concat(scaleRatio, ", 0, 0, ").concat(scaleRatio, ", ").concat((-offsetX + TRANSLATE_X) * (1 / SCALE_X) * scaleRatio +
437
+ padding +
438
+ factorWidth / 2 +
439
+ safetyMargin, ", ").concat((-offsetY + TRANSLATE_Y) * (1 / SCALE_Y) * scaleRatio + padding + factorHeight / 2 + safetyMargin, ")");
440
+ canvas.width = targetWidth + (padding + safetyMargin) * 2 * dpr;
441
+ canvas.height = targetHeight + (padding + safetyMargin) * 2 * dpr;
458
442
  ctx = canvas.getContext('2d');
459
443
  if (ctx) {
460
444
  // 清空canvas
461
445
  ctx.clearRect(0, 0, canvas.width, canvas.height);
462
- ctx.scale(dpr * scaleWidth, dpr * scaleHeight);
446
+ ctx.scale(dpr, dpr);
463
447
  // 如果有背景色,设置流程图导出的背景色
464
448
  if (backgroundColor) {
465
449
  ctx.fillStyle = backgroundColor;
@@ -717,9 +701,7 @@ var Snapshot = /** @class */ (function () {
717
701
  var _this = this;
718
702
  return __generator(this, function (_a) {
719
703
  switch (_a.label) {
720
- case 0:
721
- console.log('getSnapshotBase64---------------', backgroundColor, fileType, toImageOptions);
722
- return [4 /*yield*/, this.withExportPreparation(function () { return _this._getSnapshotBase64(backgroundColor, fileType, toImageOptions); }, toImageOptions)];
704
+ case 0: return [4 /*yield*/, this.withExportPreparation(function () { return _this._getSnapshotBase64(backgroundColor, fileType, toImageOptions); }, toImageOptions)];
723
705
  case 1: return [2 /*return*/, _a.sent()];
724
706
  }
725
707
  });
@@ -44,6 +44,9 @@ var __read = (this && this.__read) || function (o, n) {
44
44
  Object.defineProperty(exports, "__esModule", { value: true });
45
45
  exports.getCurvedEdgePath = exports.CurvedEdgeModel = exports.CurvedEdge = void 0;
46
46
  var core_1 = require("@logicflow/core");
47
+ // 方向组合到圆弧象限的映射。
48
+ // key 由进入方向(dir1)和离开方向(dir2)拼接,例如 'tr' 表示从上(t)到右(r)的拐角。
49
+ // 通过该映射确定在拐点处应该绘制的圆弧象限,用于计算中间控制点。
47
50
  var directionMap = {
48
51
  tr: 'tl',
49
52
  lb: 'tl',
@@ -54,13 +57,17 @@ var directionMap = {
54
57
  bl: 'br',
55
58
  rt: 'br',
56
59
  };
60
+ // 过滤折线中的共线中间点,减少不必要的顶点
57
61
  function pointFilter(points) {
62
+ // 原地修改传入的数组
58
63
  var all = points;
64
+ // 从第二个点开始,检查三点是否共线
59
65
  var i = 1;
60
66
  while (i < all.length - 1) {
61
67
  var _a = __read(all[i - 1], 2), x = _a[0], y = _a[1];
62
68
  var _b = __read(all[i], 2), x1 = _b[0], y1 = _b[1];
63
69
  var _c = __read(all[i + 1], 2), x2 = _c[0], y2 = _c[1];
70
+ // 如果三点在同一条水平或垂直直线上,删除中间点
64
71
  if ((x === x1 && x1 === x2) || (y === y1 && y1 === y2)) {
65
72
  all.splice(i, 1);
66
73
  }
@@ -68,6 +75,7 @@ function pointFilter(points) {
68
75
  i++;
69
76
  }
70
77
  }
78
+ // 返回精简后的点集
71
79
  return all;
72
80
  }
73
81
  function getMidPoints(cur, key, orientation, radius) {
@@ -124,18 +132,22 @@ function getMidPoints(cur, key, orientation, radius) {
124
132
  }
125
133
  function getPartialPath(prev, cur, next, radius) {
126
134
  var _a;
135
+ // 定义误差容错变量
136
+ var tolerance = 1;
127
137
  var dir1 = '';
128
138
  var dir2 = '';
129
- if (prev[0] === cur[0]) {
139
+ if (Math.abs(prev[0] - cur[0]) <= tolerance) {
140
+ // 垂直方向
130
141
  dir1 = prev[1] > cur[1] ? 't' : 'b';
131
142
  }
132
- else if (prev[1] === cur[1]) {
143
+ else if (Math.abs(prev[1] - cur[1]) <= tolerance) {
144
+ // 水平方向
133
145
  dir1 = prev[0] > cur[0] ? 'l' : 'r';
134
146
  }
135
- if (cur[0] === next[0]) {
147
+ if (Math.abs(cur[0] - next[0]) <= tolerance) {
136
148
  dir2 = cur[1] > next[1] ? 't' : 'b';
137
149
  }
138
- else if (cur[1] === next[1]) {
150
+ else if (Math.abs(cur[1] - next[1]) <= tolerance) {
139
151
  dir2 = cur[0] > next[0] ? 'l' : 'r';
140
152
  }
141
153
  var r = Math.min(Math.hypot(cur[0] - prev[0], cur[1] - prev[1]) / 2, Math.hypot(next[0] - cur[0], next[1] - cur[1]) / 2, radius) || (1 / 5) * radius;
@@ -110,10 +110,14 @@ export declare class Snapshot {
110
110
  */
111
111
  private getClassRules;
112
112
  /**
113
- * 根据浏览器类型获取Canvas尺寸限制
114
- * @returns 包含最大主维度和次维度的对象
113
+ * 根据浏览器对 canvas 的大小限制,计算等比缩放比例
114
+ * - 参考 MDN 最大的画布尺寸:
115
+ * https://developer.mozilla.org/zh-CN/docs/Web/HTML/Reference/Elements/canvas#最大的画布尺寸
116
+ *
117
+ * @param width
118
+ * @param height
115
119
  */
116
- private getCanvasDimensionsByBrowser;
120
+ private getCanvasScaleRatio;
117
121
  /**
118
122
  * 将 svg 转化为 canvas
119
123
  * @param svg - svg 元素
@@ -348,24 +348,28 @@ var Snapshot = /** @class */ (function () {
348
348
  return rules;
349
349
  };
350
350
  /**
351
- * 根据浏览器类型获取Canvas尺寸限制
352
- * @returns 包含最大主维度和次维度的对象
351
+ * 根据浏览器对 canvas 的大小限制,计算等比缩放比例
352
+ * - 参考 MDN 最大的画布尺寸:
353
+ * https://developer.mozilla.org/zh-CN/docs/Web/HTML/Reference/Elements/canvas#最大的画布尺寸
354
+ *
355
+ * @param width
356
+ * @param height
353
357
  */
354
- Snapshot.prototype.getCanvasDimensionsByBrowser = function () {
355
- var userAgent = navigator.userAgent;
356
- // 默认值
357
- var maxCanvasDimension = 65535;
358
- var otherMaxCanvasDimension = 4096;
359
- if (userAgent.indexOf('Chrome') !== -1 ||
360
- userAgent.indexOf('Edge') !== -1) {
361
- maxCanvasDimension = 65535;
362
- otherMaxCanvasDimension = 4096;
358
+ Snapshot.prototype.getCanvasScaleRatio = function (width, height) {
359
+ /* 单边最大像素 */
360
+ var maxCanvasSide = 32767;
361
+ /* 最大像素面积 */
362
+ var maxCanvasArea = 268435456;
363
+ var area = width * height;
364
+ if (width <= maxCanvasSide &&
365
+ height <= maxCanvasSide &&
366
+ area <= maxCanvasArea) {
367
+ return 1;
363
368
  }
364
- else if (userAgent.indexOf('Firefox') !== -1) {
365
- maxCanvasDimension = 32767;
366
- otherMaxCanvasDimension = 3814;
367
- }
368
- return { maxCanvasDimension: maxCanvasDimension, otherMaxCanvasDimension: otherMaxCanvasDimension };
369
+ var widthScale = maxCanvasSide / width;
370
+ var heightScale = maxCanvasSide / height;
371
+ var areaScale = maxCanvasArea / area;
372
+ return Math.min(widthScale, heightScale, areaScale);
369
373
  };
370
374
  /**
371
375
  * 将 svg 转化为 canvas
@@ -375,14 +379,14 @@ var Snapshot = /** @class */ (function () {
375
379
  */
376
380
  Snapshot.prototype.getCanvasData = function (svg, toImageOptions) {
377
381
  return __awaiter(this, void 0, void 0, function () {
378
- var width, height, backgroundColor, _a, padding, copy, dpr, base, bbox, layoutCanvas, layout, offsetX, offsetY, graphModel, transformModel, SCALE_X, SCALE_Y, TRANSLATE_X, TRANSLATE_Y, safetyFactor, actualWidth, actualHeight, bboxWidth, bboxHeight, canvas, safetyMargin, _b, maxCanvasDimension, otherMaxCanvasDimension, MAX_CANVAS_DIMENSION, OTHER_MAX_CANVAS_DIMENSION, targetWidth, targetHeight, scaleWidth, scaleHeight, ctx, img, style, foreignObject;
379
- return __generator(this, function (_c) {
380
- switch (_c.label) {
382
+ var width, height, backgroundColor, _a, padding, copy, dpr, base, bbox, layoutCanvas, layout, offsetX, offsetY, graphModel, transformModel, SCALE_X, SCALE_Y, TRANSLATE_X, TRANSLATE_Y, safetyFactor, actualWidth, actualHeight, factorWidth, factorHeight, bboxWidth, bboxHeight, canvas, safetyMargin, targetWidth, targetHeight, scaleRatio, ctx, img, style, foreignObject;
383
+ return __generator(this, function (_b) {
384
+ switch (_b.label) {
381
385
  case 0:
382
386
  width = toImageOptions.width, height = toImageOptions.height, backgroundColor = toImageOptions.backgroundColor, _a = toImageOptions.padding, padding = _a === void 0 ? 40 : _a;
383
387
  return [4 /*yield*/, this.cloneSvg(svg, false)];
384
388
  case 1:
385
- copy = _c.sent();
389
+ copy = _b.sent();
386
390
  dpr = window.devicePixelRatio || 1;
387
391
  if (dpr < 1) {
388
392
  // https://github.com/didi/LogicFlow/issues/1222
@@ -407,62 +411,42 @@ var Snapshot = /** @class */ (function () {
407
411
  graphModel = this.lf.graphModel;
408
412
  transformModel = graphModel.transformModel;
409
413
  SCALE_X = transformModel.SCALE_X, SCALE_Y = transformModel.SCALE_Y, TRANSLATE_X = transformModel.TRANSLATE_X, TRANSLATE_Y = transformModel.TRANSLATE_Y;
410
- safetyFactor = toImageOptions.safetyFactor || 1.1 // 安全系数,增加10%的空间
414
+ safetyFactor = toImageOptions.safetyFactor || 1 // 安全系数,增加10%的空间
411
415
  ;
412
- actualWidth = (bbox.width / SCALE_X) * safetyFactor;
413
- actualHeight = (bbox.height / SCALE_Y) * safetyFactor;
414
- bboxWidth = Math.ceil(actualWidth);
415
- bboxHeight = Math.ceil(actualHeight);
416
+ actualWidth = bbox.width / SCALE_X;
417
+ actualHeight = bbox.height / SCALE_Y;
418
+ factorWidth = actualWidth * (safetyFactor - 1);
419
+ factorHeight = actualHeight * (safetyFactor - 1);
420
+ bboxWidth = Math.ceil(actualWidth + factorWidth);
421
+ bboxHeight = Math.ceil(actualHeight + factorHeight);
416
422
  canvas = document.createElement('canvas');
417
423
  canvas.style.width = "".concat(bboxWidth, "px");
418
424
  canvas.style.height = "".concat(bboxHeight, "px");
419
- safetyMargin = toImageOptions.safetyMargin || 40 // 额外的安全边距
425
+ safetyMargin = toImageOptions.safetyMargin || 0 // 额外的安全边距
420
426
  ;
421
- _b = this.getCanvasDimensionsByBrowser(), maxCanvasDimension = _b.maxCanvasDimension, otherMaxCanvasDimension = _b.otherMaxCanvasDimension;
422
- MAX_CANVAS_DIMENSION = maxCanvasDimension;
423
- OTHER_MAX_CANVAS_DIMENSION = otherMaxCanvasDimension;
424
- targetWidth = bboxWidth * dpr + padding * 2 + safetyMargin;
425
- targetHeight = bboxHeight * dpr + padding * 2 + safetyMargin;
426
- scaleWidth = 1 //宽 缩放
427
- ;
428
- scaleHeight = 1 //高 缩放
429
- ;
430
- // 对宽和高分别进行缩放,如chrome,矩形单边最大宽度不超过65535,如宽超过65535,那么高不能超过4096,否则像素会超,也会显示不出。
431
- if (targetWidth > MAX_CANVAS_DIMENSION &&
432
- targetHeight > OTHER_MAX_CANVAS_DIMENSION) {
433
- scaleWidth = MAX_CANVAS_DIMENSION / targetWidth;
434
- scaleHeight = OTHER_MAX_CANVAS_DIMENSION / targetHeight;
435
- }
436
- else if (targetWidth > OTHER_MAX_CANVAS_DIMENSION &&
437
- targetHeight > MAX_CANVAS_DIMENSION) {
438
- scaleWidth = OTHER_MAX_CANVAS_DIMENSION / targetWidth;
439
- scaleHeight = MAX_CANVAS_DIMENSION / targetHeight;
440
- }
441
- else if (targetWidth > MAX_CANVAS_DIMENSION &&
442
- targetHeight < OTHER_MAX_CANVAS_DIMENSION) {
443
- scaleHeight = scaleWidth = MAX_CANVAS_DIMENSION / targetWidth;
444
- }
445
- else if (targetWidth < OTHER_MAX_CANVAS_DIMENSION &&
446
- targetHeight > MAX_CANVAS_DIMENSION) {
447
- scaleHeight = scaleWidth = MAX_CANVAS_DIMENSION / targetHeight;
448
- }
449
- if (scaleWidth < 1 || scaleHeight < 1) {
450
- targetWidth = Math.floor(targetWidth * scaleWidth);
451
- targetHeight = Math.floor(targetHeight * scaleHeight);
427
+ targetWidth = bboxWidth * dpr;
428
+ targetHeight = bboxHeight * dpr;
429
+ scaleRatio = this.getCanvasScaleRatio(targetWidth, targetHeight);
430
+ if (scaleRatio < 1) {
431
+ targetWidth = Math.floor(targetWidth * scaleRatio);
432
+ targetHeight = Math.floor(targetHeight * scaleRatio);
452
433
  }
453
434
  // 将导出区域移动到左上角,canvas 绘制的时候是从左上角开始绘制的
454
435
  // 在transform矩阵中加入padding值,确保左侧元素不会被截断
455
436
  // 对这个矩阵进行缩放,否则会导致截断
456
437
  ;
457
438
  copy.lastChild.style.transform =
458
- "matrix(".concat(scaleWidth, ", 0, 0, ").concat(scaleHeight, ", ").concat((-offsetX + TRANSLATE_X) * (1 / SCALE_X) * scaleWidth + padding / dpr, ", ").concat((-offsetY + TRANSLATE_Y) * (1 / SCALE_Y) * scaleHeight + padding / dpr, ")");
459
- canvas.width = targetWidth;
460
- canvas.height = targetHeight;
439
+ "matrix(".concat(scaleRatio, ", 0, 0, ").concat(scaleRatio, ", ").concat((-offsetX + TRANSLATE_X) * (1 / SCALE_X) * scaleRatio +
440
+ padding +
441
+ factorWidth / 2 +
442
+ safetyMargin, ", ").concat((-offsetY + TRANSLATE_Y) * (1 / SCALE_Y) * scaleRatio + padding + factorHeight / 2 + safetyMargin, ")");
443
+ canvas.width = targetWidth + (padding + safetyMargin) * 2 * dpr;
444
+ canvas.height = targetHeight + (padding + safetyMargin) * 2 * dpr;
461
445
  ctx = canvas.getContext('2d');
462
446
  if (ctx) {
463
447
  // 清空canvas
464
448
  ctx.clearRect(0, 0, canvas.width, canvas.height);
465
- ctx.scale(dpr * scaleWidth, dpr * scaleHeight);
449
+ ctx.scale(dpr, dpr);
466
450
  // 如果有背景色,设置流程图导出的背景色
467
451
  if (backgroundColor) {
468
452
  ctx.fillStyle = backgroundColor;
@@ -720,9 +704,7 @@ var Snapshot = /** @class */ (function () {
720
704
  var _this = this;
721
705
  return __generator(this, function (_a) {
722
706
  switch (_a.label) {
723
- case 0:
724
- console.log('getSnapshotBase64---------------', backgroundColor, fileType, toImageOptions);
725
- return [4 /*yield*/, this.withExportPreparation(function () { return _this._getSnapshotBase64(backgroundColor, fileType, toImageOptions); }, toImageOptions)];
707
+ case 0: return [4 /*yield*/, this.withExportPreparation(function () { return _this._getSnapshotBase64(backgroundColor, fileType, toImageOptions); }, toImageOptions)];
726
708
  case 1: return [2 /*return*/, _a.sent()];
727
709
  }
728
710
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@logicflow/extension",
3
- "version": "2.2.0-alpha.3",
3
+ "version": "2.2.0-alpha.5",
4
4
  "description": "LogicFlow Extensions",
5
5
  "main": "lib/index.js",
6
6
  "module": "es/index.js",
@@ -20,8 +20,8 @@
20
20
  "author": "Logicflow-Team",
21
21
  "license": "Apache-2.0",
22
22
  "peerDependencies": {
23
- "@logicflow/vue-node-registry": "1.2.0-alpha.3",
24
- "@logicflow/core": "2.2.0-alpha.3"
23
+ "@logicflow/core": "2.2.0-alpha.5",
24
+ "@logicflow/vue-node-registry": "1.2.0-alpha.5"
25
25
  },
26
26
  "dependencies": {
27
27
  "@antv/hierarchy": "^0.6.11",
@@ -32,8 +32,8 @@
32
32
  "preact": "^10.17.1",
33
33
  "rangy": "^1.3.1",
34
34
  "vanilla-picker": "^2.12.3",
35
- "@logicflow/core": "2.2.0-alpha.3",
36
- "@logicflow/vue-node-registry": "1.2.0-alpha.3"
35
+ "@logicflow/core": "2.2.0-alpha.5",
36
+ "@logicflow/vue-node-registry": "1.2.0-alpha.5"
37
37
  },
38
38
  "devDependencies": {
39
39
  "less": "^4.1.1",
@@ -1,9 +1,14 @@
1
1
  import { PolylineEdge, PolylineEdgeModel, h, LogicFlow } from '@logicflow/core'
2
2
  import PointTuple = LogicFlow.PointTuple
3
3
 
4
+ // 方向类型:t=上(top), b=下(bottom), l=左(left), r=右(right),'' 表示未确定
4
5
  type DirectionType = 't' | 'b' | 'l' | 'r' | ''
6
+ // 圆弧所在象限:tl=左上,tr=右上,bl=左下,br=右下,'-' 表示不需要圆弧
5
7
  type ArcQuadrantType = 'tl' | 'tr' | 'bl' | 'br' | '-'
6
8
 
9
+ // 方向组合到圆弧象限的映射。
10
+ // key 由进入方向(dir1)和离开方向(dir2)拼接,例如 'tr' 表示从上(t)到右(r)的拐角。
11
+ // 通过该映射确定在拐点处应该绘制的圆弧象限,用于计算中间控制点。
7
12
  const directionMap: {
8
13
  [key: string]: ArcQuadrantType
9
14
  } = {
@@ -17,19 +22,24 @@ const directionMap: {
17
22
  rt: 'br',
18
23
  }
19
24
 
25
+ // 过滤折线中的共线中间点,减少不必要的顶点
20
26
  function pointFilter(points: number[][]) {
27
+ // 原地修改传入的数组
21
28
  const all = points
29
+ // 从第二个点开始,检查三点是否共线
22
30
  let i = 1
23
31
  while (i < all.length - 1) {
24
32
  const [x, y] = all[i - 1]
25
33
  const [x1, y1] = all[i]
26
34
  const [x2, y2] = all[i + 1]
35
+ // 如果三点在同一条水平或垂直直线上,删除中间点
27
36
  if ((x === x1 && x1 === x2) || (y === y1 && y1 === y2)) {
28
37
  all.splice(i, 1)
29
38
  } else {
30
39
  i++
31
40
  }
32
41
  }
42
+ // 返回精简后的点集
33
43
  return all
34
44
  }
35
45
 
@@ -93,18 +103,23 @@ function getPartialPath(
93
103
  next: PointTuple,
94
104
  radius: number,
95
105
  ): string {
106
+ // 定义误差容错变量
107
+ const tolerance = 1
108
+
96
109
  let dir1: DirectionType = ''
97
110
  let dir2: DirectionType = ''
98
111
 
99
- if (prev[0] === cur[0]) {
112
+ if (Math.abs(prev[0] - cur[0]) <= tolerance) {
113
+ // 垂直方向
100
114
  dir1 = prev[1] > cur[1] ? 't' : 'b'
101
- } else if (prev[1] === cur[1]) {
115
+ } else if (Math.abs(prev[1] - cur[1]) <= tolerance) {
116
+ // 水平方向
102
117
  dir1 = prev[0] > cur[0] ? 'l' : 'r'
103
118
  }
104
119
 
105
- if (cur[0] === next[0]) {
120
+ if (Math.abs(cur[0] - next[0]) <= tolerance) {
106
121
  dir2 = cur[1] > next[1] ? 't' : 'b'
107
- } else if (cur[1] === next[1]) {
122
+ } else if (Math.abs(cur[1] - next[1]) <= tolerance) {
108
123
  dir2 = cur[0] > next[0] ? 'l' : 'r'
109
124
  }
110
125