@labelbee/lb-annotation 1.8.0 → 1.9.0-alpha.2

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.
Files changed (96) hide show
  1. package/README.md +65 -0
  2. package/dist/index.js +20 -9
  3. package/dist/types/constant/tool.d.ts +3 -1
  4. package/dist/types/core/index.d.ts +24 -4
  5. package/dist/types/core/pointCloud/OrbitControls.d.ts +60 -0
  6. package/dist/types/core/pointCloud/PCDLoader.d.ts +11 -0
  7. package/dist/types/core/pointCloud/annotation.d.ts +47 -0
  8. package/dist/types/core/pointCloud/cache.d.ts +16 -0
  9. package/dist/types/core/pointCloud/index.d.ts +325 -0
  10. package/dist/types/core/scheduler.d.ts +71 -0
  11. package/dist/types/core/toolOperation/basicToolOperation.d.ts +20 -2
  12. package/dist/types/core/toolOperation/eventListener.d.ts +2 -2
  13. package/dist/types/core/toolOperation/pointCloud2dOperation.d.ts +80 -0
  14. package/dist/types/core/toolOperation/polygonOperation.d.ts +21 -7
  15. package/dist/types/core/toolOperation/segmentByRect.d.ts +31 -0
  16. package/dist/types/index.d.ts +9 -3
  17. package/dist/types/newCore/CanvasScheduler.d.ts +31 -0
  18. package/dist/types/newCore/index.d.ts +1 -0
  19. package/dist/types/utils/MathUtils.d.ts +6 -0
  20. package/dist/types/utils/tool/CommonToolUtils.d.ts +0 -13
  21. package/dist/types/utils/tool/EnhanceCommonToolUtils.d.ts +20 -0
  22. package/dist/types/utils/tool/PolygonUtils.d.ts +1 -0
  23. package/es/assets/attributeIcon/icon_canvasEdit0.svg.js +3 -0
  24. package/es/assets/attributeIcon/icon_canvasEdit1.svg.js +3 -0
  25. package/es/assets/attributeIcon/icon_canvasEdit2.svg.js +3 -0
  26. package/es/assets/attributeIcon/icon_canvasEdit3.svg.js +3 -0
  27. package/es/assets/attributeIcon/icon_canvasEdit4.svg.js +3 -0
  28. package/es/assets/attributeIcon/icon_canvasEdit5.svg.js +3 -0
  29. package/es/assets/attributeIcon/icon_canvasEdit6.svg.js +3 -0
  30. package/es/assets/attributeIcon/icon_canvasEdit7.svg.js +3 -0
  31. package/es/assets/attributeIcon/icon_canvasEdit8.svg.js +3 -0
  32. package/es/assets/attributeIcon/icon_canvasEdit_miss.svg.js +3 -0
  33. package/es/assets/attributeIcon/icon_editFEN.svg.js +3 -0
  34. package/es/assets/attributeIcon/icon_editHUANG.svg.js +3 -0
  35. package/es/assets/attributeIcon/icon_editLAN.svg.js +3 -0
  36. package/es/assets/attributeIcon/icon_editLV.svg.js +3 -0
  37. package/es/assets/attributeIcon/icon_editQING.svg.js +3 -0
  38. package/es/constant/annotation.js +68 -0
  39. package/es/constant/annotationTask.js +25 -0
  40. package/es/constant/defaultConfig.js +260 -0
  41. package/es/constant/keyCode.js +36 -0
  42. package/es/constant/style.js +67 -0
  43. package/es/constant/tool.js +183 -0
  44. package/es/core/index.js +145 -0
  45. package/es/core/pointCloud/OrbitControls.js +661 -0
  46. package/es/core/pointCloud/PCDLoader.js +238 -0
  47. package/es/core/pointCloud/annotation.js +98 -0
  48. package/es/core/pointCloud/cache.js +42 -0
  49. package/es/core/pointCloud/index.js +771 -0
  50. package/es/core/scheduler.js +184 -0
  51. package/es/core/toolOperation/LineToolOperation.js +1369 -0
  52. package/es/core/toolOperation/TextToolOperation.js +129 -0
  53. package/es/core/toolOperation/ViewOperation.js +410 -0
  54. package/es/core/toolOperation/basicToolOperation.js +817 -0
  55. package/es/core/toolOperation/checkOperation.js +206 -0
  56. package/es/core/toolOperation/eventListener.js +35 -0
  57. package/es/core/toolOperation/measureOperation.js +39 -0
  58. package/es/core/toolOperation/pointCloud2dOperation.js +197 -0
  59. package/es/core/toolOperation/pointOperation.js +603 -0
  60. package/es/core/toolOperation/polygonOperation.js +1201 -0
  61. package/es/core/toolOperation/rectOperation.js +1170 -0
  62. package/es/core/toolOperation/segmentByRect.js +172 -0
  63. package/es/core/toolOperation/tagOperation.js +215 -0
  64. package/es/core/toolOperation/textAttributeClass.js +201 -0
  65. package/es/index.js +25 -14
  66. package/es/locales/constants.js +21 -0
  67. package/es/locales/en_US/message.js +22 -0
  68. package/es/locales/index.js +17 -0
  69. package/es/locales/zh_CN/message.js +22 -0
  70. package/es/newCore/CanvasScheduler.js +31 -0
  71. package/es/utils/ActionsHistory.js +78 -0
  72. package/es/utils/ImgUtils.js +20 -0
  73. package/es/utils/MathUtils.js +283 -0
  74. package/es/utils/VectorUtils.js +23 -0
  75. package/es/utils/tool/AttributeUtils.js +196 -0
  76. package/es/utils/tool/AxisUtils.js +254 -0
  77. package/es/utils/tool/CanvasUtils.js +60 -0
  78. package/es/utils/tool/CommonToolUtils.js +172 -0
  79. package/es/utils/tool/CurrentOperation.js +35 -0
  80. package/es/utils/tool/DblClickEventListener.js +100 -0
  81. package/es/utils/tool/DrawUtils.js +424 -0
  82. package/es/utils/tool/EnhanceCommonToolUtils.js +39 -0
  83. package/es/utils/tool/ImgPosUtils.js +56 -0
  84. package/es/utils/tool/LineToolUtils.js +255 -0
  85. package/es/utils/tool/MarkerUtils.js +9 -0
  86. package/es/utils/tool/PolygonUtils.js +458 -0
  87. package/es/utils/tool/RectUtils.js +153 -0
  88. package/es/utils/tool/RenderDomClass.js +68 -0
  89. package/es/utils/tool/RenderDomUtils.js +29 -0
  90. package/es/utils/tool/StyleUtils.js +29 -0
  91. package/es/utils/tool/TagUtils.js +127 -0
  92. package/es/utils/tool/UnitUtils.js +10 -0
  93. package/es/utils/tool/ZoomUtils.js +70 -0
  94. package/es/utils/tool/polygonTool.js +126 -0
  95. package/es/utils/uuid.js +24 -0
  96. package/package.json +6 -4
