import classNames from 'classnames'
import * as React from 'react'
import {
type GapsSnapIndicator,
type PointsSnapIndicator,
type SnapIndicator,
} from '../../editor/managers/SnapManager/SnapManager'
import { rangeIntersection } from '../../primitives/utils'
function PointsSnapIndicator({ points, zoom }: { zoom: number } & PointsSnapIndicator) {
const l = 2.5 / zoom
const minX = points.reduce((acc, p) => Math.min(acc, p.x), Infinity)
const maxX = points.reduce((acc, p) => Math.max(acc, p.x), -Infinity)
const minY = points.reduce((acc, p) => Math.min(acc, p.y), Infinity)
const maxY = points.reduce((acc, p) => Math.max(acc, p.y), -Infinity)
const useNWtoSEdireciton = points.some((p) => p.x === minX && p.y === minY)
let firstX: number, firstY: number, secondX: number, secondY: number
if (useNWtoSEdireciton) {
firstX = minX
firstY = minY
secondX = maxX
secondY = maxY
} else {
firstX = minX
firstY = maxY
secondX = maxX
secondY = minY
}
return (
{points.map((p, i) => (
))}
)
}
function GapsSnapIndicator({ gaps, direction, zoom }: { zoom: number } & GapsSnapIndicator) {
const l = 3.5 / zoom
let edgeIntersection: number[] | null = [-Infinity, +Infinity]
let nextEdgeIntersection: number[] | null = null
const horizontal = direction === 'horizontal'
// find intersection of all gaps so we can render a straight line through it;
// some range intersections may return null, in which case we skip that gap.
for (const gap of gaps) {
nextEdgeIntersection = rangeIntersection(
edgeIntersection[0],
edgeIntersection[1],
horizontal ? gap.startEdge[0].y : gap.startEdge[0].x,
horizontal ? gap.startEdge[1].y : gap.startEdge[1].x
)
if (nextEdgeIntersection) {
edgeIntersection = nextEdgeIntersection
} else {
continue
}
nextEdgeIntersection = rangeIntersection(
edgeIntersection[0],
edgeIntersection[1],
horizontal ? gap.endEdge[0].y : gap.endEdge[0].x,
horizontal ? gap.endEdge[1].y : gap.endEdge[1].x
)
if (nextEdgeIntersection) {
edgeIntersection = nextEdgeIntersection
} else {
continue
}
}
if (edgeIntersection === null) {
return null
}
const midPoint = (edgeIntersection[0] + edgeIntersection[1]) / 2
return (
{gaps.map(({ startEdge, endEdge }, i) => (
{horizontal ? (
// horizontal gap
<>
{/* start edge */}
{/* end edge */}
{/* joining line */}
{/* center point marker */}
>
) : (
// vertical gap
<>
{/* start edge */}
{/* end edge */}
{/* joining line */}
{/* center point marker */}
>
)}
))}
)
}
/** @public */
export interface TLSnapIndicatorProps {
className?: string
line: SnapIndicator
zoom: number
}
/** @public @react */
export function DefaultSnapIndicator({ className, line, zoom }: TLSnapIndicatorProps) {
return (
)
}