Skip to content

AI-native design editor. Open-source Figma alternative.

2.8 KB
Raw Blame
import {
  node,
  type BaseProps,
  type EllipseProps,
  type VectorProps,
  type ComponentProps,
  type InstanceProps,
  type ParagraphProps,
  type SpanProps,
  type TextProps,
  type TreeNode
} from '../tree'

type Child = TreeNode | string

function withChildren(type: string, props: Record<string, unknown>, children: Child[]): TreeNode {
  if (children.length > 0) {
    return node(type, { ...props, children })
  }
  return node(type, props)
}

export function Frame(props: BaseProps, ...children: Child[]): TreeNode {
  return withChildren('frame', props, children)
}

export function Text(props: TextProps, ...children: Child[]): TreeNode {
  return withChildren('text', props, children)
}

export function Span(props: SpanProps, ...children: Child[]): TreeNode {
  return withChildren('text-span', props, children)
}

export function Paragraph(props: ParagraphProps, ...children: Child[]): TreeNode {
  return withChildren('text-paragraph', props, children)
}

export function Rectangle(props: BaseProps, ...children: Child[]): TreeNode {
  return withChildren('rectangle', props, children)
}

export function Ellipse(props: BaseProps & EllipseProps, ...children: Child[]): TreeNode {
  return withChildren('ellipse', props, children)
}

export function Line(props: BaseProps, ...children: Child[]): TreeNode {
  return withChildren('line', props, children)
}

export function Star(
  props: BaseProps & { points?: number; innerRadius?: number },
  ...children: Child[]
): TreeNode {
  return withChildren('star', props, children)
}

export function Polygon(
  props: BaseProps & { pointCount?: number },
  ...children: Child[]
): TreeNode {
  return withChildren('polygon', props, children)
}

export function Vector(props: BaseProps & VectorProps, ...children: Child[]): TreeNode {
  return withChildren('vector', props, children)
}

export function Group(props: BaseProps, ...children: Child[]): TreeNode {
  return withChildren('group', props, children)
}

export function Section(props: BaseProps, ...children: Child[]): TreeNode {
  return withChildren('section', props, children)
}

export function Component(props: ComponentProps, ...children: Child[]): TreeNode {
  return withChildren('component', props, children)
}

export function ComponentSet(props: ComponentProps, ...children: Child[]): TreeNode {
  return withChildren('component-set', props, children)
}

export function Instance(props: InstanceProps, ...children: Child[]): TreeNode {
  return withChildren('instance', props, children)
}

export const View = Frame
export const Rect = Rectangle
export const Page = Frame

export const INTRINSIC_ELEMENTS = [
  'frame',
  'text',
  'text-span',
  'text-paragraph',
  'rectangle',
  'ellipse',
  'line',
  'star',
  'polygon',
  'vector',
  'group',
  'section',
  'component',
  'component-set',
  'instance'
] as const