AI-native design editor. Open-source Figma alternative.
import { isEqual } from 'es-toolkit'
import type { Rect } from '@open-pencil/scene-graph/primitives'
import type { SnapGuide } from '@open-pencil/scene-graph/snap'
import type { GuidePreview } from '#core/canvas/guides/types'
import type { ConnectorTarget } from '#core/editor/connectors/attach'
import type { ShapeHandleHover, EditorContext } from '#core/editor/types'
export function createSelectionOverlayActions(ctx: EditorContext) {
function setMarquee(rect: Rect | null) {
ctx.state.marquee = rect
ctx.requestRepaint()
}
function setSnapGuides(guides: SnapGuide[]) {
ctx.state.snapGuides = guides
ctx.requestRepaint()
}
function setGuidePreview(preview: GuidePreview | null) {
ctx.state.guides.preview = preview
ctx.requestRepaint()
}
function setHoveredGuide(selection: typeof ctx.state.guides.hovered) {
const current = ctx.state.guides.hovered
if (current?.ownerId === selection?.ownerId && current?.guideId === selection?.guideId) return
ctx.state.guides.hovered = selection
ctx.requestRepaint()
}
function setGuideRedline(redline: typeof ctx.state.guides.redline) {
ctx.state.guides.redline = redline
ctx.requestRepaint()
}
function setSelectedGuide(selection: typeof ctx.state.guides.selected) {
ctx.state.guides.selected = selection
if (selection) ctx.setSelectedIds(new Set())
ctx.requestRepaint()
}
function setRotationPreview(preview: typeof ctx.state.rotationPreview) {
if (preview === null && ctx.state.rotationPreview === null) return
ctx.state.rotationPreview = preview
ctx.emitEditorEvent('rotation:preview-changed', preview)
ctx.requestRepaint()
}
function setHoveredNode(id: string | null) {
if (ctx.state.hoveredNodeId === id) return
ctx.state.hoveredNodeId = id
ctx.requestRepaint()
}
function setTransforming(transforming: boolean) {
if (ctx.state.transforming === transforming) return
ctx.state.transforming = transforming
ctx.requestRepaint()
}
/** Marks the layer of the code element around the cursor, separately from canvas hover. */
function setCodeFocusNode(id: string | null) {
if (ctx.state.codeFocusNodeId === id) return
ctx.state.codeFocusNodeId = id
ctx.requestRepaint()
}
function setMeasurementMode(mode: typeof ctx.state.measurementMode) {
if (ctx.state.measurementMode === mode) return
ctx.state.measurementMode = mode
ctx.requestRepaint()
}
/** Hands a layer's name on the canvas to the field renaming it, or back to the canvas. */
function setRenamingLabel(id: string | null) {
if (ctx.state.renamingLabelId === id) return
ctx.state.renamingLabelId = id
ctx.requestRepaint()
}
function setDropTarget(id: string | null) {
if (ctx.state.dropTargetId === id) return
ctx.state.dropTargetId = id
ctx.requestRepaint()
}
function setLayoutInsertIndicator(indicator: typeof ctx.state.layoutInsertIndicator) {
if (ctx.state.layoutInsertIndicator === indicator) return
ctx.state.layoutInsertIndicator = indicator
ctx.requestRepaint()
}
function setAutoLayoutHover(hover: typeof ctx.state.autoLayoutHover) {
const current = ctx.state.autoLayoutHover
if (
current?.nodeId === hover?.nodeId &&
current?.kind === hover?.kind &&
current?.index === hover?.index &&
current?.side === hover?.side
) {
return
}
ctx.state.autoLayoutHover = hover
ctx.requestRepaint()
}
function setConnectorTarget(target: ConnectorTarget | null) {
if (isEqual(ctx.state.connectorTarget, target)) return
ctx.state.connectorTarget = target
ctx.requestRepaint()
}
function setShapeHandleHover(hover: ShapeHandleHover | null) {
if (isEqual(ctx.state.shapeHandleHover, hover)) return
ctx.state.shapeHandleHover = hover
ctx.requestRepaint()
}
return {
setConnectorTarget,
setMarquee,
setSnapGuides,
setGuidePreview,
setHoveredGuide,
setGuideRedline,
setSelectedGuide,
setRotationPreview,
setHoveredNode,
setTransforming,
setCodeFocusNode,
setMeasurementMode,
setDropTarget,
setRenamingLabel,
setLayoutInsertIndicator,
setAutoLayoutHover,
setShapeHandleHover
}
}