AI-native design editor. Open-source Figma alternative.
refactor(fig): split component prop refs
Danila Poyarkov4a052c574db1
diff --git a/packages/core/src/kiwi/instance-overrides/component-props/refs.ts b/packages/core/src/kiwi/instance-overrides/component-props/refs.tsnew file mode 100644index 000000000..115a4b58d--- /dev/null+++ b/packages/core/src/kiwi/instance-overrides/component-props/refs.ts@@ -0,0 +1,48 @@+import { guidToString } from '#core/kiwi/node-change/convert'++import type { ComponentPropRef, ComponentPropValue, OverrideContext } from '#core/kiwi/instance-overrides/types'++import { normalizePropName, stringToGuidParts } from './values'++export function findPropRefs(+ ctx: OverrideContext,+ nodeId: string,+ propRefsMap: Map<string, ComponentPropRef[]>+): ComponentPropRef[] | undefined {+ let sourceId: string | undefined = nodeId+ for (let depth = 0; sourceId && depth < 10; depth++) {+ const figmaId = ctx.nodeIdToGuid.get(sourceId)+ if (figmaId) {+ const refs = propRefsMap.get(figmaId)+ if (refs) return refs+ }+ const node = ctx.graph.getNode(sourceId)+ const nextId = node?.componentId ?? undefined+ if (nextId === sourceId) break+ sourceId = nextId+ }+ return undefined+}++export function fallbackRefsForChild(+ ctx: OverrideContext,+ childName: string,+ valueByDef: Map<string, ComponentPropValue>+): ComponentPropRef[] | undefined {+ const normalizedChildName = normalizePropName(childName)+ const refs: ComponentPropRef[] = []+ for (const defId of valueByDef.keys()) {+ const propName = ctx.propNames.get(defId)+ if (propName && normalizePropName(propName) === normalizedChildName) {+ refs.push({ defID: stringToGuidParts(defId), componentPropNodeField: 'VISIBLE' })+ }+ }+ return refs.length > 0 ? refs : undefined+}++export function valueForRef(+ ref: ComponentPropRef,+ valueByDef: Map<string, ComponentPropValue>+): ComponentPropValue | undefined {+ return ref.defID ? valueByDef.get(guidToString(ref.defID)) : undefined+}diff --git a/packages/core/src/kiwi/instance-overrides/props.ts b/packages/core/src/kiwi/instance-overrides/props.tsindex 042cdc436..682ebfe6e 100644--- a/packages/core/src/kiwi/instance-overrides/props.ts+++ b/packages/core/src/kiwi/instance-overrides/props.ts@@ -2,12 +2,8 @@ import { applyOverridePatch } from '#core/kiwi/instance-overrides/patches'import { guidToString } from '#core/kiwi/node-change/convert'import { collectAssignmentsMap, collectPropRefsMap } from './component-props/maps'-import {- assignmentsToValueMap,- normalizePropName,- propTextCharacters,- stringToGuidParts-} from './component-props/values'+import { fallbackRefsForChild, findPropRefs, valueForRef } from './component-props/refs'+import { assignmentsToValueMap, propTextCharacters } from './component-props/values'import { getComponentRoot, resolveOverrideTarget } from './resolve'import type {OverrideContext,@@ -16,50 +12,6 @@ import type {ComponentPropValue} from './types'-/**- * Walk the componentId chain to find componentPropRefs for a node.- * The refs may be defined on the component several levels up.- */-function findPropRefs(- ctx: OverrideContext,- nodeId: string,- propRefsMap: Map<string, ComponentPropRef[]>-): ComponentPropRef[] | undefined {- let sourceId: string | undefined = nodeId- for (let depth = 0; sourceId && depth < 10; depth++) {- const figmaId = ctx.nodeIdToGuid.get(sourceId)- if (figmaId) {- const refs = propRefsMap.get(figmaId)- if (refs) return refs- }- const node = ctx.graph.getNode(sourceId)- const nextId = node?.componentId ?? undefined- if (nextId === sourceId) break- sourceId = nextId- }- return undefined-}--/**- * Recursively apply prop assignments to children of a parent node.- * Handles VISIBLE toggles and OVERRIDDEN_SYMBOL_ID (instance swap).- */-function fallbackRefsForChild(- ctx: OverrideContext,- childName: string,- valueByDef: Map<string, ComponentPropValue>-): ComponentPropRef[] | undefined {- const normalizedChildName = normalizePropName(childName)- const refs: ComponentPropRef[] = []- for (const defId of valueByDef.keys()) {- const propName = ctx.propNames.get(defId)- if (propName && normalizePropName(propName) === normalizedChildName) {- refs.push({ defID: stringToGuidParts(defId), componentPropNodeField: 'VISIBLE' })- }- }- return refs.length > 0 ? refs : undefined-}-function applyPatchAndMark(ctx: OverrideContext,childId: string,@@ -151,8 +103,7 @@ function applyChildPropRefs(): void {if (!refs) returnfor (const ref of refs) {- if (!ref.defID) continue- const val = valueByDef.get(guidToString(ref.defID))+ const val = valueForRef(ref, valueByDef)if (val) applyComponentPropRef(ctx, childId, ref, val, modified)}}