@@ -0,0 +1,1369 @@
1
+ import _ from 'lodash';
2
+ import { ELineTypes, ELineColor, EToolName, ETextType } from '../../constant/tool.js';
3
+ import ActionsHistory from '../../utils/ActionsHistory.js';
4
+ import uuid from '../../utils/uuid.js';
5
+ import EKeyCode from '../../constant/keyCode.js';
6
+ import MathUtils from '../../utils/MathUtils.js';
7
+ import { BasicToolOperation } from './basicToolOperation.js';
8
+ import LineToolUtils from '../../utils/tool/LineToolUtils.js';
9
+ import { createSmoothCurvePointsFromPointList, isInPolygon, createSmoothCurvePoints } from '../../utils/tool/polygonTool.js';
10
+ import CommonToolUtils from '../../utils/tool/CommonToolUtils.js';
11
+ import CanvasUtils from '../../utils/tool/CanvasUtils.js';
12
+ import DrawUtils from '../../utils/tool/DrawUtils.js';
13
+ import StyleUtils from '../../utils/tool/StyleUtils.js';
14
+ import AttributeUtils from '../../utils/tool/AttributeUtils.js';
15
+ import TextAttributeClass from './textAttributeClass.js';
16
+
17
+ var __defProp = Object.defineProperty;
18
+ var __defProps = Object.defineProperties;
19
+ var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
20
+ var __getOwnPropSymbols = Object.getOwnPropertySymbols;
21
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
22
+ var __propIsEnum = Object.prototype.propertyIsEnumerable;
23
+ var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, {enumerable: true, configurable: true, writable: true, value}) : obj[key] = value;
24
+ var __spreadValues = (a, b) => {
25
+ for (var prop in b || (b = {}))
26
+ if (__hasOwnProp.call(b, prop))
27
+ __defNormalProp(a, prop, b[prop]);
28
+ if (__getOwnPropSymbols)
29
+ for (var prop of __getOwnPropSymbols(b)) {
30
+ if (__propIsEnum.call(b, prop))
31
+ __defNormalProp(a, prop, b[prop]);
32
+ }
33
+ return a;
34
+ };
35
+ var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
36
+ var EStatus;
37
+ (function(EStatus2) {
38
+ EStatus2[EStatus2["Create"] = 0] = "Create";
39
+ EStatus2[EStatus2["Active"] = 1] = "Active";
40
+ EStatus2[EStatus2["None"] = 2] = "None";
41
+ })(EStatus || (EStatus = {}));
42
+ const SEGMENT_NUMBER = 16;
43
+ const LINE_ORDER_OFFSET = {
44
+ x: 0,
45
+ y: 20
46
+ };
47
+ const POINT_RADIUS = 3;
48
+ const POINT_ACTIVE_RADIUS = 5;
49
+ const INNER_POINT_RADIUS = 2;
50
+ class LineToolOperation extends BasicToolOperation {
51
+ constructor(props) {
52
+ super(props);
53
+ this.drawActivatedLine = (coord, e, hideTempAxis) => {
54
+ const activeLine = _.cloneDeep(this.activeLine);
55
+ if (!activeLine || activeLine.length === 0) {
56
+ return;
57
+ }
58
+ const isActiveLineValid = this.isActiveLineValid();
59
+ let order;
60
+ const existLine = this.selectedID ? this.lineList.find((i) => i.id === this.selectedID) : void 0;
61
+ if (existLine) {
62
+ order = existLine.order;
63
+ } else {
64
+ order = this.nextOrder();
65
+ }
66
+ const color = this.getLineColorByAttribute({attribute: this.defaultAttribute, valid: !!isActiveLineValid});
67
+ activeLine.map((point) => Object.assign(point, this.coordUtils.getRenderCoord(point)));
68
+ this.updateActiveArea();
69
+ this.drawLine(activeLine, coord, color, true, true);
70
+ this.drawLineNumber(activeLine[0], order, color, "", this.defaultAttribute, isActiveLineValid);
71
+ if (coord && this.isCreate) {
72
+ this.arc(coord, POINT_RADIUS, color);
73
+ }
74
+ if (this.cursor && !this.selectedPoint && !hideTempAxis && !this.isShift) {
75
+ this.arc(this.cursor, POINT_ACTIVE_RADIUS, color);
76
+ }
77
+ };
78
+ this.drawHoverPoint = (coord) => {
79
+ if (this.isMousedown) {
80
+ return;
81
+ }
82
+ if (coord && this.isLineSelected) {
83
+ const pointList = this.getPointList(this.activeLine);
84
+ const hoverPoint = this.activeLine.find((i) => LineToolUtils.calcDistance(this.coordUtils.getRenderCoord(i), coord) <= POINT_ACTIVE_RADIUS);
85
+ let nearestPoint;
86
+ if (!hoverPoint && this.activeLine) {
87
+ nearestPoint = this.findNearestPoint(pointList, coord);
88
+ }
89
+ this.hoverPointID = hoverPoint ? hoverPoint.id : void 0;
90
+ this.cursor = hoverPoint ? void 0 : nearestPoint == null ? void 0 : nearestPoint.point;
91
+ }
92
+ };
93
+ this.lineList = [];
94
+ this.activeLine = [];
95
+ this.coordsInsideActiveArea = false;
96
+ this.hoverLineSegmentIndex = -1;
97
+ this.isShift = false;
98
+ this.isReference = false;
99
+ this.drawCurveLine = (ctx, points, config, applyLineWidth = true, isReference = false, hoverLineSegmentIndex) => {
100
+ const pointList = createSmoothCurvePointsFromPointList(points, SEGMENT_NUMBER);
101
+ ctx.save();
102
+ ctx.lineCap = "round";
103
+ ctx.lineJoin = "round";
104
+ ctx.strokeStyle = config.color;
105
+ if (applyLineWidth) {
106
+ ctx.lineWidth = config.lineWidth;
107
+ }
108
+ if (isReference) {
109
+ LineToolUtils.setReferenceCtx(ctx);
110
+ }
111
+ points.forEach(({specialEdge}, index) => {
112
+ const curveLinePoints = pointList.splice(0, SEGMENT_NUMBER + 1);
113
+ ctx.save();
114
+ ctx.beginPath();
115
+ if (hoverLineSegmentIndex === index) {
116
+ ctx.lineWidth = 4;
117
+ }
118
+ curveLinePoints.forEach(({x, y}, pointIndex) => {
119
+ const fn = pointIndex > 0 ? "lineTo" : "moveTo";
120
+ if (specialEdge) {
121
+ LineToolUtils.setSpecialEdgeStyle(ctx);
122
+ }
123
+ ctx[fn](x, y);
124
+ });
125
+ ctx.stroke();
126
+ ctx.restore();
127
+ });
128
+ ctx.restore();
129
+ };
130
+ this.drawLine = (points, cursor, color, showPoint = false, isActive = false) => {
131
+ const pointsToDraw = cursor ? points.concat(cursor) : points;
132
+ const lineConfig = {color, lineWidth: isActive ? 1 : this.lineStyle.lineWidth};
133
+ if (this.isCurve) {
134
+ LineToolUtils.drawCurveLine(this.ctx, pointsToDraw, lineConfig, !showPoint, this.isReference, isActive ? this.hoverLineSegmentIndex : -1);
135
+ } else {
136
+ this.drawStraightLine(pointsToDraw, lineConfig, isActive);
137
+ }
138
+ if (showPoint) {
139
+ points.forEach((point) => {
140
+ var _a, _b;
141
+ const pointID = point.id;
142
+ const pointRadius = pointID && [this.hoverPointID, (_a = this.selectedPoint) == null ? void 0 : _a.id].includes(pointID) ? POINT_ACTIVE_RADIUS : POINT_RADIUS;
143
+ this.arc(point, pointRadius, color);
144
+ if (![this.hoverPointID, (_b = this.selectedPoint) == null ? void 0 : _b.id].includes(pointID)) {
145
+ this.arc(point, INNER_POINT_RADIUS, "white");
146
+ }
147
+ });
148
+ }
149
+ };
150
+ this.drawStraightLine = (points, config, isActive = false) => {
151
+ const {ctx} = this;
152
+ if (ctx) {
153
+ ctx.save();
154
+ ctx.lineCap = "round";
155
+ ctx.lineJoin = "round";
156
+ ctx.strokeStyle = config.color;
157
+ ctx.lineWidth = config.lineWidth;
158
+ if (this.isReference) {
159
+ LineToolUtils.setReferenceCtx(ctx);
160
+ }
161
+ points.forEach((point, index) => {
162
+ ctx.beginPath();
163
+ if (index > 0) {
164
+ const prePoint = points[index - 1];
165
+ ctx.save();
166
+ if (prePoint == null ? void 0 : prePoint.specialEdge) {
167
+ LineToolUtils.setSpecialEdgeStyle(ctx);
168
+ }
169
+ if (isActive && this.hoverLineSegmentIndex + 1 === index) {
170
+ ctx.lineWidth = 4;
171
+ }
172
+ ctx.moveTo(prePoint.x, prePoint.y);
173
+ ctx.lineTo(point.x, point.y);
174
+ ctx.stroke();
175
+ ctx.restore();
176
+ }
177
+ });
178
+ ctx.restore();
179
+ }
180
+ };
181
+ this.drawLines = () => {
182
+ try {
183
+ const lineList = _.cloneDeep(this.attributeFilteredLines);
184
+ if (this.isHidden) {
185
+ return;
186
+ }
187
+ lineList.forEach((line) => {
188
+ if (line.id === this.selectedID) {
189
+ return;
190
+ }
191
+ if (line.pointList) {
192
+ line.pointList.map((i) => Object.assign(i, this.coordUtils.getRenderCoord(i)));
193
+ const {order, label} = line;
194
+ const displayOrder = order;
195
+ const color = line && this.getLineColorByAttribute(line);
196
+ this.drawLine(line.pointList, void 0, color, false);
197
+ this.drawLineNumber(line.pointList[0], displayOrder, color, label, line.attribute, line.valid);
198
+ if (line.id !== this.textEditingID) {
199
+ this.drawLineTextAttribute(line.pointList[1], color, line == null ? void 0 : line.textAttribute);
200
+ }
201
+ }
202
+ });
203
+ } catch (e) {
204
+ console.error(e, "\u7EBF\u6761\u5DE5\u5177\u6570\u636E\u89E3\u6790\u9519\u8BEF");
205
+ this.lineList = [];
206
+ this.clearCanvas();
207
+ }
208
+ };
209
+ this.render = (nextPoint) => {
210
+ super.render();
211
+ this.drawLines();
212
+ this.drawActivatedLine(nextPoint, void 0, true);
213
+ this.renderTextAttribute();
214
+ this.renderCursorLine(this.getLineColor(this.defaultAttribute));
215
+ };
216
+ this.moveLineInPolygon = (offsetX, offsetY) => {
217
+ var _a;
218
+ if (!Array.isArray(this.activeLine)) {
219
+ return false;
220
+ }
221
+ const allPointsInRange = (_a = this.activeLine) == null ? void 0 : _a.every((i) => {
222
+ return this.isInBasicPolygon({x: i.x + offsetX, y: i.y + offsetY});
223
+ });
224
+ if (allPointsInRange) {
225
+ this.lineDragging = true;
226
+ this.moveActiveArea(offsetX, offsetY);
227
+ }
228
+ };
229
+ this.moveLineInRectRange = (offsetX, offsetY, rectHorizontalRange, rectVerticalRange) => {
230
+ if (this.activeArea === void 0) {
231
+ return;
232
+ }
233
+ const {top, left, right, bottom} = this.activeArea;
234
+ const hBoundaries = [left, right].map((i) => _.isNumber(i) ? i + offsetX : 0);
235
+ const vBoundaries = [top, bottom].map((i) => _.isNumber(i) ? i + offsetY : 0);
236
+ const horizontalInRange = left >= 0 && right && MathUtils.isInRange(hBoundaries, rectHorizontalRange);
237
+ const verticalInRange = top >= 0 && bottom && MathUtils.isInRange(vBoundaries, rectVerticalRange);
238
+ const calcOffsetX = horizontalInRange ? offsetX : 0;
239
+ const calcOffsetY = verticalInRange ? offsetY : 0;
240
+ this.lineDragging = true;
241
+ this.moveActiveArea(calcOffsetX, calcOffsetY);
242
+ };
243
+ this.onRightClick = (e) => {
244
+ this.cursor = void 0;
245
+ if (this.isCreate) {
246
+ if (this.isLinePointsNotEnough()) {
247
+ return;
248
+ }
249
+ this.stopLineCreating(true);
250
+ return;
251
+ }
252
+ this.setActiveArea(this.getCoordinate(e), true);
253
+ this.emit("contextmenu");
254
+ };
255
+ this.updateSelectedAttributeAfterHistoryChanged = () => {
256
+ if (this.selectedID) {
257
+ const line = this.lineList.find((i) => i.id === this.selectedID);
258
+ const attribute = line == null ? void 0 : line.attribute;
259
+ if (typeof attribute === "string") {
260
+ this.defaultAttribute = attribute;
261
+ this.updateAttribute(attribute);
262
+ this.render();
263
+ }
264
+ }
265
+ };
266
+ this.onLeftClick = (e) => {
267
+ const coord = this.getCoordinate(e);
268
+ const {lineDragging} = this;
269
+ this.lineDragging = false;
270
+ if (this.isSpaceKey) {
271
+ return;
272
+ }
273
+ if (this.isNone && e.ctrlKey) {
274
+ const hoveredLine = this.findHoverLine(coord);
275
+ if (hoveredLine) {
276
+ this.setInvalidLine(hoveredLine.id);
277
+ }
278
+ return;
279
+ }
280
+ if (this.isLinePointsExceed()) {
281
+ return;
282
+ }
283
+ const nextAxis = this.getNextPoint(e, coord);
284
+ if (this.isCreate || this.isNone) {
285
+ this.setCreatStatusAndAddPoint(nextAxis);
286
+ return;
287
+ }
288
+ if (this.isActive) {
289
+ if (lineDragging) {
290
+ return;
291
+ }
292
+ const isMouseCoordOutsideActiveArea = this.isMouseCoordOutsideActiveArea();
293
+ if (isMouseCoordOutsideActiveArea) {
294
+ this.setNoneStatus(false);
295
+ this.setCreatStatusAndAddPoint(nextAxis);
296
+ return;
297
+ }
298
+ const isSetSpecialLine = e.shiftKey;
299
+ if (isSetSpecialLine) {
300
+ this.updateLineSegmentSpecial(coord);
301
+ return;
302
+ }
303
+ if (this.coordsInsideActiveArea && e.ctrlKey) {
304
+ this.setInvalidLine(this.selectedID);
305
+ return;
306
+ }
307
+ this.addLinePointToActiveLine();
308
+ }
309
+ };
310
+ this.onDblclick = () => {
311
+ };
312
+ this.onKeyUp = (e) => {
313
+ super.onKeyUp(e);
314
+ this.isShift = false;
315
+ this.hoverLineSegmentIndex = -1;
316
+ if (e.keyCode === EKeyCode.Esc) {
317
+ this.stopLineCreating(false);
318
+ return;
319
+ }
320
+ if (this.isActive) {
321
+ if (e.keyCode === EKeyCode.Delete) {
322
+ this.deleteLine();
323
+ return;
324
+ }
325
+ if (e.keyCode === EKeyCode.F) {
326
+ this.setInvalidLine(this.selectedID);
327
+ return;
328
+ }
329
+ if (e.keyCode === EKeyCode.Space) {
330
+ this.continueToEdit();
331
+ return;
332
+ }
333
+ }
334
+ this.keyboardEventWhileLineCreating(e);
335
+ };
336
+ this.onRightDblClick = (e) => {
337
+ super.onRightDblClick(e);
338
+ const coord = this.getCoordinate(e);
339
+ if (this.isActive) {
340
+ const hoverPoint = this.findHoveredPoint(coord);
341
+ if (hoverPoint) {
342
+ this.deleteSelectedLinePoint(hoverPoint.id);
343
+ return;
344
+ }
345
+ this.deleteSelectedLine(coord);
346
+ }
347
+ };
348
+ this.setReference = (isReference) => {
349
+ this.isReference = isReference;
350
+ };
351
+ this.pointsWithinRange = (count) => {
352
+ if (this.lowerLimitPointNum && count < this.lowerLimitPointNum) {
353
+ return false;
354
+ }
355
+ if (this.upperLimitPointNum && count > this.upperLimitPointNum) {
356
+ return false;
357
+ }
358
+ return true;
359
+ };
360
+ this.setLineList = (lineList) => {
361
+ const lengthChanged = lineList.length !== this.lineListLen;
362
+ this.lineList = lineList;
363
+ if (lengthChanged) {
364
+ this.emit("updatePageNumber");
365
+ }
366
+ };
367
+ this.textChange = (v) => {
368
+ if (this.config.textConfigurable === false || !this.selectedID) {
369
+ return;
370
+ }
371
+ this.updateSelectedTextAttribute(v);
372
+ this.emit("selectedChange");
373
+ };
374
+ this.status = 2;
375
+ this.isMousedown = false;
376
+ this.lineDragging = false;
377
+ this.isLineValid = true;
378
+ this.setConfig(props.config);
379
+ this.prevAxis = {
380
+ x: 0,
381
+ y: 0
382
+ };
383
+ this.textEditingID = "";
384
+ this.updateSelectedTextAttribute = this.updateSelectedTextAttribute.bind(this);
385
+ this.getCurrentSelectedData = this.getCurrentSelectedData.bind(this);
386
+ this.actionsHistory = new ActionsHistory();
387
+ this.dependToolConfig = {
388
+ lineType: ELineTypes.Line
389
+ };
390
+ }
391
+ get isLineSelected() {
392
+ return this.selectedID && this.activeLine;
393
+ }
394
+ get selectedLinePoints() {
395
+ return this.activeLine ? this.getPointList(this.activeLine) : [];
396
+ }
397
+ get isCreate() {
398
+ return this.status === 0;
399
+ }
400
+ get isActive() {
401
+ return this.status === 1;
402
+ }
403
+ get isNone() {
404
+ return this.status === 2;
405
+ }
406
+ get isCurve() {
407
+ return this.config.lineType === ELineTypes.Curve;
408
+ }
409
+ get isMultipleColor() {
410
+ return this.config.lineColor === ELineColor.MultiColor;
411
+ }
412
+ get imageSize() {
413
+ if (this.rotate % 180 === 0) {
414
+ return this.basicImgInfo;
415
+ }
416
+ return {
417
+ width: this.basicImgInfo.height,
418
+ height: this.basicImgInfo.width
419
+ };
420
+ }
421
+ get lineListLen() {
422
+ return this.lineList.length;
423
+ }
424
+ get edgeAdsorptionEnabled() {
425
+ return this.edgeAdsorption && !this.isCurve && this.lineListLen > 0;
426
+ }
427
+ get attributeConfigurable() {
428
+ return this.config.attributeConfigurable;
429
+ }
430
+ get isTextConfigurable() {
431
+ return this.config.textConfigurable;
432
+ }
433
+ get isDependPolygon() {
434
+ return this.dependToolName === EToolName.Polygon;
435
+ }
436
+ get isDependRect() {
437
+ return this.dependToolName === EToolName.Rect;
438
+ }
439
+ get isCurrentAttributeLocked() {
440
+ return this.attributeLockList.includes(this.defaultAttribute);
441
+ }
442
+ get attributeFilteredLines() {
443
+ if (this.attributeLockList.length > 0) {
444
+ return this.lineList.filter((v) => this.attributeLockList.includes((v == null ? void 0 : v.attribute) || ""));
445
+ }
446
+ return this.lineList;
447
+ }
448
+ get enableOutOfTarget() {
449
+ return this.config.enableOutOfTarget;
450
+ }
451
+ get showOrder() {
452
+ return this.config.showOrder;
453
+ }
454
+ get edgeAdsorption() {
455
+ return this.config.edgeAdsorption;
456
+ }
457
+ get attributeList() {
458
+ return this.config.attributeList;
459
+ }
460
+ get lowerLimitPointNum() {
461
+ return this.config.lowerLimitPointNum;
462
+ }
463
+ get upperLimitPointNum() {
464
+ return this.config.upperLimitPointNum;
465
+ }
466
+ get textCheckType() {
467
+ return this.config.textCheckType;
468
+ }
469
+ get customFormat() {
470
+ return this.config.customFormat;
471
+ }
472
+ get dataList() {
473
+ return this.lineList;
474
+ }
475
+ get hasActiveLine() {
476
+ return this.activeLine && this.activeLine.length > 0;
477
+ }
478
+ get viewPortLines() {
479
+ const viewPort = CanvasUtils.getViewPort(this.canvas, this.currentPos, this.zoom);
480
+ if (this.isHidden) {
481
+ return [];
482
+ }
483
+ return this.attributeFilteredLines.filter((i) => {
484
+ var _a;
485
+ return (_a = i == null ? void 0 : i.pointList) == null ? void 0 : _a.some((p) => CanvasUtils.inViewPort(p, viewPort));
486
+ });
487
+ }
488
+ get lineStyle() {
489
+ return {
490
+ lineWidth: this.style.width,
491
+ color: this.getLineColor(this.defaultAttribute),
492
+ opacity: this.style.opacity
493
+ };
494
+ }
495
+ get selectedText() {
496
+ var _a, _b;
497
+ return (_b = (_a = this.lineList.find((i) => i.id === this.selectedID)) == null ? void 0 : _a.textAttribute) != null ? _b : "";
498
+ }
499
+ get currentPageResult() {
500
+ return this.lineList;
501
+ }
502
+ updateStatus(status, resetText = false) {
503
+ if (status === this.status) {
504
+ return;
505
+ }
506
+ if (resetText) {
507
+ let defaultText = "";
508
+ if (this.textCheckType === ETextType.Order && this.isTextConfigurable) {
509
+ defaultText = AttributeUtils.getTextAttribute(this.lineList, this.textCheckType);
510
+ }
511
+ this.emit("updateText", defaultText);
512
+ }
513
+ this.status = status;
514
+ this.lineStatusChanged();
515
+ }
516
+ isInBasicPolygon(coord) {
517
+ var _a, _b;
518
+ return isInPolygon(coord, ((_a = this.basicResult) == null ? void 0 : _a.pointList) || [], (_b = this.dependToolConfig) == null ? void 0 : _b.lineType);
519
+ }
520
+ getPolygonPointList() {
521
+ if (!this.basicResult) {
522
+ return [];
523
+ }
524
+ const {pointList} = this.basicResult;
525
+ const {lineType} = this.dependToolConfig;
526
+ return lineType === ELineTypes.Curve ? createSmoothCurvePoints(pointList.reduce((acc, cur) => {
527
+ return [...acc, cur.x, cur.y];
528
+ }, []), 0.5, true, 20) : pointList;
529
+ }
530
+ getNextCoordByRenderCoord(renderCoord) {
531
+ return this.getNextCoordByAbsCoord(this.coordUtils.getAbsCoord(renderCoord));
532
+ }
533
+ getNextCoordByAbsCoord(absCoord) {
534
+ var _a;
535
+ const preAxis = (_a = this.activeLine) == null ? void 0 : _a.slice(-1)[0];
536
+ if (preAxis) {
537
+ return this.coordUtils.getNextCoordByDependTool(absCoord, preAxis);
538
+ }
539
+ return absCoord;
540
+ }
541
+ pointInLine(pointList, checkPoint, scope) {
542
+ if (pointList.filter((i) => i).length < 2) {
543
+ return false;
544
+ }
545
+ return pointList.some((point, index) => {
546
+ if (index === 0) {
547
+ return false;
548
+ }
549
+ const point1 = this.coordUtils.getRenderCoord(pointList[index - 1]);
550
+ const point2 = this.coordUtils.getRenderCoord(point);
551
+ return LineToolUtils.isInLine(checkPoint, point1, point2, scope);
552
+ });
553
+ }
554
+ arc(coord, size = POINT_RADIUS, color) {
555
+ var _a, _b, _c, _d, _e, _f;
556
+ if (this.ctx) {
557
+ const {x, y} = coord;
558
+ (_a = this.ctx) == null ? void 0 : _a.save();
559
+ (_b = this.ctx) == null ? void 0 : _b.beginPath();
560
+ this.ctx.fillStyle = color || this.lineStyle.color;
561
+ (_c = this.ctx) == null ? void 0 : _c.arc(x, y, size, 0, 360);
562
+ (_d = this.ctx) == null ? void 0 : _d.closePath();
563
+ (_e = this.ctx) == null ? void 0 : _e.fill();
564
+ (_f = this.ctx) == null ? void 0 : _f.restore();
565
+ }
566
+ }
567
+ renderActiveArea() {
568
+ if (this.isActive && this.activeArea && this.ctx) {
569
+ const {top, left, right, bottom} = this.activeArea;
570
+ const {x, y} = this.coordUtils.getRenderCoord({x: left, y: top});
571
+ this.ctx.save();
572
+ this.ctx.beginPath();
573
+ this.ctx.strokeStyle = "#B3B8FF";
574
+ this.ctx.rect(x, y, (right - left) * this.zoom, (bottom - top) * this.zoom);
575
+ this.ctx.stroke();
576
+ this.ctx.restore();
577
+ }
578
+ }
579
+ addLinePoint(coord) {
580
+ var _a, _b, _c, _d;
581
+ this.arc(coord);
582
+ (_a = this.activeLine) == null ? void 0 : _a.push(__spreadProps(__spreadValues({}, coord), {id: uuid()}));
583
+ if (((_b = this.activeLine) == null ? void 0 : _b.length) === 1) {
584
+ (_c = this.actionsHistory) == null ? void 0 : _c.initRecord(this.activeLine);
585
+ } else {
586
+ (_d = this.actionsHistory) == null ? void 0 : _d.pushHistory(this.activeLine);
587
+ }
588
+ this.render();
589
+ }
590
+ setCreatStatusAndAddPoint(coord, isRestText = false) {
591
+ this.updateStatus(0, isRestText);
592
+ this.addLinePoint(coord);
593
+ }
594
+ isActiveLineValid() {
595
+ var _a;
596
+ return this.selectedID ? (_a = this.lineList.find((i) => i.id === this.selectedID)) == null ? void 0 : _a.valid : this.isLineValid;
597
+ }
598
+ nextOrder() {
599
+ return this.lineListLen === 0 ? 1 : this.lineList.slice(-1)[0].order + 1;
600
+ }
601
+ getLineColorByAttribute(line, isSelected = false) {
602
+ return StyleUtils.getStrokeAndFill(this.getColor(line.attribute), line.valid, {isSelected}).stroke;
603
+ }
604
+ updateActiveArea() {
605
+ this.activeArea = this.getActiveArea();
606
+ this.renderActiveArea();
607
+ }
608
+ getActiveArea() {
609
+ return this.hasActiveLine ? MathUtils.calcViewportBoundaries(this.activeLine, this.isCurve, SEGMENT_NUMBER, this.zoom) : void 0;
610
+ }
611
+ drawLineNumber(coord, order = 1, color, label = "", attribute, valid = true) {
612
+ var _a, _b, _c;
613
+ if ((this.showOrder || this.attributeConfigurable) && this.ctx) {
614
+ let text = this.showOrder ? order.toString() : `${label}`;
615
+ if (this.attributeConfigurable) {
616
+ const keyForAttribute = attribute ? (_c = (_b = (_a = this.attributeList) == null ? void 0 : _a.find((i) => i.value === attribute)) == null ? void 0 : _b.key) != null ? _c : attribute : "";
617
+ text = [text, `${!valid && keyForAttribute ? "\u65E0\u6548" : ""}${keyForAttribute}`].filter((i) => i).join("_");
618
+ }
619
+ this.drawText(coord, text, color);
620
+ }
621
+ }
622
+ drawLineTextAttribute(coord, color, text) {
623
+ if (coord && text) {
624
+ return this.drawText(coord, text, color, 200);
625
+ }
626
+ }
627
+ drawText(coord, text, color, lineWidth) {
628
+ var _a, _b;
629
+ if (this.ctx) {
630
+ (_a = this.ctx) == null ? void 0 : _a.save();
631
+ this.ctx.font = "italic bold 14px SourceHanSansCN-Regular";
632
+ this.ctx.fillStyle = color;
633
+ this.ctx.strokeStyle = color;
634
+ this.ctx.shadowColor = "rgba(0, 0, 0, 0.6)";
635
+ this.ctx.shadowOffsetY = 2;
636
+ this.ctx.shadowBlur = 4;
637
+ if (lineWidth) {
638
+ DrawUtils.wrapText(this.canvas, text, coord.x - LINE_ORDER_OFFSET.x, coord.y - LINE_ORDER_OFFSET.y, lineWidth);
639
+ } else {
640
+ this.ctx.fillText(text, coord.x - LINE_ORDER_OFFSET.x, coord.y - LINE_ORDER_OFFSET.y);
641
+ }
642
+ (_b = this.ctx) == null ? void 0 : _b.restore();
643
+ }
644
+ }
645
+ moveActiveArea(offsetX, offsetY) {
646
+ if (this.activeArea) {
647
+ this.activeArea = Object.assign(this.activeArea, {
648
+ top: this.activeArea.top + offsetY,
649
+ bottom: this.activeArea.bottom + offsetY,
650
+ right: this.activeArea.right + offsetX,
651
+ left: this.activeArea.left + offsetX
652
+ });
653
+ }
654
+ if (this.activeLine) {
655
+ this.activeLine.map((i) => Object.assign(i, {x: i.x + offsetX, y: i.y + offsetY}));
656
+ this.updateLines();
657
+ }
658
+ }
659
+ findHoveredPoint(coord) {
660
+ if (!this.activeLine) {
661
+ return;
662
+ }
663
+ return this.activeLine.find((i) => {
664
+ const iAxis = this.coordUtils.getRenderCoord(i);
665
+ return LineToolUtils.calcDistance(iAxis, coord) <= POINT_ACTIVE_RADIUS;
666
+ });
667
+ }
668
+ findHoverLine(coord) {
669
+ const line = _.cloneDeep(this.lineList).reverse().find(({pointList}) => {
670
+ const list = pointList ? this.getPointList(pointList) : [];
671
+ const scope = this.getLineWidthScope();
672
+ return list.some((point, index) => {
673
+ if (index === 0) {
674
+ return false;
675
+ }
676
+ const point1 = this.coordUtils.getRenderCoord(point);
677
+ const point2 = this.coordUtils.getRenderCoord(list[index - 1]);
678
+ return LineToolUtils.isInLine(coord, point1, point2, scope);
679
+ });
680
+ });
681
+ return line;
682
+ }
683
+ getAdsorptionPoint(coord) {
684
+ let point;
685
+ let minDistance;
686
+ let snappedPoint;
687
+ _.cloneDeep(this.lineList).reverse().forEach(({pointList, id}) => {
688
+ if (id === this.selectedID || !pointList || (pointList == null ? void 0 : pointList.length) < 2) {
689
+ return;
690
+ }
691
+ const nearestPoint = this.findNearestPoint(pointList, coord);
692
+ if (nearestPoint) {
693
+ if (nearestPoint.minDistance === 0) {
694
+ point = nearestPoint.point;
695
+ return;
696
+ }
697
+ if (minDistance === void 0 || nearestPoint.minDistance < minDistance) {
698
+ point = nearestPoint.point;
699
+ minDistance = nearestPoint.minDistance;
700
+ }
701
+ }
702
+ });
703
+ return snappedPoint || point;
704
+ }
705
+ findNearestPoint(pointList, coord, minLength = 7) {
706
+ let point;
707
+ const minDistance = minLength;
708
+ for (let i = 1; i <= pointList.length - 1; i++) {
709
+ const point1 = this.coordUtils.getRenderCoord(pointList[i]);
710
+ const point2 = this.coordUtils.getRenderCoord(pointList[i - 1]);
711
+ const {length, footPoint} = MathUtils.getFootOfPerpendicular(coord, point1, point2);
712
+ const twoPointDistance1 = LineToolUtils.calcTwoPointDistance(point1, coord);
713
+ const twoPointDistance2 = LineToolUtils.calcTwoPointDistance(point2, coord);
714
+ if (twoPointDistance1 <= minLength * 2) {
715
+ point = point1;
716
+ minLength = 0;
717
+ break;
718
+ }
719
+ if (twoPointDistance2 <= minLength * 2) {
720
+ point = point2;
721
+ minLength = 0;
722
+ break;
723
+ }
724
+ if (length < minLength) {
725
+ point = footPoint;
726
+ minLength = length;
727
+ }
728
+ }
729
+ return point ? {point, minDistance} : void 0;
730
+ }
731
+ getPointList(pointList) {
732
+ return this.isCurve ? createSmoothCurvePointsFromPointList(pointList, SEGMENT_NUMBER) : pointList;
733
+ }
734
+ moveSelectedLine(coord) {
735
+ const offsetX = (coord.x - this.prevAxis.x) / this.zoom;
736
+ const offsetY = (coord.y - this.prevAxis.y) / this.zoom;
737
+ if (this.enableOutOfTarget) {
738
+ this.lineDragging = true;
739
+ this.moveActiveArea(offsetX, offsetY);
740
+ return;
741
+ }
742
+ if (this.isDependPolygon) {
743
+ this.moveLineInPolygon(offsetX, offsetY);
744
+ return;
745
+ }
746
+ let rectHorizontalRange = [0, this.imageSize.width];
747
+ let rectVerticalRange = [0, this.imageSize.height];
748
+ if (this.isDependRect) {
749
+ const {x, y, width, height} = this.basicResult;
750
+ rectHorizontalRange = [x, x + width];
751
+ rectVerticalRange = [y, y + height];
752
+ }
753
+ this.moveLineInRectRange(offsetX, offsetY, rectHorizontalRange, rectVerticalRange);
754
+ }
755
+ moveSelectPoint(coord) {
756
+ if (!this.selectedPoint) {
757
+ return;
758
+ }
759
+ const offsetX = coord.x - this.prevAxis.x;
760
+ const offsetY = coord.y - this.prevAxis.y;
761
+ const newX = (this.selectedPoint ? this.selectedPoint.x : 0) + offsetX / this.zoom;
762
+ const newY = (this.selectedPoint ? this.selectedPoint.y : 0) + offsetY / this.zoom;
763
+ const pointPosition = {
764
+ x: newX,
765
+ y: newY
766
+ };
767
+ Object.assign(this.selectedPoint, this.getNextCoordByAbsCoord(pointPosition));
768
+ this.updateLines();
769
+ this.render();
770
+ }
771
+ getCoordByConfig(e, coord) {
772
+ var _a;
773
+ const isVH = !!e.shiftKey;
774
+ const disabledAdsorb = e.altKey;
775
+ if (((_a = this.activeLine) == null ? void 0 : _a.length) > 0 && isVH) {
776
+ const lastPoint = this.activeLine.slice(-1)[0];
777
+ return LineToolUtils.getVHPoint(lastPoint, coord, this.coordUtils.getAbsCoord(coord), this.coordUtils.getRenderCoord(lastPoint));
778
+ }
779
+ if (this.edgeAdsorptionEnabled && !disabledAdsorb) {
780
+ return this.getAdsorptionPoint(coord);
781
+ }
782
+ return coord;
783
+ }
784
+ getNextPoint(e, nextPoint) {
785
+ const newPoint = this.getCoordByConfig(e, nextPoint) || nextPoint;
786
+ return this.enableOutOfTarget ? newPoint : this.getNextCoordByRenderCoord(newPoint);
787
+ }
788
+ lineHover() {
789
+ this.render();
790
+ }
791
+ mouseMoveHandler(e) {
792
+ const coord = this.getCoordinate(e);
793
+ const isLeftClick = e.which === 1;
794
+ if (this.isCreate) {
795
+ if (this.hasActiveLine) {
796
+ this.renderNextPoint(e, coord);
797
+ }
798
+ return;
799
+ }
800
+ if (this.isNone) {
801
+ this.lineHover();
802
+ if (this.edgeAdsorptionEnabled && !e.altKey) {
803
+ const edgeAdsorptionPoint = this.getAdsorptionPoint(coord);
804
+ if (edgeAdsorptionPoint) {
805
+ this.arc(edgeAdsorptionPoint);
806
+ }
807
+ }
808
+ }
809
+ if (this.isActive) {
810
+ if (this.isMousedown && isLeftClick) {
811
+ if (this.selectedPoint) {
812
+ this.moveSelectPoint(coord);
813
+ return;
814
+ }
815
+ if (this.coordsInsideActiveArea) {
816
+ this.moveSelectedLine(coord);
817
+ this.drawActivatedLine(void 0, void 0, true);
818
+ return;
819
+ }
820
+ }
821
+ this.drawHoverPoint(coord);
822
+ this.render();
823
+ }
824
+ }
825
+ onMouseMove(e) {
826
+ if (super.onMouseMove(e) || this.forbidMouseOperation || !this.imgInfo) {
827
+ return;
828
+ }
829
+ const coord = this.getCoordinate(e);
830
+ this.mouseMoveHandler(e);
831
+ this.prevAxis = coord;
832
+ }
833
+ setActiveArea(coord, outsideCancel = false) {
834
+ const activeLine = this.findHoverLine(coord);
835
+ if (activeLine) {
836
+ const index = this.lineList.findIndex((i) => i.id === (activeLine == null ? void 0 : activeLine.id));
837
+ const area = MathUtils.calcViewportBoundaries((activeLine == null ? void 0 : activeLine.pointList) || [], this.isCurve, SEGMENT_NUMBER, this.zoom);
838
+ const line = this.lineList[index];
839
+ this.updateStatus(1);
840
+ this.setActiveLine(line.pointList);
841
+ this.setSelectedLineID(line.id);
842
+ this.activeArea = area;
843
+ this.updateLineAttributes(line);
844
+ } else if (outsideCancel) {
845
+ this.setNoneStatus();
846
+ }
847
+ this.render();
848
+ }
849
+ setActiveLineByID(id) {
850
+ const line = this.lineList.find((i) => i.id === id);
851
+ if (line) {
852
+ const area = MathUtils.calcViewportBoundaries((line == null ? void 0 : line.pointList) || [], this.isCurve, SEGMENT_NUMBER, this.zoom);
853
+ this.updateStatus(1);
854
+ this.setActiveLine(line.pointList);
855
+ this.setSelectedLineID(line.id);
856
+ this.activeArea = area;
857
+ this.updateLineAttributes(line);
858
+ }
859
+ this.render();
860
+ }
861
+ setActiveLine(pointList) {
862
+ this.activeLine = pointList ? _.cloneDeep(pointList) : void 0;
863
+ }
864
+ historyChanged(funcName) {
865
+ const enableKeyName = `${funcName}Enabled`;
866
+ if (this.isCreate) {
867
+ if (this.actionsHistory && this.actionsHistory[enableKeyName]) {
868
+ const record = this.actionsHistory && this.actionsHistory[funcName]();
869
+ this.setActiveLine(record);
870
+ this.render();
871
+ }
872
+ return;
873
+ }
874
+ if (this.history && this.history[enableKeyName]) {
875
+ const currentHistory = this.history[funcName]();
876
+ const activeLine = currentHistory == null ? void 0 : currentHistory.find((i) => i.id === this.selectedID);
877
+ this.lineList = currentHistory;
878
+ if (this.selectedID && activeLine) {
879
+ this.setActiveLine(activeLine == null ? void 0 : activeLine.pointList);
880
+ } else {
881
+ this.setNoneStatus();
882
+ }
883
+ this.render();
884
+ }
885
+ this.emit("dataUpdated", this.lineList);
886
+ }
887
+ undo() {
888
+ this.historyChanged("undo");
889
+ this.updateSelectedAttributeAfterHistoryChanged();
890
+ }
891
+ redo() {
892
+ this.historyChanged("redo");
893
+ this.updateSelectedAttributeAfterHistoryChanged();
894
+ }
895
+ isCoordInsideTarget(coord) {
896
+ if (this.isDependPolygon) {
897
+ return this.isInBasicPolygon(coord);
898
+ }
899
+ if (this.isDependRect) {
900
+ const {x, y, width, height} = this.basicResult;
901
+ const rectHorizontalRange = [x, x + width];
902
+ const rectVerticalRange = [y, y + height];
903
+ return MathUtils.isInRange(coord.x, rectHorizontalRange) && MathUtils.isInRange(coord.y, rectVerticalRange);
904
+ }
905
+ return MathUtils.isInRange(coord.x, [0, this.imageSize.width]) && MathUtils.isInRange(coord.y, [0, this.imageSize.height]);
906
+ }
907
+ getPointInsertIndex(coord, scope) {
908
+ if (coord && this.activeLine) {
909
+ const pointList = this.getPointList(this.activeLine);
910
+ if (this.activeLine.length === 2) {
911
+ return 1;
912
+ }
913
+ return this.activeLine.findIndex((i, index) => {
914
+ if (index > 0) {
915
+ const straightLinePoints = this.activeLine ? this.activeLine[index - 1] : void 0;
916
+ const points = this.isCurve ? pointList.slice((index - 1) * (SEGMENT_NUMBER + 1), index * (SEGMENT_NUMBER + 1)) : [straightLinePoints, i];
917
+ return this.pointInLine(points, coord, scope || this.getLineWidthScope());
918
+ }
919
+ return false;
920
+ });
921
+ }
922
+ return -1;
923
+ }
924
+ getLineWidthScope() {
925
+ return this.lineStyle.lineWidth;
926
+ }
927
+ isMouseCoordOutsideActiveArea() {
928
+ return !this.coordsInsideActiveArea && !this.selectedPoint;
929
+ }
930
+ isLinePointsExceed() {
931
+ return this.isCreate && this.activeLine && this.upperLimitPointNum && ~~this.upperLimitPointNum <= this.activeLine.length;
932
+ }
933
+ isLinePointsNotEnough() {
934
+ var _a;
935
+ return this.activeLine && ((_a = this.activeLine) == null ? void 0 : _a.length) < this.lowerLimitPointNum;
936
+ }
937
+ updateLineSegmentSpecial(coord) {
938
+ const specialEdgeIndex = this.getPointInsertIndex(coord, 2) - 1;
939
+ if (specialEdgeIndex > -1) {
940
+ const pointData = this.activeLine[specialEdgeIndex];
941
+ pointData.specialEdge = !pointData.specialEdge;
942
+ this.hoverLineSegmentIndex = -1;
943
+ this.render();
944
+ }
945
+ }
946
+ addLinePointToActiveLine() {
947
+ var _a;
948
+ const insertIndex = this.getPointInsertIndex(this.cursor);
949
+ const pointsWithInRange = this.pointsWithinRange(this.activeLine.length + 1);
950
+ if (this.cursor && insertIndex > -1 && pointsWithInRange) {
951
+ this.activeLine.splice(insertIndex, 0, __spreadProps(__spreadValues({}, this.coordUtils.getAbsCoord(this.cursor)), {id: uuid()}));
952
+ this.updateLines();
953
+ (_a = this.history) == null ? void 0 : _a.pushHistory(this.lineList);
954
+ this.render();
955
+ this.cursor = void 0;
956
+ }
957
+ }
958
+ onMouseDown(e) {
959
+ if (super.onMouseDown(e) || this.forbidMouseOperation || !this.imgInfo) {
960
+ return;
961
+ }
962
+ const coord = this.getCoordinate(e);
963
+ this.isMousedown = true;
964
+ this.prevAxis = coord;
965
+ if (e.which === 3) {
966
+ this.cursor = void 0;
967
+ return;
968
+ }
969
+ this.selectedPoint = this.findHoveredPoint(coord);
970
+ this.coordsInsideActiveArea = this.isActive && this.activeArea ? LineToolUtils.inArea(this.activeArea, this.coordUtils.getAbsCoord(coord)) : false;
971
+ this.lineDragging = false;
972
+ }
973
+ lineHasChanged() {
974
+ const line = this.lineList.find((i) => i.id === this.selectedID);
975
+ return line ? JSON.stringify(line.pointList) !== JSON.stringify(this.activeLine) : false;
976
+ }
977
+ updateLines() {
978
+ const line = this.lineList.find((i) => i.id === this.selectedID);
979
+ if (line) {
980
+ line.pointList = _.cloneDeep(this.activeLine);
981
+ this.emit("dataUpdated", this.lineList);
982
+ }
983
+ }
984
+ onMouseUp(e) {
985
+ const reset = () => {
986
+ this.isMousedown = false;
987
+ this.hoverPointID = void 0;
988
+ this.cursor = void 0;
989
+ this.selectedPoint = void 0;
990
+ };
991
+ this.hoverPointID = void 0;
992
+ if (super.onMouseUp(e) || this.forbidMouseOperation || !this.imgInfo) {
993
+ reset();
994
+ return;
995
+ }
996
+ if (e.which === 1) {
997
+ this.onLeftClick(e);
998
+ }
999
+ if (e.which === 3) {
1000
+ this.onRightClick(e);
1001
+ }
1002
+ reset();
1003
+ }
1004
+ isTextValid(text) {
1005
+ return AttributeUtils.textAttributeValidate(this.textCheckType, this.customFormat, text);
1006
+ }
1007
+ createLineData() {
1008
+ const id = uuid();
1009
+ const newLine = {
1010
+ pointList: _.cloneDeep(this.activeLine),
1011
+ id,
1012
+ valid: this.isLineValid,
1013
+ order: this.nextOrder()
1014
+ };
1015
+ newLine.attribute = this.defaultAttribute;
1016
+ return newLine;
1017
+ }
1018
+ stopLineCreating(isAppend = true) {
1019
+ var _a, _b, _c, _d;
1020
+ const setActiveAfterCreating = this.selectedID ? true : !!this.isTextConfigurable;
1021
+ let selectedID;
1022
+ if (isAppend) {
1023
+ if (this.selectedID) {
1024
+ const line = this.lineList.find((i) => i.id === this.selectedID);
1025
+ selectedID = this.selectedID;
1026
+ if (line) {
1027
+ line.pointList = _.cloneWith(this.activeLine);
1028
+ if (!_.isEqual(line.pointList, (_a = this.history) == null ? void 0 : _a.pushHistory(this.lineList))) {
1029
+ (_b = this.history) == null ? void 0 : _b.pushHistory(this.lineList);
1030
+ }
1031
+ }
1032
+ } else if (this.isCreate && this.activeLine && this.activeLine.length > 1) {
1033
+ const newLine = this.createLineData();
1034
+ selectedID = newLine.id;
1035
+ this.setLineList([...this.lineList, newLine]);
1036
+ (_c = this.history) == null ? void 0 : _c.pushHistory(this.lineList);
1037
+ }
1038
+ }
1039
+ if (setActiveAfterCreating) {
1040
+ this.setActiveStatus(selectedID);
1041
+ } else {
1042
+ this.setNoneStatus();
1043
+ }
1044
+ (_d = this.actionsHistory) == null ? void 0 : _d.empty();
1045
+ this.emit("dataUpdated", this.lineList);
1046
+ this.render();
1047
+ }
1048
+ setActiveStatus(id) {
1049
+ const line = id ? this.lineList.find((i) => i.id === id) : void 0;
1050
+ if (line) {
1051
+ const pointList = line == null ? void 0 : line.pointList;
1052
+ this.updateStatus(1);
1053
+ this.setActiveLine(pointList);
1054
+ this.setSelectedLineID(id);
1055
+ } else {
1056
+ this.setNoneStatus();
1057
+ }
1058
+ }
1059
+ setNoneStatus(updateStatus = true) {
1060
+ if (updateStatus) {
1061
+ this.updateStatus(2);
1062
+ }
1063
+ this.activeLine = [];
1064
+ this.setSelectedLineID(void 0);
1065
+ this.activeArea = void 0;
1066
+ this.isLineValid = true;
1067
+ this.cursor = void 0;
1068
+ }
1069
+ setKeyDownStatus(e, value) {
1070
+ this.isShift = value != null ? value : e.keyCode === EKeyCode.Shift;
1071
+ }
1072
+ continueToEdit() {
1073
+ var _a;
1074
+ this.updateStatus(0);
1075
+ this.cursor = void 0;
1076
+ (_a = this.actionsHistory) == null ? void 0 : _a.pushHistory(this.activeLine);
1077
+ this.render();
1078
+ }
1079
+ setInvalidLineOnCreating(e) {
1080
+ if (this.selectedID && e.keyCode !== EKeyCode.Ctrl || !this.isCreate) {
1081
+ return;
1082
+ }
1083
+ const valid = !e.ctrlKey;
1084
+ if (this.selectedID) {
1085
+ this.setInvalidLine(this.selectedID, valid, false);
1086
+ } else {
1087
+ this.isLineValid = valid;
1088
+ }
1089
+ }
1090
+ onKeyDown(e) {
1091
+ super.onKeyDown(e);
1092
+ this.setKeyDownStatus(e);
1093
+ if (e.keyCode === EKeyCode.Z && !e.ctrlKey) {
1094
+ this.toggleIsHide();
1095
+ }
1096
+ if (e.keyCode === EKeyCode.Shift) {
1097
+ this.render();
1098
+ }
1099
+ if (e.keyCode === EKeyCode.Tab) {
1100
+ e.preventDefault();
1101
+ this.selectToNextLine(e);
1102
+ return;
1103
+ }
1104
+ if (this.isCreate) {
1105
+ this.keyboardEventWhileLineCreating(e);
1106
+ }
1107
+ if (this.config.attributeConfigurable) {
1108
+ const keyCode2Attribute = AttributeUtils.getAttributeByKeycode(e.keyCode, this.config.attributeList);
1109
+ if (keyCode2Attribute !== void 0) {
1110
+ this.setDefaultAttribute(keyCode2Attribute);
1111
+ }
1112
+ }
1113
+ }
1114
+ selectToNextLine(e) {
1115
+ const nextSelectedLine = CommonToolUtils.getNextSelectedRectIDByEvent(this.viewPortLines.map((i) => {
1116
+ var _a, _b, _c, _d;
1117
+ return __spreadProps(__spreadValues({}, i), {
1118
+ x: (_b = (_a = i.pointList[0]) == null ? void 0 : _a.x) != null ? _b : 0,
1119
+ y: (_d = (_c = i.pointList[0]) == null ? void 0 : _c.y) != null ? _d : 0
1120
+ });
1121
+ }), e, this.selectedID);
1122
+ if (nextSelectedLine) {
1123
+ this.setActiveLineByID(nextSelectedLine.id);
1124
+ }
1125
+ }
1126
+ keyboardEventWhileLineCreating(e) {
1127
+ if (!this.isCreate) {
1128
+ return;
1129
+ }
1130
+ if (e.keyCode === EKeyCode.Ctrl) {
1131
+ this.setInvalidLineOnCreating(e);
1132
+ }
1133
+ if ([EKeyCode.Shift, EKeyCode.Alt].includes(e.keyCode)) {
1134
+ this.renderNextPoint(e, this.prevAxis);
1135
+ }
1136
+ }
1137
+ renderNextPoint(e, coord) {
1138
+ const nextPoint = this.coordUtils.getRenderCoord(this.getNextPoint(e, coord));
1139
+ this.render(nextPoint);
1140
+ }
1141
+ deleteSelectedLine(coord) {
1142
+ const boundary = MathUtils.calcViewportBoundaries(this.activeLine, this.isCurve, SEGMENT_NUMBER, this.zoom);
1143
+ const axisOnArea = LineToolUtils.inArea(boundary, this.coordUtils.getAbsCoord(coord));
1144
+ if (axisOnArea) {
1145
+ this.deleteLine();
1146
+ }
1147
+ }
1148
+ deleteSelectedLinePoint(selectedID) {
1149
+ var _a;
1150
+ const pointsWithinRange = this.pointsWithinRange(this.activeLine.length - 1);
1151
+ if (pointsWithinRange && selectedID) {
1152
+ this.setActiveLine(this.activeLine.filter((i) => i.id !== selectedID));
1153
+ this.updateLines();
1154
+ (_a = this.history) == null ? void 0 : _a.pushHistory(this.lineList);
1155
+ }
1156
+ this.cursor = void 0;
1157
+ this.render();
1158
+ }
1159
+ deleteLine() {
1160
+ var _a;
1161
+ this.lineList = this.lineList.filter((i) => i.id !== this.selectedID);
1162
+ (_a = this.history) == null ? void 0 : _a.pushHistory(this.lineList);
1163
+ this.setNoneStatus();
1164
+ this.emit("dataUpdated", this.lineList);
1165
+ this.render();
1166
+ }
1167
+ setInvalidLine(id, valid, isRender = true) {
1168
+ var _a;
1169
+ const line = this.lineList.find((i) => i.id === id);
1170
+ if (line) {
1171
+ line.valid = valid !== void 0 ? valid : !line.valid;
1172
+ (_a = this.history) == null ? void 0 : _a.pushHistory(this.lineList);
1173
+ if (isRender) {
1174
+ this.render();
1175
+ }
1176
+ }
1177
+ }
1178
+ empty() {
1179
+ var _a, _b;
1180
+ this.lineList = [];
1181
+ this.setNoneStatus();
1182
+ this.selectedPoint = void 0;
1183
+ (_a = this.actionsHistory) == null ? void 0 : _a.empty();
1184
+ (_b = this.history) == null ? void 0 : _b.init();
1185
+ this.emit("dataUpdated", this.lineList);
1186
+ this.render();
1187
+ }
1188
+ setAttribute(attribute) {
1189
+ var _a;
1190
+ if (this.attributeConfigurable) {
1191
+ this.defaultAttribute = attribute;
1192
+ this.setLineAttribute("attribute", attribute);
1193
+ if (this.selectedID) {
1194
+ (_a = this.history) == null ? void 0 : _a.pushHistory(this.lineList);
1195
+ }
1196
+ }
1197
+ }
1198
+ setTextAttribute(text) {
1199
+ var _a;
1200
+ if (this.isTextConfigurable) {
1201
+ this.setLineAttribute("textAttribute", text);
1202
+ (_a = this.history) == null ? void 0 : _a.applyAttribute(this.selectedID, "textAttribute", text);
1203
+ }
1204
+ }
1205
+ setLineAttribute(key, value, id) {
1206
+ const targetID = id || this.selectedID;
1207
+ if (targetID) {
1208
+ const line = this.lineList.find((i) => i.id === targetID);
1209
+ if (line) {
1210
+ line[key] = value;
1211
+ }
1212
+ }
1213
+ this.render();
1214
+ }
1215
+ updateAttribute(attribute) {
1216
+ this.emit("updateAttribute", attribute);
1217
+ }
1218
+ updateLineAttributes(line) {
1219
+ var _a;
1220
+ if (this.attributeConfigurable && line) {
1221
+ const attribute = (line == null ? void 0 : line.attribute) || "";
1222
+ this.defaultAttribute = attribute;
1223
+ this.updateAttribute(attribute);
1224
+ }
1225
+ if (this.isTextConfigurable && line) {
1226
+ const text = (line == null ? void 0 : line.textAttribute) || "";
1227
+ this.updateTextAttribute(text);
1228
+ }
1229
+ (_a = this.history) == null ? void 0 : _a.updateHistory(this.lineList);
1230
+ }
1231
+ lineStatusChanged() {
1232
+ this.emit("lineStatusChanged", {
1233
+ status: this.status,
1234
+ selectedLineID: this.selectedID
1235
+ });
1236
+ }
1237
+ updateTextAttribute(text) {
1238
+ if (this.selectedID) {
1239
+ const line = this.lineList.find((i) => i.id === this.selectedID);
1240
+ if (line) {
1241
+ line.textAttribute = text;
1242
+ }
1243
+ }
1244
+ this.emit("updateText", text);
1245
+ }
1246
+ saveData() {
1247
+ this.stopLineCreating();
1248
+ this.setNoneStatus();
1249
+ this.render();
1250
+ }
1251
+ setTextEditingID(id) {
1252
+ this.textEditingID = id;
1253
+ this.render();
1254
+ }
1255
+ setSelectedLineID(id) {
1256
+ var _a, _b;
1257
+ if (this.selectedID === id) {
1258
+ return;
1259
+ }
1260
+ const oldID = this.selectedID;
1261
+ if (id !== oldID && oldID) {
1262
+ (_a = this._textAttributeInstance) == null ? void 0 : _a.changeSelected();
1263
+ }
1264
+ if (!id) {
1265
+ (_b = this._textAttributeInstance) == null ? void 0 : _b.clearTextAttribute();
1266
+ }
1267
+ this.selectedID = id;
1268
+ this.emit("selectedChange");
1269
+ }
1270
+ attributeLockListChange(attributeLockList) {
1271
+ this.attributeLockList = attributeLockList;
1272
+ this.render();
1273
+ }
1274
+ setResult(lineList) {
1275
+ this.setNoneStatus();
1276
+ this.setLineList(lineList);
1277
+ this.render();
1278
+ }
1279
+ setConfig(config) {
1280
+ super.setConfig(config);
1281
+ }
1282
+ toggleIsHide() {
1283
+ this.setIsHidden(!this.isHidden);
1284
+ this.render();
1285
+ }
1286
+ clearCanvas() {
1287
+ super.clearCanvas();
1288
+ }
1289
+ clearResult() {
1290
+ this.setResult([]);
1291
+ this.setSelectedLineID(void 0);
1292
+ this.render();
1293
+ }
1294
+ exportData() {
1295
+ return [this.lineList, this.basicImgInfo];
1296
+ }
1297
+ setDefaultAttribute(attribute = "") {
1298
+ var _a;
1299
+ if (this.attributeConfigurable) {
1300
+ this.defaultAttribute = attribute;
1301
+ this.setLineAttribute("attribute", attribute);
1302
+ if (this.selectedID) {
1303
+ (_a = this.history) == null ? void 0 : _a.pushHistory(this.lineList);
1304
+ }
1305
+ this.emit("changeAttributeSidebar");
1306
+ }
1307
+ }
1308
+ getCurrentSelectedData() {
1309
+ var _a, _b;
1310
+ const valid = this.isActiveLineValid();
1311
+ const attribute = this.defaultAttribute;
1312
+ const toolColor = this.getColor(attribute);
1313
+ const color = valid ? toolColor == null ? void 0 : toolColor.valid.stroke : toolColor == null ? void 0 : toolColor.invalid.stroke;
1314
+ const textAttribute = (_b = (_a = this.lineList.find((i) => i.id === this.selectedID)) == null ? void 0 : _a.textAttribute) != null ? _b : "";
1315
+ return {
1316
+ color,
1317
+ textAttribute
1318
+ };
1319
+ }
1320
+ renderTextAttribute() {
1321
+ var _a, _b, _c, _d;
1322
+ if (!this.ctx || !this.activeLine || ((_a = this.activeLine) == null ? void 0 : _a.length) < 2 || this.isCreate) {
1323
+ return;
1324
+ }
1325
+ const valid = this.isActiveLineValid();
1326
+ const attribute = this.defaultAttribute;
1327
+ const {x, y} = this.activeLine[1];
1328
+ const coordinate = this.coordUtils.getRenderCoord({x, y});
1329
+ const toolColor = this.getColor(attribute);
1330
+ const color = valid ? toolColor == null ? void 0 : toolColor.valid.stroke : toolColor == null ? void 0 : toolColor.invalid.stroke;
1331
+ const textAttribute = (_c = (_b = this.lineList.find((i) => i.id === this.selectedID)) == null ? void 0 : _b.textAttribute) != null ? _c : "";
1332
+ if (!this._textAttributeInstance) {
1333
+ this._textAttributeInstance = new TextAttributeClass({
1334
+ container: this.container,
1335
+ icon: this.getTextIconSvg(attribute),
1336
+ color,
1337
+ getCurrentSelectedData: this.getCurrentSelectedData,
1338
+ updateSelectedTextAttribute: this.updateSelectedTextAttribute
1339
+ });
1340
+ }
1341
+ if (this._textAttributeInstance && !((_d = this._textAttributeInstance) == null ? void 0 : _d.isExit)) {
1342
+ this._textAttributeInstance.appendToContainer();
1343
+ }
1344
+ this._textAttributeInstance.update(`${textAttribute}`, {
1345
+ left: coordinate.x,
1346
+ top: coordinate.y,
1347
+ color
1348
+ });
1349
+ this._textAttributeInstance.updateIcon(this.getTextIconSvg(attribute));
1350
+ }
1351
+ getTextIconSvg(attribute = "") {
1352
+ return AttributeUtils.getTextIconSvg(attribute, this.config.attributeList, this.config.attributeConfigurable, this.baseIcon);
1353
+ }
1354
+ updateSelectedTextAttribute(newTextAttribute) {
1355
+ if (this._textAttributeInstance && newTextAttribute && this.selectedID) {
1356
+ let textAttribute = newTextAttribute;
1357
+ const textAttributeInvalid = !AttributeUtils.textAttributeValidate(this.config.textCheckType, "", textAttribute);
1358
+ if (textAttributeInvalid) {
1359
+ this.emit("messageError", AttributeUtils.getErrorNotice(this.config.textCheckType, this.lang));
1360
+ textAttribute = "";
1361
+ }
1362
+ this.setTextAttribute(textAttribute);
1363
+ this.emit("updateTextAttribute");
1364
+ this.render();
1365
+ }
1366
+ }
1367
+ }
1368
+
1369
+ export { INNER_POINT_RADIUS, LINE_ORDER_OFFSET, POINT_ACTIVE_RADIUS, POINT_RADIUS, SEGMENT_NUMBER, LineToolOperation as default };