Skip to content

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

3.7 KB
Raw Blame
import { isEqual } from 'es-toolkit/predicate'

import type { SceneNode, StyleRun, TextParagraphStyle } from '@open-pencil/scene-graph'
import { copyDerivedGlyphs, copyGeometryPaths, copyStyleRuns } from '@open-pencil/scene-graph/copy'

import type { TextLayout } from '#core/canvas/text/layout'

export type TextEditSizeSnapshot = Partial<Pick<SceneNode, 'width' | 'height'>>

export type TextEditSnapshot = {
  text: string
  styleRuns: StyleRun[]
  textParagraphs: TextParagraphStyle[]
  size?: TextEditSizeSnapshot
}

export type TextEditSession = {
  nodeId: string
  before: TextEditSnapshot
  beforePathText: Pick<
    SceneNode,
    'derivedTextGlyphs' | 'strokeGeometry' | 'textPathData' | 'textPathBox'
  > | null
}

export function createTextEditSession(node: SceneNode): TextEditSession {
  return {
    nodeId: node.id,
    before: {
      text: node.text,
      styleRuns: copyStyleRuns(node.styleRuns),
      textParagraphs: structuredClone(node.textParagraphs),
      size: { width: node.width, height: node.height }
    },
    beforePathText: node.textPathData
      ? {
          derivedTextGlyphs: copyDerivedGlyphs(node.derivedTextGlyphs),
          strokeGeometry: copyGeometryPaths(node.strokeGeometry),
          textPathData: structuredClone(node.textPathData),
          textPathBox: node.textPathBox ? { ...node.textPathBox } : null
        }
      : null
  }
}

export function snapshotTextNode(node: SceneNode | undefined, fallbackText = ''): TextEditSnapshot {
  return {
    text: node?.text ?? fallbackText,
    styleRuns: node ? copyStyleRuns(node.styleRuns) : [],
    textParagraphs: node ? structuredClone(node.textParagraphs) : [],
    size: node ? { width: node.width, height: node.height } : undefined
  }
}

export function resizeTextNodeForEdit(
  node: SceneNode | undefined,
  paragraph: TextLayout | null
): TextEditSizeSnapshot {
  if (!node || !paragraph) return {}
  const changes: TextEditSizeSnapshot = {}
  if (node.textAutoResize === 'WIDTH_AND_HEIGHT') {
    const width = Math.ceil(paragraph.getLongestLine())
    if (width > 0 && width !== node.width) changes.width = width
  }
  if (node.textAutoResize === 'HEIGHT' || node.textAutoResize === 'WIDTH_AND_HEIGHT') {
    const height = Math.ceil(paragraph.getHeight())
    if (height > 0 && height !== node.height) changes.height = height
  }
  return changes
}

export function textSnapshotChanged(before: TextEditSnapshot, after: TextEditSnapshot): boolean {
  return (
    before.text !== after.text ||
    !styleRunsEqual(before.styleRuns, after.styleRuns) ||
    !isEqual(before.textParagraphs, after.textParagraphs) ||
    (after.size !== undefined && !sizeEqual(before.size ?? {}, after.size))
  )
}

function sizeEqual(a: TextEditSizeSnapshot, b: TextEditSizeSnapshot): boolean {
  return a.width === b.width && a.height === b.height
}

function styleRunsEqual(a: StyleRun[], b: StyleRun[]): boolean {
  if (a.length !== b.length) return false
  return a.every((run, index) => styleRunEqual(run, b[index]))
}

function styleRunEqual(a: StyleRun, b: StyleRun): boolean {
  return a.start === b.start && a.length === b.length && styleEqual(a.style, b.style)
}

function styleEqual(a: StyleRun['style'], b: StyleRun['style']): boolean {
  return (
    a.fontWeight === b.fontWeight &&
    a.italic === b.italic &&
    a.textDecoration === b.textDecoration &&
    a.fontSize === b.fontSize &&
    a.fontFamily === b.fontFamily &&
    isEqual(a.letterSpacing, b.letterSpacing) &&
    isEqual(a.lineHeight, b.lineHeight) &&
    fillsEqual(a.fills ?? [], b.fills ?? [])
  )
}

function fillsEqual(
  a: NonNullable<StyleRun['style']['fills']>,
  b: NonNullable<StyleRun['style']['fills']>
) {
  if (a.length !== b.length) return false
  return a.every((fill, index) => isEqual(fill, b[index]))
}