-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(draw): add component componentBox and template shape for uml #WI…
…K-15712 (#910)
- Loading branch information
Showing
8 changed files
with
332 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@plait/draw': minor | ||
--- | ||
|
||
add component componentBox and template shape for uml |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
import { | ||
PlaitBoard, | ||
Point, | ||
PointOfRectangle, | ||
RectangleClient, | ||
getNearestPointBetweenPointAndSegments, | ||
setStrokeLinecap | ||
} from '@plait/core'; | ||
import { PlaitGeometry, ShapeEngine } from '../../interfaces'; | ||
import { Options } from 'roughjs/bin/core'; | ||
import { getPolygonEdgeByConnectionPoint } from '../../utils/polygon'; | ||
import { RectangleEngine } from '../basic-shapes/rectangle'; | ||
import { getStrokeWidthByElement } from '../../utils'; | ||
import { ShapeDefaultSpace } from '../../constants'; | ||
import { ComponentEngine } from './component'; | ||
|
||
export const ComponentBoxEngine: ShapeEngine = { | ||
draw(board: PlaitBoard, rectangle: RectangleClient, options: Options) { | ||
const rs = PlaitBoard.getRoughSVG(board); | ||
const componentWidth = rectangle.width - 45 * 2 - 18 > 1 ? 45 : rectangle.width * 0.25; | ||
const componentHeight = rectangle.height - 30 - 8 * 2 > 1 ? 30 : rectangle.height * 0.2; | ||
|
||
const componentRectangle = { | ||
x: rectangle.x + rectangle.width - 18 - componentWidth, | ||
y: rectangle.y + 8, | ||
width: componentWidth, | ||
height: componentHeight | ||
}; | ||
const shape = rs.path( | ||
`M${rectangle.x} ${rectangle.y} | ||
H${rectangle.x + rectangle.width} | ||
V${rectangle.y + rectangle.height} | ||
H${rectangle.x} Z | ||
`, | ||
{ ...options, fillStyle: 'solid' } | ||
); | ||
|
||
const componentShape = ComponentEngine.draw(board, componentRectangle, options); | ||
shape.append(componentShape); | ||
setStrokeLinecap(shape, 'round'); | ||
|
||
return shape; | ||
}, | ||
isInsidePoint(rectangle: RectangleClient, point: Point) { | ||
const rangeRectangle = RectangleClient.getRectangleByPoints([point, point]); | ||
return RectangleClient.isHit(rectangle, rangeRectangle); | ||
}, | ||
getCornerPoints(rectangle: RectangleClient) { | ||
return RectangleClient.getCornerPoints(rectangle); | ||
}, | ||
getNearestPoint(rectangle: RectangleClient, point: Point) { | ||
return getNearestPointBetweenPointAndSegments(point, RectangleEngine.getCornerPoints(rectangle)); | ||
}, | ||
getEdgeByConnectionPoint(rectangle: RectangleClient, pointOfRectangle: PointOfRectangle): [Point, Point] | null { | ||
const corners = RectangleEngine.getCornerPoints(rectangle); | ||
const point = RectangleClient.getConnectionPoint(rectangle, pointOfRectangle); | ||
return getPolygonEdgeByConnectionPoint(corners, point); | ||
}, | ||
getConnectorPoints(rectangle: RectangleClient) { | ||
return RectangleClient.getEdgeCenterPoints(rectangle); | ||
}, | ||
getTextRectangle(element: PlaitGeometry) { | ||
const elementRectangle = RectangleClient.getRectangleByPoints(element.points!); | ||
const strokeWidth = getStrokeWidthByElement(element); | ||
const height = element.textHeight!; | ||
const componentWidth = elementRectangle.width - 45 * 2 - 18 > 1 ? 45 : elementRectangle.width * 0.25; | ||
const width = elementRectangle.width - 18 - componentWidth - ShapeDefaultSpace.rectangleAndText - strokeWidth * 2; | ||
return { | ||
height, | ||
width: width > 0 ? width : 0, | ||
x: elementRectangle.x + ShapeDefaultSpace.rectangleAndText + strokeWidth, | ||
y: elementRectangle.y + (elementRectangle.height - height) / 2 | ||
}; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
import { | ||
PlaitBoard, | ||
Point, | ||
PointOfRectangle, | ||
RectangleClient, | ||
getNearestPointBetweenPointAndSegments, | ||
setStrokeLinecap | ||
} from '@plait/core'; | ||
import { PlaitGeometry, ShapeEngine } from '../../interfaces'; | ||
import { Options } from 'roughjs/bin/core'; | ||
import { RectangleEngine } from '../basic-shapes/rectangle'; | ||
import { getStrokeWidthByElement } from '../../utils'; | ||
import { ShapeDefaultSpace } from '../../constants'; | ||
import { getUnitVectorByPointAndPoint } from '@plait/common'; | ||
|
||
export const ComponentEngine: ShapeEngine = { | ||
draw(board: PlaitBoard, rectangle: RectangleClient, options: Options) { | ||
const rs = PlaitBoard.getRoughSVG(board); | ||
const boxSize = { | ||
with: rectangle.width > 70 ? 24 : rectangle.width * 0.2, | ||
height: rectangle.height - 28 - rectangle.height * 0.35 > 1 ? 14 : rectangle.height * 0.175 | ||
}; | ||
const shape = rs.path( | ||
`M${rectangle.x + 12} ${rectangle.y} | ||
v${rectangle.height * 0.175} | ||
h${boxSize.with / 2} v${boxSize.height} h${-boxSize.with} v${-boxSize.height} h${boxSize.with / 2} | ||
M${rectangle.x + 12} ${rectangle.y + rectangle.height * 0.175 + boxSize.height} | ||
v${rectangle.height - rectangle.height * 0.35 - boxSize.height * 2} | ||
h${boxSize.with / 2} v${boxSize.height} h${-boxSize.with} v${-boxSize.height} h${boxSize.with / 2} | ||
M${rectangle.x + 12} ${rectangle.y + rectangle.height - rectangle.height * 0.175} | ||
V${rectangle.y + rectangle.height} | ||
H${rectangle.x + rectangle.width} | ||
v${-rectangle.height} | ||
h${-(rectangle.width - 12)} | ||
`, | ||
{ ...options, fillStyle: 'solid' } | ||
); | ||
setStrokeLinecap(shape, 'round'); | ||
|
||
return shape; | ||
}, | ||
isInsidePoint(rectangle: RectangleClient, point: Point) { | ||
const rangeRectangle = RectangleClient.getRectangleByPoints([point, point]); | ||
return RectangleClient.isHit(rectangle, rangeRectangle); | ||
}, | ||
getCornerPoints(rectangle: RectangleClient) { | ||
return RectangleClient.getCornerPoints(rectangle); | ||
}, | ||
getNearestPoint(rectangle: RectangleClient, point: Point) { | ||
let nearestPoint = getNearestPointBetweenPointAndSegments(point, RectangleEngine.getCornerPoints(rectangle)); | ||
if (nearestPoint[1] === rectangle.y + rectangle.height / 2) { | ||
nearestPoint = getNearestPointBetweenPointAndSegments( | ||
point, | ||
[ | ||
[rectangle.x + 12, rectangle.y + rectangle.height * 0.175 + 14], | ||
[rectangle.x + 12, rectangle.y + rectangle.height - rectangle.height * 0.175 - 14] | ||
], | ||
false | ||
); | ||
} | ||
return nearestPoint; | ||
}, | ||
|
||
getTangentVectorByConnectionPoint(rectangle: RectangleClient, pointOfRectangle: PointOfRectangle) { | ||
const connectionPoint = RectangleClient.getConnectionPoint(rectangle, pointOfRectangle); | ||
return getUnitVectorByPointAndPoint( | ||
[rectangle.x + 12, rectangle.y + rectangle.height - rectangle.height * 0.175 - 14], | ||
connectionPoint | ||
); | ||
}, | ||
getConnectorPoints(rectangle: RectangleClient) { | ||
return [ | ||
[rectangle.x + rectangle.width / 2, rectangle.y], | ||
[rectangle.x + rectangle.width, rectangle.y + rectangle.height / 2], | ||
[rectangle.x + rectangle.width / 2, rectangle.y + rectangle.height], | ||
[rectangle.x + 12, rectangle.y + rectangle.height / 2] | ||
] as [Point, Point, Point, Point]; | ||
}, | ||
getTextRectangle(element: PlaitGeometry) { | ||
const elementRectangle = RectangleClient.getRectangleByPoints(element.points!); | ||
const strokeWidth = getStrokeWidthByElement(element); | ||
const height = element.textHeight!; | ||
const width = elementRectangle.width - 24 - ShapeDefaultSpace.rectangleAndText * 2 - strokeWidth * 2; | ||
return { | ||
height, | ||
width: width > 0 ? width : 0, | ||
x: elementRectangle.x + 24 + ShapeDefaultSpace.rectangleAndText + strokeWidth, | ||
y: elementRectangle.y + (elementRectangle.height - height) / 2 | ||
}; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import { | ||
PlaitBoard, | ||
Point, | ||
PointOfRectangle, | ||
RectangleClient, | ||
drawRoundRectangle, | ||
getNearestPointBetweenPointAndSegments | ||
} from '@plait/core'; | ||
import { PlaitGeometry, ShapeEngine } from '../../interfaces'; | ||
import { Options } from 'roughjs/bin/core'; | ||
import { getPolygonEdgeByConnectionPoint } from '../../utils/polygon'; | ||
import { RectangleEngine } from '../basic-shapes/rectangle'; | ||
import { getStrokeWidthByElement } from '../../utils'; | ||
import { ShapeDefaultSpace } from '../../constants'; | ||
|
||
export const TemplateEngine: ShapeEngine = { | ||
draw(board: PlaitBoard, rectangle: RectangleClient, options: Options) { | ||
const rs = PlaitBoard.getRoughSVG(board); | ||
|
||
return drawRoundRectangle( | ||
rs, | ||
rectangle.x, | ||
rectangle.y, | ||
rectangle.x + rectangle.width, | ||
rectangle.y + rectangle.height, | ||
{ | ||
...options, | ||
fillStyle: 'solid', | ||
dashGap: 10, | ||
strokeLineDash: [10, 10] | ||
}, | ||
false, | ||
4 | ||
); | ||
}, | ||
isInsidePoint(rectangle: RectangleClient, point: Point) { | ||
const rangeRectangle = RectangleClient.getRectangleByPoints([point, point]); | ||
return RectangleClient.isHit(rectangle, rangeRectangle); | ||
}, | ||
getCornerPoints(rectangle: RectangleClient) { | ||
return RectangleClient.getCornerPoints(rectangle); | ||
}, | ||
getNearestPoint(rectangle: RectangleClient, point: Point) { | ||
return getNearestPointBetweenPointAndSegments(point, RectangleEngine.getCornerPoints(rectangle)); | ||
}, | ||
getEdgeByConnectionPoint(rectangle: RectangleClient, pointOfRectangle: PointOfRectangle): [Point, Point] | null { | ||
const corners = RectangleEngine.getCornerPoints(rectangle); | ||
const point = RectangleClient.getConnectionPoint(rectangle, pointOfRectangle); | ||
return getPolygonEdgeByConnectionPoint(corners, point); | ||
}, | ||
getConnectorPoints(rectangle: RectangleClient) { | ||
return RectangleClient.getEdgeCenterPoints(rectangle); | ||
}, | ||
getTextRectangle(element: PlaitGeometry) { | ||
const elementRectangle = RectangleClient.getRectangleByPoints(element.points!); | ||
const strokeWidth = getStrokeWidthByElement(element); | ||
const height = element.textHeight!; | ||
const width = elementRectangle.width - ShapeDefaultSpace.rectangleAndText * 2 - strokeWidth * 2; | ||
return { | ||
height, | ||
width: width > 0 ? width : 0, | ||
x: elementRectangle.x + ShapeDefaultSpace.rectangleAndText + strokeWidth, | ||
y: elementRectangle.y + (elementRectangle.height - height) / 2 | ||
}; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.