Просмотр исходного кода

wip: include arrow heads to bounds

Konstantinos Kaloutas 3 лет назад
Родитель
Сommit
476b221c51
1 измененных файлов с 37 добавлено и 8 удалено
  1. 37 8
      tldraw/apps/tldraw-logseq/src/lib/shapes/LineShape.tsx

+ 37 - 8
tldraw/apps/tldraw-logseq/src/lib/shapes/LineShape.tsx

@@ -1,15 +1,17 @@
 /* eslint-disable @typescript-eslint/no-explicit-any */
-import { Decoration, TLLineShape, TLLineShapeProps, getComputedColor } from '@tldraw/core'
+import { Decoration, TLLineShape, TLLineShapeProps, getComputedColor, BoundsUtils, TLPolylineShape } from '@tldraw/core'
 import { SVGContainer, TLComponentProps } from '@tldraw/react'
 import Vec from '@tldraw/vec'
 import { observer } from 'mobx-react-lite'
 import * as React from 'react'
 import { Arrow } from './arrow/Arrow'
+import { getStraightArrowHeadPoints } from './arrow/arrowHelpers'
 import { getArrowPath } from './arrow/arrowHelpers'
 import { CustomStyleProps, withClampedStyles } from './style-props'
 import { getTextLabelSize } from '@tldraw/core'
 import { LabelMask } from './text/LabelMask'
 import { TextLabel } from './text/TextLabel'
+import type { TLBounds } from '@tldraw/intersect'
 
 interface LineShapeProps extends CustomStyleProps, TLLineShapeProps {
   type: 'line'
@@ -104,13 +106,14 @@ export class LineShape extends TLLineShape<LineShapeProps> {
 
   ReactIndicator = observer(() => {
     const {
-      id,
-      decorations,
-      label,
-      strokeWidth,
-      handles: { start, end },
-    } = this.props
-    const bounds = this.getBounds()
+      props: {
+        id,
+        decorations,
+        label,
+        strokeWidth,
+        handles: { start, end },}
+    } = this
+    let bounds = this.getBounds()
     const labelSize = label ? getTextLabelSize(label, font, 4) : [0, 0]
     const midPoint = Vec.med(start.point, end.point)
     const dist = Vec.dist(start.point, end.point)
@@ -153,6 +156,32 @@ export class LineShape extends TLLineShape<LineShapeProps> {
     return withClampedStyles(this, props)
   }
 
+  getBounds = (): TLBounds => {
+    const {
+      point,
+      strokeWidth,
+      handles: { start, end },
+      decorations
+    } = this.props
+
+    let { points } = this
+
+    const arrowDist = Vec.dist(start.point, end.point)
+    const arrowHeadLength = Math.min(arrowDist / 3, strokeWidth * 16)
+
+    if (decorations?.start) {
+      const headPoints = getStraightArrowHeadPoints(start.point, end.point, arrowHeadLength)
+      points.push(headPoints.left, headPoints.right)
+    }
+
+    if (decorations?.end) {
+      const headPoints = getStraightArrowHeadPoints(end.point, start.point, arrowHeadLength)
+      points.push(headPoints.left, headPoints.right)
+    }
+
+    return BoundsUtils.translateBounds(BoundsUtils.getBoundsFromPoints(points), point)
+  }
+
   getShapeSVGJsx({ preview }: any) {
     const {
       stroke,