AI-native design editor. Open-source Figma alternative.
refactor(fig): simplify override prop application
Danila Poyarkov170db518e9f1
diff --git a/packages/core/src/kiwi/instance-overrides/props.ts b/packages/core/src/kiwi/instance-overrides/props.tsindex 74427a53c..e0b531bcf 100644--- a/packages/core/src/kiwi/instance-overrides/props.ts+++ b/packages/core/src/kiwi/instance-overrides/props.ts@@ -1,3 +1,4 @@+import type { GUID } from '#core/kiwi/binary/codec'import { applyOverridePatch } from '#core/kiwi/instance-overrides/patches'import { guidToString } from '#core/kiwi/node-change/convert'@@ -94,6 +95,11 @@ function assignmentsToValueMap(* Recursively apply prop assignments to children of a parent node.* Handles VISIBLE toggles and OVERRIDDEN_SYMBOL_ID (instance swap).*/+function stringToGuidParts(value: string): GUID {+ const [sessionID, localID] = value.split(':').map(Number)+ return { sessionID, localID }+}+function fallbackRefsForChild(ctx: OverrideContext,childName: string,@@ -104,43 +110,92 @@ function fallbackRefsForChild(for (const defId of valueByDef.keys()) {const propName = ctx.propNames.get(defId)if (propName && normalizePropName(propName) === normalizedChildName) {- refs.push({- defID: { sessionID: Number(defId.split(':')[0]), localID: Number(defId.split(':')[1]) },- componentPropNodeField: 'VISIBLE'- })+ refs.push({ defID: stringToGuidParts(defId), componentPropNodeField: 'VISIBLE' })}}return refs.length > 0 ? refs : undefined}-function applyComponentPropRef(+function applyPatchAndMark(ctx: OverrideContext,childId: string,- ref: ComponentPropRef,- val: ComponentPropValue,+ patch: Parameters<typeof applyOverridePatch>[1],modified?: Set<string>): void {- const child = ctx.graph.getNode(childId)- if (!child) return+ if (applyOverridePatch(ctx, patch)) modified?.add(childId)+}- if (ref.componentPropNodeField === 'VISIBLE' && val.boolValue !== undefined) {- if (applyOverridePatch(ctx, { targetId: childId, source: 'component-prop', props: { visible: val.boolValue } })) modified?.add(childId)- return- }+function applyVisibleProp(+ ctx: OverrideContext,+ childId: string,+ val: ComponentPropValue,+ modified?: Set<string>+): void {+ if (val.boolValue === undefined) return+ applyPatchAndMark(+ ctx,+ childId,+ { targetId: childId, source: 'component-prop', props: { visible: val.boolValue } },+ modified+ )+}- if (ref.componentPropNodeField === 'TEXT_DATA') {- const text = propTextCharacters(val)- if (text === undefined || child.type !== 'TEXT') return- if (applyOverridePatch(ctx, { targetId: childId, source: 'component-prop', props: { text } })) modified?.add(childId)- return- }+function applyTextProp(+ ctx: OverrideContext,+ childId: string,+ val: ComponentPropValue,+ modified?: Set<string>+): void {+ const child = ctx.graph.getNode(childId)+ const text = propTextCharacters(val)+ if (text === undefined || child?.type !== 'TEXT') return+ applyPatchAndMark(+ ctx,+ childId,+ { targetId: childId, source: 'component-prop', props: { text } },+ modified+ )+}- if (ref.componentPropNodeField !== 'OVERRIDDEN_SYMBOL_ID') return+function applySwapProp(+ ctx: OverrideContext,+ childId: string,+ val: ComponentPropValue,+ modified?: Set<string>+): void {const swapId = propTextCharacters(val) ?? (val.guidValue ? guidToString(val.guidValue) : undefined)- if (!swapId) return- const newCompId = ctx.guidToNodeId.get(swapId)+ const newCompId = swapId ? ctx.guidToNodeId.get(swapId) : undefinedif (!newCompId) return- if (applyOverridePatch(ctx, { targetId: childId, source: 'component-prop', swapComponentId: getComponentRoot(ctx, newCompId) })) modified?.add(childId)+ applyPatchAndMark(+ ctx,+ childId,+ {+ targetId: childId,+ source: 'component-prop',+ swapComponentId: getComponentRoot(ctx, newCompId)+ },+ modified+ )+}++function applyComponentPropRef(+ ctx: OverrideContext,+ childId: string,+ ref: ComponentPropRef,+ val: ComponentPropValue,+ modified?: Set<string>+): void {+ switch (ref.componentPropNodeField) {+ case 'VISIBLE':+ applyVisibleProp(ctx, childId, val, modified)+ break+ case 'TEXT_DATA':+ applyTextProp(ctx, childId, val, modified)+ break+ case 'OVERRIDDEN_SYMBOL_ID':+ applySwapProp(ctx, childId, val, modified)+ break+ }}function applyChildPropRefs(@@ -257,25 +312,28 @@ function applyOverrideAssignments(* Returns the set of modified node IDs so the caller can run a second* transitive sync to propagate the changes to deeper clones.*/-export function applyComponentProperties(ctx: OverrideContext): Set<string> {- const modified = new Set<string>()-- const propRefsMap = new Map<string, ComponentPropRef[]>()+function collectPropRefsMap(ctx: OverrideContext): Map<string, ComponentPropRef[]> {+ const result = new Map<string, ComponentPropRef[]>()for (const [figmaId, nc] of ctx.changeMap) {- if (nc.componentPropRefs?.length) {- propRefsMap.set(figmaId, nc.componentPropRefs)- }+ if (nc.componentPropRefs?.length) result.set(figmaId, nc.componentPropRefs)}- if (propRefsMap.size === 0) return modified+ return result+}- const assignmentSources = new Map<string, ComponentPropAssignment[]>()+function collectAssignmentsMap(ctx: OverrideContext): Map<string, ComponentPropAssignment[]> {+ const result = new Map<string, ComponentPropAssignment[]>()for (const [figmaId, nc] of ctx.changeMap) {- if (nc.componentPropAssignments?.length) {- assignmentSources.set(figmaId, nc.componentPropAssignments)- }+ if (nc.componentPropAssignments?.length) result.set(figmaId, nc.componentPropAssignments)}+ return result+}++export function applyComponentProperties(ctx: OverrideContext): Set<string> {+ const modified = new Set<string>()+ const propRefsMap = collectPropRefsMap(ctx)+ if (propRefsMap.size === 0) return modified- applyInstanceDirectAssignments(ctx, assignmentSources, propRefsMap, modified)+ applyInstanceDirectAssignments(ctx, collectAssignmentsMap(ctx), propRefsMap, modified)applyOverrideAssignments(ctx, propRefsMap, modified)return modified