@plait/draw 0.31.0 → 0.32.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/constants/line.d.ts +1 -0
- package/esm2022/constants/line.mjs +2 -1
- package/esm2022/utils/hit.mjs +21 -7
- package/esm2022/utils/line-arrow.mjs +22 -17
- package/esm2022/utils/line.mjs +6 -9
- package/fesm2022/plait-draw.mjs +46 -29
- package/fesm2022/plait-draw.mjs.map +1 -1
- package/package.json +1 -1
- package/utils/clipboard.d.ts +1 -1
- package/utils/geometry.d.ts +1 -1
- package/utils/hit.d.ts +2 -0
- package/utils/line.d.ts +0 -1
package/fesm2022/plait-draw.mjs
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { PlaitElement, ACTIVE_STROKE_WIDTH, PlaitBoard, setStrokeLinecap, isPointInPolygon, getNearestPointBetweenPointAndSegments, RectangleClient, isPointInEllipse, drawRectangle, drawRoundRectangle, isPointInRoundRectangle, Transforms, clearSelectedElement, addSelectedElement, PlaitNode, Point, BOARD_TO_HOST, transformPoint, toPoint, idCreator, createG, BoardTransforms, PlaitPointerType, preventTouchMove, createForeignObject, SELECTION_BORDER_COLOR, SELECTION_FILL_COLOR, drawCircle, distanceBetweenPointAndSegment, arrowPoints, createPath, drawLinearPath, rotate, getElementById,
|
|
2
|
-
import { getRectangleByPoints, Generator, normalizeShapePoints, isDndMode, isDrawingMode, getFactorByPoints, getDirectionByVector, getOppositeDirection, getDirectionFactor, DEFAULT_ROUTE_MARGIN, reduceRouteMargin, getNextPoint, generateElbowLineRoute, getPoints, removeDuplicatePoints, getPointByVector, getPointOnPolyline, getDirectionByPointOfRectangle, rotateVectorAnti90, TRANSPARENT, CommonPluginElement, ActiveGenerator, WithTextPluginKey, RESIZE_HANDLE_DIAMETER, PRIMARY_COLOR, isVirtualKey, isDelete, isSpaceHotkey, acceptImageTypes, getElementOfFocusedImage, buildImage, getRectangleResizeHandleRefs, ResizeHandle, withResize, isResizingByCondition, getRatioByPoint, ImageGenerator } from '@plait/common';
|
|
1
|
+
import { PlaitElement, ACTIVE_STROKE_WIDTH, PlaitBoard, setStrokeLinecap, isPointInPolygon, getNearestPointBetweenPointAndSegments, RectangleClient, isPointInEllipse, drawRectangle, drawRoundRectangle, isPointInRoundRectangle, Transforms, clearSelectedElement, addSelectedElement, PlaitNode, Point, BOARD_TO_HOST, transformPoint, toPoint, idCreator, createG, BoardTransforms, PlaitPointerType, preventTouchMove, createForeignObject, SELECTION_BORDER_COLOR, SELECTION_FILL_COLOR, drawCircle, distanceBetweenPointAndSegment, arrowPoints, createPath, drawLinearPath, distanceBetweenPointAndPoint, rotate, getElementById, distanceBetweenPointAndSegments, createMask, createRect, findElements, getSelectedElements, isPolylineHitRectangle, isSelectionMoving, PlaitPluginElementComponent, setClipboardData, getDataFromClipboard, depthFirstRecursion, getIsRecursionFunc, getHitElementByPoint } from '@plait/core';
|
|
2
|
+
import { getRectangleByPoints, Generator, normalizeShapePoints, isDndMode, isDrawingMode, getExtendPoint, getFactorByPoints, getDirectionByVector, getOppositeDirection, getDirectionFactor, DEFAULT_ROUTE_MARGIN, reduceRouteMargin, getNextPoint, generateElbowLineRoute, getPoints, removeDuplicatePoints, getPointByVector, getPointOnPolyline, getDirectionByPointOfRectangle, rotateVectorAnti90, TRANSPARENT, CommonPluginElement, ActiveGenerator, WithTextPluginKey, RESIZE_HANDLE_DIAMETER, PRIMARY_COLOR, isVirtualKey, isDelete, isSpaceHotkey, acceptImageTypes, getElementOfFocusedImage, buildImage, getRectangleResizeHandleRefs, ResizeHandle, withResize, isResizingByCondition, getRatioByPoint, ImageGenerator } from '@plait/common';
|
|
3
3
|
import { AlignEditor, Alignment, DEFAULT_FONT_SIZE, buildText, TextManage, getTextFromClipboard, getTextSize } from '@plait/text';
|
|
4
4
|
import { isKeyHotkey } from 'is-hotkey';
|
|
5
5
|
import { pointsOnBezierCurves } from 'points-on-curve';
|
|
@@ -1493,6 +1493,7 @@ const DefaultLineStyle = {
|
|
|
1493
1493
|
strokeWidth: 2,
|
|
1494
1494
|
strokeColor: '#000'
|
|
1495
1495
|
};
|
|
1496
|
+
const LINE_TEXT_SPACE = 4;
|
|
1496
1497
|
|
|
1497
1498
|
const createGeometryElement = (shape, points, text, options) => {
|
|
1498
1499
|
let textOptions = {};
|
|
@@ -1649,25 +1650,28 @@ const createDefaultFlowchart = (point) => {
|
|
|
1649
1650
|
return [startElement, processElement1, decisionElement, processElement2, endElement, line1, line2, line3, line4, line5];
|
|
1650
1651
|
};
|
|
1651
1652
|
|
|
1653
|
+
const MAX_LENGTH = 100;
|
|
1652
1654
|
const drawLineArrow = (element, points, options) => {
|
|
1653
1655
|
const arrowG = createG();
|
|
1654
1656
|
if (PlaitLine.isSourceMark(element, LineMarkerType.none) && PlaitLine.isTargetMark(element, LineMarkerType.none)) {
|
|
1655
1657
|
return null;
|
|
1656
1658
|
}
|
|
1659
|
+
const strokeWidth = getStrokeWidthByElement(element);
|
|
1660
|
+
const offset = (strokeWidth * strokeWidth) / 3;
|
|
1657
1661
|
if (!PlaitLine.isSourceMark(element, LineMarkerType.none)) {
|
|
1658
|
-
const source = getExtendPoint(points[0], points[1], 24);
|
|
1662
|
+
const source = getExtendPoint(points[0], points[1], 24 + offset);
|
|
1659
1663
|
const sourceArrow = getArrow(element, { marker: element.source.marker, source, target: points[0], isSource: true }, options);
|
|
1660
1664
|
sourceArrow && arrowG.appendChild(sourceArrow);
|
|
1661
1665
|
}
|
|
1662
1666
|
if (!PlaitLine.isTargetMark(element, LineMarkerType.none)) {
|
|
1663
|
-
const source = getExtendPoint(points[points.length - 1], points[points.length - 2], 24);
|
|
1667
|
+
const source = getExtendPoint(points[points.length - 1], points[points.length - 2], 24 + offset);
|
|
1664
1668
|
const arrow = getArrow(element, { marker: element.target.marker, source, target: points[points.length - 1], isSource: false }, options);
|
|
1665
1669
|
arrow && arrowG.appendChild(arrow);
|
|
1666
1670
|
}
|
|
1667
1671
|
return arrowG;
|
|
1668
1672
|
};
|
|
1669
1673
|
const getArrow = (element, arrowOptions, options) => {
|
|
1670
|
-
const { marker,
|
|
1674
|
+
const { marker, target, source, isSource } = arrowOptions;
|
|
1671
1675
|
let targetArrow;
|
|
1672
1676
|
switch (marker) {
|
|
1673
1677
|
case LineMarkerType.openTriangle: {
|
|
@@ -1707,10 +1711,10 @@ const getArrow = (element, arrowOptions, options) => {
|
|
|
1707
1711
|
};
|
|
1708
1712
|
const drawSharpArrow = (source, target, options) => {
|
|
1709
1713
|
const startPoint = target;
|
|
1710
|
-
const { pointLeft, pointRight } = arrowPoints(source, target,
|
|
1714
|
+
const { pointLeft, pointRight } = arrowPoints(source, target, 20);
|
|
1711
1715
|
const g = createG();
|
|
1712
1716
|
const path = createPath();
|
|
1713
|
-
let polylinePath = `M${pointRight[0]},${pointRight[1]}
|
|
1717
|
+
let polylinePath = `M${pointRight[0]},${pointRight[1]}A25,25,20,0,1,${pointLeft[0]},${pointLeft[1]}L${startPoint[0]},${startPoint[1]}Z`;
|
|
1714
1718
|
path.setAttribute('d', polylinePath);
|
|
1715
1719
|
path.setAttribute('stroke', `${options?.stroke}`);
|
|
1716
1720
|
path.setAttribute('stroke-width', `${options?.strokeWidth}`);
|
|
@@ -1722,8 +1726,11 @@ const drawArrow = (element, source, target, options) => {
|
|
|
1722
1726
|
const directionFactor = getFactorByPoints(source, target);
|
|
1723
1727
|
const strokeWidth = getStrokeWidthByElement(element);
|
|
1724
1728
|
const endPoint = [target[0] + (strokeWidth * directionFactor.x) / 2, target[1] + (strokeWidth * directionFactor.y) / 2];
|
|
1725
|
-
const middlePoint = [
|
|
1726
|
-
|
|
1729
|
+
const middlePoint = [
|
|
1730
|
+
endPoint[0] - (8 + strokeWidth / 2) * directionFactor.x,
|
|
1731
|
+
endPoint[1] - (8 + strokeWidth / 2) * directionFactor.y
|
|
1732
|
+
];
|
|
1733
|
+
const { pointLeft, pointRight } = arrowPoints(source, endPoint, 30);
|
|
1727
1734
|
const arrowG = drawLinearPath([pointLeft, endPoint, pointRight, middlePoint], { ...options, fill: options.stroke }, true);
|
|
1728
1735
|
const path = arrowG.querySelector('path');
|
|
1729
1736
|
path.setAttribute('stroke-linejoin', 'round');
|
|
@@ -1731,30 +1738,30 @@ const drawArrow = (element, source, target, options) => {
|
|
|
1731
1738
|
};
|
|
1732
1739
|
const drawSolidTriangle = (source, target, options) => {
|
|
1733
1740
|
const endPoint = target;
|
|
1734
|
-
const { pointLeft, pointRight } = arrowPoints(source, endPoint,
|
|
1741
|
+
const { pointLeft, pointRight } = arrowPoints(source, endPoint, 30);
|
|
1735
1742
|
return drawLinearPath([pointLeft, endPoint, pointRight], { ...options, fill: options.stroke }, true);
|
|
1736
1743
|
};
|
|
1737
1744
|
const drawOpenTriangle = (element, source, target, options) => {
|
|
1738
1745
|
const directionFactor = getFactorByPoints(source, target);
|
|
1739
1746
|
const strokeWidth = getStrokeWidthByElement(element);
|
|
1740
1747
|
const endPoint = [target[0] + (strokeWidth * directionFactor.x) / 2, target[1] + (strokeWidth * directionFactor.y) / 2];
|
|
1741
|
-
const { pointLeft, pointRight } = arrowPoints(source, endPoint,
|
|
1748
|
+
const { pointLeft, pointRight } = arrowPoints(source, endPoint, 40);
|
|
1742
1749
|
return drawLinearPath([pointLeft, endPoint, pointRight], options);
|
|
1743
1750
|
};
|
|
1744
1751
|
const drawOneSideArrow = (source, target, side, options) => {
|
|
1745
|
-
const { pointLeft, pointRight } = arrowPoints(source, target,
|
|
1752
|
+
const { pointLeft, pointRight } = arrowPoints(source, target, 40);
|
|
1746
1753
|
return drawLinearPath([side === 'up' ? pointRight : pointLeft, target], options);
|
|
1747
1754
|
};
|
|
1748
1755
|
const drawSingleSlash = (source, target, isSource, options) => {
|
|
1749
|
-
|
|
1750
|
-
const middlePoint = getExtendPoint(target, source,
|
|
1756
|
+
const length = distanceBetweenPointAndPoint(...source, ...target);
|
|
1757
|
+
const middlePoint = getExtendPoint(target, source, length / 2);
|
|
1751
1758
|
const angle = isSource ? 120 : 60;
|
|
1752
1759
|
const start = rotate(...source, ...middlePoint, (angle * Math.PI) / 180);
|
|
1753
1760
|
const end = rotate(...target, ...middlePoint, (angle * Math.PI) / 180);
|
|
1754
1761
|
return drawLinearPath([start, end], options);
|
|
1755
1762
|
};
|
|
1756
1763
|
const drawHollowTriangleArrow = (source, target, options) => {
|
|
1757
|
-
const { pointLeft, pointRight } = arrowPoints(source, target,
|
|
1764
|
+
const { pointLeft, pointRight } = arrowPoints(source, target, 30);
|
|
1758
1765
|
return drawLinearPath([pointLeft, pointRight, target], { ...options, fill: 'white' }, true);
|
|
1759
1766
|
};
|
|
1760
1767
|
|
|
@@ -1986,7 +1993,8 @@ function drawMask(board, element, id) {
|
|
|
1986
1993
|
mask.appendChild(maskFillRect);
|
|
1987
1994
|
const texts = element.texts;
|
|
1988
1995
|
texts.forEach((text, index) => {
|
|
1989
|
-
|
|
1996
|
+
let textRectangle = getLineTextRectangle(board, element, index);
|
|
1997
|
+
textRectangle = RectangleClient.inflate(textRectangle, LINE_TEXT_SPACE * 2);
|
|
1990
1998
|
const rect = createRect(textRectangle, {
|
|
1991
1999
|
fill: 'black'
|
|
1992
2000
|
});
|
|
@@ -1995,6 +2003,7 @@ function drawMask(board, element, id) {
|
|
|
1995
2003
|
//撑开 line
|
|
1996
2004
|
const maskTargetFillRect = createRect(rectangle);
|
|
1997
2005
|
maskTargetFillRect.setAttribute('opacity', '0');
|
|
2006
|
+
maskTargetFillRect.setAttribute('fill', 'none');
|
|
1998
2007
|
return { mask, maskTargetFillRect };
|
|
1999
2008
|
}
|
|
2000
2009
|
const getConnectionPoint = (geometry, connection, direction, delta) => {
|
|
@@ -2042,12 +2051,6 @@ const getBoardLines = (board) => {
|
|
|
2042
2051
|
recursion: (element) => PlaitDrawElement.isDrawElement(element)
|
|
2043
2052
|
});
|
|
2044
2053
|
};
|
|
2045
|
-
const getExtendPoint = (source, target, extendDistance) => {
|
|
2046
|
-
const distance = distanceBetweenPointAndPoint(...source, ...target);
|
|
2047
|
-
const sin = (target[1] - source[1]) / distance;
|
|
2048
|
-
const cos = (target[0] - source[0]) / distance;
|
|
2049
|
-
return [source[0] + extendDistance * cos, source[1] + extendDistance * sin];
|
|
2050
|
-
};
|
|
2051
2054
|
// quadratic Bezier to cubic Bezier
|
|
2052
2055
|
const Q2C = (points) => {
|
|
2053
2056
|
const result = [];
|
|
@@ -2107,11 +2110,18 @@ const getSelectedImageElements = (board) => {
|
|
|
2107
2110
|
return selectedElements;
|
|
2108
2111
|
};
|
|
2109
2112
|
|
|
2113
|
+
const isTextExceedingBounds = (geometry) => {
|
|
2114
|
+
const client = getRectangleByPoints(geometry.points);
|
|
2115
|
+
if (geometry.textHeight > client.height) {
|
|
2116
|
+
return true;
|
|
2117
|
+
}
|
|
2118
|
+
return false;
|
|
2119
|
+
};
|
|
2110
2120
|
const isRectangleHitDrawElement = (board, element, selection) => {
|
|
2121
|
+
const rangeRectangle = RectangleClient.toRectangleClient([selection.anchor, selection.focus]);
|
|
2111
2122
|
if (PlaitDrawElement.isGeometry(element)) {
|
|
2112
2123
|
const client = getRectangleByPoints(element.points);
|
|
2113
|
-
|
|
2114
|
-
if (element.textHeight > client.height) {
|
|
2124
|
+
if (isTextExceedingBounds(element)) {
|
|
2115
2125
|
const textClient = getTextRectangle(element);
|
|
2116
2126
|
return RectangleClient.isHit(rangeRectangle, client) || RectangleClient.isHit(rangeRectangle, textClient);
|
|
2117
2127
|
}
|
|
@@ -2119,7 +2129,6 @@ const isRectangleHitDrawElement = (board, element, selection) => {
|
|
|
2119
2129
|
}
|
|
2120
2130
|
if (PlaitDrawElement.isImage(element)) {
|
|
2121
2131
|
const client = getRectangleByPoints(element.points);
|
|
2122
|
-
const rangeRectangle = RectangleClient.toRectangleClient([selection.anchor, selection.focus]);
|
|
2123
2132
|
return RectangleClient.isHit(rangeRectangle, client);
|
|
2124
2133
|
}
|
|
2125
2134
|
if (PlaitDrawElement.isLine(element)) {
|
|
@@ -2127,7 +2136,6 @@ const isRectangleHitDrawElement = (board, element, selection) => {
|
|
|
2127
2136
|
const strokeWidth = getStrokeWidthByElement(element);
|
|
2128
2137
|
const isHitText = isHitLineText(board, element, selection.focus);
|
|
2129
2138
|
const isHit = isHitPolyLine(points, selection.focus, strokeWidth, 3) || isHitText;
|
|
2130
|
-
const rangeRectangle = RectangleClient.toRectangleClient([selection.anchor, selection.focus]);
|
|
2131
2139
|
const isContainPolyLinePoint = points.some(point => {
|
|
2132
2140
|
return RectangleClient.isHit(rangeRectangle, RectangleClient.toRectangleClient([point, point]));
|
|
2133
2141
|
});
|
|
@@ -2139,15 +2147,24 @@ const isRectangleHitDrawElement = (board, element, selection) => {
|
|
|
2139
2147
|
const isHitDrawElement = (board, element, point) => {
|
|
2140
2148
|
if (PlaitDrawElement.isGeometry(element)) {
|
|
2141
2149
|
const fill = getFillByElement(element);
|
|
2142
|
-
|
|
2150
|
+
// when shape equals text, fill is not allowed
|
|
2151
|
+
if (fill !== DefaultGeometryStyle.fill && fill !== TRANSPARENT && !PlaitDrawElement.isText(element)) {
|
|
2143
2152
|
return isRectangleHitDrawElement(board, element, { anchor: point, focus: point });
|
|
2144
2153
|
}
|
|
2145
2154
|
else {
|
|
2155
|
+
// if shape equals text, only check text rectangle
|
|
2156
|
+
if (PlaitDrawElement.isText(element)) {
|
|
2157
|
+
const textClient = getTextRectangle(element);
|
|
2158
|
+
let isHitText = RectangleClient.isPointInRectangle(textClient, point);
|
|
2159
|
+
return isHitText;
|
|
2160
|
+
}
|
|
2146
2161
|
const strokeWidth = getStrokeWidthByElement(element);
|
|
2147
2162
|
const engine = getEngine(getShape(element));
|
|
2148
2163
|
const corners = engine.getCornerPoints(getRectangleByPoints(element.points));
|
|
2149
2164
|
const isHit = isHitPolyLine(corners, point, strokeWidth, 3);
|
|
2150
|
-
|
|
2165
|
+
const textClient = getTextRectangle(element);
|
|
2166
|
+
let isHitText = RectangleClient.isPointInRectangle(textClient, point);
|
|
2167
|
+
return isHit || isHitText;
|
|
2151
2168
|
}
|
|
2152
2169
|
}
|
|
2153
2170
|
if (PlaitDrawElement.isImage(element) || PlaitDrawElement.isLine(element)) {
|
|
@@ -3318,5 +3335,5 @@ const withDraw = (board) => {
|
|
|
3318
3335
|
* Generated bundle index. Do not edit.
|
|
3319
3336
|
*/
|
|
3320
3337
|
|
|
3321
|
-
export { BasicShapes, DEFAULT_IMAGE_WIDTH, DefaultBasicShapeProperty, DefaultConnectorProperty, DefaultDataProperty, DefaultDecisionProperty, DefaultFlowchartProperty, DefaultFlowchartPropertyMap, DefaultGeometryActiveStyle, DefaultGeometryStyle, DefaultManualInputProperty, DefaultMergeProperty, DefaultTextProperty, DrawTransforms, FlowchartSymbols, GeometryComponent, GeometryThreshold, LineComponent, LineHandleKey, LineMarkerType, LineShape, PlaitDrawElement, PlaitGeometry, PlaitLine, Q2C, ShapeDefaultSpace, StrokeStyle, createDefaultFlowchart, createGeometryElement, createLineElement, drawBoundMask, drawGeometry, drawLine, getBasicPointers, getBoardLines, getCenterPointsOnPolygon, getConnectionPoint, getCurvePoints, getDefaultFlowchartProperty, getEdgeOnPolygonByPoint, getElbowPoints,
|
|
3338
|
+
export { BasicShapes, DEFAULT_IMAGE_WIDTH, DefaultBasicShapeProperty, DefaultConnectorProperty, DefaultDataProperty, DefaultDecisionProperty, DefaultFlowchartProperty, DefaultFlowchartPropertyMap, DefaultGeometryActiveStyle, DefaultGeometryStyle, DefaultManualInputProperty, DefaultMergeProperty, DefaultTextProperty, DrawTransforms, FlowchartSymbols, GeometryComponent, GeometryThreshold, LineComponent, LineHandleKey, LineMarkerType, LineShape, PlaitDrawElement, PlaitGeometry, PlaitLine, Q2C, ShapeDefaultSpace, StrokeStyle, createDefaultFlowchart, createGeometryElement, createLineElement, drawBoundMask, drawGeometry, drawLine, getBasicPointers, getBoardLines, getCenterPointsOnPolygon, getConnectionPoint, getCurvePoints, getDefaultFlowchartProperty, getEdgeOnPolygonByPoint, getElbowPoints, getFillByElement, getFlowchartPointers, getGeometryPointers, getHitConnectorPoint, getHitLineTextIndex, getLineDashByElement, getLineHandleRefPair, getLinePointers, getLinePoints, getLineTextRectangle, getNearestPoint, getPointsByCenterPoint, getSelectedDrawElements, getSelectedGeometryElements, getSelectedImageElements, getSelectedLineElements, getStrokeColorByElement, getStrokeStyleByElement, getStrokeWidthByElement, getTextRectangle, getVectorByConnection, isHitDrawElement, isHitLineText, isHitPolyLine, isRectangleHitDrawElement, isTextExceedingBounds, transformOpsToPoints, transformPointToConnection, withDraw };
|
|
3322
3339
|
//# sourceMappingURL=plait-draw.mjs.map
|