@labelbee/lb-annotation 1.14.0-alpha.17 → 1.14.0-alpha.18
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/core/toolOperation/LineToolOperation.js +1 -1
- package/dist/core/toolOperation/polygonOperation.js +1 -1
- package/dist/types/core/toolOperation/polygonOperation.d.ts +1 -0
- package/es/core/toolOperation/LineToolOperation.js +1 -1
- package/es/core/toolOperation/polygonOperation.js +1 -1
- package/package.json +1 -1
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var _=require("lodash"),tool=require("../../constant/tool.js"),ActionsHistory=require("../../utils/ActionsHistory.js"),uuid=require("../../utils/uuid.js"),keyCode=require("../../constant/keyCode.js"),MathUtils=require("../../utils/MathUtils.js"),basicToolOperation=require("./basicToolOperation.js"),LineToolUtils=require("../../utils/tool/LineToolUtils.js"),polygonTool=require("../../utils/tool/polygonTool.js"),CommonToolUtils=require("../../utils/tool/CommonToolUtils.js"),CanvasUtils=require("../../utils/tool/CanvasUtils.js"),DrawUtils=require("../../utils/tool/DrawUtils.js"),StyleUtils=require("../../utils/tool/StyleUtils.js"),AttributeUtils=require("../../utils/tool/AttributeUtils.js"),textAttributeClass=require("./textAttributeClass.js"),Selection=require("./Selection.js"),index=require("../../locales/index.js"),constants=require("../../locales/constants.js");function _interopDefaultLegacy(a){return a&&typeof a=="object"&&"default"in a?a:{default:a}}var ___default=_interopDefaultLegacy(_),__defProp=Object.defineProperty,__defProps=Object.defineProperties,__getOwnPropDescs=Object.getOwnPropertyDescriptors,__getOwnPropSymbols=Object.getOwnPropertySymbols,__hasOwnProp=Object.prototype.hasOwnProperty,__propIsEnum=Object.prototype.propertyIsEnumerable,__defNormalProp=(a,e,t)=>e in a?__defProp(a,e,{enumerable:!0,configurable:!0,writable:!0,value:t}):a[e]=t,__spreadValues=(a,e)=>{for(var t in e||(e={}))__hasOwnProp.call(e,t)&&__defNormalProp(a,t,e[t]);if(__getOwnPropSymbols)for(var t of __getOwnPropSymbols(e))__propIsEnum.call(e,t)&&__defNormalProp(a,t,e[t]);return a},__spreadProps=(a,e)=>__defProps(a,__getOwnPropDescs(e)),EStatus;(function(a){a[a.Create=0]="Create",a[a.Active=1]="Active",a[a.None=2]="None"})(EStatus||(EStatus={}));const SEGMENT_NUMBER=16,LINE_ORDER_OFFSET={x:0,y:20},POINT_RADIUS=3,POINT_ACTIVE_RADIUS=5,INNER_POINT_RADIUS=2;class LineToolOperation extends basicToolOperation.BasicToolOperation{constructor(e){super(e);this.drawActivatedLine=(t,i,s)=>{const n=this.isCreate?___default.default.cloneDeep([{pointList:this.activeLine,order:this.nextOrder(),attribute:this.defaultAttribute,valid:this.isLineValid}]):___default.default.cloneDeep(this.selectedLines),o=this.isActiveLineValid();n.forEach(r=>{if((r==null?void 0:r.pointList)&&(r.pointList=r.pointList.map(l=>Object.assign(l,{actual:{x:l.x,y:l.y}},this.coordUtils.getRenderCoord(l)))),!r.pointList)return;const h=this.getLineColorByAttribute({attribute:r.attribute,valid:r.valid});return this.selectedLines.length>0&&(this.drawLineLength(r.pointList,h),this.renderActiveArea(MathUtils.default.calcViewportBoundaries(r.pointList,this.isCurve,SEGMENT_NUMBER))),this.drawLine(r.pointList,t,h,!0,!0),this.drawLineNumber(r.pointList[0],r.order,h,"",this.defaultAttribute,o),t&&this.isCreate&&this.arc(t,POINT_RADIUS,h),this.cursor&&!this.selectedPoint&&!s&&!this.isShift&&this.arc(this.cursor,POINT_ACTIVE_RADIUS,h),r})},this.drawHoverPoint=t=>{if(!this.isMousedown&&t&&this.selectedID){const i=this.getPointList(this.activeLine),s=this.activeLine.find(o=>LineToolUtils.default.calcDistance(this.coordUtils.getRenderCoord(o),t)<=POINT_ACTIVE_RADIUS);let n;!s&&this.activeLine&&(n=this.findNearestPoint(i,t)),this.hoverPointID=s?s.id:void 0,this.cursor=s||n==null?void 0:n.point}},this.updatedLine={id:"",valid:!1,order:0},this.toolName="lineTool",this.lineList=[],this.activeLine=[],this.coordsInsideActiveArea=!1,this.hoverLineSegmentIndex=-1,this.isShift=!1,this.isReference=!1,this.drawCurveLine=(t,i,s,n=!0,o=!1,r)=>{const h=polygonTool.createSmoothCurvePointsFromPointList(i,SEGMENT_NUMBER);t.save(),t.lineCap="round",t.lineJoin="round",t.strokeStyle=s.color,n&&(t.lineWidth=s.lineWidth),o&&LineToolUtils.default.setReferenceCtx(t),i.forEach(({specialEdge:l},d)=>{const u=h.splice(0,SEGMENT_NUMBER+1);t.save(),t.beginPath(),r===d&&(t.lineWidth=4),u.forEach(({x:c,y:f},v)=>{const L=v>0?"lineTo":"moveTo";l&&LineToolUtils.default.setSpecialEdgeStyle(t),t[L](c,f)}),t.stroke(),t.restore()}),t.restore()},this.drawLine=(t,i,s,n=!1,o=!1)=>{const r=i?t.concat(i):t,h={color:s,lineWidth:o?1:this.lineStyle.lineWidth};this.isCurve?LineToolUtils.default.drawCurveLine(this.ctx,r,h,!n,this.isReference,o?this.hoverLineSegmentIndex:-1):this.drawStraightLine(r,h,o),n&&t.forEach(l=>{var d,u;const c=l.id,f=c&&[this.hoverPointID,(d=this.selectedPoint)==null?void 0:d.id].includes(c)?POINT_ACTIVE_RADIUS:POINT_RADIUS;this.arc(l,f,s),[this.hoverPointID,(u=this.selectedPoint)==null?void 0:u.id].includes(c)||this.arc(l,INNER_POINT_RADIUS,"white")})},this.drawStraightLine=(t,i,s=!1)=>{const{ctx:n}=this;n&&(n.save(),n.lineCap="round",n.lineJoin="round",n.strokeStyle=i.color,n.lineWidth=i.lineWidth,this.isReference&&LineToolUtils.default.setReferenceCtx(n),t.forEach((o,r)=>{if(n.beginPath(),r>0){const h=t[r-1];n.save(),(h==null?void 0:h.specialEdge)&&LineToolUtils.default.setSpecialEdgeStyle(n),s&&this.hoverLineSegmentIndex+1===r&&(n.lineWidth=4),n.moveTo(h.x,h.y),n.lineTo(o.x,o.y),n.stroke(),n.restore()}}),n.restore())},this.drawLines=()=>{try{const t=___default.default.cloneDeep(this.attributeFilteredLines);if(this.isHidden)return;t.forEach(i=>{if(!this.selection.isIdSelected(i.id)&&i.pointList){i.pointList.map(h=>Object.assign(h,{actual:{x:h.x,y:h.y}},this.coordUtils.getRenderCoord(h)));const{order:s,label:n}=i,o=s,r=i&&this.getLineColorByAttribute(i);this.drawLine(i.pointList,void 0,r,!1),this.drawLineNumber(i.pointList[0],o,r,n,i.attribute,i.valid),i.id!==this.textEditingID&&(this.drawLineTextAttribute(i.pointList[1],r,i==null?void 0:i.textAttribute),this.drawLineLength(i.pointList,r))}})}catch(t){console.error(t,"\u7EBF\u6761\u5DE5\u5177\u6570\u636E\u89E3\u6790\u9519\u8BEF"),this.lineList=[],this.clearCanvas()}},this.render=t=>{super.render(),this.drawLines(),this.drawActivatedLine(t,void 0,!0),this.renderTextAttribute(),this.renderCursorLine(this.getLineColor(this.defaultAttribute))},this.moveLineInPolygon=(t,i)=>{var s;if(!Array.isArray(this.activeLine))return!1;((s=this.activeLine)==null?void 0:s.every(o=>this.isInBasicPolygon({x:o.x+t,y:o.y+i})))&&(this.lineDragging=!0,this.moveActiveArea(t,i))},this.getSelectedLinesArea=()=>MathUtils.default.calcViewportBoundaries(this.selectedLines.reduce((t,i)=>(i==null?void 0:i.pointList)?t.concat(...i==null?void 0:i.pointList):t,[])),this.moveLineInRectRange=(t,i,s,n)=>{if(this.selectedLines.length===0)return;const{top:o,left:r,right:h,bottom:l}=this.getSelectedLinesArea(),d=[r,h].map(g=>___default.default.isNumber(g)?g+t:0),u=[o,l].map(g=>___default.default.isNumber(g)?g+i:0),c=r>=0&&h&&MathUtils.default.isInRange(d,s),f=o>=0&&l&&MathUtils.default.isInRange(u,n),v=c?t:0,L=f?i:0;this.lineDragging=!0,this.moveActiveArea(v,L)},this.onRightClick=t=>{if(this.cursor=void 0,this.isCreate){if(this.isLinePointsNotEnough())return;if(LineToolUtils.default.lineLengthSum(this.activeLine||[])<this.minLength)return this.emit("messageInfo",`${index.getMessagesByLocale(constants.EMessage.MinLengthLimitErrorNotice,this.lang)}${this.minLength}`),!0;this.stopLineCreating(!0);return}const i=this.findHoverLine(this.getCoordinate(t));this.setSelectedLineID(i==null?void 0:i.id,t.ctrlKey),this.emit("contextmenu")},this.updateSelectedAttributeAfterHistoryChanged=()=>{if(this.selectedIDs.length>0){const t=this.lineList.find(s=>s.id===this.selectedIDs[0]),i=t==null?void 0:t.attribute;typeof i=="string"&&(this.defaultAttribute=i,this.updateAttribute(i),this.render())}},this.onLeftClick=t=>{const i=this.getCoordinate(t),{lineDragging:s}=this;if(this.lineDragging=!1,this.isSpaceKey)return;if(this.isNone&&t.ctrlKey){const o=this.findHoverLine(i);o&&this.setInvalidLine(o.id);return}if(this.isLinePointsExceed())return;const n=this.getNextPoint(t,i);if(this.isCreate||this.isNone){this.setCreateStatusAndAddPoint(n);return}if(this.isActive){if(s)return;if(this.isMouseCoordOutsideActiveArea()){this.setNoneStatus(!1),this.setCreateStatusAndAddPoint(n);return}if(t.shiftKey){this.updateLineSegmentSpecial(i);return}this.coordsInsideActiveArea&&t.ctrlKey&&this.setInvalidLine(this.selectedID)}},this.onDblclick=()=>{},this.onKeyUp=t=>{if(super.onKeyUp(t),this.isShift=!1,this.hoverLineSegmentIndex=-1,!this.selection.triggerKeyboardEvent(t,this.setLineList)){if(t.keyCode===keyCode.Esc){this.stopLineCreating(!1);return}if(this.selectedLine){if(t.key==="Delete"){this.deleteLine();return}if(t.key==="f"){this.setInvalidLine(this.selectedID);return}if(t.key===" "){this.continueToEdit();return}}this.keyboardEventWhileLineCreating(t)}},this.onRightDblClick=t=>{super.onRightDblClick(t);const i=this.getCoordinate(t);if(this.isActive){const s=this.findHoveredPoint(i);if(s){this.deleteSelectedLinePoint(s.id);return}this.deleteSelectedLine(i)}},this.setReference=t=>{this.isReference=t},this.pointsWithinRange=t=>!(this.lowerLimitPointNum&&t<this.lowerLimitPointNum||this.upperLimitPointNum&&t>this.upperLimitPointNum),this.setLineList=t=>{const i=t.length!==this.lineListLen;this.lineList=t,i&&this.emit("updatePageNumber")},this.textChange=t=>{this.config.textConfigurable===!1||!this.selectedID||(this.updateSelectedTextAttribute(t),this.emit("selectedChange"))},this.status=2,this.isMousedown=!1,this.lineDragging=!1,this.isLineValid=!0,this.setConfig(e.config),this.prevAxis={x:0,y:0},this.textEditingID="",this.updateSelectedTextAttribute=this.updateSelectedTextAttribute.bind(this),this.getCurrentSelectedData=this.getCurrentSelectedData.bind(this),this.actionsHistory=new ActionsHistory,this.selection=new Selection(this),this.dependToolConfig={lineType:tool.ELineTypes.Line}}get selectedLines(){return this.lineList.filter(e=>this.selection.isIdSelected(e.id))}get isLineSelected(){return this.selectedID&&this.activeLine}get selectedLine(){return this.lineList.find(e=>e.id===this.selectedID)}get isCreate(){return this.status===0}get isActive(){return this.selectedLines.length>0}get isNone(){return!this.isCreate&&this.selectedIDs.length===0}get isCurve(){return this.config.lineType===tool.ELineTypes.Curve}get isMultipleColor(){return this.config.lineColor===tool.ELineColor.MultiColor}get imageSize(){return this.rotate%180==0?this.basicImgInfo:{width:this.basicImgInfo.height,height:this.basicImgInfo.width}}get lineListLen(){return this.lineList.length}get edgeAdsorptionEnabled(){return this.edgeAdsorption&&!this.isCurve&&this.lineListLen>0}get attributeConfigurable(){return this.config.attributeConfigurable}get isTextConfigurable(){return this.config.textConfigurable}get isDependPolygon(){return this.dependToolName===tool.EToolName.Polygon}get isDependRect(){return this.dependToolName===tool.EToolName.Rect}get isCurrentAttributeLocked(){return this.attributeLockList.includes(this.defaultAttribute)}get attributeFilteredLines(){return this.attributeLockList.length>0?this.lineList.filter(e=>this.attributeLockList.includes((e==null?void 0:e.attribute)||"")):this.lineList}get selectedIDs(){return this.selection.selectedIDs}get selectedID(){return this.selection.selectedID}get enableOutOfTarget(){return this.config.enableOutOfTarget||this.config.outOfTarget||this.config.drawOutsideTarget}get showOrder(){var e;return(e=this.config.showOrder)!=null?e:this.config.isShowOrder}get edgeAdsorption(){return this.config.edgeAdsorption}get attributeList(){return this.config.attributeList}get lowerLimitPointNum(){return this.config.lowerLimitPointNum}get minLength(){var e;return((e=this.config)==null?void 0:e.minLength)||1}get upperLimitPointNum(){return this.config.upperLimitPointNum}get textCheckType(){return this.config.textCheckType}get customFormat(){return this.config.customFormat}get dataList(){return this.lineList}get hasActiveLine(){return this.activeLine&&this.activeLine.length>0}get viewPortLines(){const e=CanvasUtils.getViewPort(this.canvas,this.currentPos,this.zoom);return this.isHidden?[]:this.attributeFilteredLines.filter(t=>{var i;return(i=t==null?void 0:t.pointList)==null?void 0:i.some(s=>CanvasUtils.inViewPort(s,e))})}get lineStyle(){return{lineWidth:this.style.width,color:this.getLineColor(this.defaultAttribute),opacity:this.style.opacity}}get selectedText(){var e,t;return(t=(e=this.lineList.find(i=>i.id===this.selectedID))==null?void 0:e.textAttribute)!=null?t:""}get currentPageResult(){return this.lineList}updateStatus(e,t=!1){if(e!==this.status){if(t){let i="";this.textCheckType===tool.ETextType.Order&&this.isTextConfigurable&&(i=AttributeUtils.default.getTextAttribute(this.lineList,this.textCheckType)),this.emit("updateText",i)}this.status=e,this.lineStatusChanged()}}isInBasicPolygon(e){var t,i;return polygonTool.isInPolygon(e,((t=this.basicResult)==null?void 0:t.pointList)||[],(i=this.dependToolConfig)==null?void 0:i.lineType)}getPolygonPointList(){if(!this.basicResult)return[];const{pointList:e}=this.basicResult,{lineType:t}=this.dependToolConfig;return t===tool.ELineTypes.Curve?polygonTool.createSmoothCurvePoints(e.reduce((i,s)=>[...i,s.x,s.y],[]),.5,!0,20):e}getNextCoordByRenderCoord(e){return this.getNextCoordByAbsCoord(this.coordUtils.getAbsCoord(e))}getNextCoordByAbsCoord(e){var t;const i=(t=this.activeLine)==null?void 0:t.slice(-1)[0];return i?this.coordUtils.getNextCoordByDependTool(e,i):e}pointInLine(e,t,i){return e.filter(s=>s).length<2?!1:e.some((s,n)=>{if(n===0)return!1;const o=this.coordUtils.getRenderCoord(e[n-1]),r=this.coordUtils.getRenderCoord(s);return LineToolUtils.default.isInLine(t,o,r,i)})}arc(e,t=POINT_RADIUS,i){var s,n,o,r,h,l;if(this.ctx){const{x:d,y:u}=e;(s=this.ctx)==null||s.save(),(n=this.ctx)==null||n.beginPath(),this.ctx.fillStyle=i||this.lineStyle.color,(o=this.ctx)==null||o.arc(d,u,t,0,360),(r=this.ctx)==null||r.closePath(),(h=this.ctx)==null||h.fill(),(l=this.ctx)==null||l.restore()}}renderActiveArea({top:e,left:t,right:i,bottom:s}){this.ctx&&(this.ctx.save(),this.ctx.beginPath(),this.ctx.strokeStyle="#B3B8FF",this.ctx.rect(t,e,i-t,s-e),this.ctx.stroke(),this.ctx.restore())}addLinePoint(e){var t,i,s,n;this.arc(e),(t=this.activeLine)==null||t.push(__spreadProps(__spreadValues({},e),{id:uuid()})),((i=this.activeLine)==null?void 0:i.length)===1?(s=this.actionsHistory)==null||s.initRecord(this.activeLine):(n=this.actionsHistory)==null||n.pushHistory(this.activeLine),this.render()}setCreateStatusAndAddPoint(e,t=!1){this.updateStatus(0,t),this.addLinePoint(e)}isActiveLineValid(){var e;return this.selectedID?(e=this.lineList.find(t=>t.id===this.selectedID))==null?void 0:e.valid:this.isLineValid}nextOrder(){return this.lineListLen===0?1:this.lineList.slice(-1)[0].order+1}getLineColorByAttribute(e,t=!1){return StyleUtils.getStrokeAndFill(this.getColor(e.attribute),e.valid,{isSelected:t}).stroke}drawLineNumber(e,t=1,i,s="",n,o=!0){var r,h,l;if((this.showOrder||this.attributeConfigurable)&&this.ctx){let d=this.showOrder?t.toString():`${s}`;if(this.attributeConfigurable){const u=n?(l=(h=(r=this.attributeList)==null?void 0:r.find(c=>c.value===n))==null?void 0:h.key)!=null?l:n:"";d=[d,`${!o&&u?"\u65E0\u6548":""}${u}`].filter(c=>c).join("_")}this.drawText(e,d,i)}}drawLineTextAttribute(e,t,i){if(e&&i)return this.drawText(e,i,t,200)}drawLineLength(e,t){var i;if(((i=this.config)==null?void 0:i.showLineLength)&&e){const s=e.reduce((o,r,h)=>h<=0||!e[h-1].actual||!r.actual?o:o+LineToolUtils.default.calcDistance(e[h-1].actual,r.actual),0),n=e[e.length-1];this.drawText(n,`l = ${s.toFixed(2)}`,t)}}drawText(e,t,i,s){var n,o;this.ctx&&((n=this.ctx)==null||n.save(),this.ctx.font="italic bold 14px SourceHanSansCN-Regular",this.ctx.fillStyle=i,this.ctx.strokeStyle=i,this.ctx.shadowColor="rgba(0, 0, 0, 0.6)",this.ctx.shadowOffsetY=2,this.ctx.shadowBlur=4,s?DrawUtils.wrapText(this.canvas,t,e.x-LINE_ORDER_OFFSET.x,e.y-LINE_ORDER_OFFSET.y,s):this.ctx.fillText(t,e.x-LINE_ORDER_OFFSET.x,e.y-LINE_ORDER_OFFSET.y),(o=this.ctx)==null||o.restore())}moveActiveArea(e,t){this.selectedLines.length>0&&(this.selectedLines.forEach(i=>{var s;(s=i.pointList)==null||s.forEach(n=>Object.assign(n,{x:n.x+e,y:n.y+t}))}),this.render()),this.emit("dataUpdated",this.lineList,this.selectedIDs)}findHoveredPoint(e){if(!!this.activeLine)return this.activeLine.find(t=>{const i=this.coordUtils.getRenderCoord(t);return LineToolUtils.default.calcDistance(i,e)<=POINT_ACTIVE_RADIUS})}findHoverLine(e){return ___default.default.cloneDeep(this.lineList).reverse().find(({pointList:i})=>{const s=i?this.getPointList(i):[],n=this.getLineWidthScope();return s.some((o,r)=>{if(r===0)return!1;const h=this.coordUtils.getRenderCoord(o),l=this.coordUtils.getRenderCoord(s[r-1]);return LineToolUtils.default.isInLine(e,h,l,n)})})}getAdsorptionPoint(e){let t,i,s;return ___default.default.cloneDeep(this.lineList).reverse().forEach(({pointList:n,id:o})=>{if(o===this.selectedID||!n||(n==null?void 0:n.length)<2)return;const r=this.findNearestPoint(n,e);if(r){if(r.minDistance===0){t=r.point;return}(i===void 0||r.minDistance<i)&&(t=r.point,i=r.minDistance)}}),s||t}findNearestPoint(e,t,i=7){let s;const n=i;for(let o=1;o<=e.length-1;o++){const r=this.coordUtils.getRenderCoord(e[o]),h=this.coordUtils.getRenderCoord(e[o-1]),{length:l,footPoint:d}=MathUtils.default.getFootOfPerpendicular(t,r,h),u=LineToolUtils.default.calcTwoPointDistance(r,t),c=LineToolUtils.default.calcTwoPointDistance(h,t);if(u<=i*2){s=r,i=0;break}if(c<=i*2){s=h,i=0;break}l<i&&(s=d,i=l)}return s?{point:s,minDistance:n}:void 0}getPointList(e){return this.isCurve?polygonTool.createSmoothCurvePointsFromPointList(e,SEGMENT_NUMBER):e}moveSelectedLine(e){const t=(e.x-this.prevAxis.x)/this.zoom,i=(e.y-this.prevAxis.y)/this.zoom;if(this.enableOutOfTarget){this.lineDragging=!0,this.moveActiveArea(t,i);return}if(this.isDependPolygon){this.moveLineInPolygon(t,i);return}let s=[0,this.imageSize.width],n=[0,this.imageSize.height];if(this.isDependRect){const{x:o,y:r,width:h,height:l}=this.basicResult;s=[o,o+h],n=[r,r+l]}this.moveLineInRectRange(t,i,s,n)}moveSelectPoint(e){if(!this.selectedPoint)return;const t=e.x-this.prevAxis.x,i=e.y-this.prevAxis.y,s=(this.selectedPoint?this.selectedPoint.x:0)+t/this.zoom,n=(this.selectedPoint?this.selectedPoint.y:0)+i/this.zoom,o={x:s,y:n};Object.assign(this.selectedPoint,this.getNextCoordByAbsCoord(o)),this.updateLines(),this.render()}getCoordByConfig(e,t){var i;const s=!!e.shiftKey,n=e.altKey;if(((i=this.activeLine)==null?void 0:i.length)>0&&s){const o=this.activeLine.slice(-1)[0];return LineToolUtils.default.getVHPoint(o,t,this.coordUtils.getAbsCoord(t),this.coordUtils.getRenderCoord(o))}return this.edgeAdsorptionEnabled&&!n?this.getAdsorptionPoint(t):t}getNextPoint(e,t){const i=this.getCoordByConfig(e,t)||t;return this.enableOutOfTarget?this.coordUtils.getAbsCoord(i):this.getNextCoordByRenderCoord(i)}lineHover(){this.render()}mouseMoveHandler(e){const t=this.getCoordinate(e),i=e.which===1;if(this.isCreate){this.hasActiveLine&&this.renderNextPoint(e,t);return}if(this.isNone&&(this.lineHover(),this.edgeAdsorptionEnabled&&!e.altKey)){const s=this.getAdsorptionPoint(t);s&&this.arc(s)}if(this.isActive){if(this.isMousedown&&i){if(this.selectedPoint){this.moveSelectPoint(t);return}if(this.coordsInsideActiveArea){this.moveSelectedLine(t),this.drawActivatedLine(void 0,void 0,!0);return}}this.drawHoverPoint(t),this.render()}}onMouseMove(e){if(super.onMouseMove(e)||this.forbidMouseOperation||!this.imgInfo)return;const t=this.getCoordinate(e);this.mouseMoveHandler(e),this.prevAxis=t}setActiveLine(e){this.activeLine=e?___default.default.cloneDeep(e):void 0}historyChanged(e){const t=`${e}Enabled`;if(this.isCreate){if(this.actionsHistory&&this.actionsHistory[t]){const i=this.actionsHistory&&this.actionsHistory[e]();this.setActiveLine(i),this.render()}return}if(this.history&&this.history[t]){const i=this.history[e](),s=i==null?void 0:i.find(n=>n.id===this.selectedID);this.lineList=i,this.selectedID&&s?this.setActiveLine(s==null?void 0:s.pointList):this.setNoneStatus(),this.render()}this.emit("dataUpdated",this.lineList)}undo(){this.historyChanged("undo"),this.updateSelectedAttributeAfterHistoryChanged()}redo(){this.historyChanged("redo"),this.updateSelectedAttributeAfterHistoryChanged()}isCoordInsideTarget(e){if(this.isDependPolygon)return this.isInBasicPolygon(e);if(this.isDependRect){const{x:t,y:i,width:s,height:n}=this.basicResult,o=[t,t+s],r=[i,i+n];return MathUtils.default.isInRange(e.x,o)&&MathUtils.default.isInRange(e.y,r)}return MathUtils.default.isInRange(e.x,[0,this.imageSize.width])&&MathUtils.default.isInRange(e.y,[0,this.imageSize.height])}getPointInsertIndex(e,t){if(e&&this.activeLine){const i=this.getPointList(this.activeLine);return this.activeLine.length===2?1:this.activeLine.findIndex((s,n)=>{if(n>0){const o=this.activeLine?this.activeLine[n-1]:void 0,r=this.isCurve?i.slice((n-1)*(SEGMENT_NUMBER+1),n*(SEGMENT_NUMBER+1)):[o,s];return this.pointInLine(r,e,t||this.getLineWidthScope())}return!1})}return-1}getLineWidthScope(){return this.lineStyle.lineWidth}isMouseCoordOutsideActiveArea(){return!this.coordsInsideActiveArea&&!this.selectedPoint}isLinePointsExceed(){return this.isCreate&&this.activeLine&&this.upperLimitPointNum&&~~this.upperLimitPointNum<=this.activeLine.length}isLinePointsNotEnough(){var e;return this.activeLine&&((e=this.activeLine)==null?void 0:e.length)<this.lowerLimitPointNum}updateLineSegmentSpecial(e){const t=this.getPointInsertIndex(e,2)-1;if(t>-1){const i=this.activeLine[t];i.specialEdge=!i.specialEdge,this.hoverLineSegmentIndex=-1,this.render()}}addLinePointToActiveLine(){var e;const t=this.getPointInsertIndex(this.cursor),i=this.pointsWithinRange(this.activeLine.length+1);this.cursor&&t>-1&&i&&(this.activeLine.splice(t,0,__spreadProps(__spreadValues({},this.coordUtils.getAbsCoord(this.cursor)),{id:uuid()})),this.updateLines(),(e=this.history)==null||e.pushHistory(this.lineList),this.render(),this.cursor=void 0)}isCoordOnSelectedArea(e){return this.selectedLines.some(t=>LineToolUtils.default.inArea(MathUtils.default.calcViewportBoundaries(t.pointList),this.coordUtils.getAbsCoord(e)))}onMouseDown(e){if(super.onMouseDown(e)||this.forbidMouseOperation||!this.imgInfo)return;const t=this.getCoordinate(e);if(this.isMousedown=!0,this.prevAxis=t,e.which===3){this.cursor=void 0;return}this.selectedPoint=this.findHoveredPoint(t),this.coordsInsideActiveArea=this.selectedLines.length>0?this.isCoordOnSelectedArea(t):!1,this.lineDragging=!1}lineHasChanged(){const e=this.lineList.find(t=>t.id===this.selectedID);return e?JSON.stringify(e.pointList)!==JSON.stringify(this.activeLine):!1}updateLines(){const e=this.lineList.find(t=>t.id===this.selectedID);e&&(e.pointList=___default.default.cloneDeep(this.activeLine),this.updatedLine=e,this.emit("dataUpdated",this.lineList))}onMouseUp(e){const t=()=>{this.isMousedown=!1,this.hoverPointID=void 0,this.cursor=void 0,this.selectedPoint=void 0};if(this.hoverPointID=void 0,super.onMouseUp(e)||this.forbidMouseOperation||!this.imgInfo){t();return}e.which===1&&this.onLeftClick(e),e.which===3&&this.onRightClick(e),t()}isTextValid(e){return AttributeUtils.default.textAttributeValidate(this.textCheckType,this.customFormat,e)}createLineData(){const e=uuid(),t={pointList:___default.default.cloneDeep(this.activeLine),id:e,valid:this.isLineValid,order:this.nextOrder()};return t.attribute=this.defaultAttribute,t}stopLineCreating(e=!0){var t,i,s,n;const o=this.selectedID?!0:!!this.isTextConfigurable;let r;if(e){if(this.selectedID){const h=this.lineList.find(l=>l.id===this.selectedID);r=this.selectedID,h&&(h.pointList=___default.default.cloneWith(this.activeLine),___default.default.isEqual(h.pointList,(t=this.history)==null?void 0:t.pushHistory(this.lineList))||(i=this.history)==null||i.pushHistory(this.lineList))}else if(this.isCreate&&this.activeLine&&this.activeLine.length>1){const h=this.createLineData();r=h.id,this.setLineList([...this.lineList,h]),(s=this.history)==null||s.pushHistory(this.lineList)}}this.setNoneStatus(),o&&(this.activeLine=[],this.setSelectedLineID(r)),(n=this.actionsHistory)==null||n.empty(),this.emit("dataUpdated",this.lineList,this.selectedIDs),this.render()}setNoneStatus(e=!0){e&&this.updateStatus(2),this.activeLine=[],this.setSelectedLineID(void 0),this.isLineValid=!0,this.cursor=void 0}setKeyDownStatus(e,t){this.isShift=t!=null?t:e.keyCode===keyCode.Shift}continueToEdit(){var e,t;((e=this.selectedLine)==null?void 0:e.pointList)&&(this.updateStatus(0),this.cursor=void 0,this.setActiveLine(this.selectedLine.pointList),(t=this.actionsHistory)==null||t.pushHistory(this.activeLine),this.render())}setInvalidLineOnCreating(e){if(this.selectedID&&e.keyCode!==keyCode.Ctrl||!this.isCreate)return;const t=!e.ctrlKey;this.selectedID?this.setInvalidLine(this.selectedID,t,!1):this.isLineValid=t}onKeyDown(e){if(super.onKeyDown(e),this.setKeyDownStatus(e),e.keyCode===keyCode.Z&&!e.ctrlKey&&this.toggleIsHide(),e.keyCode===keyCode.Shift&&this.render(),e.keyCode===keyCode.Tab){e.preventDefault(),this.selectToNextLine(e);return}if(this.isCreate&&this.keyboardEventWhileLineCreating(e),this.config.attributeConfigurable){const t=AttributeUtils.default.getAttributeByKeycode(e.keyCode,this.config.attributeList);t!==void 0&&this.setDefaultAttribute(t)}}selectToNextLine(e){const t=CommonToolUtils.getNextSelectedRectIDByEvent(this.viewPortLines.map(i=>{var s,n,o,r;return __spreadProps(__spreadValues({},i),{x:(n=(s=i.pointList[0])==null?void 0:s.x)!=null?n:0,y:(r=(o=i.pointList[0])==null?void 0:o.y)!=null?r:0})}),e,this.selectedID);t&&this.selection.setSelectedIDs(t.id)}keyboardEventWhileLineCreating(e){!this.isCreate||(e.keyCode===keyCode.Ctrl&&this.setInvalidLineOnCreating(e),[keyCode.Shift,keyCode.Alt].includes(e.keyCode)&&this.renderNextPoint(e,this.prevAxis))}renderNextPoint(e,t){const i=this.coordUtils.getRenderCoord(this.getNextPoint(e,t));this.render(i)}deleteSelectedLine(e){var t;if(this.selectedLine){const i=MathUtils.default.calcViewportBoundaries((t=this.selectedLine)==null?void 0:t.pointList,this.isCurve,SEGMENT_NUMBER,this.zoom);LineToolUtils.default.inArea(i,this.coordUtils.getAbsCoord(e))&&this.deleteLine()}}deleteSelectedLinePoint(e){var t;this.pointsWithinRange(this.activeLine.length-1)&&e&&(this.setActiveLine(this.activeLine.filter(s=>s.id!==e)),this.updateLines(),(t=this.history)==null||t.pushHistory(this.lineList)),this.cursor=void 0,this.render()}deleteLine(){var e;this.lineList=this.lineList.filter(t=>!this.selection.isIdSelected(t.id)),(e=this.history)==null||e.pushHistory(this.lineList),this.setNoneStatus(),this.emit("dataUpdated",this.lineList),this.emit("lineDeleted",this.selectedID),this.render()}setInvalidLine(e,t,i=!0){var s;const n=this.lineList.find(o=>o.id===e);n&&(n.valid=t!==void 0?t:!n.valid,(s=this.history)==null||s.pushHistory(this.lineList),i&&this.render())}empty(){var e,t;this.lineList=[],this.setNoneStatus(),this.selectedPoint=void 0,(e=this.actionsHistory)==null||e.empty(),(t=this.history)==null||t.init(),this.emit("dataUpdated",this.lineList),this.render()}setAttribute(e){var t;this.attributeConfigurable&&(this.defaultAttribute=e,this.setLineAttribute("attribute",e),this.selectedIDs.length>0&&((t=this.history)==null||t.pushHistory(this.lineList)))}setTextAttribute(e){var t;this.isTextConfigurable&&(this.setLineAttribute("textAttribute",e),(t=this.history)==null||t.applyAttribute(this.selectedID,"textAttribute",e))}setLineAttribute(e,t){this.selectedIDs.length>0&&(this.lineList.forEach(i=>{this.selection.isIdSelected(i.id)&&(i[e]=t)}),this.render())}updateAttribute(e){this.emit("updateAttribute",e)}updateLineAttributes(e){var t;if(this.attributeConfigurable&&e){const i=(e==null?void 0:e.attribute)||"";this.defaultAttribute=i,this.updateAttribute(i)}if(this.isTextConfigurable&&e){const i=(e==null?void 0:e.textAttribute)||"";this.updateTextAttribute(i)}(t=this.history)==null||t.updateHistory(this.lineList)}lineStatusChanged(){this.emit("lineStatusChanged",{status:this.status,selectedLineID:this.selectedID})}updateTextAttribute(e){if(this.selectedID){const t=this.lineList.find(i=>i.id===this.selectedID);t&&(t.textAttribute=e)}this.emit("updateText",e)}saveData(){this.stopLineCreating(),this.setNoneStatus(),this.render()}setTextEditingID(e){this.textEditingID=e,this.render()}updateAttrWhileIDChanged(e){if(e){const t=this.lineList.find(i=>i.id===e);t&&this.setDefaultAttribute(t.attribute)}}setSelectedLineID(e,t=!1){this.selection.setSelectedIDs(e,t),this.updateAttrWhileIDChanged(this.selectedID),this.selectedLine&&this.setActiveLine(this.selectedLine.pointList),this.emit("selectedChange")}attributeLockListChange(e){this.attributeLockList=e,this.render()}setResult(e){this.setNoneStatus(),this.setLineList(e),this.render()}setConfig(e){super.setConfig(e)}toggleIsHide(){this.setIsHidden(!this.isHidden),this.render()}clearCanvas(){super.clearCanvas()}clearResult(){this.setResult([]),this.setSelectedLineID(void 0),this.render()}exportData(){return[this.lineList,this.basicImgInfo]}setDefaultAttribute(e=""){var t;this.attributeConfigurable&&(this.defaultAttribute=e,this.changeStyle(this.defaultAttribute),this.setLineAttribute("attribute",e),this.selectedIDs.length>0&&((t=this.history)==null||t.pushHistory(this.lineList)),this.emit("changeAttributeSidebar"))}getCurrentSelectedData(){var e,t;const i=this.isActiveLineValid(),s=this.defaultAttribute,n=this.getColor(s),o=i?n==null?void 0:n.valid.stroke:n==null?void 0:n.invalid.stroke,r=(t=(e=this.lineList.find(h=>h.id===this.selectedID))==null?void 0:e.textAttribute)!=null?t:"";return{color:o,textAttribute:r}}renderTextAttribute(){var e,t,i,s;if(!this.ctx||!this.selectedLine||this.activeLine&&((e=this.activeLine)==null?void 0:e.length)<2)return;const n=this.isActiveLineValid(),o=this.defaultAttribute,{x:r,y:h}=this.selectedLine.pointList[1],l=this.coordUtils.getRenderCoord({x:r,y:h}),d=this.getColor(o),u=n?d==null?void 0:d.valid.stroke:d==null?void 0:d.invalid.stroke,c=(i=(t=this.lineList.find(f=>f.id===this.selectedID))==null?void 0:t.textAttribute)!=null?i:"";this._textAttributeInstance||(this._textAttributeInstance=new textAttributeClass({container:this.container,icon:this.getTextIconSvg(o),color:u,getCurrentSelectedData:this.getCurrentSelectedData,updateSelectedTextAttribute:this.updateSelectedTextAttribute})),this._textAttributeInstance&&!((s=this._textAttributeInstance)==null?void 0:s.isExit)&&this._textAttributeInstance.appendToContainer(),this._textAttributeInstance.update(`${c}`,{left:l.x,top:l.y,color:u}),this._textAttributeInstance.updateIcon(this.getTextIconSvg(o))}getTextIconSvg(e=""){return AttributeUtils.default.getTextIconSvg(e,this.config.attributeList,this.config.attributeConfigurable,this.baseIcon)}updateSelectedTextAttribute(e){if(this._textAttributeInstance&&e&&this.selectedID){let t=e;!AttributeUtils.default.textAttributeValidate(this.config.textCheckType,"",t)&&(this.emit("messageError",AttributeUtils.default.getErrorNotice(this.config.textCheckType,this.lang)),t=""),this.setTextAttribute(t),this.emit("updateTextAttribute"),this.render()}}}exports.INNER_POINT_RADIUS=INNER_POINT_RADIUS,exports.LINE_ORDER_OFFSET=LINE_ORDER_OFFSET,exports.POINT_ACTIVE_RADIUS=POINT_ACTIVE_RADIUS,exports.POINT_RADIUS=POINT_RADIUS,exports.SEGMENT_NUMBER=SEGMENT_NUMBER,exports.default=LineToolOperation;
|
|
1
|
+
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var _=require("lodash"),tool=require("../../constant/tool.js"),ActionsHistory=require("../../utils/ActionsHistory.js"),uuid=require("../../utils/uuid.js"),keyCode=require("../../constant/keyCode.js"),MathUtils=require("../../utils/MathUtils.js"),basicToolOperation=require("./basicToolOperation.js"),LineToolUtils=require("../../utils/tool/LineToolUtils.js"),polygonTool=require("../../utils/tool/polygonTool.js"),CommonToolUtils=require("../../utils/tool/CommonToolUtils.js"),CanvasUtils=require("../../utils/tool/CanvasUtils.js"),DrawUtils=require("../../utils/tool/DrawUtils.js"),StyleUtils=require("../../utils/tool/StyleUtils.js"),AttributeUtils=require("../../utils/tool/AttributeUtils.js"),textAttributeClass=require("./textAttributeClass.js"),Selection=require("./Selection.js"),index=require("../../locales/index.js"),constants=require("../../locales/constants.js");function _interopDefaultLegacy(a){return a&&typeof a=="object"&&"default"in a?a:{default:a}}var ___default=_interopDefaultLegacy(_),__defProp=Object.defineProperty,__defProps=Object.defineProperties,__getOwnPropDescs=Object.getOwnPropertyDescriptors,__getOwnPropSymbols=Object.getOwnPropertySymbols,__hasOwnProp=Object.prototype.hasOwnProperty,__propIsEnum=Object.prototype.propertyIsEnumerable,__defNormalProp=(a,e,t)=>e in a?__defProp(a,e,{enumerable:!0,configurable:!0,writable:!0,value:t}):a[e]=t,__spreadValues=(a,e)=>{for(var t in e||(e={}))__hasOwnProp.call(e,t)&&__defNormalProp(a,t,e[t]);if(__getOwnPropSymbols)for(var t of __getOwnPropSymbols(e))__propIsEnum.call(e,t)&&__defNormalProp(a,t,e[t]);return a},__spreadProps=(a,e)=>__defProps(a,__getOwnPropDescs(e)),EStatus;(function(a){a[a.Create=0]="Create",a[a.Active=1]="Active",a[a.None=2]="None"})(EStatus||(EStatus={}));const SEGMENT_NUMBER=16,LINE_ORDER_OFFSET={x:0,y:20},POINT_RADIUS=3,POINT_ACTIVE_RADIUS=5,INNER_POINT_RADIUS=2;class LineToolOperation extends basicToolOperation.BasicToolOperation{constructor(e){super(e);this.drawActivatedLine=(t,i,s)=>{const n=this.isCreate?___default.default.cloneDeep([{pointList:this.activeLine,order:this.nextOrder(),attribute:this.defaultAttribute,valid:this.isLineValid}]):___default.default.cloneDeep(this.selectedLines),o=this.isActiveLineValid();n.forEach(r=>{if((r==null?void 0:r.pointList)&&(r.pointList=r.pointList.map(l=>Object.assign(l,{actual:{x:l.x,y:l.y}},this.coordUtils.getRenderCoord(l)))),!r.pointList)return;const h=this.getLineColorByAttribute({attribute:r.attribute,valid:r.valid});return this.selectedLines.length>0&&(this.drawLineLength(r.pointList,h),this.renderActiveArea(MathUtils.default.calcViewportBoundaries(r.pointList,this.isCurve,SEGMENT_NUMBER))),this.drawLine(r.pointList,t,h,!0,!0),this.drawLineNumber(r.pointList[0],r.order,h,"",this.defaultAttribute,o),t&&this.isCreate&&this.arc(t,POINT_RADIUS,h),this.cursor&&!this.selectedPoint&&!s&&!this.isShift&&this.arc(this.cursor,POINT_ACTIVE_RADIUS,h),r})},this.drawHoverPoint=t=>{if(!this.isMousedown&&t&&this.selectedID){const i=this.getPointList(this.activeLine),s=this.activeLine.find(o=>LineToolUtils.default.calcDistance(this.coordUtils.getRenderCoord(o),t)<=POINT_ACTIVE_RADIUS);let n;!s&&this.activeLine&&(n=this.findNearestPoint(i,t)),this.hoverPointID=s?s.id:void 0,this.cursor=s||n==null?void 0:n.point}},this.updatedLine={id:"",valid:!1,order:0},this.toolName="lineTool",this.lineList=[],this.activeLine=[],this.coordsInsideActiveArea=!1,this.hoverLineSegmentIndex=-1,this.isShift=!1,this.isReference=!1,this.drawCurveLine=(t,i,s,n=!0,o=!1,r)=>{const h=polygonTool.createSmoothCurvePointsFromPointList(i,SEGMENT_NUMBER);t.save(),t.lineCap="round",t.lineJoin="round",t.strokeStyle=s.color,n&&(t.lineWidth=s.lineWidth),o&&LineToolUtils.default.setReferenceCtx(t),i.forEach(({specialEdge:l},d)=>{const u=h.splice(0,SEGMENT_NUMBER+1);t.save(),t.beginPath(),r===d&&(t.lineWidth=4),u.forEach(({x:c,y:f},v)=>{const L=v>0?"lineTo":"moveTo";l&&LineToolUtils.default.setSpecialEdgeStyle(t),t[L](c,f)}),t.stroke(),t.restore()}),t.restore()},this.drawLine=(t,i,s,n=!1,o=!1)=>{const r=i?t.concat(i):t,h={color:s,lineWidth:o?1:this.lineStyle.lineWidth};this.isCurve?LineToolUtils.default.drawCurveLine(this.ctx,r,h,!n,this.isReference,o?this.hoverLineSegmentIndex:-1):this.drawStraightLine(r,h,o),n&&t.forEach(l=>{var d,u;const c=l.id,f=c&&[this.hoverPointID,(d=this.selectedPoint)==null?void 0:d.id].includes(c)?POINT_ACTIVE_RADIUS:POINT_RADIUS;this.arc(l,f,s),[this.hoverPointID,(u=this.selectedPoint)==null?void 0:u.id].includes(c)||this.arc(l,INNER_POINT_RADIUS,"white")})},this.drawStraightLine=(t,i,s=!1)=>{const{ctx:n}=this;n&&(n.save(),n.lineCap="round",n.lineJoin="round",n.strokeStyle=i.color,n.lineWidth=i.lineWidth,this.isReference&&LineToolUtils.default.setReferenceCtx(n),t.forEach((o,r)=>{if(n.beginPath(),r>0){const h=t[r-1];n.save(),(h==null?void 0:h.specialEdge)&&LineToolUtils.default.setSpecialEdgeStyle(n),s&&this.hoverLineSegmentIndex+1===r&&(n.lineWidth=4),n.moveTo(h.x,h.y),n.lineTo(o.x,o.y),n.stroke(),n.restore()}}),n.restore())},this.drawLines=()=>{try{const t=___default.default.cloneDeep(this.attributeFilteredLines);if(this.isHidden)return;t.forEach(i=>{if(!this.selection.isIdSelected(i.id)&&i.pointList){i.pointList.map(h=>Object.assign(h,{actual:{x:h.x,y:h.y}},this.coordUtils.getRenderCoord(h)));const{order:s,label:n}=i,o=s,r=i&&this.getLineColorByAttribute(i);this.drawLine(i.pointList,void 0,r,!1),this.drawLineNumber(i.pointList[0],o,r,n,i.attribute,i.valid),i.id!==this.textEditingID&&(this.drawLineTextAttribute(i.pointList[1],r,i==null?void 0:i.textAttribute),this.drawLineLength(i.pointList,r))}})}catch(t){console.error(t,"\u7EBF\u6761\u5DE5\u5177\u6570\u636E\u89E3\u6790\u9519\u8BEF"),this.lineList=[],this.clearCanvas()}},this.render=t=>{super.render(),this.drawLines(),this.drawActivatedLine(t,void 0,!0),this.renderTextAttribute(),this.renderCursorLine(this.getLineColor(this.defaultAttribute))},this.moveLineInPolygon=(t,i)=>{var s;if(!Array.isArray(this.activeLine))return!1;((s=this.activeLine)==null?void 0:s.every(o=>this.isInBasicPolygon({x:o.x+t,y:o.y+i})))&&(this.lineDragging=!0,this.moveActiveArea(t,i))},this.getSelectedLinesArea=()=>MathUtils.default.calcViewportBoundaries(this.selectedLines.reduce((t,i)=>(i==null?void 0:i.pointList)?t.concat(...i==null?void 0:i.pointList):t,[])),this.moveLineInRectRange=(t,i,s,n)=>{if(this.selectedLines.length===0)return;const{top:o,left:r,right:h,bottom:l}=this.getSelectedLinesArea(),d=[r,h].map(g=>___default.default.isNumber(g)?g+t:0),u=[o,l].map(g=>___default.default.isNumber(g)?g+i:0),c=r>=0&&h&&MathUtils.default.isInRange(d,s),f=o>=0&&l&&MathUtils.default.isInRange(u,n),v=c?t:0,L=f?i:0;this.lineDragging=!0,this.moveActiveArea(v,L)},this.onRightClick=t=>{if(this.cursor=void 0,this.isCreate){if(this.isLinePointsNotEnough())return;if(LineToolUtils.default.lineLengthSum(this.activeLine||[])<this.minLength)return this.emit("messageInfo",`${index.getMessagesByLocale(constants.EMessage.MinLengthLimitErrorNotice,this.lang)}${this.minLength}`),!0;this.stopLineCreating(!0);return}const i=this.findHoverLine(this.getCoordinate(t));this.setSelectedLineID(i==null?void 0:i.id,t.ctrlKey),this.emit("contextmenu")},this.updateSelectedAttributeAfterHistoryChanged=()=>{if(this.selectedIDs.length>0){const t=this.lineList.find(s=>s.id===this.selectedIDs[0]),i=t==null?void 0:t.attribute;typeof i=="string"&&(this.defaultAttribute=i,this.updateAttribute(i),this.render())}},this.onLeftClick=t=>{const i=this.getCoordinate(t),{lineDragging:s}=this;if(this.lineDragging=!1,this.isSpaceKey)return;if(this.isNone&&t.ctrlKey){const o=this.findHoverLine(i);o&&this.setInvalidLine(o.id);return}if(this.isLinePointsExceed())return;const n=this.getNextPoint(t,i);if(this.isCreate||this.isNone){this.setCreateStatusAndAddPoint(n);return}if(this.isActive){if(s)return;if(this.isMouseCoordOutsideActiveArea()){this.setNoneStatus(!1),this.setCreateStatusAndAddPoint(n);return}if(t.shiftKey){this.updateLineSegmentSpecial(i);return}this.coordsInsideActiveArea&&t.ctrlKey&&this.setInvalidLine(this.selectedID)}},this.onDblclick=()=>{},this.onKeyUp=t=>{if(super.onKeyUp(t),this.isShift=!1,this.hoverLineSegmentIndex=-1,!this.selection.triggerKeyboardEvent(t,this.setLineList)){if(t.keyCode===keyCode.Esc){this.stopLineCreating(!1);return}if(this.selectedLine){if(t.key==="Delete"){this.deleteLine();return}if(t.key==="f"){this.setInvalidLine(this.selectedID);return}if(t.key===" "){this.continueToEdit();return}}this.keyboardEventWhileLineCreating(t)}},this.onRightDblClick=t=>{super.onRightDblClick(t);const i=this.getCoordinate(t);if(this.isActive){const s=this.findHoveredPoint(i);if(s){this.deleteSelectedLinePoint(s.id);return}this.deleteSelectedLine(i)}},this.setReference=t=>{this.isReference=t},this.pointsWithinRange=t=>!(this.lowerLimitPointNum&&t<this.lowerLimitPointNum||this.upperLimitPointNum&&t>this.upperLimitPointNum),this.setLineList=t=>{const i=t.length!==this.lineListLen;this.lineList=t,i&&this.emit("updatePageNumber")},this.textChange=t=>{this.config.textConfigurable===!1||!this.selectedID||(this.updateSelectedTextAttribute(t),this.emit("selectedChange"))},this.status=2,this.isMousedown=!1,this.lineDragging=!1,this.isLineValid=!0,this.setConfig(e.config),this.prevAxis={x:0,y:0},this.textEditingID="",this.updateSelectedTextAttribute=this.updateSelectedTextAttribute.bind(this),this.getCurrentSelectedData=this.getCurrentSelectedData.bind(this),this.actionsHistory=new ActionsHistory,this.selection=new Selection(this),this.dependToolConfig={lineType:tool.ELineTypes.Line}}get selectedLines(){return this.lineList.filter(e=>this.selection.isIdSelected(e.id))}get isLineSelected(){return this.selectedID&&this.activeLine}get selectedLine(){return this.lineList.find(e=>e.id===this.selectedID)}get isCreate(){return this.status===0}get isActive(){return this.selectedLines.length>0}get isNone(){return!this.isCreate&&this.selectedIDs.length===0}get isCurve(){return this.config.lineType===tool.ELineTypes.Curve}get isMultipleColor(){return this.config.lineColor===tool.ELineColor.MultiColor}get imageSize(){return this.rotate%180==0?this.basicImgInfo:{width:this.basicImgInfo.height,height:this.basicImgInfo.width}}get lineListLen(){return this.lineList.length}get edgeAdsorptionEnabled(){return this.edgeAdsorption&&!this.isCurve&&this.lineListLen>0}get attributeConfigurable(){return this.config.attributeConfigurable}get isTextConfigurable(){return this.config.textConfigurable}get isDependPolygon(){return this.dependToolName===tool.EToolName.Polygon}get isDependRect(){return this.dependToolName===tool.EToolName.Rect}get isCurrentAttributeLocked(){return this.attributeLockList.includes(this.defaultAttribute)}get attributeFilteredLines(){return this.attributeLockList.length>0?this.lineList.filter(e=>this.attributeLockList.includes((e==null?void 0:e.attribute)||"")):this.lineList}get selectedIDs(){return this.selection.selectedIDs}get selectedID(){return this.selection.selectedID}get enableOutOfTarget(){return this.config.enableOutOfTarget||this.config.outOfTarget||this.config.drawOutsideTarget}get showOrder(){var e;return(e=this.config.showOrder)!=null?e:this.config.isShowOrder}get edgeAdsorption(){return this.config.edgeAdsorption}get attributeList(){return this.config.attributeList}get lowerLimitPointNum(){return this.config.lowerLimitPointNum}get minLength(){var e;return((e=this.config)==null?void 0:e.minLength)||0}get upperLimitPointNum(){return this.config.upperLimitPointNum}get textCheckType(){return this.config.textCheckType}get customFormat(){return this.config.customFormat}get dataList(){return this.lineList}get hasActiveLine(){return this.activeLine&&this.activeLine.length>0}get viewPortLines(){const e=CanvasUtils.getViewPort(this.canvas,this.currentPos,this.zoom);return this.isHidden?[]:this.attributeFilteredLines.filter(t=>{var i;return(i=t==null?void 0:t.pointList)==null?void 0:i.some(s=>CanvasUtils.inViewPort(s,e))})}get lineStyle(){return{lineWidth:this.style.width,color:this.getLineColor(this.defaultAttribute),opacity:this.style.opacity}}get selectedText(){var e,t;return(t=(e=this.lineList.find(i=>i.id===this.selectedID))==null?void 0:e.textAttribute)!=null?t:""}get currentPageResult(){return this.lineList}updateStatus(e,t=!1){if(e!==this.status){if(t){let i="";this.textCheckType===tool.ETextType.Order&&this.isTextConfigurable&&(i=AttributeUtils.default.getTextAttribute(this.lineList,this.textCheckType)),this.emit("updateText",i)}this.status=e,this.lineStatusChanged()}}isInBasicPolygon(e){var t,i;return polygonTool.isInPolygon(e,((t=this.basicResult)==null?void 0:t.pointList)||[],(i=this.dependToolConfig)==null?void 0:i.lineType)}getPolygonPointList(){if(!this.basicResult)return[];const{pointList:e}=this.basicResult,{lineType:t}=this.dependToolConfig;return t===tool.ELineTypes.Curve?polygonTool.createSmoothCurvePoints(e.reduce((i,s)=>[...i,s.x,s.y],[]),.5,!0,20):e}getNextCoordByRenderCoord(e){return this.getNextCoordByAbsCoord(this.coordUtils.getAbsCoord(e))}getNextCoordByAbsCoord(e){var t;const i=(t=this.activeLine)==null?void 0:t.slice(-1)[0];return i?this.coordUtils.getNextCoordByDependTool(e,i):e}pointInLine(e,t,i){return e.filter(s=>s).length<2?!1:e.some((s,n)=>{if(n===0)return!1;const o=this.coordUtils.getRenderCoord(e[n-1]),r=this.coordUtils.getRenderCoord(s);return LineToolUtils.default.isInLine(t,o,r,i)})}arc(e,t=POINT_RADIUS,i){var s,n,o,r,h,l;if(this.ctx){const{x:d,y:u}=e;(s=this.ctx)==null||s.save(),(n=this.ctx)==null||n.beginPath(),this.ctx.fillStyle=i||this.lineStyle.color,(o=this.ctx)==null||o.arc(d,u,t,0,360),(r=this.ctx)==null||r.closePath(),(h=this.ctx)==null||h.fill(),(l=this.ctx)==null||l.restore()}}renderActiveArea({top:e,left:t,right:i,bottom:s}){this.ctx&&(this.ctx.save(),this.ctx.beginPath(),this.ctx.strokeStyle="#B3B8FF",this.ctx.rect(t,e,i-t,s-e),this.ctx.stroke(),this.ctx.restore())}addLinePoint(e){var t,i,s,n;this.arc(e),(t=this.activeLine)==null||t.push(__spreadProps(__spreadValues({},e),{id:uuid()})),((i=this.activeLine)==null?void 0:i.length)===1?(s=this.actionsHistory)==null||s.initRecord(this.activeLine):(n=this.actionsHistory)==null||n.pushHistory(this.activeLine),this.render()}setCreateStatusAndAddPoint(e,t=!1){this.updateStatus(0,t),this.addLinePoint(e)}isActiveLineValid(){var e;return this.selectedID?(e=this.lineList.find(t=>t.id===this.selectedID))==null?void 0:e.valid:this.isLineValid}nextOrder(){return this.lineListLen===0?1:this.lineList.slice(-1)[0].order+1}getLineColorByAttribute(e,t=!1){return StyleUtils.getStrokeAndFill(this.getColor(e.attribute),e.valid,{isSelected:t}).stroke}drawLineNumber(e,t=1,i,s="",n,o=!0){var r,h,l;if((this.showOrder||this.attributeConfigurable)&&this.ctx){let d=this.showOrder?t.toString():`${s}`;if(this.attributeConfigurable){const u=n?(l=(h=(r=this.attributeList)==null?void 0:r.find(c=>c.value===n))==null?void 0:h.key)!=null?l:n:"";d=[d,`${!o&&u?"\u65E0\u6548":""}${u}`].filter(c=>c).join("_")}this.drawText(e,d,i)}}drawLineTextAttribute(e,t,i){if(e&&i)return this.drawText(e,i,t,200)}drawLineLength(e,t){var i;if(((i=this.config)==null?void 0:i.showLineLength)&&e){const s=e.reduce((o,r,h)=>h<=0||!e[h-1].actual||!r.actual?o:o+LineToolUtils.default.calcDistance(e[h-1].actual,r.actual),0),n=e[e.length-1];this.drawText(n,`l = ${s.toFixed(2)}`,t)}}drawText(e,t,i,s){var n,o;this.ctx&&((n=this.ctx)==null||n.save(),this.ctx.font="italic bold 14px SourceHanSansCN-Regular",this.ctx.fillStyle=i,this.ctx.strokeStyle=i,this.ctx.shadowColor="rgba(0, 0, 0, 0.6)",this.ctx.shadowOffsetY=2,this.ctx.shadowBlur=4,s?DrawUtils.wrapText(this.canvas,t,e.x-LINE_ORDER_OFFSET.x,e.y-LINE_ORDER_OFFSET.y,s):this.ctx.fillText(t,e.x-LINE_ORDER_OFFSET.x,e.y-LINE_ORDER_OFFSET.y),(o=this.ctx)==null||o.restore())}moveActiveArea(e,t){this.selectedLines.length>0&&(this.selectedLines.forEach(i=>{var s;(s=i.pointList)==null||s.forEach(n=>Object.assign(n,{x:n.x+e,y:n.y+t}))}),this.render()),this.emit("dataUpdated",this.lineList,this.selectedIDs)}findHoveredPoint(e){if(!!this.activeLine)return this.activeLine.find(t=>{const i=this.coordUtils.getRenderCoord(t);return LineToolUtils.default.calcDistance(i,e)<=POINT_ACTIVE_RADIUS})}findHoverLine(e){return ___default.default.cloneDeep(this.lineList).reverse().find(({pointList:i})=>{const s=i?this.getPointList(i):[],n=this.getLineWidthScope();return s.some((o,r)=>{if(r===0)return!1;const h=this.coordUtils.getRenderCoord(o),l=this.coordUtils.getRenderCoord(s[r-1]);return LineToolUtils.default.isInLine(e,h,l,n)})})}getAdsorptionPoint(e){let t,i,s;return ___default.default.cloneDeep(this.lineList).reverse().forEach(({pointList:n,id:o})=>{if(o===this.selectedID||!n||(n==null?void 0:n.length)<2)return;const r=this.findNearestPoint(n,e);if(r){if(r.minDistance===0){t=r.point;return}(i===void 0||r.minDistance<i)&&(t=r.point,i=r.minDistance)}}),s||t}findNearestPoint(e,t,i=7){let s;const n=i;for(let o=1;o<=e.length-1;o++){const r=this.coordUtils.getRenderCoord(e[o]),h=this.coordUtils.getRenderCoord(e[o-1]),{length:l,footPoint:d}=MathUtils.default.getFootOfPerpendicular(t,r,h),u=LineToolUtils.default.calcTwoPointDistance(r,t),c=LineToolUtils.default.calcTwoPointDistance(h,t);if(u<=i*2){s=r,i=0;break}if(c<=i*2){s=h,i=0;break}l<i&&(s=d,i=l)}return s?{point:s,minDistance:n}:void 0}getPointList(e){return this.isCurve?polygonTool.createSmoothCurvePointsFromPointList(e,SEGMENT_NUMBER):e}moveSelectedLine(e){const t=(e.x-this.prevAxis.x)/this.zoom,i=(e.y-this.prevAxis.y)/this.zoom;if(this.enableOutOfTarget){this.lineDragging=!0,this.moveActiveArea(t,i);return}if(this.isDependPolygon){this.moveLineInPolygon(t,i);return}let s=[0,this.imageSize.width],n=[0,this.imageSize.height];if(this.isDependRect){const{x:o,y:r,width:h,height:l}=this.basicResult;s=[o,o+h],n=[r,r+l]}this.moveLineInRectRange(t,i,s,n)}moveSelectPoint(e){if(!this.selectedPoint)return;const t=e.x-this.prevAxis.x,i=e.y-this.prevAxis.y,s=(this.selectedPoint?this.selectedPoint.x:0)+t/this.zoom,n=(this.selectedPoint?this.selectedPoint.y:0)+i/this.zoom,o={x:s,y:n};Object.assign(this.selectedPoint,this.getNextCoordByAbsCoord(o)),this.updateLines(),this.render()}getCoordByConfig(e,t){var i;const s=!!e.shiftKey,n=e.altKey;if(((i=this.activeLine)==null?void 0:i.length)>0&&s){const o=this.activeLine.slice(-1)[0];return LineToolUtils.default.getVHPoint(o,t,this.coordUtils.getAbsCoord(t),this.coordUtils.getRenderCoord(o))}return this.edgeAdsorptionEnabled&&!n?this.getAdsorptionPoint(t):t}getNextPoint(e,t){const i=this.getCoordByConfig(e,t)||t;return this.enableOutOfTarget?this.coordUtils.getAbsCoord(i):this.getNextCoordByRenderCoord(i)}lineHover(){this.render()}mouseMoveHandler(e){const t=this.getCoordinate(e),i=e.which===1;if(this.isCreate){this.hasActiveLine&&this.renderNextPoint(e,t);return}if(this.isNone&&(this.lineHover(),this.edgeAdsorptionEnabled&&!e.altKey)){const s=this.getAdsorptionPoint(t);s&&this.arc(s)}if(this.isActive){if(this.isMousedown&&i){if(this.selectedPoint){this.moveSelectPoint(t);return}if(this.coordsInsideActiveArea){this.moveSelectedLine(t),this.drawActivatedLine(void 0,void 0,!0);return}}this.drawHoverPoint(t),this.render()}}onMouseMove(e){if(super.onMouseMove(e)||this.forbidMouseOperation||!this.imgInfo)return;const t=this.getCoordinate(e);this.mouseMoveHandler(e),this.prevAxis=t}setActiveLine(e){this.activeLine=e?___default.default.cloneDeep(e):void 0}historyChanged(e){const t=`${e}Enabled`;if(this.isCreate){if(this.actionsHistory&&this.actionsHistory[t]){const i=this.actionsHistory&&this.actionsHistory[e]();this.setActiveLine(i),this.render()}return}if(this.history&&this.history[t]){const i=this.history[e](),s=i==null?void 0:i.find(n=>n.id===this.selectedID);this.lineList=i,this.selectedID&&s?this.setActiveLine(s==null?void 0:s.pointList):this.setNoneStatus(),this.render()}this.emit("dataUpdated",this.lineList)}undo(){this.historyChanged("undo"),this.updateSelectedAttributeAfterHistoryChanged()}redo(){this.historyChanged("redo"),this.updateSelectedAttributeAfterHistoryChanged()}isCoordInsideTarget(e){if(this.isDependPolygon)return this.isInBasicPolygon(e);if(this.isDependRect){const{x:t,y:i,width:s,height:n}=this.basicResult,o=[t,t+s],r=[i,i+n];return MathUtils.default.isInRange(e.x,o)&&MathUtils.default.isInRange(e.y,r)}return MathUtils.default.isInRange(e.x,[0,this.imageSize.width])&&MathUtils.default.isInRange(e.y,[0,this.imageSize.height])}getPointInsertIndex(e,t){if(e&&this.activeLine){const i=this.getPointList(this.activeLine);return this.activeLine.length===2?1:this.activeLine.findIndex((s,n)=>{if(n>0){const o=this.activeLine?this.activeLine[n-1]:void 0,r=this.isCurve?i.slice((n-1)*(SEGMENT_NUMBER+1),n*(SEGMENT_NUMBER+1)):[o,s];return this.pointInLine(r,e,t||this.getLineWidthScope())}return!1})}return-1}getLineWidthScope(){return this.lineStyle.lineWidth}isMouseCoordOutsideActiveArea(){return!this.coordsInsideActiveArea&&!this.selectedPoint}isLinePointsExceed(){return this.isCreate&&this.activeLine&&this.upperLimitPointNum&&~~this.upperLimitPointNum<=this.activeLine.length}isLinePointsNotEnough(){var e;return this.activeLine&&((e=this.activeLine)==null?void 0:e.length)<this.lowerLimitPointNum}updateLineSegmentSpecial(e){const t=this.getPointInsertIndex(e,2)-1;if(t>-1){const i=this.activeLine[t];i.specialEdge=!i.specialEdge,this.hoverLineSegmentIndex=-1,this.render()}}addLinePointToActiveLine(){var e;const t=this.getPointInsertIndex(this.cursor),i=this.pointsWithinRange(this.activeLine.length+1);this.cursor&&t>-1&&i&&(this.activeLine.splice(t,0,__spreadProps(__spreadValues({},this.coordUtils.getAbsCoord(this.cursor)),{id:uuid()})),this.updateLines(),(e=this.history)==null||e.pushHistory(this.lineList),this.render(),this.cursor=void 0)}isCoordOnSelectedArea(e){return this.selectedLines.some(t=>LineToolUtils.default.inArea(MathUtils.default.calcViewportBoundaries(t.pointList),this.coordUtils.getAbsCoord(e)))}onMouseDown(e){if(super.onMouseDown(e)||this.forbidMouseOperation||!this.imgInfo)return;const t=this.getCoordinate(e);if(this.isMousedown=!0,this.prevAxis=t,e.which===3){this.cursor=void 0;return}this.selectedPoint=this.findHoveredPoint(t),this.coordsInsideActiveArea=this.selectedLines.length>0?this.isCoordOnSelectedArea(t):!1,this.lineDragging=!1}lineHasChanged(){const e=this.lineList.find(t=>t.id===this.selectedID);return e?JSON.stringify(e.pointList)!==JSON.stringify(this.activeLine):!1}updateLines(){const e=this.lineList.find(t=>t.id===this.selectedID);e&&(e.pointList=___default.default.cloneDeep(this.activeLine),this.updatedLine=e,this.emit("dataUpdated",this.lineList))}onMouseUp(e){const t=()=>{this.isMousedown=!1,this.hoverPointID=void 0,this.cursor=void 0,this.selectedPoint=void 0};if(this.hoverPointID=void 0,super.onMouseUp(e)||this.forbidMouseOperation||!this.imgInfo){t();return}e.which===1&&this.onLeftClick(e),e.which===3&&this.onRightClick(e),t()}isTextValid(e){return AttributeUtils.default.textAttributeValidate(this.textCheckType,this.customFormat,e)}createLineData(){const e=uuid(),t={pointList:___default.default.cloneDeep(this.activeLine),id:e,valid:this.isLineValid,order:this.nextOrder()};return t.attribute=this.defaultAttribute,t}stopLineCreating(e=!0){var t,i,s,n;const o=this.selectedID?!0:!!this.isTextConfigurable;let r;if(e){if(this.selectedID){const h=this.lineList.find(l=>l.id===this.selectedID);r=this.selectedID,h&&(h.pointList=___default.default.cloneWith(this.activeLine),___default.default.isEqual(h.pointList,(t=this.history)==null?void 0:t.pushHistory(this.lineList))||(i=this.history)==null||i.pushHistory(this.lineList))}else if(this.isCreate&&this.activeLine&&this.activeLine.length>1){const h=this.createLineData();r=h.id,this.setLineList([...this.lineList,h]),(s=this.history)==null||s.pushHistory(this.lineList)}}this.setNoneStatus(),o&&(this.activeLine=[],this.setSelectedLineID(r)),(n=this.actionsHistory)==null||n.empty(),this.emit("dataUpdated",this.lineList,this.selectedIDs),this.render()}setNoneStatus(e=!0){e&&this.updateStatus(2),this.activeLine=[],this.setSelectedLineID(void 0),this.isLineValid=!0,this.cursor=void 0}setKeyDownStatus(e,t){this.isShift=t!=null?t:e.keyCode===keyCode.Shift}continueToEdit(){var e,t;((e=this.selectedLine)==null?void 0:e.pointList)&&(this.updateStatus(0),this.cursor=void 0,this.setActiveLine(this.selectedLine.pointList),(t=this.actionsHistory)==null||t.pushHistory(this.activeLine),this.render())}setInvalidLineOnCreating(e){if(this.selectedID&&e.keyCode!==keyCode.Ctrl||!this.isCreate)return;const t=!e.ctrlKey;this.selectedID?this.setInvalidLine(this.selectedID,t,!1):this.isLineValid=t}onKeyDown(e){if(super.onKeyDown(e),this.setKeyDownStatus(e),e.keyCode===keyCode.Z&&!e.ctrlKey&&this.toggleIsHide(),e.keyCode===keyCode.Shift&&this.render(),e.keyCode===keyCode.Tab){e.preventDefault(),this.selectToNextLine(e);return}if(this.isCreate&&this.keyboardEventWhileLineCreating(e),this.config.attributeConfigurable){const t=AttributeUtils.default.getAttributeByKeycode(e.keyCode,this.config.attributeList);t!==void 0&&this.setDefaultAttribute(t)}}selectToNextLine(e){const t=CommonToolUtils.getNextSelectedRectIDByEvent(this.viewPortLines.map(i=>{var s,n,o,r;return __spreadProps(__spreadValues({},i),{x:(n=(s=i.pointList[0])==null?void 0:s.x)!=null?n:0,y:(r=(o=i.pointList[0])==null?void 0:o.y)!=null?r:0})}),e,this.selectedID);t&&this.selection.setSelectedIDs(t.id)}keyboardEventWhileLineCreating(e){!this.isCreate||(e.keyCode===keyCode.Ctrl&&this.setInvalidLineOnCreating(e),[keyCode.Shift,keyCode.Alt].includes(e.keyCode)&&this.renderNextPoint(e,this.prevAxis))}renderNextPoint(e,t){const i=this.coordUtils.getRenderCoord(this.getNextPoint(e,t));this.render(i)}deleteSelectedLine(e){var t;if(this.selectedLine){const i=MathUtils.default.calcViewportBoundaries((t=this.selectedLine)==null?void 0:t.pointList,this.isCurve,SEGMENT_NUMBER,this.zoom);LineToolUtils.default.inArea(i,this.coordUtils.getAbsCoord(e))&&this.deleteLine()}}deleteSelectedLinePoint(e){var t;this.pointsWithinRange(this.activeLine.length-1)&&e&&(this.setActiveLine(this.activeLine.filter(s=>s.id!==e)),this.updateLines(),(t=this.history)==null||t.pushHistory(this.lineList)),this.cursor=void 0,this.render()}deleteLine(){var e;this.lineList=this.lineList.filter(t=>!this.selection.isIdSelected(t.id)),(e=this.history)==null||e.pushHistory(this.lineList),this.setNoneStatus(),this.emit("dataUpdated",this.lineList),this.emit("lineDeleted",this.selectedID),this.render()}setInvalidLine(e,t,i=!0){var s;const n=this.lineList.find(o=>o.id===e);n&&(n.valid=t!==void 0?t:!n.valid,(s=this.history)==null||s.pushHistory(this.lineList),i&&this.render())}empty(){var e,t;this.lineList=[],this.setNoneStatus(),this.selectedPoint=void 0,(e=this.actionsHistory)==null||e.empty(),(t=this.history)==null||t.init(),this.emit("dataUpdated",this.lineList),this.render()}setAttribute(e){var t;this.attributeConfigurable&&(this.defaultAttribute=e,this.setLineAttribute("attribute",e),this.selectedIDs.length>0&&((t=this.history)==null||t.pushHistory(this.lineList)))}setTextAttribute(e){var t;this.isTextConfigurable&&(this.setLineAttribute("textAttribute",e),(t=this.history)==null||t.applyAttribute(this.selectedID,"textAttribute",e))}setLineAttribute(e,t){this.selectedIDs.length>0&&(this.lineList.forEach(i=>{this.selection.isIdSelected(i.id)&&(i[e]=t)}),this.render())}updateAttribute(e){this.emit("updateAttribute",e)}updateLineAttributes(e){var t;if(this.attributeConfigurable&&e){const i=(e==null?void 0:e.attribute)||"";this.defaultAttribute=i,this.updateAttribute(i)}if(this.isTextConfigurable&&e){const i=(e==null?void 0:e.textAttribute)||"";this.updateTextAttribute(i)}(t=this.history)==null||t.updateHistory(this.lineList)}lineStatusChanged(){this.emit("lineStatusChanged",{status:this.status,selectedLineID:this.selectedID})}updateTextAttribute(e){if(this.selectedID){const t=this.lineList.find(i=>i.id===this.selectedID);t&&(t.textAttribute=e)}this.emit("updateText",e)}saveData(){this.stopLineCreating(),this.setNoneStatus(),this.render()}setTextEditingID(e){this.textEditingID=e,this.render()}updateAttrWhileIDChanged(e){if(e){const t=this.lineList.find(i=>i.id===e);t&&this.setDefaultAttribute(t.attribute)}}setSelectedLineID(e,t=!1){this.selection.setSelectedIDs(e,t),this.updateAttrWhileIDChanged(this.selectedID),this.selectedLine&&this.setActiveLine(this.selectedLine.pointList),this.emit("selectedChange")}attributeLockListChange(e){this.attributeLockList=e,this.render()}setResult(e){this.setNoneStatus(),this.setLineList(e),this.render()}setConfig(e){super.setConfig(e)}toggleIsHide(){this.setIsHidden(!this.isHidden),this.render()}clearCanvas(){super.clearCanvas()}clearResult(){this.setResult([]),this.setSelectedLineID(void 0),this.render()}exportData(){return[this.lineList,this.basicImgInfo]}setDefaultAttribute(e=""){var t;this.attributeConfigurable&&(this.defaultAttribute=e,this.changeStyle(this.defaultAttribute),this.setLineAttribute("attribute",e),this.selectedIDs.length>0&&((t=this.history)==null||t.pushHistory(this.lineList)),this.emit("changeAttributeSidebar"))}getCurrentSelectedData(){var e,t;const i=this.isActiveLineValid(),s=this.defaultAttribute,n=this.getColor(s),o=i?n==null?void 0:n.valid.stroke:n==null?void 0:n.invalid.stroke,r=(t=(e=this.lineList.find(h=>h.id===this.selectedID))==null?void 0:e.textAttribute)!=null?t:"";return{color:o,textAttribute:r}}renderTextAttribute(){var e,t,i,s;if(!this.ctx||!this.selectedLine||this.activeLine&&((e=this.activeLine)==null?void 0:e.length)<2)return;const n=this.isActiveLineValid(),o=this.defaultAttribute,{x:r,y:h}=this.selectedLine.pointList[1],l=this.coordUtils.getRenderCoord({x:r,y:h}),d=this.getColor(o),u=n?d==null?void 0:d.valid.stroke:d==null?void 0:d.invalid.stroke,c=(i=(t=this.lineList.find(f=>f.id===this.selectedID))==null?void 0:t.textAttribute)!=null?i:"";this._textAttributeInstance||(this._textAttributeInstance=new textAttributeClass({container:this.container,icon:this.getTextIconSvg(o),color:u,getCurrentSelectedData:this.getCurrentSelectedData,updateSelectedTextAttribute:this.updateSelectedTextAttribute})),this._textAttributeInstance&&!((s=this._textAttributeInstance)==null?void 0:s.isExit)&&this._textAttributeInstance.appendToContainer(),this._textAttributeInstance.update(`${c}`,{left:l.x,top:l.y,color:u}),this._textAttributeInstance.updateIcon(this.getTextIconSvg(o))}getTextIconSvg(e=""){return AttributeUtils.default.getTextIconSvg(e,this.config.attributeList,this.config.attributeConfigurable,this.baseIcon)}updateSelectedTextAttribute(e){if(this._textAttributeInstance&&e&&this.selectedID){let t=e;!AttributeUtils.default.textAttributeValidate(this.config.textCheckType,"",t)&&(this.emit("messageError",AttributeUtils.default.getErrorNotice(this.config.textCheckType,this.lang)),t=""),this.setTextAttribute(t),this.emit("updateTextAttribute"),this.render()}}}exports.INNER_POINT_RADIUS=INNER_POINT_RADIUS,exports.LINE_ORDER_OFFSET=LINE_ORDER_OFFSET,exports.POINT_ACTIVE_RADIUS=POINT_ACTIVE_RADIUS,exports.POINT_RADIUS=POINT_RADIUS,exports.SEGMENT_NUMBER=SEGMENT_NUMBER,exports.default=LineToolOperation;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";var _=require("lodash"),lbUtils=require("@labelbee/lb-utils"),MathUtils=require("../../utils/MathUtils.js"),RectUtils=require("../../utils/tool/RectUtils.js"),annotation=require("../../constant/annotation.js"),keyCode=require("../../constant/keyCode.js"),tool=require("../../constant/tool.js"),index=require("../../locales/index.js"),constants=require("../../locales/constants.js"),ActionsHistory=require("../../utils/ActionsHistory.js"),AttributeUtils=require("../../utils/tool/AttributeUtils.js"),AxisUtils=require("../../utils/tool/AxisUtils.js"),CanvasUtils=require("../../utils/tool/CanvasUtils.js"),CommonToolUtils=require("../../utils/tool/CommonToolUtils.js"),DrawUtils=require("../../utils/tool/DrawUtils.js"),PolygonUtils=require("../../utils/tool/PolygonUtils.js"),StyleUtils=require("../../utils/tool/StyleUtils.js"),uuid=require("../../utils/uuid.js"),basicToolOperation=require("./basicToolOperation.js"),textAttributeClass=require("./textAttributeClass.js"),Selection=require("./Selection.js");function _interopDefaultLegacy(c){return c&&typeof c=="object"&&"default"in c?c:{default:c}}var ___default=_interopDefaultLegacy(_),__defProp=Object.defineProperty,__defProps=Object.defineProperties,__getOwnPropDescs=Object.getOwnPropertyDescriptors,__getOwnPropSymbols=Object.getOwnPropertySymbols,__hasOwnProp=Object.prototype.hasOwnProperty,__propIsEnum=Object.prototype.propertyIsEnumerable,__defNormalProp=(c,t,e)=>t in c?__defProp(c,t,{enumerable:!0,configurable:!0,writable:!0,value:e}):c[t]=e,__spreadValues=(c,t)=>{for(var e in t||(t={}))__hasOwnProp.call(t,e)&&__defNormalProp(c,e,t[e]);if(__getOwnPropSymbols)for(var e of __getOwnPropSymbols(t))__propIsEnum.call(t,e)&&__defNormalProp(c,e,t[e]);return c},__spreadProps=(c,t)=>__defProps(c,__getOwnPropDescs(t));const TEXT_MAX_WIDTH=164;class PolygonOperation extends basicToolOperation.BasicToolOperation{constructor(t){super(t);this.isAllowDouble=e=>{const{selectedID:i}=this,s=this.getHoverID(e);return!!(i&&i===s)},this.textChange=e=>{this.config.textConfigurable===!1||!this.selectedID||(this.setPolygonList(AttributeUtils.default.textChange(e,this.selectedID,this.polygonList)),this.emit("selectedChange"),this.render())},this.emitUpdatePolygonByDrag=()=>{if(this.dragInfo){const{originPolygon:e}=this.dragInfo;this.emit("updatePolygonByDrag",[{newPolygon:this.selectedPolygon,originPolygon:e}])}},this.config=CommonToolUtils.jsonParser(t.config),this.drawingPointList=[],this.polygonList=[],this.hoverPointIndex=-1,this.hoverEdgeIndex=-1,this.drawingHistory=new ActionsHistory,this.isCtrl=!1,this.isAlt=!1,this.isCombined=!1,this.pattern=tool.EPolygonPattern.Normal,this.getCurrentSelectedData=this.getCurrentSelectedData.bind(this),this.updateSelectedTextAttribute=this.updateSelectedTextAttribute.bind(this),this.selection=new Selection(this)}get selectedIDs(){return this.selection.selectedIDs}get selectedID(){return this.selection.selectedID}eventBinding(){super.eventBinding(),this.container.removeEventListener("mouseup",this.onMouseUp),this.container.addEventListener("mouseup",this.dragMouseUp),this.dblClickListener.addEvent(this.onMouseUp,this.onLeftDblClick,this.onRightDblClick,this.isAllowDouble)}eventUnbinding(){super.eventUnbinding(),this.container.removeEventListener("mouseup",this.dragMouseUp)}destroy(){super.destroy(),this._textAttributeInstance&&this._textAttributeInstance.clearTextAttribute()}get selectedPolygon(){return PolygonUtils.getPolygonByID(this.visiblePolygonList,this.selectedID)}get selectedPolygons(){return PolygonUtils.getPolygonByIDs(this.polygonList,this.selectedIDs)}get hoverPolygon(){return this.visiblePolygonList.find(t=>t.id===this.hoverID&&t.id!==this.selectedID)}get enableDrag(){return Boolean(this.selectedID&&this.dragInfo)}get visiblePolygonList(){return this.polygonList}get polygonListUnderZoom(){return this.visiblePolygonList.map(t=>__spreadProps(__spreadValues({},t),{pointList:AxisUtils.default.changePointListByZoom(t.pointList,this.zoom)}))}get selectedText(){var t;return(t=this.selectedPolygon)==null?void 0:t.textAttribute}get dataList(){return this.polygonList}setPattern(t,e=!1){var i;((i=this.drawingPointList)==null?void 0:i.length)>0&&e===!0||(this.pattern=t)}get currentShowList(){let t=[];const[e,i]=CommonToolUtils.getRenderResultList(this.polygonList,CommonToolUtils.getSourceID(this.basicResult),this.attributeLockList,this.selectedIDs);return t=e,this.isHidden&&(t=[]),i&&t.push(...i),t}get currentPageResult(){const[t]=CommonToolUtils.getRenderResultList(this.polygonList,CommonToolUtils.getSourceID(this.basicResult),[]);return t}setResultAndSelectedID(t,e){this.setPolygonList(t),e&&this.setSelectedID(e),this.render()}setResult(t){this.clearActiveStatus(),this.setPolygonList(t),this.render()}setPolygonDataByID(t,e){return this.polygonList.map(i=>i.id===e?__spreadValues(__spreadValues({},i),t):i)}rotatePolygon(t=1,e=annotation.ERotateDirection.Clockwise,i=this.selectedID){if(!i)return;const s=PolygonUtils.getPolygonByID(this.polygonList,i);if(!s)return;const o=PolygonUtils.updatePolygonByRotate(e,t,s==null?void 0:s.pointList);this.setPolygonList(this.setPolygonDataByID({pointList:o},i)),this.render()}addPointInDrawing(t){if(!this.imgInfo||this.forbidAddNewPolygonFuc&&this.forbidAddNewPolygonFuc(t))return;const{upperLimitPointNum:e,edgeAdsorption:i}=this.config;if(e&&this.drawingPointList.length>=e){this.emit("messageInfo",`${index.getMessagesByLocale(constants.EMessage.UpperLimitErrorNotice,this.lang)}${e}`);return}this.deleteSelectedID();const s=this.getCoordinateUnderZoom(t),o=AxisUtils.default.changeDrawOutsideTarget(s,{x:0,y:0},this.imgInfo,this.config.drawOutsideTarget,this.basicResult,this.zoom);if(AxisUtils.default.returnClosePointIndex(o,AxisUtils.default.changePointListByZoom(this.drawingPointList,this.zoom))===0){this.addDrawingPointToPolygonList();return}const{dropFoot:n}=PolygonUtils.getClosestPoint(o,this.polygonListUnderZoom,this.config.lineType,tool.edgeAdsorptionScope),r=AxisUtils.default.changePointByZoom(n&&t.altKey===!1&&i?n:o,1/this.zoom);if(this.pattern===tool.EPolygonPattern.Rect&&this.drawingPointList.length===2){const h=MathUtils.default.getRectangleByRightAngle(r,this.drawingPointList);if(this.drawingPointList=h,this.config.drawOutsideTarget===!1&&this.imgInfo&&this.isPolygonOutSide(this.drawingPointList)){this.emit("messageInfo",`${index.getMessagesByLocale(constants.EMessage.ForbiddenCreationOutsideBoundary,this.lang)}`),this.drawingPointList=[];return}this.addDrawingPointToPolygonList(!0);return}this.drawingPointList.push(r),this.drawingPointList.length===1?this.drawingHistory.initRecord(this.drawingPointList):this.drawingHistory.pushHistory(this.drawingPointList)}clearResult(){this.setPolygonList([]),this.deleteSelectedID(),this.render()}clearPolygonDrag(){this.drawingPointList=[],this.dragInfo=void 0,this.dragInfo=void 0,this.dragStatus=annotation.EDragStatus.Wait,this.hoverEdgeIndex=-1,this.hoverPointIndex=-1,this.hoverID=""}clearActiveStatus(){this.clearPolygonDrag(),this.deleteSelectedID()}clearDrawingStatus(){this.drawingPointList=[]}setPolygonList(t){const e=this.polygonList.length;this.polygonList=t,e!==t.length&&this.emit("updatePageNumber")}setSelectedID(t,e=!1){this.selection.setSelectedIDs(t,e),this.render(),this.emit("selectedChange")}setDefaultAttribute(t=""){const e=this.defaultAttribute;if(this.defaultAttribute=t,e!==t&&(this.changeStyle(t),this.emit("changeAttributeSidebar"),this.selectedPolygons.length>0&&(this.selectedPolygons.forEach(i=>{i.attribute=t}),this.history.pushHistory(this.polygonList),this.render()),this._textAttributeInstance)){if(this.attributeLockList.length>0&&!this.attributeLockList.includes(t)){this._textAttributeInstance.clearTextAttribute();return}this._textAttributeInstance.updateIcon(this.getTextIconSvg(t))}}setStyle(t){var e;super.setStyle(t),this._textAttributeInstance&&this.config.attributeConfigurable===!1&&((e=this._textAttributeInstance)==null||e.updateIcon(this.getTextIconSvg()))}setPolygonValidAndRender(t){if(!t)return;const e=this.polygonList.map(i=>{var s;if(i.id===t){const o=(s=i==null?void 0:i.valid)!=null?s:!0;return __spreadProps(__spreadValues({},i),{valid:!o})}return i});this.setPolygonList(e),this.history.pushHistory(this.polygonList),this.render(),this.emit("updateResult")}addDrawingPointToPolygonList(t){let{lowerLimitPointNum:e=3,minArea:i=1}=this.config;e<3&&(e=3),i<1&&(i=1);let s;if(this.drawingPointList.length<e){this.drawingPointList=[],this.editPolygonID="";return}if(PolygonUtils.calcPolygonSize(this.drawingPointList||[])<i){this.emit("messageInfo",`${index.getMessagesByLocale(constants.EMessage.MinAreaLimitErrorNotice,this.lang)}${i}`);return}const o=CommonToolUtils.getSourceID(this.basicResult),l=[...this.polygonList];if(this.editPolygonID){const n=l.find(r=>r.id===this.editPolygonID);if(!n)return;n.pointList=this.drawingPointList,this.editPolygonID=""}else{const n=uuid(8,62);let r={id:n,sourceID:o,valid:!this.isCtrl,textAttribute:"",pointList:this.drawingPointList,attribute:this.defaultAttribute,order:CommonToolUtils.getMaxOrder(l.filter(h=>CommonToolUtils.isSameSourceID(h.sourceID,o)))+1};if(this.config.textConfigurable){let h="";h=AttributeUtils.default.getTextAttribute(this.polygonList.filter(d=>CommonToolUtils.isSameSourceID(d.sourceID,o)),this.config.textCheckType),r=__spreadProps(__spreadValues({},r),{textAttribute:h})}this.pattern===tool.EPolygonPattern.Rect&&t===!0&&(r=__spreadProps(__spreadValues({},r),{isRect:!0})),l.push(r),s=r,this.setSelectedIdAfterAddingDrawing(n)}this.setPolygonList(l),this.isCtrl=!1,this.drawingPointList=[],this.history.pushHistory(l),s&&this.emit("polygonCreated",s,this.zoom,this.currentPos)}setSelectedIdAfterAddingDrawing(t){this.drawingPointList.length!==0&&(this.config.textConfigurable?this.setSelectedID(t):this.deleteSelectedID())}getHoverID(t){var e;const i=this.getCoordinateUnderZoom(t),s=this.currentShowList.map(o=>__spreadProps(__spreadValues({},o),{pointList:AxisUtils.default.changePointListByZoom(o.pointList,this.zoom)}));return PolygonUtils.getHoverPolygonID(i,s,10,(e=this.config)==null?void 0:e.lineType)}getHoverEdgeIndex(t){var e;if(!this.selectedID)return-1;const i=this.selectedPolygon;if(!i)return-1;const s=this.getCoordinateUnderZoom(t),o=AxisUtils.default.changePointListByZoom(i.pointList,this.zoom);return PolygonUtils.getHoverEdgeIndex(s,o,(e=this.config)==null?void 0:e.lineType)}getHoverPointIndex(t){if(!this.selectedID)return-1;const e=this.selectedPolygon;if(!e)return-1;const i=this.getCoordinateUnderZoom(t),s=AxisUtils.default.changePointListByZoom(e.pointList,this.zoom);return AxisUtils.default.returnClosePointIndex(i,s)}deletePolygons(t){var e;if(!t||t.length===0)return;const i=this.polygonList.filter(s=>this.selection.isIdSelected(s.id));this.emit("deletedObject",{deletedObject:i[0],id:i[0].id}),this.setPolygonList(this.polygonList.filter(s=>!this.selection.isIdSelected(s.id))),this.history.pushHistory(this.polygonList),(e=this._textAttributeInstance)==null||e.clearTextAttribute(),this.emit("selectedChange"),this.render()}deletePolygonPoint(t){if(!this.selectedID)return;const{selectedPolygon:e}=this;if(!e)return;let{lowerLimitPointNum:i}=this.config;if(i<3&&(i=3),e.pointList.length<=i){this.emit("messageInfo",`${index.getMessagesByLocale(constants.EMessage.LowerLimitErrorNotice,this.lang)}${i}`);return}e==null||e.pointList.splice(t,1),this.history.pushHistory(this.polygonList),this.render()}spaceKeydown(){var t,e,i;if(this.selectedID){if(((t=this.selectedPolygon)==null?void 0:t.isRect)===!0){this.emit("messageInfo",`${index.getMessagesByLocale(constants.EMessage.UnableToReannotation,this.lang)}`);return}this.editPolygonID=this.selectedID,this.drawingPointList=(i=(e=this.selectedPolygon)==null?void 0:e.pointList)!=null?i:[],this.drawingHistory.empty(),this.drawingHistory.initRecord(this.drawingPointList),this.hoverID="",this.deleteSelectedID(),this.render()}}onTabKeyDown(t){t.preventDefault();let e=annotation.ESortDirection.ascend;t.shiftKey&&(e=annotation.ESortDirection.descend),this.switchToNextPolygon(e)}switchToNextPolygon(t){if(this.drawingPointList.length>0)return;const[e,i]=CommonToolUtils.getRenderResultList(this.polygonList,CommonToolUtils.getSourceID(this.basicResult),this.attributeLockList,this.selectedIDs);let s=[...e];i&&(s=[...s,...i]);const o=CanvasUtils.getViewPort(this.canvas,this.currentPos,this.zoom),l=s.map(r=>{var h,d,g,a;return __spreadProps(__spreadValues({},r),{x:(d=(h=r.pointList[0])==null?void 0:h.x)!=null?d:0,y:(a=(g=r.pointList[0])==null?void 0:g.y)!=null?a:0})}).filter(r=>CanvasUtils.inViewPort({x:r.x,y:r.y},o)),n=CommonToolUtils.getNextSelectedRectID(l,t,this.selectedID);if(n){this.setSelectedID(n.id);const{selectedPolygon:r}=this;r&&this.setDefaultAttribute(r.attribute)}this.render()}onKeyDown(t){if(!CommonToolUtils.hotkeyFilter(t)||super.onKeyDown(t)===!1)return;const{keyCode:e}=t;switch(e){case keyCode.Space:this.spaceKeydown();break;case keyCode.Esc:this.drawingPointList=[],this.editPolygonID="";break;case keyCode.F:this.selectedID&&this.setPolygonValidAndRender(this.selectedID);break;case keyCode.Z:if(t.altKey){this.onCombinedExecute();return}this.setIsHidden(!this.isHidden),this.render();break;case keyCode.Delete:this.deletePolygons(this.selectedIDs),this.render();break;case keyCode.Ctrl:this.isCtrl=!0;break;case keyCode.Alt:this.isAlt===!1&&(t.preventDefault(),this.isAlt=!0,this.render());break;case keyCode.Tab:{this.onTabKeyDown(t);break}case keyCode.X:t.altKey&&this.segment();break;default:{if(this.config.attributeConfigurable){const i=AttributeUtils.default.getAttributeByKeycode(e,this.config.attributeList);i!==void 0&&this.setDefaultAttribute(i)}break}}}onKeyUp(t){if(super.onKeyUp(t),!this.selection.triggerKeyboardEvent(t,this.setPolygonList.bind(this)))switch(t.keyCode){case keyCode.Ctrl:this.isCtrl=!1;break;case keyCode.Alt:{const e=this.isAlt;this.isAlt=!1,e===!0&&this.render();break}}}rightMouseUp(t){if(this.drawingPointList.length>0){this.addDrawingPointToPolygonList();return}this.setSelectedID(this.hoverID,t.ctrlKey);const{selectedPolygon:e}=this;e&&this.setDefaultAttribute(e.attribute)}onLeftDblClick(t){if(this.hoverEdgeIndex>-1){const e=this.getCoordinateUnderZoom(t),{selectedPolygon:i}=this;if(!i)return;if(i.isRect===!0){this.emit("messageInfo",`${index.getMessagesByLocale(constants.EMessage.ForbidAddNewPoint,this.lang)}`),this.clearPolygonDrag();return}const{dropFoot:s}=PolygonUtils.getClosestPoint(e,this.polygonListUnderZoom,this.config.lineType,tool.edgeAdsorptionScope);if(!s)return;const{upperLimitPointNum:o}=this.config;if(o&&i.pointList.length>=o){this.emit("messageInfo",`${index.getMessagesByLocale(constants.EMessage.UpperLimitErrorNotice,this.lang)}${o}`),this.clearPolygonDrag();return}i==null||i.pointList.splice(this.hoverEdgeIndex+1,0,AxisUtils.default.changePointByZoom(s,1/this.zoom)),this.setPolygonDataByID(i,this.selectedID),this.history.pushHistory(this.polygonList),this.hoverPointIndex=-1,this.hoverEdgeIndex=-1,this.render()}this.dragInfo=void 0}onRightDblClick(t){this.dragInfo=void 0,this.clearImgDrag();const e=this.getHoverID(t),i=this.getHoverPointIndex(t);if(this.hoverPointIndex>-1&&this.hoverPointIndex===i){this.deletePolygonPoint(i),this.dragInfo=void 0,this.hoverPointIndex=-1,this.render();return}this.hoverID===this.selectedID&&this.deletePolygons([e]),this.render()}onMouseDown(t){if(!(super.onMouseDown(t)||this.forbidMouseOperation||t.ctrlKey===!0))return this.dragMouseDown(t),!0}dragMouseDown(t){if(t.button!==0)return;const e=this.getHoverID(t);if(!this.selection.isIdSelected(e))return;let i=[0],s=annotation.EDragTarget.Plane;const o=this.selectedPolygons[0].pointList,l=this.getCoordinateUnderZoom(t);if(this.selectedPolygons.length===1){this.dragStatus=annotation.EDragStatus.Start;const n=this.getHoverPointIndex(t),r=this.getHoverEdgeIndex(t);n>-1?(s=annotation.EDragTarget.Point,i=[n]):r>-1&&this.hoverEdgeIndex>-1&&(s=annotation.EDragTarget.Line,i=[r,(r+1)%o.length])}this.dragInfo={dragStartCoord:l,dragTarget:s,initPointList:o,changePointIndex:i,originPolygon:this.selectedPolygon,selectedPolygons:this.selectedPolygons,dragPrevCoord:l,originPolygonList:this.polygonList}}segment(){var t;if(!this.selectedID||((t=this.config)==null?void 0:t.lineType)!==tool.ELineTypes.Line)return;const e=PolygonUtils.getPolygonPointList(this.selectedID,this.currentShowList),i=this.currentShowList.filter(l=>l.id!==this.selectedID);if(i.length===0||e.length===0)return;const s=PolygonUtils.getWrapPolygonIndex(e,i);let o=[...this.polygonList];if(s===-1){const l=PolygonUtils.segmentPolygonByPolygon(e,i);if(!l)return;const n=l.shift();if(!n)return;let r="",h=!0;const d=CommonToolUtils.getSourceID(this.basicResult);let g="";o=this.polygonList.map(a=>{var u,f;return a.id===this.selectedID?(r=a.attribute,h=(u=a==null?void 0:a.valid)!=null?u:!0,g=(f=a==null?void 0:a.textAttribute)!=null?f:"",__spreadProps(__spreadValues({},a),{pointList:n})):a}),l.length>0&&l.forEach((a,u)=>{o.push({sourceID:d,id:uuid(8,62),pointList:a,valid:h,order:CommonToolUtils.getMaxOrder(this.currentShowList)+1+u,attribute:r,textAttribute:g})})}else o[s].pointList=PolygonUtils.clipPolygonFromWrapPolygon(e,o[s].pointList),o=o.filter(l=>l.id!==this.selectedID);this.setPolygonList(o),this.history.pushHistory(o),this.render()}onCombinedExecute(){if(!this.selectedID){this.emit("messageInfo",lbUtils.i18n.t("PolygonsToBeCombinedNeedToBeSelected"));return}this.isCombined=!this.isCombined}combine(t){var e;const i=this.getHoverID(t);if(!i||!this.selectedID||this.selectedID===i)return;if(((e=this.config)==null?void 0:e.lineType)!==tool.ELineTypes.Line){this.emit("messageInfo",lbUtils.i18n.t("CurveModeDoesNotSupportCutting"));return}const s=this.polygonList.find(h=>h.id===this.selectedID),o=this.currentShowList.find(h=>h.id===i);if(!o||!s)return;const l=PolygonUtils.combinePolygonWithPolygon(s,o);if(!l)return;const{newPolygon:n,unionList:r}=l;if(r.length===1&&n){const h=this.polygonList.filter(d=>!r.includes(d.id)).map(d=>d.id===this.selectedID?n:d);this.setPolygonList(h),this.history.pushHistory(h),this.render(),this.emit("messageInfo",lbUtils.i18n.t("CombineSuccess"))}else this.emit("messageInfo",lbUtils.i18n.t("CombiningFailedNotify"));this.isCombined=!1}isPolygonOutSide(t){if(this.dependToolName&&this.basicCanvas&&this.basicResult){let n=!1;switch(this.dependToolName){case tool.EToolName.Rect:{n=t.filter(r=>!RectUtils.isInRect(r,this.basicResult)).length>0;break}case tool.EToolName.Polygon:{n=PolygonUtils.isPointListOutSidePolygon(t,this.basicResult.pointList,this.config.lineType);break}}return n}if(!this.imgInfo)return!1;const{left:e,top:i,right:s,bottom:o}=MathUtils.default.calcViewportBoundaries(AxisUtils.default.changePointListByZoom(t,this.zoom)),l=1e-5;return e<0||i<0||s>this.imgInfo.width+l||o>this.imgInfo.height+l}onDragMove(t){const e=this.polygonList.map(i=>{if(this.selectedIDs.includes(i.id)){const s=this.dragPolygon(t,i);if(!s)return i;const o=__spreadProps(__spreadValues({},i),{pointList:s});return i.isRect===!0&&this.pattern===tool.EPolygonPattern.Normal&&Object.assign(o,{isRect:!1}),o}return i});this.dragInfo.dragPrevCoord=this.getCoordinateUnderZoom(t),this.setPolygonList(e),this.render()}getDragOffset(t,e){var i;const s=this.getCoordinateUnderZoom(t),{dragTarget:o,dragPrevCoord:l,changePointIndex:n,initPointList:r,dragStartCoord:h}=this.dragInfo;if(this.pattern===tool.EPolygonPattern.Rect&&(e==null?void 0:e.isRect)===!0&&n&&[annotation.EDragTarget.Line].includes(o)){const d=MathUtils.default.getArrayIndex(n[0]-2,4),g=MathUtils.default.getArrayIndex(n[0]-1,4),a=[r[d],r[g]],u=MathUtils.default.getRectPerpendicularOffset(h,s,a);return{x:u.x/this.zoom,y:u.y/this.zoom}}return((i=this.dragInfo)==null?void 0:i.dragTarget)===annotation.EDragTarget.Plane?{x:(s.x-l.x)/this.zoom,y:(s.y-l.y)/this.zoom}:{x:(s.x-h.x)/this.zoom,y:(s.y-h.y)/this.zoom}}dragPolygon(t,e){let i=___default.default.cloneDeep(e==null?void 0:e.pointList);if(!i||!this.dragInfo)return;const{initPointList:s,dragTarget:o,changePointIndex:l}=this.dragInfo,n=this.getDragOffset(t,e);switch(this.dragStatus=annotation.EDragStatus.Move,o){case annotation.EDragTarget.Plane:i=i.map(r=>__spreadProps(__spreadValues({},r),{x:r.x+n.x,y:r.y+n.y}));break;case annotation.EDragTarget.Point:case annotation.EDragTarget.Line:i=i.map((r,h)=>l&&l.includes(h)?__spreadProps(__spreadValues({},r),{x:s[h].x+n.x,y:s[h].y+n.y}):r);break}if(this.pattern===tool.EPolygonPattern.Rect&&(e==null?void 0:e.isRect)===!0&&o===annotation.EDragTarget.Point&&l&&(i=MathUtils.default.getPointListFromPointOffset(s,l[0],n)),!(this.config.drawOutsideTarget===!1&&this.imgInfo&&this.isPolygonOutSide(i)))return i}onMouseMove(t){if(super.onMouseMove(t)||this.forbidMouseOperation||!this.imgInfo)return;let e=-1,i=-1;const{selectedID:s}=this;if(s&&(this.hoverEdgeIndex=-1,this.hoverPointIndex=-1,e=this.getHoverPointIndex(t),e>-1?this.hoverPointIndex=e:(i=this.getHoverEdgeIndex(t),this.hoverEdgeIndex=i)),this.drawingPointList.length>0)return;const o=this.getHoverID(t);this.hoverID!==o&&(this.hoverID=o,this.render()),this.selectedIDs.length>0&&this.dragInfo&&this.onDragMove(t)}leftMouseUpdateValid(t){const e=this.getHoverID(t);return this.drawingPointList.length===0&&t.ctrlKey===!0&&e?(this.setPolygonValidAndRender(e),!0):!1}leftMouseUp(t){this.leftMouseUpdateValid(t)||this.addPointInDrawing(t)}onMouseUp(t){if(this.isCombined){switch(t.button){case 0:this.combine(t);break;case 2:this.isCombined=!1;break;default:return}return}if(!(super.onMouseUp(t)||this.forbidMouseOperation||!this.imgInfo)){if(this.dragInfo&&this.dragStatus===annotation.EDragStatus.Move){this.emit("updateResult"),this.emitUpdatePolygonByDrag(),this.dragInfo=void 0,this.dragStatus=annotation.EDragStatus.Wait,this.history.pushHistory(this.polygonList);return}switch(t.button){case 0:{this.leftMouseUp(t);break}case 2:{this.rightMouseUp(t);break}}this.render()}}dragMouseUp(){this.dragStatus===annotation.EDragStatus.Start&&(this.dragInfo=void 0,this.dragStatus=annotation.EDragStatus.Wait)}exportData(){const{polygonList:t}=this;return[t,this.basicImgInfo]}getTextIconSvg(t=""){return AttributeUtils.default.getTextIconSvg(t,this.config.attributeList,this.config.attributeConfigurable,this.baseIcon)}getCurrentSelectedData(){const{selectedPolygon:t}=this;if(!t)return;const e=this.getColor(t.attribute),i=t.valid?e==null?void 0:e.valid.stroke:e==null?void 0:e.invalid.stroke;return{width:TEXT_MAX_WIDTH,textAttribute:t.textAttribute,color:i}}updateSelectedTextAttribute(t){if(this._textAttributeInstance&&t&&this.selectedID){let e=t;AttributeUtils.default.textAttributeValidate(this.config.textCheckType,"",e)===!1&&(this.emit("messageError",AttributeUtils.default.getErrorNotice(this.config.textCheckType,this.lang)),e=""),this.setPolygonList(AttributeUtils.default.textChange(e,this.selectedID,this.polygonList)),this.emit("updateTextAttribute"),this.render()}}renderTextAttribute(){var t;const{selectedPolygon:e}=this;if(!this.ctx||this.config.textConfigurable===!1||!e)return;const{pointList:i,attribute:s,valid:o,textAttribute:l}=e,{x:n,y:r}=i[i.length-1],h=TEXT_MAX_WIDTH,d=AxisUtils.default.getOffsetCoordinate({x:n,y:r},this.currentPos,this.zoom),g=this.getColor(s),a=o?g==null?void 0:g.valid.stroke:g==null?void 0:g.invalid.stroke;this._textAttributeInstance||(this._textAttributeInstance=new textAttributeClass({width:h,container:this.container,icon:this.getTextIconSvg(s),color:a,getCurrentSelectedData:this.getCurrentSelectedData,updateSelectedTextAttribute:this.updateSelectedTextAttribute})),this._textAttributeInstance&&!((t=this._textAttributeInstance)==null?void 0:t.isExit)&&this._textAttributeInstance.appendToContainer(),this._textAttributeInstance.update(`${l}`,{left:d.x,top:d.y,color:a,width:h})}renderStaticPolygon(){var t;this.isHidden===!1&&((t=this.visiblePolygonList)==null||t.forEach(e=>{var i,s,o,l,n;if([this.selectedID,this.editPolygonID].includes(e.id))return;const{textAttribute:r,attribute:h}=e,d=this.getColor(h),g=StyleUtils.getStrokeAndFill(d,e.valid),a=AxisUtils.default.changePointListByZoom(e.pointList||[],this.zoom,this.currentPos);DrawUtils.drawPolygonWithFillAndLine(this.canvas,a,{fillColor:g.fill,strokeColor:g.stroke,pointColor:"white",thickness:(s=(i=this.style)==null?void 0:i.width)!=null?s:2,lineCap:"round",isClose:!0,lineType:(o=this.config)==null?void 0:o.lineType});let u=`${(l=AttributeUtils.default.getAttributeShowText(h,this.config.attributeList))!=null?l:""}`;((n=this.config)==null?void 0:n.isShowOrder)&&(e==null?void 0:e.order)>0&&(u=`${e.order} ${u}`),DrawUtils.drawText(this.canvas,a[0],u,__spreadValues({color:g.stroke},annotation.DEFAULT_TEXT_OFFSET));const f=a[a.length-1];DrawUtils.drawText(this.canvas,{x:f.x+annotation.TEXT_ATTRIBUTE_OFFSET.x,y:f.y+annotation.TEXT_ATTRIBUTE_OFFSET.y},r,__spreadValues({color:g.stroke},annotation.DEFAULT_TEXT_OFFSET))}))}renderSelectedPolygons(){this.selectedPolygons.forEach(t=>{this.renderSelectedPolygon(t)})}renderSelectedPolygon(t){var e,i,s;if(t){const o=this.getColor(t.attribute),l=StyleUtils.getStrokeAndFill(o,t.valid,{isSelected:!0});DrawUtils.drawSelectedPolygonWithFillAndLine(this.canvas,AxisUtils.default.changePointListByZoom(t.pointList,this.zoom,this.currentPos),{fillColor:l.fill,strokeColor:l.stroke,pointColor:"white",thickness:2,lineCap:"round",isClose:!0,lineType:(e=this.config)==null?void 0:e.lineType});let n=`${(i=AttributeUtils.default.getAttributeShowText(t.attribute,this.config.attributeList))!=null?i:""}`;((s=this.config)==null?void 0:s.isShowOrder)&&(t==null?void 0:t.order)>0&&(n=`${t.order} ${n}`),DrawUtils.drawText(this.canvas,AxisUtils.default.changePointByZoom(t.pointList[0],this.zoom,this.currentPos),n,__spreadValues({color:l.stroke},annotation.DEFAULT_TEXT_OFFSET)),this.renderTextAttribute()}}renderHoverPolygon(){var t;if(this.hoverID&&this.hoverID!==this.editPolygonID){const{hoverPolygon:e}=this;if(e){let i="";const s=this.getColor(e.attribute);e.valid?i=s.validHover.fill:i=StyleUtils.getStrokeAndFill(s,!1,{isHover:!0}).fill,DrawUtils.drawPolygonWithFill(this.canvas,AxisUtils.default.changePointListByZoom(e.pointList,this.zoom,this.currentPos),{color:i,lineType:(t=this.config)==null?void 0:t.lineType})}}}renderPolygon(){var t,e,i,s;this.renderStaticPolygon(),this.renderHoverPolygon(),this.renderSelectedPolygons();const o=this.getColor(this.defaultAttribute),l=StyleUtils.getStrokeAndFill(o,!this.isCtrl);if(((t=this.drawingPointList)==null?void 0:t.length)>0){let n=[...this.drawingPointList],r=AxisUtils.default.getOriginCoordinateWithOffsetCoordinate(this.coord,this.zoom,this.currentPos);if(this.pattern===tool.EPolygonPattern.Rect&&n.length===2)n=MathUtils.default.getRectangleByRightAngle(r,n);else{if(((e=this.config)==null?void 0:e.edgeAdsorption)&&this.isAlt===!1){const{dropFoot:h}=PolygonUtils.getClosestPoint(r,this.polygonList,(i=this.config)==null?void 0:i.lineType,tool.edgeAdsorptionScope/this.zoom);h&&(r=h)}n.push(r)}DrawUtils.drawSelectedPolygonWithFillAndLine(this.canvas,AxisUtils.default.changePointListByZoom(n,this.zoom,this.currentPos),{fillColor:l.fill,strokeColor:l.stroke,pointColor:"white",thickness:2,lineCap:"round",isClose:!1,lineType:this.config.lineType})}if(this.hoverPointIndex>-1&&this.selectedID){const n=this.selectedPolygon;if(!n)return;const r=StyleUtils.getStrokeAndFill(o,n.valid,{isSelected:!0}),h=n==null?void 0:n.pointList[this.hoverPointIndex];if(h){const{x:d,y:g}=AxisUtils.default.changePointByZoom(h,this.zoom,this.currentPos);DrawUtils.drawCircleWithFill(this.canvas,{x:d,y:g},5,{color:r.fill})}}if(this.hoverEdgeIndex>-1&&this.selectedID){const n=this.selectedPolygon;if(!n)return;const r=StyleUtils.getStrokeAndFill(o,n.valid,{isSelected:!0});DrawUtils.drawLineWithPointList(this.canvas,AxisUtils.default.changePointListByZoom(n.pointList,this.zoom,this.currentPos),{color:r.stroke,thickness:10,hoverEdgeIndex:this.hoverEdgeIndex,lineType:(s=this.config)==null?void 0:s.lineType})}}render(){!this.ctx||(super.render(),this.renderPolygon(),this.renderCursorLine(this.getLineColor(this.defaultAttribute)))}renderCursorLine(t){if(super.renderCursorLine(t),this.isCombined){const{x:e,y:i}=this.coord,s=10,o=186,l=32;DrawUtils.drawRectWithFill(this.canvas,{x:e+s,y:i-s*4-1,width:o,height:l},{color:"black"}),DrawUtils.drawText(this.canvas,{x:e,y:i},lbUtils.i18n.t("ClickAnotherPolygon"),{textAlign:"center",color:"white",offsetX:o/2+s,offsetY:-(l/2+s/2)}),DrawUtils.drawRect(this.canvas,{x:e-s,y:i-s,width:s*2,height:s*2},{lineDash:[6],color:"white"})}}undo(){if(this.drawingPointList.length>0){const e=this.drawingHistory.undo();if(!e)return;this.drawingPointList=e,this.render();return}const t=this.history.undo();t&&(t.length!==this.polygonList.length&&this.deleteSelectedID(),this.setPolygonList(t),this.render())}redo(){if(this.drawingPointList.length>0){const e=this.drawingHistory.redo();if(!e)return;this.drawingPointList=e,this.render();return}const t=this.history.redo();t&&(t.length!==this.polygonList.length&&this.deleteSelectedID(),this.setPolygonList(t),this.render())}deleteSelectedID(){this.setSelectedID("")}}module.exports=PolygonOperation;
|
|
1
|
+
"use strict";var _=require("lodash"),lbUtils=require("@labelbee/lb-utils"),MathUtils=require("../../utils/MathUtils.js"),RectUtils=require("../../utils/tool/RectUtils.js"),annotation=require("../../constant/annotation.js"),keyCode=require("../../constant/keyCode.js"),tool=require("../../constant/tool.js"),index=require("../../locales/index.js"),constants=require("../../locales/constants.js"),ActionsHistory=require("../../utils/ActionsHistory.js"),AttributeUtils=require("../../utils/tool/AttributeUtils.js"),AxisUtils=require("../../utils/tool/AxisUtils.js"),CanvasUtils=require("../../utils/tool/CanvasUtils.js"),CommonToolUtils=require("../../utils/tool/CommonToolUtils.js"),DrawUtils=require("../../utils/tool/DrawUtils.js"),PolygonUtils=require("../../utils/tool/PolygonUtils.js"),StyleUtils=require("../../utils/tool/StyleUtils.js"),uuid=require("../../utils/uuid.js"),basicToolOperation=require("./basicToolOperation.js"),textAttributeClass=require("./textAttributeClass.js"),Selection=require("./Selection.js");function _interopDefaultLegacy(c){return c&&typeof c=="object"&&"default"in c?c:{default:c}}var ___default=_interopDefaultLegacy(_),__defProp=Object.defineProperty,__defProps=Object.defineProperties,__getOwnPropDescs=Object.getOwnPropertyDescriptors,__getOwnPropSymbols=Object.getOwnPropertySymbols,__hasOwnProp=Object.prototype.hasOwnProperty,__propIsEnum=Object.prototype.propertyIsEnumerable,__defNormalProp=(c,t,e)=>t in c?__defProp(c,t,{enumerable:!0,configurable:!0,writable:!0,value:e}):c[t]=e,__spreadValues=(c,t)=>{for(var e in t||(t={}))__hasOwnProp.call(t,e)&&__defNormalProp(c,e,t[e]);if(__getOwnPropSymbols)for(var e of __getOwnPropSymbols(t))__propIsEnum.call(t,e)&&__defNormalProp(c,e,t[e]);return c},__spreadProps=(c,t)=>__defProps(c,__getOwnPropDescs(t));const TEXT_MAX_WIDTH=164;class PolygonOperation extends basicToolOperation.BasicToolOperation{constructor(t){super(t);this.isAllowDouble=e=>{const{selectedID:i}=this,s=this.getHoverID(e);return!!(i&&i===s)},this.textChange=e=>{this.config.textConfigurable===!1||!this.selectedID||(this.setPolygonList(AttributeUtils.default.textChange(e,this.selectedID,this.polygonList)),this.emit("selectedChange"),this.render())},this.emitUpdatePolygonByDrag=()=>{if(this.dragInfo){const{originPolygon:e}=this.dragInfo;this.emit("updatePolygonByDrag",[{newPolygon:this.selectedPolygon,originPolygon:e}])}},this.config=CommonToolUtils.jsonParser(t.config),this.drawingPointList=[],this.polygonList=[],this.hoverPointIndex=-1,this.hoverEdgeIndex=-1,this.drawingHistory=new ActionsHistory,this.isCtrl=!1,this.isAlt=!1,this.isCombined=!1,this.pattern=tool.EPolygonPattern.Normal,this.getCurrentSelectedData=this.getCurrentSelectedData.bind(this),this.updateSelectedTextAttribute=this.updateSelectedTextAttribute.bind(this),this.selection=new Selection(this)}get selectedIDs(){return this.selection.selectedIDs}get selectedID(){return this.selection.selectedID}get minArea(){return this.config.minArea||0}eventBinding(){super.eventBinding(),this.container.removeEventListener("mouseup",this.onMouseUp),this.container.addEventListener("mouseup",this.dragMouseUp),this.dblClickListener.addEvent(this.onMouseUp,this.onLeftDblClick,this.onRightDblClick,this.isAllowDouble)}eventUnbinding(){super.eventUnbinding(),this.container.removeEventListener("mouseup",this.dragMouseUp)}destroy(){super.destroy(),this._textAttributeInstance&&this._textAttributeInstance.clearTextAttribute()}get selectedPolygon(){return PolygonUtils.getPolygonByID(this.visiblePolygonList,this.selectedID)}get selectedPolygons(){return PolygonUtils.getPolygonByIDs(this.polygonList,this.selectedIDs)}get hoverPolygon(){return this.visiblePolygonList.find(t=>t.id===this.hoverID&&t.id!==this.selectedID)}get enableDrag(){return Boolean(this.selectedID&&this.dragInfo)}get visiblePolygonList(){return this.polygonList}get polygonListUnderZoom(){return this.visiblePolygonList.map(t=>__spreadProps(__spreadValues({},t),{pointList:AxisUtils.default.changePointListByZoom(t.pointList,this.zoom)}))}get selectedText(){var t;return(t=this.selectedPolygon)==null?void 0:t.textAttribute}get dataList(){return this.polygonList}setPattern(t,e=!1){var i;((i=this.drawingPointList)==null?void 0:i.length)>0&&e===!0||(this.pattern=t)}get currentShowList(){let t=[];const[e,i]=CommonToolUtils.getRenderResultList(this.polygonList,CommonToolUtils.getSourceID(this.basicResult),this.attributeLockList,this.selectedIDs);return t=e,this.isHidden&&(t=[]),i&&t.push(...i),t}get currentPageResult(){const[t]=CommonToolUtils.getRenderResultList(this.polygonList,CommonToolUtils.getSourceID(this.basicResult),[]);return t}setResultAndSelectedID(t,e){this.setPolygonList(t),e&&this.setSelectedID(e),this.render()}setResult(t){this.clearActiveStatus(),this.setPolygonList(t),this.render()}setPolygonDataByID(t,e){return this.polygonList.map(i=>i.id===e?__spreadValues(__spreadValues({},i),t):i)}rotatePolygon(t=1,e=annotation.ERotateDirection.Clockwise,i=this.selectedID){if(!i)return;const s=PolygonUtils.getPolygonByID(this.polygonList,i);if(!s)return;const o=PolygonUtils.updatePolygonByRotate(e,t,s==null?void 0:s.pointList);this.setPolygonList(this.setPolygonDataByID({pointList:o},i)),this.render()}addPointInDrawing(t){if(!this.imgInfo||this.forbidAddNewPolygonFuc&&this.forbidAddNewPolygonFuc(t))return;const{upperLimitPointNum:e,edgeAdsorption:i}=this.config;if(e&&this.drawingPointList.length>=e){this.emit("messageInfo",`${index.getMessagesByLocale(constants.EMessage.UpperLimitErrorNotice,this.lang)}${e}`);return}this.deleteSelectedID();const s=this.getCoordinateUnderZoom(t),o=AxisUtils.default.changeDrawOutsideTarget(s,{x:0,y:0},this.imgInfo,this.config.drawOutsideTarget,this.basicResult,this.zoom);if(AxisUtils.default.returnClosePointIndex(o,AxisUtils.default.changePointListByZoom(this.drawingPointList,this.zoom))===0){this.addDrawingPointToPolygonList();return}const{dropFoot:n}=PolygonUtils.getClosestPoint(o,this.polygonListUnderZoom,this.config.lineType,tool.edgeAdsorptionScope),r=AxisUtils.default.changePointByZoom(n&&t.altKey===!1&&i?n:o,1/this.zoom);if(this.pattern===tool.EPolygonPattern.Rect&&this.drawingPointList.length===2){const h=MathUtils.default.getRectangleByRightAngle(r,this.drawingPointList);if(this.drawingPointList=h,this.config.drawOutsideTarget===!1&&this.imgInfo&&this.isPolygonOutSide(this.drawingPointList)){this.emit("messageInfo",`${index.getMessagesByLocale(constants.EMessage.ForbiddenCreationOutsideBoundary,this.lang)}`),this.drawingPointList=[];return}this.addDrawingPointToPolygonList(!0);return}this.drawingPointList.push(r),this.drawingPointList.length===1?this.drawingHistory.initRecord(this.drawingPointList):this.drawingHistory.pushHistory(this.drawingPointList)}clearResult(){this.setPolygonList([]),this.deleteSelectedID(),this.render()}clearPolygonDrag(){this.drawingPointList=[],this.dragInfo=void 0,this.dragInfo=void 0,this.dragStatus=annotation.EDragStatus.Wait,this.hoverEdgeIndex=-1,this.hoverPointIndex=-1,this.hoverID=""}clearActiveStatus(){this.clearPolygonDrag(),this.deleteSelectedID()}clearDrawingStatus(){this.drawingPointList=[]}setPolygonList(t){const e=this.polygonList.length;this.polygonList=t,e!==t.length&&this.emit("updatePageNumber")}setSelectedID(t,e=!1){this.selection.setSelectedIDs(t,e),this.render(),this.emit("selectedChange")}setDefaultAttribute(t=""){const e=this.defaultAttribute;if(this.defaultAttribute=t,e!==t&&(this.changeStyle(t),this.emit("changeAttributeSidebar"),this.selectedPolygons.length>0&&(this.selectedPolygons.forEach(i=>{i.attribute=t}),this.history.pushHistory(this.polygonList),this.render()),this._textAttributeInstance)){if(this.attributeLockList.length>0&&!this.attributeLockList.includes(t)){this._textAttributeInstance.clearTextAttribute();return}this._textAttributeInstance.updateIcon(this.getTextIconSvg(t))}}setStyle(t){var e;super.setStyle(t),this._textAttributeInstance&&this.config.attributeConfigurable===!1&&((e=this._textAttributeInstance)==null||e.updateIcon(this.getTextIconSvg()))}setPolygonValidAndRender(t){if(!t)return;const e=this.polygonList.map(i=>{var s;if(i.id===t){const o=(s=i==null?void 0:i.valid)!=null?s:!0;return __spreadProps(__spreadValues({},i),{valid:!o})}return i});this.setPolygonList(e),this.history.pushHistory(this.polygonList),this.render(),this.emit("updateResult")}addDrawingPointToPolygonList(t){let{lowerLimitPointNum:e=3}=this.config;e<3&&(e=3);let i;if(this.drawingPointList.length<e){this.drawingPointList=[],this.editPolygonID="";return}if(PolygonUtils.calcPolygonSize(this.drawingPointList||[])<this.minArea){this.emit("messageInfo",`${index.getMessagesByLocale(constants.EMessage.MinAreaLimitErrorNotice,this.lang)}${this.minArea}`);return}const s=CommonToolUtils.getSourceID(this.basicResult),o=[...this.polygonList];if(this.editPolygonID){const l=o.find(n=>n.id===this.editPolygonID);if(!l)return;l.pointList=this.drawingPointList,this.editPolygonID=""}else{const l=uuid(8,62);let n={id:l,sourceID:s,valid:!this.isCtrl,textAttribute:"",pointList:this.drawingPointList,attribute:this.defaultAttribute,order:CommonToolUtils.getMaxOrder(o.filter(r=>CommonToolUtils.isSameSourceID(r.sourceID,s)))+1};if(this.config.textConfigurable){let r="";r=AttributeUtils.default.getTextAttribute(this.polygonList.filter(h=>CommonToolUtils.isSameSourceID(h.sourceID,s)),this.config.textCheckType),n=__spreadProps(__spreadValues({},n),{textAttribute:r})}this.pattern===tool.EPolygonPattern.Rect&&t===!0&&(n=__spreadProps(__spreadValues({},n),{isRect:!0})),o.push(n),i=n,this.setSelectedIdAfterAddingDrawing(l)}this.setPolygonList(o),this.isCtrl=!1,this.drawingPointList=[],this.history.pushHistory(o),i&&this.emit("polygonCreated",i,this.zoom,this.currentPos)}setSelectedIdAfterAddingDrawing(t){this.drawingPointList.length!==0&&(this.config.textConfigurable?this.setSelectedID(t):this.deleteSelectedID())}getHoverID(t){var e;const i=this.getCoordinateUnderZoom(t),s=this.currentShowList.map(o=>__spreadProps(__spreadValues({},o),{pointList:AxisUtils.default.changePointListByZoom(o.pointList,this.zoom)}));return PolygonUtils.getHoverPolygonID(i,s,10,(e=this.config)==null?void 0:e.lineType)}getHoverEdgeIndex(t){var e;if(!this.selectedID)return-1;const i=this.selectedPolygon;if(!i)return-1;const s=this.getCoordinateUnderZoom(t),o=AxisUtils.default.changePointListByZoom(i.pointList,this.zoom);return PolygonUtils.getHoverEdgeIndex(s,o,(e=this.config)==null?void 0:e.lineType)}getHoverPointIndex(t){if(!this.selectedID)return-1;const e=this.selectedPolygon;if(!e)return-1;const i=this.getCoordinateUnderZoom(t),s=AxisUtils.default.changePointListByZoom(e.pointList,this.zoom);return AxisUtils.default.returnClosePointIndex(i,s)}deletePolygons(t){var e;if(!t||t.length===0)return;const i=this.polygonList.filter(s=>this.selection.isIdSelected(s.id));this.emit("deletedObject",{deletedObject:i[0],id:i[0].id}),this.setPolygonList(this.polygonList.filter(s=>!this.selection.isIdSelected(s.id))),this.history.pushHistory(this.polygonList),(e=this._textAttributeInstance)==null||e.clearTextAttribute(),this.emit("selectedChange"),this.render()}deletePolygonPoint(t){if(!this.selectedID)return;const{selectedPolygon:e}=this;if(!e)return;let{lowerLimitPointNum:i}=this.config;if(i<3&&(i=3),e.pointList.length<=i){this.emit("messageInfo",`${index.getMessagesByLocale(constants.EMessage.LowerLimitErrorNotice,this.lang)}${i}`);return}e==null||e.pointList.splice(t,1),this.history.pushHistory(this.polygonList),this.render()}spaceKeydown(){var t,e,i;if(this.selectedID){if(((t=this.selectedPolygon)==null?void 0:t.isRect)===!0){this.emit("messageInfo",`${index.getMessagesByLocale(constants.EMessage.UnableToReannotation,this.lang)}`);return}this.editPolygonID=this.selectedID,this.drawingPointList=(i=(e=this.selectedPolygon)==null?void 0:e.pointList)!=null?i:[],this.drawingHistory.empty(),this.drawingHistory.initRecord(this.drawingPointList),this.hoverID="",this.deleteSelectedID(),this.render()}}onTabKeyDown(t){t.preventDefault();let e=annotation.ESortDirection.ascend;t.shiftKey&&(e=annotation.ESortDirection.descend),this.switchToNextPolygon(e)}switchToNextPolygon(t){if(this.drawingPointList.length>0)return;const[e,i]=CommonToolUtils.getRenderResultList(this.polygonList,CommonToolUtils.getSourceID(this.basicResult),this.attributeLockList,this.selectedIDs);let s=[...e];i&&(s=[...s,...i]);const o=CanvasUtils.getViewPort(this.canvas,this.currentPos,this.zoom),l=s.map(r=>{var h,d,g,a;return __spreadProps(__spreadValues({},r),{x:(d=(h=r.pointList[0])==null?void 0:h.x)!=null?d:0,y:(a=(g=r.pointList[0])==null?void 0:g.y)!=null?a:0})}).filter(r=>CanvasUtils.inViewPort({x:r.x,y:r.y},o)),n=CommonToolUtils.getNextSelectedRectID(l,t,this.selectedID);if(n){this.setSelectedID(n.id);const{selectedPolygon:r}=this;r&&this.setDefaultAttribute(r.attribute)}this.render()}onKeyDown(t){if(!CommonToolUtils.hotkeyFilter(t)||super.onKeyDown(t)===!1)return;const{keyCode:e}=t;switch(e){case keyCode.Space:this.spaceKeydown();break;case keyCode.Esc:this.drawingPointList=[],this.editPolygonID="";break;case keyCode.F:this.selectedID&&this.setPolygonValidAndRender(this.selectedID);break;case keyCode.Z:if(t.altKey){this.onCombinedExecute();return}this.setIsHidden(!this.isHidden),this.render();break;case keyCode.Delete:this.deletePolygons(this.selectedIDs),this.render();break;case keyCode.Ctrl:this.isCtrl=!0;break;case keyCode.Alt:this.isAlt===!1&&(t.preventDefault(),this.isAlt=!0,this.render());break;case keyCode.Tab:{this.onTabKeyDown(t);break}case keyCode.X:t.altKey&&this.segment();break;default:{if(this.config.attributeConfigurable){const i=AttributeUtils.default.getAttributeByKeycode(e,this.config.attributeList);i!==void 0&&this.setDefaultAttribute(i)}break}}}onKeyUp(t){if(super.onKeyUp(t),!this.selection.triggerKeyboardEvent(t,this.setPolygonList.bind(this)))switch(t.keyCode){case keyCode.Ctrl:this.isCtrl=!1;break;case keyCode.Alt:{const e=this.isAlt;this.isAlt=!1,e===!0&&this.render();break}}}rightMouseUp(t){if(this.drawingPointList.length>0){this.addDrawingPointToPolygonList();return}this.setSelectedID(this.hoverID,t.ctrlKey);const{selectedPolygon:e}=this;e&&this.setDefaultAttribute(e.attribute)}onLeftDblClick(t){if(this.hoverEdgeIndex>-1){const e=this.getCoordinateUnderZoom(t),{selectedPolygon:i}=this;if(!i)return;if(i.isRect===!0){this.emit("messageInfo",`${index.getMessagesByLocale(constants.EMessage.ForbidAddNewPoint,this.lang)}`),this.clearPolygonDrag();return}const{dropFoot:s}=PolygonUtils.getClosestPoint(e,this.polygonListUnderZoom,this.config.lineType,tool.edgeAdsorptionScope);if(!s)return;const{upperLimitPointNum:o}=this.config;if(o&&i.pointList.length>=o){this.emit("messageInfo",`${index.getMessagesByLocale(constants.EMessage.UpperLimitErrorNotice,this.lang)}${o}`),this.clearPolygonDrag();return}i==null||i.pointList.splice(this.hoverEdgeIndex+1,0,AxisUtils.default.changePointByZoom(s,1/this.zoom)),this.setPolygonDataByID(i,this.selectedID),this.history.pushHistory(this.polygonList),this.hoverPointIndex=-1,this.hoverEdgeIndex=-1,this.render()}this.dragInfo=void 0}onRightDblClick(t){this.dragInfo=void 0,this.clearImgDrag();const e=this.getHoverID(t),i=this.getHoverPointIndex(t);if(this.hoverPointIndex>-1&&this.hoverPointIndex===i){this.deletePolygonPoint(i),this.dragInfo=void 0,this.hoverPointIndex=-1,this.render();return}this.hoverID===this.selectedID&&this.deletePolygons([e]),this.render()}onMouseDown(t){if(!(super.onMouseDown(t)||this.forbidMouseOperation||t.ctrlKey===!0))return this.dragMouseDown(t),!0}dragMouseDown(t){if(t.button!==0)return;const e=this.getHoverID(t);if(!this.selection.isIdSelected(e))return;let i=[0],s=annotation.EDragTarget.Plane;const o=this.selectedPolygons[0].pointList,l=this.getCoordinateUnderZoom(t);if(this.selectedPolygons.length===1){this.dragStatus=annotation.EDragStatus.Start;const n=this.getHoverPointIndex(t),r=this.getHoverEdgeIndex(t);n>-1?(s=annotation.EDragTarget.Point,i=[n]):r>-1&&this.hoverEdgeIndex>-1&&(s=annotation.EDragTarget.Line,i=[r,(r+1)%o.length])}this.dragInfo={dragStartCoord:l,dragTarget:s,initPointList:o,changePointIndex:i,originPolygon:this.selectedPolygon,selectedPolygons:this.selectedPolygons,dragPrevCoord:l,originPolygonList:this.polygonList}}segment(){var t;if(!this.selectedID||((t=this.config)==null?void 0:t.lineType)!==tool.ELineTypes.Line)return;const e=PolygonUtils.getPolygonPointList(this.selectedID,this.currentShowList),i=this.currentShowList.filter(l=>l.id!==this.selectedID);if(i.length===0||e.length===0)return;const s=PolygonUtils.getWrapPolygonIndex(e,i);let o=[...this.polygonList];if(s===-1){const l=PolygonUtils.segmentPolygonByPolygon(e,i);if(!l)return;const n=l.shift();if(!n)return;let r="",h=!0;const d=CommonToolUtils.getSourceID(this.basicResult);let g="";o=this.polygonList.map(a=>{var u,f;return a.id===this.selectedID?(r=a.attribute,h=(u=a==null?void 0:a.valid)!=null?u:!0,g=(f=a==null?void 0:a.textAttribute)!=null?f:"",__spreadProps(__spreadValues({},a),{pointList:n})):a}),l.length>0&&l.forEach((a,u)=>{o.push({sourceID:d,id:uuid(8,62),pointList:a,valid:h,order:CommonToolUtils.getMaxOrder(this.currentShowList)+1+u,attribute:r,textAttribute:g})})}else o[s].pointList=PolygonUtils.clipPolygonFromWrapPolygon(e,o[s].pointList),o=o.filter(l=>l.id!==this.selectedID);this.setPolygonList(o),this.history.pushHistory(o),this.render()}onCombinedExecute(){if(!this.selectedID){this.emit("messageInfo",lbUtils.i18n.t("PolygonsToBeCombinedNeedToBeSelected"));return}this.isCombined=!this.isCombined}combine(t){var e;const i=this.getHoverID(t);if(!i||!this.selectedID||this.selectedID===i)return;if(((e=this.config)==null?void 0:e.lineType)!==tool.ELineTypes.Line){this.emit("messageInfo",lbUtils.i18n.t("CurveModeDoesNotSupportCutting"));return}const s=this.polygonList.find(h=>h.id===this.selectedID),o=this.currentShowList.find(h=>h.id===i);if(!o||!s)return;const l=PolygonUtils.combinePolygonWithPolygon(s,o);if(!l)return;const{newPolygon:n,unionList:r}=l;if(r.length===1&&n){const h=this.polygonList.filter(d=>!r.includes(d.id)).map(d=>d.id===this.selectedID?n:d);this.setPolygonList(h),this.history.pushHistory(h),this.render(),this.emit("messageInfo",lbUtils.i18n.t("CombineSuccess"))}else this.emit("messageInfo",lbUtils.i18n.t("CombiningFailedNotify"));this.isCombined=!1}isPolygonOutSide(t){if(this.dependToolName&&this.basicCanvas&&this.basicResult){let n=!1;switch(this.dependToolName){case tool.EToolName.Rect:{n=t.filter(r=>!RectUtils.isInRect(r,this.basicResult)).length>0;break}case tool.EToolName.Polygon:{n=PolygonUtils.isPointListOutSidePolygon(t,this.basicResult.pointList,this.config.lineType);break}}return n}if(!this.imgInfo)return!1;const{left:e,top:i,right:s,bottom:o}=MathUtils.default.calcViewportBoundaries(AxisUtils.default.changePointListByZoom(t,this.zoom)),l=1e-5;return e<0||i<0||s>this.imgInfo.width+l||o>this.imgInfo.height+l}onDragMove(t){const e=this.polygonList.map(i=>{if(this.selectedIDs.includes(i.id)){const s=this.dragPolygon(t,i);if(!s)return i;const o=__spreadProps(__spreadValues({},i),{pointList:s});return i.isRect===!0&&this.pattern===tool.EPolygonPattern.Normal&&Object.assign(o,{isRect:!1}),o}return i});this.dragInfo.dragPrevCoord=this.getCoordinateUnderZoom(t),this.setPolygonList(e),this.render()}getDragOffset(t,e){var i;const s=this.getCoordinateUnderZoom(t),{dragTarget:o,dragPrevCoord:l,changePointIndex:n,initPointList:r,dragStartCoord:h}=this.dragInfo;if(this.pattern===tool.EPolygonPattern.Rect&&(e==null?void 0:e.isRect)===!0&&n&&[annotation.EDragTarget.Line].includes(o)){const d=MathUtils.default.getArrayIndex(n[0]-2,4),g=MathUtils.default.getArrayIndex(n[0]-1,4),a=[r[d],r[g]],u=MathUtils.default.getRectPerpendicularOffset(h,s,a);return{x:u.x/this.zoom,y:u.y/this.zoom}}return((i=this.dragInfo)==null?void 0:i.dragTarget)===annotation.EDragTarget.Plane?{x:(s.x-l.x)/this.zoom,y:(s.y-l.y)/this.zoom}:{x:(s.x-h.x)/this.zoom,y:(s.y-h.y)/this.zoom}}dragPolygon(t,e){let i=___default.default.cloneDeep(e==null?void 0:e.pointList);if(!i||!this.dragInfo)return;const{initPointList:s,dragTarget:o,changePointIndex:l}=this.dragInfo,n=this.getDragOffset(t,e);switch(this.dragStatus=annotation.EDragStatus.Move,o){case annotation.EDragTarget.Plane:i=i.map(r=>__spreadProps(__spreadValues({},r),{x:r.x+n.x,y:r.y+n.y}));break;case annotation.EDragTarget.Point:case annotation.EDragTarget.Line:i=i.map((r,h)=>l&&l.includes(h)?__spreadProps(__spreadValues({},r),{x:s[h].x+n.x,y:s[h].y+n.y}):r);break}if(this.pattern===tool.EPolygonPattern.Rect&&(e==null?void 0:e.isRect)===!0&&o===annotation.EDragTarget.Point&&l&&(i=MathUtils.default.getPointListFromPointOffset(s,l[0],n)),!(this.config.drawOutsideTarget===!1&&this.imgInfo&&this.isPolygonOutSide(i)))return i}onMouseMove(t){if(super.onMouseMove(t)||this.forbidMouseOperation||!this.imgInfo)return;let e=-1,i=-1;const{selectedID:s}=this;if(s&&(this.hoverEdgeIndex=-1,this.hoverPointIndex=-1,e=this.getHoverPointIndex(t),e>-1?this.hoverPointIndex=e:(i=this.getHoverEdgeIndex(t),this.hoverEdgeIndex=i)),this.drawingPointList.length>0)return;const o=this.getHoverID(t);this.hoverID!==o&&(this.hoverID=o,this.render()),this.selectedIDs.length>0&&this.dragInfo&&this.onDragMove(t)}leftMouseUpdateValid(t){const e=this.getHoverID(t);return this.drawingPointList.length===0&&t.ctrlKey===!0&&e?(this.setPolygonValidAndRender(e),!0):!1}leftMouseUp(t){this.leftMouseUpdateValid(t)||this.addPointInDrawing(t)}onMouseUp(t){if(this.isCombined){switch(t.button){case 0:this.combine(t);break;case 2:this.isCombined=!1;break;default:return}return}if(!(super.onMouseUp(t)||this.forbidMouseOperation||!this.imgInfo)){if(this.dragInfo&&this.dragStatus===annotation.EDragStatus.Move){this.emit("updateResult"),this.emitUpdatePolygonByDrag(),this.dragInfo=void 0,this.dragStatus=annotation.EDragStatus.Wait,this.history.pushHistory(this.polygonList);return}switch(t.button){case 0:{this.leftMouseUp(t);break}case 2:{this.rightMouseUp(t);break}}this.render()}}dragMouseUp(){this.dragStatus===annotation.EDragStatus.Start&&(this.dragInfo=void 0,this.dragStatus=annotation.EDragStatus.Wait)}exportData(){const{polygonList:t}=this;return[t,this.basicImgInfo]}getTextIconSvg(t=""){return AttributeUtils.default.getTextIconSvg(t,this.config.attributeList,this.config.attributeConfigurable,this.baseIcon)}getCurrentSelectedData(){const{selectedPolygon:t}=this;if(!t)return;const e=this.getColor(t.attribute),i=t.valid?e==null?void 0:e.valid.stroke:e==null?void 0:e.invalid.stroke;return{width:TEXT_MAX_WIDTH,textAttribute:t.textAttribute,color:i}}updateSelectedTextAttribute(t){if(this._textAttributeInstance&&t&&this.selectedID){let e=t;AttributeUtils.default.textAttributeValidate(this.config.textCheckType,"",e)===!1&&(this.emit("messageError",AttributeUtils.default.getErrorNotice(this.config.textCheckType,this.lang)),e=""),this.setPolygonList(AttributeUtils.default.textChange(e,this.selectedID,this.polygonList)),this.emit("updateTextAttribute"),this.render()}}renderTextAttribute(){var t;const{selectedPolygon:e}=this;if(!this.ctx||this.config.textConfigurable===!1||!e)return;const{pointList:i,attribute:s,valid:o,textAttribute:l}=e,{x:n,y:r}=i[i.length-1],h=TEXT_MAX_WIDTH,d=AxisUtils.default.getOffsetCoordinate({x:n,y:r},this.currentPos,this.zoom),g=this.getColor(s),a=o?g==null?void 0:g.valid.stroke:g==null?void 0:g.invalid.stroke;this._textAttributeInstance||(this._textAttributeInstance=new textAttributeClass({width:h,container:this.container,icon:this.getTextIconSvg(s),color:a,getCurrentSelectedData:this.getCurrentSelectedData,updateSelectedTextAttribute:this.updateSelectedTextAttribute})),this._textAttributeInstance&&!((t=this._textAttributeInstance)==null?void 0:t.isExit)&&this._textAttributeInstance.appendToContainer(),this._textAttributeInstance.update(`${l}`,{left:d.x,top:d.y,color:a,width:h})}renderStaticPolygon(){var t;this.isHidden===!1&&((t=this.visiblePolygonList)==null||t.forEach(e=>{var i,s,o,l,n;if([this.selectedID,this.editPolygonID].includes(e.id))return;const{textAttribute:r,attribute:h}=e,d=this.getColor(h),g=StyleUtils.getStrokeAndFill(d,e.valid),a=AxisUtils.default.changePointListByZoom(e.pointList||[],this.zoom,this.currentPos);DrawUtils.drawPolygonWithFillAndLine(this.canvas,a,{fillColor:g.fill,strokeColor:g.stroke,pointColor:"white",thickness:(s=(i=this.style)==null?void 0:i.width)!=null?s:2,lineCap:"round",isClose:!0,lineType:(o=this.config)==null?void 0:o.lineType});let u=`${(l=AttributeUtils.default.getAttributeShowText(h,this.config.attributeList))!=null?l:""}`;((n=this.config)==null?void 0:n.isShowOrder)&&(e==null?void 0:e.order)>0&&(u=`${e.order} ${u}`),DrawUtils.drawText(this.canvas,a[0],u,__spreadValues({color:g.stroke},annotation.DEFAULT_TEXT_OFFSET));const f=a[a.length-1];DrawUtils.drawText(this.canvas,{x:f.x+annotation.TEXT_ATTRIBUTE_OFFSET.x,y:f.y+annotation.TEXT_ATTRIBUTE_OFFSET.y},r,__spreadValues({color:g.stroke},annotation.DEFAULT_TEXT_OFFSET))}))}renderSelectedPolygons(){this.selectedPolygons.forEach(t=>{this.renderSelectedPolygon(t)})}renderSelectedPolygon(t){var e,i,s;if(t){const o=this.getColor(t.attribute),l=StyleUtils.getStrokeAndFill(o,t.valid,{isSelected:!0});DrawUtils.drawSelectedPolygonWithFillAndLine(this.canvas,AxisUtils.default.changePointListByZoom(t.pointList,this.zoom,this.currentPos),{fillColor:l.fill,strokeColor:l.stroke,pointColor:"white",thickness:2,lineCap:"round",isClose:!0,lineType:(e=this.config)==null?void 0:e.lineType});let n=`${(i=AttributeUtils.default.getAttributeShowText(t.attribute,this.config.attributeList))!=null?i:""}`;((s=this.config)==null?void 0:s.isShowOrder)&&(t==null?void 0:t.order)>0&&(n=`${t.order} ${n}`),DrawUtils.drawText(this.canvas,AxisUtils.default.changePointByZoom(t.pointList[0],this.zoom,this.currentPos),n,__spreadValues({color:l.stroke},annotation.DEFAULT_TEXT_OFFSET)),this.renderTextAttribute()}}renderHoverPolygon(){var t;if(this.hoverID&&this.hoverID!==this.editPolygonID){const{hoverPolygon:e}=this;if(e){let i="";const s=this.getColor(e.attribute);e.valid?i=s.validHover.fill:i=StyleUtils.getStrokeAndFill(s,!1,{isHover:!0}).fill,DrawUtils.drawPolygonWithFill(this.canvas,AxisUtils.default.changePointListByZoom(e.pointList,this.zoom,this.currentPos),{color:i,lineType:(t=this.config)==null?void 0:t.lineType})}}}renderPolygon(){var t,e,i,s;this.renderStaticPolygon(),this.renderHoverPolygon(),this.renderSelectedPolygons();const o=this.getColor(this.defaultAttribute),l=StyleUtils.getStrokeAndFill(o,!this.isCtrl);if(((t=this.drawingPointList)==null?void 0:t.length)>0){let n=[...this.drawingPointList],r=AxisUtils.default.getOriginCoordinateWithOffsetCoordinate(this.coord,this.zoom,this.currentPos);if(this.pattern===tool.EPolygonPattern.Rect&&n.length===2)n=MathUtils.default.getRectangleByRightAngle(r,n);else{if(((e=this.config)==null?void 0:e.edgeAdsorption)&&this.isAlt===!1){const{dropFoot:h}=PolygonUtils.getClosestPoint(r,this.polygonList,(i=this.config)==null?void 0:i.lineType,tool.edgeAdsorptionScope/this.zoom);h&&(r=h)}n.push(r)}DrawUtils.drawSelectedPolygonWithFillAndLine(this.canvas,AxisUtils.default.changePointListByZoom(n,this.zoom,this.currentPos),{fillColor:l.fill,strokeColor:l.stroke,pointColor:"white",thickness:2,lineCap:"round",isClose:!1,lineType:this.config.lineType})}if(this.hoverPointIndex>-1&&this.selectedID){const n=this.selectedPolygon;if(!n)return;const r=StyleUtils.getStrokeAndFill(o,n.valid,{isSelected:!0}),h=n==null?void 0:n.pointList[this.hoverPointIndex];if(h){const{x:d,y:g}=AxisUtils.default.changePointByZoom(h,this.zoom,this.currentPos);DrawUtils.drawCircleWithFill(this.canvas,{x:d,y:g},5,{color:r.fill})}}if(this.hoverEdgeIndex>-1&&this.selectedID){const n=this.selectedPolygon;if(!n)return;const r=StyleUtils.getStrokeAndFill(o,n.valid,{isSelected:!0});DrawUtils.drawLineWithPointList(this.canvas,AxisUtils.default.changePointListByZoom(n.pointList,this.zoom,this.currentPos),{color:r.stroke,thickness:10,hoverEdgeIndex:this.hoverEdgeIndex,lineType:(s=this.config)==null?void 0:s.lineType})}}render(){!this.ctx||(super.render(),this.renderPolygon(),this.renderCursorLine(this.getLineColor(this.defaultAttribute)))}renderCursorLine(t){if(super.renderCursorLine(t),this.isCombined){const{x:e,y:i}=this.coord,s=10,o=186,l=32;DrawUtils.drawRectWithFill(this.canvas,{x:e+s,y:i-s*4-1,width:o,height:l},{color:"black"}),DrawUtils.drawText(this.canvas,{x:e,y:i},lbUtils.i18n.t("ClickAnotherPolygon"),{textAlign:"center",color:"white",offsetX:o/2+s,offsetY:-(l/2+s/2)}),DrawUtils.drawRect(this.canvas,{x:e-s,y:i-s,width:s*2,height:s*2},{lineDash:[6],color:"white"})}}undo(){if(this.drawingPointList.length>0){const e=this.drawingHistory.undo();if(!e)return;this.drawingPointList=e,this.render();return}const t=this.history.undo();t&&(t.length!==this.polygonList.length&&this.deleteSelectedID(),this.setPolygonList(t),this.render())}redo(){if(this.drawingPointList.length>0){const e=this.drawingHistory.redo();if(!e)return;this.drawingPointList=e,this.render();return}const t=this.history.redo();t&&(t.length!==this.polygonList.length&&this.deleteSelectedID(),this.setPolygonList(t),this.render())}deleteSelectedID(){this.setSelectedID("")}}module.exports=PolygonOperation;
|
|
@@ -35,6 +35,7 @@ declare class PolygonOperation extends BasicToolOperation {
|
|
|
35
35
|
constructor(props: IPolygonOperationProps);
|
|
36
36
|
get selectedIDs(): string[];
|
|
37
37
|
get selectedID(): string | undefined;
|
|
38
|
+
get minArea(): number;
|
|
38
39
|
eventBinding(): void;
|
|
39
40
|
eventUnbinding(): void;
|
|
40
41
|
destroy(): void;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
import g from"lodash";import{ELineTypes as P,ELineColor as U,EToolName as _,ETextType as M}from"../../constant/tool.js";import B from"../../utils/ActionsHistory.js";import S from"../../utils/uuid.js";import L from"../../constant/keyCode.js";import v from"../../utils/MathUtils.js";import{BasicToolOperation as j}from"./basicToolOperation.js";import u from"../../utils/tool/LineToolUtils.js";import{createSmoothCurvePointsFromPointList as R,isInPolygon as V,createSmoothCurvePoints as W}from"../../utils/tool/polygonTool.js";import K from"../../utils/tool/CommonToolUtils.js";import N from"../../utils/tool/CanvasUtils.js";import F from"../../utils/tool/DrawUtils.js";import z from"../../utils/tool/StyleUtils.js";import y from"../../utils/tool/AttributeUtils.js";import $ from"./textAttributeClass.js";import J from"./Selection.js";import Y from"../../locales/index.js";import{EMessage as X}from"../../locales/constants.js";var q=Object.defineProperty,G=Object.defineProperties,Z=Object.getOwnPropertyDescriptors,O=Object.getOwnPropertySymbols,Q=Object.prototype.hasOwnProperty,tt=Object.prototype.propertyIsEnumerable,E=(d,e,t)=>e in d?q(d,e,{enumerable:!0,configurable:!0,writable:!0,value:t}):d[e]=t,w=(d,e)=>{for(var t in e||(e={}))Q.call(e,t)&&E(d,t,e[t]);if(O)for(var t of O(e))tt.call(e,t)&&E(d,t,e[t]);return d},T=(d,e)=>G(d,Z(e)),k;(function(d){d[d.Create=0]="Create",d[d.Active=1]="Active",d[d.None=2]="None"})(k||(k={}));const p=16,C={x:0,y:20},x=3,m=5,H=2;class et extends j{constructor(e){super(e);this.drawActivatedLine=(t,i,s)=>{const n=this.isCreate?g.cloneDeep([{pointList:this.activeLine,order:this.nextOrder(),attribute:this.defaultAttribute,valid:this.isLineValid}]):g.cloneDeep(this.selectedLines),o=this.isActiveLineValid();n.forEach(r=>{if((r==null?void 0:r.pointList)&&(r.pointList=r.pointList.map(l=>Object.assign(l,{actual:{x:l.x,y:l.y}},this.coordUtils.getRenderCoord(l)))),!r.pointList)return;const h=this.getLineColorByAttribute({attribute:r.attribute,valid:r.valid});return this.selectedLines.length>0&&(this.drawLineLength(r.pointList,h),this.renderActiveArea(v.calcViewportBoundaries(r.pointList,this.isCurve,p))),this.drawLine(r.pointList,t,h,!0,!0),this.drawLineNumber(r.pointList[0],r.order,h,"",this.defaultAttribute,o),t&&this.isCreate&&this.arc(t,x,h),this.cursor&&!this.selectedPoint&&!s&&!this.isShift&&this.arc(this.cursor,m,h),r})},this.drawHoverPoint=t=>{if(!this.isMousedown&&t&&this.selectedID){const i=this.getPointList(this.activeLine),s=this.activeLine.find(o=>u.calcDistance(this.coordUtils.getRenderCoord(o),t)<=m);let n;!s&&this.activeLine&&(n=this.findNearestPoint(i,t)),this.hoverPointID=s?s.id:void 0,this.cursor=s||n==null?void 0:n.point}},this.updatedLine={id:"",valid:!1,order:0},this.toolName="lineTool",this.lineList=[],this.activeLine=[],this.coordsInsideActiveArea=!1,this.hoverLineSegmentIndex=-1,this.isShift=!1,this.isReference=!1,this.drawCurveLine=(t,i,s,n=!0,o=!1,r)=>{const h=R(i,p);t.save(),t.lineCap="round",t.lineJoin="round",t.strokeStyle=s.color,n&&(t.lineWidth=s.lineWidth),o&&u.setReferenceCtx(t),i.forEach(({specialEdge:l},a)=>{const c=h.splice(0,p+1);t.save(),t.beginPath(),r===a&&(t.lineWidth=4),c.forEach(({x:f,y:b},I)=>{const D=I>0?"lineTo":"moveTo";l&&u.setSpecialEdgeStyle(t),t[D](f,b)}),t.stroke(),t.restore()}),t.restore()},this.drawLine=(t,i,s,n=!1,o=!1)=>{const r=i?t.concat(i):t,h={color:s,lineWidth:o?1:this.lineStyle.lineWidth};this.isCurve?u.drawCurveLine(this.ctx,r,h,!n,this.isReference,o?this.hoverLineSegmentIndex:-1):this.drawStraightLine(r,h,o),n&&t.forEach(l=>{var a,c;const f=l.id,b=f&&[this.hoverPointID,(a=this.selectedPoint)==null?void 0:a.id].includes(f)?m:x;this.arc(l,b,s),[this.hoverPointID,(c=this.selectedPoint)==null?void 0:c.id].includes(f)||this.arc(l,H,"white")})},this.drawStraightLine=(t,i,s=!1)=>{const{ctx:n}=this;n&&(n.save(),n.lineCap="round",n.lineJoin="round",n.strokeStyle=i.color,n.lineWidth=i.lineWidth,this.isReference&&u.setReferenceCtx(n),t.forEach((o,r)=>{if(n.beginPath(),r>0){const h=t[r-1];n.save(),(h==null?void 0:h.specialEdge)&&u.setSpecialEdgeStyle(n),s&&this.hoverLineSegmentIndex+1===r&&(n.lineWidth=4),n.moveTo(h.x,h.y),n.lineTo(o.x,o.y),n.stroke(),n.restore()}}),n.restore())},this.drawLines=()=>{try{const t=g.cloneDeep(this.attributeFilteredLines);if(this.isHidden)return;t.forEach(i=>{if(!this.selection.isIdSelected(i.id)&&i.pointList){i.pointList.map(h=>Object.assign(h,{actual:{x:h.x,y:h.y}},this.coordUtils.getRenderCoord(h)));const{order:s,label:n}=i,o=s,r=i&&this.getLineColorByAttribute(i);this.drawLine(i.pointList,void 0,r,!1),this.drawLineNumber(i.pointList[0],o,r,n,i.attribute,i.valid),i.id!==this.textEditingID&&(this.drawLineTextAttribute(i.pointList[1],r,i==null?void 0:i.textAttribute),this.drawLineLength(i.pointList,r))}})}catch(t){console.error(t,"\u7EBF\u6761\u5DE5\u5177\u6570\u636E\u89E3\u6790\u9519\u8BEF"),this.lineList=[],this.clearCanvas()}},this.render=t=>{super.render(),this.drawLines(),this.drawActivatedLine(t,void 0,!0),this.renderTextAttribute(),this.renderCursorLine(this.getLineColor(this.defaultAttribute))},this.moveLineInPolygon=(t,i)=>{var s;if(!Array.isArray(this.activeLine))return!1;((s=this.activeLine)==null?void 0:s.every(o=>this.isInBasicPolygon({x:o.x+t,y:o.y+i})))&&(this.lineDragging=!0,this.moveActiveArea(t,i))},this.getSelectedLinesArea=()=>v.calcViewportBoundaries(this.selectedLines.reduce((t,i)=>(i==null?void 0:i.pointList)?t.concat(...i==null?void 0:i.pointList):t,[])),this.moveLineInRectRange=(t,i,s,n)=>{if(this.selectedLines.length===0)return;const{top:o,left:r,right:h,bottom:l}=this.getSelectedLinesArea(),a=[r,h].map(A=>g.isNumber(A)?A+t:0),c=[o,l].map(A=>g.isNumber(A)?A+i:0),f=r>=0&&h&&v.isInRange(a,s),b=o>=0&&l&&v.isInRange(c,n),I=f?t:0,D=b?i:0;this.lineDragging=!0,this.moveActiveArea(I,D)},this.onRightClick=t=>{if(this.cursor=void 0,this.isCreate){if(this.isLinePointsNotEnough())return;if(u.lineLengthSum(this.activeLine||[])<this.minLength)return this.emit("messageInfo",`${Y.getMessagesByLocale(X.MinLengthLimitErrorNotice,this.lang)}${this.minLength}`),!0;this.stopLineCreating(!0);return}const i=this.findHoverLine(this.getCoordinate(t));this.setSelectedLineID(i==null?void 0:i.id,t.ctrlKey),this.emit("contextmenu")},this.updateSelectedAttributeAfterHistoryChanged=()=>{if(this.selectedIDs.length>0){const t=this.lineList.find(s=>s.id===this.selectedIDs[0]),i=t==null?void 0:t.attribute;typeof i=="string"&&(this.defaultAttribute=i,this.updateAttribute(i),this.render())}},this.onLeftClick=t=>{const i=this.getCoordinate(t),{lineDragging:s}=this;if(this.lineDragging=!1,this.isSpaceKey)return;if(this.isNone&&t.ctrlKey){const o=this.findHoverLine(i);o&&this.setInvalidLine(o.id);return}if(this.isLinePointsExceed())return;const n=this.getNextPoint(t,i);if(this.isCreate||this.isNone){this.setCreateStatusAndAddPoint(n);return}if(this.isActive){if(s)return;if(this.isMouseCoordOutsideActiveArea()){this.setNoneStatus(!1),this.setCreateStatusAndAddPoint(n);return}if(t.shiftKey){this.updateLineSegmentSpecial(i);return}this.coordsInsideActiveArea&&t.ctrlKey&&this.setInvalidLine(this.selectedID)}},this.onDblclick=()=>{},this.onKeyUp=t=>{if(super.onKeyUp(t),this.isShift=!1,this.hoverLineSegmentIndex=-1,!this.selection.triggerKeyboardEvent(t,this.setLineList)){if(t.keyCode===L.Esc){this.stopLineCreating(!1);return}if(this.selectedLine){if(t.key==="Delete"){this.deleteLine();return}if(t.key==="f"){this.setInvalidLine(this.selectedID);return}if(t.key===" "){this.continueToEdit();return}}this.keyboardEventWhileLineCreating(t)}},this.onRightDblClick=t=>{super.onRightDblClick(t);const i=this.getCoordinate(t);if(this.isActive){const s=this.findHoveredPoint(i);if(s){this.deleteSelectedLinePoint(s.id);return}this.deleteSelectedLine(i)}},this.setReference=t=>{this.isReference=t},this.pointsWithinRange=t=>!(this.lowerLimitPointNum&&t<this.lowerLimitPointNum||this.upperLimitPointNum&&t>this.upperLimitPointNum),this.setLineList=t=>{const i=t.length!==this.lineListLen;this.lineList=t,i&&this.emit("updatePageNumber")},this.textChange=t=>{this.config.textConfigurable===!1||!this.selectedID||(this.updateSelectedTextAttribute(t),this.emit("selectedChange"))},this.status=2,this.isMousedown=!1,this.lineDragging=!1,this.isLineValid=!0,this.setConfig(e.config),this.prevAxis={x:0,y:0},this.textEditingID="",this.updateSelectedTextAttribute=this.updateSelectedTextAttribute.bind(this),this.getCurrentSelectedData=this.getCurrentSelectedData.bind(this),this.actionsHistory=new B,this.selection=new J(this),this.dependToolConfig={lineType:P.Line}}get selectedLines(){return this.lineList.filter(e=>this.selection.isIdSelected(e.id))}get isLineSelected(){return this.selectedID&&this.activeLine}get selectedLine(){return this.lineList.find(e=>e.id===this.selectedID)}get isCreate(){return this.status===0}get isActive(){return this.selectedLines.length>0}get isNone(){return!this.isCreate&&this.selectedIDs.length===0}get isCurve(){return this.config.lineType===P.Curve}get isMultipleColor(){return this.config.lineColor===U.MultiColor}get imageSize(){return this.rotate%180==0?this.basicImgInfo:{width:this.basicImgInfo.height,height:this.basicImgInfo.width}}get lineListLen(){return this.lineList.length}get edgeAdsorptionEnabled(){return this.edgeAdsorption&&!this.isCurve&&this.lineListLen>0}get attributeConfigurable(){return this.config.attributeConfigurable}get isTextConfigurable(){return this.config.textConfigurable}get isDependPolygon(){return this.dependToolName===_.Polygon}get isDependRect(){return this.dependToolName===_.Rect}get isCurrentAttributeLocked(){return this.attributeLockList.includes(this.defaultAttribute)}get attributeFilteredLines(){return this.attributeLockList.length>0?this.lineList.filter(e=>this.attributeLockList.includes((e==null?void 0:e.attribute)||"")):this.lineList}get selectedIDs(){return this.selection.selectedIDs}get selectedID(){return this.selection.selectedID}get enableOutOfTarget(){return this.config.enableOutOfTarget||this.config.outOfTarget||this.config.drawOutsideTarget}get showOrder(){var e;return(e=this.config.showOrder)!=null?e:this.config.isShowOrder}get edgeAdsorption(){return this.config.edgeAdsorption}get attributeList(){return this.config.attributeList}get lowerLimitPointNum(){return this.config.lowerLimitPointNum}get minLength(){var e;return((e=this.config)==null?void 0:e.minLength)||1}get upperLimitPointNum(){return this.config.upperLimitPointNum}get textCheckType(){return this.config.textCheckType}get customFormat(){return this.config.customFormat}get dataList(){return this.lineList}get hasActiveLine(){return this.activeLine&&this.activeLine.length>0}get viewPortLines(){const e=N.getViewPort(this.canvas,this.currentPos,this.zoom);return this.isHidden?[]:this.attributeFilteredLines.filter(t=>{var i;return(i=t==null?void 0:t.pointList)==null?void 0:i.some(s=>N.inViewPort(s,e))})}get lineStyle(){return{lineWidth:this.style.width,color:this.getLineColor(this.defaultAttribute),opacity:this.style.opacity}}get selectedText(){var e,t;return(t=(e=this.lineList.find(i=>i.id===this.selectedID))==null?void 0:e.textAttribute)!=null?t:""}get currentPageResult(){return this.lineList}updateStatus(e,t=!1){if(e!==this.status){if(t){let i="";this.textCheckType===M.Order&&this.isTextConfigurable&&(i=y.getTextAttribute(this.lineList,this.textCheckType)),this.emit("updateText",i)}this.status=e,this.lineStatusChanged()}}isInBasicPolygon(e){var t,i;return V(e,((t=this.basicResult)==null?void 0:t.pointList)||[],(i=this.dependToolConfig)==null?void 0:i.lineType)}getPolygonPointList(){if(!this.basicResult)return[];const{pointList:e}=this.basicResult,{lineType:t}=this.dependToolConfig;return t===P.Curve?W(e.reduce((i,s)=>[...i,s.x,s.y],[]),.5,!0,20):e}getNextCoordByRenderCoord(e){return this.getNextCoordByAbsCoord(this.coordUtils.getAbsCoord(e))}getNextCoordByAbsCoord(e){var t;const i=(t=this.activeLine)==null?void 0:t.slice(-1)[0];return i?this.coordUtils.getNextCoordByDependTool(e,i):e}pointInLine(e,t,i){return e.filter(s=>s).length<2?!1:e.some((s,n)=>{if(n===0)return!1;const o=this.coordUtils.getRenderCoord(e[n-1]),r=this.coordUtils.getRenderCoord(s);return u.isInLine(t,o,r,i)})}arc(e,t=x,i){var s,n,o,r,h,l;if(this.ctx){const{x:a,y:c}=e;(s=this.ctx)==null||s.save(),(n=this.ctx)==null||n.beginPath(),this.ctx.fillStyle=i||this.lineStyle.color,(o=this.ctx)==null||o.arc(a,c,t,0,360),(r=this.ctx)==null||r.closePath(),(h=this.ctx)==null||h.fill(),(l=this.ctx)==null||l.restore()}}renderActiveArea({top:e,left:t,right:i,bottom:s}){this.ctx&&(this.ctx.save(),this.ctx.beginPath(),this.ctx.strokeStyle="#B3B8FF",this.ctx.rect(t,e,i-t,s-e),this.ctx.stroke(),this.ctx.restore())}addLinePoint(e){var t,i,s,n;this.arc(e),(t=this.activeLine)==null||t.push(T(w({},e),{id:S()})),((i=this.activeLine)==null?void 0:i.length)===1?(s=this.actionsHistory)==null||s.initRecord(this.activeLine):(n=this.actionsHistory)==null||n.pushHistory(this.activeLine),this.render()}setCreateStatusAndAddPoint(e,t=!1){this.updateStatus(0,t),this.addLinePoint(e)}isActiveLineValid(){var e;return this.selectedID?(e=this.lineList.find(t=>t.id===this.selectedID))==null?void 0:e.valid:this.isLineValid}nextOrder(){return this.lineListLen===0?1:this.lineList.slice(-1)[0].order+1}getLineColorByAttribute(e,t=!1){return z.getStrokeAndFill(this.getColor(e.attribute),e.valid,{isSelected:t}).stroke}drawLineNumber(e,t=1,i,s="",n,o=!0){var r,h,l;if((this.showOrder||this.attributeConfigurable)&&this.ctx){let a=this.showOrder?t.toString():`${s}`;if(this.attributeConfigurable){const c=n?(l=(h=(r=this.attributeList)==null?void 0:r.find(f=>f.value===n))==null?void 0:h.key)!=null?l:n:"";a=[a,`${!o&&c?"\u65E0\u6548":""}${c}`].filter(f=>f).join("_")}this.drawText(e,a,i)}}drawLineTextAttribute(e,t,i){if(e&&i)return this.drawText(e,i,t,200)}drawLineLength(e,t){var i;if(((i=this.config)==null?void 0:i.showLineLength)&&e){const s=e.reduce((o,r,h)=>h<=0||!e[h-1].actual||!r.actual?o:o+u.calcDistance(e[h-1].actual,r.actual),0),n=e[e.length-1];this.drawText(n,`l = ${s.toFixed(2)}`,t)}}drawText(e,t,i,s){var n,o;this.ctx&&((n=this.ctx)==null||n.save(),this.ctx.font="italic bold 14px SourceHanSansCN-Regular",this.ctx.fillStyle=i,this.ctx.strokeStyle=i,this.ctx.shadowColor="rgba(0, 0, 0, 0.6)",this.ctx.shadowOffsetY=2,this.ctx.shadowBlur=4,s?F.wrapText(this.canvas,t,e.x-C.x,e.y-C.y,s):this.ctx.fillText(t,e.x-C.x,e.y-C.y),(o=this.ctx)==null||o.restore())}moveActiveArea(e,t){this.selectedLines.length>0&&(this.selectedLines.forEach(i=>{var s;(s=i.pointList)==null||s.forEach(n=>Object.assign(n,{x:n.x+e,y:n.y+t}))}),this.render()),this.emit("dataUpdated",this.lineList,this.selectedIDs)}findHoveredPoint(e){if(!!this.activeLine)return this.activeLine.find(t=>{const i=this.coordUtils.getRenderCoord(t);return u.calcDistance(i,e)<=m})}findHoverLine(e){return g.cloneDeep(this.lineList).reverse().find(({pointList:i})=>{const s=i?this.getPointList(i):[],n=this.getLineWidthScope();return s.some((o,r)=>{if(r===0)return!1;const h=this.coordUtils.getRenderCoord(o),l=this.coordUtils.getRenderCoord(s[r-1]);return u.isInLine(e,h,l,n)})})}getAdsorptionPoint(e){let t,i,s;return g.cloneDeep(this.lineList).reverse().forEach(({pointList:n,id:o})=>{if(o===this.selectedID||!n||(n==null?void 0:n.length)<2)return;const r=this.findNearestPoint(n,e);if(r){if(r.minDistance===0){t=r.point;return}(i===void 0||r.minDistance<i)&&(t=r.point,i=r.minDistance)}}),s||t}findNearestPoint(e,t,i=7){let s;const n=i;for(let o=1;o<=e.length-1;o++){const r=this.coordUtils.getRenderCoord(e[o]),h=this.coordUtils.getRenderCoord(e[o-1]),{length:l,footPoint:a}=v.getFootOfPerpendicular(t,r,h),c=u.calcTwoPointDistance(r,t),f=u.calcTwoPointDistance(h,t);if(c<=i*2){s=r,i=0;break}if(f<=i*2){s=h,i=0;break}l<i&&(s=a,i=l)}return s?{point:s,minDistance:n}:void 0}getPointList(e){return this.isCurve?R(e,p):e}moveSelectedLine(e){const t=(e.x-this.prevAxis.x)/this.zoom,i=(e.y-this.prevAxis.y)/this.zoom;if(this.enableOutOfTarget){this.lineDragging=!0,this.moveActiveArea(t,i);return}if(this.isDependPolygon){this.moveLineInPolygon(t,i);return}let s=[0,this.imageSize.width],n=[0,this.imageSize.height];if(this.isDependRect){const{x:o,y:r,width:h,height:l}=this.basicResult;s=[o,o+h],n=[r,r+l]}this.moveLineInRectRange(t,i,s,n)}moveSelectPoint(e){if(!this.selectedPoint)return;const t=e.x-this.prevAxis.x,i=e.y-this.prevAxis.y,s=(this.selectedPoint?this.selectedPoint.x:0)+t/this.zoom,n=(this.selectedPoint?this.selectedPoint.y:0)+i/this.zoom,o={x:s,y:n};Object.assign(this.selectedPoint,this.getNextCoordByAbsCoord(o)),this.updateLines(),this.render()}getCoordByConfig(e,t){var i;const s=!!e.shiftKey,n=e.altKey;if(((i=this.activeLine)==null?void 0:i.length)>0&&s){const o=this.activeLine.slice(-1)[0];return u.getVHPoint(o,t,this.coordUtils.getAbsCoord(t),this.coordUtils.getRenderCoord(o))}return this.edgeAdsorptionEnabled&&!n?this.getAdsorptionPoint(t):t}getNextPoint(e,t){const i=this.getCoordByConfig(e,t)||t;return this.enableOutOfTarget?this.coordUtils.getAbsCoord(i):this.getNextCoordByRenderCoord(i)}lineHover(){this.render()}mouseMoveHandler(e){const t=this.getCoordinate(e),i=e.which===1;if(this.isCreate){this.hasActiveLine&&this.renderNextPoint(e,t);return}if(this.isNone&&(this.lineHover(),this.edgeAdsorptionEnabled&&!e.altKey)){const s=this.getAdsorptionPoint(t);s&&this.arc(s)}if(this.isActive){if(this.isMousedown&&i){if(this.selectedPoint){this.moveSelectPoint(t);return}if(this.coordsInsideActiveArea){this.moveSelectedLine(t),this.drawActivatedLine(void 0,void 0,!0);return}}this.drawHoverPoint(t),this.render()}}onMouseMove(e){if(super.onMouseMove(e)||this.forbidMouseOperation||!this.imgInfo)return;const t=this.getCoordinate(e);this.mouseMoveHandler(e),this.prevAxis=t}setActiveLine(e){this.activeLine=e?g.cloneDeep(e):void 0}historyChanged(e){const t=`${e}Enabled`;if(this.isCreate){if(this.actionsHistory&&this.actionsHistory[t]){const i=this.actionsHistory&&this.actionsHistory[e]();this.setActiveLine(i),this.render()}return}if(this.history&&this.history[t]){const i=this.history[e](),s=i==null?void 0:i.find(n=>n.id===this.selectedID);this.lineList=i,this.selectedID&&s?this.setActiveLine(s==null?void 0:s.pointList):this.setNoneStatus(),this.render()}this.emit("dataUpdated",this.lineList)}undo(){this.historyChanged("undo"),this.updateSelectedAttributeAfterHistoryChanged()}redo(){this.historyChanged("redo"),this.updateSelectedAttributeAfterHistoryChanged()}isCoordInsideTarget(e){if(this.isDependPolygon)return this.isInBasicPolygon(e);if(this.isDependRect){const{x:t,y:i,width:s,height:n}=this.basicResult,o=[t,t+s],r=[i,i+n];return v.isInRange(e.x,o)&&v.isInRange(e.y,r)}return v.isInRange(e.x,[0,this.imageSize.width])&&v.isInRange(e.y,[0,this.imageSize.height])}getPointInsertIndex(e,t){if(e&&this.activeLine){const i=this.getPointList(this.activeLine);return this.activeLine.length===2?1:this.activeLine.findIndex((s,n)=>{if(n>0){const o=this.activeLine?this.activeLine[n-1]:void 0,r=this.isCurve?i.slice((n-1)*(p+1),n*(p+1)):[o,s];return this.pointInLine(r,e,t||this.getLineWidthScope())}return!1})}return-1}getLineWidthScope(){return this.lineStyle.lineWidth}isMouseCoordOutsideActiveArea(){return!this.coordsInsideActiveArea&&!this.selectedPoint}isLinePointsExceed(){return this.isCreate&&this.activeLine&&this.upperLimitPointNum&&~~this.upperLimitPointNum<=this.activeLine.length}isLinePointsNotEnough(){var e;return this.activeLine&&((e=this.activeLine)==null?void 0:e.length)<this.lowerLimitPointNum}updateLineSegmentSpecial(e){const t=this.getPointInsertIndex(e,2)-1;if(t>-1){const i=this.activeLine[t];i.specialEdge=!i.specialEdge,this.hoverLineSegmentIndex=-1,this.render()}}addLinePointToActiveLine(){var e;const t=this.getPointInsertIndex(this.cursor),i=this.pointsWithinRange(this.activeLine.length+1);this.cursor&&t>-1&&i&&(this.activeLine.splice(t,0,T(w({},this.coordUtils.getAbsCoord(this.cursor)),{id:S()})),this.updateLines(),(e=this.history)==null||e.pushHistory(this.lineList),this.render(),this.cursor=void 0)}isCoordOnSelectedArea(e){return this.selectedLines.some(t=>u.inArea(v.calcViewportBoundaries(t.pointList),this.coordUtils.getAbsCoord(e)))}onMouseDown(e){if(super.onMouseDown(e)||this.forbidMouseOperation||!this.imgInfo)return;const t=this.getCoordinate(e);if(this.isMousedown=!0,this.prevAxis=t,e.which===3){this.cursor=void 0;return}this.selectedPoint=this.findHoveredPoint(t),this.coordsInsideActiveArea=this.selectedLines.length>0?this.isCoordOnSelectedArea(t):!1,this.lineDragging=!1}lineHasChanged(){const e=this.lineList.find(t=>t.id===this.selectedID);return e?JSON.stringify(e.pointList)!==JSON.stringify(this.activeLine):!1}updateLines(){const e=this.lineList.find(t=>t.id===this.selectedID);e&&(e.pointList=g.cloneDeep(this.activeLine),this.updatedLine=e,this.emit("dataUpdated",this.lineList))}onMouseUp(e){const t=()=>{this.isMousedown=!1,this.hoverPointID=void 0,this.cursor=void 0,this.selectedPoint=void 0};if(this.hoverPointID=void 0,super.onMouseUp(e)||this.forbidMouseOperation||!this.imgInfo){t();return}e.which===1&&this.onLeftClick(e),e.which===3&&this.onRightClick(e),t()}isTextValid(e){return y.textAttributeValidate(this.textCheckType,this.customFormat,e)}createLineData(){const e=S(),t={pointList:g.cloneDeep(this.activeLine),id:e,valid:this.isLineValid,order:this.nextOrder()};return t.attribute=this.defaultAttribute,t}stopLineCreating(e=!0){var t,i,s,n;const o=this.selectedID?!0:!!this.isTextConfigurable;let r;if(e){if(this.selectedID){const h=this.lineList.find(l=>l.id===this.selectedID);r=this.selectedID,h&&(h.pointList=g.cloneWith(this.activeLine),g.isEqual(h.pointList,(t=this.history)==null?void 0:t.pushHistory(this.lineList))||(i=this.history)==null||i.pushHistory(this.lineList))}else if(this.isCreate&&this.activeLine&&this.activeLine.length>1){const h=this.createLineData();r=h.id,this.setLineList([...this.lineList,h]),(s=this.history)==null||s.pushHistory(this.lineList)}}this.setNoneStatus(),o&&(this.activeLine=[],this.setSelectedLineID(r)),(n=this.actionsHistory)==null||n.empty(),this.emit("dataUpdated",this.lineList,this.selectedIDs),this.render()}setNoneStatus(e=!0){e&&this.updateStatus(2),this.activeLine=[],this.setSelectedLineID(void 0),this.isLineValid=!0,this.cursor=void 0}setKeyDownStatus(e,t){this.isShift=t!=null?t:e.keyCode===L.Shift}continueToEdit(){var e,t;((e=this.selectedLine)==null?void 0:e.pointList)&&(this.updateStatus(0),this.cursor=void 0,this.setActiveLine(this.selectedLine.pointList),(t=this.actionsHistory)==null||t.pushHistory(this.activeLine),this.render())}setInvalidLineOnCreating(e){if(this.selectedID&&e.keyCode!==L.Ctrl||!this.isCreate)return;const t=!e.ctrlKey;this.selectedID?this.setInvalidLine(this.selectedID,t,!1):this.isLineValid=t}onKeyDown(e){if(super.onKeyDown(e),this.setKeyDownStatus(e),e.keyCode===L.Z&&!e.ctrlKey&&this.toggleIsHide(),e.keyCode===L.Shift&&this.render(),e.keyCode===L.Tab){e.preventDefault(),this.selectToNextLine(e);return}if(this.isCreate&&this.keyboardEventWhileLineCreating(e),this.config.attributeConfigurable){const t=y.getAttributeByKeycode(e.keyCode,this.config.attributeList);t!==void 0&&this.setDefaultAttribute(t)}}selectToNextLine(e){const t=K.getNextSelectedRectIDByEvent(this.viewPortLines.map(i=>{var s,n,o,r;return T(w({},i),{x:(n=(s=i.pointList[0])==null?void 0:s.x)!=null?n:0,y:(r=(o=i.pointList[0])==null?void 0:o.y)!=null?r:0})}),e,this.selectedID);t&&this.selection.setSelectedIDs(t.id)}keyboardEventWhileLineCreating(e){!this.isCreate||(e.keyCode===L.Ctrl&&this.setInvalidLineOnCreating(e),[L.Shift,L.Alt].includes(e.keyCode)&&this.renderNextPoint(e,this.prevAxis))}renderNextPoint(e,t){const i=this.coordUtils.getRenderCoord(this.getNextPoint(e,t));this.render(i)}deleteSelectedLine(e){var t;if(this.selectedLine){const i=v.calcViewportBoundaries((t=this.selectedLine)==null?void 0:t.pointList,this.isCurve,p,this.zoom);u.inArea(i,this.coordUtils.getAbsCoord(e))&&this.deleteLine()}}deleteSelectedLinePoint(e){var t;this.pointsWithinRange(this.activeLine.length-1)&&e&&(this.setActiveLine(this.activeLine.filter(s=>s.id!==e)),this.updateLines(),(t=this.history)==null||t.pushHistory(this.lineList)),this.cursor=void 0,this.render()}deleteLine(){var e;this.lineList=this.lineList.filter(t=>!this.selection.isIdSelected(t.id)),(e=this.history)==null||e.pushHistory(this.lineList),this.setNoneStatus(),this.emit("dataUpdated",this.lineList),this.emit("lineDeleted",this.selectedID),this.render()}setInvalidLine(e,t,i=!0){var s;const n=this.lineList.find(o=>o.id===e);n&&(n.valid=t!==void 0?t:!n.valid,(s=this.history)==null||s.pushHistory(this.lineList),i&&this.render())}empty(){var e,t;this.lineList=[],this.setNoneStatus(),this.selectedPoint=void 0,(e=this.actionsHistory)==null||e.empty(),(t=this.history)==null||t.init(),this.emit("dataUpdated",this.lineList),this.render()}setAttribute(e){var t;this.attributeConfigurable&&(this.defaultAttribute=e,this.setLineAttribute("attribute",e),this.selectedIDs.length>0&&((t=this.history)==null||t.pushHistory(this.lineList)))}setTextAttribute(e){var t;this.isTextConfigurable&&(this.setLineAttribute("textAttribute",e),(t=this.history)==null||t.applyAttribute(this.selectedID,"textAttribute",e))}setLineAttribute(e,t){this.selectedIDs.length>0&&(this.lineList.forEach(i=>{this.selection.isIdSelected(i.id)&&(i[e]=t)}),this.render())}updateAttribute(e){this.emit("updateAttribute",e)}updateLineAttributes(e){var t;if(this.attributeConfigurable&&e){const i=(e==null?void 0:e.attribute)||"";this.defaultAttribute=i,this.updateAttribute(i)}if(this.isTextConfigurable&&e){const i=(e==null?void 0:e.textAttribute)||"";this.updateTextAttribute(i)}(t=this.history)==null||t.updateHistory(this.lineList)}lineStatusChanged(){this.emit("lineStatusChanged",{status:this.status,selectedLineID:this.selectedID})}updateTextAttribute(e){if(this.selectedID){const t=this.lineList.find(i=>i.id===this.selectedID);t&&(t.textAttribute=e)}this.emit("updateText",e)}saveData(){this.stopLineCreating(),this.setNoneStatus(),this.render()}setTextEditingID(e){this.textEditingID=e,this.render()}updateAttrWhileIDChanged(e){if(e){const t=this.lineList.find(i=>i.id===e);t&&this.setDefaultAttribute(t.attribute)}}setSelectedLineID(e,t=!1){this.selection.setSelectedIDs(e,t),this.updateAttrWhileIDChanged(this.selectedID),this.selectedLine&&this.setActiveLine(this.selectedLine.pointList),this.emit("selectedChange")}attributeLockListChange(e){this.attributeLockList=e,this.render()}setResult(e){this.setNoneStatus(),this.setLineList(e),this.render()}setConfig(e){super.setConfig(e)}toggleIsHide(){this.setIsHidden(!this.isHidden),this.render()}clearCanvas(){super.clearCanvas()}clearResult(){this.setResult([]),this.setSelectedLineID(void 0),this.render()}exportData(){return[this.lineList,this.basicImgInfo]}setDefaultAttribute(e=""){var t;this.attributeConfigurable&&(this.defaultAttribute=e,this.changeStyle(this.defaultAttribute),this.setLineAttribute("attribute",e),this.selectedIDs.length>0&&((t=this.history)==null||t.pushHistory(this.lineList)),this.emit("changeAttributeSidebar"))}getCurrentSelectedData(){var e,t;const i=this.isActiveLineValid(),s=this.defaultAttribute,n=this.getColor(s),o=i?n==null?void 0:n.valid.stroke:n==null?void 0:n.invalid.stroke,r=(t=(e=this.lineList.find(h=>h.id===this.selectedID))==null?void 0:e.textAttribute)!=null?t:"";return{color:o,textAttribute:r}}renderTextAttribute(){var e,t,i,s;if(!this.ctx||!this.selectedLine||this.activeLine&&((e=this.activeLine)==null?void 0:e.length)<2)return;const n=this.isActiveLineValid(),o=this.defaultAttribute,{x:r,y:h}=this.selectedLine.pointList[1],l=this.coordUtils.getRenderCoord({x:r,y:h}),a=this.getColor(o),c=n?a==null?void 0:a.valid.stroke:a==null?void 0:a.invalid.stroke,f=(i=(t=this.lineList.find(b=>b.id===this.selectedID))==null?void 0:t.textAttribute)!=null?i:"";this._textAttributeInstance||(this._textAttributeInstance=new $({container:this.container,icon:this.getTextIconSvg(o),color:c,getCurrentSelectedData:this.getCurrentSelectedData,updateSelectedTextAttribute:this.updateSelectedTextAttribute})),this._textAttributeInstance&&!((s=this._textAttributeInstance)==null?void 0:s.isExit)&&this._textAttributeInstance.appendToContainer(),this._textAttributeInstance.update(`${f}`,{left:l.x,top:l.y,color:c}),this._textAttributeInstance.updateIcon(this.getTextIconSvg(o))}getTextIconSvg(e=""){return y.getTextIconSvg(e,this.config.attributeList,this.config.attributeConfigurable,this.baseIcon)}updateSelectedTextAttribute(e){if(this._textAttributeInstance&&e&&this.selectedID){let t=e;!y.textAttributeValidate(this.config.textCheckType,"",t)&&(this.emit("messageError",y.getErrorNotice(this.config.textCheckType,this.lang)),t=""),this.setTextAttribute(t),this.emit("updateTextAttribute"),this.render()}}}export{H as INNER_POINT_RADIUS,C as LINE_ORDER_OFFSET,m as POINT_ACTIVE_RADIUS,x as POINT_RADIUS,p as SEGMENT_NUMBER,et as default};
|
|
1
|
+
import g from"lodash";import{ELineTypes as P,ELineColor as U,EToolName as _,ETextType as M}from"../../constant/tool.js";import B from"../../utils/ActionsHistory.js";import S from"../../utils/uuid.js";import L from"../../constant/keyCode.js";import v from"../../utils/MathUtils.js";import{BasicToolOperation as j}from"./basicToolOperation.js";import u from"../../utils/tool/LineToolUtils.js";import{createSmoothCurvePointsFromPointList as R,isInPolygon as V,createSmoothCurvePoints as W}from"../../utils/tool/polygonTool.js";import K from"../../utils/tool/CommonToolUtils.js";import N from"../../utils/tool/CanvasUtils.js";import F from"../../utils/tool/DrawUtils.js";import z from"../../utils/tool/StyleUtils.js";import y from"../../utils/tool/AttributeUtils.js";import $ from"./textAttributeClass.js";import J from"./Selection.js";import Y from"../../locales/index.js";import{EMessage as X}from"../../locales/constants.js";var q=Object.defineProperty,G=Object.defineProperties,Z=Object.getOwnPropertyDescriptors,O=Object.getOwnPropertySymbols,Q=Object.prototype.hasOwnProperty,tt=Object.prototype.propertyIsEnumerable,E=(d,e,t)=>e in d?q(d,e,{enumerable:!0,configurable:!0,writable:!0,value:t}):d[e]=t,w=(d,e)=>{for(var t in e||(e={}))Q.call(e,t)&&E(d,t,e[t]);if(O)for(var t of O(e))tt.call(e,t)&&E(d,t,e[t]);return d},T=(d,e)=>G(d,Z(e)),k;(function(d){d[d.Create=0]="Create",d[d.Active=1]="Active",d[d.None=2]="None"})(k||(k={}));const p=16,C={x:0,y:20},x=3,m=5,H=2;class et extends j{constructor(e){super(e);this.drawActivatedLine=(t,i,s)=>{const n=this.isCreate?g.cloneDeep([{pointList:this.activeLine,order:this.nextOrder(),attribute:this.defaultAttribute,valid:this.isLineValid}]):g.cloneDeep(this.selectedLines),o=this.isActiveLineValid();n.forEach(r=>{if((r==null?void 0:r.pointList)&&(r.pointList=r.pointList.map(l=>Object.assign(l,{actual:{x:l.x,y:l.y}},this.coordUtils.getRenderCoord(l)))),!r.pointList)return;const h=this.getLineColorByAttribute({attribute:r.attribute,valid:r.valid});return this.selectedLines.length>0&&(this.drawLineLength(r.pointList,h),this.renderActiveArea(v.calcViewportBoundaries(r.pointList,this.isCurve,p))),this.drawLine(r.pointList,t,h,!0,!0),this.drawLineNumber(r.pointList[0],r.order,h,"",this.defaultAttribute,o),t&&this.isCreate&&this.arc(t,x,h),this.cursor&&!this.selectedPoint&&!s&&!this.isShift&&this.arc(this.cursor,m,h),r})},this.drawHoverPoint=t=>{if(!this.isMousedown&&t&&this.selectedID){const i=this.getPointList(this.activeLine),s=this.activeLine.find(o=>u.calcDistance(this.coordUtils.getRenderCoord(o),t)<=m);let n;!s&&this.activeLine&&(n=this.findNearestPoint(i,t)),this.hoverPointID=s?s.id:void 0,this.cursor=s||n==null?void 0:n.point}},this.updatedLine={id:"",valid:!1,order:0},this.toolName="lineTool",this.lineList=[],this.activeLine=[],this.coordsInsideActiveArea=!1,this.hoverLineSegmentIndex=-1,this.isShift=!1,this.isReference=!1,this.drawCurveLine=(t,i,s,n=!0,o=!1,r)=>{const h=R(i,p);t.save(),t.lineCap="round",t.lineJoin="round",t.strokeStyle=s.color,n&&(t.lineWidth=s.lineWidth),o&&u.setReferenceCtx(t),i.forEach(({specialEdge:l},a)=>{const c=h.splice(0,p+1);t.save(),t.beginPath(),r===a&&(t.lineWidth=4),c.forEach(({x:f,y:b},I)=>{const D=I>0?"lineTo":"moveTo";l&&u.setSpecialEdgeStyle(t),t[D](f,b)}),t.stroke(),t.restore()}),t.restore()},this.drawLine=(t,i,s,n=!1,o=!1)=>{const r=i?t.concat(i):t,h={color:s,lineWidth:o?1:this.lineStyle.lineWidth};this.isCurve?u.drawCurveLine(this.ctx,r,h,!n,this.isReference,o?this.hoverLineSegmentIndex:-1):this.drawStraightLine(r,h,o),n&&t.forEach(l=>{var a,c;const f=l.id,b=f&&[this.hoverPointID,(a=this.selectedPoint)==null?void 0:a.id].includes(f)?m:x;this.arc(l,b,s),[this.hoverPointID,(c=this.selectedPoint)==null?void 0:c.id].includes(f)||this.arc(l,H,"white")})},this.drawStraightLine=(t,i,s=!1)=>{const{ctx:n}=this;n&&(n.save(),n.lineCap="round",n.lineJoin="round",n.strokeStyle=i.color,n.lineWidth=i.lineWidth,this.isReference&&u.setReferenceCtx(n),t.forEach((o,r)=>{if(n.beginPath(),r>0){const h=t[r-1];n.save(),(h==null?void 0:h.specialEdge)&&u.setSpecialEdgeStyle(n),s&&this.hoverLineSegmentIndex+1===r&&(n.lineWidth=4),n.moveTo(h.x,h.y),n.lineTo(o.x,o.y),n.stroke(),n.restore()}}),n.restore())},this.drawLines=()=>{try{const t=g.cloneDeep(this.attributeFilteredLines);if(this.isHidden)return;t.forEach(i=>{if(!this.selection.isIdSelected(i.id)&&i.pointList){i.pointList.map(h=>Object.assign(h,{actual:{x:h.x,y:h.y}},this.coordUtils.getRenderCoord(h)));const{order:s,label:n}=i,o=s,r=i&&this.getLineColorByAttribute(i);this.drawLine(i.pointList,void 0,r,!1),this.drawLineNumber(i.pointList[0],o,r,n,i.attribute,i.valid),i.id!==this.textEditingID&&(this.drawLineTextAttribute(i.pointList[1],r,i==null?void 0:i.textAttribute),this.drawLineLength(i.pointList,r))}})}catch(t){console.error(t,"\u7EBF\u6761\u5DE5\u5177\u6570\u636E\u89E3\u6790\u9519\u8BEF"),this.lineList=[],this.clearCanvas()}},this.render=t=>{super.render(),this.drawLines(),this.drawActivatedLine(t,void 0,!0),this.renderTextAttribute(),this.renderCursorLine(this.getLineColor(this.defaultAttribute))},this.moveLineInPolygon=(t,i)=>{var s;if(!Array.isArray(this.activeLine))return!1;((s=this.activeLine)==null?void 0:s.every(o=>this.isInBasicPolygon({x:o.x+t,y:o.y+i})))&&(this.lineDragging=!0,this.moveActiveArea(t,i))},this.getSelectedLinesArea=()=>v.calcViewportBoundaries(this.selectedLines.reduce((t,i)=>(i==null?void 0:i.pointList)?t.concat(...i==null?void 0:i.pointList):t,[])),this.moveLineInRectRange=(t,i,s,n)=>{if(this.selectedLines.length===0)return;const{top:o,left:r,right:h,bottom:l}=this.getSelectedLinesArea(),a=[r,h].map(A=>g.isNumber(A)?A+t:0),c=[o,l].map(A=>g.isNumber(A)?A+i:0),f=r>=0&&h&&v.isInRange(a,s),b=o>=0&&l&&v.isInRange(c,n),I=f?t:0,D=b?i:0;this.lineDragging=!0,this.moveActiveArea(I,D)},this.onRightClick=t=>{if(this.cursor=void 0,this.isCreate){if(this.isLinePointsNotEnough())return;if(u.lineLengthSum(this.activeLine||[])<this.minLength)return this.emit("messageInfo",`${Y.getMessagesByLocale(X.MinLengthLimitErrorNotice,this.lang)}${this.minLength}`),!0;this.stopLineCreating(!0);return}const i=this.findHoverLine(this.getCoordinate(t));this.setSelectedLineID(i==null?void 0:i.id,t.ctrlKey),this.emit("contextmenu")},this.updateSelectedAttributeAfterHistoryChanged=()=>{if(this.selectedIDs.length>0){const t=this.lineList.find(s=>s.id===this.selectedIDs[0]),i=t==null?void 0:t.attribute;typeof i=="string"&&(this.defaultAttribute=i,this.updateAttribute(i),this.render())}},this.onLeftClick=t=>{const i=this.getCoordinate(t),{lineDragging:s}=this;if(this.lineDragging=!1,this.isSpaceKey)return;if(this.isNone&&t.ctrlKey){const o=this.findHoverLine(i);o&&this.setInvalidLine(o.id);return}if(this.isLinePointsExceed())return;const n=this.getNextPoint(t,i);if(this.isCreate||this.isNone){this.setCreateStatusAndAddPoint(n);return}if(this.isActive){if(s)return;if(this.isMouseCoordOutsideActiveArea()){this.setNoneStatus(!1),this.setCreateStatusAndAddPoint(n);return}if(t.shiftKey){this.updateLineSegmentSpecial(i);return}this.coordsInsideActiveArea&&t.ctrlKey&&this.setInvalidLine(this.selectedID)}},this.onDblclick=()=>{},this.onKeyUp=t=>{if(super.onKeyUp(t),this.isShift=!1,this.hoverLineSegmentIndex=-1,!this.selection.triggerKeyboardEvent(t,this.setLineList)){if(t.keyCode===L.Esc){this.stopLineCreating(!1);return}if(this.selectedLine){if(t.key==="Delete"){this.deleteLine();return}if(t.key==="f"){this.setInvalidLine(this.selectedID);return}if(t.key===" "){this.continueToEdit();return}}this.keyboardEventWhileLineCreating(t)}},this.onRightDblClick=t=>{super.onRightDblClick(t);const i=this.getCoordinate(t);if(this.isActive){const s=this.findHoveredPoint(i);if(s){this.deleteSelectedLinePoint(s.id);return}this.deleteSelectedLine(i)}},this.setReference=t=>{this.isReference=t},this.pointsWithinRange=t=>!(this.lowerLimitPointNum&&t<this.lowerLimitPointNum||this.upperLimitPointNum&&t>this.upperLimitPointNum),this.setLineList=t=>{const i=t.length!==this.lineListLen;this.lineList=t,i&&this.emit("updatePageNumber")},this.textChange=t=>{this.config.textConfigurable===!1||!this.selectedID||(this.updateSelectedTextAttribute(t),this.emit("selectedChange"))},this.status=2,this.isMousedown=!1,this.lineDragging=!1,this.isLineValid=!0,this.setConfig(e.config),this.prevAxis={x:0,y:0},this.textEditingID="",this.updateSelectedTextAttribute=this.updateSelectedTextAttribute.bind(this),this.getCurrentSelectedData=this.getCurrentSelectedData.bind(this),this.actionsHistory=new B,this.selection=new J(this),this.dependToolConfig={lineType:P.Line}}get selectedLines(){return this.lineList.filter(e=>this.selection.isIdSelected(e.id))}get isLineSelected(){return this.selectedID&&this.activeLine}get selectedLine(){return this.lineList.find(e=>e.id===this.selectedID)}get isCreate(){return this.status===0}get isActive(){return this.selectedLines.length>0}get isNone(){return!this.isCreate&&this.selectedIDs.length===0}get isCurve(){return this.config.lineType===P.Curve}get isMultipleColor(){return this.config.lineColor===U.MultiColor}get imageSize(){return this.rotate%180==0?this.basicImgInfo:{width:this.basicImgInfo.height,height:this.basicImgInfo.width}}get lineListLen(){return this.lineList.length}get edgeAdsorptionEnabled(){return this.edgeAdsorption&&!this.isCurve&&this.lineListLen>0}get attributeConfigurable(){return this.config.attributeConfigurable}get isTextConfigurable(){return this.config.textConfigurable}get isDependPolygon(){return this.dependToolName===_.Polygon}get isDependRect(){return this.dependToolName===_.Rect}get isCurrentAttributeLocked(){return this.attributeLockList.includes(this.defaultAttribute)}get attributeFilteredLines(){return this.attributeLockList.length>0?this.lineList.filter(e=>this.attributeLockList.includes((e==null?void 0:e.attribute)||"")):this.lineList}get selectedIDs(){return this.selection.selectedIDs}get selectedID(){return this.selection.selectedID}get enableOutOfTarget(){return this.config.enableOutOfTarget||this.config.outOfTarget||this.config.drawOutsideTarget}get showOrder(){var e;return(e=this.config.showOrder)!=null?e:this.config.isShowOrder}get edgeAdsorption(){return this.config.edgeAdsorption}get attributeList(){return this.config.attributeList}get lowerLimitPointNum(){return this.config.lowerLimitPointNum}get minLength(){var e;return((e=this.config)==null?void 0:e.minLength)||0}get upperLimitPointNum(){return this.config.upperLimitPointNum}get textCheckType(){return this.config.textCheckType}get customFormat(){return this.config.customFormat}get dataList(){return this.lineList}get hasActiveLine(){return this.activeLine&&this.activeLine.length>0}get viewPortLines(){const e=N.getViewPort(this.canvas,this.currentPos,this.zoom);return this.isHidden?[]:this.attributeFilteredLines.filter(t=>{var i;return(i=t==null?void 0:t.pointList)==null?void 0:i.some(s=>N.inViewPort(s,e))})}get lineStyle(){return{lineWidth:this.style.width,color:this.getLineColor(this.defaultAttribute),opacity:this.style.opacity}}get selectedText(){var e,t;return(t=(e=this.lineList.find(i=>i.id===this.selectedID))==null?void 0:e.textAttribute)!=null?t:""}get currentPageResult(){return this.lineList}updateStatus(e,t=!1){if(e!==this.status){if(t){let i="";this.textCheckType===M.Order&&this.isTextConfigurable&&(i=y.getTextAttribute(this.lineList,this.textCheckType)),this.emit("updateText",i)}this.status=e,this.lineStatusChanged()}}isInBasicPolygon(e){var t,i;return V(e,((t=this.basicResult)==null?void 0:t.pointList)||[],(i=this.dependToolConfig)==null?void 0:i.lineType)}getPolygonPointList(){if(!this.basicResult)return[];const{pointList:e}=this.basicResult,{lineType:t}=this.dependToolConfig;return t===P.Curve?W(e.reduce((i,s)=>[...i,s.x,s.y],[]),.5,!0,20):e}getNextCoordByRenderCoord(e){return this.getNextCoordByAbsCoord(this.coordUtils.getAbsCoord(e))}getNextCoordByAbsCoord(e){var t;const i=(t=this.activeLine)==null?void 0:t.slice(-1)[0];return i?this.coordUtils.getNextCoordByDependTool(e,i):e}pointInLine(e,t,i){return e.filter(s=>s).length<2?!1:e.some((s,n)=>{if(n===0)return!1;const o=this.coordUtils.getRenderCoord(e[n-1]),r=this.coordUtils.getRenderCoord(s);return u.isInLine(t,o,r,i)})}arc(e,t=x,i){var s,n,o,r,h,l;if(this.ctx){const{x:a,y:c}=e;(s=this.ctx)==null||s.save(),(n=this.ctx)==null||n.beginPath(),this.ctx.fillStyle=i||this.lineStyle.color,(o=this.ctx)==null||o.arc(a,c,t,0,360),(r=this.ctx)==null||r.closePath(),(h=this.ctx)==null||h.fill(),(l=this.ctx)==null||l.restore()}}renderActiveArea({top:e,left:t,right:i,bottom:s}){this.ctx&&(this.ctx.save(),this.ctx.beginPath(),this.ctx.strokeStyle="#B3B8FF",this.ctx.rect(t,e,i-t,s-e),this.ctx.stroke(),this.ctx.restore())}addLinePoint(e){var t,i,s,n;this.arc(e),(t=this.activeLine)==null||t.push(T(w({},e),{id:S()})),((i=this.activeLine)==null?void 0:i.length)===1?(s=this.actionsHistory)==null||s.initRecord(this.activeLine):(n=this.actionsHistory)==null||n.pushHistory(this.activeLine),this.render()}setCreateStatusAndAddPoint(e,t=!1){this.updateStatus(0,t),this.addLinePoint(e)}isActiveLineValid(){var e;return this.selectedID?(e=this.lineList.find(t=>t.id===this.selectedID))==null?void 0:e.valid:this.isLineValid}nextOrder(){return this.lineListLen===0?1:this.lineList.slice(-1)[0].order+1}getLineColorByAttribute(e,t=!1){return z.getStrokeAndFill(this.getColor(e.attribute),e.valid,{isSelected:t}).stroke}drawLineNumber(e,t=1,i,s="",n,o=!0){var r,h,l;if((this.showOrder||this.attributeConfigurable)&&this.ctx){let a=this.showOrder?t.toString():`${s}`;if(this.attributeConfigurable){const c=n?(l=(h=(r=this.attributeList)==null?void 0:r.find(f=>f.value===n))==null?void 0:h.key)!=null?l:n:"";a=[a,`${!o&&c?"\u65E0\u6548":""}${c}`].filter(f=>f).join("_")}this.drawText(e,a,i)}}drawLineTextAttribute(e,t,i){if(e&&i)return this.drawText(e,i,t,200)}drawLineLength(e,t){var i;if(((i=this.config)==null?void 0:i.showLineLength)&&e){const s=e.reduce((o,r,h)=>h<=0||!e[h-1].actual||!r.actual?o:o+u.calcDistance(e[h-1].actual,r.actual),0),n=e[e.length-1];this.drawText(n,`l = ${s.toFixed(2)}`,t)}}drawText(e,t,i,s){var n,o;this.ctx&&((n=this.ctx)==null||n.save(),this.ctx.font="italic bold 14px SourceHanSansCN-Regular",this.ctx.fillStyle=i,this.ctx.strokeStyle=i,this.ctx.shadowColor="rgba(0, 0, 0, 0.6)",this.ctx.shadowOffsetY=2,this.ctx.shadowBlur=4,s?F.wrapText(this.canvas,t,e.x-C.x,e.y-C.y,s):this.ctx.fillText(t,e.x-C.x,e.y-C.y),(o=this.ctx)==null||o.restore())}moveActiveArea(e,t){this.selectedLines.length>0&&(this.selectedLines.forEach(i=>{var s;(s=i.pointList)==null||s.forEach(n=>Object.assign(n,{x:n.x+e,y:n.y+t}))}),this.render()),this.emit("dataUpdated",this.lineList,this.selectedIDs)}findHoveredPoint(e){if(!!this.activeLine)return this.activeLine.find(t=>{const i=this.coordUtils.getRenderCoord(t);return u.calcDistance(i,e)<=m})}findHoverLine(e){return g.cloneDeep(this.lineList).reverse().find(({pointList:i})=>{const s=i?this.getPointList(i):[],n=this.getLineWidthScope();return s.some((o,r)=>{if(r===0)return!1;const h=this.coordUtils.getRenderCoord(o),l=this.coordUtils.getRenderCoord(s[r-1]);return u.isInLine(e,h,l,n)})})}getAdsorptionPoint(e){let t,i,s;return g.cloneDeep(this.lineList).reverse().forEach(({pointList:n,id:o})=>{if(o===this.selectedID||!n||(n==null?void 0:n.length)<2)return;const r=this.findNearestPoint(n,e);if(r){if(r.minDistance===0){t=r.point;return}(i===void 0||r.minDistance<i)&&(t=r.point,i=r.minDistance)}}),s||t}findNearestPoint(e,t,i=7){let s;const n=i;for(let o=1;o<=e.length-1;o++){const r=this.coordUtils.getRenderCoord(e[o]),h=this.coordUtils.getRenderCoord(e[o-1]),{length:l,footPoint:a}=v.getFootOfPerpendicular(t,r,h),c=u.calcTwoPointDistance(r,t),f=u.calcTwoPointDistance(h,t);if(c<=i*2){s=r,i=0;break}if(f<=i*2){s=h,i=0;break}l<i&&(s=a,i=l)}return s?{point:s,minDistance:n}:void 0}getPointList(e){return this.isCurve?R(e,p):e}moveSelectedLine(e){const t=(e.x-this.prevAxis.x)/this.zoom,i=(e.y-this.prevAxis.y)/this.zoom;if(this.enableOutOfTarget){this.lineDragging=!0,this.moveActiveArea(t,i);return}if(this.isDependPolygon){this.moveLineInPolygon(t,i);return}let s=[0,this.imageSize.width],n=[0,this.imageSize.height];if(this.isDependRect){const{x:o,y:r,width:h,height:l}=this.basicResult;s=[o,o+h],n=[r,r+l]}this.moveLineInRectRange(t,i,s,n)}moveSelectPoint(e){if(!this.selectedPoint)return;const t=e.x-this.prevAxis.x,i=e.y-this.prevAxis.y,s=(this.selectedPoint?this.selectedPoint.x:0)+t/this.zoom,n=(this.selectedPoint?this.selectedPoint.y:0)+i/this.zoom,o={x:s,y:n};Object.assign(this.selectedPoint,this.getNextCoordByAbsCoord(o)),this.updateLines(),this.render()}getCoordByConfig(e,t){var i;const s=!!e.shiftKey,n=e.altKey;if(((i=this.activeLine)==null?void 0:i.length)>0&&s){const o=this.activeLine.slice(-1)[0];return u.getVHPoint(o,t,this.coordUtils.getAbsCoord(t),this.coordUtils.getRenderCoord(o))}return this.edgeAdsorptionEnabled&&!n?this.getAdsorptionPoint(t):t}getNextPoint(e,t){const i=this.getCoordByConfig(e,t)||t;return this.enableOutOfTarget?this.coordUtils.getAbsCoord(i):this.getNextCoordByRenderCoord(i)}lineHover(){this.render()}mouseMoveHandler(e){const t=this.getCoordinate(e),i=e.which===1;if(this.isCreate){this.hasActiveLine&&this.renderNextPoint(e,t);return}if(this.isNone&&(this.lineHover(),this.edgeAdsorptionEnabled&&!e.altKey)){const s=this.getAdsorptionPoint(t);s&&this.arc(s)}if(this.isActive){if(this.isMousedown&&i){if(this.selectedPoint){this.moveSelectPoint(t);return}if(this.coordsInsideActiveArea){this.moveSelectedLine(t),this.drawActivatedLine(void 0,void 0,!0);return}}this.drawHoverPoint(t),this.render()}}onMouseMove(e){if(super.onMouseMove(e)||this.forbidMouseOperation||!this.imgInfo)return;const t=this.getCoordinate(e);this.mouseMoveHandler(e),this.prevAxis=t}setActiveLine(e){this.activeLine=e?g.cloneDeep(e):void 0}historyChanged(e){const t=`${e}Enabled`;if(this.isCreate){if(this.actionsHistory&&this.actionsHistory[t]){const i=this.actionsHistory&&this.actionsHistory[e]();this.setActiveLine(i),this.render()}return}if(this.history&&this.history[t]){const i=this.history[e](),s=i==null?void 0:i.find(n=>n.id===this.selectedID);this.lineList=i,this.selectedID&&s?this.setActiveLine(s==null?void 0:s.pointList):this.setNoneStatus(),this.render()}this.emit("dataUpdated",this.lineList)}undo(){this.historyChanged("undo"),this.updateSelectedAttributeAfterHistoryChanged()}redo(){this.historyChanged("redo"),this.updateSelectedAttributeAfterHistoryChanged()}isCoordInsideTarget(e){if(this.isDependPolygon)return this.isInBasicPolygon(e);if(this.isDependRect){const{x:t,y:i,width:s,height:n}=this.basicResult,o=[t,t+s],r=[i,i+n];return v.isInRange(e.x,o)&&v.isInRange(e.y,r)}return v.isInRange(e.x,[0,this.imageSize.width])&&v.isInRange(e.y,[0,this.imageSize.height])}getPointInsertIndex(e,t){if(e&&this.activeLine){const i=this.getPointList(this.activeLine);return this.activeLine.length===2?1:this.activeLine.findIndex((s,n)=>{if(n>0){const o=this.activeLine?this.activeLine[n-1]:void 0,r=this.isCurve?i.slice((n-1)*(p+1),n*(p+1)):[o,s];return this.pointInLine(r,e,t||this.getLineWidthScope())}return!1})}return-1}getLineWidthScope(){return this.lineStyle.lineWidth}isMouseCoordOutsideActiveArea(){return!this.coordsInsideActiveArea&&!this.selectedPoint}isLinePointsExceed(){return this.isCreate&&this.activeLine&&this.upperLimitPointNum&&~~this.upperLimitPointNum<=this.activeLine.length}isLinePointsNotEnough(){var e;return this.activeLine&&((e=this.activeLine)==null?void 0:e.length)<this.lowerLimitPointNum}updateLineSegmentSpecial(e){const t=this.getPointInsertIndex(e,2)-1;if(t>-1){const i=this.activeLine[t];i.specialEdge=!i.specialEdge,this.hoverLineSegmentIndex=-1,this.render()}}addLinePointToActiveLine(){var e;const t=this.getPointInsertIndex(this.cursor),i=this.pointsWithinRange(this.activeLine.length+1);this.cursor&&t>-1&&i&&(this.activeLine.splice(t,0,T(w({},this.coordUtils.getAbsCoord(this.cursor)),{id:S()})),this.updateLines(),(e=this.history)==null||e.pushHistory(this.lineList),this.render(),this.cursor=void 0)}isCoordOnSelectedArea(e){return this.selectedLines.some(t=>u.inArea(v.calcViewportBoundaries(t.pointList),this.coordUtils.getAbsCoord(e)))}onMouseDown(e){if(super.onMouseDown(e)||this.forbidMouseOperation||!this.imgInfo)return;const t=this.getCoordinate(e);if(this.isMousedown=!0,this.prevAxis=t,e.which===3){this.cursor=void 0;return}this.selectedPoint=this.findHoveredPoint(t),this.coordsInsideActiveArea=this.selectedLines.length>0?this.isCoordOnSelectedArea(t):!1,this.lineDragging=!1}lineHasChanged(){const e=this.lineList.find(t=>t.id===this.selectedID);return e?JSON.stringify(e.pointList)!==JSON.stringify(this.activeLine):!1}updateLines(){const e=this.lineList.find(t=>t.id===this.selectedID);e&&(e.pointList=g.cloneDeep(this.activeLine),this.updatedLine=e,this.emit("dataUpdated",this.lineList))}onMouseUp(e){const t=()=>{this.isMousedown=!1,this.hoverPointID=void 0,this.cursor=void 0,this.selectedPoint=void 0};if(this.hoverPointID=void 0,super.onMouseUp(e)||this.forbidMouseOperation||!this.imgInfo){t();return}e.which===1&&this.onLeftClick(e),e.which===3&&this.onRightClick(e),t()}isTextValid(e){return y.textAttributeValidate(this.textCheckType,this.customFormat,e)}createLineData(){const e=S(),t={pointList:g.cloneDeep(this.activeLine),id:e,valid:this.isLineValid,order:this.nextOrder()};return t.attribute=this.defaultAttribute,t}stopLineCreating(e=!0){var t,i,s,n;const o=this.selectedID?!0:!!this.isTextConfigurable;let r;if(e){if(this.selectedID){const h=this.lineList.find(l=>l.id===this.selectedID);r=this.selectedID,h&&(h.pointList=g.cloneWith(this.activeLine),g.isEqual(h.pointList,(t=this.history)==null?void 0:t.pushHistory(this.lineList))||(i=this.history)==null||i.pushHistory(this.lineList))}else if(this.isCreate&&this.activeLine&&this.activeLine.length>1){const h=this.createLineData();r=h.id,this.setLineList([...this.lineList,h]),(s=this.history)==null||s.pushHistory(this.lineList)}}this.setNoneStatus(),o&&(this.activeLine=[],this.setSelectedLineID(r)),(n=this.actionsHistory)==null||n.empty(),this.emit("dataUpdated",this.lineList,this.selectedIDs),this.render()}setNoneStatus(e=!0){e&&this.updateStatus(2),this.activeLine=[],this.setSelectedLineID(void 0),this.isLineValid=!0,this.cursor=void 0}setKeyDownStatus(e,t){this.isShift=t!=null?t:e.keyCode===L.Shift}continueToEdit(){var e,t;((e=this.selectedLine)==null?void 0:e.pointList)&&(this.updateStatus(0),this.cursor=void 0,this.setActiveLine(this.selectedLine.pointList),(t=this.actionsHistory)==null||t.pushHistory(this.activeLine),this.render())}setInvalidLineOnCreating(e){if(this.selectedID&&e.keyCode!==L.Ctrl||!this.isCreate)return;const t=!e.ctrlKey;this.selectedID?this.setInvalidLine(this.selectedID,t,!1):this.isLineValid=t}onKeyDown(e){if(super.onKeyDown(e),this.setKeyDownStatus(e),e.keyCode===L.Z&&!e.ctrlKey&&this.toggleIsHide(),e.keyCode===L.Shift&&this.render(),e.keyCode===L.Tab){e.preventDefault(),this.selectToNextLine(e);return}if(this.isCreate&&this.keyboardEventWhileLineCreating(e),this.config.attributeConfigurable){const t=y.getAttributeByKeycode(e.keyCode,this.config.attributeList);t!==void 0&&this.setDefaultAttribute(t)}}selectToNextLine(e){const t=K.getNextSelectedRectIDByEvent(this.viewPortLines.map(i=>{var s,n,o,r;return T(w({},i),{x:(n=(s=i.pointList[0])==null?void 0:s.x)!=null?n:0,y:(r=(o=i.pointList[0])==null?void 0:o.y)!=null?r:0})}),e,this.selectedID);t&&this.selection.setSelectedIDs(t.id)}keyboardEventWhileLineCreating(e){!this.isCreate||(e.keyCode===L.Ctrl&&this.setInvalidLineOnCreating(e),[L.Shift,L.Alt].includes(e.keyCode)&&this.renderNextPoint(e,this.prevAxis))}renderNextPoint(e,t){const i=this.coordUtils.getRenderCoord(this.getNextPoint(e,t));this.render(i)}deleteSelectedLine(e){var t;if(this.selectedLine){const i=v.calcViewportBoundaries((t=this.selectedLine)==null?void 0:t.pointList,this.isCurve,p,this.zoom);u.inArea(i,this.coordUtils.getAbsCoord(e))&&this.deleteLine()}}deleteSelectedLinePoint(e){var t;this.pointsWithinRange(this.activeLine.length-1)&&e&&(this.setActiveLine(this.activeLine.filter(s=>s.id!==e)),this.updateLines(),(t=this.history)==null||t.pushHistory(this.lineList)),this.cursor=void 0,this.render()}deleteLine(){var e;this.lineList=this.lineList.filter(t=>!this.selection.isIdSelected(t.id)),(e=this.history)==null||e.pushHistory(this.lineList),this.setNoneStatus(),this.emit("dataUpdated",this.lineList),this.emit("lineDeleted",this.selectedID),this.render()}setInvalidLine(e,t,i=!0){var s;const n=this.lineList.find(o=>o.id===e);n&&(n.valid=t!==void 0?t:!n.valid,(s=this.history)==null||s.pushHistory(this.lineList),i&&this.render())}empty(){var e,t;this.lineList=[],this.setNoneStatus(),this.selectedPoint=void 0,(e=this.actionsHistory)==null||e.empty(),(t=this.history)==null||t.init(),this.emit("dataUpdated",this.lineList),this.render()}setAttribute(e){var t;this.attributeConfigurable&&(this.defaultAttribute=e,this.setLineAttribute("attribute",e),this.selectedIDs.length>0&&((t=this.history)==null||t.pushHistory(this.lineList)))}setTextAttribute(e){var t;this.isTextConfigurable&&(this.setLineAttribute("textAttribute",e),(t=this.history)==null||t.applyAttribute(this.selectedID,"textAttribute",e))}setLineAttribute(e,t){this.selectedIDs.length>0&&(this.lineList.forEach(i=>{this.selection.isIdSelected(i.id)&&(i[e]=t)}),this.render())}updateAttribute(e){this.emit("updateAttribute",e)}updateLineAttributes(e){var t;if(this.attributeConfigurable&&e){const i=(e==null?void 0:e.attribute)||"";this.defaultAttribute=i,this.updateAttribute(i)}if(this.isTextConfigurable&&e){const i=(e==null?void 0:e.textAttribute)||"";this.updateTextAttribute(i)}(t=this.history)==null||t.updateHistory(this.lineList)}lineStatusChanged(){this.emit("lineStatusChanged",{status:this.status,selectedLineID:this.selectedID})}updateTextAttribute(e){if(this.selectedID){const t=this.lineList.find(i=>i.id===this.selectedID);t&&(t.textAttribute=e)}this.emit("updateText",e)}saveData(){this.stopLineCreating(),this.setNoneStatus(),this.render()}setTextEditingID(e){this.textEditingID=e,this.render()}updateAttrWhileIDChanged(e){if(e){const t=this.lineList.find(i=>i.id===e);t&&this.setDefaultAttribute(t.attribute)}}setSelectedLineID(e,t=!1){this.selection.setSelectedIDs(e,t),this.updateAttrWhileIDChanged(this.selectedID),this.selectedLine&&this.setActiveLine(this.selectedLine.pointList),this.emit("selectedChange")}attributeLockListChange(e){this.attributeLockList=e,this.render()}setResult(e){this.setNoneStatus(),this.setLineList(e),this.render()}setConfig(e){super.setConfig(e)}toggleIsHide(){this.setIsHidden(!this.isHidden),this.render()}clearCanvas(){super.clearCanvas()}clearResult(){this.setResult([]),this.setSelectedLineID(void 0),this.render()}exportData(){return[this.lineList,this.basicImgInfo]}setDefaultAttribute(e=""){var t;this.attributeConfigurable&&(this.defaultAttribute=e,this.changeStyle(this.defaultAttribute),this.setLineAttribute("attribute",e),this.selectedIDs.length>0&&((t=this.history)==null||t.pushHistory(this.lineList)),this.emit("changeAttributeSidebar"))}getCurrentSelectedData(){var e,t;const i=this.isActiveLineValid(),s=this.defaultAttribute,n=this.getColor(s),o=i?n==null?void 0:n.valid.stroke:n==null?void 0:n.invalid.stroke,r=(t=(e=this.lineList.find(h=>h.id===this.selectedID))==null?void 0:e.textAttribute)!=null?t:"";return{color:o,textAttribute:r}}renderTextAttribute(){var e,t,i,s;if(!this.ctx||!this.selectedLine||this.activeLine&&((e=this.activeLine)==null?void 0:e.length)<2)return;const n=this.isActiveLineValid(),o=this.defaultAttribute,{x:r,y:h}=this.selectedLine.pointList[1],l=this.coordUtils.getRenderCoord({x:r,y:h}),a=this.getColor(o),c=n?a==null?void 0:a.valid.stroke:a==null?void 0:a.invalid.stroke,f=(i=(t=this.lineList.find(b=>b.id===this.selectedID))==null?void 0:t.textAttribute)!=null?i:"";this._textAttributeInstance||(this._textAttributeInstance=new $({container:this.container,icon:this.getTextIconSvg(o),color:c,getCurrentSelectedData:this.getCurrentSelectedData,updateSelectedTextAttribute:this.updateSelectedTextAttribute})),this._textAttributeInstance&&!((s=this._textAttributeInstance)==null?void 0:s.isExit)&&this._textAttributeInstance.appendToContainer(),this._textAttributeInstance.update(`${f}`,{left:l.x,top:l.y,color:c}),this._textAttributeInstance.updateIcon(this.getTextIconSvg(o))}getTextIconSvg(e=""){return y.getTextIconSvg(e,this.config.attributeList,this.config.attributeConfigurable,this.baseIcon)}updateSelectedTextAttribute(e){if(this._textAttributeInstance&&e&&this.selectedID){let t=e;!y.textAttributeValidate(this.config.textCheckType,"",t)&&(this.emit("messageError",y.getErrorNotice(this.config.textCheckType,this.lang)),t=""),this.setTextAttribute(t),this.emit("updateTextAttribute"),this.render()}}}export{H as INNER_POINT_RADIUS,C as LINE_ORDER_OFFSET,m as POINT_ACTIVE_RADIUS,x as POINT_RADIUS,p as SEGMENT_NUMBER,et as default};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
import j from"lodash";import{i18n as _}from"@labelbee/lb-utils";import D from"../../utils/MathUtils.js";import N from"../../utils/tool/RectUtils.js";import{ERotateDirection as W,EDragStatus as x,ESortDirection as U,EDragTarget as v,TEXT_ATTRIBUTE_OFFSET as E,DEFAULT_TEXT_OFFSET as k}from"../../constant/annotation.js";import L from"../../constant/keyCode.js";import{EPolygonPattern as w,edgeAdsorptionScope as R,ELineTypes as O,EToolName as H}from"../../constant/tool.js";import C from"../../locales/index.js";import{EMessage as S}from"../../locales/constants.js";import K from"../../utils/ActionsHistory.js";import b from"../../utils/tool/AttributeUtils.js";import c from"../../utils/tool/AxisUtils.js";import B from"../../utils/tool/CanvasUtils.js";import f from"../../utils/tool/CommonToolUtils.js";import P from"../../utils/tool/DrawUtils.js";import u from"../../utils/tool/PolygonUtils.js";import A from"../../utils/tool/StyleUtils.js";import M from"../../utils/uuid.js";import{BasicToolOperation as $}from"./basicToolOperation.js";import V from"./textAttributeClass.js";import X from"./Selection.js";var Y=Object.defineProperty,q=Object.defineProperties,G=Object.getOwnPropertyDescriptors,Z=Object.getOwnPropertySymbols,J=Object.prototype.hasOwnProperty,Q=Object.prototype.propertyIsEnumerable,F=(I,t,e)=>t in I?Y(I,t,{enumerable:!0,configurable:!0,writable:!0,value:e}):I[t]=e,y=(I,t)=>{for(var e in t||(t={}))J.call(t,e)&&F(I,e,t[e]);if(Z)for(var e of Z(t))Q.call(t,e)&&F(I,e,t[e]);return I},m=(I,t)=>q(I,G(t));const z=164;class tt extends ${constructor(t){super(t);this.isAllowDouble=e=>{const{selectedID:i}=this,s=this.getHoverID(e);return!!(i&&i===s)},this.textChange=e=>{this.config.textConfigurable===!1||!this.selectedID||(this.setPolygonList(b.textChange(e,this.selectedID,this.polygonList)),this.emit("selectedChange"),this.render())},this.emitUpdatePolygonByDrag=()=>{if(this.dragInfo){const{originPolygon:e}=this.dragInfo;this.emit("updatePolygonByDrag",[{newPolygon:this.selectedPolygon,originPolygon:e}])}},this.config=f.jsonParser(t.config),this.drawingPointList=[],this.polygonList=[],this.hoverPointIndex=-1,this.hoverEdgeIndex=-1,this.drawingHistory=new K,this.isCtrl=!1,this.isAlt=!1,this.isCombined=!1,this.pattern=w.Normal,this.getCurrentSelectedData=this.getCurrentSelectedData.bind(this),this.updateSelectedTextAttribute=this.updateSelectedTextAttribute.bind(this),this.selection=new X(this)}get selectedIDs(){return this.selection.selectedIDs}get selectedID(){return this.selection.selectedID}eventBinding(){super.eventBinding(),this.container.removeEventListener("mouseup",this.onMouseUp),this.container.addEventListener("mouseup",this.dragMouseUp),this.dblClickListener.addEvent(this.onMouseUp,this.onLeftDblClick,this.onRightDblClick,this.isAllowDouble)}eventUnbinding(){super.eventUnbinding(),this.container.removeEventListener("mouseup",this.dragMouseUp)}destroy(){super.destroy(),this._textAttributeInstance&&this._textAttributeInstance.clearTextAttribute()}get selectedPolygon(){return u.getPolygonByID(this.visiblePolygonList,this.selectedID)}get selectedPolygons(){return u.getPolygonByIDs(this.polygonList,this.selectedIDs)}get hoverPolygon(){return this.visiblePolygonList.find(t=>t.id===this.hoverID&&t.id!==this.selectedID)}get enableDrag(){return Boolean(this.selectedID&&this.dragInfo)}get visiblePolygonList(){return this.polygonList}get polygonListUnderZoom(){return this.visiblePolygonList.map(t=>m(y({},t),{pointList:c.changePointListByZoom(t.pointList,this.zoom)}))}get selectedText(){var t;return(t=this.selectedPolygon)==null?void 0:t.textAttribute}get dataList(){return this.polygonList}setPattern(t,e=!1){var i;((i=this.drawingPointList)==null?void 0:i.length)>0&&e===!0||(this.pattern=t)}get currentShowList(){let t=[];const[e,i]=f.getRenderResultList(this.polygonList,f.getSourceID(this.basicResult),this.attributeLockList,this.selectedIDs);return t=e,this.isHidden&&(t=[]),i&&t.push(...i),t}get currentPageResult(){const[t]=f.getRenderResultList(this.polygonList,f.getSourceID(this.basicResult),[]);return t}setResultAndSelectedID(t,e){this.setPolygonList(t),e&&this.setSelectedID(e),this.render()}setResult(t){this.clearActiveStatus(),this.setPolygonList(t),this.render()}setPolygonDataByID(t,e){return this.polygonList.map(i=>i.id===e?y(y({},i),t):i)}rotatePolygon(t=1,e=W.Clockwise,i=this.selectedID){if(!i)return;const s=u.getPolygonByID(this.polygonList,i);if(!s)return;const o=u.updatePolygonByRotate(e,t,s==null?void 0:s.pointList);this.setPolygonList(this.setPolygonDataByID({pointList:o},i)),this.render()}addPointInDrawing(t){if(!this.imgInfo||this.forbidAddNewPolygonFuc&&this.forbidAddNewPolygonFuc(t))return;const{upperLimitPointNum:e,edgeAdsorption:i}=this.config;if(e&&this.drawingPointList.length>=e){this.emit("messageInfo",`${C.getMessagesByLocale(S.UpperLimitErrorNotice,this.lang)}${e}`);return}this.deleteSelectedID();const s=this.getCoordinateUnderZoom(t),o=c.changeDrawOutsideTarget(s,{x:0,y:0},this.imgInfo,this.config.drawOutsideTarget,this.basicResult,this.zoom);if(c.returnClosePointIndex(o,c.changePointListByZoom(this.drawingPointList,this.zoom))===0){this.addDrawingPointToPolygonList();return}const{dropFoot:n}=u.getClosestPoint(o,this.polygonListUnderZoom,this.config.lineType,R),r=c.changePointByZoom(n&&t.altKey===!1&&i?n:o,1/this.zoom);if(this.pattern===w.Rect&&this.drawingPointList.length===2){const h=D.getRectangleByRightAngle(r,this.drawingPointList);if(this.drawingPointList=h,this.config.drawOutsideTarget===!1&&this.imgInfo&&this.isPolygonOutSide(this.drawingPointList)){this.emit("messageInfo",`${C.getMessagesByLocale(S.ForbiddenCreationOutsideBoundary,this.lang)}`),this.drawingPointList=[];return}this.addDrawingPointToPolygonList(!0);return}this.drawingPointList.push(r),this.drawingPointList.length===1?this.drawingHistory.initRecord(this.drawingPointList):this.drawingHistory.pushHistory(this.drawingPointList)}clearResult(){this.setPolygonList([]),this.deleteSelectedID(),this.render()}clearPolygonDrag(){this.drawingPointList=[],this.dragInfo=void 0,this.dragInfo=void 0,this.dragStatus=x.Wait,this.hoverEdgeIndex=-1,this.hoverPointIndex=-1,this.hoverID=""}clearActiveStatus(){this.clearPolygonDrag(),this.deleteSelectedID()}clearDrawingStatus(){this.drawingPointList=[]}setPolygonList(t){const e=this.polygonList.length;this.polygonList=t,e!==t.length&&this.emit("updatePageNumber")}setSelectedID(t,e=!1){this.selection.setSelectedIDs(t,e),this.render(),this.emit("selectedChange")}setDefaultAttribute(t=""){const e=this.defaultAttribute;if(this.defaultAttribute=t,e!==t&&(this.changeStyle(t),this.emit("changeAttributeSidebar"),this.selectedPolygons.length>0&&(this.selectedPolygons.forEach(i=>{i.attribute=t}),this.history.pushHistory(this.polygonList),this.render()),this._textAttributeInstance)){if(this.attributeLockList.length>0&&!this.attributeLockList.includes(t)){this._textAttributeInstance.clearTextAttribute();return}this._textAttributeInstance.updateIcon(this.getTextIconSvg(t))}}setStyle(t){var e;super.setStyle(t),this._textAttributeInstance&&this.config.attributeConfigurable===!1&&((e=this._textAttributeInstance)==null||e.updateIcon(this.getTextIconSvg()))}setPolygonValidAndRender(t){if(!t)return;const e=this.polygonList.map(i=>{var s;if(i.id===t){const o=(s=i==null?void 0:i.valid)!=null?s:!0;return m(y({},i),{valid:!o})}return i});this.setPolygonList(e),this.history.pushHistory(this.polygonList),this.render(),this.emit("updateResult")}addDrawingPointToPolygonList(t){let{lowerLimitPointNum:e=3,minArea:i=1}=this.config;e<3&&(e=3),i<1&&(i=1);let s;if(this.drawingPointList.length<e){this.drawingPointList=[],this.editPolygonID="";return}if(u.calcPolygonSize(this.drawingPointList||[])<i){this.emit("messageInfo",`${C.getMessagesByLocale(S.MinAreaLimitErrorNotice,this.lang)}${i}`);return}const o=f.getSourceID(this.basicResult),l=[...this.polygonList];if(this.editPolygonID){const n=l.find(r=>r.id===this.editPolygonID);if(!n)return;n.pointList=this.drawingPointList,this.editPolygonID=""}else{const n=M(8,62);let r={id:n,sourceID:o,valid:!this.isCtrl,textAttribute:"",pointList:this.drawingPointList,attribute:this.defaultAttribute,order:f.getMaxOrder(l.filter(h=>f.isSameSourceID(h.sourceID,o)))+1};if(this.config.textConfigurable){let h="";h=b.getTextAttribute(this.polygonList.filter(a=>f.isSameSourceID(a.sourceID,o)),this.config.textCheckType),r=m(y({},r),{textAttribute:h})}this.pattern===w.Rect&&t===!0&&(r=m(y({},r),{isRect:!0})),l.push(r),s=r,this.setSelectedIdAfterAddingDrawing(n)}this.setPolygonList(l),this.isCtrl=!1,this.drawingPointList=[],this.history.pushHistory(l),s&&this.emit("polygonCreated",s,this.zoom,this.currentPos)}setSelectedIdAfterAddingDrawing(t){this.drawingPointList.length!==0&&(this.config.textConfigurable?this.setSelectedID(t):this.deleteSelectedID())}getHoverID(t){var e;const i=this.getCoordinateUnderZoom(t),s=this.currentShowList.map(o=>m(y({},o),{pointList:c.changePointListByZoom(o.pointList,this.zoom)}));return u.getHoverPolygonID(i,s,10,(e=this.config)==null?void 0:e.lineType)}getHoverEdgeIndex(t){var e;if(!this.selectedID)return-1;const i=this.selectedPolygon;if(!i)return-1;const s=this.getCoordinateUnderZoom(t),o=c.changePointListByZoom(i.pointList,this.zoom);return u.getHoverEdgeIndex(s,o,(e=this.config)==null?void 0:e.lineType)}getHoverPointIndex(t){if(!this.selectedID)return-1;const e=this.selectedPolygon;if(!e)return-1;const i=this.getCoordinateUnderZoom(t),s=c.changePointListByZoom(e.pointList,this.zoom);return c.returnClosePointIndex(i,s)}deletePolygons(t){var e;if(!t||t.length===0)return;const i=this.polygonList.filter(s=>this.selection.isIdSelected(s.id));this.emit("deletedObject",{deletedObject:i[0],id:i[0].id}),this.setPolygonList(this.polygonList.filter(s=>!this.selection.isIdSelected(s.id))),this.history.pushHistory(this.polygonList),(e=this._textAttributeInstance)==null||e.clearTextAttribute(),this.emit("selectedChange"),this.render()}deletePolygonPoint(t){if(!this.selectedID)return;const{selectedPolygon:e}=this;if(!e)return;let{lowerLimitPointNum:i}=this.config;if(i<3&&(i=3),e.pointList.length<=i){this.emit("messageInfo",`${C.getMessagesByLocale(S.LowerLimitErrorNotice,this.lang)}${i}`);return}e==null||e.pointList.splice(t,1),this.history.pushHistory(this.polygonList),this.render()}spaceKeydown(){var t,e,i;if(this.selectedID){if(((t=this.selectedPolygon)==null?void 0:t.isRect)===!0){this.emit("messageInfo",`${C.getMessagesByLocale(S.UnableToReannotation,this.lang)}`);return}this.editPolygonID=this.selectedID,this.drawingPointList=(i=(e=this.selectedPolygon)==null?void 0:e.pointList)!=null?i:[],this.drawingHistory.empty(),this.drawingHistory.initRecord(this.drawingPointList),this.hoverID="",this.deleteSelectedID(),this.render()}}onTabKeyDown(t){t.preventDefault();let e=U.ascend;t.shiftKey&&(e=U.descend),this.switchToNextPolygon(e)}switchToNextPolygon(t){if(this.drawingPointList.length>0)return;const[e,i]=f.getRenderResultList(this.polygonList,f.getSourceID(this.basicResult),this.attributeLockList,this.selectedIDs);let s=[...e];i&&(s=[...s,...i]);const o=B.getViewPort(this.canvas,this.currentPos,this.zoom),l=s.map(r=>{var h,a,g,d;return m(y({},r),{x:(a=(h=r.pointList[0])==null?void 0:h.x)!=null?a:0,y:(d=(g=r.pointList[0])==null?void 0:g.y)!=null?d:0})}).filter(r=>B.inViewPort({x:r.x,y:r.y},o)),n=f.getNextSelectedRectID(l,t,this.selectedID);if(n){this.setSelectedID(n.id);const{selectedPolygon:r}=this;r&&this.setDefaultAttribute(r.attribute)}this.render()}onKeyDown(t){if(!f.hotkeyFilter(t)||super.onKeyDown(t)===!1)return;const{keyCode:e}=t;switch(e){case L.Space:this.spaceKeydown();break;case L.Esc:this.drawingPointList=[],this.editPolygonID="";break;case L.F:this.selectedID&&this.setPolygonValidAndRender(this.selectedID);break;case L.Z:if(t.altKey){this.onCombinedExecute();return}this.setIsHidden(!this.isHidden),this.render();break;case L.Delete:this.deletePolygons(this.selectedIDs),this.render();break;case L.Ctrl:this.isCtrl=!0;break;case L.Alt:this.isAlt===!1&&(t.preventDefault(),this.isAlt=!0,this.render());break;case L.Tab:{this.onTabKeyDown(t);break}case L.X:t.altKey&&this.segment();break;default:{if(this.config.attributeConfigurable){const i=b.getAttributeByKeycode(e,this.config.attributeList);i!==void 0&&this.setDefaultAttribute(i)}break}}}onKeyUp(t){if(super.onKeyUp(t),!this.selection.triggerKeyboardEvent(t,this.setPolygonList.bind(this)))switch(t.keyCode){case L.Ctrl:this.isCtrl=!1;break;case L.Alt:{const e=this.isAlt;this.isAlt=!1,e===!0&&this.render();break}}}rightMouseUp(t){if(this.drawingPointList.length>0){this.addDrawingPointToPolygonList();return}this.setSelectedID(this.hoverID,t.ctrlKey);const{selectedPolygon:e}=this;e&&this.setDefaultAttribute(e.attribute)}onLeftDblClick(t){if(this.hoverEdgeIndex>-1){const e=this.getCoordinateUnderZoom(t),{selectedPolygon:i}=this;if(!i)return;if(i.isRect===!0){this.emit("messageInfo",`${C.getMessagesByLocale(S.ForbidAddNewPoint,this.lang)}`),this.clearPolygonDrag();return}const{dropFoot:s}=u.getClosestPoint(e,this.polygonListUnderZoom,this.config.lineType,R);if(!s)return;const{upperLimitPointNum:o}=this.config;if(o&&i.pointList.length>=o){this.emit("messageInfo",`${C.getMessagesByLocale(S.UpperLimitErrorNotice,this.lang)}${o}`),this.clearPolygonDrag();return}i==null||i.pointList.splice(this.hoverEdgeIndex+1,0,c.changePointByZoom(s,1/this.zoom)),this.setPolygonDataByID(i,this.selectedID),this.history.pushHistory(this.polygonList),this.hoverPointIndex=-1,this.hoverEdgeIndex=-1,this.render()}this.dragInfo=void 0}onRightDblClick(t){this.dragInfo=void 0,this.clearImgDrag();const e=this.getHoverID(t),i=this.getHoverPointIndex(t);if(this.hoverPointIndex>-1&&this.hoverPointIndex===i){this.deletePolygonPoint(i),this.dragInfo=void 0,this.hoverPointIndex=-1,this.render();return}this.hoverID===this.selectedID&&this.deletePolygons([e]),this.render()}onMouseDown(t){if(!(super.onMouseDown(t)||this.forbidMouseOperation||t.ctrlKey===!0))return this.dragMouseDown(t),!0}dragMouseDown(t){if(t.button!==0)return;const e=this.getHoverID(t);if(!this.selection.isIdSelected(e))return;let i=[0],s=v.Plane;const o=this.selectedPolygons[0].pointList,l=this.getCoordinateUnderZoom(t);if(this.selectedPolygons.length===1){this.dragStatus=x.Start;const n=this.getHoverPointIndex(t),r=this.getHoverEdgeIndex(t);n>-1?(s=v.Point,i=[n]):r>-1&&this.hoverEdgeIndex>-1&&(s=v.Line,i=[r,(r+1)%o.length])}this.dragInfo={dragStartCoord:l,dragTarget:s,initPointList:o,changePointIndex:i,originPolygon:this.selectedPolygon,selectedPolygons:this.selectedPolygons,dragPrevCoord:l,originPolygonList:this.polygonList}}segment(){var t;if(!this.selectedID||((t=this.config)==null?void 0:t.lineType)!==O.Line)return;const e=u.getPolygonPointList(this.selectedID,this.currentShowList),i=this.currentShowList.filter(l=>l.id!==this.selectedID);if(i.length===0||e.length===0)return;const s=u.getWrapPolygonIndex(e,i);let o=[...this.polygonList];if(s===-1){const l=u.segmentPolygonByPolygon(e,i);if(!l)return;const n=l.shift();if(!n)return;let r="",h=!0;const a=f.getSourceID(this.basicResult);let g="";o=this.polygonList.map(d=>{var p,T;return d.id===this.selectedID?(r=d.attribute,h=(p=d==null?void 0:d.valid)!=null?p:!0,g=(T=d==null?void 0:d.textAttribute)!=null?T:"",m(y({},d),{pointList:n})):d}),l.length>0&&l.forEach((d,p)=>{o.push({sourceID:a,id:M(8,62),pointList:d,valid:h,order:f.getMaxOrder(this.currentShowList)+1+p,attribute:r,textAttribute:g})})}else o[s].pointList=u.clipPolygonFromWrapPolygon(e,o[s].pointList),o=o.filter(l=>l.id!==this.selectedID);this.setPolygonList(o),this.history.pushHistory(o),this.render()}onCombinedExecute(){if(!this.selectedID){this.emit("messageInfo",_.t("PolygonsToBeCombinedNeedToBeSelected"));return}this.isCombined=!this.isCombined}combine(t){var e;const i=this.getHoverID(t);if(!i||!this.selectedID||this.selectedID===i)return;if(((e=this.config)==null?void 0:e.lineType)!==O.Line){this.emit("messageInfo",_.t("CurveModeDoesNotSupportCutting"));return}const s=this.polygonList.find(h=>h.id===this.selectedID),o=this.currentShowList.find(h=>h.id===i);if(!o||!s)return;const l=u.combinePolygonWithPolygon(s,o);if(!l)return;const{newPolygon:n,unionList:r}=l;if(r.length===1&&n){const h=this.polygonList.filter(a=>!r.includes(a.id)).map(a=>a.id===this.selectedID?n:a);this.setPolygonList(h),this.history.pushHistory(h),this.render(),this.emit("messageInfo",_.t("CombineSuccess"))}else this.emit("messageInfo",_.t("CombiningFailedNotify"));this.isCombined=!1}isPolygonOutSide(t){if(this.dependToolName&&this.basicCanvas&&this.basicResult){let n=!1;switch(this.dependToolName){case H.Rect:{n=t.filter(r=>!N.isInRect(r,this.basicResult)).length>0;break}case H.Polygon:{n=u.isPointListOutSidePolygon(t,this.basicResult.pointList,this.config.lineType);break}}return n}if(!this.imgInfo)return!1;const{left:e,top:i,right:s,bottom:o}=D.calcViewportBoundaries(c.changePointListByZoom(t,this.zoom)),l=1e-5;return e<0||i<0||s>this.imgInfo.width+l||o>this.imgInfo.height+l}onDragMove(t){const e=this.polygonList.map(i=>{if(this.selectedIDs.includes(i.id)){const s=this.dragPolygon(t,i);if(!s)return i;const o=m(y({},i),{pointList:s});return i.isRect===!0&&this.pattern===w.Normal&&Object.assign(o,{isRect:!1}),o}return i});this.dragInfo.dragPrevCoord=this.getCoordinateUnderZoom(t),this.setPolygonList(e),this.render()}getDragOffset(t,e){var i;const s=this.getCoordinateUnderZoom(t),{dragTarget:o,dragPrevCoord:l,changePointIndex:n,initPointList:r,dragStartCoord:h}=this.dragInfo;if(this.pattern===w.Rect&&(e==null?void 0:e.isRect)===!0&&n&&[v.Line].includes(o)){const a=D.getArrayIndex(n[0]-2,4),g=D.getArrayIndex(n[0]-1,4),d=[r[a],r[g]],p=D.getRectPerpendicularOffset(h,s,d);return{x:p.x/this.zoom,y:p.y/this.zoom}}return((i=this.dragInfo)==null?void 0:i.dragTarget)===v.Plane?{x:(s.x-l.x)/this.zoom,y:(s.y-l.y)/this.zoom}:{x:(s.x-h.x)/this.zoom,y:(s.y-h.y)/this.zoom}}dragPolygon(t,e){let i=j.cloneDeep(e==null?void 0:e.pointList);if(!i||!this.dragInfo)return;const{initPointList:s,dragTarget:o,changePointIndex:l}=this.dragInfo,n=this.getDragOffset(t,e);switch(this.dragStatus=x.Move,o){case v.Plane:i=i.map(r=>m(y({},r),{x:r.x+n.x,y:r.y+n.y}));break;case v.Point:case v.Line:i=i.map((r,h)=>l&&l.includes(h)?m(y({},r),{x:s[h].x+n.x,y:s[h].y+n.y}):r);break}if(this.pattern===w.Rect&&(e==null?void 0:e.isRect)===!0&&o===v.Point&&l&&(i=D.getPointListFromPointOffset(s,l[0],n)),!(this.config.drawOutsideTarget===!1&&this.imgInfo&&this.isPolygonOutSide(i)))return i}onMouseMove(t){if(super.onMouseMove(t)||this.forbidMouseOperation||!this.imgInfo)return;let e=-1,i=-1;const{selectedID:s}=this;if(s&&(this.hoverEdgeIndex=-1,this.hoverPointIndex=-1,e=this.getHoverPointIndex(t),e>-1?this.hoverPointIndex=e:(i=this.getHoverEdgeIndex(t),this.hoverEdgeIndex=i)),this.drawingPointList.length>0)return;const o=this.getHoverID(t);this.hoverID!==o&&(this.hoverID=o,this.render()),this.selectedIDs.length>0&&this.dragInfo&&this.onDragMove(t)}leftMouseUpdateValid(t){const e=this.getHoverID(t);return this.drawingPointList.length===0&&t.ctrlKey===!0&&e?(this.setPolygonValidAndRender(e),!0):!1}leftMouseUp(t){this.leftMouseUpdateValid(t)||this.addPointInDrawing(t)}onMouseUp(t){if(this.isCombined){switch(t.button){case 0:this.combine(t);break;case 2:this.isCombined=!1;break;default:return}return}if(!(super.onMouseUp(t)||this.forbidMouseOperation||!this.imgInfo)){if(this.dragInfo&&this.dragStatus===x.Move){this.emit("updateResult"),this.emitUpdatePolygonByDrag(),this.dragInfo=void 0,this.dragStatus=x.Wait,this.history.pushHistory(this.polygonList);return}switch(t.button){case 0:{this.leftMouseUp(t);break}case 2:{this.rightMouseUp(t);break}}this.render()}}dragMouseUp(){this.dragStatus===x.Start&&(this.dragInfo=void 0,this.dragStatus=x.Wait)}exportData(){const{polygonList:t}=this;return[t,this.basicImgInfo]}getTextIconSvg(t=""){return b.getTextIconSvg(t,this.config.attributeList,this.config.attributeConfigurable,this.baseIcon)}getCurrentSelectedData(){const{selectedPolygon:t}=this;if(!t)return;const e=this.getColor(t.attribute),i=t.valid?e==null?void 0:e.valid.stroke:e==null?void 0:e.invalid.stroke;return{width:z,textAttribute:t.textAttribute,color:i}}updateSelectedTextAttribute(t){if(this._textAttributeInstance&&t&&this.selectedID){let e=t;b.textAttributeValidate(this.config.textCheckType,"",e)===!1&&(this.emit("messageError",b.getErrorNotice(this.config.textCheckType,this.lang)),e=""),this.setPolygonList(b.textChange(e,this.selectedID,this.polygonList)),this.emit("updateTextAttribute"),this.render()}}renderTextAttribute(){var t;const{selectedPolygon:e}=this;if(!this.ctx||this.config.textConfigurable===!1||!e)return;const{pointList:i,attribute:s,valid:o,textAttribute:l}=e,{x:n,y:r}=i[i.length-1],h=z,a=c.getOffsetCoordinate({x:n,y:r},this.currentPos,this.zoom),g=this.getColor(s),d=o?g==null?void 0:g.valid.stroke:g==null?void 0:g.invalid.stroke;this._textAttributeInstance||(this._textAttributeInstance=new V({width:h,container:this.container,icon:this.getTextIconSvg(s),color:d,getCurrentSelectedData:this.getCurrentSelectedData,updateSelectedTextAttribute:this.updateSelectedTextAttribute})),this._textAttributeInstance&&!((t=this._textAttributeInstance)==null?void 0:t.isExit)&&this._textAttributeInstance.appendToContainer(),this._textAttributeInstance.update(`${l}`,{left:a.x,top:a.y,color:d,width:h})}renderStaticPolygon(){var t;this.isHidden===!1&&((t=this.visiblePolygonList)==null||t.forEach(e=>{var i,s,o,l,n;if([this.selectedID,this.editPolygonID].includes(e.id))return;const{textAttribute:r,attribute:h}=e,a=this.getColor(h),g=A.getStrokeAndFill(a,e.valid),d=c.changePointListByZoom(e.pointList||[],this.zoom,this.currentPos);P.drawPolygonWithFillAndLine(this.canvas,d,{fillColor:g.fill,strokeColor:g.stroke,pointColor:"white",thickness:(s=(i=this.style)==null?void 0:i.width)!=null?s:2,lineCap:"round",isClose:!0,lineType:(o=this.config)==null?void 0:o.lineType});let p=`${(l=b.getAttributeShowText(h,this.config.attributeList))!=null?l:""}`;((n=this.config)==null?void 0:n.isShowOrder)&&(e==null?void 0:e.order)>0&&(p=`${e.order} ${p}`),P.drawText(this.canvas,d[0],p,y({color:g.stroke},k));const T=d[d.length-1];P.drawText(this.canvas,{x:T.x+E.x,y:T.y+E.y},r,y({color:g.stroke},k))}))}renderSelectedPolygons(){this.selectedPolygons.forEach(t=>{this.renderSelectedPolygon(t)})}renderSelectedPolygon(t){var e,i,s;if(t){const o=this.getColor(t.attribute),l=A.getStrokeAndFill(o,t.valid,{isSelected:!0});P.drawSelectedPolygonWithFillAndLine(this.canvas,c.changePointListByZoom(t.pointList,this.zoom,this.currentPos),{fillColor:l.fill,strokeColor:l.stroke,pointColor:"white",thickness:2,lineCap:"round",isClose:!0,lineType:(e=this.config)==null?void 0:e.lineType});let n=`${(i=b.getAttributeShowText(t.attribute,this.config.attributeList))!=null?i:""}`;((s=this.config)==null?void 0:s.isShowOrder)&&(t==null?void 0:t.order)>0&&(n=`${t.order} ${n}`),P.drawText(this.canvas,c.changePointByZoom(t.pointList[0],this.zoom,this.currentPos),n,y({color:l.stroke},k)),this.renderTextAttribute()}}renderHoverPolygon(){var t;if(this.hoverID&&this.hoverID!==this.editPolygonID){const{hoverPolygon:e}=this;if(e){let i="";const s=this.getColor(e.attribute);e.valid?i=s.validHover.fill:i=A.getStrokeAndFill(s,!1,{isHover:!0}).fill,P.drawPolygonWithFill(this.canvas,c.changePointListByZoom(e.pointList,this.zoom,this.currentPos),{color:i,lineType:(t=this.config)==null?void 0:t.lineType})}}}renderPolygon(){var t,e,i,s;this.renderStaticPolygon(),this.renderHoverPolygon(),this.renderSelectedPolygons();const o=this.getColor(this.defaultAttribute),l=A.getStrokeAndFill(o,!this.isCtrl);if(((t=this.drawingPointList)==null?void 0:t.length)>0){let n=[...this.drawingPointList],r=c.getOriginCoordinateWithOffsetCoordinate(this.coord,this.zoom,this.currentPos);if(this.pattern===w.Rect&&n.length===2)n=D.getRectangleByRightAngle(r,n);else{if(((e=this.config)==null?void 0:e.edgeAdsorption)&&this.isAlt===!1){const{dropFoot:h}=u.getClosestPoint(r,this.polygonList,(i=this.config)==null?void 0:i.lineType,R/this.zoom);h&&(r=h)}n.push(r)}P.drawSelectedPolygonWithFillAndLine(this.canvas,c.changePointListByZoom(n,this.zoom,this.currentPos),{fillColor:l.fill,strokeColor:l.stroke,pointColor:"white",thickness:2,lineCap:"round",isClose:!1,lineType:this.config.lineType})}if(this.hoverPointIndex>-1&&this.selectedID){const n=this.selectedPolygon;if(!n)return;const r=A.getStrokeAndFill(o,n.valid,{isSelected:!0}),h=n==null?void 0:n.pointList[this.hoverPointIndex];if(h){const{x:a,y:g}=c.changePointByZoom(h,this.zoom,this.currentPos);P.drawCircleWithFill(this.canvas,{x:a,y:g},5,{color:r.fill})}}if(this.hoverEdgeIndex>-1&&this.selectedID){const n=this.selectedPolygon;if(!n)return;const r=A.getStrokeAndFill(o,n.valid,{isSelected:!0});P.drawLineWithPointList(this.canvas,c.changePointListByZoom(n.pointList,this.zoom,this.currentPos),{color:r.stroke,thickness:10,hoverEdgeIndex:this.hoverEdgeIndex,lineType:(s=this.config)==null?void 0:s.lineType})}}render(){!this.ctx||(super.render(),this.renderPolygon(),this.renderCursorLine(this.getLineColor(this.defaultAttribute)))}renderCursorLine(t){if(super.renderCursorLine(t),this.isCombined){const{x:e,y:i}=this.coord,s=10,o=186,l=32;P.drawRectWithFill(this.canvas,{x:e+s,y:i-s*4-1,width:o,height:l},{color:"black"}),P.drawText(this.canvas,{x:e,y:i},_.t("ClickAnotherPolygon"),{textAlign:"center",color:"white",offsetX:o/2+s,offsetY:-(l/2+s/2)}),P.drawRect(this.canvas,{x:e-s,y:i-s,width:s*2,height:s*2},{lineDash:[6],color:"white"})}}undo(){if(this.drawingPointList.length>0){const e=this.drawingHistory.undo();if(!e)return;this.drawingPointList=e,this.render();return}const t=this.history.undo();t&&(t.length!==this.polygonList.length&&this.deleteSelectedID(),this.setPolygonList(t),this.render())}redo(){if(this.drawingPointList.length>0){const e=this.drawingHistory.redo();if(!e)return;this.drawingPointList=e,this.render();return}const t=this.history.redo();t&&(t.length!==this.polygonList.length&&this.deleteSelectedID(),this.setPolygonList(t),this.render())}deleteSelectedID(){this.setSelectedID("")}}export{tt as default};
|
|
1
|
+
import j from"lodash";import{i18n as _}from"@labelbee/lb-utils";import D from"../../utils/MathUtils.js";import N from"../../utils/tool/RectUtils.js";import{ERotateDirection as W,EDragStatus as x,ESortDirection as U,EDragTarget as v,TEXT_ATTRIBUTE_OFFSET as E,DEFAULT_TEXT_OFFSET as k}from"../../constant/annotation.js";import L from"../../constant/keyCode.js";import{EPolygonPattern as w,edgeAdsorptionScope as R,ELineTypes as O,EToolName as H}from"../../constant/tool.js";import C from"../../locales/index.js";import{EMessage as S}from"../../locales/constants.js";import K from"../../utils/ActionsHistory.js";import b from"../../utils/tool/AttributeUtils.js";import a from"../../utils/tool/AxisUtils.js";import B from"../../utils/tool/CanvasUtils.js";import f from"../../utils/tool/CommonToolUtils.js";import P from"../../utils/tool/DrawUtils.js";import u from"../../utils/tool/PolygonUtils.js";import A from"../../utils/tool/StyleUtils.js";import M from"../../utils/uuid.js";import{BasicToolOperation as $}from"./basicToolOperation.js";import V from"./textAttributeClass.js";import X from"./Selection.js";var Y=Object.defineProperty,q=Object.defineProperties,G=Object.getOwnPropertyDescriptors,Z=Object.getOwnPropertySymbols,J=Object.prototype.hasOwnProperty,Q=Object.prototype.propertyIsEnumerable,F=(I,t,e)=>t in I?Y(I,t,{enumerable:!0,configurable:!0,writable:!0,value:e}):I[t]=e,y=(I,t)=>{for(var e in t||(t={}))J.call(t,e)&&F(I,e,t[e]);if(Z)for(var e of Z(t))Q.call(t,e)&&F(I,e,t[e]);return I},m=(I,t)=>q(I,G(t));const z=164;class tt extends ${constructor(t){super(t);this.isAllowDouble=e=>{const{selectedID:i}=this,s=this.getHoverID(e);return!!(i&&i===s)},this.textChange=e=>{this.config.textConfigurable===!1||!this.selectedID||(this.setPolygonList(b.textChange(e,this.selectedID,this.polygonList)),this.emit("selectedChange"),this.render())},this.emitUpdatePolygonByDrag=()=>{if(this.dragInfo){const{originPolygon:e}=this.dragInfo;this.emit("updatePolygonByDrag",[{newPolygon:this.selectedPolygon,originPolygon:e}])}},this.config=f.jsonParser(t.config),this.drawingPointList=[],this.polygonList=[],this.hoverPointIndex=-1,this.hoverEdgeIndex=-1,this.drawingHistory=new K,this.isCtrl=!1,this.isAlt=!1,this.isCombined=!1,this.pattern=w.Normal,this.getCurrentSelectedData=this.getCurrentSelectedData.bind(this),this.updateSelectedTextAttribute=this.updateSelectedTextAttribute.bind(this),this.selection=new X(this)}get selectedIDs(){return this.selection.selectedIDs}get selectedID(){return this.selection.selectedID}get minArea(){return this.config.minArea||0}eventBinding(){super.eventBinding(),this.container.removeEventListener("mouseup",this.onMouseUp),this.container.addEventListener("mouseup",this.dragMouseUp),this.dblClickListener.addEvent(this.onMouseUp,this.onLeftDblClick,this.onRightDblClick,this.isAllowDouble)}eventUnbinding(){super.eventUnbinding(),this.container.removeEventListener("mouseup",this.dragMouseUp)}destroy(){super.destroy(),this._textAttributeInstance&&this._textAttributeInstance.clearTextAttribute()}get selectedPolygon(){return u.getPolygonByID(this.visiblePolygonList,this.selectedID)}get selectedPolygons(){return u.getPolygonByIDs(this.polygonList,this.selectedIDs)}get hoverPolygon(){return this.visiblePolygonList.find(t=>t.id===this.hoverID&&t.id!==this.selectedID)}get enableDrag(){return Boolean(this.selectedID&&this.dragInfo)}get visiblePolygonList(){return this.polygonList}get polygonListUnderZoom(){return this.visiblePolygonList.map(t=>m(y({},t),{pointList:a.changePointListByZoom(t.pointList,this.zoom)}))}get selectedText(){var t;return(t=this.selectedPolygon)==null?void 0:t.textAttribute}get dataList(){return this.polygonList}setPattern(t,e=!1){var i;((i=this.drawingPointList)==null?void 0:i.length)>0&&e===!0||(this.pattern=t)}get currentShowList(){let t=[];const[e,i]=f.getRenderResultList(this.polygonList,f.getSourceID(this.basicResult),this.attributeLockList,this.selectedIDs);return t=e,this.isHidden&&(t=[]),i&&t.push(...i),t}get currentPageResult(){const[t]=f.getRenderResultList(this.polygonList,f.getSourceID(this.basicResult),[]);return t}setResultAndSelectedID(t,e){this.setPolygonList(t),e&&this.setSelectedID(e),this.render()}setResult(t){this.clearActiveStatus(),this.setPolygonList(t),this.render()}setPolygonDataByID(t,e){return this.polygonList.map(i=>i.id===e?y(y({},i),t):i)}rotatePolygon(t=1,e=W.Clockwise,i=this.selectedID){if(!i)return;const s=u.getPolygonByID(this.polygonList,i);if(!s)return;const o=u.updatePolygonByRotate(e,t,s==null?void 0:s.pointList);this.setPolygonList(this.setPolygonDataByID({pointList:o},i)),this.render()}addPointInDrawing(t){if(!this.imgInfo||this.forbidAddNewPolygonFuc&&this.forbidAddNewPolygonFuc(t))return;const{upperLimitPointNum:e,edgeAdsorption:i}=this.config;if(e&&this.drawingPointList.length>=e){this.emit("messageInfo",`${C.getMessagesByLocale(S.UpperLimitErrorNotice,this.lang)}${e}`);return}this.deleteSelectedID();const s=this.getCoordinateUnderZoom(t),o=a.changeDrawOutsideTarget(s,{x:0,y:0},this.imgInfo,this.config.drawOutsideTarget,this.basicResult,this.zoom);if(a.returnClosePointIndex(o,a.changePointListByZoom(this.drawingPointList,this.zoom))===0){this.addDrawingPointToPolygonList();return}const{dropFoot:n}=u.getClosestPoint(o,this.polygonListUnderZoom,this.config.lineType,R),r=a.changePointByZoom(n&&t.altKey===!1&&i?n:o,1/this.zoom);if(this.pattern===w.Rect&&this.drawingPointList.length===2){const h=D.getRectangleByRightAngle(r,this.drawingPointList);if(this.drawingPointList=h,this.config.drawOutsideTarget===!1&&this.imgInfo&&this.isPolygonOutSide(this.drawingPointList)){this.emit("messageInfo",`${C.getMessagesByLocale(S.ForbiddenCreationOutsideBoundary,this.lang)}`),this.drawingPointList=[];return}this.addDrawingPointToPolygonList(!0);return}this.drawingPointList.push(r),this.drawingPointList.length===1?this.drawingHistory.initRecord(this.drawingPointList):this.drawingHistory.pushHistory(this.drawingPointList)}clearResult(){this.setPolygonList([]),this.deleteSelectedID(),this.render()}clearPolygonDrag(){this.drawingPointList=[],this.dragInfo=void 0,this.dragInfo=void 0,this.dragStatus=x.Wait,this.hoverEdgeIndex=-1,this.hoverPointIndex=-1,this.hoverID=""}clearActiveStatus(){this.clearPolygonDrag(),this.deleteSelectedID()}clearDrawingStatus(){this.drawingPointList=[]}setPolygonList(t){const e=this.polygonList.length;this.polygonList=t,e!==t.length&&this.emit("updatePageNumber")}setSelectedID(t,e=!1){this.selection.setSelectedIDs(t,e),this.render(),this.emit("selectedChange")}setDefaultAttribute(t=""){const e=this.defaultAttribute;if(this.defaultAttribute=t,e!==t&&(this.changeStyle(t),this.emit("changeAttributeSidebar"),this.selectedPolygons.length>0&&(this.selectedPolygons.forEach(i=>{i.attribute=t}),this.history.pushHistory(this.polygonList),this.render()),this._textAttributeInstance)){if(this.attributeLockList.length>0&&!this.attributeLockList.includes(t)){this._textAttributeInstance.clearTextAttribute();return}this._textAttributeInstance.updateIcon(this.getTextIconSvg(t))}}setStyle(t){var e;super.setStyle(t),this._textAttributeInstance&&this.config.attributeConfigurable===!1&&((e=this._textAttributeInstance)==null||e.updateIcon(this.getTextIconSvg()))}setPolygonValidAndRender(t){if(!t)return;const e=this.polygonList.map(i=>{var s;if(i.id===t){const o=(s=i==null?void 0:i.valid)!=null?s:!0;return m(y({},i),{valid:!o})}return i});this.setPolygonList(e),this.history.pushHistory(this.polygonList),this.render(),this.emit("updateResult")}addDrawingPointToPolygonList(t){let{lowerLimitPointNum:e=3}=this.config;e<3&&(e=3);let i;if(this.drawingPointList.length<e){this.drawingPointList=[],this.editPolygonID="";return}if(u.calcPolygonSize(this.drawingPointList||[])<this.minArea){this.emit("messageInfo",`${C.getMessagesByLocale(S.MinAreaLimitErrorNotice,this.lang)}${this.minArea}`);return}const s=f.getSourceID(this.basicResult),o=[...this.polygonList];if(this.editPolygonID){const l=o.find(n=>n.id===this.editPolygonID);if(!l)return;l.pointList=this.drawingPointList,this.editPolygonID=""}else{const l=M(8,62);let n={id:l,sourceID:s,valid:!this.isCtrl,textAttribute:"",pointList:this.drawingPointList,attribute:this.defaultAttribute,order:f.getMaxOrder(o.filter(r=>f.isSameSourceID(r.sourceID,s)))+1};if(this.config.textConfigurable){let r="";r=b.getTextAttribute(this.polygonList.filter(h=>f.isSameSourceID(h.sourceID,s)),this.config.textCheckType),n=m(y({},n),{textAttribute:r})}this.pattern===w.Rect&&t===!0&&(n=m(y({},n),{isRect:!0})),o.push(n),i=n,this.setSelectedIdAfterAddingDrawing(l)}this.setPolygonList(o),this.isCtrl=!1,this.drawingPointList=[],this.history.pushHistory(o),i&&this.emit("polygonCreated",i,this.zoom,this.currentPos)}setSelectedIdAfterAddingDrawing(t){this.drawingPointList.length!==0&&(this.config.textConfigurable?this.setSelectedID(t):this.deleteSelectedID())}getHoverID(t){var e;const i=this.getCoordinateUnderZoom(t),s=this.currentShowList.map(o=>m(y({},o),{pointList:a.changePointListByZoom(o.pointList,this.zoom)}));return u.getHoverPolygonID(i,s,10,(e=this.config)==null?void 0:e.lineType)}getHoverEdgeIndex(t){var e;if(!this.selectedID)return-1;const i=this.selectedPolygon;if(!i)return-1;const s=this.getCoordinateUnderZoom(t),o=a.changePointListByZoom(i.pointList,this.zoom);return u.getHoverEdgeIndex(s,o,(e=this.config)==null?void 0:e.lineType)}getHoverPointIndex(t){if(!this.selectedID)return-1;const e=this.selectedPolygon;if(!e)return-1;const i=this.getCoordinateUnderZoom(t),s=a.changePointListByZoom(e.pointList,this.zoom);return a.returnClosePointIndex(i,s)}deletePolygons(t){var e;if(!t||t.length===0)return;const i=this.polygonList.filter(s=>this.selection.isIdSelected(s.id));this.emit("deletedObject",{deletedObject:i[0],id:i[0].id}),this.setPolygonList(this.polygonList.filter(s=>!this.selection.isIdSelected(s.id))),this.history.pushHistory(this.polygonList),(e=this._textAttributeInstance)==null||e.clearTextAttribute(),this.emit("selectedChange"),this.render()}deletePolygonPoint(t){if(!this.selectedID)return;const{selectedPolygon:e}=this;if(!e)return;let{lowerLimitPointNum:i}=this.config;if(i<3&&(i=3),e.pointList.length<=i){this.emit("messageInfo",`${C.getMessagesByLocale(S.LowerLimitErrorNotice,this.lang)}${i}`);return}e==null||e.pointList.splice(t,1),this.history.pushHistory(this.polygonList),this.render()}spaceKeydown(){var t,e,i;if(this.selectedID){if(((t=this.selectedPolygon)==null?void 0:t.isRect)===!0){this.emit("messageInfo",`${C.getMessagesByLocale(S.UnableToReannotation,this.lang)}`);return}this.editPolygonID=this.selectedID,this.drawingPointList=(i=(e=this.selectedPolygon)==null?void 0:e.pointList)!=null?i:[],this.drawingHistory.empty(),this.drawingHistory.initRecord(this.drawingPointList),this.hoverID="",this.deleteSelectedID(),this.render()}}onTabKeyDown(t){t.preventDefault();let e=U.ascend;t.shiftKey&&(e=U.descend),this.switchToNextPolygon(e)}switchToNextPolygon(t){if(this.drawingPointList.length>0)return;const[e,i]=f.getRenderResultList(this.polygonList,f.getSourceID(this.basicResult),this.attributeLockList,this.selectedIDs);let s=[...e];i&&(s=[...s,...i]);const o=B.getViewPort(this.canvas,this.currentPos,this.zoom),l=s.map(r=>{var h,c,g,d;return m(y({},r),{x:(c=(h=r.pointList[0])==null?void 0:h.x)!=null?c:0,y:(d=(g=r.pointList[0])==null?void 0:g.y)!=null?d:0})}).filter(r=>B.inViewPort({x:r.x,y:r.y},o)),n=f.getNextSelectedRectID(l,t,this.selectedID);if(n){this.setSelectedID(n.id);const{selectedPolygon:r}=this;r&&this.setDefaultAttribute(r.attribute)}this.render()}onKeyDown(t){if(!f.hotkeyFilter(t)||super.onKeyDown(t)===!1)return;const{keyCode:e}=t;switch(e){case L.Space:this.spaceKeydown();break;case L.Esc:this.drawingPointList=[],this.editPolygonID="";break;case L.F:this.selectedID&&this.setPolygonValidAndRender(this.selectedID);break;case L.Z:if(t.altKey){this.onCombinedExecute();return}this.setIsHidden(!this.isHidden),this.render();break;case L.Delete:this.deletePolygons(this.selectedIDs),this.render();break;case L.Ctrl:this.isCtrl=!0;break;case L.Alt:this.isAlt===!1&&(t.preventDefault(),this.isAlt=!0,this.render());break;case L.Tab:{this.onTabKeyDown(t);break}case L.X:t.altKey&&this.segment();break;default:{if(this.config.attributeConfigurable){const i=b.getAttributeByKeycode(e,this.config.attributeList);i!==void 0&&this.setDefaultAttribute(i)}break}}}onKeyUp(t){if(super.onKeyUp(t),!this.selection.triggerKeyboardEvent(t,this.setPolygonList.bind(this)))switch(t.keyCode){case L.Ctrl:this.isCtrl=!1;break;case L.Alt:{const e=this.isAlt;this.isAlt=!1,e===!0&&this.render();break}}}rightMouseUp(t){if(this.drawingPointList.length>0){this.addDrawingPointToPolygonList();return}this.setSelectedID(this.hoverID,t.ctrlKey);const{selectedPolygon:e}=this;e&&this.setDefaultAttribute(e.attribute)}onLeftDblClick(t){if(this.hoverEdgeIndex>-1){const e=this.getCoordinateUnderZoom(t),{selectedPolygon:i}=this;if(!i)return;if(i.isRect===!0){this.emit("messageInfo",`${C.getMessagesByLocale(S.ForbidAddNewPoint,this.lang)}`),this.clearPolygonDrag();return}const{dropFoot:s}=u.getClosestPoint(e,this.polygonListUnderZoom,this.config.lineType,R);if(!s)return;const{upperLimitPointNum:o}=this.config;if(o&&i.pointList.length>=o){this.emit("messageInfo",`${C.getMessagesByLocale(S.UpperLimitErrorNotice,this.lang)}${o}`),this.clearPolygonDrag();return}i==null||i.pointList.splice(this.hoverEdgeIndex+1,0,a.changePointByZoom(s,1/this.zoom)),this.setPolygonDataByID(i,this.selectedID),this.history.pushHistory(this.polygonList),this.hoverPointIndex=-1,this.hoverEdgeIndex=-1,this.render()}this.dragInfo=void 0}onRightDblClick(t){this.dragInfo=void 0,this.clearImgDrag();const e=this.getHoverID(t),i=this.getHoverPointIndex(t);if(this.hoverPointIndex>-1&&this.hoverPointIndex===i){this.deletePolygonPoint(i),this.dragInfo=void 0,this.hoverPointIndex=-1,this.render();return}this.hoverID===this.selectedID&&this.deletePolygons([e]),this.render()}onMouseDown(t){if(!(super.onMouseDown(t)||this.forbidMouseOperation||t.ctrlKey===!0))return this.dragMouseDown(t),!0}dragMouseDown(t){if(t.button!==0)return;const e=this.getHoverID(t);if(!this.selection.isIdSelected(e))return;let i=[0],s=v.Plane;const o=this.selectedPolygons[0].pointList,l=this.getCoordinateUnderZoom(t);if(this.selectedPolygons.length===1){this.dragStatus=x.Start;const n=this.getHoverPointIndex(t),r=this.getHoverEdgeIndex(t);n>-1?(s=v.Point,i=[n]):r>-1&&this.hoverEdgeIndex>-1&&(s=v.Line,i=[r,(r+1)%o.length])}this.dragInfo={dragStartCoord:l,dragTarget:s,initPointList:o,changePointIndex:i,originPolygon:this.selectedPolygon,selectedPolygons:this.selectedPolygons,dragPrevCoord:l,originPolygonList:this.polygonList}}segment(){var t;if(!this.selectedID||((t=this.config)==null?void 0:t.lineType)!==O.Line)return;const e=u.getPolygonPointList(this.selectedID,this.currentShowList),i=this.currentShowList.filter(l=>l.id!==this.selectedID);if(i.length===0||e.length===0)return;const s=u.getWrapPolygonIndex(e,i);let o=[...this.polygonList];if(s===-1){const l=u.segmentPolygonByPolygon(e,i);if(!l)return;const n=l.shift();if(!n)return;let r="",h=!0;const c=f.getSourceID(this.basicResult);let g="";o=this.polygonList.map(d=>{var p,T;return d.id===this.selectedID?(r=d.attribute,h=(p=d==null?void 0:d.valid)!=null?p:!0,g=(T=d==null?void 0:d.textAttribute)!=null?T:"",m(y({},d),{pointList:n})):d}),l.length>0&&l.forEach((d,p)=>{o.push({sourceID:c,id:M(8,62),pointList:d,valid:h,order:f.getMaxOrder(this.currentShowList)+1+p,attribute:r,textAttribute:g})})}else o[s].pointList=u.clipPolygonFromWrapPolygon(e,o[s].pointList),o=o.filter(l=>l.id!==this.selectedID);this.setPolygonList(o),this.history.pushHistory(o),this.render()}onCombinedExecute(){if(!this.selectedID){this.emit("messageInfo",_.t("PolygonsToBeCombinedNeedToBeSelected"));return}this.isCombined=!this.isCombined}combine(t){var e;const i=this.getHoverID(t);if(!i||!this.selectedID||this.selectedID===i)return;if(((e=this.config)==null?void 0:e.lineType)!==O.Line){this.emit("messageInfo",_.t("CurveModeDoesNotSupportCutting"));return}const s=this.polygonList.find(h=>h.id===this.selectedID),o=this.currentShowList.find(h=>h.id===i);if(!o||!s)return;const l=u.combinePolygonWithPolygon(s,o);if(!l)return;const{newPolygon:n,unionList:r}=l;if(r.length===1&&n){const h=this.polygonList.filter(c=>!r.includes(c.id)).map(c=>c.id===this.selectedID?n:c);this.setPolygonList(h),this.history.pushHistory(h),this.render(),this.emit("messageInfo",_.t("CombineSuccess"))}else this.emit("messageInfo",_.t("CombiningFailedNotify"));this.isCombined=!1}isPolygonOutSide(t){if(this.dependToolName&&this.basicCanvas&&this.basicResult){let n=!1;switch(this.dependToolName){case H.Rect:{n=t.filter(r=>!N.isInRect(r,this.basicResult)).length>0;break}case H.Polygon:{n=u.isPointListOutSidePolygon(t,this.basicResult.pointList,this.config.lineType);break}}return n}if(!this.imgInfo)return!1;const{left:e,top:i,right:s,bottom:o}=D.calcViewportBoundaries(a.changePointListByZoom(t,this.zoom)),l=1e-5;return e<0||i<0||s>this.imgInfo.width+l||o>this.imgInfo.height+l}onDragMove(t){const e=this.polygonList.map(i=>{if(this.selectedIDs.includes(i.id)){const s=this.dragPolygon(t,i);if(!s)return i;const o=m(y({},i),{pointList:s});return i.isRect===!0&&this.pattern===w.Normal&&Object.assign(o,{isRect:!1}),o}return i});this.dragInfo.dragPrevCoord=this.getCoordinateUnderZoom(t),this.setPolygonList(e),this.render()}getDragOffset(t,e){var i;const s=this.getCoordinateUnderZoom(t),{dragTarget:o,dragPrevCoord:l,changePointIndex:n,initPointList:r,dragStartCoord:h}=this.dragInfo;if(this.pattern===w.Rect&&(e==null?void 0:e.isRect)===!0&&n&&[v.Line].includes(o)){const c=D.getArrayIndex(n[0]-2,4),g=D.getArrayIndex(n[0]-1,4),d=[r[c],r[g]],p=D.getRectPerpendicularOffset(h,s,d);return{x:p.x/this.zoom,y:p.y/this.zoom}}return((i=this.dragInfo)==null?void 0:i.dragTarget)===v.Plane?{x:(s.x-l.x)/this.zoom,y:(s.y-l.y)/this.zoom}:{x:(s.x-h.x)/this.zoom,y:(s.y-h.y)/this.zoom}}dragPolygon(t,e){let i=j.cloneDeep(e==null?void 0:e.pointList);if(!i||!this.dragInfo)return;const{initPointList:s,dragTarget:o,changePointIndex:l}=this.dragInfo,n=this.getDragOffset(t,e);switch(this.dragStatus=x.Move,o){case v.Plane:i=i.map(r=>m(y({},r),{x:r.x+n.x,y:r.y+n.y}));break;case v.Point:case v.Line:i=i.map((r,h)=>l&&l.includes(h)?m(y({},r),{x:s[h].x+n.x,y:s[h].y+n.y}):r);break}if(this.pattern===w.Rect&&(e==null?void 0:e.isRect)===!0&&o===v.Point&&l&&(i=D.getPointListFromPointOffset(s,l[0],n)),!(this.config.drawOutsideTarget===!1&&this.imgInfo&&this.isPolygonOutSide(i)))return i}onMouseMove(t){if(super.onMouseMove(t)||this.forbidMouseOperation||!this.imgInfo)return;let e=-1,i=-1;const{selectedID:s}=this;if(s&&(this.hoverEdgeIndex=-1,this.hoverPointIndex=-1,e=this.getHoverPointIndex(t),e>-1?this.hoverPointIndex=e:(i=this.getHoverEdgeIndex(t),this.hoverEdgeIndex=i)),this.drawingPointList.length>0)return;const o=this.getHoverID(t);this.hoverID!==o&&(this.hoverID=o,this.render()),this.selectedIDs.length>0&&this.dragInfo&&this.onDragMove(t)}leftMouseUpdateValid(t){const e=this.getHoverID(t);return this.drawingPointList.length===0&&t.ctrlKey===!0&&e?(this.setPolygonValidAndRender(e),!0):!1}leftMouseUp(t){this.leftMouseUpdateValid(t)||this.addPointInDrawing(t)}onMouseUp(t){if(this.isCombined){switch(t.button){case 0:this.combine(t);break;case 2:this.isCombined=!1;break;default:return}return}if(!(super.onMouseUp(t)||this.forbidMouseOperation||!this.imgInfo)){if(this.dragInfo&&this.dragStatus===x.Move){this.emit("updateResult"),this.emitUpdatePolygonByDrag(),this.dragInfo=void 0,this.dragStatus=x.Wait,this.history.pushHistory(this.polygonList);return}switch(t.button){case 0:{this.leftMouseUp(t);break}case 2:{this.rightMouseUp(t);break}}this.render()}}dragMouseUp(){this.dragStatus===x.Start&&(this.dragInfo=void 0,this.dragStatus=x.Wait)}exportData(){const{polygonList:t}=this;return[t,this.basicImgInfo]}getTextIconSvg(t=""){return b.getTextIconSvg(t,this.config.attributeList,this.config.attributeConfigurable,this.baseIcon)}getCurrentSelectedData(){const{selectedPolygon:t}=this;if(!t)return;const e=this.getColor(t.attribute),i=t.valid?e==null?void 0:e.valid.stroke:e==null?void 0:e.invalid.stroke;return{width:z,textAttribute:t.textAttribute,color:i}}updateSelectedTextAttribute(t){if(this._textAttributeInstance&&t&&this.selectedID){let e=t;b.textAttributeValidate(this.config.textCheckType,"",e)===!1&&(this.emit("messageError",b.getErrorNotice(this.config.textCheckType,this.lang)),e=""),this.setPolygonList(b.textChange(e,this.selectedID,this.polygonList)),this.emit("updateTextAttribute"),this.render()}}renderTextAttribute(){var t;const{selectedPolygon:e}=this;if(!this.ctx||this.config.textConfigurable===!1||!e)return;const{pointList:i,attribute:s,valid:o,textAttribute:l}=e,{x:n,y:r}=i[i.length-1],h=z,c=a.getOffsetCoordinate({x:n,y:r},this.currentPos,this.zoom),g=this.getColor(s),d=o?g==null?void 0:g.valid.stroke:g==null?void 0:g.invalid.stroke;this._textAttributeInstance||(this._textAttributeInstance=new V({width:h,container:this.container,icon:this.getTextIconSvg(s),color:d,getCurrentSelectedData:this.getCurrentSelectedData,updateSelectedTextAttribute:this.updateSelectedTextAttribute})),this._textAttributeInstance&&!((t=this._textAttributeInstance)==null?void 0:t.isExit)&&this._textAttributeInstance.appendToContainer(),this._textAttributeInstance.update(`${l}`,{left:c.x,top:c.y,color:d,width:h})}renderStaticPolygon(){var t;this.isHidden===!1&&((t=this.visiblePolygonList)==null||t.forEach(e=>{var i,s,o,l,n;if([this.selectedID,this.editPolygonID].includes(e.id))return;const{textAttribute:r,attribute:h}=e,c=this.getColor(h),g=A.getStrokeAndFill(c,e.valid),d=a.changePointListByZoom(e.pointList||[],this.zoom,this.currentPos);P.drawPolygonWithFillAndLine(this.canvas,d,{fillColor:g.fill,strokeColor:g.stroke,pointColor:"white",thickness:(s=(i=this.style)==null?void 0:i.width)!=null?s:2,lineCap:"round",isClose:!0,lineType:(o=this.config)==null?void 0:o.lineType});let p=`${(l=b.getAttributeShowText(h,this.config.attributeList))!=null?l:""}`;((n=this.config)==null?void 0:n.isShowOrder)&&(e==null?void 0:e.order)>0&&(p=`${e.order} ${p}`),P.drawText(this.canvas,d[0],p,y({color:g.stroke},k));const T=d[d.length-1];P.drawText(this.canvas,{x:T.x+E.x,y:T.y+E.y},r,y({color:g.stroke},k))}))}renderSelectedPolygons(){this.selectedPolygons.forEach(t=>{this.renderSelectedPolygon(t)})}renderSelectedPolygon(t){var e,i,s;if(t){const o=this.getColor(t.attribute),l=A.getStrokeAndFill(o,t.valid,{isSelected:!0});P.drawSelectedPolygonWithFillAndLine(this.canvas,a.changePointListByZoom(t.pointList,this.zoom,this.currentPos),{fillColor:l.fill,strokeColor:l.stroke,pointColor:"white",thickness:2,lineCap:"round",isClose:!0,lineType:(e=this.config)==null?void 0:e.lineType});let n=`${(i=b.getAttributeShowText(t.attribute,this.config.attributeList))!=null?i:""}`;((s=this.config)==null?void 0:s.isShowOrder)&&(t==null?void 0:t.order)>0&&(n=`${t.order} ${n}`),P.drawText(this.canvas,a.changePointByZoom(t.pointList[0],this.zoom,this.currentPos),n,y({color:l.stroke},k)),this.renderTextAttribute()}}renderHoverPolygon(){var t;if(this.hoverID&&this.hoverID!==this.editPolygonID){const{hoverPolygon:e}=this;if(e){let i="";const s=this.getColor(e.attribute);e.valid?i=s.validHover.fill:i=A.getStrokeAndFill(s,!1,{isHover:!0}).fill,P.drawPolygonWithFill(this.canvas,a.changePointListByZoom(e.pointList,this.zoom,this.currentPos),{color:i,lineType:(t=this.config)==null?void 0:t.lineType})}}}renderPolygon(){var t,e,i,s;this.renderStaticPolygon(),this.renderHoverPolygon(),this.renderSelectedPolygons();const o=this.getColor(this.defaultAttribute),l=A.getStrokeAndFill(o,!this.isCtrl);if(((t=this.drawingPointList)==null?void 0:t.length)>0){let n=[...this.drawingPointList],r=a.getOriginCoordinateWithOffsetCoordinate(this.coord,this.zoom,this.currentPos);if(this.pattern===w.Rect&&n.length===2)n=D.getRectangleByRightAngle(r,n);else{if(((e=this.config)==null?void 0:e.edgeAdsorption)&&this.isAlt===!1){const{dropFoot:h}=u.getClosestPoint(r,this.polygonList,(i=this.config)==null?void 0:i.lineType,R/this.zoom);h&&(r=h)}n.push(r)}P.drawSelectedPolygonWithFillAndLine(this.canvas,a.changePointListByZoom(n,this.zoom,this.currentPos),{fillColor:l.fill,strokeColor:l.stroke,pointColor:"white",thickness:2,lineCap:"round",isClose:!1,lineType:this.config.lineType})}if(this.hoverPointIndex>-1&&this.selectedID){const n=this.selectedPolygon;if(!n)return;const r=A.getStrokeAndFill(o,n.valid,{isSelected:!0}),h=n==null?void 0:n.pointList[this.hoverPointIndex];if(h){const{x:c,y:g}=a.changePointByZoom(h,this.zoom,this.currentPos);P.drawCircleWithFill(this.canvas,{x:c,y:g},5,{color:r.fill})}}if(this.hoverEdgeIndex>-1&&this.selectedID){const n=this.selectedPolygon;if(!n)return;const r=A.getStrokeAndFill(o,n.valid,{isSelected:!0});P.drawLineWithPointList(this.canvas,a.changePointListByZoom(n.pointList,this.zoom,this.currentPos),{color:r.stroke,thickness:10,hoverEdgeIndex:this.hoverEdgeIndex,lineType:(s=this.config)==null?void 0:s.lineType})}}render(){!this.ctx||(super.render(),this.renderPolygon(),this.renderCursorLine(this.getLineColor(this.defaultAttribute)))}renderCursorLine(t){if(super.renderCursorLine(t),this.isCombined){const{x:e,y:i}=this.coord,s=10,o=186,l=32;P.drawRectWithFill(this.canvas,{x:e+s,y:i-s*4-1,width:o,height:l},{color:"black"}),P.drawText(this.canvas,{x:e,y:i},_.t("ClickAnotherPolygon"),{textAlign:"center",color:"white",offsetX:o/2+s,offsetY:-(l/2+s/2)}),P.drawRect(this.canvas,{x:e-s,y:i-s,width:s*2,height:s*2},{lineDash:[6],color:"white"})}}undo(){if(this.drawingPointList.length>0){const e=this.drawingHistory.undo();if(!e)return;this.drawingPointList=e,this.render();return}const t=this.history.undo();t&&(t.length!==this.polygonList.length&&this.deleteSelectedID(),this.setPolygonList(t),this.render())}redo(){if(this.drawingPointList.length>0){const e=this.drawingHistory.redo();if(!e)return;this.drawingPointList=e,this.render();return}const t=this.history.redo();t&&(t.length!==this.polygonList.length&&this.deleteSelectedID(),this.setPolygonList(t),this.render())}deleteSelectedID(){this.setSelectedID("")}}export{tt as default};
|