Skip to content

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

2.0 KB
Raw Blame
import { textCacheInvalidationChanges } from './text/picture'
import type { SceneNode } from './types'
import { normalizeVectorNetwork } from './vector-network'

export type NodePreviewObserver = (node: SceneNode, changes: Partial<SceneNode>) => void

type PreviewGraph = {
  nodes: Map<string, SceneNode>
  positionPreviewVersion: number
  clearAbsPosCache: () => void
}

const LAYOUT_AFFECTING_KEYS = new Set<string>([
  'x',
  'y',
  'width',
  'height',
  'rotation',
  'flipX',
  'flipY',
  'parentId',
  'childIds',
  'layoutMode',
  'layoutDirection',
  'layoutWrap',
  'primaryAxisSizing',
  'counterAxisSizing',
  'itemSpacing',
  'counterAxisSpacing',
  'paddingTop',
  'paddingRight',
  'paddingBottom',
  'paddingLeft',
  'layoutGrow',
  'layoutAlignSelf',
  'layoutPositioning',
  'minWidth',
  'maxWidth',
  'minHeight',
  'maxHeight',
  'visible',
  'text',
  'fontSize',
  'lineHeight',
  'letterSpacing',
  'styleRuns',
  'textAutoResize'
])

export function updateNodePreview(
  graph: PreviewGraph,
  id: string,
  changes: Partial<SceneNode>,
  beforeUpdate?: NodePreviewObserver
): Partial<SceneNode> | null {
  const node = graph.nodes.get(id)
  if (!node) return null
  changes = Object.fromEntries(
    (Object.entries(changes) as Array<[string, unknown]>).filter(([, value]) => value !== undefined)
  )
  if ((Object.keys(changes) as (keyof SceneNode)[]).every((key) => node[key] === changes[key])) {
    return null
  }
  const affectsLayout = Object.keys(changes).some((key) => LAYOUT_AFFECTING_KEYS.has(key))
  if (affectsLayout) graph.clearAbsPosCache()
  let normalizedChanges = changes
  if (node.type === 'TEXT') {
    normalizedChanges = { ...textCacheInvalidationChanges(node, changes), ...changes }
  }
  if (changes.vectorNetwork) {
    normalizedChanges = {
      ...normalizedChanges,
      vectorNetwork: normalizeVectorNetwork(changes.vectorNetwork)
    }
  }
  beforeUpdate?.(node, normalizedChanges)
  graph.positionPreviewVersion++
  Object.assign(node, normalizedChanges)
  return normalizedChanges
}