Skip to content

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

Fix Figma clipboard paste: broken layout and missing properties

Danila Poyarkov22e9553b3087
diff --git a/packages/core/src/clipboard.ts b/packages/core/src/clipboard.ts
index 6509392ee..d5ff7f2ad 100644
--- a/packages/core/src/clipboard.ts
+++ b/packages/core/src/clipboard.ts
@@ -1,7 +1,5 @@
import { inflateSync, deflateSync } from 'fflate'
-import { BLACK } from './constants'
-import { styleToWeight } from './fonts'
import {
sceneNodeToKiwi,
buildFigKiwi,
@@ -10,20 +8,10 @@ import {
} from './kiwi-serialize'
import { initCodec, getCompiledSchema, getSchemaBytes } from './kiwi/codec'
import { decodeBinarySchema, compileSchema, ByteBuffer } from './kiwi/kiwi-schema'
-import { decodeVectorNetworkBlob } from './vector'
+import { nodeChangeToProps } from './kiwi/kiwi-convert'
import type { NodeChange as KiwiNodeChange } from './kiwi/codec'
-import type {
- SceneGraph,
- SceneNode,
- Fill,
- Stroke,
- LayoutMode,
- LayoutSizing,
- LayoutAlign,
- LayoutCounterAlign,
- VectorNetwork
-} from './scene-graph'
+import type { SceneGraph, SceneNode } from './scene-graph'
interface FigmaClipboardMeta {
fileKey: string
@@ -83,27 +71,6 @@ export async function parseFigmaClipboard(
return { nodes: msg.nodeChanges ?? [], meta, blobs }
}
-function decodeVectorData(nc: KiwiNodeChange, blobs: Uint8Array[]): VectorNetwork | null {
- const vectorData = nc.vectorData as
- | {
- vectorNetworkBlob?: number
- normalizedSize?: { x: number; y: number }
- styleOverrideTable?: Array<{ styleID: number; handleMirroring?: string }>
- }
- | undefined
-
- if (!vectorData || vectorData.vectorNetworkBlob === undefined) return null
-
- const blobIdx = vectorData.vectorNetworkBlob
- if (blobIdx < 0 || blobIdx >= blobs.length) return null
-
- try {
- return decodeVectorNetworkBlob(blobs[blobIdx], vectorData.styleOverrideTable)
- } catch {
- return null
- }
-}
-
const NON_VISUAL_TYPES = new Set([
'DOCUMENT',
'CANVAS',
@@ -202,96 +169,15 @@ export function importClipboardNodes(
const nc = guidMap.get(figmaId)
if (!nc) return
- const x = (nc.transform?.m02 ?? 0) + (ourParentId === targetParentId ? offsetX : 0)
- const y = (nc.transform?.m12 ?? 0) + (ourParentId === targetParentId ? offsetY : 0)
+ const { nodeType, ...props } = nodeChangeToProps(nc, blobs)
+ if (nodeType === 'DOCUMENT' || nodeType === 'VARIABLE') return
- let rotation = 0
- if (nc.transform) {
- rotation = Math.atan2(nc.transform.m10, nc.transform.m00) * (180 / Math.PI)
+ if (ourParentId === targetParentId) {
+ props.x = (props.x ?? 0) + offsetX
+ props.y = (props.y ?? 0) + offsetY
}
- const fills: Fill[] = (nc.fillPaints ?? [])
- .filter((p) => p.type === 'SOLID' && p.color)
- .map((p) => ({
- type: 'SOLID' as const,
- color: p.color ?? { ...BLACK },
- opacity: p.opacity ?? 1,
- visible: p.visible ?? true
- }))
-
- const strokes: Stroke[] = (nc.strokePaints ?? [])
- .filter((p) => p.type === 'SOLID' && p.color)
- .map((p) => ({
- color: p.color ?? { ...BLACK },
- weight: nc.strokeWeight ?? 1,
- opacity: p.opacity ?? 1,
- visible: p.visible ?? true,
- align: 'CENTER' as const
- }))
-
- const nodeType = mapNodeType(nc.type)
- const node = graph.createNode(nodeType, ourParentId, {
- name: nc.name ?? nodeType,
- x,
- y,
- width: nc.size?.x ?? 100,
- height: nc.size?.y ?? 100,
- rotation,
- opacity: nc.opacity ?? 1,
- visible: nc.visible ?? true,
- fills,
- strokes,
- cornerRadius: nc.cornerRadius ?? 0,
- independentCorners: nc.rectangleCornerRadiiIndependent ?? false,
- topLeftRadius: nc.rectangleTopLeftCornerRadius ?? 0,
- topRightRadius: nc.rectangleTopRightCornerRadius ?? 0,
- bottomLeftRadius: nc.rectangleBottomLeftCornerRadius ?? 0,
- bottomRightRadius: nc.rectangleBottomRightCornerRadius ?? 0,
- text: nc.textData?.characters ?? '',
- fontSize: nc.fontSize ?? 14,
- fontFamily: nc.fontName?.family ?? 'Inter',
- textAlignHorizontal:
- (nc.textAlignHorizontal as 'LEFT' | 'CENTER' | 'RIGHT' | 'JUSTIFIED') ?? 'LEFT',
- layoutMode: mapLayoutMode(nc.stackMode as string),
- itemSpacing: (nc.stackSpacing as number) ?? 0,
- paddingTop: (nc.stackVerticalPadding as number) ?? (nc.stackPadding as number) ?? 0,
- paddingBottom:
- (nc.stackPaddingBottom as number) ??
- (nc.stackVerticalPadding as number) ??
- (nc.stackPadding as number) ??
- 0,
- paddingLeft: (nc.stackHorizontalPadding as number) ?? (nc.stackPadding as number) ?? 0,
- paddingRight:
- (nc.stackPaddingRight as number) ??
- (nc.stackHorizontalPadding as number) ??
- (nc.stackPadding as number) ??
- 0,
- primaryAxisSizing: mapSizing(nc.stackPrimarySizing as string),
- counterAxisSizing: mapSizing(nc.stackCounterSizing as string),
- primaryAxisAlign: mapPrimaryAlign(
- (nc.stackPrimaryAlignItems as string) ?? (nc.stackJustify as string)
- ),
- counterAxisAlign: mapCounterAlign(
- (nc.stackCounterAlignItems as string) ?? (nc.stackCounterAlign as string)
- ),
- layoutWrap: (nc.stackWrap as string) === 'WRAP' ? ('WRAP' as const) : ('NO_WRAP' as const),
- counterAxisSpacing: (nc.stackCounterSpacing as number) ?? 0,
- layoutPositioning:
- (nc.stackPositioning as string) === 'ABSOLUTE' ? ('ABSOLUTE' as const) : ('AUTO' as const),
- layoutGrow: (nc.stackChildPrimaryGrow as number) ?? 0,
- layoutAlignSelf:
- (nc.stackChildAlignSelf as string) === 'STRETCH' ? ('STRETCH' as const) : ('AUTO' as const),
- clipsContent: nc.frameMaskDisabled === false,
- textAutoResize: 'NONE' as const,
- fontWeight: nc.fontWeight ?? styleToWeight(nc.fontName?.style ?? ''),
- italic: nc.fontName?.style?.toLowerCase().includes('italic') ?? false,
- lineHeight: mapLineHeight(nc.lineHeight as { value: number; units: string } | undefined),
- letterSpacing: mapLetterSpacing(
- nc.letterSpacing as { value: number; units: string } | undefined,
- nc.fontSize as number | undefined
- ),
- vectorNetwork: decodeVectorData(nc, blobs)
- })
+ const node = graph.createNode(nodeType, ourParentId, props)
created.set(figmaId, node.id)
if (ourParentId === targetParentId) createdIds.push(node.id)
@@ -319,82 +205,6 @@ export function importClipboardNodes(
return createdIds
}
-function mapLayoutMode(mode?: string): LayoutMode {
- if (mode === 'HORIZONTAL') return 'HORIZONTAL'
- if (mode === 'VERTICAL') return 'VERTICAL'
- return 'NONE'
-}
-
-function mapSizing(sizing?: string): LayoutSizing {
- if (sizing === 'RESIZE_TO_FIT' || sizing === 'RESIZE_TO_FIT_WITH_IMPLICIT_SIZE') return 'HUG'
- if (sizing === 'FILL') return 'FILL'
- return 'FIXED'
-}
-
-function mapPrimaryAlign(align?: string): LayoutAlign {
- if (align === 'CENTER') return 'CENTER'
- if (align === 'MAX') return 'MAX'
- if (align === 'SPACE_BETWEEN' || align === 'SPACE_EVENLY') return 'SPACE_BETWEEN'
- return 'MIN'
-}
-
-function mapCounterAlign(align?: string): LayoutCounterAlign {
- if (align === 'CENTER') return 'CENTER'
- if (align === 'MAX') return 'MAX'
- if (align === 'STRETCH') return 'STRETCH'
- if (align === 'BASELINE') return 'BASELINE'
- return 'MIN'
-}
-
-function mapLetterSpacing(ls?: { value: number; units: string }, fontSize?: number): number {
- if (!ls) return 0
- if (ls.units === 'PIXELS') return ls.value
- if (ls.units === 'PERCENT') return (ls.value / 100) * (fontSize ?? 14)
- return 0
-}
-
-function mapLineHeight(lh?: { value: number; units: string }): number | undefined {
- if (!lh) return undefined
- if (lh.units === 'PIXELS') return lh.value
- if (lh.units === 'PERCENT') return undefined
- return undefined
-}
-
-function mapNodeType(type?: string): SceneNode['type'] {
- switch (type) {
- case 'FRAME':
- return 'FRAME'
- case 'COMPONENT':
- return 'COMPONENT'
- case 'COMPONENT_SET':
- return 'COMPONENT_SET'
- case 'INSTANCE':
- return 'INSTANCE'
- case 'RECTANGLE':
- case 'ROUNDED_RECTANGLE':
- return 'RECTANGLE'
- case 'ELLIPSE':
- return 'ELLIPSE'
- case 'TEXT':
- return 'TEXT'
- case 'LINE':
- return 'LINE'
- case 'STAR':
- return 'STAR'
- case 'REGULAR_POLYGON':
- return 'POLYGON'
- case 'VECTOR':
- case 'BOOLEAN_OPERATION':
- return 'VECTOR'
- case 'GROUP':
- return 'GROUP'
- case 'SECTION':
- return 'SECTION'
- default:
- return 'RECTANGLE'
- }
-}
-
export function buildFigmaClipboardHTML(nodes: SceneNode[], graph: SceneGraph): string | null {
const compiled = getCompiledSchema()
const schemaDeflated = deflateSync(getSchemaBytes())
diff --git a/packages/core/src/kiwi/fig-import.ts b/packages/core/src/kiwi/fig-import.ts
index 9f79c4915..3dc9d2a07 100644
--- a/packages/core/src/kiwi/fig-import.ts
+++ b/packages/core/src/kiwi/fig-import.ts
@@ -1,346 +1,8 @@
-import { BLACK, DEFAULT_STROKE_MITER_LIMIT } from '../constants'
-import { styleToWeight } from '../fonts'
import { SceneGraph } from '../scene-graph'
-import { decodeVectorNetworkBlob } from '../vector'
-import type {
- NodeType,
- Fill,
- FillType,
- Stroke,
- Effect,
- Color,
- BlendMode,
- ImageScaleMode,
- GradientTransform,
- StrokeCap,
- StrokeJoin,
- LayoutMode,
- LayoutSizing,
- LayoutAlign,
- LayoutCounterAlign,
- ConstraintType,
- TextAutoResize,
- TextAlignVertical,
- TextCase,
- TextDecoration,
- ArcData,
- VectorNetwork,
- StyleRun,
- CharacterStyleOverride
-} from '../scene-graph'
-import type { NodeChange, Paint, Effect as KiwiEffect, GUID } from './codec'
+import { guidToString, nodeChangeToProps } from './kiwi-convert'
-function ext(nc: NodeChange): Record<string, unknown> {
- return nc as unknown as Record<string, unknown>
-}
-
-function guidToString(guid: GUID): string {
- return `${guid.sessionID}:${guid.localID}`
-}
-
-function convertColor(color?: { r: number; g: number; b: number; a: number }): Color {
- if (!color) return { ...BLACK }
- return { r: color.r, g: color.g, b: color.b, a: color.a }
-}
-
-function imageHashToString(hash: Record<string, number>): string {
- const bytes = Object.keys(hash)
- .sort((a, b) => Number(a) - Number(b))
- .map((k) => hash[Number(k)])
- return bytes.map((b) => b.toString(16).padStart(2, '0')).join('')
-}
-
-function convertGradientTransform(t?: {
- m00: number
- m01: number
- m02: number
- m10: number
- m11: number
- m12: number
-}): GradientTransform | undefined {
- if (!t) return undefined
- return { m00: t.m00, m01: t.m01, m02: t.m02, m10: t.m10, m11: t.m11, m12: t.m12 }
-}
-
-function convertFills(paints?: Paint[]): Fill[] {
- if (!paints) return []
- return paints.map((p) => {
- const base: Fill = {
- type: (p.type ?? 'SOLID') as FillType,
- color: convertColor(p.color),
- opacity: p.opacity ?? 1,
- visible: p.visible ?? true,
- blendMode: (p.blendMode ?? 'NORMAL') as BlendMode
- }
-
- if (p.type?.startsWith('GRADIENT') && p.stops) {
- base.gradientStops = p.stops.map((s) => ({
- color: convertColor(s.color),
- position: s.position
- }))
- if (p.transform) {
- base.gradientTransform = convertGradientTransform(p.transform)
- }
- }
-
- if (p.type === 'IMAGE') {
- if (p.image && typeof p.image === 'object') {
- const img = p.image as { hash: string | Record<string, number> }
- if (typeof img.hash === 'object') {
- base.imageHash = imageHashToString(img.hash)
- } else if (typeof img.hash === 'string') {
- base.imageHash = img.hash
- }
- }
- base.imageScaleMode = (p.imageScaleMode as ImageScaleMode) ?? 'FILL'
- if (p.transform) {
- base.imageTransform = convertGradientTransform(p.transform)
- }
- }
-
- return base
- })
-}
-
-function convertStrokes(
- paints?: Paint[],
- weight?: number,
- align?: string,
- cap?: string,
- join?: string,
- dashPattern?: number[]
-): Stroke[] {
- if (!paints) return []
- return paints.map((p) => ({
- color: convertColor(p.color),
- weight: weight ?? 1,
- opacity: p.opacity ?? 1,
- visible: p.visible ?? true,
- align: (align === 'INSIDE'
- ? 'INSIDE'
- : align === 'OUTSIDE'
- ? 'OUTSIDE'
- : 'CENTER') as Stroke['align'],
- cap: (cap ?? 'NONE') as StrokeCap,
- join: (join ?? 'MITER') as StrokeJoin,
- dashPattern: dashPattern ?? []
- }))
-}
-
-function convertEffects(effects?: KiwiEffect[]): Effect[] {
- if (!effects) return []
- return effects.map((e) => ({
- type: e.type as Effect['type'],
- color: convertColor(e.color),
- offset: e.offset ?? { x: 0, y: 0 },
- radius: e.radius ?? 0,
- spread: e.spread ?? 0,
- visible: e.visible ?? true,
- blendMode: (e.blendMode as BlendMode) ?? 'NORMAL'
- }))
-}
-
-function mapNodeType(type?: string): NodeType | 'DOCUMENT' | 'VARIABLE' {
- switch (type) {
- case 'DOCUMENT':
- return 'DOCUMENT'
- case 'VARIABLE':
- return 'VARIABLE'
- case 'CANVAS':
- return 'CANVAS'
- case 'FRAME':
- return 'FRAME'
- case 'RECTANGLE':
- return 'RECTANGLE'
- case 'ROUNDED_RECTANGLE':
- return 'ROUNDED_RECTANGLE'
- case 'ELLIPSE':
- return 'ELLIPSE'
- case 'TEXT':
- return 'TEXT'
- case 'LINE':
- return 'LINE'
- case 'STAR':
- return 'STAR'
- case 'REGULAR_POLYGON':
- return 'POLYGON'
- case 'VECTOR':
- return 'VECTOR'
- case 'GROUP':
- return 'GROUP'
- case 'SECTION':
- return 'SECTION'
- case 'COMPONENT':
- return 'COMPONENT'
- case 'COMPONENT_SET':
- return 'COMPONENT_SET'
- case 'INSTANCE':
- return 'INSTANCE'
- case 'SYMBOL':
- return 'COMPONENT'
- case 'CONNECTOR':
- return 'CONNECTOR'
- case 'SHAPE_WITH_TEXT':
- return 'SHAPE_WITH_TEXT'
- default:
- return 'RECTANGLE'
- }
-}
-
-function mapStackMode(mode?: string): LayoutMode {
- switch (mode) {
- case 'HORIZONTAL':
- return 'HORIZONTAL'
- case 'VERTICAL':
- return 'VERTICAL'
- default:
- return 'NONE'
- }
-}
-
-function mapStackSizing(sizing?: string): LayoutSizing {
- switch (sizing) {
- case 'RESIZE_TO_FIT':
- case 'RESIZE_TO_FIT_WITH_IMPLICIT_SIZE':
- return 'HUG'
- case 'FILL':
- return 'FILL'
- default:
- return 'FIXED'
- }
-}
-
-function mapStackJustify(justify?: string): LayoutAlign {
- switch (justify) {
- case 'CENTER':
- return 'CENTER'
- case 'MAX':
- return 'MAX'
- case 'SPACE_BETWEEN':
- case 'SPACE_EVENLY':
- return 'SPACE_BETWEEN'
- default:
- return 'MIN'
- }
-}
-
-function mapStackCounterAlign(align?: string): LayoutCounterAlign {
- switch (align) {
- case 'CENTER':
- return 'CENTER'
- case 'MAX':
- return 'MAX'
- case 'STRETCH':
- return 'STRETCH'
- case 'BASELINE':
- return 'BASELINE'
- default:
- return 'MIN'
- }
-}
-
-function mapConstraint(c?: string): ConstraintType {
- switch (c) {
- case 'CENTER':
- return 'CENTER'
- case 'MAX':
- return 'MAX'
- case 'STRETCH':
- return 'STRETCH'
- case 'SCALE':
- return 'SCALE'
- default:
- return 'MIN'
- }
-}
-
-function mapTextDecoration(d?: string): TextDecoration {
- switch (d) {
- case 'UNDERLINE':
- return 'UNDERLINE'
- case 'STRIKETHROUGH':
- return 'STRIKETHROUGH'
- default:
- return 'NONE'
- }
-}
-
-function mapArcData(data?: Record<string, number>): ArcData | null {
- if (!data) return null
- return {
- startingAngle: data.startingAngle ?? 0,
- endingAngle: data.endingAngle ?? 2 * Math.PI,
- innerRadius: data.innerRadius ?? 0
- }
-}
-
-function importStyleRuns(nc: NodeChange): StyleRun[] {
- const td = nc.textData
- if (!td?.characterStyleIDs || !td.styleOverrideTable) return []
-
- const ids = td.characterStyleIDs
- const table = td.styleOverrideTable
- if (ids.length === 0 || table.length === 0) return []
-
- const styleMap = new Map<number, CharacterStyleOverride>()
- for (const override of table) {
- const id = (override as unknown as Record<string, unknown>).styleID as number | undefined
- if (id === undefined) continue
- const style: CharacterStyleOverride = {}
- if (override.fontName) {
- style.fontFamily = override.fontName.family
- style.fontWeight = styleToWeight(override.fontName.style ?? '')
- style.italic = override.fontName.style?.toLowerCase().includes('italic') ?? false
- }
- if (override.fontSize !== undefined) style.fontSize = override.fontSize
- if (override.letterSpacing) style.letterSpacing = override.letterSpacing.value
- if (override.lineHeight) style.lineHeight = override.lineHeight.value
- const deco = ext(override).textDecoration as string | undefined
- if (deco) style.textDecoration = mapTextDecoration(deco)
- if (Object.keys(style).length > 0) styleMap.set(id, style)
- }
-
- if (styleMap.size === 0) return []
-
- const runs: StyleRun[] = []
- let currentId = ids[0]
- let start = 0
-
- for (let i = 1; i <= ids.length; i++) {
- if (i === ids.length || ids[i] !== currentId) {
- if (currentId !== 0) {
- const style = styleMap.get(currentId)
- if (style) runs.push({ start, length: i - start, style })
- }
- if (i < ids.length) {
- currentId = ids[i]
- start = i
- }
- }
- }
-
- return runs
-}
-
-function resolveVectorNetwork(nc: NodeChange, blobs: Uint8Array[]): VectorNetwork | null {
- const vectorData = (nc as unknown as Record<string, unknown>).vectorData as
- | {
- vectorNetworkBlob?: number
- styleOverrideTable?: Array<{ styleID: number; handleMirroring?: string }>
- }
- | undefined
-
- if (!vectorData || vectorData.vectorNetworkBlob === undefined) return null
- const idx = vectorData.vectorNetworkBlob
- if (idx < 0 || idx >= blobs.length) return null
-
- try {
- return decodeVectorNetworkBlob(blobs[idx], vectorData.styleOverrideTable)
- } catch {
- return null
- }
-}
+import type { NodeChange } from './codec'
export function importNodeChanges(
nodeChanges: NodeChange[],
@@ -403,140 +65,21 @@ export function importNodeChanges(
const nc = changeMap.get(ncId)
if (!nc) return
- const nodeType = mapNodeType(nc.type)
+ const { nodeType, ...props } = nodeChangeToProps(nc, blobs)
if (nodeType === 'DOCUMENT' || nodeType === 'VARIABLE') return
- const x = nc.transform?.m02 ?? 0
- const y = nc.transform?.m12 ?? 0
- const width = nc.size?.x ?? 100
- const height = nc.size?.y ?? 100
-
- let rotation = 0
- let flipX = false
- let flipY = false
- if (nc.transform) {
- const det = nc.transform.m00 * nc.transform.m11 - nc.transform.m01 * nc.transform.m10
- if (det < 0) flipX = true
- const sx = flipX ? -1 : 1
- rotation = Math.atan2(nc.transform.m10 * sx, nc.transform.m00 * sx) * (180 / Math.PI)
- }
-
- const dashPattern = (ext(nc).dashPattern as number[]) ?? []
-
- const node = graph.createNode(nodeType, graphParentId, {
- name: nc.name ?? nodeType,
- x,
- y,
- width,
- height,
- rotation,
- flipX,
- flipY,
- opacity: nc.opacity ?? 1,
- visible: nc.visible ?? true,
- locked: nc.locked ?? false,
- blendMode: (ext(nc).blendMode as Fill['blendMode']) ?? 'PASS_THROUGH',
- fills: convertFills(nc.fillPaints),
- strokes: convertStrokes(
- nc.strokePaints,
- nc.strokeWeight,
- nc.strokeAlign,
- nc.strokeCap,
- nc.strokeJoin,
- dashPattern
- ),
- effects: convertEffects(nc.effects),
- cornerRadius: nc.cornerRadius ?? 0,
- topLeftRadius: nc.rectangleTopLeftCornerRadius ?? nc.cornerRadius ?? 0,
- topRightRadius: nc.rectangleTopRightCornerRadius ?? nc.cornerRadius ?? 0,
- bottomRightRadius: nc.rectangleBottomRightCornerRadius ?? nc.cornerRadius ?? 0,
- bottomLeftRadius: nc.rectangleBottomLeftCornerRadius ?? nc.cornerRadius ?? 0,
- independentCorners: nc.rectangleCornerRadiiIndependent ?? false,
- cornerSmoothing: nc.cornerSmoothing ?? 0,
- text: nc.textData?.characters ?? '',
- fontSize: nc.fontSize ?? 14,
- fontFamily: nc.fontName?.family ?? 'Inter',
- fontWeight: styleToWeight(nc.fontName?.style ?? ''),
- italic: nc.fontName?.style?.toLowerCase().includes('italic') ?? false,
- textAlignHorizontal:
- (nc.textAlignHorizontal as 'LEFT' | 'CENTER' | 'RIGHT' | 'JUSTIFIED') ?? 'LEFT',
- textAlignVertical: (ext(nc).textAlignVertical as TextAlignVertical) ?? 'TOP',
- textAutoResize: (ext(nc).textAutoResize as TextAutoResize) ?? 'NONE',
- textCase: (ext(nc).textCase as TextCase) ?? 'ORIGINAL',
- textDecoration: mapTextDecoration(ext(nc).textDecoration as string),
- lineHeight: nc.lineHeight?.value ?? null,
- letterSpacing: nc.letterSpacing?.value ?? 0,
- maxLines: (ext(nc).maxLines as number) ?? null,
- styleRuns: importStyleRuns(nc),
- horizontalConstraint: mapConstraint(ext(nc).horizontalConstraint as string),
- verticalConstraint: mapConstraint(ext(nc).verticalConstraint as string),
- layoutMode: mapStackMode(nc.stackMode),
- itemSpacing: nc.stackSpacing ?? 0,
- paddingTop: nc.stackVerticalPadding ?? nc.stackPadding ?? 0,
- paddingBottom: nc.stackPaddingBottom ?? nc.stackVerticalPadding ?? nc.stackPadding ?? 0,
- paddingLeft: nc.stackHorizontalPadding ?? nc.stackPadding ?? 0,
- paddingRight: nc.stackPaddingRight ?? nc.stackHorizontalPadding ?? nc.stackPadding ?? 0,
- primaryAxisSizing: mapStackSizing(nc.stackPrimarySizing),
- counterAxisSizing: mapStackSizing(nc.stackCounterSizing),
- primaryAxisAlign: mapStackJustify(nc.stackPrimaryAlignItems ?? nc.stackJustify),
- counterAxisAlign: mapStackCounterAlign(nc.stackCounterAlignItems ?? nc.stackCounterAlign),
- layoutWrap: ext(nc).stackWrap === 'WRAP' ? 'WRAP' : 'NO_WRAP',
- counterAxisSpacing: (ext(nc).stackCounterSpacing as number) ?? 0,
- layoutPositioning: ext(nc).stackPositioning === 'ABSOLUTE' ? 'ABSOLUTE' : 'AUTO',
- layoutGrow: (ext(nc).stackChildPrimaryGrow as number) ?? 0,
- layoutAlignSelf: (ext(nc).stackChildAlignSelf as string) === 'STRETCH' ? 'STRETCH' : 'AUTO',
- vectorNetwork: resolveVectorNetwork(nc, blobs),
- arcData: mapArcData(ext(nc).arcData as Record<string, number> | undefined),
- strokeCap: (nc.strokeCap ?? 'NONE') as StrokeCap,
- strokeJoin: (nc.strokeJoin ?? 'MITER') as StrokeJoin,
- dashPattern,
- borderTopWeight: (ext(nc).borderTopWeight as number) ?? 0,
- borderRightWeight: (ext(nc).borderRightWeight as number) ?? 0,
- borderBottomWeight: (ext(nc).borderBottomWeight as number) ?? 0,
- borderLeftWeight: (ext(nc).borderLeftWeight as number) ?? 0,
- independentStrokeWeights: (ext(nc).borderStrokeWeightsIndependent as boolean) ?? false,
- strokeMiterLimit: DEFAULT_STROKE_MITER_LIMIT,
- minWidth: (ext(nc).minWidth as number) ?? null,
- maxWidth: (ext(nc).maxWidth as number) ?? null,
- minHeight: (ext(nc).minHeight as number) ?? null,
- maxHeight: (ext(nc).maxHeight as number) ?? null,
- isMask: (ext(nc).isMask as boolean) ?? false,
- maskType: ((ext(nc).maskType as string) ?? 'ALPHA') as 'ALPHA' | 'VECTOR' | 'LUMINANCE',
- counterAxisAlignContent:
- (ext(nc).stackCounterAlignContent as string) === 'SPACE_BETWEEN' ? 'SPACE_BETWEEN' : 'AUTO',
- itemReverseZIndex: (ext(nc).stackReverseZIndex as boolean) ?? false,
- strokesIncludedInLayout: (ext(nc).strokesIncludedInLayout as boolean) ?? false,
- expanded: true,
- textTruncation: (ext(nc).textTruncation as string) === 'ENDING' ? 'ENDING' : 'DISABLED',
- autoRename: (ext(nc).autoRename as boolean) ?? true,
- boundVariables: extractBoundVariables(nc)
- })
+ const node = graph.createNode(nodeType, graphParentId, props)
for (const childId of getChildren(ncId)) {
createSceneNode(childId, node.id)
}
}
- function extractBoundVariables(nc: NodeChange): Record<string, string> {
- const bindings: Record<string, string> = {}
- nc.fillPaints?.forEach((paint, i) => {
- if (paint.colorVariableBinding) {
- bindings[`fills/${i}/color`] = guidToString(paint.colorVariableBinding.variableID)
- }
- })
- nc.strokePaints?.forEach((paint, i) => {
- if (paint.colorVariableBinding) {
- bindings[`strokes/${i}/color`] = guidToString(paint.colorVariableBinding.variableID)
- }
- })
- return bindings
- }
-
function importVariables() {
for (const [id, nc] of changeMap) {
if (nc.type !== 'VARIABLE') continue
const varData = (
- ext(nc) as {
+ nc as unknown as {
variableData?: {
value?: { boolValue?: boolean; textValue?: string; floatValue?: number }
dataType?: string
diff --git a/packages/core/src/kiwi/kiwi-convert.ts b/packages/core/src/kiwi/kiwi-convert.ts
new file mode 100644
index 000000000..02ec382eb
--- /dev/null
+++ b/packages/core/src/kiwi/kiwi-convert.ts
@@ -0,0 +1,488 @@
+import { BLACK, DEFAULT_STROKE_MITER_LIMIT } from '../constants'
+import { styleToWeight } from '../fonts'
+import { decodeVectorNetworkBlob } from '../vector'
+
+import type {
+ SceneNode,
+ NodeType,
+ Fill,
+ FillType,
+ Stroke,
+ Effect,
+ Color,
+ BlendMode,
+ ImageScaleMode,
+ GradientTransform,
+ StrokeCap,
+ StrokeJoin,
+ LayoutMode,
+ LayoutSizing,
+ LayoutAlign,
+ LayoutCounterAlign,
+ ConstraintType,
+ TextAutoResize,
+ TextAlignVertical,
+ TextCase,
+ TextDecoration,
+ ArcData,
+ VectorNetwork,
+ StyleRun,
+ CharacterStyleOverride
+} from '../scene-graph'
+import type { NodeChange, Paint, Effect as KiwiEffect, GUID } from './codec'
+
+function ext(nc: NodeChange): Record<string, unknown> {
+ return nc as unknown as Record<string, unknown>
+}
+
+export function guidToString(guid: GUID): string {
+ return `${guid.sessionID}:${guid.localID}`
+}
+
+function convertColor(color?: { r: number; g: number; b: number; a: number }): Color {
+ if (!color) return { ...BLACK }
+ return { r: color.r, g: color.g, b: color.b, a: color.a }
+}
+
+function imageHashToString(hash: Record<string, number>): string {
+ const bytes = Object.keys(hash)
+ .sort((a, b) => Number(a) - Number(b))
+ .map((k) => hash[Number(k)])
+ return bytes.map((b) => b.toString(16).padStart(2, '0')).join('')
+}
+
+function convertGradientTransform(t?: {
+ m00: number
+ m01: number
+ m02: number
+ m10: number
+ m11: number
+ m12: number
+}): GradientTransform | undefined {
+ if (!t) return undefined
+ return { m00: t.m00, m01: t.m01, m02: t.m02, m10: t.m10, m11: t.m11, m12: t.m12 }
+}
+
+export function convertFills(paints?: Paint[]): Fill[] {
+ if (!paints) return []
+ return paints.map((p) => {
+ const base: Fill = {
+ type: (p.type ?? 'SOLID') as FillType,
+ color: convertColor(p.color),
+ opacity: p.opacity ?? 1,
+ visible: p.visible ?? true,
+ blendMode: (p.blendMode ?? 'NORMAL') as BlendMode
+ }
+
+ if (p.type?.startsWith('GRADIENT') && p.stops) {
+ base.gradientStops = p.stops.map((s) => ({
+ color: convertColor(s.color),
+ position: s.position
+ }))
+ if (p.transform) {
+ base.gradientTransform = convertGradientTransform(p.transform)
+ }
+ }
+
+ if (p.type === 'IMAGE') {
+ if (p.image && typeof p.image === 'object') {
+ const img = p.image as { hash: string | Record<string, number> }
+ if (typeof img.hash === 'object') {
+ base.imageHash = imageHashToString(img.hash)
+ } else if (typeof img.hash === 'string') {
+ base.imageHash = img.hash
+ }
+ }
+ base.imageScaleMode = (p.imageScaleMode as ImageScaleMode) ?? 'FILL'
+ if (p.transform) {
+ base.imageTransform = convertGradientTransform(p.transform)
+ }
+ }
+
+ return base
+ })
+}
+
+export function convertStrokes(
+ paints?: Paint[],
+ weight?: number,
+ align?: string,
+ cap?: string,
+ join?: string,
+ dashPattern?: number[]
+): Stroke[] {
+ if (!paints) return []
+ return paints.map((p) => ({
+ color: convertColor(p.color),
+ weight: weight ?? 1,
+ opacity: p.opacity ?? 1,
+ visible: p.visible ?? true,
+ align: (align === 'INSIDE'
+ ? 'INSIDE'
+ : align === 'OUTSIDE'
+ ? 'OUTSIDE'
+ : 'CENTER') as Stroke['align'],
+ cap: (cap ?? 'NONE') as StrokeCap,
+ join: (join ?? 'MITER') as StrokeJoin,
+ dashPattern: dashPattern ?? []
+ }))
+}
+
+export function convertEffects(effects?: KiwiEffect[]): Effect[] {
+ if (!effects) return []
+ return effects.map((e) => ({
+ type: e.type as Effect['type'],
+ color: convertColor(e.color),
+ offset: e.offset ?? { x: 0, y: 0 },
+ radius: e.radius ?? 0,
+ spread: e.spread ?? 0,
+ visible: e.visible ?? true,
+ blendMode: (e.blendMode as BlendMode) ?? 'NORMAL'
+ }))
+}
+
+export function mapNodeType(type?: string): NodeType | 'DOCUMENT' | 'VARIABLE' {
+ switch (type) {
+ case 'DOCUMENT':
+ return 'DOCUMENT'
+ case 'VARIABLE':
+ return 'VARIABLE'
+ case 'CANVAS':
+ return 'CANVAS'
+ case 'FRAME':
+ return 'FRAME'
+ case 'RECTANGLE':
+ return 'RECTANGLE'
+ case 'ROUNDED_RECTANGLE':
+ return 'ROUNDED_RECTANGLE'
+ case 'ELLIPSE':
+ return 'ELLIPSE'
+ case 'TEXT':
+ return 'TEXT'
+ case 'LINE':
+ return 'LINE'
+ case 'STAR':
+ return 'STAR'
+ case 'REGULAR_POLYGON':
+ return 'POLYGON'
+ case 'VECTOR':
+ return 'VECTOR'
+ case 'BOOLEAN_OPERATION':
+ return 'VECTOR'
+ case 'GROUP':
+ return 'GROUP'
+ case 'SECTION':
+ return 'SECTION'
+ case 'COMPONENT':
+ return 'COMPONENT'
+ case 'COMPONENT_SET':
+ return 'COMPONENT_SET'
+ case 'INSTANCE':
+ return 'INSTANCE'
+ case 'SYMBOL':
+ return 'COMPONENT'
+ case 'CONNECTOR':
+ return 'CONNECTOR'
+ case 'SHAPE_WITH_TEXT':
+ return 'SHAPE_WITH_TEXT'
+ default:
+ return 'RECTANGLE'
+ }
+}
+
+export function mapStackMode(mode?: string): LayoutMode {
+ switch (mode) {
+ case 'HORIZONTAL':
+ return 'HORIZONTAL'
+ case 'VERTICAL':
+ return 'VERTICAL'
+ default:
+ return 'NONE'
+ }
+}
+
+export function mapStackSizing(sizing?: string): LayoutSizing {
+ switch (sizing) {
+ case 'RESIZE_TO_FIT':
+ case 'RESIZE_TO_FIT_WITH_IMPLICIT_SIZE':
+ return 'HUG'
+ case 'FILL':
+ return 'FILL'
+ default:
+ return 'FIXED'
+ }
+}
+
+export function mapStackJustify(justify?: string): LayoutAlign {
+ switch (justify) {
+ case 'CENTER':
+ return 'CENTER'
+ case 'MAX':
+ return 'MAX'
+ case 'SPACE_BETWEEN':
+ case 'SPACE_EVENLY':
+ return 'SPACE_BETWEEN'
+ default:
+ return 'MIN'
+ }
+}
+
+export function mapStackCounterAlign(align?: string): LayoutCounterAlign {
+ switch (align) {
+ case 'CENTER':
+ return 'CENTER'
+ case 'MAX':
+ return 'MAX'
+ case 'STRETCH':
+ return 'STRETCH'
+ case 'BASELINE':
+ return 'BASELINE'
+ default:
+ return 'MIN'
+ }
+}
+
+export function mapConstraint(c?: string): ConstraintType {
+ switch (c) {
+ case 'CENTER':
+ return 'CENTER'
+ case 'MAX':
+ return 'MAX'
+ case 'STRETCH':
+ return 'STRETCH'
+ case 'SCALE':
+ return 'SCALE'
+ default:
+ return 'MIN'
+ }
+}
+
+export function mapTextDecoration(d?: string): TextDecoration {
+ switch (d) {
+ case 'UNDERLINE':
+ return 'UNDERLINE'
+ case 'STRIKETHROUGH':
+ return 'STRIKETHROUGH'
+ default:
+ return 'NONE'
+ }
+}
+
+function convertLetterSpacing(
+ ls?: { value: number; units: string },
+ fontSize?: number
+): number {
+ if (!ls) return 0
+ if (ls.units === 'PIXELS') return ls.value
+ if (ls.units === 'PERCENT') return (ls.value / 100) * (fontSize ?? 14)
+ return ls.value
+}
+
+export function mapArcData(data?: Record<string, number>): ArcData | null {
+ if (!data) return null
+ return {
+ startingAngle: data.startingAngle ?? 0,
+ endingAngle: data.endingAngle ?? 2 * Math.PI,
+ innerRadius: data.innerRadius ?? 0
+ }
+}
+
+export function importStyleRuns(nc: NodeChange): StyleRun[] {
+ const td = nc.textData
+ if (!td?.characterStyleIDs || !td.styleOverrideTable) return []
+
+ const ids = td.characterStyleIDs
+ const table = td.styleOverrideTable
+ if (ids.length === 0 || table.length === 0) return []
+
+ const styleMap = new Map<number, CharacterStyleOverride>()
+ for (const override of table) {
+ const id = (override as unknown as Record<string, unknown>).styleID as number | undefined
+ if (id === undefined) continue
+ const style: CharacterStyleOverride = {}
+ if (override.fontName) {
+ style.fontFamily = override.fontName.family
+ style.fontWeight = styleToWeight(override.fontName.style ?? '')
+ style.italic = override.fontName.style?.toLowerCase().includes('italic') ?? false
+ }
+ if (override.fontSize !== undefined) style.fontSize = override.fontSize
+ if (override.letterSpacing) style.letterSpacing = override.letterSpacing.value
+ if (override.lineHeight) style.lineHeight = override.lineHeight.value
+ const deco = ext(override).textDecoration as string | undefined
+ if (deco) style.textDecoration = mapTextDecoration(deco)
+ if (Object.keys(style).length > 0) styleMap.set(id, style)
+ }
+
+ if (styleMap.size === 0) return []
+
+ const runs: StyleRun[] = []
+ let currentId = ids[0]
+ let start = 0
+
+ for (let i = 1; i <= ids.length; i++) {
+ if (i === ids.length || ids[i] !== currentId) {
+ if (currentId !== 0) {
+ const style = styleMap.get(currentId)
+ if (style) runs.push({ start, length: i - start, style })
+ }
+ if (i < ids.length) {
+ currentId = ids[i]
+ start = i
+ }
+ }
+ }
+
+ return runs
+}
+
+export function resolveVectorNetwork(
+ nc: NodeChange,
+ blobs: Uint8Array[]
+): VectorNetwork | null {
+ const vectorData = (nc as unknown as Record<string, unknown>).vectorData as
+ | {
+ vectorNetworkBlob?: number
+ styleOverrideTable?: Array<{ styleID: number; handleMirroring?: string }>
+ }
+ | undefined
+
+ if (!vectorData || vectorData.vectorNetworkBlob === undefined) return null
+ const idx = vectorData.vectorNetworkBlob
+ if (idx < 0 || idx >= blobs.length) return null
+
+ try {
+ return decodeVectorNetworkBlob(blobs[idx], vectorData.styleOverrideTable)
+ } catch {
+ return null
+ }
+}
+
+export function extractBoundVariables(nc: NodeChange): Record<string, string> {
+ const bindings: Record<string, string> = {}
+ nc.fillPaints?.forEach((paint, i) => {
+ if (paint.colorVariableBinding) {
+ bindings[`fills/${i}/color`] = guidToString(paint.colorVariableBinding.variableID)
+ }
+ })
+ nc.strokePaints?.forEach((paint, i) => {
+ if (paint.colorVariableBinding) {
+ bindings[`strokes/${i}/color`] = guidToString(paint.colorVariableBinding.variableID)
+ }
+ })
+ return bindings
+}
+
+export function nodeChangeToProps(
+ nc: NodeChange,
+ blobs: Uint8Array[]
+): Partial<SceneNode> & { nodeType: NodeType | 'DOCUMENT' | 'VARIABLE' } {
+ const nodeType = mapNodeType(nc.type)
+
+ const x = nc.transform?.m02 ?? 0
+ const y = nc.transform?.m12 ?? 0
+ const width = nc.size?.x ?? 100
+ const height = nc.size?.y ?? 100
+
+ let rotation = 0
+ let flipX = false
+ let flipY = false
+ if (nc.transform) {
+ const det = nc.transform.m00 * nc.transform.m11 - nc.transform.m01 * nc.transform.m10
+ if (det < 0) flipX = true
+ const sx = flipX ? -1 : 1
+ rotation = Math.atan2(nc.transform.m10 * sx, nc.transform.m00 * sx) * (180 / Math.PI)
+ }
+
+ const dashPattern = (ext(nc).dashPattern as number[]) ?? []
+
+ return {
+ nodeType,
+ name: nc.name ?? nodeType,
+ x,
+ y,
+ width,
+ height,
+ rotation,
+ flipX,
+ flipY,
+ opacity: nc.opacity ?? 1,
+ visible: nc.visible ?? true,
+ locked: nc.locked ?? false,
+ blendMode: (ext(nc).blendMode as Fill['blendMode']) ?? 'PASS_THROUGH',
+ fills: convertFills(nc.fillPaints),
+ strokes: convertStrokes(
+ nc.strokePaints,
+ nc.strokeWeight,
+ nc.strokeAlign,
+ nc.strokeCap,
+ nc.strokeJoin,
+ dashPattern
+ ),
+ effects: convertEffects(nc.effects),
+ cornerRadius: nc.cornerRadius ?? 0,
+ topLeftRadius: nc.rectangleTopLeftCornerRadius ?? nc.cornerRadius ?? 0,
+ topRightRadius: nc.rectangleTopRightCornerRadius ?? nc.cornerRadius ?? 0,
+ bottomRightRadius: nc.rectangleBottomRightCornerRadius ?? nc.cornerRadius ?? 0,
+ bottomLeftRadius: nc.rectangleBottomLeftCornerRadius ?? nc.cornerRadius ?? 0,
+ independentCorners: nc.rectangleCornerRadiiIndependent ?? false,
+ cornerSmoothing: nc.cornerSmoothing ?? 0,
+ text: nc.textData?.characters ?? '',
+ fontSize: nc.fontSize ?? 14,
+ fontFamily: nc.fontName?.family ?? 'Inter',
+ fontWeight: styleToWeight(nc.fontName?.style ?? ''),
+ italic: nc.fontName?.style?.toLowerCase().includes('italic') ?? false,
+ textAlignHorizontal:
+ (nc.textAlignHorizontal as 'LEFT' | 'CENTER' | 'RIGHT' | 'JUSTIFIED') ?? 'LEFT',
+ textAlignVertical: (ext(nc).textAlignVertical as TextAlignVertical) ?? 'TOP',
+ textAutoResize: (ext(nc).textAutoResize as TextAutoResize) ?? 'NONE',
+ textCase: (ext(nc).textCase as TextCase) ?? 'ORIGINAL',
+ textDecoration: mapTextDecoration(ext(nc).textDecoration as string),
+ lineHeight: nc.lineHeight?.value ?? null,
+ letterSpacing: convertLetterSpacing(nc.letterSpacing, nc.fontSize),
+ maxLines: (ext(nc).maxLines as number) ?? null,
+ styleRuns: importStyleRuns(nc),
+ horizontalConstraint: mapConstraint(ext(nc).horizontalConstraint as string),
+ verticalConstraint: mapConstraint(ext(nc).verticalConstraint as string),
+ layoutMode: mapStackMode(nc.stackMode),
+ itemSpacing: nc.stackSpacing ?? 0,
+ paddingTop: nc.stackVerticalPadding ?? nc.stackPadding ?? 0,
+ paddingBottom: nc.stackPaddingBottom ?? nc.stackVerticalPadding ?? nc.stackPadding ?? 0,
+ paddingLeft: nc.stackHorizontalPadding ?? nc.stackPadding ?? 0,
+ paddingRight: nc.stackPaddingRight ?? nc.stackHorizontalPadding ?? nc.stackPadding ?? 0,
+ primaryAxisSizing: mapStackSizing(nc.stackPrimarySizing),
+ counterAxisSizing: mapStackSizing(nc.stackCounterSizing),
+ primaryAxisAlign: mapStackJustify(nc.stackPrimaryAlignItems ?? nc.stackJustify),
+ counterAxisAlign: mapStackCounterAlign(nc.stackCounterAlignItems ?? nc.stackCounterAlign),
+ layoutWrap: ext(nc).stackWrap === 'WRAP' ? 'WRAP' : 'NO_WRAP',
+ counterAxisSpacing: (ext(nc).stackCounterSpacing as number) ?? 0,
+ layoutPositioning: ext(nc).stackPositioning === 'ABSOLUTE' ? 'ABSOLUTE' : 'AUTO',
+ layoutGrow: (ext(nc).stackChildPrimaryGrow as number) ?? 0,
+ layoutAlignSelf: (ext(nc).stackChildAlignSelf as string) === 'STRETCH' ? 'STRETCH' : 'AUTO',
+ vectorNetwork: resolveVectorNetwork(nc, blobs),
+ arcData: mapArcData(ext(nc).arcData as Record<string, number> | undefined),
+ strokeCap: (nc.strokeCap ?? 'NONE') as StrokeCap,
+ strokeJoin: (nc.strokeJoin ?? 'MITER') as StrokeJoin,
+ dashPattern,
+ borderTopWeight: (ext(nc).borderTopWeight as number) ?? 0,
+ borderRightWeight: (ext(nc).borderRightWeight as number) ?? 0,
+ borderBottomWeight: (ext(nc).borderBottomWeight as number) ?? 0,
+ borderLeftWeight: (ext(nc).borderLeftWeight as number) ?? 0,
+ independentStrokeWeights: (ext(nc).borderStrokeWeightsIndependent as boolean) ?? false,
+ strokeMiterLimit: DEFAULT_STROKE_MITER_LIMIT,
+ minWidth: (ext(nc).minWidth as number) ?? null,
+ maxWidth: (ext(nc).maxWidth as number) ?? null,
+ minHeight: (ext(nc).minHeight as number) ?? null,
+ maxHeight: (ext(nc).maxHeight as number) ?? null,
+ isMask: (ext(nc).isMask as boolean) ?? false,
+ maskType: ((ext(nc).maskType as string) ?? 'ALPHA') as 'ALPHA' | 'VECTOR' | 'LUMINANCE',
+ counterAxisAlignContent:
+ (ext(nc).stackCounterAlignContent as string) === 'SPACE_BETWEEN' ? 'SPACE_BETWEEN' : 'AUTO',
+ itemReverseZIndex: (ext(nc).stackReverseZIndex as boolean) ?? false,
+ strokesIncludedInLayout: (ext(nc).strokesIncludedInLayout as boolean) ?? false,
+ expanded: true,
+ textTruncation: (ext(nc).textTruncation as string) === 'ENDING' ? 'ENDING' : 'DISABLED',
+ autoRename: (ext(nc).autoRename as boolean) ?? true,
+ boundVariables: extractBoundVariables(nc),
+ clipsContent: nc.frameMaskDisabled === false
+ }
+}
diff --git a/tests/engine/clipboard.test.ts b/tests/engine/clipboard.test.ts
index 036fa457c..ac38ed40e 100644
--- a/tests/engine/clipboard.test.ts
+++ b/tests/engine/clipboard.test.ts
@@ -200,6 +200,80 @@ describe('importClipboardNodes', () => {
expect(graph.getNode(created[2])!.letterSpacing).toBe(0)
})
+ it('maps SYMBOL type to COMPONENT with auto-layout', () => {
+ const { graph, pageId } = createGraphWithPage()
+
+ const nodeChanges = [
+ { guid: { sessionID: 0, localID: 0 }, type: 'DOCUMENT', name: 'Doc' },
+ { guid: { sessionID: 0, localID: 1 }, parentIndex: { guid: { sessionID: 0, localID: 0 }, position: '!' }, type: 'CANVAS', name: 'Page' },
+ {
+ guid: { sessionID: 0, localID: 10 },
+ parentIndex: { guid: { sessionID: 0, localID: 1 }, position: '!' },
+ type: 'SYMBOL',
+ name: 'Dialog/Form',
+ size: { x: 452, y: 299 },
+ transform: { m00: 1, m01: 0, m02: 0, m10: 0, m11: 1, m12: 0 },
+ stackMode: 'VERTICAL',
+ stackSpacing: 16,
+ stackVerticalPadding: 24,
+ stackHorizontalPadding: 24,
+ stackPrimarySizing: 'RESIZE_TO_FIT',
+ stackCounterSizing: 'RESIZE_TO_FIT',
+ },
+ {
+ guid: { sessionID: 0, localID: 11 },
+ parentIndex: { guid: { sessionID: 0, localID: 10 }, position: '!' },
+ type: 'TEXT',
+ name: 'Title',
+ size: { x: 404, y: 32 },
+ transform: { m00: 1, m01: 0, m02: 24, m10: 0, m11: 1, m12: 24 },
+ textData: { characters: 'Hello' },
+ fontSize: 24,
+ fontWeight: 700,
+ },
+ {
+ guid: { sessionID: 0, localID: 12 },
+ parentIndex: { guid: { sessionID: 0, localID: 10 }, position: '"' },
+ type: 'RECTANGLE',
+ name: 'Divider',
+ size: { x: 404, y: 1 },
+ transform: { m00: 1, m01: 0, m02: 24, m10: 0, m11: 1, m12: 72 },
+ },
+ ] as any[]
+
+ const created = importClipboardNodes(nodeChanges, graph, pageId)
+ expect(created).toHaveLength(1)
+
+ const component = graph.getNode(created[0])!
+ expect(component.type).toBe('COMPONENT')
+ expect(component.layoutMode).toBe('VERTICAL')
+ expect(component.itemSpacing).toBe(16)
+ expect(component.primaryAxisSizing).toBe('HUG')
+ expect(component.counterAxisSizing).toBe('HUG')
+
+ const children = graph.getChildren(component.id)
+ expect(children).toHaveLength(2)
+ expect(children[0].name).toBe('Title')
+ expect(children[1].name).toBe('Divider')
+ })
+
+ it('imports textAutoResize from clipboard data', () => {
+ const { graph, pageId } = createGraphWithPage()
+
+ const nodeChanges = [
+ { guid: { sessionID: 0, localID: 0 }, type: 'DOCUMENT', name: 'Doc' },
+ { guid: { sessionID: 0, localID: 1 }, parentIndex: { guid: { sessionID: 0, localID: 0 }, position: '!' }, type: 'CANVAS', name: 'Page' },
+ { guid: { sessionID: 0, localID: 10 }, parentIndex: { guid: { sessionID: 0, localID: 1 }, position: '!' }, type: 'TEXT', name: 'AutoHeight', size: { x: 200, y: 24 }, transform: { m00: 1, m01: 0, m02: 0, m10: 0, m11: 1, m12: 0 }, textData: { characters: 'Hello' }, fontSize: 16, textAutoResize: 'HEIGHT' },
+ { guid: { sessionID: 0, localID: 11 }, parentIndex: { guid: { sessionID: 0, localID: 1 }, position: '"' }, type: 'TEXT', name: 'AutoBoth', size: { x: 100, y: 24 }, transform: { m00: 1, m01: 0, m02: 0, m10: 0, m11: 1, m12: 30 }, textData: { characters: 'World' }, fontSize: 16, textAutoResize: 'WIDTH_AND_HEIGHT' },
+ { guid: { sessionID: 0, localID: 12 }, parentIndex: { guid: { sessionID: 0, localID: 1 }, position: '#' }, type: 'TEXT', name: 'Fixed', size: { x: 100, y: 24 }, transform: { m00: 1, m01: 0, m02: 0, m10: 0, m11: 1, m12: 60 }, textData: { characters: 'Fixed' }, fontSize: 16 },
+ ] as any[]
+
+ const created = importClipboardNodes(nodeChanges, graph, pageId)
+ expect(graph.getNode(created[0])!.textAutoResize).toBe('HEIGHT')
+ expect(graph.getNode(created[1])!.textAutoResize).toBe('WIDTH_AND_HEIGHT')
+ expect(graph.getNode(created[2])!.textAutoResize).toBe('NONE')
+ })
+
it('undo removes all imported nodes including children', () => {
const { graph, pageId } = createGraphWithPage()