Skip to content

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

2.8 KB
Raw Blame
import type { SceneGraph, SceneNode } from '@open-pencil/scene-graph'
import { referencedStyleIds } from '@open-pencil/scene-graph/shared-styles'

import { clipboardPlacement, type ClipboardPlacement } from './placement'
import { captureClipboardVariables, type ClipboardVariables } from './variables'

export type ClipboardNodeTree = SceneNode & { children?: ClipboardNodeTree[] }

export interface ClipboardSnapshot {
  sourceRootId: string
  componentDependencies: ClipboardNodeTree[]
  styleDefinitions: SceneNode[]
  variableDependencies: ClipboardVariables
  nodes: ClipboardNodeTree[]
  /** Where the copied layers sat on the canvas when they were copied. */
  placement: ClipboardPlacement
  images: Map<string, Uint8Array>
  /** Written by Copy properties: Paste skips it and Paste properties reads it, as in Figma. */
  properties?: boolean
}

export function captureClipboardSnapshot(
  graph: SceneGraph,
  selectedNodes: SceneNode[]
): ClipboardSnapshot {
  const selectedIds = new Set(selectedNodes.map((node) => node.id))
  const roots = selectedNodes.filter((node) => !node.parentId || !selectedIds.has(node.parentId))
  const images = new Map<string, Uint8Array>()
  const capturedNodes: SceneNode[] = []
  function capture(node: SceneNode): ClipboardNodeTree {
    capturedNodes.push(node)
    for (const paint of node.fills) {
      if (!paint.imageHash) continue
      const bytes = graph.images.get(paint.imageHash)
      if (bytes) images.set(paint.imageHash, bytes.slice())
    }
    return { ...structuredClone(node), children: graph.getChildren(node.id).map(capture) }
  }
  const nodes = roots.map(capture)
  const componentDependencies = new Map<string, ClipboardNodeTree>()
  function captureComponent(id: string) {
    if (componentDependencies.has(id) || selectedIds.has(id)) return
    const component = graph.getNode(id)
    if (!component || (component.type !== 'COMPONENT' && component.type !== 'COMPONENT_SET')) return
    const tree = capture(component)
    componentDependencies.set(id, tree)
    visitReferences(tree)
  }
  function visitReferences(node: ClipboardNodeTree) {
    if (node.componentId) captureComponent(node.componentId)
    for (const child of node.children ?? []) visitReferences(child)
  }
  for (const node of nodes) visitReferences(node)
  const styleIds = new Set(capturedNodes.flatMap(referencedStyleIds))
  const styleDefinitions: SceneNode[] = []
  for (const node of graph.getAllNodes()) {
    if (node.source.id && styleIds.has(node.source.id)) styleDefinitions.push(structuredClone(node))
  }
  return {
    sourceRootId: graph.rootId,
    componentDependencies: [...componentDependencies.values()],
    styleDefinitions,
    variableDependencies: captureClipboardVariables(graph, capturedNodes),
    nodes,
    placement: clipboardPlacement(graph, roots),
    images
  }
}