Skip to content

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

Apply symbol overrides to cloned instance children on .fig import

Danila Poyarkov9d35b3a9b509
diff --git a/packages/core/src/kiwi/fig-import.ts b/packages/core/src/kiwi/fig-import.ts
index 035dcdcbc..dcf516a63 100644
--- a/packages/core/src/kiwi/fig-import.ts
+++ b/packages/core/src/kiwi/fig-import.ts
@@ -1,8 +1,18 @@
import { SceneGraph } from '../scene-graph'
-import { guidToString, nodeChangeToProps, sortChildren } from './kiwi-convert'
+import { guidToString, nodeChangeToProps, convertOverrideToProps, sortChildren } from './kiwi-convert'
-import type { NodeChange } from './codec'
+import type { NodeChange, GUID } from './codec'
+
+interface SymbolOverride {
+ guidPath?: { guids?: GUID[] }
+ [key: string]: unknown
+}
+
+interface SymbolData {
+ symbolID?: GUID
+ symbolOverrides?: SymbolOverride[]
+}
export function importNodeChanges(
nodeChanges: NodeChange[],
@@ -193,6 +203,77 @@ export function importNodeChanges(
}
}
+ // Apply symbol overrides to instance children.
+ // Each override has a guidPath (chain of original Figma GUIDs) targeting
+ // a descendant. We resolve it by walking cloned children via componentId.
+ // Build a set of all node IDs that are transitively cloned from a given
+ // component node. componentId chains: clone → source → source's source → ...
+ // We cache the root of each chain for fast lookup.
+ const componentIdRoot = new Map<string, string>()
+ function getComponentRoot(nodeId: string): string {
+ if (componentIdRoot.has(nodeId)) return componentIdRoot.get(nodeId) ?? nodeId
+ const node = graph.getNode(nodeId)
+ if (!node?.componentId) {
+ componentIdRoot.set(nodeId, nodeId)
+ return nodeId
+ }
+ const root = getComponentRoot(node.componentId)
+ componentIdRoot.set(nodeId, root)
+ return root
+ }
+
+ function findDescendantByComponentId(parentId: string, componentId: string): string | null {
+ const targetRoot = getComponentRoot(componentId)
+ const parent = graph.getNode(parentId)
+ if (!parent) return null
+ for (const childId of parent.childIds) {
+ const child = graph.getNode(childId)
+ if (!child) continue
+ if (child.componentId && getComponentRoot(child.componentId) === targetRoot) return childId
+ const deep = findDescendantByComponentId(childId, componentId)
+ if (deep) return deep
+ }
+ return null
+ }
+
+ function resolveOverrideTarget(instanceId: string, guids: GUID[]): string | null {
+ let currentId = instanceId
+ for (const guid of guids) {
+ const figmaId = guidToString(guid)
+ const remapped = guidToNodeId.get(figmaId)
+ if (!remapped) return null
+ const found = findDescendantByComponentId(currentId, remapped)
+ if (!found) return null
+ currentId = found
+ }
+ return currentId
+ }
+
+ for (const [ncId, nc] of changeMap) {
+ if (nc.type !== 'INSTANCE') continue
+ const sd = (nc as unknown as Record<string, unknown>).symbolData as SymbolData | undefined
+ if (!sd?.symbolOverrides?.length) continue
+
+ const nodeId = guidToNodeId.get(ncId)
+ if (!nodeId) continue
+
+ for (const ov of sd.symbolOverrides) {
+ const guids = ov.guidPath?.guids
+ if (!guids?.length) continue
+
+ const targetId = resolveOverrideTarget(nodeId, guids)
+ if (!targetId) continue
+
+ const { guidPath: _, ...fields } = ov
+ if (Object.keys(fields).length === 0) continue
+
+ const updates = convertOverrideToProps(fields)
+ if (Object.keys(updates).length > 0) {
+ graph.updateNode(targetId, updates)
+ }
+ }
+ }
+
// Ensure at least one page exists
if (graph.getPages(true).length === 0) {
graph.addPage('Page 1')
diff --git a/packages/core/src/kiwi/kiwi-convert.ts b/packages/core/src/kiwi/kiwi-convert.ts
index 0167cdcbe..1a2ca5fe4 100644
--- a/packages/core/src/kiwi/kiwi-convert.ts
+++ b/packages/core/src/kiwi/kiwi-convert.ts
@@ -557,3 +557,122 @@ function extractSymbolId(nc: NodeChange): string {
if (!sd?.symbolID) return ''
return guidToString(sd.symbolID)
}
+
+export function convertOverrideToProps(ov: Record<string, unknown>): Partial<SceneNode> {
+ const updates: Partial<SceneNode> = {}
+
+ if (ov.textData != null) {
+ const td = ov.textData as { characters?: string }
+ if (td.characters != null) updates.text = td.characters
+ const runs = importStyleRuns(ov as unknown as NodeChange)
+ if (runs.length > 0) updates.styleRuns = runs
+ }
+ if (ov.fillPaints != null) updates.fills = convertFills(ov.fillPaints as Paint[])
+ if (ov.strokePaints != null)
+ updates.strokes = convertStrokes(
+ ov.strokePaints as Paint[],
+ ov.strokeWeight as number | undefined,
+ ov.strokeAlign as string | undefined
+ )
+ if (ov.effects != null) updates.effects = convertEffects(ov.effects as KiwiEffect[])
+ if (ov.visible != null) updates.visible = ov.visible as boolean
+ if (ov.opacity != null) updates.opacity = ov.opacity as number
+ if (ov.name != null) updates.name = ov.name as string
+ if (ov.locked != null) updates.locked = ov.locked as boolean
+
+ if (ov.size != null) {
+ const sz = ov.size as { x?: number; y?: number }
+ if (sz.x != null) updates.width = sz.x
+ if (sz.y != null) updates.height = sz.y
+ }
+
+ if (ov.cornerRadius != null) updates.cornerRadius = ov.cornerRadius as number
+ if (ov.rectangleTopLeftCornerRadius != null)
+ updates.topLeftRadius = ov.rectangleTopLeftCornerRadius as number
+ if (ov.rectangleTopRightCornerRadius != null)
+ updates.topRightRadius = ov.rectangleTopRightCornerRadius as number
+ if (ov.rectangleBottomRightCornerRadius != null)
+ updates.bottomRightRadius = ov.rectangleBottomRightCornerRadius as number
+ if (ov.rectangleBottomLeftCornerRadius != null)
+ updates.bottomLeftRadius = ov.rectangleBottomLeftCornerRadius as number
+ if (ov.rectangleCornerRadiiIndependent != null)
+ updates.independentCorners = ov.rectangleCornerRadiiIndependent as boolean
+
+ if (ov.stackSpacing != null) updates.itemSpacing = ov.stackSpacing as number
+ if (ov.stackPrimarySizing != null)
+ updates.primaryAxisSizing = mapStackSizing(ov.stackPrimarySizing as string)
+ if (ov.stackCounterSizing != null)
+ updates.counterAxisSizing = mapStackSizing(ov.stackCounterSizing as string)
+ if (ov.stackPrimaryAlignItems != null)
+ updates.primaryAxisAlign = mapStackJustify(ov.stackPrimaryAlignItems as string)
+ if (ov.stackCounterAlignItems != null)
+ updates.counterAxisAlign = mapStackCounterAlign(ov.stackCounterAlignItems as string)
+ if (ov.stackChildPrimaryGrow != null) updates.layoutGrow = ov.stackChildPrimaryGrow as number
+ if (ov.stackChildAlignSelf != null)
+ updates.layoutAlignSelf =
+ (ov.stackChildAlignSelf as string) === 'STRETCH' ? 'STRETCH' : 'AUTO'
+ if (ov.stackPositioning != null)
+ updates.layoutPositioning =
+ (ov.stackPositioning as string) === 'ABSOLUTE' ? 'ABSOLUTE' : 'AUTO'
+ if (ov.stackVerticalPadding != null) {
+ updates.paddingTop = ov.stackVerticalPadding as number
+ if (ov.stackPaddingBottom == null) updates.paddingBottom = ov.stackVerticalPadding as number
+ }
+ if (ov.stackHorizontalPadding != null) {
+ updates.paddingLeft = ov.stackHorizontalPadding as number
+ if (ov.stackPaddingRight == null) updates.paddingRight = ov.stackHorizontalPadding as number
+ }
+ if (ov.stackPaddingBottom != null) updates.paddingBottom = ov.stackPaddingBottom as number
+ if (ov.stackPaddingRight != null) updates.paddingRight = ov.stackPaddingRight as number
+
+ if (ov.strokeWeight != null && !ov.strokePaints) {
+ updates.strokes = updates.strokes ?? []
+ }
+ if (ov.strokeAlign != null && updates.strokes) {
+ const align = (
+ ov.strokeAlign === 'INSIDE' ? 'INSIDE' : ov.strokeAlign === 'OUTSIDE' ? 'OUTSIDE' : 'CENTER'
+ ) as Stroke['align']
+ for (const s of updates.strokes) s.align = align
+ }
+ if (ov.borderTopWeight != null) updates.borderTopWeight = ov.borderTopWeight as number
+ if (ov.borderRightWeight != null) updates.borderRightWeight = ov.borderRightWeight as number
+ if (ov.borderBottomWeight != null) updates.borderBottomWeight = ov.borderBottomWeight as number
+ if (ov.borderLeftWeight != null) updates.borderLeftWeight = ov.borderLeftWeight as number
+ if (ov.borderStrokeWeightsIndependent != null)
+ updates.independentStrokeWeights = ov.borderStrokeWeightsIndependent as boolean
+
+ if (ov.fontName != null) {
+ const fn = ov.fontName as { family?: string; style?: string }
+ if (fn.family) updates.fontFamily = fn.family
+ if (fn.style) {
+ updates.fontWeight = styleToWeight(fn.style)
+ updates.italic = fn.style.toLowerCase().includes('italic')
+ }
+ }
+ if (ov.fontSize != null) updates.fontSize = ov.fontSize as number
+ if (ov.textAlignHorizontal != null)
+ updates.textAlignHorizontal = ov.textAlignHorizontal as SceneNode['textAlignHorizontal']
+ if (ov.textAutoResize != null) updates.textAutoResize = ov.textAutoResize as TextAutoResize
+ if (ov.lineHeight != null)
+ updates.lineHeight = convertLineHeight(
+ ov.lineHeight as NodeChange['lineHeight'],
+ ov.fontSize as number | undefined
+ )
+ if (ov.letterSpacing != null)
+ updates.letterSpacing = convertLetterSpacing(
+ ov.letterSpacing as NodeChange['letterSpacing'],
+ ov.fontSize as number | undefined
+ )
+ if (ov.maxLines != null) updates.maxLines = ov.maxLines as number | null
+ if (ov.textTruncation != null)
+ updates.textTruncation =
+ (ov.textTruncation as string) === 'ENDING' ? 'ENDING' : 'DISABLED'
+ if (ov.textDecoration != null)
+ updates.textDecoration = mapTextDecoration(ov.textDecoration as string)
+
+ if (ov.arcData != null)
+ updates.arcData = mapArcData(ov.arcData as Record<string, number> | undefined)
+ if (ov.frameMaskDisabled != null) updates.clipsContent = ov.frameMaskDisabled === false
+
+ return updates
+}